Advertisement
<boolean-expression> and <boolean-expression>
# 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")
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
# 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()
first logical expression
second logical expression
Let's play a game!
Advertisement
<boolean-expression> or <boolean-expression>
# 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");
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
# 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()
first logical expression
Let's play a game!
second logical expression
not <boolean-expression>
# 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")
!(10>15), is true
!(a<10) is true
not 0 is true
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement