Comparison operators

Name

Symbol

Syntax

Explanation

Equal

==

var_1 == var_2

Checks if var_1 is equal to var_2

Not equal

!=

var_1 != var_2

Checks if var_1 is not equal to var_2

Greater than

>

var_1 > var_2

Checks if var_1 is greater than var_2

Greater than or equal to

>=

var_1 >= var_2

Checks if var_1 is greater than or equal to var_2

Less than

<

var_1 < var_2

Checks if var_1 is less than var_2

Less than or equal to

<=

var_1 <= var_2

Checks if var_1 is less than or equal to var_2

Comparison operators

Let’s explore the comparison operators using two variables, num_one and num_two. Initially, we set num_one to 6 and num_two to 3. Applying the comparison operators would give the following results:

Equal

Checking if num_one is equal to num_two would result in False.

Expression: num_one == num_two

Not equal

Checking if num_one is not equal to num_two would result in True.

Expression: num_one != num_two

Greater than

Checking if num_one is greater than num_two would result in True.

Expression: num_one > num_two

Less than

Checking if num_one is less than num_two would result in False.

Expression: num_one < num_two

Greater than or equal to

Checking if num_one is greater than or equal to num_two would result in True.

Expression: num_one >= num_two

Less than or equal to

Checking if num_one is less than or equal to num_two would result in False.

Expression: num_one <= num_two

Code

Now we’re ready to put this into Python code, so let’s get started! Click the “Run” button to see your output, and feel free to experiment with the values.

num_one = 6
num_two = 3
print(num_one == num_two)
print(num_one != num_two)
print(num_one > num_two)
print(num_one < num_two)
print(num_one >= num_two)
print(num_one <= num_two)

We can make the values equal before using the not equal operator in order to see how its value changes. This time, let’s also make the second value greater than the first value.

num_one = 4
num_two = 2
print(num_one == num_two)
print(num_one != num_two)
num_one = 4
num_two = 4
print(num_one != num_two)
num_one = 4
num_two = 6
print(num_one > num_two)
print(num_one < num_two)
print(num_one >= num_two)
print(num_one <= num_two)