Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Range() in a for Loop 2/2 | 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

Range() in a for Loop 2/2

Note

In many of our tasks, we'll frequently employ a counter variable to facilitate basic arithmetic operations.

For instance, if we wish to sum 2 and 3, we'll initialize the counter with counter = 0 and then add 2 and 3 to it: counter = 0 + 2 + 3 = 5 => counter = 5.

The utilization of such variables is essential for managing straightforward mathematical operations effectively.

Take a look at the following code:

12345678
counter = 0 # Summing all numbers from 50 to 100 (included) for i in range(50, 101): counter += i # Printing the counter print(counter)
copy

How does the code work?

Tarefa

We've previously seen an example of summing elements from 50 to 100 using an increasing list. Now, let's tackle this task with a decreasing list:

  1. Establish a for loop, utilizing i as an element of the range and setting step = -1.
  2. Execute the summation by adding the elements within the range.
  3. Print the value of the counter.

Tarefa

We've previously seen an example of summing elements from 50 to 100 using an increasing list. Now, let's tackle this task with a decreasing list:

  1. Establish a for loop, utilizing i as an element of the range and setting step = -1.
  2. Execute the summation by adding the elements within the range.
  3. Print the value of the counter.

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 3
toggle bottom row

Range() in a for Loop 2/2

Note

In many of our tasks, we'll frequently employ a counter variable to facilitate basic arithmetic operations.

For instance, if we wish to sum 2 and 3, we'll initialize the counter with counter = 0 and then add 2 and 3 to it: counter = 0 + 2 + 3 = 5 => counter = 5.

The utilization of such variables is essential for managing straightforward mathematical operations effectively.

Take a look at the following code:

12345678
counter = 0 # Summing all numbers from 50 to 100 (included) for i in range(50, 101): counter += i # Printing the counter print(counter)
copy

How does the code work?

Tarefa

We've previously seen an example of summing elements from 50 to 100 using an increasing list. Now, let's tackle this task with a decreasing list:

  1. Establish a for loop, utilizing i as an element of the range and setting step = -1.
  2. Execute the summation by adding the elements within the range.
  3. Print the value of the counter.

Tarefa

We've previously seen an example of summing elements from 50 to 100 using an increasing list. Now, let's tackle this task with a decreasing list:

  1. Establish a for loop, utilizing i as an element of the range and setting step = -1.
  2. Execute the summation by adding the elements within the range.
  3. Print the value of the counter.

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 3
toggle bottom row

Range() in a for Loop 2/2

Note

In many of our tasks, we'll frequently employ a counter variable to facilitate basic arithmetic operations.

For instance, if we wish to sum 2 and 3, we'll initialize the counter with counter = 0 and then add 2 and 3 to it: counter = 0 + 2 + 3 = 5 => counter = 5.

The utilization of such variables is essential for managing straightforward mathematical operations effectively.

Take a look at the following code:

12345678
counter = 0 # Summing all numbers from 50 to 100 (included) for i in range(50, 101): counter += i # Printing the counter print(counter)
copy

How does the code work?

Tarefa

We've previously seen an example of summing elements from 50 to 100 using an increasing list. Now, let's tackle this task with a decreasing list:

  1. Establish a for loop, utilizing i as an element of the range and setting step = -1.
  2. Execute the summation by adding the elements within the range.
  3. Print the value of the counter.

Tarefa

We've previously seen an example of summing elements from 50 to 100 using an increasing list. Now, let's tackle this task with a decreasing list:

  1. Establish a for loop, utilizing i as an element of the range and setting step = -1.
  2. Execute the summation by adding the elements within the range.
  3. Print the value of the counter.

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

Tudo estava claro?

Note

In many of our tasks, we'll frequently employ a counter variable to facilitate basic arithmetic operations.

For instance, if we wish to sum 2 and 3, we'll initialize the counter with counter = 0 and then add 2 and 3 to it: counter = 0 + 2 + 3 = 5 => counter = 5.

The utilization of such variables is essential for managing straightforward mathematical operations effectively.

Take a look at the following code:

12345678
counter = 0 # Summing all numbers from 50 to 100 (included) for i in range(50, 101): counter += i # Printing the counter print(counter)
copy

How does the code work?

Tarefa

We've previously seen an example of summing elements from 50 to 100 using an increasing list. Now, let's tackle this task with a decreasing list:

  1. Establish a for loop, utilizing i as an element of the range and setting step = -1.
  2. Execute the summation by adding the elements within the range.
  3. Print the value of the counter.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 1. Capítulo 3
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