Logical operators

Logical operators  are used to combine  two or more conditions. The andornot   are logical operators. Here is the list of logical operator

Operator

Meaning

Example

and

It returns True if both the expressions or operands are true

T and  T

or

It returns True if either  the expression or operand is true

 T or F

not

It  returns True if  an expression or operand is false, it returns False if an expression is true (complements the operand)

not  T

 

Example 1: Logical Operators in Python

a = True

b = False

print('a and b is',a and b)

# Output: a == b is False

print('a or b is',a or b)

# Output: a or b is True

​​​​​​​print('not a is',not a)

# Output: not a is False

Output

 

a and b is False

a or b is True

not a is False

Example 2: Logical Operators in Python

x = 1

y = 2

print(' x > y  and  x == y is', (x > y )and (x == y))

# Output: x > y and x == y is True

print('not x is',not x)

# Output: not x is False

Output

x > y and x == y is False

not x is False

Note: A Non Zero value is evaluated as True, hence not x (1) is True