Challenge - 5 Problems
Import Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of importing specific functions
What is the output of this Python code when importing specific functions from a module?
Python
from math import sqrt, ceil print(sqrt(16)) print(ceil(4.2))
Attempts:
2 left
💡 Hint
Remember that sqrt(16) returns the square root of 16, and ceil(4.2) rounds 4.2 up to the nearest integer.
✗ Incorrect
The code imports sqrt and ceil from the math module. sqrt(16) returns 4.0, and ceil(4.2) returns 5. So the output is two lines: 4.0 and 5.
❓ Predict Output
intermediate2:00remaining
Output when importing a variable and a function
What will be printed by this code snippet?
Python
from random import randint, seed seed(1) print(randint(1, 10))
Attempts:
2 left
💡 Hint
The seed function sets the random number generator state. With seed(1), randint(1,10) returns a fixed number.
✗ Incorrect
Using seed(1) fixes the random sequence. The first randint(1,10) call after seed(1) returns 2.
🔧 Debug
advanced2:00remaining
Identify the error in importing specific items
What error will this code raise and why?
Python
from math import squareroot print(squareroot(9))
Attempts:
2 left
💡 Hint
Check the spelling of the function you are importing from math.
✗ Incorrect
The math module does not have a function named 'squareroot'. The import statement fails with ImportError.
🧠 Conceptual
advanced2:00remaining
Effect of importing specific items on namespace
After running this code, which names are available in the current namespace?
Python
from collections import deque, Counter x = deque() y = Counter()
Attempts:
2 left
💡 Hint
Importing specific items brings only those names into the current namespace.
✗ Incorrect
Using 'from collections import deque, Counter' imports only these two names. The collections module itself is not imported as a whole.
📝 Syntax
expert2:00remaining
Correct syntax for importing multiple specific items
Which option shows the correct syntax to import 'path' and 'mkdir' from the 'os' module?
Attempts:
2 left
💡 Hint
Use 'from module import name1, name2' syntax to import multiple specific items.
✗ Incorrect
Option A correctly uses 'from os import path, mkdir'. Option A tries to import attributes directly which is invalid syntax. Option A and D have syntax errors.