Numbers & Arithmetic
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)
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)
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))
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
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 17 + 5 | 22 |
- | Subtraction | 17 - 5 | 12 |
* | Multiplication | 17 * 5 | 85 |
/ | Division (always gives a float) | 17 / 5 | 3.4 |
// | Floor division (rounds down) | 17 // 5 | 3 |
% | Modulus (remainder) | 17 % 5 | 2 |
** | Power | 2 ** 3 | 8 |
print(10 / 2) print(5 + 2.0) print(7 * 3)
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)
They're equally handy for converting units, such as minutes into hours and minutes:
minutes = 150 print(minutes // 60, "hours and", minutes % 60, "minutes")
number % 2 is 0 for even numbers and 1 for odd numbers. You'll use this test constantly in if statements and loops.
// 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)
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
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
math.ceil() is great for questions like "how many 14-seater matatus do 45 students need?": math.ceil(45 / 14) gives 4.
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
intis for whole numbers (no size limit);floatis 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()anddivmod()are built in; themathmodule adds more.
Tip: use the ← and → keys to move between pages.
