Class 11:Python Logical operators

Logical operators

In python we have the following Logical operators :

and,
or,
not

Logical operators in Python

Operator

Meaning

Example

and

True if both the operands are true

x and y

or

True if either of the operands is true

x or y

not

True if operand is false (complements the operand)

not x

“and Logical (Boolean)

“and” will result in True only if both the operands are True. The truth table for and is given below:

Truth table for “and” operator

A

B

A and B

True

True

True

True

False

False

False

True

False

False

False

False

Example:

a=True and True
b=True and False
c=False and True
d=False and False
print(a)
print(b)
print(c)
print(d)

Output:

True
False
False
False

“or” Logical (Boolean)

“or” will result in True if any of the operands is True. The truth table for or is given below:

The truth table for “or” operator

A

B

A or B

True

True

True

True

False

True

False

True

True

False

False

False

Example:

a=True or True
b=True or False
c=False or True
d=False or False
print(a)
print(b)
print(c)
print(d)

Output:

True
True
True
False
>>>

“not” Logical (Boolean)

“not” operator is used to invert the truth value. The truth table for not is given below:

The truth table for “not” Operator

A

not A

True

False

False

True

Example:

a=not True
b=not False
print(a)
print(b)

Output:

False
True
>>>