Home Course Roadmap
Home Python Foundations Comparison Operators
Lesson 11 · Operators

Python Comparison Operators

Learn how Python compares values using operators such as equal to, not equal to, greater than, and less than — the foundation of decision-making in Python programs.

30–40 minutes Beginner Python Foundations
Reading Progress 0%
01
Before You Begin

Learning Objectives

By the end of this lesson, you should be able to:

  • Understand what comparison operators do.
  • Use all six primary comparison operators.
  • Understand Boolean results such as True and False.
  • Compare variables and expressions.
  • Compare strings correctly.
  • Compare values received through input().
  • Use chained comparisons.
  • Apply comparisons to real-world programming problems.
02
Core Concept

What Are Comparison Operators?

Comparison operators are used to compare two values. The result of a comparison is always a Boolean value: True or False.

Comparisons are extremely important because they allow programs to make decisions.

Comparison operators produce Boolean results

For example, 10 > 5 evaluates to True, while 10 < 5 evaluates to False.

Basic comparison
print(10 > 5)
print(10 < 5)
print(10 == 10)
Output
True
False
True
03
Operator 1

Equal To ==

The double equals operator == checks whether two values are equal.

Equal comparison
print(10 == 10)
print(10 == 5)
Output
True
False
Do not confuse == with =

A single equals sign = assigns a value to a variable. Double equals == compares two values.

Assignment vs comparison
age = 20

print(age == 20)
Output
True
04
Operator 2

Not Equal To !=

The != operator checks whether two values are different.

Not equal example
print(10 != 5)
print(10 != 10)
Output
True
False

This is useful when a program needs to check whether something has changed or whether a value is different from an expected value.

05
Operator 3

Greater Than >

The greater-than operator checks whether the value on the left is greater than the value on the right.

Greater-than comparison
print(20 > 10)
print(5 > 15)
Output
True
False

A comparison can also involve variables.

Comparing variables
salary_a = 80000
salary_b = 65000

print(salary_a > salary_b)
Output
True
06
Operator 4

Less Than <

The less-than operator checks whether the value on the left is smaller than the value on the right.

Less-than comparison
print(5 < 10)
print(20 < 15)
Output
True
False
07
Operator 5

Greater Than or Equal To >=

The >= operator checks whether a value is greater than or equal to another value.

Greater-than-or-equal example
print(10 >= 10)
print(15 >= 10)
print(5 >= 10)
Output
True
True
False

Notice that equality is included. Therefore, 10 >= 10 is True.

08
Operator 6

Less Than or Equal To <=

The <= operator checks whether a value is less than or equal to another value.

Less-than-or-equal example
print(5 <= 10)
print(10 <= 10)
print(15 <= 10)
Output
True
True
False
09
Understanding Results

Boolean Results

Every comparison produces one of two Boolean values: True or False.

Boolean results
result = 25 > 10

print(result)
print(type(result))
Output
True
<class 'bool'>

Boolean results become especially powerful when they are used with conditional statements, which you will learn in the upcoming lessons.

Think of comparisons as questions

For example, age >= 18 asks: "Is the person's age at least 18?" Python answers with True or False.

10
Practical Python

Comparing Variables

Comparison operators can compare values stored in variables.

Variable comparison
marks = 85
passing_marks = 50

print(marks >= passing_marks)
Output
True

Comparisons can also compare the results of expressions.

Comparing expressions
print(10 + 5 > 12)
print(20 - 5 == 15)
print(4 * 3 < 10)
Output
True
True
False
11
Working with Text

Comparing Strings

Python can also compare strings.

String comparison
print("Python" == "Python")
print("Python" == "Java")
print("Python" != "Java")
Output
True
False
True
String comparison is case-sensitive

"Python" and "python" are not considered equal because uppercase and lowercase letters are different.

Case sensitivity
print("Python" == "python")
print("Hello" == "Hello")
Output
False
True
12
Interactive Programs

Comparing User Input

Comparison operators become very useful when working with information entered by a user.

Because input() returns a string, convert numeric input before comparing it with numbers.

Checking age
age = int(input("Enter your age: "))

