Introduction
A module is a file of ready-made code that you can use instead of writing everything yourself. Python ships with hundreds of them, called the standard library. This lesson uses two of the most common: math and random.
Good programmers reuse tested code rather than writing everything themselves. Python's standard library handles dates, maths, files and much more, so that you can spend your time on the part of the problem that is really yours.
How it works
from … import
from math import gcd, factorial
print(gcd(48, 180))
print(factorial(5))from math import gcd, factorial brings just those two names into your program, so you can call gcd(48, 180) without the math. prefix. Use this form when you need only a few tools and their names are clear.
Random numbers you can repeat
import random
random.seed(7)
dice = random.randint(1, 6)
coin = random.choice(["heads", "tails"])
print(dice, coin)random.randint(1, 6) picks a whole number from 1 to 6, both included, and random.choice(list) picks one item. random.seed(7) fixes the starting point, so the "random" results are the same every time you run it: here 3 heads. That makes programs that use randomness testable. Remove the seed line to get different results on each run.
Syntax
import math
math.sqrt(16)
from math import gcd
gcd(12, 18)
import random
random.seed(1)
random.randint(1, 6)
Example
import math
r = 3
area = math.pi * r * r
print(round(area, 2))
print(math.sqrt(144), math.floor(7.9), math.ceil(7.1))import math loads the module once; after that, its tools are reached with a dot: math.pi, math.sqrt(144). In the variables panel, math appears as a variable too, because a module is just another value with a name.
| Tool | Gives | Example |
|---|---|---|
math.pi | π, about 3.14159 | math.pi * r * r |
math.sqrt(x) | square root, as a float | math.sqrt(144) is 12.0 |
math.floor(x) | round down | math.floor(7.9) is 7 |
math.ceil(x) | round up | math.ceil(7.1) is 8 |
round(x, 2) | built-in, not from math: round to 2 places | 28.27 |
Line by line
import mathloads the module and gives it a name.r = 3is the radius.math.pi * r * ruses the module's value of pi.round(area, 2)is a built-in function, and it keeps two decimals.- The last line shows three more tools from the module.
Expected result
28.27
12.0 7 8
In the real world
Almost every real program imports something. Beyond the standard library, thousands of free packages handle tasks such as web servers, data analysis and machine learning, and using them well is a core skill.
Common mistakes
- Naming your own file
math.pyorrandom.py. Python then imports your file instead of the real module. - Forgetting the prefix: after
import math, plainsqrt(9)is aNameError; writemath.sqrt(9). random.randint(1, 6)includes 6, butrange(1, 6)does not. The two count differently.
Best practices
- Put imports at the top of the file.
- Use
import mathwhen you use many tools, andfromfor a few clear names. - Use a seed when you need to test code that uses randomness.
Summary
A module is ready-made code that you load with import. Its tools are reached with a dot, or brought in by name with from. math handles calculations, and random makes choices that a seed can make repeatable.
Practice
Simulate rolling two dice 10 times with random.seed(1) and print each total. Then count how often the total was 7.
Quick check
Key takeaways
- Do not write what the standard library already provides.
- Never name your own file after a module you use.
- randint includes both ends, unlike range.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.