Bird
Raised Fist0
Pythonprogramming~10 mins

Importing specific items in Python - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the sqrt function from the math module.

Python
from math import [1]

result = sqrt(16)
print(result)
Drag options to blanks, or click blank then click option'
Apow
Bsqrt
Crandom
Dceil
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the whole math module instead of just sqrt.
Using a function name that does not exist in math.
2fill in blank
medium

Complete the code to import the choice function from the random module.

Python
from random import [1]

items = ['apple', 'banana', 'cherry']
print(choice(items))
Drag options to blanks, or click blank then click option'
Asample
Bshuffle
Crandint
Dchoice
Attempts:
3 left
💡 Hint
Common Mistakes
Importing a function that shuffles the list instead of picking one item.
Using a function that generates random numbers instead of selecting items.
3fill in blank
hard

Fix the error in the code by importing the correct item from the datetime module.

Python
from datetime import [1]

now = datetime.now()
print(now)
Drag options to blanks, or click blank then click option'
Adatetime
Btime
Cdate
Dtimedelta
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'date' or 'time' which do not have the now() method.
Not importing the class named 'datetime'.
4fill in blank
hard

Fill both blanks to import sin and cos from the math module.

Python
from math import [1], [2]

angle = 0.5
print(sin(angle))
print(cos(angle))
Drag options to blanks, or click blank then click option'
Asin
Btan
Ccos
Dlog
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'tan' or 'log' instead of 'cos'.
Importing only one function instead of both.
5fill in blank
hard

Fill all three blanks to import join, split, and exists from their respective modules.

Python
from os.path import [1]
from os.path import [2]
from os.path import [3]

print(join('folder', 'file.txt'))
print(split('folder/file.txt'))
print(exists('folder/file.txt'))
Drag options to blanks, or click blank then click option'
Ajoin
Bsplit
Cexists
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing os.path.split with the string method split.
Importing path instead of join, split, or exists.

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