Chapter 1: The Basics / Lesson 6

Arithmetic Operators

6 min ยท The Basics

Arithmetic Operators

Python supports all standard mathematical operations you're familiar with. These operators allow you to perform calculations and manipulate numeric values in your programs.

Understanding arithmetic operators is fundamental to programming, as they're used in almost every program for calculations, comparisons, and data manipulation.

arithmetic.py
a = 10 b = 3 print(a + b) # Addition: 13 print(a - b) # Subtraction: 7 print(a * b) # Multiplication: 30 print(a / b) # Division: 3.333... (always returns float) print(a // b) # Floor Division: 3 (integer division, rounds down) print(a % b) # Modulus (remainder): 1 (10 รท 3 = 3 remainder 1) print(a ** b) # Exponentiation: 1000 (10 raised to the power of 3)

Detailed Operator Explanation

Let's break down each operator with more examples:

operators_detail.py
# Addition (+) print(5 + 3) # 8 print(10.5 + 2.3) # 12.8 # Subtraction (-) print(10 - 4) # 6 print(5 - 10) # -5 # Multiplication (*) print(4 * 7) # 28 print(3.5 * 2) # 7.0 # Division (/) - always returns float print(10 / 2) # 5.0 (float, not int!) print(9 / 2) # 4.5 # Floor Division (//) - rounds down to integer print(10 // 3) # 3 (not 3.333) print(17 // 5) # 3 # Modulus (%) - returns remainder print(10 % 3) # 1 (10 รท 3 = 3 remainder 1) print(15 % 4) # 3 (15 รท 4 = 3 remainder 3) print(8 % 2) # 0 (even number, no remainder) # Exponentiation (**) - power operation print(2 ** 3) # 8 (2 to the power of 3) print(5 ** 2) # 25 (5 squared) print(16 ** 0.5) # 4.0 (square root of 16)

Assignment Operators

Assignment operators provide shortcuts for updating variables. They combine an arithmetic operation with assignment:

assignment.py
x = 10 # Equivalent to: x = x + 5 x += 5 # x is now 15 print(x) # Equivalent to: x = x - 3 x -= 3 # x is now 12 # Equivalent to: x = x * 2 x *= 2 # x is now 24 # Equivalent to: x = x / 4 x /= 4 # x is now 6.0 # Equivalent to: x = x // 2 x //= 2 # x is now 3.0 # Other assignment operators y = 5 y %= 3 # y = y % 3 โ†’ y = 2 z = 2 z **= 3 # z = z ** 3 โ†’ z = 8

Operator Precedence (Order of Operations)

Python follows standard mathematical order of operations. When an expression has multiple operators, Python evaluates them in a specific order:

๐Ÿ“ Order of Operations (PEMDAS)

Parentheses (highest priority)

Exponents (**)

Multiplication, Division (*, /, //, %)

Addition, Subtraction (+, -)

precedence.py
# Multiplication happens before addition print(2 + 3 * 4) # 14 (not 20) # Calculated as: 2 + (3 * 4) = 2 + 12 = 14 # Parentheses override precedence print((2 + 3) * 4) # 20 # Calculated as: (2 + 3) * 4 = 5 * 4 = 20 # Exponents evaluate right to left print(2 ** 3 ** 2) # 512 (not 64) # Calculated as: 2 ** (3 ** 2) = 2 ** 9 = 512 # Complex expression result = (2 + 3) * 4 ** 2 - 10 # Step 1: (2 + 3) = 5 # Step 2: 4 ** 2 = 16 # Step 3: 5 * 16 = 80 # Step 4: 80 - 10 = 70 print(result) # 70

Practical Examples

Here are real-world examples using arithmetic operators:

practical.py
# Calculate total price with tax price = 100 tax_rate = 0.08 # 8% tax total = price + (price * tax_rate) print(f"Total: ${total}") # Calculate area of a rectangle length = 10 width = 5 area = length * width print(f"Area: {area} square units") # Check if number is even (modulus with 2) number = 42 if number % 2 == 0: print("Even number") # Calculate average scores = [85, 90, 88] average = (scores[0] + scores[1] + scores[2]) / 3 print(f"Average: {average}")

Important Notes

๐Ÿ’ก Division Always Returns Float

Regular division (/) always returns a float, even if the result is a whole number. Use floor division (//) if you need an integer result.

๐Ÿ’ก Modulus is Useful

The modulus operator (%) is great for checking if numbers are even/odd, wrapping around values, and many other useful patterns.

๐Ÿ’ก Use Parentheses for Clarity

When in doubt, use parentheses to make your expressions clear. It's better to be explicit than to rely on operator precedence!

๐ŸŽ‰

Lesson Complete!

Great work! Continue to the next lesson.

main.py
๐Ÿ“ค Output
Click "Run" to execute...