Programming Examples
Python program to interchange the values of two tuple
Write a program to input any two tuples and interchange the tuple values.
Solution
t1=tuple()
n=int(input("Total number of values in first tuple"))
for i in range(n):
a=input("enter elements")
t1=t1+(a,)
t2=tuple()
m=int(input("Total number of values in second tuple"))
for i in range(m):
a=input("enter elements")
t2=t2+(a,)
print ("Before SWAPPING")
print ("First Tuple",t1)
print ("Second Tuple",t2)
t1,t2=t2,t1
print ("AFTER SWAPPING")
print ("First Tuple",t1)
print ("Second Tuple",t2)
Output/ Explanation:
Total number of values in first tuple3
enter elements10
enter elements20
enter elements30
Total number of values in second tuple4
enter elements100
enter elements200
enter elements300
enter elements400
Before SWAPPING
First Tuple ('10', '20', '30')
Second Tuple ('100', '200', '300', '400')
AFTER SWAPPING
First Tuple ('100', '200', '300', '400')
Second Tuple ('10', '20', '30')
Write a program to input any two tuples and interchange the tuple values.
Solution
t1=eval(input("Enter Tuple 1"))
t2=eval(input("Enter Tuple 2"))
print ("Before SWAPPING")
print ("First Tuple",t1)
print ("Second Tuple",t2)
t1,t2=t2,t1
print ("AFTER SWAPPING")
print ("First Tuple",t1)
print ("Second Tuple",t2)
Output/ Explanation:
Enter Tuple 1(1,2,3)
Enter Tuple 2(5,6,7,8)
Before SWAPPING
First Tuple (1, 2, 3)
Second Tuple (5, 6, 7, 8)
AFTER SWAPPING
First Tuple (5, 6, 7, 8)
Second Tuple (1, 2, 3)