Comparission operator
In Python, comparison operators are used to compare values and determine the relationship between them. These operators return a Boolean value (True or False) based on the comparison result. Here are the comparison operators in Python:
Equal to (
==
): It checks if the values on both sides of the operator are equal.
Example: 2 == 2
returns True
, 3 == 4
returns False
.
Not equal to (
!=
): It checks if the values on both sides of the operator are not equal.
Example: 2 != 2
returns False
, 3 != 4
returns True
.
Greater than (
>
): It checks if the value on the left side of the operator is greater than the value on the right side.
Example: 3 > 2
returns True
, 2 > 4
returns False
.
Less than (
<
): It checks if the value on the left side of the operator is less than the value on the right side.
Example: 2 < 3
returns True
, 4 < 2
returns False
.
Greater than or equal to (
>=
): It checks if the value on the left side of the operator is greater than or equal to the value on the right side.
Example: 3 >= 3
returns True
, 2 >= 4
returns False
.
Less than or equal to (
<=
): It checks if the value on the left side of the operator is less than or equal to the value on the right side.
Example: 2 <= 3
returns True
, 4 <= 2
returns False
.
These comparison operators can be used to compare various types of values, including numbers, strings, and other objects that support comparison operations.