Python Comparisons
Comparison operators in Python are used to compare values and determine the relationship between them. They return boolean values (True
or False
) based on the result of the comparison.
1. Comparison Operators
1.1. Equal to (==
)
Checks if two values are equal.
Syntax:
Example:
Output:
1.2. Not equal to (!=
)
Checks if two values are not equal.
Syntax:
Example:
Output:
1.3. Greater than (>
)
Checks if the value on the left is greater than the value on the right.
Syntax:
Example:
Output:
1.4. Less than (<
)
Checks if the value on the left is less than the value on the right.
Syntax:
Example:
Output:
1.5. Greater than or equal to (>=
)
Checks if the value on the left is greater than or equal to the value on the right.
Syntax:
Example:
Output:
1.6. Less than or equal to (<=
)
Checks if the value on the left is less than or equal to the value on the right.
Syntax:
Example:
Output:
2. Chained Comparisons
Python allows chaining multiple comparison operators to form more complex conditions.
Example:
Output:
Explanation:
- The first comparison
5 < x < 15
checks ifx
is between5
and15
. - The second comparison
15 < x < 20
checks ifx
is between15
and20
.
3. Comparison with Strings
Comparison operators can also be used with strings, where they compare lexicographically.
Example:
Output:
Explanation:
- Strings are compared based on their lexicographic order (alphabetical order), so
"apple"
is less than"banana"
.
4. Comparison with Lists and Other Data Structures
Comparison operators can be used to compare lists and other data structures, typically comparing them element-wise.
Example:
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = [1, 2, 4]
print(list1 == list2) # True
print(list1 != list3) # True
print(list1 < list3) # True
Output:
Explanation:
list1
is equal tolist2
because they contain the same elements in the same order.list1
is not equal tolist3
because the elements differ.- The comparison
list1 < list3
checks the lists element-wise and determines that[1, 2, 3]
is less than[1, 2, 4]
.
Conclusion
Python comparison operators are essential for evaluating relationships between values. They allow you to make decisions and control the flow of execution in your programs. By understanding and using these operators effectively, you can write more flexible and dynamic code.