MENU
Question -

Write a program in Python to explain logical operators.



Answer -

Following example to understand all the logical operators available in Python programming language:

#!/usr/b in/python

a = 10
b = 20
c = 0
if (a and b):
print тАЬLine 1 тАУ a and b are trueтАЭ
else:
print тАЬLine 1 тАУ Either a is not true or b is not trueтАЭ

if (a or b):
print тАЬLine 2 тАУ Either a is true or b is true or both are trueтАЭ
else:
print тАЬLine 2 тАУ Neither a is true nor b is trueтАЭ

a = 0
if ( a and b ):
print тАЬLine 3 тАУ a and b are trueтАЭ
else:
print тАЬLine 3 тАУ Either a is not true or b is not trueтАЭ

if ( a or b ):
print тАЬLine 4 тАУ Either a is true or b
is true or both are trueтАЭ
else:
print тАЬLine 4 тАУ Neither a is true nor b is trueтАЭ
if not( a and b) :
print тАЬLine 5 тАУ a and b are trueтАЭ else:
print тАЬLine 5 тАУ Either a is not true or b is not trueтАЭ

When you execute the above program it produces following result:

Line 1 тАУ a and b are true
Line 2 тАУ Either a is true or b is true or both are true
Line 3 тАУ Either a is not true or b is not true
Line 4 тАУ Either a is true or b is true or both are true
Line 5 тАУ a and b are true

Comment(S)

Show all Coment

Leave a Comment

Free - Previous Years Question Papers
Any questions? Ask us!
×