Comparison and Logical Operators in Python
The comparison operators returns a boolean either True or False.
Assuming that x=10 and y=20, the result of the operations is also given in following table:
Operator | Description | Example |
---|---|---|
> | True if the left operand is higher than the right one | >>> x>y False |
< | True if the left operand is lower than right one | >>> x<y True |
== | True if the operands are equal | >>> x==y False |
!= | True if the operands are not equal | >>> x!=y True |
>= | True if the left operand is higher than or equal to the right one | >>> x>=y False |
<= | True if the left operand is lower than or equal to the right one | >>> x<=y True |
Logical Operators in Python
The following keywords in Python combine two Boolean expressions. They are called logical operators. Two operands should have Boolean value True or False. Assuming that x=True and y=False.
Operator | Description | Example |
---|---|---|
and | True if both are true | >>> x and y False |
or | True if at least one is true | >>> x or y True |
not | Returns True if an expression evalutes to false and vice-versa | >>> not x False |
Comparison and logical operators are useful in controlling flow of program.