Lesson 1.3 – Operators
โ Operators โ Do the Math (and More!)
Operators are the action buttons of Python โ they add, subtract, compare, and combine. Learn the critical difference between = and ==, and unlock the mystery of the % operator.
๐๏ธ Key Terms First
New words in this lesson. Tap a card to flip it and see what each term means before you start practicing.
Math Operators
These work just like a calculator โ with two bonus operators you might not have seen before:
| Operator | Name | Example | Result | Plain English |
|---|---|---|---|---|
| + | Addition | 5 + 3 | 8 | Add |
| - | Subtraction | 10 - 4 | 6 | Subtract |
| * | Multiplication | 3 * 4 | 12 | Multiply |
| / | Division | 15 / 3 | 5.0 | Divide (always gives a float!) |
| ** | Exponent | 2 ** 3 | 8 | 2 to the power of 3 |
| % | Modulo | 10 % 3 | 1 | Remainder after dividing |
10 % 3 gives you. Modulo is used all the time to check if a number is even or odd!
health = 100
damage = 35
health = health - damage
print("Health:", health) # 65
score = 50
bonus = 25
total = score + bonus
print("Total:", total) # 75
print(10 % 2) # 0 โ 10 is even (no remainder)
print(11 % 2) # 1 โ 11 is odd (remainder of 1)
print(2 ** 8) # 256 โ used in computer memory sizes!
Comparison Operators
Comparison operators ask a question about two values. The answer is always True or False โ a boolean! These are what power every if/else decision in your programs.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Is equal to? | 5 == 5 | True |
| != | Is NOT equal to? | 5 != 3 | True |
| > | Is greater than? | 10 > 7 | True |
| < | Is less than? | 3 < 1 | False |
| >= | Greater than or equal? | 5 >= 5 | True |
| <= | Less than or equal? | 4 <= 3 | False |
= is the assignment operator โ it puts a value into a variable.== is the comparison operator โ it asks "are these two things equal?"Writing
if score = 100: will cause an error. It must be if score == 100:
The + Operator with Strings
The + operator doesn't just add numbers โ it can also join strings together. Combining strings is called concatenation (say it: con-CAT-en-AY-shun).
first_name = "Alex"
last_name = "Coder"
# Joining with + (don't forget the space in the middle!)
full_name = first_name + " " + last_name
print(full_name) # Alex Coder
# f-strings do the same thing but are easier to read:
print(f"Hello, {first_name}!") # Hello, Alex!
๐งฎ Interactive Operator Explorer
Type two numbers, pick an operator, and see what Python would output โ right here on the page!
๐ฏ Predict the Output!
Read each code snippet and type what you think Python will print. Then click Check to see if you were right. No peeking โ try it in your head first!
โจ๏ธ CodeSkills Lab Exercises
What to do: Build a mini game score calculator. Start with a score of 0, add points for three different actions, then print the final score and check whether it's over 100.
- Copy the starter code into your CodeSkills Lab
- Fill in the three action scores with numbers of your choice
- Run it and check that the output makes sense
- Add one more line using
%to check if the final score is even or odd
# Exercise 3A โ Score Calculator
score = 0
# Add points for three actions (choose your own values!)
score = score + 0 # Collected a coin
score = score + 0 # Defeated an enemy
score = score + 0 # Found a treasure chest
print("Final score:", score)
print("Score over 100?", score > 100)
# BONUS: Is your score even or odd?
# Add your modulo line here:
What to do: Practice string concatenation and f-strings by building a personalized greeting. Use both methods and compare the output.
- Copy the starter code into your CodeSkills Lab
- Replace the placeholder names with your own first and last name
- Run it and confirm both print statements produce the same result
- Add a third line using an f-string that also prints your age
# Exercise 3B โ Name Builder
first_name = "First"
last_name = "Last"
age = 0
# Method 1: Concatenation with +
greeting_concat = "Hello, " + first_name + " " + last_name + "!"
print(greeting_concat)
# Method 2: f-string
greeting_fstring = f"Hello, {first_name} {last_name}!"
print(greeting_fstring)
# Add your f-string with age below:
๐ง Lesson 3 Knowledge Check
Answer all three questions, then click Check Answers to see your score!
