Web Analytics

Arithmetic Operators

Beginner ~20 min read

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
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33.333...
//Floor Division10 // 33
%Modulus10 % 31
**Exponentiation10 ** 31000
Output
Click Run to execute your code
Python 3 vs Python 2: In Python 3, the / 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.

Output
Click Run to execute your code
Pro Tip: Use // 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.

Output
Click Run to execute your code
Caution: The modulus operator with negative numbers can be confusing! Python's modulus always returns a result with the same sign as the divisor, which differs from some other languages. -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, -xUnary plus/minus
4*, /, //, %Multiplication, Division
5 (lowest)+, -Addition, Subtraction
Output
Click Run to execute your code
Best Practice: When in doubt, use parentheses! They make your code clearer and ensure the calculation happens in the order you intend, even if you're not sure about operator precedence.

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
Output
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.