Logical Operators in Python

Logical operators are used to combine statements applying boolean algebra as shown in this article Booleans in Python.

They are:

  • and: True only when both statements are true
  • or: False only when both x and y are false
  • not: The not operator simply inverts the input, True becomes False and vice versa.

Let’s see a program that shows how each of them is used.

x = 5
y = 2

print(x == 5 and y > 3)

print(x == 5 or y > 3)

print(not (x == 5))

The output is:

False

True

False