Python | Loop through tuple
for loop
using for loop you can print the tuple items one by one.
Example
tuple = ('mango' ,' banana', 'apple', 'mango' , 'graps' , 'apple')
for x in tuple:
print(x)
Output
mango
banana
apple
mango
graps
apple
for loop with index number
use the len() or range() Function to print items
Example
tuple = ('mango' ,' banana', 'apple', 'mango' , 'graps' , 'apple')
for x in range(len(tuple)):
print(tuple[x])
Output
mango
banana
apple
mango
graps
apple
while loop
first get the length of tuple, then start from zero and loop your way through the tuples items by Referring to their indexes
set the start index as Zero
Example
tuple = ('mango' ,' banana', 'apple', 'mango' , 'graps' , 'apple')
i = 0
while i < len(tuple):
print(tuple[i])
i = i + 1
Output
mango
banana
apple
mango
graps
apple