11-15-2025, 10:39 PM
Chapter 15 — Modules & Importing Code
Modules are collections of functions, tools, and features that you can bring into your program with a single command.
Python becomes extremely powerful the moment you begin importing modules.
---
15.1 What Is a Module?
A module is a file that contains:
• functions
• variables
• tools
• classes
You import it to use those features.
Examples of built-in modules:
• math
• random
• time
• statistics
• os
---
15.2 Importing a Module
Basic import:
Then use it:
---
15.3 Importing Specific Functions
---
15.4 Renaming a Module (Alias)
Useful for long module names:
---
15.5 The math Module
Common tools:
Example:
---
15.6 The random Module
Generate randomness:
---
15.7 The time Module
Delay programs:
---
15.8 Writing Your Own Module
Create a file:
mymath.py
Put this inside:
Then import in another file:
You just created a module.
---
15.9 Importing Everything (Use Carefully)
This gives access to all math tools directly:
But this can cause naming conflicts. Use sparingly.
---
15.10 Real-World Use — JSON Module
APIs send data in JSON format.
---
15.11 Mini Project — Random Password Generator
Use random.choice() to create a password with:
• letters
• numbers
• symbols
Length: 10–15 characters.
Example output:
---
15.12 Challenge — Dice Battle Simulator
Rules:
• two players roll dice (1–6)
• use random.randint()
• highest number wins
• repeat 5 rounds
• show total wins
Example:
---
15.13 Chapter Summary
• modules add extra abilities to Python
• import the whole module or specific functions
• math, random, time, and json are essential tools
• you can write your own modules
• modules make programs cleaner, shorter, and more powerful
Next:
Chapter 16 — File Handling: Reading & Writing Data
This is when learners start interacting with real files — saving data, loading data, and building real software.
---
Written and Compiled by Lee Johnston — Founder of The Lumin Archive
Modules are collections of functions, tools, and features that you can bring into your program with a single command.
Python becomes extremely powerful the moment you begin importing modules.
---
15.1 What Is a Module?
A module is a file that contains:
• functions
• variables
• tools
• classes
You import it to use those features.
Examples of built-in modules:
• math
• random
• time
• statistics
• os
---
15.2 Importing a Module
Basic import:
Code:
import mathThen use it:
Code:
print(math.sqrt(25))---
15.3 Importing Specific Functions
Code:
from math import sqrt, pi
print(sqrt(49))
print(pi)---
15.4 Renaming a Module (Alias)
Useful for long module names:
Code:
import statistics as stats
print(stats.mean([2, 4, 6]))---
15.5 The math Module
Common tools:
Code:
math.sqrt(x)
math.floor(x)
math.ceil(x)
math.pi
math.eExample:
Code:
import math
print(math.floor(3.9)) # 3---
15.6 The random Module
Generate randomness:
Code:
import random
print(random.randint(1, 6)) # random dice roll
print(random.choice(["red", "blue", "green"]))---
15.7 The time Module
Delay programs:
Code:
import time
print("3...")
time.sleep(1)
print("2...")
time.sleep(1)
print("1...")---
15.8 Writing Your Own Module
Create a file:
mymath.py
Put this inside:
Code:
def triple(x):
return x * 3Then import in another file:
Code:
import mymath
print(mymath.triple(5))You just created a module.
---
15.9 Importing Everything (Use Carefully)
Code:
from math import *This gives access to all math tools directly:
Code:
print(sqrt(81))But this can cause naming conflicts. Use sparingly.
---
15.10 Real-World Use — JSON Module
APIs send data in JSON format.
Code:
import json
data = '{"name": "Lee", "score": 95}'
parsed = json.loads(data)
print(parsed["name"])---
15.11 Mini Project — Random Password Generator
Use random.choice() to create a password with:
• letters
• numbers
• symbols
Length: 10–15 characters.
Example output:
Code:
Your password: 7G!pK3a@2m---
15.12 Challenge — Dice Battle Simulator
Rules:
• two players roll dice (1–6)
• use random.randint()
• highest number wins
• repeat 5 rounds
• show total wins
Example:
Code:
Round 1: Player A wins
Round 2: Tie
Round 3: Player B wins
...---
15.13 Chapter Summary
• modules add extra abilities to Python
• import the whole module or specific functions
• math, random, time, and json are essential tools
• you can write your own modules
• modules make programs cleaner, shorter, and more powerful
Next:
Chapter 16 — File Handling: Reading & Writing Data
This is when learners start interacting with real files — saving data, loading data, and building real software.
---
Written and Compiled by Lee Johnston — Founder of The Lumin Archive
