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

Added 25 Nov 2026

What is the purpose of the if __name__ == '__main__': block in Python?

  1. Define the main function
  2. Import the main module
  3. Exit the program
  4. Run code only when the file is executed directly, not imported Correct answer

Correct answer

D. Run code only when the file is executed directly, not imported

5 views

MCQSEXAM

Explanation

The block prevents code from running when the module is imported elsewhere.

Reference

Asked in: Python Interview Basics

https://docs.python.org/3/tutorial/introduction.html

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? 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