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

bookHow to Iterate Over Indexes in Python

Keep in mind that the range() function needs at least one argument. Since indexing in Python begins at 0, you can use the length of the sequence as the sole argument for the range() function.

To determine the length of a sequence, employ the len() function. For example, you can loop through the list from earlier sections, but this time by its indices.

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.

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 5. Kapittel 7

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Still meg spørsmål om dette emnet

Oppsummer dette kapittelet

Vis eksempler fra virkeligheten

Awesome!

Completion rate improved to 1.64

bookHow to Iterate Over Indexes in Python

Sveip for å vise menyen

Keep in mind that the range() function needs at least one argument. Since indexing in Python begins at 0, you can use the length of the sequence as the sole argument for the range() function.

To determine the length of a sequence, employ the len() function. For example, you can loop through the list from earlier sections, but this time by its indices.

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.

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 5. Kapittel 7
some-alt