1. Home
  2. Data Science
  3. Python
  4. What is the primary purpose of the if __name__ ==…

Added 25 Jan 2024

What is the primary purpose of the if __name__ == "__main__": statement in a Python script?

  1. Declare variables
  2. Define functions
  3. Execute code when the script is run directly Correct answer
  4. Include external libraries

Correct answer

C. Execute code when the script is run directly

15 views

MCQSEXAM

Explanation

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.

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Python MCQs

Practise all Python MCQs

Similar MCQs