← Quiz: Modules and Libraries: all 14 questions
Quiz: Modules and Libraries · Question 3 of 14
What is the purpose of if name == "main":?
What is the purpose of if __name__ == "__main__":?
- A.To make the program faster
- B.To run code only when the file is executed directly, not when imported
- C.To declare the main function
- D.It is required in every file
Answer & explanation
Correct answer: To run code only when the file is executed directly, not when imported
When a file is imported, name is the module name, so that block is skipped.
Can you get the rest right without peeking?
Take the timed Quiz: Modules and Libraries quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →