4️⃣ Logical Operators

Python has 3 logical operators. The logical operators always retuns boolean value (True/False or 1 or 0) as a result.

1) and:

It retuns True if both the values are True. Otherwise, it retuns False.

Below table explains the concept of logical and operater

Inputs

Output

A

B

A and B

False

False

False

True

False

False

False

True

False

True

True

True

💻 Example:

1
2
print(True and True)
print(1 and 1)

✅ Output:

True
1

💻 Example 1:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Python program to demonstrate logical 'and' operator

a = 30
b = 70
c = -90

if a > 0 and b > 0:
    print("The numbers are greater than 0")

if a > 0 and b > 0 and c > 0:
    print("The numbers are greater than 0")
else:
    print("Atleast one number is not greater than 0")

✅ Output:

The numbers are greater than 0
Atleast one number is not greater than 0

💻 Example 2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Python program to demonstrate logical 'and' operator

a = 20
b = 30
c = 0

if a and b and c:
    print("All the numbers have boolean value as True")
else:
    print("Atleast one number has boolean value as False")

✅ Output:

Atleast one number has boolean value as False

2) or:

It retuns True if any of the value is True and retuns False if both values are False.

Below table explains the concept of logical or operater

Inputs

Output

A

B

A or B

False

False

False

True

False

True

False

True

True

True

True

True

💻 Example:

1
2
3
4
5
6
7
8
print(True or True)
print(True or False)
print(False or True)
print(False or False)
print(1 or 1)
print(1 or 0)
print(0 or 1)
print(0 or 0)

✅ Output:

True
True
True
False
1
1
1
0

💻 Example 1:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Python program to demonstrate logical 'or' operator

a = 10
b = -10
c = 0

if a > 0 or b > 0:
    print("Either of the number is greater than 0")
else:
    print("No number is greater than 0")

if b > 0 or c > 0:
    print("Either of the number is greater than 0")
else:
    print("No number is greater than 0")

✅ Output:

Either of the number is greater than 0
No number is greater than 0

💻 Example 2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Python program to demonstrate logical 'or' operator

a = 10
b = 12
c = 0

if a or b or c:
    print("Atleast one number has boolean value as True")
else:
    print("All the numbers have boolean value as False")

✅ Output:

Atleast one number has boolean value as True

3) not:

It reverses the result. Returns True if input is False and vice versa.

Below table explains the concept of logical not operater

Input

Output

A

not A

False

True

True

False

💻 Example:

1
2
3
4
print(not False)
print(not True)
print(not 1)
print(not 0)

✅ Output:

True
False
1

💻 Example:

1
2
3
4
5
6
7
8
# Python program to demonstrate logical 'not' operator

x = 10

if not x > 10:
    print("True")
else:
    print("False")

✅ Output:

True

Follow below video to clear the concepts practically. Comparison and logical operators are explained in detail.


Be prepared with below questions prior to your interview !

Frequently asked Interview Questions
  1. What are logical operators in Python?

  2. What is the purpose of the ‘not’ operator in Python?

  3. How can logical operators be used in conditional statements?