Skip to content

Python Logical Operators

python logical operators

Python Logical operators are used to check various conditions .Logical operators always returns a boolean value . All the logical operators and their applications are shown below in the form of a table taking x and y as True and False .

OperatorExampleDescriptionResult
andx and yReturns True if both operands are True else FalseFalse
orx or yReturns True if any of the operands is True else FalseTrue
notx not yReturns True if the operand is False else FalseFalse

Now we will see about all the logical operators in details with code –

Logical AND operator

Python Logical AND(andoperator returns True if both operands are True else returns False .

Example 1:

x = True
y = False
print("x and y =",x and y)
x and y = False

In the above code between x and y as both are not True , so it printed False in the output .

We can also refer the below table for logical AND operator .

Variable1 Variable2Result
TrueTrueTrue
TrueFalseFalse
False True False
FalseFalseFalse

Logical OR operator

Python Logical OR(or) operator returns True if any of the operands is True else returns False .

Example 1:

print("x or y =",x or y)
x or y = True

In the above code between x and y as any one(here x) is True , so it printed True in the output .

We can also refer the below table for logical OR operator –

Variable1 Variable2Result
TrueTrueTrue
TrueFalse True
False True True
FalseFalseFalse

Logical NOT operator

Python Logical NOT(not) operator returns True if the operand is False else returns False .

Example 1:

print("not x =",not x)
print("not y =",not y)
not x = False
not y = True

In the above code , in first case , as the value of x is True ,so not x returns False . In the second case , as the value of y is False , so not y returns True .

We can also refer the below table for logical NOT operator –

Variable Result
TrueFalse
FalseTrue

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply