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)
>>>
This code will only give the index value..change line4 to newtup+=(tup[i],)
ReplyDeleteOk, Sorry for inconvenience.
Deleteit this code corrected?
ReplyDeleteYes, You Can run it your IDLE Python.
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )