Python – Operators

Created with Sketch.

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:

OperatorDescriptionExample
>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.

OperatorDescriptionExample
andTrue if both are true>>> x and y
False
orTrue if at least one is true>>> x or y
True
notReturns True if an expression evalutes to false and vice-versa>>> not x
True

Comparison and logical operators are useful in controlling flow of program.

Leave a Reply

Your email address will not be published. Required fields are marked *