What are Arithmetic Operators?
Arithmetic operators are special symbols used in Python to perform math operations on numbers.
They allow you to perform addition, subtraction, multiplication, and division — and they are the foundation of every numerical task.
Basics: The Operators You Already Know
These work exactly like they do on a calculator.
- + (Addition)
# Adds numbers
result = 10 + 5
# result is 15
- - (Subtraction)
# Subtracts numbers
result = 10 - 5
# result is 5
- * (Multiplication)
# Multiplies numbers
result = 10 * 5
# result is 50
- / (Division)
# Divides numbers. Always gives you a float (a number with a decimal point).
result = 10 / 3
# result is 3.333...
Advanced Symbols: The Ones That Actually Matter
These are the operators that cause confusion. Pay attention here.
- ** (Exponentiation): Raises the first number to the power of the second.
# 5 to the power of 2 (5²)
result = 5 ** 2
# result is 25
- // (Floor Division): Divides and then chops off the decimal part, rounding down to the nearest whole number.
# How many times does 3 go into 10 completely?
result = 10 // 3
# result is 3
Compare this to regular division (10 / 3), which gives you 3.333....
- % (Modulo): This gives you the remainder of a division. Think back to elementary school long division.
# 10 divided by 3 is 3 with a remainder of 1. Modulo gives you that remainder.
result = 10 % 3
# result is 1
This is super useful for checking if a number is even or odd. If number % 2 is 0, it's even.
Order of Operations (PEMDAS/BODMAS)
Python isn't dumb. It follows the same order of operations you learned in math class. The acronym is often remembered as PEMDAS.
- Parentheses ()
- Exponents **
- Multiplication *, Division /, Floor Division //, Modulo % (from left to right)
- Addition +, Subtraction - (from left to right)
Multiplication and division have the same level of importance, so Python just does them from left to right.
Example:
result = 100 - 25 * 3 % 4
How Python calculates this:
- 25 * 3 is 75
- 75 % 4 is 3 (75 divided by 4 is 18 with a remainder of 3)
- 100 - 3 is 97
Pro Tip:
Don't try to be a hero and memorize precedence. If an equation looks complicated, just use parentheses () to make it clear what you want to happen first. It makes your code easier to read and guarantees the correct result.
# Unclear
x = 8 / 2 + 3 * 4
# Better. Leaves no doubt.
x = (8 / 2) + (3 * 4)
FAQs
Why did 10 / 5 give me 2.0 instead of 2?
The standard division operator /
always returns a float. If you need an integer, use floor division //
or convert it: int(10 / 5)
.
What's the point of floor division //?
It's for when you only care about how many times a number fits completely into another. For example, if you have 100 items and each box holds 12, 100 // 12
tells you that you can fill 8 boxes.
I did my_variable - 20 but the variable didn't change. Why?
Performing an operation does not automatically update the variable. You need to assign the result back to the variable.
score = 100
score - 20 # This does nothing to score
# Do this instead:
score = score - 20 # Now score is 80
# Or the shortcut:
score -= 20 # Also works