Day 22 45 min beginner

Math and Randomness

Learn how to perform advanced calculations and generate random data using built-in modules.

Learning Objectives

  • Use the math module for advanced operations
  • Generate random numbers and choices
  • Understand the concept of 'pseudo-randomness'
  • Practice using external libraries

Beyond Basic Arithmetic

Python’s built-in operators (+, -, *, /) are great, but sometimes you need more: calculating square roots, working with Pi, or rolling a virtual die. Python provides two standard modules for this: math and random.

The math Module

This module provides mathematical constants and functions.

math_examples.py
1
2
3
4
5
6
import math

print(f"Pi is: {math.pi}")
print(f"Square root of 16: {math.sqrt(16)}")
print(f"Ceiling of 4.2: {math.ceil(4.2)}")  # Rounds UP to 5
print(f"Floor of 4.8: {math.floor(4.8)}")   # Rounds DOWN to 4

The random Module

Crucial for games, simulations, and security, this module handles chance.

random_examples.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import random

# Random float between 0.0 and 1.0
print(random.random())

# Random integer between 1 and 10 (inclusive)
print(random.randint(1, 10))

# Random choice from a list
options = ["Red", "Green", "Blue"]
print(random.choice(options))

# Shuffling a list
deck = [1, 2, 3, 4, 5]
random.shuffle(deck)
print(deck)

Is it really random?

Computers aren’t actually random; they follow complex algorithms. This is called Pseudo-randomness. For most uses (like games), it’s perfect. For high-security encryption, developers use different tools.


Interactive Practice

Try building a simple “Coin Toss” program that randomly prints “Heads” or “Tails”.

Module Common Uses
math Scientific apps, geometry, rounding logic
random Games, shuffling data, sampling

Quiz

Complete this quiz with a minimum score of 80% to mark Day 22 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!