8. Logical Operators

Logical operators are used to combine conditional expressions.

  • and: Returns True if both conditions are True.
  • or: Returns True if at least one condition is True.
  • not: Reverses the result (True becomes False, and vice versa).

Example 1:


x = 5
y = 10

print(x > 3 and y < 15)   # True
print(x == 5 or y == 5)   # True
print(not(x > 5))         # True

Example 2:


a = 5
b = 20

print(a > 10 or b > 10)   # True
print(not(a > 10))        # True