Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
range() | For Loops
Python Loops
course content

Conteúdo do Curso

Python Loops

Python Loops

1. While Loops: Get Started
2. While Loops: Essentials
3. For Loops
4. Nested Loops

range()

range() is a special function that generates range of numbers. range() has three parameters:

range(start, end, step) - returns the numbers starting from start up to end (exclusive) with the step step.

Examples

range(1, 10, 1): returns (1, 2, 3, 4, 5, 6, 7, 8, 9).

range(-3, 4, 3): returns (-3, 0, 3).

range(22, 22, 1): returns empty range.

You can also use such a range() functions:

  • range(end): the shorter version of range(0, end, 1)
  • range(start, end): the shorter version of range(start, end, 1).

Run the following examples to see the loop iteration.

12345678
for i in range(8): print(i) for i in range(7, -1, -1): print(i) for i in range(1, 11, 2): print(i)
copy

Using range() in for loop condition helps to specify the start and end conditions, however set a step. By the way, variable that stores the value (here is an i) shouldn't be defined before the loop. It exists only inside the loop.

Negative step

What happens if pass negative value of step? Actually, still the same: generates range of numbers starting with start decreasing down to end (still exclusive). The code to generate odd numbers between 8 and 20 in descending order:

12
for i in range(20, 8, -2): print(i)
copy

Tarefa

Output the list in reverse order using for loop and range(): iterate from the last symbol to the first one. Output each symbol separately on each line.

Tarefa

Output the list in reverse order using for loop and range(): iterate from the last symbol to the first one. Output each symbol separately on each line.

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

Tudo estava claro?

Seção 3. Capítulo 2
toggle bottom row

range()

range() is a special function that generates range of numbers. range() has three parameters:

range(start, end, step) - returns the numbers starting from start up to end (exclusive) with the step step.

Examples

range(1, 10, 1): returns (1, 2, 3, 4, 5, 6, 7, 8, 9).

range(-3, 4, 3): returns (-3, 0, 3).

range(22, 22, 1): returns empty range.

You can also use such a range() functions:

  • range(end): the shorter version of range(0, end, 1)
  • range(start, end): the shorter version of range(start, end, 1).

Run the following examples to see the loop iteration.

12345678
for i in range(8): print(i) for i in range(7, -1, -1): print(i) for i in range(1, 11, 2): print(i)
copy

Using range() in for loop condition helps to specify the start and end conditions, however set a step. By the way, variable that stores the value (here is an i) shouldn't be defined before the loop. It exists only inside the loop.

Negative step

What happens if pass negative value of step? Actually, still the same: generates range of numbers starting with start decreasing down to end (still exclusive). The code to generate odd numbers between 8 and 20 in descending order:

12
for i in range(20, 8, -2): print(i)
copy

Tarefa

Output the list in reverse order using for loop and range(): iterate from the last symbol to the first one. Output each symbol separately on each line.

Tarefa

Output the list in reverse order using for loop and range(): iterate from the last symbol to the first one. Output each symbol separately on each line.

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

Tudo estava claro?

Seção 3. Capítulo 2
toggle bottom row

range()

range() is a special function that generates range of numbers. range() has three parameters:

range(start, end, step) - returns the numbers starting from start up to end (exclusive) with the step step.

Examples

range(1, 10, 1): returns (1, 2, 3, 4, 5, 6, 7, 8, 9).

range(-3, 4, 3): returns (-3, 0, 3).

range(22, 22, 1): returns empty range.

You can also use such a range() functions:

  • range(end): the shorter version of range(0, end, 1)
  • range(start, end): the shorter version of range(start, end, 1).

Run the following examples to see the loop iteration.

12345678
for i in range(8): print(i) for i in range(7, -1, -1): print(i) for i in range(1, 11, 2): print(i)
copy

Using range() in for loop condition helps to specify the start and end conditions, however set a step. By the way, variable that stores the value (here is an i) shouldn't be defined before the loop. It exists only inside the loop.

Negative step

What happens if pass negative value of step? Actually, still the same: generates range of numbers starting with start decreasing down to end (still exclusive). The code to generate odd numbers between 8 and 20 in descending order:

12
for i in range(20, 8, -2): print(i)
copy

Tarefa

Output the list in reverse order using for loop and range(): iterate from the last symbol to the first one. Output each symbol separately on each line.

Tarefa

Output the list in reverse order using for loop and range(): iterate from the last symbol to the first one. Output each symbol separately on each line.

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

Tudo estava claro?

range() is a special function that generates range of numbers. range() has three parameters:

range(start, end, step) - returns the numbers starting from start up to end (exclusive) with the step step.

Examples

range(1, 10, 1): returns (1, 2, 3, 4, 5, 6, 7, 8, 9).

range(-3, 4, 3): returns (-3, 0, 3).

range(22, 22, 1): returns empty range.

You can also use such a range() functions:

  • range(end): the shorter version of range(0, end, 1)
  • range(start, end): the shorter version of range(start, end, 1).

Run the following examples to see the loop iteration.

12345678
for i in range(8): print(i) for i in range(7, -1, -1): print(i) for i in range(1, 11, 2): print(i)
copy

Using range() in for loop condition helps to specify the start and end conditions, however set a step. By the way, variable that stores the value (here is an i) shouldn't be defined before the loop. It exists only inside the loop.

Negative step

What happens if pass negative value of step? Actually, still the same: generates range of numbers starting with start decreasing down to end (still exclusive). The code to generate odd numbers between 8 and 20 in descending order:

12
for i in range(20, 8, -2): print(i)
copy

Tarefa

Output the list in reverse order using for loop and range(): iterate from the last symbol to the first one. Output each symbol separately on each line.

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