Lesson 3 - Math Operations
Learning Objective
Perform arithmetic with Python’s operators, and correctly predict the result type and order of operations for a mixed expression.
Introduction
Python supports the arithmetic you’d expect — addition, subtraction, multiplication, division — plus a few operators that surprise beginners:
//for floor division%for the remainder (modulus)
Getting comfortable with these, along with the standard order of operations, is essential before you can write anything that calculates real values, from a shopping cart total to a robot’s turning angle.
Arithmetic Operators
5 + 3
# 85 - 3
# 25 * 3
# 155 / 2
# 2.5Always returns a float.
Division (/)5 // 2
# 2Rounds down to the nearest whole number.
Floor Division (//)5 % 2
# 1Returns the remainder.
Modulus (%)5 ** 3
# 125Raises a number to a power.
Exponent (**)Code
# math_operations.py
a = 17
b = 5
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("True Division:", a / b)
print("Floor Division:", a // b)
print("Modulus (remainder):", a % b)
print("Exponent:", a ** b)
# Operator precedence (PEMDAS: Parentheses, Exponents, Mult/Div, Add/Sub)
result = 2 + 3 * 4 ** 2 - (6 / 2)
print("Precedence example:", result)Expected Output
Click to see expected output
Addition: 22
Subtraction: 12
Multiplication: 85
True Division: 3.4
Floor Division: 3
Modulus (remainder): 2
Exponent: 1419857
Precedence example: 47.0Note that a / b returns a float (3.4) even though both inputs are integers, while a // b rounds down to the nearest whole number (3).
Understanding Operator Precedence
Python follows the standard mathematical order of operations (PEMDAS):
- Parentheses
() - Exponents
** - Multiplication
*, Division/, Floor Division//, Modulus% - Addition
+, Subtraction-
Example:
result = 2 + 3 * 4 ** 2 - (6 / 2)Evaluation order:
4 ** 2 -> 16
3 * 16 -> 48
6 / 2 -> 3.0
2 + 48 - 3.0 -> 47.0Final result:
47.0Common Uses of Modulus
The modulus operator % is extremely useful.
Check if a number is even:
n = 10
print(n % 2 == 0)
# TrueCheck if a number is odd:
n = 7
print(n % 2 == 1)
# TrueWrap values into a range:
heading = 370
print(heading % 360)
# 10🔧 Under the Hood
Why does Python have two division operators?
Most languages give you a single /, and its behavior depends on the operand types. Dividing two integers in some languages may silently truncate the result, which often surprises beginners.
Python separates the two ideas explicitly:
True Division
7 / 2
# 3.5/ always returns a floating-point result.
Floor Division
7 // 2
# 3// rounds down to the nearest whole number.
For negative values:
-7 // 2
# -4Notice that Python rounds toward negative infinity, not toward zero.
The modulus operator % naturally pairs with floor division.
For any values a and b:
a == (a // b) * b + (a % b)This relationship makes % useful for tasks such as:
- Checking even/odd numbers
- Cycling through indexes
- Working with clocks and angles
- Wrapping values into fixed ranges
Student Challenge
Reflection Question
The precedence example mixed
+,*,**,-, and parentheses in one line. Why might relying purely on memorized operator precedence rules become risky as an expression grows longer, and what’s a simple habit that avoids the risk entirely?
By the end of this lesson, you should be comfortable using Python’s arithmetic operators, predicting the results of mixed expressions, understanding the difference between / and //, and using % and ** in practical calculations.