bhylu.com

Advertisements

Loop Tuples

A tuple can be iterated directly. Use that when you have a fixed sequence of values to process rather than trying to edit its positions.

Understand the idea

The loop does not care whether its iterable is a list or tuple. It requests the next element until iteration ends. enumerate adds a count without changing the original record. If a tuple represents one structured record rather than many similar items, unpack its fields instead of treating them as interchangeable rows.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

A worked example

Python

days = ("Monday", "Wednesday", "Friday")
for day in days:
    print(day)

Read the result

The three names print in their stored order.

Try one change

Add display numbering with enumerate without changing days.