< Prev
Next >

# Python - Logical Operators

Python language gives us three logical operators -
• and
• or
• not

## and operator

This operator is called the and operator because its functioning corresponds to the AND gate. Let's see the syntax of the declaration of an and logical operator.

``<boolean-expression> and <boolean-expression>``

Rules of the and operator :

• If the result of both boolean expression around the and operator is true, only then the and operator results in a true boolean value.
• If either of the results of both boolean expressions is false, the and operator results in a false boolean value.
• There should be a space between the and operator and a boolean expression.

## and Operator Example

``````# Python and logical operator example

a=10	#variable1
b=20	#variable2
d=2.5 	#variable3

str1='Hi'
str2='Hey'

if a<15 and a>10 :			#a is less than 15 and a is greater than 10
print("a<15 and a>10, is true")

if b<25 and b>19 :			#b is less than 25 and a is greater than 19
print("b<25 and b>19, is true")

if str1=='Hi' and str2=='Hello' : 	#str1 has "Hi" and str2 has 'Hello'
print("str1=='Hi' and str1=='Hello', is true")

if d>2 and str2=='Hey' :		#d is greater than 2 and value in str1 is 'Hey'
print("d>2 and str2=='Hey', is true")

if a<=10 and d<=2.5 :			#a is less than or equal to 10 and d is less than or equal to 2.5
print("a<=10 and d<=2.5, is true")

if a<10 and str1=='Hi' :		#a is less than 10 and str1 has 'Hi'
print("a<=10 and str1=='Hi', is true")
``````

## Output is

``````b<25 and b>19, is true
d>2 and str2=='Hey', is true
a<=10 and d<=2.5, is true
a<=10 and str1=='Hi', is true``````

## The and operator also supports short-ciruiting

The and operator first evaluates the boolean expression on its left and then on its right. Both boolean expressions should return true for the and operator to return true. Hence, if the result of the left boolean expression of the and is false then the operator doesn't waste time in evaluating the boolean expression on its right, making it a short-circuit logical operator.

Example depicting short circuit capability of the and operator :
``````# Defining a function named play which returns a 1(boolean true) upon exit
def play():
print("Let's play a game!")
return 1

print("first logical expression")
0 and play()

print("second logical expression")
1 or play()
``````

## Output

``````first logical expression
second logical expression
Let's play a game!``````

## Program Analysis

• In the first logical expression, the first boolean expression is 0(false), hence the second expression i.e. play() function is not checked for its boolean value, therefore the play() function is not called.
• In the second logical expression, the first boolean expression is 1(true), hence the second expression i.e. play() function is checked for its boolean value, therefore the play() function is called.

## or operator

This operator is also known as the or operator because its functioning corresponds to the OR gate. Let's see the syntax of the declaration of an or logical operator.

``<boolean-expression> or <boolean-expression>``

Rules of or operator

• If either of the results of the boolean expression is true, the or operator will still result in a true boolean value.
• If the result of both boolean expressions is true, the or operator results in a true boolean value.
• If the result of both boolean expressions is false, only then the or operator results in a false boolean value.
• There should be a space between the or operator and a boolean expression.

## or Operator Example

``````# Python or logical operator example

a=10 			# integer variable1
b=20			# integer variable2
d=2.5			# floating point variable

str1 = 'ab'		#string variable
str2 = 'abc'		#string variable

if a<15 or a>10	:	      # a is less than 15 or a is greater than 10
print("a<15 or a>10, is true");

if str1=='ab' or str2=='ac' : # str1 has 'a' or str2 has 'ac'
print("str1=='ab' or str2=='ac', is true");

if b<25 or b>19 :	      # b is less than 25 or a is greater than 19
print("b<25 or b>19, is true");

if d>2 or str2=='b' :	      # d is greater than 2 or str2 has 'b', is true
print("d>2 or str2==b, is true");

if a<=10 or d<=2.5 :	      # a is less than or equal to 10 or d is less than or equal to 2.5
print("a<=10 or d<=2.5, is true");

if a<10 or str1=='ab' :	      # a is less than 10 or str1 has 'ab'
print("a<10 or str1=='ab', is true");
``````

## Output is

``````a<15 or a>10, is true
str1=='ab' or str2=='ac', is true
b<25 or b>19, is true
d>2 or str2==b, is true
a<=10 or d<=2.5, is true
a<10 or str1=='ab', is true``````

## The or operator also supports short-ciruiting

The or operator first evaluates the boolean expression on its left and then evaluates the one on its right. Either of the boolean expressions may return true for the or operator to return true. Hence, if the result of the left boolean expression of the or operator is true then the or operator doesn't waste time in evaluating the boolean expression on its right, making it a short-circuit logical operator.

Example depicting short circuit capability of an or operator :
``````# Defining a function named play which returns a 1(boolean true) upon exit
def play():
print("Let's play a game!")
return 1

print("first logical expression")
0 or play()

print("second logical expression")
1 or play()
``````

## Output

``````first logical expression
Let's play a game!
second logical expression``````

## Program Analysis

• In the first logical expression, the first boolean expression is 0(false), hence the second expression i.e. play() function is checked for its boolean value, therefore the play() function is called.
• In the second logical expression, the first boolean expression is 1(true), hence the second expression i.e. play() function is not checked for its boolean value, therefore the play() function is not called.

## not operator

The functioning of the not operator corresponds to the NOT gate. It inverts the value of a boolean expression. Let's see the syntax of the declaration of a not logical operator.

``not <boolean-expression>``

Rules of a not operator :

• not operator applied to a boolean expression that resulted in true will invert its boolean value to false.
• not operator applied to a boolean expression that resulted in false will invert its boolean value to true.
• There should be a space between a not operator and a boolean expression.

## not Operator Example

``````# Python not logical operator example

a=10		#integer variable1
b=20 		#integer variable2
d=2.5 		#floating point variable

str1 = 'a' 	#string variable
str2 ='b'  	#string variable

if not 10>15 :
print("not(10>15) is true ")

if not(b<25 or b>19) :		#Invert of (b is less than 25 and a is greater than 19)
print("not(b<25 or b>19), is false")

if not(str1=='a' and str2=='b'): #Inverting the boolean value of (str1=='a' and str2=='b')
print("not(str1=='a' and str2=='b'), is false")

if d>2 and not(str2=='b') :	#Inverting the boolean value of an expression by, not(str2=='b')
print("d>2  and not(str2=='b'), is true")

if not(a<=10 or d>=2.5)	:	#Inverting the boolean value of (a<=10 or d>=2.5)
print("a<=10 or d<=2.5, is true")

if not a<10 :			#Inverting the boolean value of (a<10)
print("not(a<10) is true")

if not 1 :			#Inverting boolean value true(1) to false(0)
print("this won't be printed")

if not 0 :			#Inverting boolean value false(0) to true(1)
print("not 0 is true")
``````

## Output -

``````!(10>15), is true
!(a<10) is true
not 0 is true``````

< Prev
Next >
< Python relational operators
Python conditional operator >   