Bird
Raised Fist0
Pythonprogramming~20 mins

Importing specific items in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the statement from math import sqrt do in Python?
easy
A. It imports the entire math module.
B. It imports only the sqrt function from the math module.
C. It imports all functions except sqrt from math.
D. It renames the math module to sqrt.

Solution

  1. Step 1: Understand the import syntax

    The syntax from module import item imports only the specified item from the module.
  2. Step 2: Apply to the given statement

    Here, sqrt function is imported from the math module, not the whole module.
  3. Final Answer:

    It imports only the sqrt function from the math module. -> Option B
  4. Quick Check:

    from module import item = import only that item [OK]
Hint: Remember: 'from module import item' imports just that item [OK]
Common Mistakes:
  • Thinking it imports the whole module
  • Confusing import with renaming
  • Assuming it excludes the named item
2. Which of the following is the correct syntax to import the choice and shuffle functions from the random module?
easy
A. from random import choice, shuffle
B. import random.choice, random.shuffle
C. import choice, shuffle from random
D. from random import (choice shuffle)

Solution

  1. Step 1: Recall correct import syntax for multiple items

    To import multiple items, use from module import item1, item2 separated by commas.
  2. Step 2: Check each option

    from random import choice, shuffle matches the correct syntax: from random import choice, shuffle.
  3. Final Answer:

    from random import choice, shuffle -> Option A
  4. Quick Check:

    Multiple imports use commas inside from-import [OK]
Hint: Use commas to import multiple items from a module [OK]
Common Mistakes:
  • Using 'import module.item' syntax incorrectly
  • Placing 'from' after 'import'
  • Using parentheses without commas
3. What will be the output of this code?
from math import sqrt
print(sqrt(16))
medium
A. sqrt(16)
B. 16
C. NameError
D. 4.0

Solution

  1. Step 1: Understand what sqrt(16) does

    The sqrt function returns the square root of the number, so sqrt(16) returns 4.0.
  2. Step 2: Confirm import allows direct use

    Since sqrt was imported directly, calling sqrt(16) works without prefix.
  3. Final Answer:

    4.0 -> Option D
  4. Quick Check:

    sqrt(16) = 4.0 [OK]
Hint: Direct import lets you call function without module name [OK]
Common Mistakes:
  • Expecting integer 4 instead of float 4.0
  • Forgetting to import sqrt causing NameError
  • Trying to call math.sqrt without importing math
4. Identify the error in this code:
from os import path
print(os.path.exists('file.txt'))
medium
A. NameError because os is not imported
B. AttributeError because path has no exists method
C. SyntaxError in import statement
D. No error, code runs fine

Solution

  1. Step 1: Analyze the import statement

    The code imports only path from os, not the whole os module.
  2. Step 2: Check usage of os.path.exists

    The code tries to use os.path.exists, but os is not defined, causing a NameError.
  3. Final Answer:

    NameError because os is not imported -> Option A
  4. Quick Check:

    Importing item only means module name is undefined [OK]
Hint: Importing item doesn't import module name itself [OK]
Common Mistakes:
  • Assuming module name is available after importing item
  • Confusing AttributeError with NameError
  • Thinking import syntax is wrong
5. You want to import the datetime and timedelta classes from the datetime module but rename timedelta to td for clarity. Which is the correct import statement?
hard
A. from datetime import datetime as dt, timedelta as td
B. import datetime.datetime, datetime.timedelta as td
C. from datetime import datetime, timedelta as td
D. from datetime import datetime, timedelta td

Solution

  1. Step 1: Understand renaming syntax in import

    You can rename an imported item using as, e.g., timedelta as td.
  2. Step 2: Check options for correct syntax

    from datetime import datetime, timedelta as td correctly imports datetime and renames timedelta to td using from datetime import datetime, timedelta as td.
  3. Final Answer:

    from datetime import datetime, timedelta as td -> Option C
  4. Quick Check:

    Use 'as' to rename imported items [OK]
Hint: Use 'as' to rename imported items for clarity [OK]
Common Mistakes:
  • Trying to rename module instead of item
  • Incorrect syntax without commas or 'as'
  • Using import instead of from-import for renaming