Standard Library Tour: math, random, string

Python 3.14 · ✓ verified by execution on 2026-07-23

Standard Library Tour

The Python Standard Library is a collection of modules that come pre-installed with Python.

The Math Module

The math module gives you common mathematical functions and constants — square roots, trigonometry, logarithms, pi, and more — backed by your platform’s fast C math library.

python
import math
print(math.sqrt(25.0))
Output
5.0

The math functions don’t work with complex numbers. Ask for the square root of a negative number and you get a ValueError, not a complex result — for that you’d use the cmath module instead:

python
import math
try:
    math.sqrt(-1.0)
except ValueError as e:
    print('ValueError:', e)
Output
ValueError: expected a nonnegative input, got -1.0

The Random Module

The random module produces pseudo-random numbers — they look random but come from a deterministic algorithm. That’s why seeding it with random.seed(42) makes the sequence repeatable, which is exactly what you want when writing tests.

You can generate random integers:

python
import random
random.seed(42)
print(random.randint(1, 10))
Output
2

Or choose a random element from a list:

python
import random
random.seed(42)
print(random.choice(['apple', 'banana', 'cherry']))
Output
cherry

The String Module

The string module defines handy constants for common groups of characters — like every digit, or every letter:

python
import string
print(string.digits)
Output
0123456789

string.ascii_letters is simply the lowercase letters followed by the uppercase ones — 52 in all:

python
import string
print(len(string.ascii_letters))
Output
52

Edge Cases

Check yourself

The random module generates truly unpredictable random numbers suitable for security.

Reveal answer

It generates pseudo-random numbers that are predictable. For security, the secrets module must be used. — It generates pseudo-random numbers that are predictable. For security, the secrets module must be used.

math.sqrt(-1) will return a complex number '1j'.

Reveal answer

It raises a ValueError. The cmath module is required for complex numbers. — It raises a ValueError. The cmath module is required for complex numbers.

You can use string.ascii_letters without importing the string module.

Reveal answer

They live in the string module namespace and require an explicit import. — They live in the string module namespace and require an explicit import.

Standard library modules need to be installed with pip.

Reveal answer

They are built-in and shipped with Python by default. — They are built-in and shipped with Python by default.

Challenges

Challenge 1 +25 XP

Import the math module and use `math.hypot()` to calculate the hypotenuse of a right triangle with sides 3 and 4. Print the result.

python
  • Test 1 — expects "5.0"
Need a hint? (−25% XP)

Check the math documentation for hypot.

Show solution (0 XP)
import math
print(math.hypot(3, 4))

Challenge 2 +25 XP

Import `random` and `string`. Set the random seed to 42 using `random.seed(42)`. Choose a random letter from `string.ascii_uppercase` and print it.

python
  • Test 1 — expects "U"
Need a hint? (−25% XP)

You need to import two modules.

Show solution (0 XP)
import random
import string
random.seed(42)
print(random.choice(string.ascii_uppercase))