Python Boolean Operators
April 02, 2019
Python Boolean operators are or
, and
, not
. The or
and and
are short-circuit operators. The highest priority of Boolean operators is not
then and
and then or
operator. The not
operator has the lower priority than non-Boolean operators.
Boolean operators are evaluated as following.
Operation | Result |
---|---|
x or y | if x is false, then y, else x |
x and y | if x is false, then x, else y |
not x | if x is false, then True, else False |
For the demo we are using Python 3.7.0 in our example. Now find the Python Boolean operators in detail.
1. or
Operator
or
operator works as following.
Operation | Result |
---|---|
True or True | True |
True or False | True |
False or True | True |
False or False | False |
or
operator is a short-circuit operator, so it only evaluates the second argument if the first one is False
.
Now find the examples of
or
operator.Ex1.
a = 40 if a > 100 or a < 50 : print ('Valid number') else : print ('Invalid number')
Valid number
name = "Mahesh" if name.startswith("S") or name.startswith("M") : print ('Valid name') else : print ('Invalid name')
Valid name
2. and
Operator
and
operator works as following.
Operation | Result |
---|---|
True and True | True |
True and False | False |
False and True | False |
False and False | False |
and
operator is a short-circuit operator, so it only evaluates the second argument if the first one is True
.
Now find the examples of
and
operator. Ex1.
a = 30 if a > 10 and a < 50 : print ('Valid number') else : print ('Invalid number')
Valid number
name = "Suresh" if name.startswith("S") and name.endswith("h") : print ('Valid name') else : print ('Invalid name')
Valid name
3. not
Operator
not
operator works as following.
Operation | Result |
---|---|
not True | False |
not False | True |
Now find the examples of
not
operator.Ex1.
name = "Krishna" if not name.startswith("S") : print ('Valid name') else : print ('Invalid name')
Valid name
name = "Krishna" if not name.endswith("a") : print ('Valid name') else : print ('Invalid name')
Invalid name
Priorities of Boolean Operators
The highest priority of Boolean operators isnot
then and
and then or
operator. The not
operator has the lower priority than non-Boolean operators.
1.
and
has the higher priority than or
operator.
>>> True and False or True True
>>> (True and False) or True True
x= 10 if x > 5 and x < 10 or x%2==0 : print ('Valid number') else : print ('Invalid number')
Valid number
if
statement is same as following.
if (x > 5 and x < 10) or x%2==0 :
not
has the higher priority than and
operator.
>>> not False and True True
>>> (not False) and True True
name = "Mahesh" if not name.startswith("S") and name.endswith("h") : print ('Valid name') else : print ('Invalid name')
Valid name
if
statement is same as following.
if (not name.startswith("S")) and name.endswith("h") :
not
operator has the lower priority than non-Boolean operators.
>>> not True == True False
>>> not (True == True) False
name = "Mahesh" if not name.index("a") == 1 : print ('Valid name') else : print ('Invalid name')
Invalid name
if
statement is same as following.
if not (name.index("a") == 1) :
References
Boolean Operations - and, or, notPython Getting Started