Skip to content
Kuza Ndoto Academy

Numbers & Arithmetic

Beginner → IntermediateChapter 2 · Page 8 of 564 min readUpdated 15 Nov 2026

Python is an excellent calculator. It works with whole numbers (int) and decimal numbers (float), supports all the usual maths operators and includes built-in functions for rounding, powers and more. This page covers everything you need for everyday arithmetic.

Integers

An integer is a whole number, positive or negative, with no decimal point. Python integers have no fixed size limit, so they can grow as big as your computer's memory allows.

population = 5_400_000     # underscores make big numbers readable
print(population)
print(2 ** 100)
5400000 1267650600228229401496703205376

The underscores in 5_400_000 are ignored by Python; they just help humans read the number. ** means "to the power of", so 2 ** 100 is 2 multiplied by itself 100 times.

Floats

A float is a number with a decimal point. You can also write very large or very small floats in scientific notation with e, meaning "times ten to the power of".

temperature = 23.5
distance_km = 1.5e3       # 1.5 × 10³
print(temperature)
print(distance_km)
23.5 1500.0

Floats are not perfectly exact

Computers store floats in binary, and many decimal fractions, such as 0.1, can't be stored exactly. Tiny rounding errors can appear:

print(0.1 + 0.2)
print(round(0.1 + 0.2, 2))
0.30000000000000004 0.3

This isn't a Python bug; almost every programming language behaves the same way. Round your results for display, and for money consider working in whole cents (or whole shillings) as integers.

Arithmetic operators

OperatorMeaningExampleResult
+Addition17 + 522
-Subtraction17 - 512
*Multiplication17 * 585
/Division (always gives a float)17 / 53.4
//Floor division (rounds down)17 // 53
%Modulus (remainder)17 % 52
**Power2 ** 38
print(10 / 2)
print(5 + 2.0)
print(7 * 3)
5.0 7.0 21

Two rules explain these results. First, / always returns a float, even when the answer is whole. Second, if either number in a calculation is a float, the result is a float.

Floor division and remainder in practice

// and % are perfect for splitting things into groups. Suppose four friends share a KES 2,350 bill:

bill = 2350
friends = 4

print("Exact share:", bill / friends)
print("Each pays:", bill // friends)
print("Left over:", bill % friends)
Exact share: 587.5 Each pays: 587 Left over: 2

They're equally handy for converting units, such as minutes into hours and minutes:

minutes = 150
print(minutes // 60, "hours and", minutes % 60, "minutes")
2 hours and 30 minutes
Tip

number % 2 is 0 for even numbers and 1 for odd numbers. You'll use this test constantly in if statements and loops.

Warning

// rounds down towards negative infinity, not towards zero. So -17 // 5 is -4 (because -3.4 rounded down is -4), and -17 % 5 is 3. This only matters with negative numbers, but it surprises people.

Order of operations

Python follows the same order you learnt in maths (often remembered as BODMAS or PEMDAS): brackets first, then powers, then multiplication and division, then addition and subtraction. Operators of equal rank are worked out left to right, except **, which works right to left.

print(2 + 3 * 4)
print((2 + 3) * 4)
print(2 ** 3 ** 2)
print(-2 ** 2)
14 20 512 -4

2 ** 3 ** 2 means 2 ** 9, which is 512. In -2 ** 2, the power happens before the minus sign, giving -4. When in doubt, add brackets: they make your intention clear to Python and to readers.

Useful built-in functions

print(abs(-7))            # distance from zero
print(round(3.14159, 2))  # round to 2 decimal places
print(round(7.8))         # round to nearest whole number
print(max(4, 9, 2))       # largest value
print(min(4, 9, 2))       # smallest value
print(pow(2, 5))          # same as 2 ** 5
print(divmod(17, 5))      # (floor division, remainder) together
7 3.14 8 9 2 32 (3, 2)
Common mistake

round() uses "round half to even" (also called banker's rounding): when a number is exactly halfway, it rounds to the nearest even number. So round(2.5) is 2 but round(3.5) is 4. Float storage adds another twist: round(2.675, 2) gives 2.67, because 2.675 is actually stored as a value slightly below 2.675.

The math module

For more advanced maths, Python includes a math module. The line import math makes its tools available; you'll learn more about imports on the Importing Modules page.

import math

print(math.sqrt(16))     # square root
print(math.pi)
print(math.floor(4.7))   # round down
print(math.ceil(4.2))    # round up
4.0 3.141592653589793 4 5

math.ceil() is great for questions like "how many 14-seater matatus do 45 students need?": math.ceil(45 / 14) gives 4.

Common mistake

Dividing by zero, with /, // or %, raises ZeroDivisionError and stops your program. Check that a divisor isn't zero before dividing, for example when calculating an average of zero items.

Key points

  • int is for whole numbers (no size limit); float is for decimals and may have tiny rounding errors.
  • / always returns a float; // gives the rounded-down whole part and % the remainder.
  • Python follows the usual order of operations; brackets make intentions clear.
  • abs(), round(), max(), min(), pow() and divmod() are built in; the math module adds more.
Up next · Python BasicsStringsCreate, slice and format text.
→

Tip: use the ← and → keys to move between pages.