Page Contents
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 .
Operator | Example | Description | Result |
---|---|---|---|
and | x and y | Returns True if both operands are True else False | False |
or | x or y | Returns True if any of the operands is True else False | True |
not | x not y | Returns True if the operand is False else False | False |
Now we will see about all the logical operators in details with code –
Logical AND operator
Python Logical AND(and
) operator 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 | Variable2 | Result |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
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 | Variable2 | Result |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
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 |
---|---|
True | False |
False | True |
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .