Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära How to Iterate Over Indexes in Python | Loops in Python
Introduction to Python(ihor)

bookHow to Iterate Over Indexes in Python

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.

question mark

When using range(len(values)) to iterate over a list, what does the variable i represent?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 7

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Ställ mig frågor om detta ämne

Sammanfatta detta kapitel

Visa verkliga exempel

Awesome!

Completion rate improved to 1.67

bookHow to Iterate Over Indexes in Python

Svep för att visa menyn

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.

question mark

When using range(len(values)) to iterate over a list, what does the variable i represent?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 7
some-alt