In this tutorial, you will learn all about Python comparison operators and how to use them with the help of examples.
Before going through this article, please read our Python operators tutorial so that you have knowledge of operators in Python.
Headings of Contents
Python Comparison Operators
Python comparison operators are used to compare two values and return Boolean value: True or False.
There are various type of comparison operators in Python.
- Equal
- Not Equal
- Greater Than
- Less Than
- Greater Than or equal to
- Less than or equal to
Comparison operators in Python
Operator | Name | Example |
---|---|---|
== | Equal to | x == y |
!= | Not equal to | x != y |
=== | Strict equal to | x === y |
!== | Not strict equal to | x !== y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
- The Equal ( == ) operator returns True if the both operands are equal.
Example
value1 = int(input("Please enter the first number:- ") )
value2 = int(input("Please enter the second number:- ") )
if value1 == value2:
print("Both value are equal...")
else:
print("Both values are not equal....")
Output
Please enter the first number:- 12
Please enter the second number:- 12
Both values are equal...
- The Not Equal ( != ) operator returns True if the both operands are not equal.
Example
value1 = int(input("Please enter the first number:- "))
value2 = int(input("Please enter the second number:- "))
if value1 != value2:
print("Both values are not equal...")
Output
Please enter the first number:- 12
Please enter the second number:- 30
Both values are not equal...
- The Greater Than ( > ) returns True if the left operand greater than right operand.
Example
value1 = int(input("Please enter the first number:- "))
value2 = int(input("Please enter the second number:- "))
if value1 > value2:
print(f"{value1} is greater than {value2}.")
Output
Please enter the first number:- 12
Please enter the second number:- 10
12 is greater than 10.
- The Less Than ( < ) operator returns True if the left operand less than right operand.
Example
value1 = int(input("Please enter the first number:- "))
value2 = int(input("Please enter the second number:- "))
if value1 < value2:
print(f"{value1} is less than {value2}")
Output
Please enter the first number:- 12
Please enter the second number:- 15
12 is less than 15
- The Greater Than or equal to ( >= ) returns True if the left operand greater or equal than right operand.
Example
value1 = int(input("Please enter the first number:- ") )
value2 = int(input("Please enter the second number:- ") )
if value1 >= value2:
print("Welcome to Programming Funda")
Output
Please enter the first number:- 12
Please enter the second number:- 12
Welcome to Programming Funda
- The Less Than or equal to ( <= ) operator returns True if the left operand less than or equal to right operand.
Example
value1 = int(input("Please enter the first number:- "))
value2 = int(input("Please enter the second number:- "))
if value1 <= value2:
print("Welcome to Programming Funda")
Output
Please enter the first number:- 12
Please enter the second number:- 20
Welcome to Programming Funda
Here we have mention all the Python comparison operators in single examples.
Example: Comparison operators in Python.
x = int(input("Please enter the first number:- ")
y = int(input("Please enter the second number:- ")
print("x > y:- ", x > y)
print("x < y:- ", x < y)
print("x == y:- ", x == y)
print("x != y:- ", x != y)
print("x >= y:- ", x >= y)
print("x <= y:- ", x <= y)
Summary
In this article, you explored all the Python comparison operators with the help of examples. comparison operators in Python are very useful operators when you work with real-life python projects to compare the value and perform operations.
If you like this article, please share and keep visit for further Python tutorials.
For More Information:- Click Here