Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate40 min
Python import statement four syntaxes hero — Module 14 Lesson 3

Relatable analogy: Think of modules as apps on your phone. import is 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 import really means — connect, not copy
  • 4 import syntax variations with visuals
  • How to access features with module.name or alias.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:

arithops bankinfo mathconstants modules as phone apps

Your Python program opens whichever app it needs:

main.py imports arithops module connection diagram

Simple definition: import connects your program to another Python file.


The 4 import syntaxes

Four import syntaxes numbered list import math import a b import as alias

We learn them one by one below.


Syntax 1 — import one module

Think of opening one app.

Syntax 1 import bankinfo module dot name access
python
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.

Syntax 2 import bankinfo arithops comma separated modules
python
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.

mathconstants original name to mc alias nickname diagram
Syntax 3 import mathconstants as mc alias shorter name
python
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.

Syntax 4 import bankinfo as bi arithops as ao multiple aliases
python
import bankinfo as bi, arithops as ao

print(bi.BNAME)
ao.subop(10, 20)

Or on separate lines — same idea:

python
import bankinfo as bi
import arithops as ao

How Python resolves names

This is what actually happens behind the scenes.

import arithops as ao alias points to module name resolution

The alias points to the module, not to individual functions.

When you write ao.sumop(10, 20):

  1. Python finds the module bound to ao
  2. Looks up sumop inside that module
  3. 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

Four import syntaxes interview cheat sheet module dot feature

Remember this sentence: import loads the module, and you access everything using module.feature or alias.feature.


Key takeaways

  • Phone-apps analogy — import opens 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:

python
from arithops import sumop

sumop(10, 20)  # no arithops. prefix needed

That 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