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
Import Statement Behavior
📖 Scenario: You are organizing a small Python project with two files. One file contains a function, and the other file will use that function by importing it.
🎯 Goal: Learn how to use the import statement to access functions from another file.
📋 What You'll Learn
Create a Python file named helper.py with a function called greet that returns the string 'Hello from helper!'.
Create a Python file named main.py that imports the greet function from helper.py.
Call the greet function inside main.py and store the result in a variable called message.
Print the message variable in main.py.
💡 Why This Matters
🌍 Real World
In real projects, code is often split into multiple files to keep things organized. Importing lets you use code from other files easily.
💼 Career
Understanding imports is essential for working on larger Python projects and collaborating with others.
Progress0 / 4 steps
1
Create the helper function
Create a file named helper.py and write a function called greet that returns the string 'Hello from helper!'. Write exactly this function definition:
Python
Hint
Remember to use def to define a function and return to send back the string.
2
Import the greet function
In a new file named main.py, write an import statement to import the greet function from the helper module.
Python
Hint
Use the syntax from module_name import function_name.
3
Call the greet function
In main.py, call the greet function and store its return value in a variable called message.
Python
Hint
Call the function by writing greet() and assign it to message.
4
Print the message
In main.py, print the variable message to display the greeting.
Python
Hint
Use print(message) to show the greeting on the screen.
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