Relational or Comparison Operators in Python
In this lesson, we will learn what is the relational or comparison operator and how it works in Python programming with some examples.
What is Relational Operators
Relational operators in Python, also known as comparison operators, are used to compare the values of two operands. For example, they check if one operand is equal to the other, greater than, or less than the other, and so on.
There are 6 types of relational operators in Python and they are:
- == Equality
- > Greater than
- < Less than
- >= Greater than or equal to
- <= Less than or equal to
- != Not equal to
Now let's see the examples of all the relational operators one by one for more understanding.
Equality ==
The equality operator == is used to check if the values of two operands are the same. The result of the == operator is True if the values are equal; otherwise, the result is False if they are not equal.
x=5
y=5
z=6
a = (x == y) # result of a is True because x=5 is equal to y=5 so relation is true
b = (x == z) # result of b is False because x=5 is not equal to z=6 so relation is false
In the example above, the result of a is True because the value of x and y is the same so, the relation is correct. On the other hand, the result of b is False because the value of x and z is not the same so the relationship is wrong.
Greater than >
The greater than > relational operator is used to check if the value of the left operand is greater than the value of the right operand. The result of the > operator is True if the left operand is greater; otherwise, the result is False if it is not.
x=8
y=5
z=10
a = (x > y) # result of a is True because x=8 is greater than y=5 so relation is true
b = (x > z) # result of b is False because x=8 is not greater than z=10 so relation is false
In the example above, the result of a is True because the value of x is greater than the value of y so, the relation is correct. On the other hand, the result of b is False because the value of x is not greater than the value of z so the relationship is wrong.
Less than <
The less than < relational operator is used to check if the value of the left operand is less than the value of the right operand. The result of the < operator is True if the left operand is less; otherwise, the result is False if it is not.
x=5
y=8
z=3
a = (x < y) # result of a is True because x=5 is less than y=8 so relation is true
b = (x < z) # result of b is False because x=5 is not less than z=3 so relation is false
In the example above, the result of a is True because the value of x is less than the value of y so, the relation is correct. On the other hand, the result of b is False because the value of x is not less than the value of z so the relationship is wrong.
Greater than or equal to >=
The greater than or equal to >= relational operator is used to check if the value of the left operand is greater than or equal to the value of the right operand. The result of the >= operator is True if the condition is met; otherwise, the result is False if it is not.
w=5
x=5
y=3
z=10
a = (w >= x) # result of a is True because w=5 is equal to x=5 so relation is true
b = (x >= y) # result of b is True because x=5 is greater than y=3 so relation is true
c = (y >= z) # result of c is False because y=5 is neither greater than z=10 nor equal to z=10 so relation is false
In the example above, the result of a is True because the value of w is not greater but equal to the value of x so, the relation is correct. Next, the result of b is True because the value of x is greater than the value of y so, the relationship is correct. On the other hand, the result of c is False because the value of y is neither greater nor equal to the value of z so the relationship is wrong.
Less than or equal to <=
The less than or equal to <= relational operator is used to check if the value of the left operand is less than or equal to the value of the right operand. The result of the <= operator is True if the condition is met; otherwise, the result is False if it is not.
w=5
x=5
y=7
z=3
a = (w <= x) # result of a is True because w=5 is equal to x=5 so relation is true
b = (x <= y) # result of b is True because x=5 is less than y=7 so relation is true
c = (y <= z) # result of c is False because y=7 is neither less than z=3 nor equal to z=3 so relation is false
In the example above, the result of a is True because the value of w is not less but equal to the value of x so, the relation is correct. Next, the result of b is True because the value of x is less than the value of y so, the relationship is correct. On the other hand, the result of c is False because the value of y is neither less nor equal to the value of z so the relationship is wrong.
Not equal to !=
The not equal to != relational operator is used to check if the value of the left operand is not equal to the value of the right operand. The result of the != operator is True if the values are not equal; otherwise, the result will be False if the values are equal.
x=5
y=8
z=5
a = (x != y) # result of a is True because x=5 is not equal to y=8 so relation is true
b = (x != z) # result of b is False because x=5 is equal to y=5 so relation is false
In the example above, the result of a is True because the value of x is not equal to the value of y so, the relation is correct. On the other hand, the result of b is False because the value of x is equal to the value of z so the relationship is wrong.
Test Your Knowledge
Attempt the questions in the exercise to check whether you have understood the lesson clearly.