How do you compare 2 numbers in python?

PythonProgramming




How do you compare 2 numbers in python?

Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

Mohammad Nauman

More Detail

How do you compare 2 numbers in python?

Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

MANAS DASGUPTA

More Detail

How do you compare 2 numbers in python?

Practical Data Science using Python

22 Lectures 6 hours

MANAS DASGUPTA

More Detail

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

How do you compare 2 numbers in python?

Samual Sam

Updated on 05-Mar-2020 10:48:16

  • Related Questions & Answers
  • Compare Version Numbers in Python
  • How to compare two numbers in JavaScript?
  • How to compare files in Python
  • How to compare two strings which are numbers in MySQL?
  • How to compare date strings in Python?
  • How to compare two lists in Python?
  • How to compare string and number in Python?
  • How to compare calendar.timegm() vs. time.mktime() in Python?
  • Compare tuples in Python
  • How to compare regular expressions in Perl and Python?
  • How to compare two strings using regex in Python?
  • How to compare Python string formatting: % with .format?
  • How to compare Python DateTime with Javascript DateTime?
  • PHP – How to compare two arbitrary precision numbers using bccomp() function?
  • How do we compare Python Dates?

Previous Page Print Page Next Page  

Advertisements


These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators.

Assume variable a holds 10 and variable b holds 20, then −

OperatorDescriptionExample
== If the values of two operands are equal, then the condition becomes true. (a == b) is not true.
!= If values of two operands are not equal, then condition becomes true. (a != b) is true.
<> If values of two operands are not equal, then condition becomes true. (a <> b) is true. This is similar to != operator.
> If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is not true.
< If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is true.
>= If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is not true.
<= If the value of left operand is less than or equal to the value of right operand, then condition becomes true. (a <= b) is true.

Example

Assume variable a holds 10 and variable b holds 20, then −

#!/usr/bin/python

a = 21
b = 10
c = 0

if ( a == b ):
   print "Line 1 - a is equal to b"
else:
   print "Line 1 - a is not equal to b"

if ( a != b ):
   print "Line 2 - a is not equal to b"
else:
   print "Line 2 - a is equal to b"

if ( a <> b ):
   print "Line 3 - a is not equal to b"
else:
   print "Line 3 - a is equal to b"

if ( a < b ):
   print "Line 4 - a is less than b" 
else:
   print "Line 4 - a is not less than b"

if ( a > b ):
   print "Line 5 - a is greater than b"
else:
   print "Line 5 - a is not greater than b"

a = 5;
b = 20;
if ( a <= b ):
   print "Line 6 - a is either less than or equal to  b"
else:
   print "Line 6 - a is neither less than nor equal to  b"

if ( b >= a ):
   print "Line 7 - b is either greater than  or equal to b"
else:
   print "Line 7 - b is neither greater than  nor equal to b"

When you execute the above program it produces the following result −

Line 1 - a is not equal to b
Line 2 - a is not equal to b
Line 3 - a is not equal to b
Line 4 - a is not less than b
Line 5 - a is greater than b
Line 6 - a is either less than or equal to b
Line 7 - b is either greater than or equal to b

python_basic_operators.htm

How do you compare two numbers in Python?

Both “is” and “==” are used for object comparison in Python. The operator “==” compares values of two objects, while “is” checks if two objects are same (In other words two references to same object).

How do you compare the value of two numbers?

To compare two numbers, follow these steps:.
Write the numbers in a place-value chart..
Compare the digits starting with the greatest place value..
If the digits are the same, compare the digits in the next place value to the right. Keep comparing digits with the same place value until you find digits that are different..

What operator do you use to compare two values in Python?

Summary. A comparison operator compares two values and returns a boolean value, either True or False . Python has six comparison operators: less than ( < ), less than or equal to ( <= ), greater than ( > ), greater than or equal to ( >= ), equal to ( == ), and not equal to ( != ).

How do you compare two set values in Python?

The set() function and == operator.
list1 = [11, 12, 13, 14, 15].
list2 = [12, 13, 11, 15, 14].
a = set(list1).
b = set(list2).
if a == b:.
print("The list1 and list2 are equal").
print("The list1 and list2 are not equal").