This Python Modules lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.
A module is a file of Python you can import. The standard library ships with modules such as math, json, and datetime.
import math loads the module. math.sqrt is one of its functions. A second form, from math import sqrt, copies that name into your file.
Example 1: Start here
Import at the top of a file so a reader sees the dependencies immediately.
import math # load a tool that comes with Python
print(math.sqrt(81))
Example 2: Import a tool
import loads a module. You then use its name and a dot.
import math # load a tool that comes with Python
print(math.sqrt(81))
Example 3: Import one name
from lets you call sqrt without the math prefix.
from math import sqrt # load a tool that comes with Python
print(sqrt(81))
Example 4: Give it a short name
as renames the module inside this file only.
import math as m # load a tool that comes with Python
print(m.sqrt(81))
