Lesson 3: Python Operators and Expressions
⏱️ 3 min read
When you write Python code, you’re mostly just performing actions on data.
- Operators are the action verbs (the symbols like
+,-, or>). - Expressions are the complete sentences (a combination of values and operators that create a final result).
1. Arithmetic Operators (The Math)
Used for standard calculations. Python has a few special tricks here!
+(Add):5 + 2➔ 7/(Divide):10 / 2➔ 5.0//(Floor Divide):10 // 3➔ 3- (Exponent/Power):
3 2➔ 9
2. Relational Operators (The Comparers)
These ask a question and always return True or False. (Notice the capital ‘T’ and ‘F’ in Python!)
==(Are they equal?):5 == 5➔ True!=(Are they different?):5 != 5➔ False>(Is it greater?):10 > 2➔ True
3. Logical Operators (The Decision Makers)
Used to string multiple conditions together. Python keeps it simple by using plain English words.
and(Both must be True):(is_raining) and (has_umbrella)or(Only one needs to be True):(is_saturday) or (is_sunday)not(Flips the answer):not (is_tired)➔ Means you are awake!
4. Assignment Operators (The Storage)
Used to save a value inside a variable.
=:score = 10(Saves 10 into the ‘score’ box)+=:score += 5(Adds 5 to the current score. The score is now 15)
current_score = 90
points_gained = 15
# An Expression updating the score using an Assignment and Arithmetic operator
current_score += points_gained
# An Expression using a Relational operator to see if they won
game_won = current_score >= 100
# Result: game_won is True!
print(game_won)
Ready to continue?
Move on to the next lesson to keep learning.
Continue: Lesson 4: Python Conditional Statements