← Back to Interactive Learning

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?

Fill in the Blank

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?

Fill in the Blank

Complete: 50 _____ 40

Greater than >

sales = 50000

print(sales > 40000)

CHECK YOUR UNDERSTANDING

What will sales > 40000 return?

Fill in the Blank

Complete: 100 _____ 50

Less than <

price = 1200

print(price < 1500)

CHECK YOUR UNDERSTANDING

What will price < 1500 return?

Fill in the Blank

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?

Fill in the Blank

Complete: 25 _____ 30

Comparison operator cheat sheet

OperatorMeaningExample
==Equal10 == 10
!=Not equal10 != 20
>Greater than20 > 10
<Less than10 < 20
>=Greater than or equal20 >= 20
<=Less than or equal20 <= 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?

Fill in the Blank

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?

Fill in the Blank

Correct the condition: age _____ 18

Practice

Complete the operators

Fill in the Blank

Check equality: 10 _____ 10

Fill in the Blank

Check difference: 10 _____ 20

Fill in the Blank

Check greater than: 50 _____ 20

Fill in the Blank

Check less than: 20 _____ 50

Fill in the Blank

Both conditions must be true: condition1 _____ condition2

Fill in the Blank

At least one condition must be true: condition1 _____ condition2

Fill in the Blank

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.