Operators in Python: arithmetic, division, and remainders
Numbers drive every program — damage dealt, pixels moved, a moving average stepped forward. Python's arithmetic operators are how you compute them.
The big idea
Arithmetic operators combine numbers; / always gives a float, // floors to a whole number, and % returns the remainder.
See it in code
Numbers drive every program. * multiplies — here it totals the damage from three hits:
damage = 25
hits = 3
print(damage * hits)75
25 * 3 is 75. Multiplication, addition, and subtraction behave just like you'd expect.
Python has two division operators. / gives an exact answer with a decimal, while // floors the result to a whole number:
health = 45
print(health / 2)
print(health // 2)22.5 22
45 / 2 is 22.5 (a float), but 45 // 2 is 22 (an int) — the fraction is thrown away. Which one you want depends on whether you need a whole number.
The same operators, plus one more. Alongside * and the two divisions, % hands back what's left over after dividing:
damage = 25
hits = 3
print("Total damage:", damage * hits)
health = 100
print("Half health:", health / 2)
print("Whole halves:", health // 2)
print("Leftover of 100 / 7:", 100 % 7)Total damage: 75 Half health: 50.0 Whole halves: 50 Leftover of 100 / 7: 2
100 / 2 is 50.0 (a float — note the .0), while 100 // 2 is 50 (an int). % is the remainder: 100 % 7 is 2, because 7 goes into 100 fourteen times with 2 left over. That remainder is how you test even/odd or wrap a value around.
** raises to a power for compound growth, % cycles an index around a list, // splits a pixel grid into whole cells. These operators show up in trading math, physics steps, hashing, and every place numbers meet code.
Try it yourself
Check whether a number is even with damage % 2 (0 means even). Then compute 2 ** 10 and confirm you get 1024. Notice which results come back as floats and which as ints.
The common mistake
Expecting / to give a whole number. In Python 3, 10 / 2 is 5.0, not 5 — division always produces a float. When you need an integer (a list index, a pixel count), use // instead.
What it unlocks
The values you compute feed comparison operators, and understanding int versus float is the heart of data types.