Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
for Loop | Loops
Introduction to Python

for Loop

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:

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.

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'.

Tudo estava claro?

Seção 5. Capítulo 3
course content

Conteúdo do Curso

Introduction to Python

for Loop

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:

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.

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'.

Tudo estava claro?

Seção 5. Capítulo 3
some-alt