Python Logical Operators

Python Logical Operators

Python Logical Operators with Examples
Python Logical Operators with Examples

Python provides three logical operators to combine existing expressions. These are or, and, and not.
Before we proceed to the discussion of logical operators, it is important for you to know about Truth Value Testing, because in some cases logical operators base their results on truth value testing.


Truth Value Testing

Python associates with every value type, some truth value (the truthiness), i.e., Python internally categorizes them as true or false. Any object can be tested for truth value. Python considers following values false, (i.e., with truth-value as false) and true :

values with truth value as falseValues with truth value as true
NoneAll other values are considered true.
False (Boolean value False)
zero of any numeric type, for example, 0, 0.0, 0j
any empty sequence, for example, “, (), []
(please note, “is empty string ; () is empty tuple ; and [] is empty list
any empty mapping, for example, {}
The result of a relational expression can be True or False depending upon the values of its operands and the comparison taking place.

Do not confuse between Boolean values True, False and truth values (truthiness values) true, false. Simply put truth-value tests zero-ness or emptiness of a value. Boolean values belong to just one data type, i.e., Boolean type, whereas we can test truthiness for every value object in Python.

1. The or Operator

The or operator combines two expressions, which make its operands. The or operator works in these ways :
(i) relational expressions as operands (ii) numbers or strings or lists as operands

(i) Relational expressions as operands

When or operator has its operands as relational expressions (e.g., p>q, j!=k, etc.) then the or operator performs as per following principle :
The or operator evaluates to True if either of its (relational) operands evaluates to True ; False if both operands evaluates to False.

That is :

xyx or y
FalseFalseFalse
FalseTrueTrue
TrueFalseTrue
TrueTrueTrue

Following are some examples of this or operands :

(4 == 4) or (5 == 8)                            results into True because first expression (4 == 4) is True.
5 > 8 or 5 < 2                                      results into False because both expressions 5 > 8 and 5 < 2 are False.

(ii) Numbers / strings/ lists as operands

When or operator has its operands as numbers or strings or lists (e.g., ‘a’ or ”, 3 or 0, etc.) then the or operator performs as per following principle :

In an expression x or y, if first operand, (i.e., expression x) has false vale, then return second operand y as result, otherwise return x.

That is :

xyx or y
False ValueFalse Valuey
False ValueTrue Valuey
True ValueFalse Valuex
True ValueTrue Valuex

The or operator will test the second operand only if the first operand is false, otherwise ignore it ; even if the second operand is logically wrong e.g.,

20 > 10 or "a" + 1 > 1
will give you result as 
True

without checking the second operand of or i.e., “a” + 1 > 1, which is syntactically wrong – you cannot add an integer to a string.

2. The and Operator

The and operator combines two expressions, which make its operands. The and operator works in these ways :
(i) relational expressions as operands
(ii) numbers or strings or lists as operands


(i) Relational expressions as operands

When and operator has its operands as relational expressions (e.g., p>q, j! = k, etc.) then the and operator performs as per following principle :

The and operator evaluates to True if both of its (relational) operands evaluate to True ; False if either both operands evaluate to False.

That is :

xyx or y
FalseFalseFalse
FalseTrueFalse
TrueFalseFalse
TrueTrueTrue


Following are some examples of the and operation :

(4 == 4) and (5 == 8)             

results into False because first expression (4 == 4) is True but second expression (5 == 8) evaluates to False.

5 > 8 and 5 < 2 

results into False because first expression : 5 > 8 evaluates to False.

8 > 5 and 2 < 5

result into True because both operands : 8 > 5 and 2 < 5 evaluate to True.

(ii)Numbers / Strings / lists as operands

When and operator has its operands as number or strings or lists (e.g., ‘a’ or ”, 3 or 0, etc.) then the and operator performs as per following principle :

In an expression x and y, if first operand, (i.e., expression x)has false, then return first operand x as result, otherwise return y.
That is :

xyx or y
False ValueFalse Valuex
False ValueTrue Valuex
True ValueFalse Valuey
True ValueTrue Valuey

Examples

OperationResult intoReason
0 and 00first expression(0) has false value, hence first expression 0 is returned
0 and 80first expression(0) has false value, hence first expression 0 is returned
5 and 0.00.0first expression(5) has true value, hence second expression 0.0 is returned
‘hello’ and “first expression(‘hello’) has true value, hence expression ” is returned
” and ‘a’first expression(“) has false value, hence expression ” is returned
” and “first expression(“) has false value, hence expression ” is returned
‘a’ and ‘j’‘j’first expression (‘a’) has true value, hence second expression ‘j’ is returned

Important

The and operator will test second operand only if first operand is true, otherwise ignore it; even if the second operand is logically wrong e.g.,

10 > 20 abd "a"+10 <  5
will give you result as
False

ignoring the second operand completely, even if it is wrong – you cannot add an integer to a strings in Python.

3. The not Operator

The Boolean/logical not operator, works on single expression or operand i.e., it is a unary operator. The logical not operator negates or reverses the truth value of the expression following it i.e., if the expression is True or true value, then not expression False, and vice versa.
Unlike ‘and’ and ‘or’ operators that can return number or a string or a list etc. as result, the ‘not’ operator returns always a Boolean value True or False.

Consider some examples below :

not 5                           results into False because 5 is non-zero (i.e., true value)
not 0                            results into True because 0 is zero (i.e., false value)
not -4                           results into False because -4 is non zero thus true value.
not(5 > 2)                     results into False because the expression 5 > 3 is True.
not (5 > 9)                    results into True because the expression 5 > 9 is False.

Following table summarizes the logical operators

OperationResultNotes
x or yif x is false value, then return y as result, else xIt (or) only evaluates the second argument if the first one is false value
x and yif x is false value, then return x as result, else yIt (and) only evaluates the second argument if the first one is true value
not xif x is false value, then return True as result, else Falsenot has a lower priority than non-Boolean operators.

Chained Comparison Operators

While discussing Logical operators, Python has something interesting to offer. You can chain multiple comparisons which are like shortened version of larger Boolean expressions. Let us see how. Rather than writing 1<2 and 2<3, you even write 1<2<3, which is chained version of earlier Boolean expression.


The above statement will check if 1 was less than 2 and if 2 was less than 3.Let’s look at few examples will check if 1 was less than 2 and if 2 was less than 3.
Let’s look at a few examples of using chains :

>>> 1<2<3                  
True 

is equivalent to

>>> 1<2 and 2<3
True 

As per the property of and, the expression 1<3 will be first evaluated and if only it is True, then only the next chained expression 2<3 will be evaluated.
Similarly consider some more examples :

>>> 11<13 and 13>12
True

The above expression check if 13 is larger than both the other number ; it is the shortened version of 11<13 and 13>12

Python Logical Operators video

Python Logical Operators video Credit : Codeitup

Read More :

Python Loops – For, While, Nested Loops With Examples

Python Logical Operators with Examples

Frequently Asked Questions

What are logical operators in Python?

Python provides three logical operators to combine existing expressions. These are or, and, and not.

What are the 3 python logical operators?

There are three logical operators that are used to compare values. They evaluate expressions down to Boolean values, returning either True or False . These operators are and , or , and not and are defined in the table below.

Is == a logical operator?

Comparison operators — operators that compare values and return true or false . The operators include: > , < , >= , <= , === , and !== … Logical operators — operators that combine multiple boolean expressions or values and provide a single boolean output. The operators include: && , || , and ! .

What is logical operator example?

For example, a && b returns true when both a and b are true (i.e. non-zero). Logical OR operator: The ‘||’ operator returns true even if one (or both) of the conditions under consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b or both are true (i.e. non-zero).

Is not a logical operator?

The NOT logical operator reverses the true/false outcome of the expression that immediately follows. The NOT operator affects only the expression that immediately follows, unless a more complex logical expression is enclosed in parentheses. You can substitute ~ or ¬ for NOT as a logical operator.
python logical operator