Arithmetic Operators
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Python does the usual arithmetic, with two differences from C and Java: / always gives a float, and whole numbers can grow as large as memory allows.
Adding, subtracting and multiplying
Operator is a symbol that works on values, called operands, and gives back a new value. In 15 + 4 the symbol + is the operator and 15 and 4 are the operands.
Arithmetic needs a symbol and the values it works on. Python reads them in the usual order and gives back a new value.
a = 15b = 4print(a + b)print(a - b)print(a * b)print(a + 4.0)Mixing an int with a float gives a float, so the last line shows 19.0.
Division: / and //
Floor division is division with // that rounds the answer down to the next whole number on its left on the number line. For negative answers this means away from zero.
Sometimes you want the exact answer, sometimes only the whole part. Python gives each its own symbol.
print(10 / 4)print(10 // 4)print(6 / 2)print(7.5 // 2)print(-7 // 2)Example 1 · Packing orders into boxes
total_items = 47box_capacity = 10
full_boxes = total_items // box_capacityleftover_items = total_items % box_capacity
print(full_boxes)print(leftover_items)4 7
What this code does
//counts how many full boxes of 10 fit into 47.%gives the items that do not fill a box.- Together they split 47 into 4 boxes and 7 left over.
/ always returns a float, even for 6 / 2 (3.0). // keeps only the whole part, and 7.5 // 2 stays a float (3.0). -7 // 2 is -4: it rounds down, not toward zero.
Remainder with %
Modulo is the % operator. It gives the remainder left after dividing the first number by the second. In Python the result has the same sign as the second number.
After dividing, something may be left over. The % operator gives exactly that leftover.
print(17 % 5)print(10 % 2)print(-7 % 3)print((17 // 5) * 5 + 17 % 5)Example 2 · Splitting seconds into minutes
total_seconds = 4000minutes = total_seconds // 60seconds = total_seconds % 60
print(minutes, seconds)66 40
What this code does
4000 // 60is 66 whole minutes.4000 % 60is the 40 seconds that remain.printwith a comma puts a space between the two values.
17 % 5 is 2. -7 % 3 is 2, not -1: the result takes the sign of the right-hand number. The last line shows that // and % always fit back together to the original number.
Powers with ** and no overflow
Exponentiation is raising a number to a power with the ** operator. 2 ** 8 means 2 multiplied by itself 8 times. The ^ symbol in Python is something else (bitwise XOR).
Powers use two stars, not ^. And an int never overflows: Python simply keeps adding digits.
print(2 ** 8)print(2 ** -1)print(2 ** 100)print(-2 ** 2)Example 3 · Counting ticket codes
codes = 2 ** 64print(codes)print(codes + 1)18446744073709551616 18446744073709551617
What this code does
- A 64-bit counter would stop here in C or Java.
- Python ints keep growing, so adding 1 still works exactly.
2 ** -1 is 0.5. 2 ** 100 is exact, with no overflow. -2 ** 2 is -4 because ** is applied first and the minus sign after.
Next step
Check your understanding
9 quick questions