Legends ofPythos
Claim your name
Writing Real Programs

Using the standard library

Lesson 11 of 16

Watch the lesson1:16 ยท 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 gives you a namespace. You access functions as math.sqrt(). This is clear and avoids name collisions.

from random import randint puts the function directly into your file's scope. 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 goes down to the nearest integer. Ceil always goes up.

Randomness you can trust

import random
random.seed(42)
print(random.randint(1, 10))
# Run it again with the same seed and get the exact same number.
Seeding makes randomness repeatable
random.seed() sets the starting point of a deterministic sequence. 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 debugging or tests, always seed so the output is predictable.

Your turn

0 of 3 solved

Exercise 1

+30 XP
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
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
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 scales any raw score into the 0-100 range using ceiling. For example if someone scored 8 out of 9 on an assignment, normalize it to round up proportionally. 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.