Flat_Eric Operating on a laptop

Logical Operators

It's not rocket surgery.

Logical Operators

We have already used some logical operators throughout the previous lessons without formally introducing them. In this part we are going to look at them properly and understand what each one does.

Logical operators are symbols or keywords that Python uses to compare values or combine conditions. The result of any logical operation is always a Boolean, True or False. Here are the operators we will cover:

= vs ==

This is one of the most common sources of confusion for beginners and it is worth taking the time to understand the difference clearly.

A single = is the assignment operator. It is used to store a value in a variable. You are not asking a question, you are giving Python an instruction.

A double == is the equality operator. It is used to compare two values and ask the question "are these the same?" Python will evaluate the expression and return either True or False.

As for ===, this operator exists in languages like JavaScript where it is used for strict equality checking. It does not exist in Python. If you try to use it Python will throw a SyntaxError. In Python == already handles strict comparison, so a third operator is not needed.

Try each of the following in the box below and run it each time to see what Python returns:

  • name = 'Eric' then print(name)
  • print(name == 'Eric')
  • print(name == 'John')
  • print(name === 'Eric') and note the error

Less Than and Greater Than

The < and > operators compare two numerical values. Just like ==, they return either True or False. You can use them directly inside a print() statement and Python will evaluate the expression and print the result.

Try each of the following expressions in the box below and run it each time to see what Python returns:

  • print(3 > 10)
  • print(5 < 9)
  • print(10 > 10)
  • print(10 >= 10)
  • print(7 < 3)

The not Operator

The not operator is the simplest of all the logical operators. It does exactly what it sounds like. It reverses the Boolean value of a condition. If something is True, not makes it False. If something is False, not makes it True.

You have already seen conditions like this:

if is_logged_in:

With not you can flip that condition without changing the variable itself:

if not is_logged_in:

This reads very naturally in plain English. "If the user is not logged in, do something." This makes not a very readable and practical tool for writing conditions that check for the absence of something rather than the presence of it.

Try each of the following in the box below and run it each time to see what Python returns:

  • print(not True)
  • print(not False)
  • print(not 0)
  • print(not 1)
  • print(not '')
  • print(not 'hello')

Time to experiment!

Coding Exercises (VS Code) Instructions:

Exercise 1: Comparing Strings

  • print('a' > 'b')
  • print('a' > 'A')

Exercise 2: Chained Comparisons

  • print(1 < 2 < 3 < 4)
  • print(1 < 2 > 3 < 4)
  • print(1 < 2 > 1 < 4)

Exercise 3: <=, >=, !=

  • print(10 >= 10)
  • print(9 <= 10)
  • print(5 != 5)
  • print(5 != 6)

Exercise 4: Combining Operators

Exercise 5: Student Check

Don't Forget to commit and Push!

This course was built by DevSTEM - we turn teaching materials into interactive web courses like this one.

Build your own course