Python Relational Operators
Navigating Relationships: Relational Operators in Python
Relational operators in Python play a pivotal role in evaluating and expressing relationships between values. They allow you to compare variables, check conditions, and make decisions based on the outcomes of these comparisons. In this comprehensive guide, weβll explore the realm of relational operators, their syntax, and their applications in Python programming.
The following table lists the relational operators in Python:
| Operator | Description | Example |
|---|---|---|
==== | If the values of two operands are equal, then the condition becomes true | x == yx == y |
!=!= | If values of two operands are not equal, then the condition becomes true | x != yx != y |
>> | If the value of the left operand is greater than the value of the right operand, then the condition becomes true | x > yx > y |
<< | If the value of the left operand is less than the value of the right operand, then the condition becomes true | x < yx < y |
>=>= | If the value of the left operand is greater than or equal to the value of the right operand, then the condition becomes true | x >= yx >= y |
<=<= | If the value of the left operand is less than or equal to the value of the right operand, then the condition becomes true | x <= yx <= y |
Equality
==== (Equality) Operator
The equality operator (====) compares the values of two operands. If the values of both operands are equal, then the condition becomes TrueTrue. Otherwise, the condition becomes FalseFalse. The following example demonstrates how to use the equality operator in Python:
# Equality operator
x = 10
y = 5
z = x == y
t = x == 10
print(z)
print(t)# Equality operator
x = 10
y = 5
z = x == y
t = x == 10
print(z)
print(t)Output:
C:\Users\Your Name> python operators.py
False
TrueC:\Users\Your Name> python operators.py
False
TrueIn the above example, we have used the equality operator to compare the values of two operands xx and yy. Since the value of xx is not equal to the value of yy, the condition becomes FalseFalse. The result of the equality operator is then assigned to the variable zz. The value of zz is then printed to the console.
Inequality
!=!= (Inequality) Operator
The inequality operator (!=!=) compares the values of two operands. If the values of both operands are not equal, then the condition becomes TrueTrue. Otherwise, the condition becomes FalseFalse. The following example demonstrates how to use the inequality operator in Python:
# Inequality operator
x = 10
y = 5
z = x != y
t = x != 10
print(z)
print(t)# Inequality operator
x = 10
y = 5
z = x != y
t = x != 10
print(z)
print(t)Output:
C:\Users\Your Name> python operators.py
True
FalseC:\Users\Your Name> python operators.py
True
FalseIn the above example, we have used the inequality operator to compare the values of two operands xx and yy. Since the value of xx is not equal to the value of yy, the condition becomes TrueTrue. The result of the inequality operator is then assigned to the variable zz. The value of zz is then printed to the console.
Greater Than
>> (Greater Than) Operator
The greater than operator (>>) compares the values of two operands. If the value of the left operand is greater than the value of the right operand, then the condition becomes TrueTrue. Otherwise, the condition becomes FalseFalse. The following example demonstrates how to use the greater than operator in Python:
# Greater than operator
x = 10
y = 5
z = x > y
t = x > 10
print(z)
print(t)# Greater than operator
x = 10
y = 5
z = x > y
t = x > 10
print(z)
print(t)Output:
C:\Users\Your Name> python operators.py
True
FalseC:\Users\Your Name> python operators.py
True
FalseIn the above example, we have used the greater than operator to compare the values of two operands xx and yy. Since the value of xx is greater than the value of yy, the condition becomes TrueTrue. The result of the greater than operator is then assigned to the variable zz. The value of zz is then printed to the console.
Less Than
<< (Less Than) Operator
The less than operator (<<) compares the values of two operands. If the value of the left operand is less than the value of the right operand, then the condition becomes TrueTrue. Otherwise, the condition becomes FalseFalse. The following example demonstrates how to use the less than operator in Python:
# Less than operator
x = 10
y = 5
z = x < y
t = x < 10
print(z)
print(t)# Less than operator
x = 10
y = 5
z = x < y
t = x < 10
print(z)
print(t)Output:
C:\Users\Your Name> python operators.py
False
FalseC:\Users\Your Name> python operators.py
False
FalseIn the above example, we have used the less than operator to compare the values of two operands xx and yy. Since the value of xx is not less than the value of yy, the condition becomes FalseFalse. The result of the less than operator is then assigned to the variable zz. The value of zz is then printed to the console.
Greater Than or Equal To
>=>= (Greater Than or Equal To) Operator
The greater than or equal to operator (>=>=) compares the values of two operands. If the value of the left operand is greater than or equal to the value of the right operand, then the condition becomes TrueTrue. Otherwise, the condition becomes FalseFalse. The following example demonstrates how to use the greater than or equal to operator in Python:
# Greater than or equal to operator
x = 10
y = 5
z = x >= y
t = x >= 10
print(z)
print(t)# Greater than or equal to operator
x = 10
y = 5
z = x >= y
t = x >= 10
print(z)
print(t)Output:
C:\Users\Your Name> python operators.py
True
TrueC:\Users\Your Name> python operators.py
True
TrueIn the above example, we have used the greater than or equal to operator to compare the values of two operands xx and yy. Since the value of xx is greater than the value of yy, the condition becomes TrueTrue. The result of the greater than or equal to operator is then assigned to the variable zz. The value of zz is then printed to the console.
Less Than or Equal To
<=<= (Less Than or Equal To) Operator
The less than or equal to operator (<=<=) compares the values of two operands. If the value of the left operand is less than or equal to the value of the right operand, then the condition becomes TrueTrue. Otherwise, the condition becomes FalseFalse. The following example demonstrates how to use the less than or equal to operator in Python:
# Less than or equal to operator
x = 10
y = 5
z = x <= y
t = x <= 10
print(z)
print(t)# Less than or equal to operator
x = 10
y = 5
z = x <= y
t = x <= 10
print(z)
print(t)Output:
C:\Users\Your Name> python operators.py
False
TrueC:\Users\Your Name> python operators.py
False
TrueIn the above example, we have used the less than or equal to operator to compare the values of two operands xx and yy. Since the value of xx is not less than the value of yy, the condition becomes FalseFalse. The result of the less than or equal to operator is then assigned to the variable zz. The value of zz is then printed to the console.
Combining Relational Operators
You can combine relational operators in Python to create complex conditions. For example, you can combine the equality operator (====) and the greater than operator (>>) to create a condition that checks if the value of xx is greater than yy and equal to zz. The following example demonstrates how to combine relational operators in Python:
# Combining relational operators
x = 10
y = 5
z = 10
t = x > y and x == z
print(t)# Combining relational operators
x = 10
y = 5
z = 10
t = x > y and x == z
print(t)Output:
C:\Users\Your Name> python operators.py
TrueC:\Users\Your Name> python operators.py
TrueIn the above example, we have combined the equality operator (====) and the greater than operator (>>) to create a condition that checks if the value of xx is greater than yy and equal to zz. Since the value of xx is greater than yy and equal to zz, the condition becomes TrueTrue. The result of the condition is then assigned to the variable tt. The value of tt is then printed to the console.
Relational Operators with Strings
You can also use relational operators with strings in Python. For example, you can use the equality operator (====) to check if two strings are equal. The following example demonstrates how to use relational operators with strings in Python:
# Relational operators with strings
x = "Hello"
y = "World"
z = x == y
t = x == "Hello"
print(z)
print(t)# Relational operators with strings
x = "Hello"
y = "World"
z = x == y
t = x == "Hello"
print(z)
print(t)Output:
C:\Users\Your Name> python operators.py
False
TrueC:\Users\Your Name> python operators.py
False
TrueIn the above example, we have used the equality operator to check if the value of xx is equal to the value of yy. Since the value of xx is not equal to the value of yy, the condition becomes FalseFalse. The result of the condition is then assigned to the variable zz. The value of zz is then printed to the console.
Conclusion
Relational operators are fundamental tools for expressing conditions and making decisions in Python. Whether youβre comparing numerical values, checking string order, or combining conditions with logical operators, relational operators provide the means to navigate relationships between variables.
As you continue your Python journey, experiment with different relational operators, explore their applications in real-world scenarios, and use them to create dynamic and responsive code. For more insights and practical examples, check out our tutorials on Python Central Hub!
Was this page helpful?
Let us know how we did
