What is the primary purpose of the if __name__ == "__main__": statement in a Python script?
Correct answer
C. Execute code when the script is run directly
15 views
C. Execute code when the script is run directly
15 views
It executes code only when the script is run directly. When a file is run as a program, Python sets __name__ to "__main__"; when the same file is imported as a module, __name__ is the module's name, so the block is skipped.
This lets one file work both as a reusable module and as a standalone script.
For more solved questions like this one, see Computer Science MCQs on MCQsExam.com.
Not sure why the answer is C? Ask here and get an explanation.