0
0
Pythonprogramming~20 mins

__name__ and __main__ behavior in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of __name__ and __main__
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code when run as a script?
Consider this Python script saved as script.py. What will it print when executed directly with python script.py?
Python
print(f"Top-level __name__ is: {__name__}")

if __name__ == "__main__":
    print("Running as main script")
else:
    print("Imported as a module")
A
Top-level __name__ is: __main__
Running as main script
B
Top-level __name__ is: script
Running as main script
C
Top-level __name__ is: __main__
Imported as a module
D
Top-level __name__ is: script
Imported as a module
Attempts:
2 left
💡 Hint
When a Python file is run directly, __name__ is set to "__main__".
Predict Output
intermediate
2:00remaining
What is the output when this module is imported?
Given the same code below saved as module.py, what will be printed when another script imports it using import module?
Python
print(f"Top-level __name__ is: {__name__}")

if __name__ == "__main__":
    print("Running as main script")
else:
    print("Imported as a module")
A
Top-level __name__ is: __main__
Running as main script
B
Top-level __name__ is: module
Running as main script
C
Top-level __name__ is: module
Imported as a module
D
Top-level __name__ is: __main__
Imported as a module
Attempts:
2 left
💡 Hint
When a file is imported, __name__ is the module's name, not "__main__".
🔧 Debug
advanced
2:00remaining
Why does this code print nothing when run directly?
This code is saved as test.py. When run with python test.py, it prints nothing. Why?
Python
def main():
    print("Hello from main")

if __name__ == "__main__":
    main
ABecause __name__ is not "__main__" when run directly.
BBecause main is referenced but not called (missing parentheses).
CBecause print statement is inside a function that is never defined.
DBecause the code has a syntax error and does not run.
Attempts:
2 left
💡 Hint
Check if the function main is actually executed.
📝 Syntax
advanced
2:00remaining
Which option causes a SyntaxError?
Which of these code snippets will cause a SyntaxError when run?
A
if __name__ = "__main__":
    print("Running")
B
if __name__ == "__main__":
    print("Running")
C
)"gninnuR"(tnirp    
:"__niam__" == __eman__ fi
D
if __name__ == "__main__":
    print("Running")
else:
    print("Not main")
Attempts:
2 left
💡 Hint
Check the operator used in the if condition.
🚀 Application
expert
2:00remaining
What is the output of this code when run directly?
Analyze this code saved as app.py. What will it print when executed with python app.py?
Python
def greet():
    print(f"Hello from {__name__}!")

if __name__ == "__main__":
    greet()
else:
    print(f"Imported module: {__name__}")
ANo output
BImported module: app
CHello from app!
DHello from __main__!
Attempts:
2 left
💡 Hint
Remember what __name__ is when running a script directly.