Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
List & for Loop | The for Loop
Python Loops Tutorial
course content

Conteúdo do Curso

Python Loops Tutorial

Python Loops Tutorial

1. The for Loop
2. The while Loop
3. Nested Loops

List & for Loop

We can employ various data structures when working with loops. So far, we've covered strings, and now we'll explore working with a list of numbers.

Examine the following code:

12345
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Printing all elements of the list for i in numbers: print(i)
copy

How does the code work?

To work with a decreasing list, you can utilize the reversed() function. This function allows you to reverse the order of elements in a list, effectively turning it into a decreasing list.

12345
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Printing all elements of the reversed list for i in reversed(numbers): print(i)
copy

How does the code work?

Tarefa

To sum the elements inside the list, follow these steps:

  1. Configure the for loop to operate on the numbers, utilizing i as an element of the list.
  2. Calculate the sum by adding elements to the counter within the loop.
  3. Print the value of the counter after the loop has completed.

Tarefa

To sum the elements inside the list, follow these steps:

  1. Configure the for loop to operate on the numbers, utilizing i as an element of the list.
  2. Calculate the sum by adding elements to the counter within the loop.
  3. Print the value of the counter after the loop has completed.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 1. Capítulo 4
toggle bottom row

List & for Loop

We can employ various data structures when working with loops. So far, we've covered strings, and now we'll explore working with a list of numbers.

Examine the following code:

12345
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Printing all elements of the list for i in numbers: print(i)
copy

How does the code work?

To work with a decreasing list, you can utilize the reversed() function. This function allows you to reverse the order of elements in a list, effectively turning it into a decreasing list.

12345
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Printing all elements of the reversed list for i in reversed(numbers): print(i)
copy

How does the code work?

Tarefa

To sum the elements inside the list, follow these steps:

  1. Configure the for loop to operate on the numbers, utilizing i as an element of the list.
  2. Calculate the sum by adding elements to the counter within the loop.
  3. Print the value of the counter after the loop has completed.

Tarefa

To sum the elements inside the list, follow these steps:

  1. Configure the for loop to operate on the numbers, utilizing i as an element of the list.
  2. Calculate the sum by adding elements to the counter within the loop.
  3. Print the value of the counter after the loop has completed.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 1. Capítulo 4
toggle bottom row

List & for Loop

We can employ various data structures when working with loops. So far, we've covered strings, and now we'll explore working with a list of numbers.

Examine the following code:

12345
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Printing all elements of the list for i in numbers: print(i)
copy

How does the code work?

To work with a decreasing list, you can utilize the reversed() function. This function allows you to reverse the order of elements in a list, effectively turning it into a decreasing list.

12345
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Printing all elements of the reversed list for i in reversed(numbers): print(i)
copy

How does the code work?

Tarefa

To sum the elements inside the list, follow these steps:

  1. Configure the for loop to operate on the numbers, utilizing i as an element of the list.
  2. Calculate the sum by adding elements to the counter within the loop.
  3. Print the value of the counter after the loop has completed.

Tarefa

To sum the elements inside the list, follow these steps:

  1. Configure the for loop to operate on the numbers, utilizing i as an element of the list.
  2. Calculate the sum by adding elements to the counter within the loop.
  3. Print the value of the counter after the loop has completed.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

We can employ various data structures when working with loops. So far, we've covered strings, and now we'll explore working with a list of numbers.

Examine the following code:

12345
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Printing all elements of the list for i in numbers: print(i)
copy

How does the code work?

To work with a decreasing list, you can utilize the reversed() function. This function allows you to reverse the order of elements in a list, effectively turning it into a decreasing list.

12345
numbers = [2, 3, 4, 5, 6, 7, 8, 11] # Printing all elements of the reversed list for i in reversed(numbers): print(i)
copy

How does the code work?

Tarefa

To sum the elements inside the list, follow these steps:

  1. Configure the for loop to operate on the numbers, utilizing i as an element of the list.
  2. Calculate the sum by adding elements to the counter within the loop.
  3. Print the value of the counter after the loop has completed.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 1. Capítulo 4
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt