How to Use Loop in Tuple Python?
In Python, you can use a for
loop to iterate over the elements of a tuple.
Since tuples are iterable objects, you can access each element one by one using a loop and perform operations on them.
# Sample tuple
fruits = ('apple', 'banana', 'orange', 'grape')
# Using a for loop to iterate over the tuple
for fruit in fruits:
Syntax
# Sample tuple
my_tuple = (element1, element2, element3, …)
# Using a for loop to iterate over the tuple
for element in my_tuple:
# Your code here (perform operations on each element)
How to Use Loop in Tuple Python
# Sample tuple
fruits = ('apple', 'banana', 'orange', 'grape')
# Using a for loop to iterate over the tuple
for fruit in fruits:
print(fruit)
# Output: apple
# banana
# orange
# grape