Arithmetic Operators — Doing Math with Python
You've been doing math your whole life. Five plus three equals eight. Ten minus four equals six. Now you're going to do the exact same math with Python—except Python calls them operators instead of just "math".
An operator is a symbol that tells Python to do something with values. In this lesson, you'll learn the seven arithmetic operators that let you perform calculations. You already know the concepts; Python just has its own symbols for them.
What It Is: Operators as Verbs for Numbers
Think of operators like verbs in English. Just like "run", "jump", and "walk" are actions, operators are actions you perform on numbers.
Python has seven arithmetic operators:
+Addition — combines values-Subtraction — finds the difference*Multiplication — scales values/Division — splits into parts//Floor division — counts whole groups (no decimals)%Modulus — finds the remainder**Exponentiation — raises to a power
All of these do exactly what you learned in math class. The difference is that Python needs to know what to do with types—and that's where it gets interesting.
The Seven Arithmetic Operators
Let's see all seven operators in action. Each one takes two numbers (called operands) and produces a result.
Addition and Subtraction: The Basics
# Arithmetic operators: doing math with Python
x: int = 10
y: int = 3
# Addition: combine values
add_result: int = x + y
print(f"{x} + {y} = {add_result}") # 10 + 3 = 13
# Subtraction: find the difference
sub_result: int = x - y
print(f"{x} - {y} = {sub_result}") # 10 - 3 = 7
Both addition and subtraction keep the type consistent. When you add two integers, you get an integer back. Same with subtraction.
💬 AI Colearning Prompt
"Python is designed to be helpful with numbers. But why does Python have both
/and//for division when math just has one division symbol? Explain the difference and why this matters."
Notice something: we're asking AI to explain the design choice behind operators, not just what they do. That's more useful than memorization.
Multiplication, Division, and Floor Division
# Multiplication: scaling
mul_result: int = x * y
print(f"{x} * {y} = {mul_result}") # 10 * 3 = 30
# Division: splits into parts (always float)
div_result: float = x / y
print(f"{x} / {y} = {div_result}") # 10 / 3 = 3.3333...
# Floor division: counts whole groups (integer result)
floor_result: int = x // y
print(f"{x} // {y} = {floor_result}") # 10 // 3 = 3
Here's something important: division with / always returns a float, even when both numbers are integers. This surprises many beginners because they expect 10 / 5 to give 2 (an int), but it actually gives 2.0 (a float).
Why? Because division often produces decimals, so Python designed / to always return float. If you want an integer result (discarding the decimal), you use // (floor division).
🎓 Instructor Commentary
In AI-native development, you don't memorize why Python made this choice. You understand what's happening (division produces floats, floor division gives integers) and use the right operator for your intent. Then you verify with
type()to be sure.
Let's verify this with type():
# Verify the types
print(f"Type of 10 / 3: {type(div_result)}") # <class 'float'>
print(f"Type of 10 // 3: {type(floor_result)}") # <class 'int'>
# What if we use floor division with floats?
f1: float = 10.5
f2: float = 3.0
float_floor: float = f1 // f2
print(f"Type of 10.5 // 3.0: {type(float_floor)}") # <class 'float'> (not int!)
Notice: when you floor divide two floats, the result is still a float. Python follows this rule: if either operand is a float, the result is a float.
Modulus: The Remainder Operator
# Modulus: finds the remainder
mod_result: int = x % y
print(f"{x} % {y} = {mod_result}") # 10 % 3 = 1
# (10 divided by 3 is 3 with remainder 1)
The modulus operator % is useful when you care about what's left over. For example:
- Check if a number is even:
5 % 2gives 1 (odd),4 % 2gives 0 (even) - Find the last digit:
234 % 10gives 4 - Cycle through values:
7 % 3gives 1,8 % 3gives 2,9 % 3gives 0 (then it repeats)
Exponentiation: Raising to a Power
# Exponentiation: raising to a power
exp_result: int = x ** 2
print(f"{x} ** 2 = {exp_result}") # 10 ** 2 = 100
# More examples
print(f"2 ** 3 = {2 ** 3}") # 8 (2 × 2 × 2)
print(f"5 ** 0 = {5 ** 0}") # 1 (anything to the 0th power is 1)
print(f"2 ** 10 = {2 ** 10}") # 1024 (exponents grow fast!)
Notice the symbol: ** (two asterisks), not ^. The caret symbol ^ does something different in Python (bitwise XOR), so don't use it for exponentiation.
🚀 CoLearning Challenge
Ask your AI Co-Teacher:
"I understand that arithmetic operators in Python are +, -, *, /, //, %, **. But why does
/always return a float even when dividing two integers? And when would I actually use//vs./? Give me a concrete example where using the wrong one would cause a bug."
Expected Outcome: You'll understand the design decisions behind Python's division operators and how they interact with types.
Type Behavior: Why Types Change
Here's where it gets interesting. When you combine operands of different types, Python follows rules about what type the result is.
# All integer operations
int_add: int = 5 + 3 # 8 (int)
int_mult: int = 5 * 3 # 15 (int)
# Division always gives float
int_div: float = 5 / 2 # 2.5 (float)
# Mixing int and float
mixed: float = 5 + 2.0 # 7.0 (float) - float "wins"
mixed2: float = 5 * 2.5 # 12.5 (float)
# Verify with type()
print(type(int_add)) # <class 'int'>
print(type(int_div)) # <class 'float'>
print(type(mixed)) # <class 'float'>
The pattern: When you mix int and float, the result is always float. Python considers this safe because a float can represent any integer value (though with potential precision loss for very large numbers).
✨ Teaching Tip
Use your AI tool to explore edge cases: "What happens if I try to add an integer to a string? Show me the error and explain what
TypeErrormeans."
Operator Precedence: Order Matters
Just like in math, operators have an order of operations. Multiplication happens before addition.
# Wrong order: 2 + 3 * 4
result1: int = 2 + 3 * 4
print(result1) # 14 (not 20!)
# Python evaluates: 3 * 4 = 12, then 2 + 12 = 14
# Use parentheses to control order
result2: int = (2 + 3) * 4
print(result2) # 20 (addition happens first)
Python follows PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction). But honestly, the best practice is to use parentheses. Make your intent clear.
# Real-world example: calculating total with tax
price: float = 100.0
tax_rate: float = 0.08
fee: float = 10.0
# Without parentheses, this is confusing
total1: float = price * (1 + tax_rate) + fee # 118.0 (tax then fee)
# The parentheses make it obvious: first multiply by (1 + 0.08), then add fee
total2: float = (price * (1 + tax_rate)) + fee # 118.0 (same result, clearer intent)
Parentheses aren't just for changing order—they're for clarifying your intent to anyone reading your code (including yourself a month from now).
💬 AI Colearning Prompt
"When I see
2 + 3 * 4, how does Python know to do multiplication first? Explain operator precedence. And when should I use parentheses even if they're not required?"
Putting It Together: A Complete Example
Let's see all seven operators working together:
# Working with arithmetic operators
x: int = 20
y: int = 7
# All seven operators
print(f"Addition: {x} + {y} = {x + y}") # 27
print(f"Subtraction: {x} - {y} = {x - y}") # 13
print(f"Multiplication: {x} * {y} = {x * y}") # 140
print(f"Division: {x} / {y} = {x / y}") # 2.857...
print(f"Floor division: {x} // {y} = {x // y}") # 2
print(f"Modulus: {x} % {y} = {x % y}") # 6 (remainder when 20 ÷ 7)
print(f"Exponentiation: {x} ** 2 = {x ** 2}") # 400 (20 squared)
# Verify types
print(f"\nType verification:")
print(f"Type of {x} + {y}: {type(x + y)}") # <class 'int'>
print(f"Type of {x} / {y}: {type(x / y)}") # <class 'float'>
print(f"Type of {x} // {y}: {type(x // y)}") # <class 'int'>
This example shows:
- All seven operators working
- Different types in the results (int vs. float)
- Type verification with
type()
🎓 Instructor Commentary
In AI-native development, you don't memorize these operators—you understand them by using them, checking types, and asking AI when surprised. The moment you see an unexpected result (like
10 / 3being3.3333...), you ask: "Why is this a float?" That question-asking habit is more valuable than memorization.
Common Mistakes (And Why They Happen)
Mistake 1: Expecting 5 / 2 to give 2
result: float = 5 / 2 # Gives 2.5, not 2
# Use // if you want an integer result
result_int: int = 5 // 2 # Gives 2
Mistake 2: Forgetting that mixing int and float gives float
result: float = 5 + 2.0 # Result is 2.0, not an int
# This often surprises people because 5 is an int, but the result is float
Mistake 3: Trying to use / for exponentiation
# WRONG: This is division by 2, not 2 to the power
result = 5 / 2 # 2.5 (not what you want)
# CORRECT: Use ** for exponentiation
result = 5 ** 2 # 25 (5 squared)
Try With AI
Now it's your turn to explore arithmetic operators with an AI co-teacher. These prompts build from understanding to exploration to application.
Tool Choice: Use Claude Code, Gemini CLI, or ChatGPT web—whatever you have access to.
Prompt 1: Concept Exploration (Understand)
Copy and ask your AI:
"I'm learning Python arithmetic operators. I understand +, -, *, but I'm confused about
/vs.//.
- What's the difference between 10 / 3 and 10 // 3?
- Why does Python have both?
- Which one should I use and when?
Show me examples with their types using
type()."
Expected Outcome: You'll understand that / always returns float (division with decimals), while // returns integer (counting whole groups). You'll know when to use each based on what you need the result for.
Prompt 2: Application (Apply)
Copy and ask your AI:
"Write Python code that calculates:
- Total cost = $50 base price
- Tax = 8% (multiply by 1.08)
- Delivery fee = $10
Write it with type hints for all variables. Use arithmetic operators to:
- Calculate total with tax
- Add the delivery fee
- Show the final total
Then explain: why is the final total a float and not an int? Use
type()to verify."
Expected Outcome: You'll see arithmetic operators applied to a realistic problem. You'll understand why mixing integers and floats produces floats. You'll practice reading and understanding type hints.
Prompt 3: Edge Case Discovery (Explore)
Copy and ask your AI:
"I want to understand what happens at the edges. Try these:
- 10 / 0 (divide by zero)
- 0.1 + 0.2 (floating point math)
- 10 % 0 (modulus by zero)
- 2 ** 100 (very large exponent)
For each one: Does it work or error? What's the result or error message? Why?"
Expected Outcome: You'll discover that division by zero causes an error (expected), floating point numbers have precision issues (surprising!), and Python handles large exponents well. You'll see errors as learning opportunities, not failures.
Prompt 4: Synthesis & Validation (Understand + Analyze)
Copy and ask your AI:
"I've learned that Python has 7 arithmetic operators. I remember from Chapter 14 that Python also has type hints. How do these connect?
- When I write
result: int = 5 + 3, what does the type hint: inttell me?- Why does
result: int = 5 / 2cause a problem? (Hint: 5 / 2 is a float, not int!)- Should I always use type hints with arithmetic operators?
Show me an example where the type hint helps catch a mistake."
Expected Outcome: You'll understand that type hints are promises: you're saying "this variable will be an int" and Python checks if your code keeps that promise. You'll see how arithmetic operators interact with type hints from Chapter 14. You'll appreciate why type hints matter for catching bugs early.
Safety Note: AI-generated code may contain errors. Always run it, check the results with type(), and ask "Why?" when something surprises you. If division by zero error appears, that's normal—it's telling you something can't be done.