Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Explore the for Loop in Python | Loops in Python
Introduction to Python (dev copy)

bookExplore the for Loop in Python

When you need to loop through a specific set of values, the for loop is your go-to in Python. Unlike some other languages, you don't need a predefined counter variable for this loop. Instead, you use an iterator variable, which you don't have to define ahead of time. Python for loops can work with sequence types, including lists, tuples, strings, and dictionaries. For instance, if you loop through a string:

123456
# Initial string word = 'Codefinity' # Initialize a for loop for i in word: print(i, end = ' ')
copy

Note

In this code, i is a variable that takes on the value of each character in the string word during each iteration of the for loop. As the loop progresses, i sequentially represents each character in 'Codefinity', and each character is printed out.

As shown, the loop runs through each character (or element) of the string. Similarly, when looping through a list, it covers every item in that list.

123456
# Initial list values = [1, [2, 3], 4, "code"] # Initialize a for loop for el in values: print(el, end = ' ')
copy

Note

The for loop doesn't need you to set up a counter variable in advance. You're free to pick any variable name that suits you. Many programmers gravitate towards names like i or j. In our second example, we opted for el, short for 'element'.

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 5. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Mi faccia domande su questo argomento

Riassuma questo capitolo

Mostri esempi dal mondo reale

Awesome!

Completion rate improved to 1.64

bookExplore the for Loop in Python

Scorri per mostrare il menu

When you need to loop through a specific set of values, the for loop is your go-to in Python. Unlike some other languages, you don't need a predefined counter variable for this loop. Instead, you use an iterator variable, which you don't have to define ahead of time. Python for loops can work with sequence types, including lists, tuples, strings, and dictionaries. For instance, if you loop through a string:

123456
# Initial string word = 'Codefinity' # Initialize a for loop for i in word: print(i, end = ' ')
copy

Note

In this code, i is a variable that takes on the value of each character in the string word during each iteration of the for loop. As the loop progresses, i sequentially represents each character in 'Codefinity', and each character is printed out.

As shown, the loop runs through each character (or element) of the string. Similarly, when looping through a list, it covers every item in that list.

123456
# Initial list values = [1, [2, 3], 4, "code"] # Initialize a for loop for el in values: print(el, end = ' ')
copy

Note

The for loop doesn't need you to set up a counter variable in advance. You're free to pick any variable name that suits you. Many programmers gravitate towards names like i or j. In our second example, we opted for el, short for 'element'.

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 5. Capitolo 3
some-alt