Header Ads Widget

Responsive Advertisement

write a program to input any two tuples and interchange the tuple variable

write a program to input any two tuples and interchange the tuple variable


In this article you will learn python program to input two tuples and interchange the tuple variable,
First, we ask user to input the length of first tuple and then using for loop we get the tuple variables. once we get the first tuple next, we ask user to input the length of second tuple and then using for loop we get the second tuple variables


After getting both the tuples we swap the two tuples to interchange their values. once swapping is  done we print both the tuples and you can see that tuples values are interchanged.


Below is the Python program to input any two tuples and interchange the tuple variable


firsttuplelen = int(input("Enter the First Tuple Length : "))
print("Enter the first tuple variables :")
firsttuple = ()
for x in range(firsttuplelen):
temp = input()
firsttuple = firsttuple + (temp, )

secondtuplelen = int(input("Enter the Second Tuple Length : "))
print("Enter the second tuple variables :")
secondtuple = ()
for x in range(secondtuplelen):
temp = input()
secondtuple = secondtuple + (temp, )

print("First Tuple is : ", firsttuple)
print("Second Tuple is : ", secondtuple)

# Interchange the Tuple Variables
firsttuple, secondtuple = secondtuple, firsttuple

print("First Tuple is : ", firsttuple)
print("Second Tuple is : ", secondtuple)



Below is the output

> python tuple.py
 
Enter the First Tuple Length : 3
Enter the first tuple variables :
apple
banana
watermelon
Enter the Second Tuple Length : 3
Enter the second tuple variables :
2
4
2
First Tuple is :  ('apple', 'banana', 'watermelon')
Second Tuple is :  ('2', '4', '2')
First Tuple is :  ('2', '4', '2')
Second Tuple is :  ('apple', 'banana', 'watermelon')


You can notice that in the output, tuple can also contain multi data type that is int, string are any other data type Python supports.

This is simple program to input two tuples and read them from the user using input function call and then interchange their values and print to the user.

 

 

Post a Comment

0 Comments