Python Tuple For Loop

Python Tuple – Iterate using For Loop

To iterate over the items present in a Python Tuple, you can use for loop statement.

The tuple variable returns an iterator and when used with Python For Loop, you can access each item of the tuple during respective iteration.

Example 1: Iterate over Tuple using For Loop

In this example Python program, we defined a tuple with some string values. Then we used for loop to print each item of the tuple. You can access this item during each iteration by the variable name you provide after for keyword.

Python Program

tuple1 = ('a', 'e', 'i', 'o', 'u')

for item in tuple1:
    #do something
    print(item)
Run this program ONLINE

Output

a
e
i
o
u

Example 2: Tuple For Loop

In this example Python program, we defined a tuple with some numbers. Then we used for loop to iterate over the Tuple items and sum them.

Python Program

tuple1 = (5, 3, 2, 8, 4, 4, 6, 2)

sum = 0

for num in tuple1:
    sum += num
    
print(sum)
Run this program ONLINE

Output

34

Summary

In this tutorial of Python Examples, we learned how to use for loop to iterate over the items of a Tuple in Python.