Tuple Unpacking :
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)
Output :
apple
banana
cherry
Tuple Unpacking with Asterisk (*) :
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")
(green, yellow, *red) = fruits
print(green)
print(yellow)
print(red)
Output :
apple
banana
['cherry', 'strawberry', 'raspberry']
Asterisk in Middle Variable :
fruits = ("apple", "mango", "papaya", "pineapple", "cherry")
(green, *tropic, red) = fruits
print(green)
print(tropic)
print(red)
Output :
apple
['mango', 'papaya', 'pineapple']
cherry
Loop Through Tuple using For Loop :
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
Output :
apple
banana
cherry
Loop Through Tuple using Index :
thistuple = ("apple", "banana", "cherry")
for i in range(len(thistuple)):
print(thistuple[i])
Output :
apple
banana
cherry
Loop Through Tuple using While Loop :
thistuple = ("apple", "banana", "cherry")
i = 0
while i < len(thistuple):
print(thistuple[i])
i += 1
Output :
apple
banana
cherry