Comparison Operator

Comparison operators are used to compare two values. Tests numerical equalities and inequalities between two operands and returns a Boolean value. It returns either True or False according to the condition.

operatorDescriptionExample
==If the values of two operands are equal, then the condition becomes true.a == b
!=If values of two operands are not equal, then condition becomes true.a != b
>If the value of left operand is greater than the value of right operand, then condition becomes truea > b
<If the value of left operand is less than the value of right operand, then condition becomes truea < b
>=If the value of left operand is greater than or equal to the value of right operand, then condition becomes truea >= b
<=If the value of left operand is less than or equal to the value of right operand, then condition becomes truea <= b

Comparison Operator - Program Link

a=int(input())
b=int(input())
print("a==b?",a==b)
print("a!=b?",a!=b)
print("a<b?",a<b)
print("a>b?",a>b)
print("a<=b?",a<=b)
print("a>=b?",a>=b)

Output

# a=2 b=3
a==b? False
a!=b? True
a<b? True
a>b? False
a<=b? True
a>=b? False

References

  • Allen B. Downey, “Think Python: How to Think Like a Computer Scientist‘‘, 2nd edition, Updated for Python 3, Shroff/O‘Reilly Publishers, 2016 (http://greenteapress.com/wp/thinkpython/)
  • Guido van Rossum and Fred L. Drake Jr, ―An Introduction to Python – Revised and updated for Python 3.2, Network Theory Ltd., 2011.
  • John V Guttag, ―Introduction to Computation and Programming Using Python‘‘, Revised and expanded Edition, MIT Press , 2013
  • Robert Sedgewick, Kevin Wayne, Robert Dondero, ―Introduction to Programming in Python: An Inter-disciplinary Approach, Pearson India Education Services Pvt. Ltd., 2016.
  • Timothy A. Budd, ―Exploring Python‖, Mc-Graw Hill Education (India) Private Ltd.,, 2015. 4. Kenneth A. Lambert, ―Fundamentals of Python: First Programs‖, CENGAGE Learning, 2012.
  • Charles Dierbach, ―Introduction to Computer Science using Python: A Computational Problem-Solving Focus, Wiley India Edition, 2013.
  • Paul Gries, Jennifer Campbell and Jason Montojo, ―Practical Programming: An Introduction to Computer Science using Python 3‖, Second edition, Pragmatic Programmers, LLC, 2013.