0
0
Pythonprogramming~20 mins

Importing specific items in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Import Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
A16\n4
B4.0\n5
Csqrt\nceil
DError: ImportError
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.
Predict Output
intermediate
2: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))
A1
BError: NameError
C3
D2
Attempts:
2 left
💡 Hint
The seed function sets the random number generator state. With seed(1), randint(1,10) returns a fixed number.
🔧 Debug
advanced
2:00remaining
Identify the error in importing specific items
What error will this code raise and why?
Python
from math import squareroot

print(squareroot(9))
AImportError: cannot import name 'squareroot' from 'math'
BNameError: name 'squareroot' is not defined
CSyntaxError: invalid syntax
DAttributeError: module 'math' has no attribute 'squareroot'
Attempts:
2 left
💡 Hint
Check the spelling of the function you are importing from math.
🧠 Conceptual
advanced
2: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()
AOnly 'collections' is available as a module
BAll names from collections module are available
COnly 'deque' and 'Counter' are available
DNo names are available
Attempts:
2 left
💡 Hint
Importing specific items brings only those names into the current namespace.
📝 Syntax
expert
2:00remaining
Correct syntax for importing multiple specific items
Which option shows the correct syntax to import 'path' and 'mkdir' from the 'os' module?
Afrom os import path, mkdir
Bimport os.path, os.mkdir
Cimport os(path, mkdir)
Dfrom os import (path mkdir)
Attempts:
2 left
💡 Hint
Use 'from module import name1, name2' syntax to import multiple specific items.