Control Flow
Relational Operators: Equals and Not Equals

Now that we understand what boolean expressions are, let’s learn to create them in Python. We can create a boolean expression by using relational operators.

Relational operators compare two items and return either True or False. For this reason, you will sometimes hear them called comparators.

The two boolean operators we’ll cover first are:

  • Equals: ==
  • Not equals: !=

These operators compare two items and return True or False if they are equal or not.

We can create boolean expressions by comparing two values using these operators:

>>> 1 == 1 True >>> 2 != 4 True >>> 3 == 5 False >>> '7' == 7 False

Each of these is an example of a boolean expression. >>> is the prompt when you run Python in your terminal, which you can then use to evaluate simple expressions, such as these.

Why is the last statement false? The '' marks in '7' make it a string, which is different from the integer value 7, so they are not equal. When using relational operators it is important to always be mindful of type.



Determine if the following boolean expressions are True or False. Input your answer as True or False in the appropriate variable to the right.

Statement one:

(5 * 2) - 1 == 8 + 1

Statement two:
13 - 6 != (3 * 2) + 1

Statement three:
3 * (2 - 1) == 4 - 1
Folder Icon

Sign up to start coding

Already have an account?