Practice with Relational and Logical Operators

Try these exercises for more practice with relational and logical operators.

2. Comparison Operators

In programming, comparison operators are used to compare values and evaluate down to a single Boolean value of either True or False.

The table below shows Boolean comparison operators.

Operator What it means
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to

To understand how these operators work, let’s assign two integers to two variables in a Python program:

x = 5
y = 8

We know that in this example, since x has the value of 5, it is less than y which has the value of 8.

Using those two variables and their associated values, let’s go through the operators from the table above. In our program, we’ll ask Python to print out whether each comparison operator evaluates to either True or False. To help us and other humans better understand this output, we’ll have Python also print a string to show us what it’s evaluating.

x = 5
y = 8

print("x == y:", x == y)
print("x != y:", x != y)
print("x < y:", x < y)
print("x > y:", x > y)
print("x <= y:", x <= y)
print("x >= y:", x >= y)
Output
x == y: False
x != y: True
x < y: True
x > y: False
x <= y: True
x >= y: False

Following mathematical logic, in each of the expressions above, Python has evaluated:

  • Is 5 (x) equal to 8 (y)? False
  • Is 5 not equal to 8? True
  • Is 5 less than 8? True
  • Is 5 greater than 8? False
  • Is 5 less than or equal to 8? True
  • Is 5 not less than or equal to 8? False


Although we used integers here, we could substitute them with float values.

Strings can also be used with Boolean operators. They are case-sensitive unless you employ an additional string method.

We can look at how strings are compared in practice:

Sammy = "Sammy"
sammy = "sammy"

print("Sammy == sammy: ", Sammy == sammy)
Output
Sammy == sammy:  False

The string "Sammy" above is not equal to the string "sammy", because they are not exactly the same; one starts with an upper-case S and the other with a lower-case s. But, if we add another variable that is assigned the value of "Sammy", then they will evaluate to equal:

Sammy = "Sammy"
sammy = "sammy"
also_Sammy = "Sammy"

print("Sammy == sammy: ", Sammy == sammy)
print("Sammy == also_Sammy", Sammy == also_Sammy)
Output
Sammy == sammy:  False
Sammy == also_Sammy:  True

You can also use the other comparison operators including > and < to compare two strings. Python will compare these strings lexicographically using the ASCII values of the characters.

We can also evaluate Boolean values with comparison operators:

t = True
f = False

print("t != f: ", t != f)
Output
t != f:  True

The above code block evaluated that True is not equal to False.

Note the difference between the two operators = and ==.

x = y   # Sets x equal to y
x == y  # Evaluates whether x is equal to y

The first, = is the assignment operator, which will set one value equal to another. The second, == is a comparison operator which will evaluate whether two values are equal.