Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

Part 07 / Topic 2 of 9

Import styles

There are three common ways to import. Pick the one that keeps your code clear.

Real-world example: a dice game and a date stamp.

import math                      # 1. whole module -> use math.floor
from random import randint       # 2. one name     -> use randint directly
import datetime as dt            # 3. alias        -> use dt.date

print(math.floor(7.9))           # 7
print(1 <= randint(1, 6) <= 6)   # True
print(dt.date(2025, 8, 15))      # 2025-08-15
Style You write Good for
import math math.sqrt(9) Most cases, clear where names come from
from math import sqrt sqrt(9) A few names you use a lot
import datetime as dt dt.date.today() Long module names
from math import * sqrt(9) โŒ Avoid: hides where names came from, can overwrite your names

๐Ÿ” Going deeper: from shop_utils import GST_RATE does not copy anything. It binds a new name GST_RATE in your file to the same object that shop_utils.GST_RATE points to right now (GST_RATE is shop_utils.GST_RATE is True). But the two names are separate labels: if code later does shop_utils.GST_RATE = 0.12, only the module's label moves to the new value, and your GST_RATE still says 0.18. When a value may change, use import shop_utils and read shop_utils.GST_RATE.

Done here? The practice set for Part 7 โ€” 7 exercises with solutions โ€” is on the part page.

Search the guide