Code Examples
Example 1
The equal operator (==) checks if two values are equal. Note that this is different from the assignment operator (=). The comparison is strict - different data types or string cases will not be considered equal.
# Comparing numbers
x = 5
y = 5
result = x == y
print(result) # Output: True
# Comparing different values
a = 10
b = 15
print(a == b) # Output: False
# Comparing strings (case sensitive)
name1 = "Python"
name2 = "python"
print(name1 == name2) # Output: False
# Comparing different data types
value1 = 5
value2 = "5"
print(value1 == value2) # Output: False
Example 2
The not equal operator (!=) checks if two values are different. It returns True when the values are not equal and False when they are equal.
# Comparing numbers
x = 10
y = 20
result = x != y
print(result) # Output: True
# Equal values
a = 7
b = 7
print(a != b) # Output: False
# Comparing strings
password = "secret123"
user_input = "guess123"
print(password != user_input) # Output: True
Example 3
The greater than operator (>) checks if the value on the left is larger than the value on the right. For strings, it compares based on alphabetical order (lexicographically) where "a" comes before "b".
# Comparing numbers
x = 15
y = 10
result = x > y
print(result) # Output: True
# Equal values
a = 7
b = 7
print(a > b) # Output: False
# Comparing strings (based on alphabetical order)
word1 = "apple"
word2 = "banana"
print(word1 > word2) # Output: False
Example 4
The less than operator (<) checks if the value on the left is smaller than the value on the right. For strings, "Alice" comes before "Bob" alphabetically, so it returns True.
# Comparing numbers
x = 5
y = 10
result = x < y
print(result) # Output: True
# Equal values
a = 7
b = 7
print(a < b) # Output: False
# Comparing strings
name1 = "Alice"
name2 = "Bob"
print(name1 < name2) # Output: True
Example 5
The greater than or equal to operator (>=) checks if the value on the left is larger than or equal to the value on the right. It returns True in both cases.
# Comparing numbers
x = 15
y = 10
result = x >= y
print(result) # Output: True
# Equal values
a = 7
b = 7
print(a >= b) # Output: True
# Less than
score = 65
passing_score = 70
print(score >= passing_score) # Output: False
Example 6
The less than or equal to operator (<=) checks if the value on the left is smaller than or equal to the value on the right. It returns True in both cases.
# Comparing numbers
x = 5
y = 10
result = x <= y
print(result) # Output: True
# Equal values
a = 7
b = 7
print(a <= b) # Output: True
# Greater than
temperature = 25
max_temp = 20
print(temperature <= max_temp) # Output: False
Example 7
Python allows you to chain multiple comparison operators together in a single expression. This provides a more readable way to check if a value falls within a range or meets multiple conditions.
# Standard way to check if a value is in a range
x = 15
result = x >= 10 and x <= 20
print(result) # Output: True
# Python allows chaining comparisons (cleaner syntax)
result = 10 <= x <= 20
print(result) # Output: True
# Works with all comparison operators
y = 5
result = 0 < y < 10 != 15
print(result) # Output: True
Example 8
Python can compare complex objects like lists. The equality operator (==) checks if the contents are the same.
# Comparing lists
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = [1, 2, 3, 4]
print(list1 == list2) # Output: True
print(list1 == list3) # Output: False