Write A Python Program To Reverse A Tuple

In this article let's learn a Python program to reverse a given python tuple. Let's take the input value as a tuple containing the integers, ask the user to enter all the tuple items (integers) as input value and accept it and then reverse it.

There are two ways to solve this python program. Let's look into both the solutions.

 

Write A Python Program To Reverse A Tuple

 


 

Write A Python Program To Reverse A Tuple

Let's take an integer variable named as n, it will store the tuple  length,  ask a user to enter the tuple length using input function call and then convert into an integer.

Also take an Empty tuple variable named as my_tuple and initialize it to empty. Using the following iterate  from zero to double length and use the input function call to enter the tuple items,  append the item to the tuple  using + plus  as shown below.

n = int(input("Enter Tuple Length : "))
my_tuple = tuple()
for _ in range(n):
   temp = int(input("Enter Tuple Item : "))
   my_tuple += (temp, )





Use the print function call to print the user entered tuple / given tuple.

print("The Tuple you entered is ...")
print(my_tuple)





Again using the print statement print the reverse of a tuple by using the string slicing method i.e, using [::-1].  This will reverse any iterable object, so tuple also gets reversed, finally print reverse of tuple.

print("Reverse of Tuple is ...")
print(my_tuple[::-1])




Code
============
n = int(input("Enter Tuple Length : "))
my_tuple = tuple()
for _ in range(n):
   temp = int(input("Enter Tuple Item : "))
   my_tuple += (temp, )


print("The Tuple you entered is ...")
print(my_tuple)

print("Reverse of Tuple is ...")
print(my_tuple[::-1])



Output
===========
Enter Tuple Length : 5
Enter Tuple Item : 1
Enter Tuple Item : 2
Enter Tuple Item : 3
Enter Tuple Item : 4
Enter Tuple Item : 5
The Tuple you entered is ...
(1, 2, 3, 4, 5)
Reverse of Tuple is ...
(5, 4, 3, 2, 1)





There is another method to reverse a tuple using the for loop,  as usual take variables to store the tuple length and the people ask the user to enter the tuple items then print the tuple using the print function call.

n = int(input("Enter Tuple Length : "))
my_tuple = tuple()
for _ in range(n):
   temp = int(input("Enter Tuple Item : "))
   my_tuple += (temp, )

print("The Tuple you entered is ...")
print(my_tuple)





Take another empty new_tuple, using the for loop to iterate all the items present in the tuple. While you are iterating all the items keep adding the tuple item to the new_tuple in the front of new_tuple as shown below. Once the for loop complete using the print function print the new_tuple

new_tuple = tuple()
for item in my_tuple:
   new_tuple = (item, ) + new_tuple

print("Reverse of Tuple is ...")
print(new_tuple)





Code
===========
n = int(input("Enter Tuple Length : "))
my_tuple = tuple()
for _ in range(n):
   temp = int(input("Enter Tuple Item : "))
   my_tuple += (temp, )

print("The Tuple you entered is ...")
print(my_tuple)

new_tuple = tuple()
for item in my_tuple:
   new_tuple = (item, ) + new_tuple

print("Reverse of Tuple is ...")
print(new_tuple)




Output
===========
Enter Tuple Length : 7
Enter Tuple Item : 11
Enter Tuple Item : 22
Enter Tuple Item : 33
Enter Tuple Item : 44
Enter Tuple Item : 55
Enter Tuple Item : 66
Enter Tuple Item : 77
The Tuple you entered is ...
(11, 22, 33, 44, 55, 66, 77)
Reverse of Tuple is ...
(77, 66, 55, 44, 33, 22, 11)




Conclusion
==============
Execute the above Python program by providing the integer values as an input to the couple and not down the results.

Comment down below if you have any suggestions regarding the above Python program.

Post a Comment

0 Comments