Bird
Raised Fist0

Consider this code in a file named module.py:

medium📝 Predict Output Q5 of Q15
Python - Modules and Code Organization
Consider this code in a file named module.py:
print(f"Module name is {__name__}")

if __name__ == '__main__':
    print('Executed directly')

What will be printed when you run python module.py?
AModule name is module Executed directly
BModule name is __main__ Executed directly
CModule name is __main__
DExecuted directly
Step-by-Step Solution
Solution:
  1. Step 1: Value of __name__ when run directly

    When run directly, __name__ is "__main__".
  2. Step 2: Output lines

    First line prints "Module name is __main__". Then the if condition is True, so it prints "Executed directly".
  3. Final Answer:

    Module name is __main__ Executed directly -> Option B
  4. Quick Check:

    Direct run sets __name__ to '__main__' [OK]
Quick Trick: Direct run sets __name__ to '__main__' [OK]
Common Mistakes:
MISTAKES
  • Assuming __name__ is module name when run directly
  • Missing the second print line
  • Confusing output order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes