Relational Operators in Python

Relational operators are used to compare two or more values on both sides and the relational operator always returns a boolean result .

The relational operator fall into three categories in python, which are listed below :-

  1. Comparison Operators
  2. Identity Operators
  3. Membership Operators

Let’s first understand comparison operators and their working in python language, the comparison operators are listed below :-

< Less than
> Greater than
<= Less than equals to
>= Greater than equals to
== Equality opertor
!= Not equals to

 Here, we will discuss each operator with an example.

Equality Operator (==)

The Python Equality operator is used to compare values on both sides that are equal or not. If both sides' values are equal then it will return True and if they aren’t, then it will return False as result.

Let us understand this through a program :-

x = 12
y = 20
z = 12
print(x == y)  # line 1
print(y == z)  # line 2
print(x == z)  # line 3

The result of the first line  and second line will be False as they aren’t equal, but the result of the third line will be True because the value of x is equal to the value of z.

So, the output will be :-

False
False
True

Greater Than (>)

Python Greater than operator is also used to compare values on both sides. It will return True if the left side value is greater than the right side value, else it will return False.

For example :-

x = 10
y = 4
print(x > y)  # line 1
print(y > x)   # line 2

The result of the first line will be True and the second line will be False. Because in the first line x is greater than y.

So, the output will be :-

True
False

Less Than (>)

Python Less than operator is  just opposite to the greater than operator. It means, this operator is also used to compare two values but it will return True if the right side value is greater than the left one, else it will return False.

For example :-

x = 2
y = 3
print(x < y)   # line 1
print(y < x)   # line 2

The result of the first line will be True as the right side value of operator is greater than the left side value and the second line will be False, because left side value is greater.

So, the output will be :-

True
False

Less than Equals to( <=)

Python Less than equals to operator is used to compare values on both sides. This operator will return True if the right side value is greater than the left side or equals to the left side value, else it will return False.

For example :-

x = 10
y = 20
z = 10
print(x <= y)   # line 1
print(y <= x)   # line 2
print(y <= z)   # line 3
print(x <= z)   # line 4

The result of the first , third and fourth line will be True as the first  and third line has greater value on the right side and in the fourth statement, both values are equal.

So, the output will be :-

True
False
True
True

Greater than Equals to (>=)

Python Greater than equals to operator is used to compare values on both sides. This operator will return True if the left side value is greater than the right side or equals to the right side value, else it will return False.

For example :-

x = 10
y = 20
z = 10
print(x >= y)   # line 1
print(y >= x)   # line 2  
print(x >= z)   # line 3

The result of the second and third line will be True as the second line has greater value in the left side and  in the third both values are equal.

So, the output will be :-

False
True
True

Not Equals to (!=)

Python Not equals to operator is also used to compare equality of two values. It will return True if they aren’t equal but if they are, it will return False.

For example :-

x = 10
y = 20
z = 20
print(x != y)   # line 1
print(x ! z)    # line 2

The result of the first line  will be True as they aren’t equal and the second line will be False.

So, the output will be :-

True
False