Compare tuples in Python

PythonServer Side ProgrammingProgramming

When it is required to compare tuples, the '<', '>', and '==' operators can be used.

it returns True or False depending on whether the tuples are equal to each other, less than or greater than one another.

Below is a demonstration of the same −

Example

Live Demo

my_tuple_1 = (87, 90, 31, 85)
my_tuple_2 = (34, 56, 12, 5)

print("The first tuple is :")
print(my_tuple_1)
print("The second tuple is :")
print(my_tuple_2)
print("Comparing the two tuples")
print(my_tuple_1< my_tuple_2)
print(my_tuple_1==my_tuple_2)
print(my_tuple_2 > my_tuple_1)

Output

The first tuple is :
(87, 90, 31, 85)
The second tuple is :
(34, 56, 12, 5)
Comparing the two tuples
False
False
False

Explanation

  • Two tuples are defined, and are displayed on the console.
  • They are compared using the '<', '>', and '==' operator.
  • It is displayed as output on the console.
raja
Published on 11-Mar-2021 09:57:22
Advertisements