Comparison and Logical Operators
Overview
Before writing conditions, you need to compare values. You'll learn about relational operators (==, !=, <, >, etc.) and logical operators (and, or, not) to build complex decision statements that evaluate multiple conditions at once.
What You Will Learn in This Lesson
By the end of this lesson, you will know:
- Comparison operators: How to compare values using ==, !=, <, >, <=, >=
- Boolean results: Understanding True and False values
- Logical operators: How to combine conditions using and, or, not
- Complex conditions: Building multi-part decision statements
Why This Matters
Comparison and logical operators are the foundation of decision-making in Python. Without them, you can't write if statements or loops. They let your program ask questions like "Is this greater than that?" or "Are both conditions true?"
Step 1: Comparison Operators
Comparison operators let you compare two values. They always return True or False (these are called boolean values). Think of them like asking questions: "Is 5 equal to 5?" → True, "Is 5 equal to 3?" → False.
== - Equal To
Checks if two values are equal. Returns True if they match exactly, False otherwise. Notice it uses TWO equals signs, not one.
5 == 5 # True
5 == 3 # False
"hi" == "hi" # True
"hi" == "Hi" # False (case matters!)
!= - Not Equal To
Checks if two values are different. Returns True if they don't match, False if they're the same.
5 != 3 # True (they're different)
5 != 5 # False (they're the same)
"hi" != "bye" # True
< - Less Than
Checks if the left value is smaller than the right value. Works with numbers.
3 < 5 # True (3 is smaller)
5 < 3 # False (5 is bigger)
10 < 10 # False (they're equal)
> - Greater Than
Checks if the left value is larger than the right value.
5 > 3 # True (5 is bigger)
3 > 5 # False (3 is smaller)
10 > 10 # False (they're equal)
<= - Less Than or Equal
Checks if the left value is smaller than OR equal to the right value.
5 <= 5 # True (equal counts!)
3 <= 5 # True (3 is smaller)
6 <= 5 # False (6 is bigger)
>= - Greater Than or Equal
Checks if the left value is larger than OR equal to the right value.
5 >= 5 # True (equal counts!)
5 >= 3 # True (5 is bigger)
5 >= 6 # False (5 is smaller)
Important: Don't confuse == (comparison) with = (assignment). Remember: == asks "are these equal?" while = means "store this value". If you use = in an if statement, you'll get an error!
Mini Practice #1: Comparison Operators
Try It YourselfTry these comparisons. Notice how each one returns True or False:
What happened? Each comparison returned either True or False. These are called boolean values (named after mathematician George Boole). When Python evaluates a comparison like 5 == 5, it checks if the statement is true or false and returns the corresponding boolean value. These True/False values are what Python uses to make decisions in if statements. Think of it like answering yes/no questions: "Is 5 equal to 5?" → True (yes), "Is 5 equal to 3?" → False (no).
Step 2: Logical Operators
Logical operators let you combine multiple conditions. They help you build complex decision statements. Think of them like connecting multiple yes/no questions together.
and - Both Must Be True
Returns True only if both conditions are True. If either is False, the result is False. It's like saying "both of these must be true".
True and True # True
True and False # False
False and False # False
Example: age >= 18 and has_license
Both must be True for the whole thing to be True
or - At Least One Must Be True
Returns True if at least one condition is True. Only False if both are False. It's like saying "at least one of these is okay".
True or True # True
True or False # True
False or False # False
Example: age >= 18 or parent_present
Only one needs to be True
not - Reverses the Result
Flips True to False and False to True. It's like saying "the opposite of".
not True # False
not False # True
not (5 > 3) # False (because 5 > 3 is True)
Example: not is_raining
Gives you the opposite
Real-World Examples
Here are some practical examples of how logical operators are used:
# Can drive: must be 18+ AND have license
can_drive = age >= 18 and has_license
# Can enter: must be 21+ OR have parent
can_enter = age >= 21 or parent_present
# Is not raining: the opposite of is_raining
go_outside = not is_raining
Notice how and requires both to be true, or only needs one, and not flips the value.
Mini Practice #2: Logical Operators
Try It YourselfTry combining conditions with logical operators:
What happened? The first comparison (age >= 18 and has_license) returned True because both parts were True: age (20) is >= 18, AND has_license is True. The and operator is like saying "both must be true". The second comparison (age < 18 or has_license) also returned True because even though age < 18 is False, has_license is True. The or operator is like saying "at least one must be true". This is how you combine multiple conditions to make more complex decisions.
Step 3: Combining Comparison and Logical Operators
You can combine comparison operators with logical operators to create complex conditions. Python evaluates them in this order: comparisons first, then not, then and, then or. Use parentheses to make the order clear.
age = 25
has_license = True
has_insurance = False
# Complex condition: must be 18+ AND have license AND insurance
can_drive = age >= 18 and has_license and has_insurance
print(can_drive) # False (missing insurance)
# Alternative: must be 18+ AND (have license OR have insurance)
can_drive_alt = age >= 18 and (has_license or has_insurance)
print(can_drive_alt) # True
Tip: Use parentheses to make the order of evaluation clear. Python will evaluate what's inside parentheses first. This makes your code easier to read and prevents mistakes.
End-of-Lesson Exercises
Exercise 1: Use Comparison Operators
Create variables: age = 20 and minimum_age = 18. Use the >= operator to check if age is greater than or equal to minimum_age, then print the result.
Use the >= operator to compare age and minimum_age.
Exercise 2: Combine Conditions
Create variables: age = 20, has_ticket = True. Check if age >= 18 AND has_ticket is True, then print the result.
Use the and operator to combine two conditions.