Lesson 5 of 12
Math in Python ➕
What You'll Learn
- Use Python's arithmetic operators
- Understand integer division and modulo
- Build a simple calculator
Arithmetic Operators
Python
# Basic arithmetic print(10 + 3) # 13 (addition) print(10 - 3) # 7 (subtraction) print(10 * 3) # 30 (multiplication) print(10 / 3) # 3.333 (division - returns float) print(10 // 3) # 3 (integer division - no remainder) print(10 % 3) # 1 (modulo - remainder only) print(2 ** 10) # 1024 (exponent - 2 to the power of 10)
Modulo Trick
The % operator is great for checking if a number is even or odd. If number % 2 == 0, it is even!
Order of Operations
Python follows PEMDAS (just like math class): Parentheses, Exponents, Multiply/Divide, Add/Subtract.
Python
print(2 + 3 * 4) # 14 (not 20 - multiply first) print((2 + 3) * 4) # 20 (parentheses first)
Math with Variables
Python
price = 5.99
quantity = 3
total = price * quantity
tax = total * 0.085
grand_total = total + tax
print('Total:', grand_total)Simple Calculator
Build a program that:
- Asks for two numbers (use float())
- Prints their sum, difference, product, and quotient
Quick Check
What does 17 % 5 equal?
What does 10 // 3 equal?
Which operator means exponent (power) in Python?