Introduction to Python : Logical operators

Logical operators

Logical operators are used to check the logical truthfulness in one or multiple statements. Python supports these three logical operators:

And (and) operator

The 'and' logical operator evaluates two expressions and returns True if and only if both expressions are True. Examples:

print (2 < 3 and 4 < 5)

will output True since 2 is less than 3 and 4 is less than 5.

print (2 > 3 and 4 < 5)

will output False since one of the expressions (2 > 3) evaluates to False.

Or (or) operator

The 'or' logical operator evaluates two expressions and returns True if at least one of the expressions is True. So

print (2 > 3 or 4 < 5)

will evaluate to True since the right expression (4 < 5) evaluates to True.

Not (not) operator

The 'not' logical operator is a unary operator (operator with only one operand) that complements an operand. Example

x = True
print (not x)

will output False since the complement of True is False.

 


course logo
Introduction to Python
Number of sections:
11
Content length:
Delivery formats: