Episode 19 of 29
Modules and Packages
Learn how to organize code with modules and packages, and how to import functionality.
Modules are Python files you can import. Packages are folders of modules.
Importing Modules
import math
print(math.pi) # 3.14159...
print(math.sqrt(16)) # 4.0
from random import randint
print(randint(1, 100))Creating Your Own Module
# utils.py
def greet(name):
return "Hello, " + name
# app.py
from utils import greet
print(greet("Python"))Popular Built-in Modules
math— Mathematical functionsrandom— Random number generationdatetime— Date and time handlingos— Operating system interfacejson— JSON parsing