print(age >= 18)
Example Output
Enter your age: 22
True

You can also compare text entered by a user.

Checking a username
username = input("Enter username: ")

print(username == "admin")
Example Output
Enter username: admin
True
13
Intermediate Concept

Chained Comparisons

Python allows you to combine comparisons in a clean and readable form.

For example, instead of writing two separate comparisons, you can check whether a value falls inside a range.

Checking a range
age = 25

print(18 <= age <= 60)
Output
True

The expression means:

Read it from left to right

"18 is less than or equal to age, and age is less than or equal to 60."

Another example
temperature = 25

print(20 < temperature < 30)
Output
True
14
Real-World Applications

Comparison Operators in Real Programs

Checking Exam Results

Pass or fail condition
marks = 72
passing_marks = 50

print(marks >= passing_marks)
Output
True

Checking Inventory

Stock check
stock = 7
minimum_stock = 10

print(stock < minimum_stock)
Output
True

A result of True tells the program that stock has fallen below the desired level.

Checking a Discount Eligibility

Purchase amount
purchase = 7500

print(purchase >= 5000)
Output
True

These simple comparisons become much more powerful when combined with if statements, which you will learn later in the course.

15
Troubleshooting

Common Mistakes

1. Using = instead of ==

Assignment and comparison are two completely different operations.

Remember the difference
age = 18       # Assignment
age == 18      # Comparison

2. Forgetting case sensitivity

Case-sensitive comparison
print("Python" == "python")
Output
False

3. Comparing a string with a number

A common mistake is forgetting that user input is initially a string.

Convert numeric input
age = int(input("Enter your age: "))

print(age >= 18)

4. Misreading the direction

Pay attention to which value is on the left and which is on the right.

Direction matters
print(10 > 20)
print(10 < 20)
Output
False
True
16
Practice

Practice Exercises

Try to solve each exercise yourself before checking your work.

Exercise 1 — Basic Comparisons

Predict the results of:

  • 10 > 5
  • 10 < 5
  • 10 == 10
  • 10 != 5

Exercise 2 — Greater or Equal

Create variables for a student's marks and passing marks. Check whether the student has passed.

Exercise 3 — Age Verification

Ask the user for their age and check whether they are at least 18 years old.

Exercise 4 — Stock Alert

Create a variable named stock. Check whether its value is less than 5.

Exercise 5 — String Comparison

Ask the user to enter a password and compare it with a predefined password.

Exercise 6 — Range Check

Check whether a person's age is between 18 and 60 using a chained comparison.

17
Build Something

Mini Challenge — Eligibility Checker

Build a small program that asks the user for their age and exam marks.

Your program should determine two Boolean results:

  • Whether the user is at least 18 years old.
  • Whether the user has achieved at least 50 marks.
Challenge requirements

Use input(), int(), and comparison operators. Do not use an if statement yet.

One possible solution
age = int(input("Enter your age: "))
marks = int(input("Enter your marks: "))

adult = age >= 18
passed = marks >= 50

print("Age requirement:", adult)
print("Passing requirement:", passed)
Challenge extension

Try checking whether the marks are between 50 and 100 using a chained comparison.

18
Test Yourself

Quick Knowledge Check

1. Which operator checks whether two values are equal?

2. What is the result of 10 > 5?

3. Which operator means "not equal to"?

4. What type of value does a comparison return?

5. What is the result of 10 >= 10?

6. Are string comparisons case-sensitive?

7. Which expression checks whether age is between 18 and 60?

19
Wrap Up

Lesson Summary

Comparison operators allow Python programs to compare values and produce Boolean results. They are essential for building programs that can make decisions.

Operator Meaning Example
== Equal to 10 == 10
!= Not equal to 10 != 5
> Greater than 10 > 5
< Less than 5 < 10
>= Greater than or equal 10 >= 10
<= Less than or equal 10 <= 10
Key takeaway

Comparison operators turn questions about values into True or False. These Boolean results are the foundation of Python decision-making and will become especially important when you learn conditional statements.

Ready to continue?

Complete this lesson after practicing the comparison operators and understanding Boolean results.