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.

Examples

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 Code Copy

Output

a
e
i
o
u

2. Iterate over tuple of integers

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 Code Copy

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.

Related Tutorials

Code copied to clipboard successfully 👍