How to use Python Comparison Operators | EP-33 Python Comparison Operators | Precedence in Python
Comparison Operators in Python
In Python, comparison operators are used to compare the values of two
operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters (lexicographic order).
Be cautious when comparing floating-point numbers due to potential precision issues.
Equality Operators
The Equal to Operator is also known as the Equality Operator in Python, as it is used to check for equality. It returns True if both the operands are equal i.e. if both the left and the right operands are equal to each other. Otherwise, it returns False.
Syntax: a == b
a=9
b=5
c=9
print(a==b)
print(a==c)
Inequality Operators
The Not Equal To Operator returns True if both the operands are not equal and returns False if both the operands are equal.
Syntax: a != b
a=6
b=2
c=6
print(a!=b)
print(a!=c)
Greater than Sign
The Greater Than Operator returns True if the left operand is greater than the right operand otherwise returns False.
Syntax: a greater than b
a=9
b=5
print(a grater than b)
print(b greater than a)
Less than Sign
The Less Than Operator returns True if the left operand is less than the right operand otherwise it returns False.
Syntax: a less than b
a=10
b=5
print(a less than b) #10 less than 5
print(b less than a)#5 less than 10
Greater than or Equal to Sign
The Greater Than or Equal To Operator returns True if the left operand is greater than or equal to the right operand, else it will return False.
Syntax: x greater than= y
a=9
b=5
c=9
print(a greater than =b,"1") #9 greater than=5
print(a greater than=c,"2")#9 greater than=9
print(b greater than=a,"3")#5greater than=9
Less than or Equal to Sign
The Less Than or Equal To Operator returns True if the left operand is less than or equal to the right operand.
Syntax: x less than= y"""
a=6
b=5
c=6
print(a less than=b)#6 less than=5
print(a less than=c)#6 less than=6
print(b less than=a)#5 less than=6
Chaining Comparision Operators
In Python, we can use the chaining comparison operators to check multiple conditions in a single expression. One simple way of solving multiple conditions is by using Logical Operators. But in the chaining comparison operators method, we can achieve this without any
other operator.
a op1 b op2 c"""
a=5
print(1 less than a less than 10,"1")#1 less than 5 less than 10
print(a greater than 1 greater than 2,"2")#5 greater than 1 greater than 2
#PythonComparisonOperators #PythonForBeginners #PythonProgramming #LearnPython #CodingForBeginners #PythonTutorial #OperatorPrecedence #ComparisonOperators #Programming #Coding #PythonTips #PythonTrick #PythonDevelopment
Connect With Us:
—————————————
➡️ Website: https://www.cybrosys.com/
➡️ Email: info@cybrosys.com
➡️ Twitter: / cybrosys
➡️ LinkedIn: / cybrosys
➡️ Facebook: / cybrosystechnologies
➡️ Instagram: / cybrosystech
➡️ Pinterest: / cybrosys