What are Relational Operators in Python
Relational Operators in Python are used to compare two values using Comparison Operators and return a result in Boolean values, either ‘True’ or ‘False’.
As the name suggests, Relational Operators are used to determine the relationship between two operands.
There are mainly six commonly used Relational Operators in Python. All these six operators allows us to compare values and determine the relationship between them. Here is a list of these six relational operators.
Name | Operator | Example | Description |
---|---|---|---|
Equal to | == | x == y | Returns True if x is equal to y, otherwise False. |
Not Equal to | != | x != y | Returns True if x is not equal to y, otherwise False. |
Greater than | > | x > y | Returns True if x is greater then y, otherwise False. |
Less Than | < | x < y | Returns True if x is less then y, otherwise False. |
Greater than or equal to | >= | x >= y | Returns True if x is greater or equal to y, otherwise False. |
Less than or equal to | <= | x <= y | Returns True if x is less or equal to y, otherwise False. |
These operators are used to compare and control the flow of Python program. We can also use these operators in conditional statements, loops and other parts in our python code, to make decisions based on the relationships between two values.
Examples of Python Relational Operators
Here are some examples of Relational Operators, that will clear your all doubts about this topic.
Using Equal to Operator
x = 5 y = 4 print (x == y) #Outputs: False
In an above code we use the Python Equal to operator and compare the two values, x and y. It outputs ‘False’ because the value of y is not equal to value of z.
Using Less than Operator
x = 9 y = 14 print (x < y) #Outputs: True
In an above code the value of x is less than the value of y, so it outputs True.
Using Greater than or equal to Operator
x = 19 y = 7 print (x >= y) #Outputs: True
In an above code the value of x is greater than the value of y, so the result we get is True. Even the value of x is equal to the value of y still we get an output True, because we have used the Greater or equal to operator.
NOTE:- Relational Operators are used to compare two values at a time. If you want to compare three or more values then use Python Logical Operator.
Share This Post!