Python Arithmetic Operators
An arithmetic operator is a maths sign that works on values. Python uses almost the same signs as school maths.
Add and subtract
Adding and taking away work just like in school maths.
print(7 + 3)
print(7 - 3)Multiply and divide
Python gives a float answer for every divide.
print(4 * 3)
print(10 / 4)
print(8 / 2)Floor division
Floor division means divide, then round down to a whole number.
print(17 // 5)The remainder
The remainder is what is left after you share a number evenly.
print(17 % 5)Power
A power means you times a number by itself that many times.
print(2 ** 3)
print(3 ** 2)Brackets change the order
Times is worked out before plus. Brackets can force another order.
print(2 + 3 * 4)
print((2 + 3) * 4)Example
total = 94
people = 4
share = total // people
left_over = total % people
print("Total:", total)
print("Each person pays:", share)
print("Left over:", left_over)Key points
- Operator: a sign that tells Python to do something with one or more values.
- Use / to divide.
- The result is always a float, a number with a point.
- Use // for floor division, which rounds the answer down to a whole number.
- Use % for the remainder, the amount left over after sharing evenly.
- Use ** for a power, which means a number times itself.
- Order inside arithmetic: brackets first, then **, then * and /, then + and -.
In this example, the total bill is 94 and there are 4 people. Python divides 94 by 4 with // and gets 23. It then finds the remainder with % and gets 2. The three print lines show the total, the share, and the left over amount.
Note
Floor division rounds down, even below zero.
So -7 // 2 gives -4, not -3.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
What does 17 % 5 give?