What is the difference between using the logical operators
and
and
or
in Python?
When using the
and
logical operator in Python all conditions in the statement need to be
True
for the entire expression to evaluate to
True
. If one condition evaluates to
False
then the entire expression evaluates to
False
.
When using the
or
logical operator only one condition in the entire expression needs to evaluate to
True
for the entire expression to evaluate to
True
. If all conditions are
False
then the expression evaluates to
False
.
Here is a couple of simple examples in the Python REPL:
>>> e = 10
>>> f = 100
>>> e > 0 and f > 0
True
>>> e < 0 and f > 0
False
>>> e < 0 or f > 0
True
>>> e < 0 or f < 0
False
As you can see from the basic examples above the first expression with
e > 0 and f > 0
both evaluate to
True
and because the
and
operator is used if
all conditions
are
True
then the result is
True
.
For the second expression with
e < 0 and f > 0
the first condition is
False
and even though the second condition is
True
because
all conditions
are not
True
the result is
False
.
With the third expression
e < 0 or f > 0
only the second condition is
True
and as
only one condition
needs to be
True
then the result of the expression
is
True
.
Finally, with the last expression
e < 0 or f < 0
where all conditions are
False
this produces a
False
result as no conditions evaluate to
True
.
Can You Use
&&
For
and
?
Python does not recognise the syntax
&&
which is the equivalent of
and
in other programming languages such as Javascript.
If you do type the syntax
&&
in a statement you will get a
SyntaxError
, as demonstrated below:
>>> e > 0 && f < 0
File "<stdin>", line 1
e > 0 && f < 0
^
SyntaxError: invalid syntax
With the above error, the REPL terminal is showing you where the error is and the fact it’s invalid syntax.
A Shortcut Way To Use
and
If typing 3 characters each time you want to do
and
is too tedious for you, then there is 1 mathematical character you could use which is known as the equivalent of
and
in mathematics – the humble multiplication symbol
*
.
Another way of writing the same expression instead of using
and
is to replace the operator with the mathematical symbol for multiplication in Python
*
.
In mathematics the multiplication operator works in the same manner as the logical operator
and
.
Why?
A
False
value in computer languages can numerically be represented as the number
0
. What happens when you multiply the number
0
to any other number?
You still get
0
.
Therefore, the principle behind the multiplication of numbers similarly applies to
and
conditions as each condition needs to evaluate to a number, any number, bar
0
.
Otherwise, if there’s one condition in the expression statement that evaluates to
0
then every multiplication applied will result in
0
.
And
0
evaluates to
False
as a boolean data type.
Using the same example above I’ve replaced the logical operator with the multiplication symbol:
>>> e = 1
>>> f = -1
>>> (e > 0) * (f < 0)
1
>>> (e < 0) * (f < 0)
0
As you can see from the above code wrapping the conditions in parentheses helps to properly evaluate the expression. However, now the results instead are not boolean but rather numeric: either
1
or
0
.
The principle is still the same.
Why Not Use
||
For
or
?
Similar to the double ampersand for
and
in Python other languages use
||
(double pipes) for Python’s equivalent of
or
.
Using the syntax
||
for
or
in an expression will produce a
SyntaxError
as demonstrated in the following code:
>>> e = 10
>>> f = 100
>>> e > 0 || f < 0
File "<stdin>", line 1
e > 0 || f < 0
^
SyntaxError: invalid syntax
A Shortcut Way Of Using
or
If writing 2 characters is too much there is a way of using the principle of
or
by typing just one character: the humble addition symbol
+
.
Similar to how the logical operator
and
can be replaced with the multiplication symbol so too can the logical operator
or
be replaced with the mathematical symbol for addition
+
.
How does this addition principle work?
As
False
values are represented numerically as the number zero,
True
values are represented numerically as
1
, but any number
not
0
is deemed to be
True
.
Therefore, if all conditions in your expression are
False
when adding each condition the sum of zero would also be zero. Whereas if there was just one
True
condition
Here’s an example using the same example above with the
or
operator replaced with the
+
operator:
>>> e = 10
>>> f = 100
>>> (e > 0) + (f < 0)
1
>>> (e < 0) + (f < 0)
0
Summary
Use the logical operator
and
when all conditions in your expression need to be satisfied. Use the
or
logical operator when only one condition in your expression need to be satisfied.
Instead of using the operators
and
and
or
you can use the mathematical equivalents
*
and
+
respectively.
Finally, Python does not permit the syntax equivalent of
and
with
&&
as other languages do. Also, you cannot use
||
for
or
as you will also get a
SyntaxError
.