Python Arithmetic Operators
Table of Contents
In the last lesson, we learned Python data types. Now let’s use numbers and values in calculations.
After learning Python Data Types, we know how Python stores numbers and other values. Now let’s use those numeric values in calculations. In this tutorial, we’ll learn about Python arithmetic operators with easy examples.
What Are Arithmetic Operators in Python?
Math is everywhere in programming. And Python makes it easy.
We already know some operators from earlier articles. But let’s go through all of them properly.
An operator is just a symbol that does something. Like + adds. - subtracts. Simple.
Let’s look at all the arithmetic operators in Python.
The Seven Arithmetic Operators in Python
Python has seven arithmetic operators.
| Operator | Name | Example |
|---|---|---|
+ | Addition | 5 + 3 |
- | Subtraction | 5 - 3 |
* | Multiplication | 5 * 3 |
/ | Division | 5 / 3 |
// | Floor Division | 5 // 3 |
% | Modulo | 5 % 3 |
** | Exponentiation | 5 ** 3 |
Let’s see each one.
Python Addition Operator (+)
If we want to add any two numbers, we use the + operator. Just like in maths.
print(5 + 3) # 8print(10 + 20) # 30print(1.5 + 2.5) # 4.0We can also add strings. This is called concatenation.
print("Hello" + " " + "World") # Hello WorldBut we cannot add a string and a number.
print("Age: " + 25) # Error!Fix: We convert the number to a string first.
print("Age: " + str(25)) # Age: 25Python Subtraction Operator (-)
If we want to subtract one number from another, we use the - operator. Same as maths.
print(10 - 3) # 7print(50 - 20) # 30print(1.5 - 0.5) # 1.0We can also subtract a negative number.
print(10 - (-3)) # 13Because subtracting a negative is like adding.
Python Multiplication Operator (*)
If we want to multiply two numbers, we use the * operator. Same as maths.
print(5 * 3) # 15print(10 * 10) # 100print(2.5 * 4) # 10.0We can also multiply a string by a number. This repeats the string.
print("Ha" * 3) # HaHaHaprint("-" * 20) # --------------------But we cannot multiply two strings.
print("Hello" * "World") # Error!Python Division Operator (/)
If we want to divide one number by another, we use the / operator. Same as maths.
print(10 / 2) # 5.0print(9 / 2) # 4.5print(10 / 3) # 3.3333333333333335Remember — division always gives a float. Always. Even if the answer is a whole number.
print(10 / 2) # 5.0 (not 5)That .0 at the end means it is a float.
Python Floor Division Operator (//)
Floor division divides and then rounds down to the nearest whole number.
print(10 // 2) # 5print(9 // 2) # 4print(10 // 3) # 3See? 9 // 2 is 4, not 4.5. It just chops off the decimal.
It does not round. It floors. Which means it goes to the lower number.
print(9 // 2) # 4print(-9 // 2) # -5Wait. Why is -9 // 2 equal to -5?
Because floor division rounds down. And -4.5 rounded down is -5.
That is a bit weird. But that is how it works.
Python Modulo Operator (%)
Modulo gives us the remainder after division.
print(10 % 3) # 1Why? Because 3 goes into 10 three times (9), and 1 is left over.
print(10 % 2) # 0Because 2 goes into 10 exactly five times. No remainder.
print(7 % 4) # 3Because 4 goes into 7 one time (4), and 3 is left over.
Modulo is very useful. For example, to check if a number is even or odd.
print(4 % 2) # 0 (even)print(5 % 2) # 1 (odd)If number % 2 == 0, it is even. Otherwise, odd.
Python Exponentiation Operator (**)
Raises a number to a power.
print(2 ** 3) # 8 (2 * 2 * 2)print(5 ** 2) # 25 (5 * 5)print(10 ** 0) # 1We can also use negative exponents.
print(2 ** -1) # 0.5Because 2 ** -1 is the same as 1 / 2.
Order of Operations in Python (PEMDAS/BODMAS)
Python follows the same math rules we learned in school. This is called PEMDAS or BODMAS.
The order is:
- Parentheses
( ) - Exponents
** - Multiplication and Division
*,/,//,% - Addition and Subtraction
+,-
print(2 + 3 * 4) # 14 (not 20)Because 3 * 4 happens first (12). Then 2 + 12 gives 14.
print((2 + 3) * 4) # 20Because parentheses come first. 2 + 3 is 5. Then 5 * 4 is 20.
We use parentheses when we want to change the order. Or when we are not sure. It makes our code clearer.
Real-Life Example: Calculating a Restaurant Bill in Python
Let’s calculate a restaurant bill.
meal = 50.00tax_rate = 0.10tip_rate = 0.15
tax = meal * tax_ratetip = meal * tip_ratetotal = meal + tax + tip
print(f"Meal: ${meal:.2f}")print(f"Tax: ${tax:.2f}")print(f"Tip: ${tip:.2f}")print(f"Total: ${total:.2f}")Output
Meal: $50.00Tax: $5.00Tip: $7.50Total: $62.50See? We calculated everything using arithmetic operators.
Common Mistakes with Python Arithmetic Operators
Mistake 1: Confusing / and //
Here’s a mistake we all make. We write 10 / 3 thinking we will get 3. But we don’t. We get 3.3333333333333335.
print(10 / 3) # 3.3333333333333335print(10 // 3) # 3Why? Because / always gives us the full decimal answer. Always. If we want just the whole number part, we use //. That’s the only difference.
Mistake 2: Forgetting Order of Operations
Let’s say we write 2 + 3 * 4. What do we get? 14? Or 20?
We get 14. Not 20.
print(2 + 3 * 4) # 14, not 20Why? Because 3 * 4 happens first. That’s 12. Then 2 + 12 gives us 14.
If we want 20, we use parentheses.
print((2 + 3) * 4) # 20Now 2 + 3 happens first. That’s 5. Then 5 * 4 is 20.
Mistake 3: Trying to Do Math with Strings
Here’s one. We write "5" + 3. And we get an error.
print("5" + 3) # Error!Why? Because “5” is a string. And 3 is a number. Python doesn’t mix them.
So what do we do? We convert the string to a number first.
print(int("5") + 3) # 8Now it works. Because both are numbers.
Mistake 4: Thinking % is Percentage
We might think % means percentage. It doesn’t. It means modulo. It gives us the remainder.
print(10 % 3) # 1 (remainder)So 10 % 3 is 1. Not 10 percent of 3.
If we want percentage, we calculate it ourselves.
print(10 * 0.10) # 1.0 (10% of 10)FAQ: Python Arithmetic Operators
What is the difference between / and // in Python?
/ gives the full decimal answer. // gives only the whole number part.
What does % do in Python?
It gives the remainder after division.
What does **mean in Python?
It means power.2 \*\* 3 is 8.
Can we add a string and a number in Python? No. We need to convert the number to a string first.
Does division always give a float in Python?
Yes. Even 10 / 2 gives 5.0, not 5.
Next, we will learn about Comparison Operators so we can continue step by step.