Python Arithmetic Operators (+, -, *, /, %, **, //)
What Are Arithmetic Operators in Python?
An arithmetic operator is a symbol (like + or -) that performs math operations. It works on values called operands. In the expression 5 + 3, the + is the operator and 5 and 3 are the operands.
Operators help you work with numbers and build logic for everything from simple counting to complex data analysis. For example, you can use the + operator to calculate the total price of items in a shopping cart.
Want to master Python? This lesson is part of our Free Python course covering operators, logic, loops, and more.
Explore this free Python course →The 7 Python Arithmetic Operators
Python gives you seven main arithmetic operators for common math tasks. Each one does something specific, from basic addition to finding remainders.
Here are the operators we'll cover:
- Addition (
+) - Subtraction (
-) - Multiplication (
*) - Division (
/) - Modulo (
%) - Exponentiation (
**) - Floor Division (
//)
1. Addition (+)
The addition operator adds two numbers together. It works with whole numbers (integers), decimal numbers (floats), or both.
The + operator can also join strings together. However, you can't add a number and a string directly. You need to convert the number to a string first.
x = 10
y = 5
result = x + y
print(result)
a = 12.5
b = 2.5
print(a + b)
Your output will appear here...
2. Subtraction (-)
The subtraction operator takes the right number away from the left number. It works the same way as addition for integers and floats.
The - symbol can also make a number negative. For example, z = -10 sets the variable z to negative ten.
x = 10
y = 5
result = x - y
print(result)
Your output will appear here...
3. Multiplication (*)
The multiplication operator is used to multiply two numbers. When you multiply numbers, it works exactly like normal math. If you multiply a string by a number, Python repeats that string many times.
x = 10
y = 5
result = x * y
print(result)
#with string
text = "hi"
number = 3
print(text * number)
Your output will appear here...
4. Division (/)
The division operator divides the left number by the right number. It always gives you a decimal number (float), even when the answer is a whole number. This ensures division is always accurate. You won't lose information from rounding.
x = 10
y = 5
result = x / y
print(result)
a = 10
b = 4
print(a / b)
Your output will appear here...
5. Modulo (%)
The modulo operator gives you the remainder after division. It shows what's left over when you divide one number by another. This makes it easy to check if numbers divide evenly.
One common use is checking if a number is even or odd. A number is even when dividing by 2 leaves no remainder.
x = 10
y = 3
result = x % y
print(result)
num = 14
if num % 2 == 0:
print("The number is even.")
Your output will appear here...
6. Exponentiation (**)
The exponentiation operator raises one number to the power of another. It's a quick way to do power calculations.
This does the same thing as Python's pow() function. For example, pow(2, 3) gives the same result.
x = 2
y = 3
result = x ** y
print(result)
Your output will appear here...
7. Floor Division (//)
Floor division divides two numbers and rounds down to the nearest whole number. It throws away any decimal part.
Regular division (/) gives you an exact decimal. Floor division (//) gives you just the whole number part. Use this when you only care about how many times one number fits completely into another.
x = 10
y = 3
result = x // y
print(result)
a = 15
b = 4
print(a // b)
Your output will appear here...
You're making great progress! Ready to challenge yourself with more complex calculations?
Python ExercisesArithmetic Operator Precedence
Python evaluates expressions with multiple operators in a specific order. This is called operator precedence. It works like the order of operations in math class (PEMDAS: Parentheses, Exponents, Multiplication/Division, Addition/Subtraction).
Here's the order for arithmetic operators:
- Parentheses
() - Exponentiation
** - Multiplication
*, Division/, Modulo%, Floor Division//(left to right) - Addition
+, Subtraction-(left to right)
Example:
result = 10 + 5 * 2
print(result)
result = (10 + 5) * 2
print(result)
Your output will appear here...
Common Errors and How to Fix Them
You can run into errors when using operators. The two most common are TypeError and ZeroDivisionError.
1. TypeError
A TypeError happens when you try to use an operator on incompatible types. For example, you can't add a string and a number. Python doesn't know how to combine them, so it raises an error.
Solution: Convert the number to a string using str() before combining them.
items = 5
# Correct way to fix TypeError
message = "You have " + str(items) + " items in your cart."
print(message)
Your output will appear here...
2. ZeroDivisionError
A ZeroDivisionError happens when you try to divide by zero. This is impossible in math, so Python stops and reports an error.
total = 100
count = 10
print(total / count)
Your output will appear here...
Lesson Completed
You have successfully learned about Python Arithmetic Operators, precedence rules, and how to handle common math errors.
Full Python Course
Master Python with 11+ hours of content, 50+ exercises, and real-world projects.
Enroll Now