Numbers and math
Overview
Programs calculate all the time: totals, averages, conversions, scores. Python handles numbers with two main types and a set of operators that work like a calculator — with a few differences worth knowing.
Two kinds of number
count = 42 # int: a whole number
price = 3.99 # float: a number with a decimal point
big = 2 ** 100 # ints can be as big as you like
print(big) # 1267650600228229401496703205376- int (integer): whole numbers, positive or negative, with no size limit.
- float (floating point): numbers with a decimal point —
3.99,0.5,-2.0, also scientific notation like1.5e6(1,500,000).
For readability you can put underscores in long numbers: population = 8_100_000_000.
Arithmetic operators
A few that surprise newcomers:
/always returns a float, even when the division is exact:8 / 2is4.0.//(floor division) divides and rounds down:7 // 2is3. With negatives it still rounds down:-7 // 2is-4.%(modulo) gives the remainder:7 % 2is1. It’s great for “is this even?” (n % 2 == 0), wrapping around (clock hours:(hour + 5) % 24), and splitting units.**is “to the power of”:2 ** 10is1024. (^is something different in Python — don’t use it for powers.)
Splitting units with // and %
// and % together split a quantity into larger and smaller units:
total_minutes = 135
hours = total_minutes // 60 # 2
minutes = total_minutes % 60 # 15divmod(135, 60) returns both at once: (2, 15).
Order of operations
Python follows the usual mathematical order: brackets, powers, then multiplication/division, then addition/subtraction — left to right within a level.
2 + 3 * 4 # 14
(2 + 3) * 4 # 20
-2 ** 2 # -4 (the power happens before the minus)When in doubt, add brackets. They cost nothing and make code clearer.
Mixing ints and floats
An operation with a float gives a float: 3 + 1.5 is 4.5, 2 * 1.0 is 2.0. Convert explicitly with int() and float():
int(3.99) # 3 — cuts off the decimals (towards zero), it does NOT round
float(3) # 3.0Rounding
round(3.14159, 2) # 3.14
round(2.675, 2) # 2.67 (!) — see below
round(7.5) # 8
round(6.5) # 6 — halves round to the nearest EVEN numberround() without a second argument returns an int. Halves go to the nearest even number (“banker’s rounding”), which avoids bias when rounding many values. For always-down or always-up, use math.floor() and math.ceil().
Floats are approximate
>>> 0.1 + 0.2
0.30000000000000004This isn’t a Python bug; it happens in almost every language. Computers store floats in binary, and most decimal fractions (like 0.1) can’t be represented exactly — just as 1/3 can’t be written exactly in decimal. Consequences:
- round for display:
round(total, 2)or f-string formatting (Formatting output); - expect surprises when rounding: 87.40 × 1.125 ÷ 3 is exactly 32.775, but
round(…, 2)gives32.77, because the float stored is 32.77499…; - don’t compare floats with
==: usemath.isclose(a, b); - for money, consider the
decimalmodule (Decimal("0.10")) or work in whole cents.
The math module
Python’s math module has the functions a scientific calculator has:
import math
math.sqrt(16) # 4.0
math.pi # 3.141592653589793
math.floor(3.7) # 3
math.ceil(3.2) # 4
math.isclose(0.1 + 0.2, 0.3) # True
abs(-5) # 5 (abs is built in — no import needed)
max(3, 9, 4) # 9 (also built in)import math at the top of the file makes the module available; you’ll learn more about modules in Modules and imports.
Remember:
/gives floats,//rounds down,%is the remainder,**is power — and floats are approximate, so round when you display them.
Time and money calculator
Write three small calculations. (1) Convert 7,384 seconds into hours, minutes and seconds using // and %, and print 2 h 3 min 4 s. (2) A restaurant bill is 87.40 for 3 people with a 12.5% tip: print each person’s share rounded to 2 decimal places (and explain the result you get!). (3) Using the math module, print the area of a circle with radius 7 rounded to 1 decimal place, and the square root of 2 rounded to 3 places.
import math
# 1. Seconds → hours, minutes, seconds
total_seconds = 7384
# 2. Bill split with tip
bill = 87.40
people = 3
tip_rate = 0.125
# 3. Circle area and square root of 2
radius = 7import math
# 1. Seconds → hours, minutes, seconds
total_seconds = 7384
hours = total_seconds // 3600
minutes = total_seconds % 3600 // 60
seconds = total_seconds % 60
print(hours, "h", minutes, "min", seconds, "s") # 2 h 3 min 4 s
# 2. Bill split with tip
bill = 87.40
people = 3
tip_rate = 0.125
share = bill * (1 + tip_rate) / people
print("Each person pays", round(share, 2)) # 32.77 — not 32.78!
# The exact share is 32.775, but as a float it is stored as 32.77499…,
# so it rounds down. See "Floats are approximate" in the lesson.
# 3. Circle area and square root of 2
radius = 7
print("Area:", round(math.pi * radius ** 2, 1)) # 153.9
print("√2 ≈", round(math.sqrt(2), 3)) # 1.414Summary
- Python has
intfor whole numbers (any size) andfloatfor decimals. /always gives a float;//divides and rounds down;%gives the remainder;**raises to a power.- Precedence: brackets, then
**, then* / // %, then+ -; use brackets for clarity. - Floats are approximate (
0.1 + 0.2is not exactly0.3) — round for display, usedecimalfor money. - The
mathmodule adds functions likesqrt,floor,ceiland constants likepi.