Module 3: Operators in Python
Comparison Operators
Comparison operators compare two values. They always give True or False — you will use them heavily in if statements soon.
About 8 minutes
- == equal to
- != not equal to
- > greater than
- < less than
- >= greater or equal
- <= less or equal
Example code
age = 18 print(age == 18) # True print(age != 21) # True print(age > 16) # True print(age < 16) # False
Use == for comparison. A single = is assignment, not comparison!
Practice
Set temperature = 32. Print whether it is below 0, equal to 32, and above 100.
Key takeaways
- Comparisons return True or False.
- == checks equality; = assigns values.
- Use comparisons before decisions (if/else).
Quick check: Comparison Operators
Question 1 of 2
What is the result of 5 == 5?