Q. Write a program that inputs a tuple and creates a new tuple that contains every third element of the original tuple, starting at index 0. Don't use slice notation. Write code using a for loop.


Answer =


tup = eval(input("Enter Tuple = "))
newtup = ()
for i in range (0, len(tup), 3) :
    newtup += (tup[i],)
print ("New Tuple :-",newtup)

Output :-

Enter Tuple = (1,2,3,4,5,6,7,8,9,10)
New Tuple :- (1, 4, 7, 10)

>>> 

Enter Tuple = (2,4,6,8,10,12,14,16)
New Tuple :- (2, 8, 14)

>>>

4 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. This code will only give the index value..change line4 to newtup+=(tup[i],)

    ReplyDelete
  2. it this code corrected?

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post