Please enable JavaScript to use CodeHS

Chapter 2

Conditionals

2.3 Comparison Operators

Comparison Operators

Comparison operators provide a comparison between two values. They look like and are almost identical to the comparison operators you’ve seen in your math classes: less than <, greater than >, less than or equal to <=, and greater than or equal to >=.

One main difference is that the symbol used to show equality is ==. There is also a symbol for not equal which is !=. Using comparison operators allows programs to make decisions.

height_of_bar = 1.9
yelena_jump = 2.0
if yelena_jump > height_of_bar:
  print("Yelena cleared the bar!")
Python

In the example above, Yelena is competing in the high jump event. Let’s say that you want to know if she successfully cleared the bar. First, you’ll define two variables - one for the height of the bar in meters, and one for how high Yelena jumps in meters.

Then, you can use an if statement together with a Boolean expression and a comparison operator. In this case, the expression evaluates to True, so the program will print that Yelena has cleared the bar!

In this example, the comparison operator used was the greater-than operator. Operators like greater-than can be used to compare two numbers and generate a Boolean value which will be either True or False.

Below is a chart of all of the comparison operators used in Python.

Operator Meaning Example Result
== equal to 6 == 7 False
!= not equal to 6 != 7 True
> greater than 6 > 7 False
< less than 6 < 7 True
>= greater than or equal to 6 >= 7 False
<= less than or equal to 6 <= 7 True

Pay close attention to the equal-to operator. Notice that this uses two equals signs right next to each other. A single equals sign cannot be used here since that is already reserved to be used in an assignment statement.

A single equals sign is for creating or changing a variable, whereas double equals is used for checking if two things are equal to each other.

If/Elif/Else Statements

Additional conditions can be included within an if/else statement. This is where an if/elif/else statement comes into play.

x = 10
if x < 20:
  print("less than 20")
elif x == 20:
  print("equal to 20")
else:
  print("greater than 20")
Python

In the example above, there are three possible pieces of code that the Python interpreter might execute. Each of these pieces of code is called a branch. Only one of them will ultimately run. The second one, under the keyword elif, will only execute if the first condition is False and the elif condition is True. The last one, under the word else, will only execute if the first condition is False and the second condition is also False.

Operators and Strings

Operators work differently when used with string values. Strings must be exactly the same to be considered equal, including capitalization and spacing. Considering the greater-than and less-than operators, a string is greater than another string if it would appear earlier in the dictionary and less than another string if it would appear later in the dictionary.

Here are a few examples of how comparison operators work when used with strings.

Operator Meaning Example Result
== equal to "hello" == "hi" False
!= not equal to "hello" != "hi" True
> later in the dictionary "hello" > "hi" False
< earlier in the dictionary "hello" < "hi" True
>= greater than or equal to "hello" >= "hi" False
<= less than or equal to "hello" <= "hi" True

Age Group

This program determines which age group a certain number falls into.

Note that a particular branch of an if/elif/else statement only gets run if the condition of every if or elif before it has evaluated to False.

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    What will the following program print to the console?

    x = 5
    
    if x > 5:
        print("Greater than")
    elif x <= 5:
        print("Less or equal")
    elif x == 5:
        print("Equal")
    else:
        print("Unknown")
    Plain text

Exercise: Positive, Zero, or Negative?

Write a program that asks the user for a number. Report whether that number is greater than 0, zero, or less than 0.

An example interaction with your program might look like this:

Enter a number: -3
That number is less than zero!
Plain text

… or this:

Enter a number: 5
That number is greater than zero!
Plain text

… or this:

Enter a number: 0
That number is zero!
Plain text