beginner⏱30 minutesLesson 10 of 10
Modules, Packages, and Standard Library
Organize code with modules and packages, explore Python's rich standard library, and learn to use third-party packages
Modules, Packages, and Standard Library
As programs grow, you need to organize code into separate files. Python uses modules (files) and packages (directories) for this.
Modules
A module is simply a .py file:
python
# utils.py
def greet(name):
return f"Hello, {name}"
PI = 3.14159
class Circle:
def __init__(self, radius):
self.radius = radiusImport it in another file:
python
# main.py
import utils
print(utils.greet("Alice")) # "Hello, Alice"
print(utils.PI) # 3.14159
circle = utils.Circle(5)Import Styles
python
import math # Import whole module
from math import sqrt # Import specific items
from math import * # Import everything (avoid!)
import math as m # Alias
from math import sqrt as s # Alias specificNote
Packages
A package is a directory containing __init__.py (can be empty):
my_project/
├── main.py
└── shapes/
├── __init__.py
├── circle.py
└── rectangle.py
python
# shapes/circle.py
def area(radius):
return 3.14159 * radius ** 2python
# main.py
from shapes import circle
print(circle.area(5))Python Standard Library
Python's "batteries-included" philosophy means a rich standard library:
Math
python
import math
print(math.sqrt(16)) # 4.0
print(math.pi) # 3.14159...
print(math.floor(3.7)) # 3
print(math.ceil(3.2)) # 4Random
python
import random
print(random.randint(1, 10)) # Random int 1-10
print(random.choice(["a", "b", "c"])) # Random element
items = [1, 2, 3, 4, 5]
random.shuffle(items) # Shuffle in placeDatetime
python
from datetime import datetime, date
now = datetime.now()
print(now) # Current date/time
print(now.strftime("%Y-%m-%d")) # Formatted string
birthday = date(2025, 12, 25)
print(birthday) # 2025-12-25OS and File Operations
python
import os
print(os.getcwd()) # Current directory
os.mkdir("new_folder") # Create directory
print(os.listdir(".")) # List filesJSON
python
import json
data = {"name": "Alice", "age": 25}
json_str = json.dumps(data) # Serialize to string
parsed = json.loads(json_str) # Parse back to dictUsing Third-Party Packages with pip
python
# Install
pip install requests
# Use
import requests
response = requests.get("https://api.github.com")
data = response.json()
print(data)The if __name__ == "__main__" Pattern
Allows a file to be both imported and run:
python
# calculator.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
if __name__ == "__main__":
# This runs only when the file is executed directly
print("Testing calculator:")
print(f"3 + 5 = {add(3, 5)}")
print(f"10 - 4 = {subtract(10, 4)}")bash
python calculator.py # Runs the test codepython
# other.py
import calculator # Does NOT run the test codeReal-World Example: Data Analysis Script
python
# analyze.py
import csv
import json
from collections import Counter
from datetime import datetime
def load_data(filename: str) -> list[dict]:
with open(filename, "r") as f:
return list(csv.DictReader(f))
def analyze_sales(data: list[dict]) -> dict:
total = sum(float(row["amount"]) for row in data)
categories = Counter(row["category"] for row in data)
return {"total": total, "categories": dict(categories)}
if __name__ == "__main__":
sales = load_data("sales.csv")
report = analyze_sales(sales)
print(json.dumps(report, indent=2))Success
Practice Questions
- What's the difference between a module and a package?
- How do you import a specific function from a module?
- What's the purpose of
__init__.py? - Generate a random number between 1 and 100.
- How do you get the current date and time?
- What does
if __name__ == "__main__":do? - Convert a Python dict to a JSON string.
- How do you install a third-party package?
- Why should you avoid
from module import *? - Write a module with two functions and test it using
__name__.
Progress100%