Python Relational Operators | Explained

In Python, the relational operator compares two values and returns the Boolean, i.e., “True” or “False”. Relational Operators or “Comparison Operators” can compare the value on both sides. These operators help make decisions at the coding level. The “Relational Operators” are further divided into several categories, i.e., greater than, less than, not equal to, equal to, and so on.

Considering their importance, this post provides a detailed overview of “Relational Operators” in Python.

For a better understanding, each relational operator’s category is explained explicitly in the following manner:

So let’s get started!

Using “Greater Than” or “Less Than” Operators

How to install Kali Linux in Window...

“Greater Than” operator returns the output value “True” when the first operand value is greater than the other. While the “Less Than” operator returns the output value “True” when the first value is smaller than the other. Let’s understand this via the following code:

Code

value_1 = 40  
value_2 = 20  
 
value_3 = value_1 + value_2  
print ("sum of value 3 is equal to: ", value_3)  
  
print ('Value 1 is Greater: ',value_1 > value_2,
       '\nValue 1 is Smaller: ',value_1 < value_2)  
print ('Value 1 is Greater: ',value_3 < value_1,
       '\nValue 3 is Greater: ',value_3 > value_2)

In the above code:

  • Two integer variables named “value_1” and “value_2” are initialized in the program.
  • Addition of both variable values stored in a new variable named “value_3”.
  • Print statement prints the sum of two variables by taking “value_3” as an input.
  • The relational operator “<” and“>” is used to print Boolean values “True” and “False” by checking the variable’s value.

Output

The above output shows which value is “Greater Than” and “Lesser Than” by using Boolean Values “True” and “False”.

Using the “Not Equal To” Operator

The “Not Equal To” operators return the value “True” when the first number is not the same as the second number. Let’s see an example of code given below to understand this relational operator:

Code

value_1 = 20  
value_2 = 10  
value_3 = 20  
# Using Not Equal to operators   
print('Value 1 is Not Equal To Value 2 : ',value_1 != value_3)  
print('Value 3 is Not Equal To Value 2 : ',value_3!=value_2)  
print('Value 1 is Equal To Value 3 : ',value_1==value_3)

In the above code:

  • Three variables named “value_1”, “value_2” and “value_3” are initialized in the program.
  • The relational operator “!=” Not Equal To is used to print Boolean values “True” and “False” by checking whether the variable value is Equal or Not.

Output

In the above output, when the value is “Not Equal”, then the relational operator “!=” prints “True” otherwise “False”.

Using “Equal To” Operators

Equal To” operators return the value “True” when the first operand is the same as the second operand. Let’s see an example of code given below to understand this relational operator:

Code

value_1 = 30  
value_2 = 30  

value_3 = value_1 + value_2  
value_4 = value_1 - value_2  
print ("sum is equal to: ", value_3)  
print ("Difference is equal to: ", value_4)  

print ('value 3 is equal to value 4: ',value_3 == value_4,
       '\nvalue 1 is equal to value 2: ',value_1 == value_2)

In the above code:

  • Two integer variables named “value_1” and “value_2” are initialized.
  • “Addition” of both variable values stored in a new variable named “value_3”.
  • “Subtraction” of both variables stored in a new variable named “value_4”.
  • Print statement prints the sum of two variables by taking “value_3” as an argument and prints the subtraction of two variables by taking “value_4” as an argument.
  • The relational operator “== Equal To is used to print Boolean values “True” and “False” by checking whether the variable value is Equal or Not.

Output

In the above output, the variable value checked whether the value is “Equal To” by returning “True” and “False”.

Using “Greater Than or Equal To” and “Less Than or Equal To” Operators

The relational operator “<=” returns the value “True” when the first value is “>=” to the second value. Similarly, the “<=” operator returns the value “True” when the first Number is less than or equal to the second Number. Let’s understand it with an example of code given below:

Code

value_1 = 20  
value_2 = 10  
value_3 = 30  
  
value_4 = value_1 + value_2  
print("Sum is Equal to: ", value_4)  
 
# Using <= and >= operators  
print('Value 1 is Greater Than or Equal To value 2: ',value_1 >= value_2)   
print('Value 3 is Greater Than or Equal To value 4: ',value_3 <= value_4)

In the above code:

  • Three integer variables named “value_1”, “value_2” and “value_3” are initialized.
  • Addition of both variable values stored in a new variable named “value_4”.
  • The relational operators “<=”and “>=” are used to print Boolean values “True” and “False” by checking whether the variable value is “Greater Than or Equal To” and “Less Than or Equal To”.

Output

The output shows the Boolean value “True” when the “value_1” is Greater Than or Equal To “value_2” and “False” when the condition is not satisfied.

That’s all from this guide on Python’s relational operators.

Conclusion

In Python, the “Relational Operator” such as “==”, “!=”, “< and >”, and“<=,>=” are used to compare two operand values. These operators are the same in every version of Python and provide a logical data comparison. “Relational Operators” returns the output value in Boolean, which is either “True” or “False”. This guide has provided a detailed demonstration of Python’s relational operators.