How to compare numbers in Python?


You can use relational operators in python to compare numbers(both float and int) in python. These operators compare the values on either side of them and decide the relation among them. Assume variable a holds 10 and variable b holds 20, then

Operator
Example
==
(a == b) is not true.
!=
(a != b) is true.
>
(a > b) is not true.
<
(a < b) is true.
>=
(a >= b) is not true.
<=
(a <= b) is true.

Example

You can use this as follows −

a = 10
b = 20
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)

Output

This will give the output −

False
True
False
True
False
True

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 05-Mar-2020

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements