Python Comparison Operators (==, !=, >, <, >=, <=)
What Are Comparison Operators in Python?
A comparison operator is a symbol that compares two values. It determines the relationship between them, such as whether one value is greater than, less than, or equal to another. This lets you control your program's flow based on specific conditions. For example, you can check if a user's age is over 18 before granting access.
Want to master Python? This lesson is part of our Free Python course covering operators, loops, conditions, and more.
Explore this free Python course →The 6 Python Comparison Operators
Python includes six main comparison operators. Each one serves a specific purpose and returns either True or False.
1. Equal to (==)
The equal to operator checks if two values are the same. If they match, it returns True. If they don't match, it returns False. You'll use this operator often to check conditions.
x = 10
y = 10
print(x == y) # Output: True
a = "hello"
b = "world"
print(a == b) # Output: False
Your output will appear here...
Don't confuse the comparison operator (==) with the assignment operator (=). The = operator assigns a value to a variable, while == compares two values for equality.
2. Not Equal to (!=)
The not equal to operator is the opposite of the equal to operator. It checks if two values are different. It returns True if they're different and False if they're the same.
x = 10
y = 20
print(x != y) # Output: True
a = "Python"
b = "Python"
print(a != b) # Output: False
Your output will appear here...
3. Greater Than (>)
The greater than operator checks if the left value is larger than the right value. You'll commonly use this for number comparisons, but it also works with other data types like strings.
score = 100
passing_score = 60
print(score > passing_score) # Output: True
print(15 > 25) # Output: False
Your output will appear here...
4. Less Than (<)
The less than operator checks if the left value is smaller than the right value. It works as the opposite of the greater than operator.
temperature = 15
freezing_point = 32
print(temperature < freezing_point) # Output: True
print(50 < 20) # Output: False
Your output will appear here...
5. Greater Than or Equal to (>=)
This operator checks if the left value is greater than or equal to the right value. It returns True if the left value is larger or if both values are exactly equal.
age = 18
voting_age = 18
print(age >= voting_age) # Output: True
items_in_cart = 4
shipping_minimum = 5
print(items_in_cart >= shipping_minimum) # Output: False
Your output will appear here...
6. Less Than or Equal to (<=)
The less than or equal to operator checks if the left value is smaller than or equal to the right value. It returns True if the left value is smaller or if the two values are identical.
price = 49.99
budget = 50.00
print(price <= budget) # Output: True
player_level = 9
required_level = 10
print(player_level <= required_level) # Output: True
Your output will appear here...
You're making great progress! Ready to practice challenges?
Take Python ExercisesHow Comparison Operators Work with Different Data Types
Comparison operators behave differently depending on what data types you're comparing. Understanding these differences helps you write bug-free code.
Comparing Numbers (Integers and Floats)
When you compare numbers, Python looks at their mathematical value. This works smoothly between whole numbers (integers) and decimal numbers (floats).
print(100 > 99.9) # Output: True
print(5 == 5.0) # Output: True
print(-10 < -20) # Output: False
Your output will appear here...
Comparing Strings
Python compares strings lexicographically. This means it compares strings character by character based on their Unicode value, similar to alphabetical order.
String comparison is case-sensitive. Uppercase letters have lower Unicode values than lowercase letters. This means Python considers an uppercase letter "less than" a lowercase letter.
print('apple' < 'banana') # Output: True
print('Zebra' < 'apple') # Output: True, because 'Z' comes before 'a'
print('hello' == 'Hello') # Output: False
Your output will appear here...
Comparing Booleans
You can also compare booleans. Python treats True as the number 1 and False as the number 0. This means True is always greater than False.
print(True > False) # Output: True
print(True == 1) # Output: True
print(False == 0) # Output: True
print(True != False) # Output: True
Your output will appear here...
Comparing Different Data Types
In Python 3, you can't compare most incompatible data types. Trying to compare a number to a string with > or < will raise a TypeError. This prevents unexpected logical errors in your code.
# This code will raise a TypeError
print(10 > 8)
Your output will appear here...
However, the == and != operators can compare different types. They'll almost always return True for != and False for == unless you're comparing compatible numeric types.
Chaining Comparison Operators
Python lets you chain multiple comparison operators together in a single expression. This makes your code shorter and easier to read. Python evaluates the expression from left to right as if each part is connected with and.
For example, to check if a variable age is between 18 and 65, you can write it on a single line. This is much cleaner than writing two separate conditions.
age = 30
# Chained comparison
if 18 <= age < 65:
print("Age is within the working range.")
# Traditional way
if age >= 18 and age < 65:
print("Age is within the working range.")
Your output will appear here...
Common Pitfalls and Best Practices
To write reliable code with comparison operators, keep these best practices in mind:
- Use
==for comparison, not=for assignment. A single equals sign (=) assigns a value, while a double equals sign (==) checks for equality. - Remember that string comparisons are case-sensitive. The string 'Python' is not equal to 'python'. For case-insensitive comparisons, convert both strings to the same case with
.lower()or.upper(). - Avoid using
==for floating-point numbers. Computers can introduce small precision errors when storing decimal numbers. Instead of checking for exact equality, check if the numbers are close enough for your needs.
Final Challenge: Eligibility Check
Put your knowledge to the test! Check if a user is eligible for a specific role based on age and experience.
Your Task:
- Create a variable
agewith a value of 25. - Create a variable
years_experiencewith a value of 5. - Check if
ageis greater than or equal to 21 ANDyears_experienceis greater than 3.
age = 25
years_experience = 5
is_eligible = age >= 21 and years_experience > 3
print(is_eligible)
Your output will appear here...
Lesson Completed
You have successfully learned about Python Comparison Operators, how they work with different data types, and how to chain them.
Full Python Course
Master Python with 11+ hours of content, 50+ exercises, and real-world projects.
Enroll Now