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
Recall & Review
beginner
What does it mean to import specific items in Python?
It means you bring only certain functions, classes, or variables from a module into your program instead of importing the whole module.
Click to reveal answer
beginner
How do you import only the <code>sqrt</code> function from the <code>math</code> module?
You write: <br><code>from math import sqrt</code><br>This lets you use <code>sqrt()</code> directly without <code>math.</code> prefix.
Click to reveal answer
beginner
What happens if you try to use a function not imported specifically?
You get an error because Python doesn't know about it unless you import it or the whole module.
Click to reveal answer
beginner
Can you import multiple specific items from a module at once? How?
Yes, by listing them separated by commas: <br><code>from module_name import item1, item2</code>
Click to reveal answer
beginner
Why might you prefer importing specific items instead of the whole module?
It keeps your program clean and can save memory by only loading what you need.
Click to reveal answer
Which syntax correctly imports the choice function from the random module?
Aimport choice from random
Bimport random.choice
Cfrom random import choice
Dfrom choice import random
✗ Incorrect
The correct syntax is from random import choice to import a specific function.
What will happen if you write import math and then call sqrt(9)?
AIt works and returns 3
BError: <code>sqrt</code> is not defined
CImports <code>sqrt</code> automatically
DReturns 9
✗ Incorrect
You must use math.sqrt(9) or import sqrt specifically; otherwise, sqrt alone is undefined.
How do you import both sin and cos from the math module?
Afrom math import sin, cos
Bimport math.sin, math.cos
Cfrom math import sin cos
Dimport sin, cos from math
✗ Incorrect
Use commas to separate multiple items: from math import sin, cos.
What is a benefit of importing specific items instead of the whole module?
AFaster program startup and less memory use
BYou get all module features automatically
CYou can use any function without import
DNo difference at all
✗ Incorrect
Importing only what you need can make your program start faster and use less memory.
Which of these is NOT a correct way to import the datetime class from the datetime module?
Afrom datetime import datetime
Bfrom datetime import date
Cimport datetime
Dimport datetime.datetime
✗ Incorrect
You cannot import like import datetime.datetime. Use from datetime import datetime or import datetime.
Explain how to import specific functions from a Python module and why it might be useful.
Think about how you bring only what you need from a toolbox.
You got /3 concepts.
Describe what happens if you try to use a function from a module without importing it specifically or importing the whole module.
Imagine trying to use a tool you never took out of the box.
You got /3 concepts.
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
Step 1: Understand the import syntax
The syntax from module import item imports only the specified item from the module.
Step 2: Apply to the given statement
Here, sqrt function is imported from the math module, not the whole module.
Final Answer:
It imports only the sqrt function from the math module. -> Option B
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
Step 1: Recall correct import syntax for multiple items
To import multiple items, use from module import item1, item2 separated by commas.
Step 2: Check each option
from random import choice, shuffle matches the correct syntax: from random import choice, shuffle.
Final Answer:
from random import choice, shuffle -> Option A
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
Step 1: Understand what sqrt(16) does
The sqrt function returns the square root of the number, so sqrt(16) returns 4.0.
Step 2: Confirm import allows direct use
Since sqrt was imported directly, calling sqrt(16) works without prefix.
Final Answer:
4.0 -> Option D
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
Step 1: Analyze the import statement
The code imports only path from os, not the whole os module.
Step 2: Check usage of os.path.exists
The code tries to use os.path.exists, but os is not defined, causing a NameError.
Final Answer:
NameError because os is not imported -> Option A
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
Step 1: Understand renaming syntax in import
You can rename an imported item using as, e.g., timedelta as td.
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.
Final Answer:
from datetime import datetime, timedelta as td -> Option C
Quick Check:
Use 'as' to rename imported items [OK]
Hint: Use 'as' to rename imported items for clarity [OK]