Lesson 03
Python import Statement — 4 Syntaxes Made Easy
Master all 4 import syntaxes with phone-app visuals, aliases, module.name access, and runnable examples using bankinfo, arithops, and mathconstants.
Relatable analogy: Think of modules as apps on your phone.
importis how you install or open the app so you can use its features.
Use the playground and quiz at the bottom to try all four syntaxes yourself.
What you will learn
- What
importreally means — connect, not copy - 4 import syntax variations with visuals
- How to access features with
module.nameoralias.name - How Python resolves names behind the scenes
- Common mistakes and the golden rule
Bring another Python file into your program
import does not copy code — it creates a connection so you can use another module's variables, functions, and classes.
What does import really mean?
Imagine these are apps installed on your phone:
Your Python program opens whichever app it needs:
Simple definition:
importconnects your program to another Python file.
The 4 import syntaxes
We learn them one by one below.
Syntax 1 — import one module
Think of opening one app.
import bankinfo
print(bankinfo.BNAME)
bankinfo.simple_interest(100000, 2, 10)Rule: Always use the module name before the feature → module.feature
Syntax 2 — import multiple modules
Open multiple apps together.
import bankinfo, arithops
print(bankinfo.BNAME)
arithops.sumop(10, 20)Same rule: module.feature for every access.
Syntax 3 — import with alias
Sometimes names are long. Give them a nickname.
import mathconstants as mc
print(mc.PI)
print(mc.E)Alias = shorter module name. Access through the alias, not the original filename.
Syntax 4 — multiple modules + aliases
This is simply Syntax 2 + Syntax 3 combined.
import bankinfo as bi, arithops as ao
print(bi.BNAME)
ao.subop(10, 20)Or on separate lines — same idea:
import bankinfo as bi
import arithops as aoHow Python resolves names
This is what actually happens behind the scenes.
The alias points to the module, not to individual functions.
When you write ao.sumop(10, 20):
- Python finds the module bound to
ao - Looks up
sumopinside that module - Calls the function
Comparison table
| Syntax | Form | Access pattern |
|---|---|---|
| 1 | import mod |
mod.feature |
| 2 | import mod1, mod2 |
mod1.feature, mod2.feature |
| 3 | import mod as alias |
alias.feature |
| 4 | import mod1 as a, mod2 as b |
a.feature, b.feature |
Important rule
With import, you can alias module names only — not individual variables or functions inside the module.
To rename specific functions or variables, use from … import (next lesson).
Common mistakes
| Wrong | Correct |
|---|---|
sumop(10, 20) |
arithops.sumop(10, 20) |
PI |
mathconstants.PI |
ao = sumop |
import arithops as ao |
| Typo in module name | Filename must match: import bankinfo needs bankinfo.py |
Golden rule: After import, use module.feature (or alias.feature).
Interview memory card
Remember this sentence: import loads the module, and you access everything using module.feature or alias.feature.
Key takeaways
- Phone-apps analogy —
importopens the module you need - Syntax 1 — one module:
import bankinfo→bankinfo.BNAME - Syntax 2 — comma list:
import bankinfo, arithops - Syntax 3 — alias:
import mathconstants as mc→mc.PI - Syntax 4 — combine both:
import bankinfo as bi, arithops as ao - Next:
from … import— pull individual names without the module prefix
Next lesson
The next topic is from … import …, where you import individual functions directly:
from arithops import sumop
sumop(10, 20) # no arithops. prefix neededThat removes the need to write the module prefix every time.
Loading playground…
Check your understanding
import statement quiz
What is the purpose of the `import` statement?
1 / 10