Python Boolean Operators

By Arvind Rai, 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') 
Output
Valid number 
Ex2.
name = "Mahesh"
if name.startswith("S") or name.startswith("M") :
 print ('Valid name')
else :
 print ('Invalid name') 
Output
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') 
Output
Valid number 
Ex2.
name = "Suresh"
if name.startswith("S") and name.endswith("h") :
 print ('Valid name')
else :
 print ('Invalid name') 
Output
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') 
Output
Valid name 
Ex2.
name = "Krishna"
if not name.endswith("a") :
 print ('Valid name')
else :
 print ('Invalid name')
Output
Invalid name 

Priorities of Boolean 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.
1. and has the higher priority than or operator.
>>> True and False or True
True 
is equivalent to
>>> (True and False) or True
True 
Ex.
x= 10
if x > 5 and x < 10 or x%2==0 :
 print ('Valid number')
else :
 print ('Invalid number') 
Output
Valid number 
The above if statement is same as following.
if (x > 5 and x < 10) or x%2==0 : 
2. not has the higher priority than and operator.
>>> not False and True
True 
is equivalent to
>>> (not False) and True
True 
Ex.
name = "Mahesh"
if not name.startswith("S") and name.endswith("h") :
 print ('Valid name')
else :
 print ('Invalid name') 
Output
Valid name 
The above if statement is same as following.
if (not name.startswith("S")) and name.endswith("h") : 
3. The not operator has the lower priority than non-Boolean operators.
>>> not True == True
False 
is equivalent to
>>> not (True == True)
False 
Ex.
name = "Mahesh"
if not name.index("a") == 1 :
 print ('Valid name')
else :
 print ('Invalid name') 
Output
Invalid name 
The above if statement is same as following.
if not (name.index("a") == 1) : 

References

Boolean Operations - and, or, not
Python Getting Started
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us