Relational Operators
Relational operators also called Comparison operators. These are used to compare values. It returns either True
or False
according to the condition.
> ==> Greater than : True if left operand is greater than the right < ==> Less than : True if left operand is less than the right >= ==> Greater than or equal to : True if left operand is greater than or equal to the right <= ==> Less than or equal to : True if left operand is less than or equal to the right == ==> Equal to : True if both operands are equal != ==> Not equal to : True if operands are not equal
E.g
a = 10 b = 20 print("a > b is ", a > b) print("a >= b is ", a >= b) print("a < b is ", a < b) print("a <= b is ", a <= b) print("a == b is ", a == b) print("a != b is ", a != b)
Output
a > b is False a >= b is False a < b is True a <= b is True a == b is False a != b is True
We can apply relational operators for str types also.
a="waytoeasylearn" b="waytoeasylearn" print("a > b is ", a>b) print("a >= b is ", a>=b) print("a < b is ", a<b) print("a <= b is ", a<=b) print("a == b is ", a == b) print("a != b is ", a != b)
Output
a > b is False a >= b is True a < b is False a <= b is True a == b is True a != b is False
E.g
print(True > True) ==> False print(True >= True) ==> True print(10 > True) ==> True print(False > True) ==> False print(10 > 'waytoeasylearn') ==> TypeError: '>' not supported between instances of 'int' and 'str'
Note
Chaining of relational operators is possible. In the chaining, if all comparisons returns True then only result is True. If at least one comparison returns False then the result is False.
10<20 ==> True 10<20<30 ==> True 10<20<30<40 ==> True 10<20<30<40>50 ==> False 10==20==30==40 ==> False 10==10==10==10 ==> True
Relational Operators