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 the import statement do in Python?
It loads a module so you can use its functions, classes, or variables in your code.
Click to reveal answer
intermediate
What happens if you import the same module multiple times in Python?
Python loads the module only once and reuses it, so importing again does not reload the module.
Click to reveal answer
beginner
Explain the difference between <code>import module</code> and <code>from module import name</code>.
<code>import module</code> imports the whole module and you access items with <code>module.name</code>. <br><code>from module import name</code> imports only the specific item directly, so you use <code>name</code> without the module prefix.
Click to reveal answer
beginner
What is the effect of using <code>import module as alias</code>?
It imports the module but lets you use a shorter or different name (alias) to refer to it in your code.
Click to reveal answer
intermediate
How does Python find the module when you use an import statement?
Python looks in a list of places called sys.path, which includes the current folder, installed packages, and standard library folders.
Click to reveal answer
What will happen if you write import math twice in the same Python program?
APython ignores the second import statement.
BThe math module is loaded twice, causing an error.
CThe second import overwrites the first one.
DThe math module is loaded only once and reused.
✗ Incorrect
Python loads a module only once per program run and reuses it for subsequent imports.
Which import style allows you to use a function directly without the module name?
Afrom module import function
Bimport module as alias
Cimport module
Dimport *
✗ Incorrect
Using from module import function lets you call function() directly.
What does import module as alias do?
AImports only the alias from the module.
BImports the module and renames it to alias.
CImports the module twice.
DCreates a new module named alias.
✗ Incorrect
It imports the module but lets you use the shorter name alias in your code.
Where does Python look for modules when you use an import statement?
AOnly in the standard library.
BOnly in the current folder.
CIn the current folder, installed packages, and standard library folders.
DOnly in the internet.
✗ Incorrect
Python searches a list of folders called sys.path, including current folder and installed packages.
What happens if you try to import a module that does not exist?
APython raises a ModuleNotFoundError.
BPython creates a new empty module.
CPython ignores the import.
DPython imports a default module instead.
✗ Incorrect
Python raises an error called ModuleNotFoundError if the module cannot be found.
Explain how Python handles multiple imports of the same module in a program.
Think about efficiency and how Python avoids repeating work.
You got /3 concepts.
Describe the difference between import module and from module import name in Python.
Consider how you write the code after importing.
You got /4 concepts.
Practice
(1/5)
1. What happens when you use import module_name in Python?
easy
A. The module is copied into your current file.
B. Only the functions you call from the module are loaded.
C. The module code runs every time you call a function from it.
D. The entire module is loaded and its code runs once.
Solution
Step 1: Understand import behavior
When you import a module, Python loads the whole module and runs its code once.
Step 2: Recognize module reuse
After the first import, Python reuses the loaded module without running its code again.
Final Answer:
The entire module is loaded and its code runs once. -> Option D
Quick Check:
Import runs module once = A [OK]
Hint: Import runs module code once, then reuses it [OK]
Common Mistakes:
Thinking module code runs every time a function is called
Believing only used functions are loaded
Assuming module code is copied into current file
2. Which of the following is the correct syntax to import only the sqrt function from the math module?
easy
A. from math import sqrt
B. import math.sqrt
C. import sqrt from math
D. from sqrt import math
Solution
Step 1: Recall import syntax for specific functions
To import a specific function, use from module import function.
Step 2: Match syntax to options
from math import sqrt matches this syntax: from math import sqrt.
Final Answer:
from math import sqrt -> Option A
Quick Check:
Specific import uses 'from module import item' = A [OK]
Hint: Use 'from module import name' to import parts [OK]
Common Mistakes:
Using dot notation in import statement incorrectly