Arithmetic Operators
Arithmetic operators are the foundation of mathematical operations in Python. From basic addition to powerful exponentiation, these operators let you perform calculations on numbers with ease.
Basic Arithmetic Operators
Python supports all the standard mathematical operations you'd expect, plus a few extras.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 3 | 13 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 10 * 3 | 30 |
/ | Division | 10 / 3 | 3.333... |
// | Floor Division | 10 // 3 | 3 |
% | Modulus | 10 % 3 | 1 |
** | Exponentiation | 10 ** 3 | 1000 |
Click Run to execute your code
/ operator always
returns a float, even when dividing two integers. In Python 2, 10 / 3 would
return 3. This tutorial uses Python 3.
Division: Regular vs Floor
Python has two division operators with different behaviors. Understanding the difference is crucial for writing correct programs.
Click Run to execute your code
// when you need a whole number result, like
calculating how many complete items fit in a container. Use / when you need
the precise decimal result.
The Modulus Operator (%)
The modulus operator returns the remainder of a division. It's incredibly useful for checking divisibility, cycling through values, and many other tasks.
Click Run to execute your code
-7 % 3 returns 2, not -1.
Order of Operations (PEMDAS)
Python follows the standard mathematical order of operations. Use parentheses to control the order explicitly.
| Priority | Operators | Description |
|---|---|---|
| 1 (highest) | () | Parentheses |
| 2 | ** | Exponentiation |
| 3 | +x, -x | Unary plus/minus |
| 4 | *, /, //, % | Multiplication, Division |
| 5 (lowest) | +, - | Addition, Subtraction |
Click Run to execute your code
Common Mistakes
1. Confusing / and //
# Wrong - unexpected float when you wanted int
pages = 100
per_chapter = 7
chapters = pages / per_chapter # 14.285714...
# Correct - use floor division for whole numbers
chapters = pages // per_chapter # 14
2. Integer division truncates toward negative infinity
# Surprising behavior with negative numbers!
print(7 // 3) # 2 (as expected)
print(-7 // 3) # -3 (not -2!)
# Floor division rounds DOWN, not toward zero
# -7 / 3 = -2.33... rounds down to -3
3. Forgetting operator precedence
# Wrong - calculates (2 + 3) last
result = 2 + 3 * 4 # 14, not 20!
# Correct - use parentheses
result = (2 + 3) * 4 # 20
4. Division by zero
# This crashes your program!
result = 10 / 0 # ZeroDivisionError!
# Always check before dividing
divisor = 0
if divisor != 0:
result = 10 / divisor
else:
print("Cannot divide by zero")
Exercise: Calculator Practice
Task: Use arithmetic operators to solve real-world calculations.
Requirements:
- Calculate the area of a rectangle (length * width)
- Calculate how many full boxes are needed (using floor division)
- Check if a number is even (using modulus)
- Calculate compound interest using exponentiation
Click Run to execute your code
Show Solution
# Given values
length = 15
width = 8
total_items = 100
items_per_box = 12
number = 42
principal = 1000
rate = 0.05
years = 3
# 1. Area of rectangle
area = length * width
print("Area:", area) # 120
# 2. Full boxes needed
full_boxes = total_items // items_per_box
print("Full boxes:", full_boxes) # 8
# 3. Check if even (remainder is 0)
is_even = number % 2 == 0
print("Is even:", is_even) # True
# 4. Compound interest: A = P(1 + r)^t
final_amount = principal * (1 + rate) ** years
print("Final amount:", final_amount) # 1157.625
Summary
- Basic operators:
+-*/work as expected - Floor division:
//rounds down to nearest integer - Modulus:
%returns the remainder of division - Exponentiation:
**raises to a power - PEMDAS: Parentheses, Exponents, Multiplication/Division, Addition/Subtraction
- Use parentheses: When in doubt, make precedence explicit
What's Next?
Now that you can perform calculations, let's learn about comparison operators - how to compare values and make decisions based on the results. These are essential for controlling program flow.
Enjoying these tutorials?