Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn How to Iterate Over Indexes in Python | Loops in Python
Introduction to Python
course content

Course Content

Introduction to Python

Introduction to Python

1. First Acquaintance with Python
2. Variables and Types in Python
3. Conditional Statements in Python
4. Other Data Types in Python
5. Loops in Python
6. Functions in Python

book
How to Iterate Over Indexes in Python

Remember, the range() function requires at least one argument. Since Python indexing starts at 0, using the length of a sequence as the sole argument allows iteration over all its indices.

To get the sequence length, use the len() function. For example, you can loop through the list from previous sections, but this time by indexing its elements.

12345678
# Initial list values = [1, [2, 3], 4, "code"] # Initialize a for loop over indexes for i in range(len(values)): print("Index:", i) print("Value:", values[i]) print("----") # Delimiter
copy

At each iteration of the loop, the variable i sequentially traverses the indices of values, ranging from 0 to 3.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 7
We're sorry to hear that something went wrong. What happened?
some-alt