
Comparison Operations
Python provides comparison operators that allow you to compare values and expressions. These operators return Boolean values (True or False) based on the comparison result.
Comparison operators
Equal: ==
The equal operator ==
checks if two values are equal.
It is important to note that in Python, a single equal sign =
is for assigning values, whereas double equal signs ==
is for comparing equality.
x = 5y = 5print(x == y)
Output:
True
Not Equal: !=
The not equal operator !=
checks if two values are not equal.
x = 5y = 10print(x != y)
Output:
True
It returns True because 5 is not equal to 10.
Greater Than: >
The greater than operator >
checks if the left operand is greater than the right operand.
x = 10y = 5print(x > y)
Output:
True
It returns True because 10 is greater than 5
Greater Than or Equal To: >=
The greater than or equal to operator >=
checks if the left operand is greater than or equal to the right operand.
x = 10y = 10print(x >= y)
Output:
True
Less Than: <
The less than operator <
checks if the left operand is less than the right operand.
x = 10y = 5print(x < y)
Output:
False
It returns True because 10 is greater than 5
Less Than or Equal To: <=
The less than or equal to operator <=
checks if the left operand is less than or equal to the right operand.
x = 5y = 10print(x <= y)
Output:
True
Recap
Comparison operators in Python evaluate conditions and aid in decision-making by returning True if a condition is met and False otherwise. When combined with logical operators, they enable more complex use cases. In the next tutorial, we'll delve into logical operators and explore their use with comparison operators for advanced applications.