Skip to Content

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


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

Note 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):

  1. Parentheses ()
  2. Exponents **
  3. Multiplication *, Division /, Floor Division //, Modulus %
  4. 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.0

Final result:

47.0

Common Uses of Modulus

The modulus operator % is extremely useful.

Check if a number is even:

n = 10 print(n % 2 == 0) # True

Check if a number is odd:

n = 7 print(n % 2 == 1) # True

Wrap 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 # -4

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

Last updated on