In programming, making decisions is one of the most important tasks. A program often needs to compare two values before deciding what action to take. For example, a banking application checks whether an account has sufficient balance before allowing a withdrawal. An online shopping website determines whether a customer qualifies for free shipping based on the order amount. A school management system checks whether a student’s marks meet the passing criteria. These decisions are possible because of comparison operators.
Python comparison operators compare two values and return a Boolean result—either True or False. These Boolean values form the foundation of decision-making in Python and are widely used with conditional statements such as if, elif, and else, loops, filtering data, and validating user input.
Comparison operators are among the most frequently used operators in Python programming. Whether you are building websites, desktop applications, automation scripts, data analysis projects, or Machine Learning applications, you will regularly compare numbers, strings, dates, and other values.
In Data Analytics, comparison operators are essential for filtering records, identifying trends, validating data, detecting outliers, and selecting rows that satisfy specific conditions. For example, an analyst may want to display customers whose purchases exceed a certain amount or employees whose salaries are greater than a predefined threshold.
In this lesson, you will learn every Python comparison operator through practical coding examples, real-world business scenarios, and Data Analytics applications.
After completing this lesson, you will be able to:
True and False).Comparison operators are operators that compare two values or expressions and return either True or False.
If the comparison is correct, Python returns True. Otherwise, it returns False.
For example:
print(10 > 5)
Output:
True
Since 10 is greater than 5, the result is True.
Another example:
print(8 == 12)
Output:
False
Because 8 is not equal to 12, Python returns False.
Comparison operators help programs make decisions based on conditions. Without them, Python would not be able to determine whether a condition is satisfied.
They are commonly used for:
if statements.Python provides the following comparison operators.
| Operator | Name | Example | Returns |
|---|---|---|---|
| == | Equal To | 10 == 10 | True |
| != | Not Equal To | 10 != 5 | True |
| > | Greater Than | 20 > 10 | True |
| < | Less Than | 5 < 9 | True |
| >= | Greater Than or Equal To | 15 >= 15 | True |
| <= | Less Than or Equal To | 10 <= 20 | True |
Each operator returns either True or False depending on whether the comparison is correct.
The equal to operator (==) checks whether two values are equal. If both values are the same, Python returns True; otherwise, it returns False.
value1 == value2
print(25 == 25)
Output:
True
print(18 == 20)
Output:
False
age = 21
minimum_age = 21
print(age == minimum_age)
Output:
True
Suppose a student must score exactly 100 marks to receive a perfect score certificate.
marks = 100
print(marks == 100)
Output:
True
The comparison confirms that the student achieved a perfect score.
The not equal to operator (!=) checks whether two values are different. If the values are different, Python returns True. If they are the same, it returns False.
value1 != value2
print(50 != 25)
Output:
True
print(75 != 75)
Output:
False
password = "Python123"
entered_password = "python123"
print(password != entered_password)
Output:
True
Python treats uppercase and lowercase letters as different characters, so these two strings are not equal.
Suppose an analyst wants to identify records where the sales target was not achieved.
target_sales = 50000
actual_sales = 45000
print(actual_sales != target_sales)
Output:
True
This comparison indicates that the actual sales differ from the target value.
In this section, you learned the fundamentals of Python comparison operators and understood how they return Boolean values (True or False). You explored the importance of comparison operators in decision-making and learned how to use the == (equal to) and != (not equal to) operators with practical programming examples, business scenarios, and Data Analytics applications. In the next section, you will learn the remaining comparison operators: >, <, >=, and <=, along with practical examples and real-world use cases.
The greater than operator (>) checks whether the value on the left is greater than the value on the right. If the condition is true, Python returns True; otherwise, it returns False.
value1 > value2
print(25 > 10)
Output:
True
print(15 > 20)
Output:
False
salary = 75000
minimum_salary = 50000
print(salary > minimum_salary)
Output:
True
A company gives free delivery for orders above ₹2,000.
order_amount = 2500
print(order_amount > 2000)
Output:
True
The less than operator (<) checks whether the value on the left is smaller than the value on the right.
value1 < value2
print(15 < 40)
Output:
True
print(50 < 30)
Output:
False
attendance = 68
print(attendance < 75)
Output:
True
This comparison indicates that the student’s attendance is below the required percentage.
The greater than or equal to operator (>=) checks whether the first value is greater than or equal to the second value.
value1 >= value2
print(20 >= 20)
Output:
True
print(18 >= 21)
Output:
False
A customer qualifies for a membership if they are at least 18 years old.
age = 21
print(age >= 18)
Output:
True
An analyst wants to identify products whose sales are at least 1,000 units.
sales = 1200
print(sales >= 1000)
Output:
True
The less than or equal to operator (<=) checks whether the first value is less than or equal to the second value.
value1 <= value2
print(25 <= 40)
Output:
True
print(80 <= 60)
Output:
False
A customer is eligible for a basic loan if the requested amount is ₹100,000 or less.
loan_amount = 90000
print(loan_amount <= 100000)
Output:
True
x = 15
y = 25
print(x < y)
print(x > y)
print(x == y)
print(x != y)
Output:
True
False
False
True
experience = 6
print(experience >= 5)
Output:
True
The employee is eligible because the experience is greater than or equal to five years.
temperature = 42
print(temperature > 40)
Output:
True
This comparison can be used to trigger a heat warning.
stock = 12
print(stock <= 20)
Output:
True
This condition indicates that the inventory is running low and may need replenishment.
| Operator | Description | Example | Result |
|---|---|---|---|
| == | Equal to | 10 == 10 | True |
| != | Not equal to | 10 != 5 | True |
| > | Greater than | 20 > 10 | True |
| < | Less than | 5 < 10 | True |
| >= | Greater than or equal to | 25 >= 25 | True |
| <= | Less than or equal to | 15 <= 20 | True |
In this section, you learned how to use the >, <, >=, and <= comparison operators. You explored their syntax, practical programming examples, business scenarios, and Data Analytics applications. In the next section, you will learn how Python compares strings and user input, followed by real-world use cases, common mistakes, and best practices for using comparison operators effectively.
Comparison operators are not limited to numbers. Python can also compare strings. When comparing strings, Python compares them character by character based on their Unicode values.
If every character matches exactly, the strings are considered equal. Since Python is case-sensitive, uppercase and lowercase letters are treated as different characters.
language = "Python"
print(language == "Python")
Output:
True
language = "Python"
print(language == "python")
Output:
False
The result is False because P and p are different characters.
city = "Dehradun"
print(city != "Delhi")
Output:
True
print("Apple" < "Banana")
Output:
True
Python compares strings alphabetically by evaluating each character from left to right.
Comparison operators are frequently used to validate user input. Programs often compare the entered value with an expected value before performing an action.
password = input("Enter password: ")
print(password == "Python123")
If the entered password matches exactly, Python returns True; otherwise, it returns False.
age = int(input("Enter your age: "))
print(age >= 18)
If the entered age is 18 or above, the program returns True.
marks = int(input("Enter your marks: "))
print(marks >= 40)
The comparison determines whether the student has achieved the minimum passing marks.
An online store offers free shipping for orders of $100 or more.
order_amount = 120
print(order_amount >= 100)
Output:
True
age = 20
print(age >= 18)
Output:
True
The comparison checks whether the person is eligible to vote.
stock = 8
print(stock > 0)
Output:
True
This comparison can be used to determine whether a product is available for purchase.
salary = 65000
print(salary >= 50000)
Output:
True
This comparison checks whether a candidate meets the minimum salary requirement.
Comparison operators are widely used in Data Analytics to filter, validate, and analyze data. Analysts compare values to identify records that satisfy specific conditions.
sales = [12000, 18000, 9000, 25000]
for amount in sales:
print(amount > 15000)
Output:
False
True
False
True
This comparison identifies sales values greater than 15,000.
marks = [85, 32, 76, 41]
for score in marks:
print(score >= 40)
Output:
True
False
True
True
The comparison determines whether each student has passed the examination.
inventory = [5, 12, 0, 18]
for item in inventory:
print(item == 0)
Output:
False
False
True
False
This comparison helps identify products that are out of stock.
=) instead of the comparison operator (==).> instead of >=, or < instead of <=, when equality should also be included.Predict the output before running each program.
print(15 > 8)
print(20 == 15)
print("Python" == "python")
print(25 <= 25)
marks = 38
print(marks >= 40)
In this section, you learned how Python compares strings, user input, and variables using comparison operators. You explored practical business scenarios, Data Analytics applications, common beginner mistakes, and best practices for writing reliable comparison expressions. In the final section, you will review the lesson summary, key takeaways, frequently asked questions, coding exercises, interview questions, a mini project, and a preview of the next lesson on Python Logical Operators.
In this lesson, you learned how Python comparison operators are used to compare two values and return a Boolean result—either True or False. These Boolean values are fundamental to decision-making in Python and are widely used in conditional statements, loops, data filtering, and validation.
You explored all six comparison operators: equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). You also learned how Python compares numbers, strings, variables, and user input.
Through practical programming examples, business scenarios, and Data Analytics applications, you saw how comparison operators help build intelligent programs that can make decisions based on conditions. These operators are essential for writing programs that validate input, filter records, determine eligibility, and process data efficiently.
True or False.== operator checks whether two values are equal.!= operator checks whether two values are different.> operator checks whether the left value is greater than the right value.< operator checks whether the left value is less than the right value.>= operator checks whether a value is greater than or equal to another value.<= operator checks whether a value is less than or equal to another value.if statements and loops.Comparison operators compare two values and return either True or False.
The == operator checks whether two values are equal.
The = operator assigns a value to a variable, while == compares two values.
The != operator checks whether two values are different.
Yes. Python treats uppercase and lowercase letters as different characters.
You can compare numbers, strings, variables, Boolean values, and many other Python objects.
Because they evaluate whether a condition is satisfied, producing a Boolean result that can be used for decision-making.
They are commonly used in conditional statements, loops, data filtering, validation, and business logic.
Yes. User input is often compared with expected values after converting it to the appropriate data type.
They help filter datasets, identify trends, validate records, detect outliers, and select data that satisfies specific conditions.
= and ==.!= operator?> and >=.Create a Python program that determines whether a student is eligible for admission based on predefined criteria.
Your program should:
This project helps you practice using comparison operators to evaluate multiple conditions that are commonly found in real-world applications.
Congratulations! You have successfully completed the lesson on Python Comparison Operators. You now understand how to compare values, interpret Boolean results, and use comparison operators to build programs that make decisions based on conditions.
In the next lesson, you will learn Python Logical Operators. You will explore the and, or, and not operators, understand how multiple conditions are combined, and learn how logical operators are used in decision-making, filtering data, authentication systems, and Data Analytics applications.