Legends ofPythos
Claim your name
Writing Real Programs

Using the standard library

Lesson 11 of 16

Watch the lesson5:42 ยท with Sigrid
You have built your own tools, but Python ships with a huge toolbox called the standard library. It is included with every install. You do not need pip or internet access to use it.

Importing modules

# Import the whole module
import math
result = math.sqrt(16)
print(result)  # 4.0

# Or import specific names directly
from random import randint
die = randint(1, 6)
Two ways to bring in library code
import math brings in the whole module. You reach its tools with a dot, as in math.sqrt(). This is clear, and its names can't clash with yours.

from random import randint brings in just that one name, so you write randint() with no dot. It is shorter, but if two modules have a function with the same name, it can get confusing.

The math module

import math
print(math.pi)       # 3.141592653589793
print(math.sqrt(2))  # ~1.414...
print(math.floor(3.9))   # 3 (round down)
print(math.ceil(3.1))    # 4 (round up)
Common math utilities
floor and ceil are different from the built-in round(). Floor always rounds down to the whole number below. Ceil always rounds up to the one above.

Randomness you can trust

import random
random.seed(42)
print(random.randint(1, 10))
# Same seed, same number, every time you run it.
Seeding makes randomness repeatable
random.seed() sets where the random numbers start, so the same seed always gives the same numbers. If you do not set a seed, Python starts from a fresh random point (it asks the operating system), and results differ every run.

For testing, seed it so the output is the same every time. In a real game, seed once, or not at all: seeding again with the same seed before every roll gives the same roll every time.

Your turn

0 of 3 solved

Exercise 1

+30 XP
Start with import math. Write a function called circle_area(r) that returns the area of a circle with radius r. Use math.pi for pi. Then write a second function, rounded_radius(x), that takes a float and returns its ceiling (round up to a whole number).

Example call:
circle_area(2), rounded_radius(3.1)
Should return approximately (12.56..., 4)
Write your Python here.

Run your code to check it against the tests.

Exercise 2

+30 XP
Start with import random. Write a function roll_dice(n, seed=42) that simulates rolling an n-sided die exactly once. It should return the result as an integer between 1 and n inclusive.

Use random.seed(seed) at the start of your function so results are repeatable for testing purposes.

Example: roll_dice(6) with default seed returns a specific number (you can verify by running it).
Write your Python here.

Run your code to check it against the tests.

Exercise 3

+30 XP
Start with import math. Write a function grade_band(score) that takes a numeric score from 0 to 100 and returns a letter grade string using these rules:

95 or above: 'A'
85 up to but not including 95: 'B'
70 up to but not including 85: 'C'
60 up to but not including 70: 'D'
Below 60: 'F'

Also write a helper normalize_score(raw, max_raw) that turns a raw score into a percentage from 0 to 100, rounded up to a whole number with math.ceil(). For example, 8 out of 9 is 88.89%, which rounds up to 89. Multiply by 100 before you divide, so a score that is already a whole percentage (11 out of 20 is 55) stays exactly that.

For example, (grade_band(72), normalize_score(8, 9)) gives ('C', 89).
Write your Python here.

Run your code to check it against the tests.