Comparison Operators
We'll cover the following...
Comparison operators
Name | Symbol | Syntax | Explanation |
Equal | == |
| Checks if |
Not equal | != |
| Checks if |
Greater than | > |
| Checks if |
Greater than or equal to | >= |
| Checks if |
Less than | < |
| Checks if |
Less than or equal to | <= |
| Checks if |
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 = 6num_two = 3print(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 = 4num_two = 2print(num_one == num_two)print(num_one != num_two)num_one = 4num_two = 4print(num_one != num_two)num_one = 4num_two = 6print(num_one > num_two)print(num_one < num_two)print(num_one >= num_two)print(num_one <= num_two)