import: using code from other files
Python comes with a large standard library, and every package you install later is used the same way: import it, then call what is inside.
import math
tokens = 1234
print(tokens / 1000)
print(math.ceil(tokens / 1000))import math makes the math module available, and a dot reaches into it. math.ceil rounds up, which is how a price per started thousand is worked out.
Importing names directly
from datetime import date, timedelta
ordered = date(2026, 3, 3)
expected = ordered + timedelta(days=5)
print(expected)
print(date(2026, 3, 10) > expected)from datetime import date, timedelta brings those two names in, so you write date instead of datetime.date. A timedelta is a length of time, and adding it to a date gives a later date.
Your own module
Any .py file is a module. Move categorise from lesson 11 into a file called triage.py:
def categorise(text):
text = text.lower()
if "charged" in text or "refund" in text:
return "billing"
if "parcel" in text or "arrived" in text:
return "shipping"
return "other"from triage import categorise
print(categorise("Can I get a refund for the blue mug?"))The file name without .py is the module name. The file must be in the same folder as the code importing it, which is the case here.
A module that is not there
import triage_toolsModuleNotFoundError means Python looked and found no file or installed package with that name.
- Print
math.sqrt(144). - Add a function
is_urgent(text)totriage.pyand import both names in one line. - Try
import triage as tand callt.categorise.
This is what real progress feels like.