Numbers and math

Topic 2 of 5 in Values and variables6 min readBeginnerFree course

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

Python
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 like 1.5e6 (1,500,000).

For readability you can put underscores in long numbers: population = 8_100_000_000.

Arithmetic operators

Arithmetic operators. + adds: 7 + 2 is 9. - subtracts: 7 - 2 is 5. * multiplies: 7 * 2 is 14. / divides and always gives a float: 7 / 2 is 3.5. // floor-divides: 7 // 2 is 3. % gives the remainder (modulo): 7 % 2 is 1. ** raises to a power: 7 ** 2 is 49. Precedence, top first: brackets, then **, then * / // %, then + -; operators at the same level go left to right. 2 + 3 * 4 is 14, not 20, while (2 + 3) * 4 is 20. Use brackets whenever it helps readers.

A few that surprise newcomers:

  • / always returns a float, even when the division is exact: 8 / 2 is 4.0.
  • // (floor division) divides and rounds down: 7 // 2 is 3. With negatives it still rounds down: -7 // 2 is -4.
  • % (modulo) gives the remainder: 7 % 2 is 1. 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 ** 10 is 1024. (^ 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:

Python
total_minutes = 135
hours = total_minutes // 60      # 2
minutes = total_minutes % 60     # 15

divmod(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.

Python
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():

Python
int(3.99)      # 3 — cuts off the decimals (towards zero), it does NOT round
float(3)       # 3.0

Rounding

Python
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 number

round() 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

Python
>>> 0.1 + 0.2
0.30000000000000004

This 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) gives 32.77, because the float stored is 32.77499…;
  • don’t compare floats with ==: use math.isclose(a, b);
  • for money, consider the decimal module (Decimal("0.10")) or work in whole cents.

The math module

Python’s math module has the functions a scientific calculator has:

Python
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.

Exercise

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.

Python
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 = 7

Summary

  • Python has int for whole numbers (any size) and float for 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.2 is not exactly 0.3) — round for display, use decimal for money.
  • The math module adds functions like sqrt, floor, ceil and constants like pi.