All Courses
Python Basics

Python Math & Random

1. Built-in Math Functions

These work without any imports.

abs() — Absolute Value

Returns the positive version of a number.

abs(-9)      # → 9
abs(-29.5)   # → 29.5
abs(0)       # → 0

max() — Maximum Value

Returns the largest value from multiple arguments or an iterable.

max(1, 2, 5)        # → 5
max(-4, 1, 3)       # → 3
max("a", "b")       # → "b"  (compares by ordinal value)
max((1, 2, 3))      # → 3   (works on tuples/lists too)

min() — Minimum Value

Returns the smallest value. Works the same way as max().

min(1, 2, 3)        # → 1
min(-4, 1, 3)       # → -4
min((1, 2, 3))      # → 1

sum() — Sum of an Iterable

Takes a single iterable (list, tuple) — not multiple arguments.

sum([1, 2, 3])         # → 6
sum((3.4, 5.6))        # → 9.0
sum([1, 2, 3, -4])     # → 2

⚠️ sum(1, 2, 3) is invalid — you must pass a list or tuple, not separate values.

round() — Round a Number

Rounds to the nearest whole number by default, or to a specified number of decimal places.

round(3.4)         # → 3   (default: round to whole number)
round(3.51)        # → 4
round(3.45, 1)     # → 3.5  (round to 1 decimal place)
round(3.495, 2)    # → 3.5  (round to 2 decimal places)
round(3.375, 3)    # → 3.375

2. The math Module

Import at the top of your file to access advanced math features.

import math

Common Functions

math.sin(90)         # sine of 90 radians
math.cos(0)          # → 1.0
math.cos(math.pi)    # → -1.0
math.tan(x)

⚠️ Trig functions use radians, not degrees.

Useful Constants & Functions

math.pi              # → 3.14159...
math.sqrt(16)        # → 4.0
math.floor(3.9)      # → 3  (round down)
math.ceil(3.1)       # → 4  (round up)
math.log(x)          # natural log
math.log(x, base)    # log with custom base

If you need a specific math feature, search "[function name] math module Python" — it's likely in there.


3. The random Module

Import at the top of your file.

import random

random.randint(a, b) — Random Integer (inclusive)

Returns a random integer between a and b, including both endpoints.

random.randint(1, 10)    # → any integer from 1 to 10 (including 1 and 10)

random.randrange(start, stop, step) — Random Integer (range-style)

Works like range()stop is exclusive.

random.randrange(1, 10, 2)   # → random odd number: 1, 3, 5, 7, or 9
randint randrange
Stop value Inclusive Exclusive
Step Not supported Supported

random.choice(iterable) — Random Element from a Collection

Randomly picks one element from a list, tuple, or string.

lst = ["he", "hi", "ho", "hello"]
random.choice(lst)    # → one random element from the list

Cheat Sheet

# Built-in (no import needed)
abs(-5)             # → 5
max(1, 2, 3)        # → 3
min(1, 2, 3)        # → 1
sum([1, 2, 3])      # → 6
round(3.567, 2)     # → 3.57

# math module
import math
math.pi             # → 3.14159...
math.sqrt(9)        # → 3.0
math.sin(x)
math.cos(x)
math.tan(x)
math.floor(3.9)     # → 3
math.ceil(3.1)      # → 4

# random module
import random
random.randint(1, 10)          # random int, 1–10 inclusive
random.randrange(1, 10, 2)     # random odd int, 1–9
random.choice([1, 2, 3])       # random element from list