9) Operators and Booleans Lesson

Python Logical Operators

5 min to complete · By Martin Breuss

Logical operators are also known as conditional operators. They are implemented following Boolean algebra. If you ask Python a question using a logical operator, then Python's answer is a Boolean value.

What are Python Logical Operators

There are only three logical operators, and yet you can build entire computers with this logic.

Operator Meaning Example Result
and True if both operands are True True and True True
or True if either of the operands is True True or False True
not True if the operand is False, False if the operand is True not False True

If you happen to have access to a Minecraft installation, you can use it to play around and learn about Logic circuits that are built based on Boolean algebra. You can even build your own computer inside of a computer world like that!

Playground: Logical Operator Practice

Practice using logic operators with some examples in the code playground:

a = 12

print(a > 10 and a < 20)
print(a < 20 and a < 10)

Truth Table

You can use the following truth table as a reference until each operator makes sense. The first two columns show the initial values of the variables p and q. The other columns display what truth value is the result from applying the logic operators on these values:

p q p and q p or q not p not q
False False False False True True
True False False True False True
False True False True True False
True True True True False False

Keep coming back to this truth table if you are unsure about how to use one of the logical operators.

Playground: Logical Comparison Exercise

  • Think of a logical comparison that uses at least two different logical operators and implement it in code.
# your turn!

Keep in mind that you'll usually want to compare the same data types with each other when using any operators. You can think of it as comparing apples with apples and oranges with oranges. That also means that you might get errors or unexpected results when comparing apples with oranges.

Colorful illustration of a light bulb

Info: Feel free to play around and see what happens when you compare other data types using logical operators. The results may seem confusing! You can set your doubt aside for now and keep moving forward since you'll learn more about this later on. If you want to peek ahead, check out What is truthy and falsy.

By tackling the basic rules of Boolean algebra and its implementation in Python using logical operators, you've nearly made it to the end of Python's commonly used operators. In the next lesson, you'll get to know the final one, called the identity operators.

Summary: Python Logical Operators

  • Logical operators are used to combine boolean values
  • There are a total of three logical operators: and, or, and not
  • Each logical operator returns a bool
  • The truth table details the output for all possible combinations between logical operators and the boolean values