Comparative Operators

A comparative operator will compare two values and return a boolean value to say if the relationship defined by the comparative operator is true or false.

Comparative operators always give a true or false answer. For example, you could say “Is it raining?” and there would only be two answers - Yes or No


Comparing Literal Values

The comparative operators can be used to compare literal values.

In the examples below we are checking to see if the two values are the same.

10 == 17

10 is not equal to 17 so the return value is false.


89 == 89

89 is equal to 89 so the return value is true.

Test the code by typing in and running program below:

Comparing Values Stored in Variables

The comparative operators can also be used to compare values stored in variables.

In the examples below we will store some numbers in two variables and then compare the output.

num1 = 10

num2 = 17

print(num1 < num2)

The output will be true, because 10 is less than 17.

num1 = 10

num2 = 17

print(num1 > num2)

The output will be false, because 10 is not greater than 17.


Copy the program to see if it gives the correct answers...

Boolean values can be stored in a variable so it’s perfectly possible to assign the result of a comparative operation to a variable and then use that boolean value later.

If we adapt the program above to do so, it would look like this:

Watch the video below to see how to use comparative operators

pythonComparisons.mp4

Key Words

Comparative Operator

A symbol that is used to ask the computer to compare two values. It will return a boolean value stating if the expression is true or false.

Boolean Value

A logical value that is either true or false

Literal Value

An actual number that is used as given (i.e. not a variable)