Python Basics · Lesson 11
Comparison & Logical Operators
Learn how Python compares values and combines conditions using comparison and logical operators. These operators are essential for decision-making and data analysis.
What you will learn
- • Equal to ==
- • Not equal !=
- • Greater than >
- • Less than <
- • Greater than or equal >=
- • Less than or equal <=
- • Logical AND
- • Logical OR
- • Logical NOT
- • Boolean results
- • Compound conditions
- • Data-analysis conditions
Part 1
Why comparison operators matter
Comparison operators compare two values and produce a Boolean result: True or False.
age = 21
print(age > 18)CHECK YOUR UNDERSTANDING
What will this code print?
Equal to ==
The == operator checks whether two values are equal.
x = 10
y = 10
print(x == y)CHECK YOUR UNDERSTANDING
What is the result of x == y?
Complete the equality comparison: 25 _____ 25
Not equal !=
The != operator checks whether two values are different.
x = 10
y = 20
print(x != y)CHECK YOUR UNDERSTANDING
What will x != y return?
Complete: 50 _____ 40
Greater than >
sales = 50000
print(sales > 40000)CHECK YOUR UNDERSTANDING
What will sales > 40000 return?
Complete: 100 _____ 50
Less than <
price = 1200
print(price < 1500)CHECK YOUR UNDERSTANDING
What will price < 1500 return?
Complete: 20 _____ 50
Greater than or equal to >=
The >= operator is True when the left value is greater than or equal to the right value.
score = 60
print(score >= 60)CHECK YOUR UNDERSTANDING
What will score >= 60 return?
Less than or equal to <=
age = 18
print(age <= 18)CHECK YOUR UNDERSTANDING
What will age <= 18 return?
Complete: 25 _____ 30
Comparison operator cheat sheet
| Operator | Meaning | Example |
|---|---|---|
| == | Equal | 10 == 10 |
| != | Not equal | 10 != 20 |
| > | Greater than | 20 > 10 |
| < | Less than | 10 < 20 |
| >= | Greater than or equal | 20 >= 20 |
| <= | Less than or equal | 20 <= 25 |
Logical Operators
and
The and operator returns True only when both conditions are True.
age = 25
income = 50000
print(age >= 18 and income >= 30000)CHECK YOUR UNDERSTANDING
What will this expression return?
CHECK YOUR UNDERSTANDING
If one condition is False, what does and return?
or
The or operator returns True when at least one condition is True.
day = "Sunday"
print(day == "Saturday" or day == "Sunday")CHECK YOUR UNDERSTANDING
What will this expression return?
CHECK YOUR UNDERSTANDING
When does or return False?
not
The not operator reverses a Boolean result.
is_active = True
print(not is_active)CHECK YOUR UNDERSTANDING
What will not is_active return?
Complete: _____ True
Combining conditions
Real programs often combine multiple comparisons into one expression.
marks = 75
attendance = 85
eligible = marks >= 60 and attendance >= 75
print(eligible)CHECK YOUR UNDERSTANDING
What will eligible contain?
Predict the output
Think before running the code
x = 15
y = 10
print(x > y and y > 5)CHECK YOUR UNDERSTANDING
What will the program print?
sales = 25000
print(sales > 30000 or sales == 25000)CHECK YOUR UNDERSTANDING
What will this expression return?
Data Analytics Connection
Filtering business data
Comparison and logical operators are fundamental when filtering records based on business rules.
revenue = 150000
profit = 30000
high_value = revenue >= 100000 and profit >= 25000
print(high_value)CHECK YOUR UNDERSTANDING
Does this sale satisfy both conditions?
CHECK YOUR UNDERSTANDING
Which operator is useful when a record can satisfy either of two conditions?
Debug the condition
age = 25
if age = 18:
print("Adult")CHECK YOUR UNDERSTANDING
What is wrong with the comparison?
Correct the condition: age _____ 18
Practice
Complete the operators
Check equality: 10 _____ 10
Check difference: 10 _____ 20
Check greater than: 50 _____ 20
Check less than: 20 _____ 50
Both conditions must be true: condition1 _____ condition2
At least one condition must be true: condition1 _____ condition2
Reverse a Boolean result: _____ is_active
Mini Challenge
Customer eligibility
A customer is eligible for a premium offer when their total spending is at least ₹50,000 and their order count is at least 3.
spending = 65000
orders = 4
eligible = spending >= 50000 and orders >= 3
print(eligible)CHECK YOUR UNDERSTANDING
Is the customer eligible?
Final Challenge
Sales Performance Filter
Build a condition that identifies a high-performing sale. A sale is considered high-performing when revenue is at least ₹100,000 and profit is at least ₹20,000.
revenue = 125000
profit = 25000
high_performance = (
revenue >= 100000
and profit >= 20000
)
print(high_performance)Expected result: True
Lesson recap
- ✓ == checks equality.
- ✓ != checks whether values are different.
- ✓ > checks greater than.
- ✓ < checks less than.
- ✓ >= checks greater than or equal.
- ✓ <= checks less than or equal.
- ✓ and requires both conditions to be True.
- ✓ or requires at least one condition to be True.
- ✓ not reverses a Boolean result.
- ✓ Comparison expressions produce True or False.
- ✓ These operators are essential for filtering data.