Python logical operators are used to perform logical operations
on conditional statements.
Like a comparison operation, the result of a logical operation is a
boolean value,
either True or False.
There are three logical operators in python: and,
or and not.
The and and or
operate on two operands, so they are binary operators.
The not operates
on a single operand, so it is a unary operator.
The logical operator and
or or
is placed between two conditional expressions:
expression1 and/or expression2
The expression1 & expression2 are the two operands of the logical operator.
The and operator results in a boolean value True if both the operands are True. If one of the operands is False, the result is False.
Expression1 | Operator | Expression2 | Output |
---|---|---|---|
True | and | True | True |
True | and | False | False |
False | and | True | False |
False | and | False | False |
Let us take two comparison expressions 7 > 2 and 0 < -1. The first expression is True, the second one is False.
True and False has returned a False.
Expression | Output | Reason |
---|---|---|
5 == 5 and 3 > 2 | True | both expressions are True |
5 == 5 and 3 == 2 | False | expression1 is True, expression2 is False |
5 != 5 and 3 > 2 | False | expression1 is False, expression2 is True |
5 != 5 and 3 == 2 | False | both expressions are False |
More examples of logical and:
The or operator results in a boolean value True if any of the operands is True. If both of the operands are False, the result is False.
Expression1 | Operator | Expression2 | Output |
---|---|---|---|
True | or | True | True |
True | or | False | True |
False | or | True | True |
False | or | False | False |
Let us take the comparison expression 7 > 2 or 0 < -1. The first sub-expression is True, the second one sub-expression is False.
True or False has returned a True.
Expression | Output | Reason |
---|---|---|
5 == 5 or 3 > 2 | True | both expressions are True |
5 == 5 or 3 == 2 | True | expression1 is True, expression2 is False |
5 != 5 or 3 > 2 | True | expression1 is False, expression2 is True |
5 != 5 or 3 == 2 | False | both expressions are False |
More examples of logical or:
The logical not operator takes only one operor,
which can be a boolean expression
(or it can be a python object also, which will be discussed later when python "trythy" and "falsy" is studied).
The not is a unary operator, sits before a
single operand/expression, and reverses the boolean value of the operand/expression.
The expression not True will result in a False.
The expression not False will result in a True.
Operator | Expression | Output |
---|---|---|
not | True | False |
not | False | True |
Again,
Since 2 == 2 is True, not 2 == 2 is False.
Since 2 > 3 is False, not 2 > 3 is True.
Let us apply the logical and to a problem. Suppose we have to determine whether a given number is a two-digit odd number. That means if the given number is of two digits, and it is odd, the output should be True, else the output should be False.
In the third line, the arithmetic sub-expression
number % 2 determines the remainder of
number / 2.
If it is 1, True
is stored in the variable is_odd.
If it is 0,
a False is stored.
Observe the fourth line. if the variable
number is greater than 9,
and it is less than 100, variable
is_two_digit will hold the value
True,
else it will hold a False.
In the fifth line, if both the variables is_two_digit and
is_odd hold the value True,
True is stored in the variable is_two_digit_odd,
else a False is stored.
We finally check the value of is_two_digit_odd;
a True
will indicate that the input number is a two-digit odd, a
False will indicate that it is not a two-digit odd number.
Each print in lines 6, 7 and 8 displays
three separate values of type 'int', 'str' and 'int' in a single line.
By changing the value 98 in the second line,
you can observe the change in the output window.
--- x ---
Want to leave a message for me?