Challenge - 5 Problems
Import Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of importing a module with side effects
What is the output when running this code?
Python
import math print(math.pi) import math print(math.sqrt(16))
Attempts:
2 left
💡 Hint
Remember that importing a module twice does not reload it.
✗ Incorrect
The math module is imported once. The second import does nothing. math.pi prints 3.141592653589793 and math.sqrt(16) prints 4.0.
❓ Predict Output
intermediate2:00remaining
Effect of import * on namespace
What is the output of this code?
Python
from math import * print(sin(0)) print(cos(0))
Attempts:
2 left
💡 Hint
import * brings all functions into current namespace.
✗ Incorrect
sin(0) returns 0.0 and cos(0) returns 1.0 from the math module.
❓ Predict Output
advanced2:00remaining
Importing a module with circular dependency
Given two modules a.py and b.py where each imports the other, what error occurs when running a.py?
Python
a.py: import b x = 5 b.py: import a y = 10
Attempts:
2 left
💡 Hint
Circular imports without accessing attributes during module initialization do not cause errors.
✗ Incorrect
Python handles circular imports by providing the partially initialized module object. Since b.py does not access any attributes from a.py during its initialization, no AttributeError occurs, and the program runs fine.
❓ Predict Output
advanced2:00remaining
Effect of import aliasing on variable access
What is the output of this code?
Python
import math as m print(m.sqrt(25)) print(math.sqrt(25))
Attempts:
2 left
💡 Hint
Aliasing changes the module name in current scope.
✗ Incorrect
math is not defined because it was imported as m. m.sqrt(25) prints 5.0 but math.sqrt(25) raises NameError.
🧠 Conceptual
expert2:00remaining
Understanding import caching behavior
Which statement best describes Python's import caching behavior?
Attempts:
2 left
💡 Hint
Think about how Python avoids repeated work when importing.
✗ Incorrect
Python loads a module once and stores it in sys.modules. Subsequent imports use the cached module, improving performance.