Saturday, February 1, 2020

Tuple Manipulation Practice


Tuple Manipulation Practice


Question: Write the output of the given python code :

(a) tup1 = (12, 34.56);
      tup2 = (‘abc’, ‘xyz’);
      #Following action is not valid for tuples
      #tup1 [0] = 100;
      #So let’s create a new tuple as follows
      tup3 = tup1 + tup2;
      print tup3;
     Answer:
     (12,34.56, ’abc’, ‘xyz’)

(b) tuple1, tuple2 = (123, ‘xyz’, ‘zara’, ‘abc’), (456, 700, 200)
      print “min value element : “, min (tuple1);
      print “min value element : “, min (tuple2);
      Answer:
      min value element : Error
      min value element : 200

Question:
What is a tuple ?
Answer:
Tuple is a sequence of immutable Python objects

Question:
Can we remove individual tuple elements ?
Answer:
No, we cannot remove individual tuple elements.

Question:
Which function gives the total length of the tuple.
Answer:
len(tuple)

Question:
Which function returns item from the tuple with max value.
Answer:
max(tuple)

Question: Write a program to input any two tuples and interchange the tupel values.
Answer:
t1 = tuple()
t2 = tuple()
n = int(input("Total number of values n in first tuple"))
for i in range (n):
    a = input("Enter elements")
    t1 = t1 + (a, )
m = int(input("Total number of values m in second tuple"))
for i in range (m):
    a = input("Enter elements")
    t2 = t2 + (a, )

print("First tuple")
print(t1)
print("Second tuple")
print(t2)

t1, t2 = t2, t1
print("After swapping")
print("First tuple")
print(t1)
print("Second tuple")
print(t2)

Question: Write a program to input ‘n’ numbers and separate the tuple in the following manner.
Example
T=(10,20,30,40,50,60)
Tl=(10,30,50)
T2=(20,40,60)
Answer:
t=tuple( )
n=input(” Enter number of values:”)
for i in range(n):
    a=input(“enter elements”)
    t=t+(a,)
t1=t2=tuple( )
for i in t:
    if i%2 == 0:
        t2= t2 + (i,)
    else:
        tl=tl+(i,)
print “First Tuple”
print t1
print “Second Tuple”
print t2



No comments:

Post a Comment