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.
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
TrueandFalse. - Compare variables and expressions.
- Compare strings correctly.
-
Compare values received through
input(). - Use chained comparisons.
- Apply comparisons to real-world programming problems.
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.
For example,
10 > 5 evaluates to
True, while
10 < 5 evaluates to
False.
print(10 > 5)
print(10 < 5)
print(10 == 10)
True False True
Equal To ==
The double equals operator == checks
whether two values are equal.
print(10 == 10)
print(10 == 5)
True False
== with =
A single equals sign = assigns
a value to a variable. Double equals
== compares two values.
age = 20
print(age == 20)
True
Not Equal To !=
The != operator checks whether two
values are different.
print(10 != 5)
print(10 != 10)
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.
Greater Than >
The greater-than operator checks whether the value on the left is greater than the value on the right.
print(20 > 10)
print(5 > 15)
True False
A comparison can also involve variables.
salary_a = 80000
salary_b = 65000
print(salary_a > salary_b)
True
Less Than <
The less-than operator checks whether the value on the left is smaller than the value on the right.
print(5 < 10)
print(20 < 15)
True False
Greater Than or Equal To
>=
The >= operator checks whether a
value is greater than or equal to another value.
print(10 >= 10)
print(15 >= 10)
print(5 >= 10)
True True False
Notice that equality is included. Therefore,
10 >= 10 is True.
Less Than or Equal To
<=
The <= operator checks whether a
value is less than or equal to another value.
print(5 <= 10)
print(10 <= 10)
print(15 <= 10)
True True False
Boolean Results
Every comparison produces one of two Boolean values:
True or False.
result = 25 > 10
print(result)
print(type(result))
True <class 'bool'>
Boolean results become especially powerful when they are used with conditional statements, which you will learn in the upcoming lessons.
For example,
age >= 18 asks:
"Is the person's age at least 18?"
Python answers with True or
False.
Comparing Variables
Comparison operators can compare values stored in variables.
marks = 85
passing_marks = 50
print(marks >= passing_marks)
True
Comparisons can also compare the results of expressions.
print(10 + 5 > 12)
print(20 - 5 == 15)
print(4 * 3 < 10)
True True False
Comparing Strings
Python can also compare strings.
print("Python" == "Python")
print("Python" == "Java")
print("Python" != "Java")
True False True
"Python" and
"python" are not considered
equal because uppercase and lowercase
letters are different.
print("Python" == "python")
print("Hello" == "Hello")
False True
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.
age = int(input("Enter your age: "))
print(age >= 18)
Enter your age: 22 True
You can also compare text entered by a user.
username = input("Enter username: ")
print(username == "admin")
Enter username: admin True
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.
age = 25
print(18 <= age <= 60)
True
The expression means:
"18 is less than or equal to age, and age is less than or equal to 60."
temperature = 25
print(20 < temperature < 30)
True
Comparison Operators in Real Programs
Checking Exam Results
marks = 72
passing_marks = 50
print(marks >= passing_marks)
True
Checking Inventory
stock = 7
minimum_stock = 10
print(stock < minimum_stock)
True
A result of True tells the program
that stock has fallen below the desired level.
Checking a Discount Eligibility
purchase = 7500
print(purchase >= 5000)
True
These simple comparisons become much more powerful
when combined with if statements,
which you will learn later in the course.
Common Mistakes
1. Using = instead of ==
Assignment and comparison are two completely different operations.
age = 18 # Assignment
age == 18 # Comparison
2. Forgetting case sensitivity
print("Python" == "python")
False
3. Comparing a string with a number
A common mistake is forgetting that user input is initially a string.
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.
print(10 > 20)
print(10 < 20)
False True
Practice Exercises
Try to solve each exercise yourself before checking your work.
Exercise 1 — Basic Comparisons
Predict the results of:
10 > 510 < 510 == 1010 != 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.
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.
Use input(),
int(), and comparison
operators. Do not use an
if statement yet.
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)
Try checking whether the marks are between 50 and 100 using a chained comparison.
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?
Lesson Summary
Comparison operators allow Python programs to compare values and produce Boolean results. They are essential for building programs that can make decisions.
==
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
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.