Python Arithmetic Operators
Table of Contents + โ
In the last lesson you learned about the Python Data Types Overview. Numbers were one of the main types there. Now we get to actually do something with those numbers. We want to add them. We want to multiply them. We want to split a bill or check if a number is even. For all of that you need arithmetic operators. These are the little symbols that tell Python to do math.
๐ค Why Do We Need Arithmetic Operators?
Say you are building a shopping cart. Someone buys 3 items at 250 each. You need the total. Or you split a dinner bill of 1000 between 4 friends. You want each share. You can not just stare at the numbers and hope for an answer. You need to tell the computer to calculate.
Arithmetic operators are how you say โdo this mathโ in Python. They look just like the symbols you used in school. There are also a couple of new ones for things school did not cover.
โ The Basic Four: Add, Subtract, Multiply, Divide
These four are the ones you already know from a calculator. The symbols are simple.
Here is each one doing its job:
print(10 + 3) # addprint(10 - 3) # subtractprint(10 * 3) # multiplyprint(10 / 3) # divideOutput
137303.3333333333333335Add, subtract, and multiply behave exactly how you would expect. The one to watch is divide, the / symbol.
See how 10 / 3 gave 3.3333333333333335 and not just 3? That is on purpose. In Python the / operator always gives a float. A float is a number with a decimal point. So even 10 / 2 gives 5.0, not 5. Keep that in mind. Sometimes you want a clean whole number instead. We will fix that in a second.
๐ข Floor Division: When You Want a Whole Number
Here is the pain. You have 7 cookies and 2 children. 7 / 2 gives 3.5. But you can not give half a cookie to a child. You want to know how many whole cookies each child gets. That is what floor division is for. Its symbol is //, which is two slashes.
This is the same division. But it throws away the decimal part and keeps the whole number:
print(7 // 2) # how many whole cookies each child getsprint(10 // 3)print(20 // 6)Output
333So 7 // 2 is 3, not 3.5. The // does not round to the nearest number. It always goes down to the whole number below. That is why it is called floor division. The word โfloorโ means round down.
Note
Floor division rounds down even with negative numbers. So -7 // 2 gives -4, not -3. That is because -4 is the whole number below -3.5. You will rarely hit this. But now you know.
โ Modulo: The Remainder Left Over
Okay. Floor division told us each child gets 3 whole cookies. But 2 children times 3 cookies is 6 cookies. We had 7. So 1 cookie is left over. How do we get that leftover number?
That leftover is called the remainder. The operator that gives it is %, called modulo. People often read it as โmodโ.
print(7 % 2) # 1 cookie left overprint(10 % 3) # what is left after taking out whole 3sprint(20 % 5) # divides evenly, nothing leftOutput
110Read it like this. 7 % 2 asks โafter splitting 7 into groups of 2, how much is left?โ The answer is 1. And 20 % 5 gives 0 because 5 divides 20 perfectly. Nothing is left over.
Making Modulo Click: Is a Number Even?
Here is the most common real use of %. You often need to know if a number is even or odd. Think of coloring every second row of a table. Or splitting people into two teams.
An even number divides by 2 with nothing left over. An odd number always leaves 1. So you just check the remainder when you divide by 2:
n = 8print(n % 2) # 0 means even
n = 7print(n % 2) # 1 means oddOutput
01So the rule is simple. If n % 2 is 0, the number is even. If it is 1, the number is odd. You will use this trick again and again once you start writing real programs.
โฌ๏ธ Exponent: Raising to a Power
The last operator is for powers. Say you want 2 to the power of 10. Or you want to square a number. The symbol is **, which is two stars. It is called the exponent operator.
print(2 ** 3) # 2 multiplied by itself 3 timesprint(5 ** 2) # 5 squaredprint(9 ** 0.5) # power of 0.5 is the square rootOutput
8253.0So 2 ** 3 means 2 times 2 times 2, which is 8. And 5 ** 2 is 5 squared, which is 25. Here is a nice bonus. Raising a number to the power of 0.5 gives its square root. So 9 ** 0.5 gives 3.0.
๐ All the Operators at a Glance
Here are all seven in one place. You can come back and look them up any time.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Add | 10 + 3 | 13 |
- | Subtract | 10 - 3 | 7 |
* | Multiply | 10 * 3 | 30 |
/ | Divide (gives a float) | 10 / 3 | 3.333... |
// | Floor division (rounds down) | 10 // 3 | 3 |
% | Modulo (remainder) | 10 % 3 | 1 |
** | Exponent (power) | 2 ** 3 | 8 |
๐งฎ Operator Precedence: Who Goes First?
Now, what happens when you mix operators in one line? Python does not just go left to right. It follows an order. It is the same order you learned in math class.
The short version, from first to last:
**(exponent) happens first- then
*,/,//,%(multiply and divide kinds) - then
+and-(add and subtract) last
So in 2 + 3 * 4, the multiply runs first. That gives 2 + 12, which is 14. It is not 5 * 4.
print(2 + 3 * 4) # multiply first, then addprint(2 ** 3 * 2) # power first, then multiplyOutput
1416If you want a different order, use parentheses. These are the round symbols ( ). Whatever is inside ( ) runs first, always. Parentheses let you force the order you actually want:
print((2 + 3) * 4) # parentheses force the add to run firstOutput
20See the difference? Without parentheses you got 14. With parentheses around 2 + 3, the add runs first and you get 20.
Tip
When in doubt, just add parentheses. Even if Python would have done the right order anyway, parentheses make your math clear to anyone reading it later. That includes you next month.
๐ A Combined Example
Let us put it together with that shopping cart from the start. Someone buys 3 items at 250 each. There is a flat 50 delivery fee. And they have a coupon that takes 10% off the items only.
price = 250quantity = 3delivery = 50
items_total = price * quantity # 750discount = items_total * 10 / 100 # 75final = items_total - discount + delivery
print("Items total:", items_total)print("Discount:", discount)print("You pay:", final)Output
Items total: 750Discount: 75.0You pay: 725.0Walk through it. price * quantity gives the items total of 750. Then items_total * 10 / 100 works out the 10% discount, which is 75. Multiply and divide have the same rank. So Python reads them left to right. First 750 * 10 is 7500. Then divide by 100 gives 75. Finally we subtract the discount and add delivery. That gives what the user pays.
โ ๏ธ Common Mistakes
A few traps catch almost everyone at the start. Watch for these.
-
Expecting
/to give a whole number. It never does.10 / 2is5.0, a float. Use//when you want a plain whole number. -
Mixing up
//and%. Floor division//gives the whole groups. Modulo%gives the leftover. They answer different questions. -
Using
^for power. In many places^means โto the powerโ, but not in Python. Always use**for powers.
# โ Avoid: ^ does not mean power in Pythonprint(2 ^ 3) # gives 1, not 8 โ here ^ is the bitwise XOR operator, not power
# โ
Good: ** is the power operatorprint(2 ** 3) # gives 8- Forgetting precedence.
100 - 10 * 2is80, not180. The multiply runs before the subtract. Add parentheses if you meant the other order.
โ Best Practices
Small habits that keep your math correct and easy to read.
-
Use
//when you genuinely want a whole number, like splitting people into equal teams. Use/when a decimal answer is fine. -
Reach for
% 2whenever you need an even or odd check. It is the standard way. Other coders will read it instantly. -
Add parentheses around any math that is even slightly tricky. Clear beats clever.
-
Put spaces around your operators. Write
a + b, nota+b. It reads much easier and is the normal Python style.
๐งฉ What Youโve Learned
A quick recap of what you can now do.
- โ
Do basic math with
+,-,*, and/, and remember that/always gives a float. - โ
Use
//for floor division when you want a whole number, rounded down. - โ
Use
%to get the remainder, and check even or odd withn % 2. - โ
Raise numbers to a power with
**, including square roots using** 0.5. - โ
Follow operator precedence (
**first, then*///%, then+-) and force any order you want with parentheses.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does 10 / 3 give in Python?
Why: The / operator always returns a float, so you get the full decimal result, not a whole number.
- 2
You want the whole number of times 5 fits into 17. Which gives 3?
Why: Floor division // gives the whole-number result rounded down, so 17 // 5 is 3.
- 3
How do you check if a number n is even?
Why: An even number leaves no remainder when divided by 2, so n % 2 == 0 means even.
- 4
What does 2 + 3 * 4 evaluate to?
Why: Multiply runs before add, so 3 * 4 is 12 first, then 2 + 12 gives 14.
๐ Whatโs Next?
You can do math now. But real programs also need to compare values. Think of checking if a price is greater than your budget. That is exactly what comparison operators do next.