Numbers and arithmetic
Numbers show up in almost every program you write. A shopping cart totals a price. A game updates a score. A script counts how many times something happened. Python gives you arithmetic operators that work like paper maths, plus a few more that are worth knowing from the start.
The operators
The four operators from maths (+, -, *, /) work exactly as you would expect. Python adds three more that you will use constantly: integer division, remainder, and exponentiation.
price = 12.99
quantity = 3
print(price * quantity) # 38.97
print(price + 2) # 14.99
print(price - 1.00) # 11.99| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 5 / 3 | 1.6666... |
// | Integer division | 5 // 3 | 1 |
% | Remainder | 5 % 3 | 2 |
** | Exponentiation | 5 ** 3 | 125 |
Division: / vs //
/ always gives you the exact decimal result, even if the answer is a whole number. // gives you only the whole-number part, cutting off everything after the decimal point. It does not round; it cuts:
10 / 2 # 5.0 (always float, even when it divides evenly)
10 / 3 # 3.3333333333333335
10 // 3 # 3
7 // 2 # 3
-7 // 2 # -4 (floors toward negative infinity, not toward zero)The -7 // 2 result surprises people. You will mostly use // with positive numbers where this does not come up. Keep it in the back of your mind for when negatives show up.
The remainder operator %
% gives you what is left over after integer division. If 10 // 3 is 3 (because 3 goes into 10 three times), then 10 % 3 is 1 (because 3 × 3 = 9, and 10 - 9 = 1). The most common use is checking whether a number is even or odd.
10 % 3 # 1
10 % 2 # 0 (divides evenly)
10 % 7 # 3
6 % 2 # 0 (even)
7 % 2 # 1 (odd)Exponentiation **
** raises a number to a power. Use two asterisks, not the ^ symbol (which means something else in Python):
2 ** 10 # 1024
3 ** 3 # 27
9 ** 0.5 # 3.0 (square root: raise to the power of 0.5)Operator precedence
Python follows the standard maths order: exponentiation first, then multiplication and division, then addition and subtraction. When you are unsure, use parentheses. They make the intention clear and cost nothing:
2 + 3 * 4 # 14, not 20
2 ** 3 + 1 # 9, not 512
10 - 4 / 2 # 8.0, not 3.0
(2 + 3) * 4 # 20
10 / (2 + 3) # 2.0How int and float interact
Python has a consistent rule: / always returns a decimal (even 4 / 2 gives 2.0), and any operation mixing an integer and a decimal gives a decimal. When you need a whole number, use // or convert with int().
4 / 2 # 2.0 (float, always)
4 // 2 # 2 (int)
4 + 2 # 6 (int)
4 + 2.0 # 6.0 (float)
4 * 0.5 # 2.0 (float)Float precision
There is a gotcha that surprises almost everyone at some point:
0.1 + 0.2 # 0.30000000000000004That tiny error is not a Python bug. Computers store decimal numbers in binary, and some values like 0.1 cannot be represented exactly. It is similar to how 1/3 cannot be written exactly in decimal. For most everyday calculations it does not matter. For displaying money, round() or the :.2f format specifier will keep the output tidy.
Readable number literals
Python lets you put underscores in number literals to make large numbers easier to read. Python ignores them completely; they are just for you:
population = 8_100_000_000
distance_km = 384_400
pi_approx = 3.141_592_653Useful built-in functions
abs()
abs() returns the absolute value: always positive, regardless of the sign of the input. Use it when you care about how far a number is from zero, not which direction.
abs(-5) # 5
abs(3.7) # 3.7
abs(-0.5) # 0.5round()
round() rounds to the nearest integer by default. Pass a second argument to keep a specific number of decimal places:
round(3.7) # 4
round(3.2) # 3
round(3.14159, 2) # 3.14One thing worth knowing: round(2.5) gives 2, not 3. Python rounds to the nearest even number when a value is exactly halfway between two options.
divmod()
divmod() gives you both the quotient and the remainder in a single call. It returns a pair of values you can assign to two names at once:
divmod(10, 3) # (3, 1): quotient 3, remainder 1
divmod(7, 2) # (3, 1)
divmod(9, 3) # (3, 0)
quotient, remainder = divmod(10, 3)
print(quotient) # 3
print(remainder) # 1What's the (3, 1) thing?
That is a tuple: a fixed pair of values returned together. Tuples get their own chapter. For now, pull the two values apart by assigning to two names at once, as shown above.
In practice
A tip calculator:
bill = 45.50
tip_rate = 0.18
tip = round(bill * tip_rate, 2)
total = round(bill + tip, 2)
print(f"Bill: ${bill}")
print(f"Tip: ${tip}")
print(f"Total: ${total}")round() keeps the output looking like money rather than a long run of decimal places.

