Arithmetic and Expressions
Overview
Python can serve as a powerful calculator. You'll learn how to perform mathematical operations using arithmetic operators, combine them into complex expressions, and apply order of operations to get accurate results.
What You Will Learn in This Lesson
By the end of this lesson, you will know:
- Basic arithmetic operators: Master addition, subtraction, multiplication, and division.
- Advanced operators: Learn about floor division, modulo, and exponentiation.
- Order of operations: Understand how Python evaluates expressions (PEMDAS).
- Using variables in expressions: Combine variables and numbers in calculations.
Why This Matters
Almost every program needs to do math. Whether you're calculating prices, measuring distances, or counting items, arithmetic operations are fundamental. Understanding how Python handles math will help you write correct programs.
Step 1: Basic Arithmetic Operators
Python supports all the basic math operations you learned in school. These operators work with numbers (integers and floats) to perform calculations.
Addition: +
Adds two numbers together.
5 + 3 # Returns 8
10 + 2.5 # Returns 12.5
Subtraction: -
Subtracts the second number from the first.
10 - 3 # Returns 7
5.5 - 2 # Returns 3.5
Multiplication: *
Multiplies two numbers together.
4 * 3 # Returns 12
2.5 * 4 # Returns 10.0
Division: /
Divides the first number by the second. Always returns a float.
10 / 2 # Returns 5.0
15 / 4 # Returns 3.75
Important: Division in Python always returns a float, even if the result is a whole number. So 10 / 2 returns 5.0, not 5. This is different from some other programming languages.
Mini Practice #1: Basic Math
Try It YourselfTry performing some basic calculations. Notice how Python handles different operations:
Notice: Each operation produces a result. Python evaluates the expression and gives you the answer. Notice that division returns a float (5.0) even though it's a whole number.
Step 2: Advanced Arithmetic Operators
Python also provides some advanced operators that are useful for specific types of calculations.
Floor Division: //
Divides and rounds down to the nearest whole number. Returns an integer.
15 // 4 # Returns 3
10 // 3 # Returns 3
Useful when you need whole number results.
Modulo: %
Returns the remainder after division. Very useful for checking if numbers are even/odd.
15 % 4 # Returns 3 (remainder)
10 % 3 # Returns 1 (remainder)
8 % 2 # Returns 0 (even number)
Great for checking divisibility.
Exponentiation: **
Raises a number to a power. The first number is the base, the second is the exponent.
2 ** 3 # Returns 8 (2³)
5 ** 2 # Returns 25 (5²)
10 ** 0.5 # Returns 3.16... (square root)
Useful for powers and roots.
Parentheses: ()
Control the order of operations. Operations inside parentheses happen first.
(2 + 3) * 4 # Returns 20
2 + (3 * 4) # Returns 14
Use to override default order.
Mini Practice #2: Advanced Operators
Try It YourselfTry using the advanced operators. Notice how they work differently from basic division:
What happened? Floor division (//) gave you 3 (15 divided by 4, rounded down). Modulo (%) gave you 3 (the remainder). Exponentiation (**) gave you 16 (2 to the power of 4).
Step 3: Order of Operations (PEMDAS)
Python follows the same order of operations you learned in math class. This is often remembered as PEMDAS: Parentheses, Exponents, Multiplication, Division, Addition, Subtraction.
2 + 3 * 4 # Returns 14 (multiplication first)
(2 + 3) * 4 # Returns 20 (parentheses first)
2 ** 3 + 1 # Returns 9 (exponentiation first)
10 / 2 * 3 # Returns 15.0 (left to right)
Parentheses
Operations inside parentheses are done first. Use parentheses to control the order.
(2 + 3) * 4 # Parentheses first: 5 * 4 = 20
Exponents
Exponentiation (**) happens next, before multiplication and division.
2 ** 3 + 1 # Exponent first: 8 + 1 = 9
Multiplication & Division
Multiplication and division happen next, from left to right.
10 / 2 * 3 # Left to right: 5.0 * 3 = 15.0
Addition & Subtraction
Addition and subtraction happen last, from left to right.
10 - 3 + 2 # Left to right: 7 + 2 = 9
When in Doubt, Use Parentheses
If you're not sure about the order of operations, use parentheses to make your intent clear. This makes your code easier to read and prevents mistakes.
# Unclear order
result = 2 + 3 * 4
# Clear with parentheses
result = 2 + (3 * 4) # Makes it obvious
Mini Practice #3: Order of Operations
Try It YourselfTry these expressions. Can you predict the results before running them?
Notice: The parentheses changed the result! Without parentheses, multiplication happens first (2 + 12 = 14). With parentheses, addition happens first (5 * 4 = 20).
Step 4: Using Variables in Expressions
You can use variables in arithmetic expressions just like you use numbers. This makes your code flexible and powerful.
price = 10
quantity = 3
total = price * quantity
print(total) # Prints 30
length = 5
width = 3
height = 2
volume = length * width * height
print(volume) # Prints 30
Key Concept: When Python sees a variable in an expression, it replaces the variable with its value, then performs the calculation. So price * quantity becomes 10 * 3 if price is 10 and quantity is 3.
Step 5: Common Math Operations
Here are some common patterns you'll use when doing math in Python:
Calculating Totals
Multiply price by quantity to get total cost.
price = 5.99
quantity = 4
total = price * quantity
Finding Averages
Add numbers and divide by count.
sum = 10 + 20 + 30
average = sum / 3
Checking Even/Odd
Use modulo to check if a number is divisible by 2.
number = 8
is_even = number % 2 == 0 # True if even
Calculating Percentages
Multiply by the percentage as a decimal.
price = 100
discount = price * 0.20 # 20% discount
End-of-Lesson Exercises
Exercise 1: Calculate Area
Create variables: length = 8 and width = 5. Calculate and print the area of a rectangle (length × width).
Multiply length by width to get the area.
Exercise 2: Complex Expression
Calculate: (10 + 5) × 3 - 8. Use parentheses to control the order of operations, then print the result.
Remember PEMDAS: Parentheses first, then multiplication, then subtraction.