Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
Comparison operators, also known as relational operators, in Python are used to compare two values and determine their relationship. They return a boolean value, either True
or False
, depending on whether the comparison is true or false.
In this tutorial, we will explore the different comparison operators in Python and how to use them.
==
):The equal to operator checks if two values are equal. If they are equal, it returns True
; otherwise, it returns False
.
a = 10 b = 5 print(a == b) # Output: False
!=
):The not equal to operator checks if two values are not equal. If they are not equal, it returns True
; otherwise, it returns False
.
a = 10 b = 5 print(a != b) # Output: True
>
):The greater than operator checks if the left operand is greater than the right operand. If it is, it returns True
; otherwise, it returns False
.
a = 10 b = 5 print(a > b) # Output: True
<
):The less than operator checks if the left operand is less than the right operand. If it is, it returns True
; otherwise, it returns False
.
a = 10 b = 5 print(a < b) # Output: False
>=
):The greater than or equal to operator checks if the left operand is greater than or equal to the right operand. If it is, it returns True
; otherwise, it returns False
.
a = 10 b = 5 print(a >= b) # Output: True
<=
):The less than or equal to operator checks if the left operand is less than or equal to the right operand. If it is, it returns True
; otherwise, it returns False
.
a = 10 b = 5 print(a <= b) # Output: False
You can chain multiple comparison operators in a single expression. Python will evaluate each comparison and return True
only if all comparisons are true.
a = 10 b = 5 c = 15 print(a > b and a < c) # Output: True print(b < a < c) # Equivalent to the above expression
In conclusion, comparison operators in Python are used to compare two values and determine their relationship. They return a boolean value, either True
or False
, based on the result of the comparison. These operators are essential for making decisions and controlling the flow of your program.
Using == for equality comparison in Python:
==
operator is used for equality comparison. It checks if the values on both sides are equal.x = 5 y = 5 result = x == y # Result: True
Inequality comparison with != in Python:
!=
operator checks if the values on both sides are not equal.a = 10 b = 20 result = a != b # Result: True
Less than (<) and greater than (>) operators in Python:
<
operator checks if the left operand is less than the right, and >
checks if the left operand is greater than the right.x = 15 y = 20 less_than_result = x < y # Result: True greater_than_result = y > x # Result: True
Less than or equal to (<=) and greater than or equal to (>=) in Python:
<=
operator checks if the left operand is less than or equal to the right, and >=
checks if the left operand is greater than or equal to the right.a = 30 b = 30 less_than_equal_result = a <= b # Result: True greater_than_equal_result = b >= a # Result: True
Chaining multiple comparison operators in Python:
x = 10 y = 20 z = 30 chained_result = x < y < z # Result: True
Negating comparisons with not in Python:
not
keyword is used to negate the result of a comparison.x = 5 y = 10 not_result = not (x == y) # Result: True (since x is not equal to y)
Comparing strings and other data types in Python:
string1 = "apple" string2 = "banana" string_comparison = string1 < string2 # Result: True (based on lexicographic order)
Boolean results and truthy/falsy values in Python comparisons:
True
or False
). In Python, values are considered truthy or falsy, and these properties can affect the outcome of comparisons.x = 0 y = 10 truthy_result = x < y # Result: True (0 is falsy, so x < y is True)