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

Imagine that we wish to print numbers from 1 to 100. Naturally, we could do this manually:

But that would be incredibly time-consuming!

If we had to print numbers from 1 to 1,000,000, we'd be counting manually for the rest of our lives!

To tackle this challenge, we'll employ the for loop in conjunction with the range() function!

Take a look at the following code:

123
# Printing all numbers from 0 to 100 for i in range(101): print(i)
copy

How does the code work?

The range() function provides a sequence of numbers that starts at 0 (by default) when the initial limit is unspecified. It increments by 1 (by default) until it reaches a final limit (the final limit itself is not included).

The format of the range() function is: range(start, end, step).

If our intention is to operate with decreasing numbers, we can achieve this by inputting a negative step argument into the range() function.

Look at the code below:

123456789
# Increasing # Printing numbers from 0 to 10 for i in range(0, 11, 1): print(i) # Decreasing # Printing numbers from 10 to 0 for i in range(10, -1, -1): print(i)
copy

Tarefa

Print numbers from -1 to -5, follow these steps:

  1. Configure the for loop to display decreasing numbers from -1 to -5.
  2. Print each number within the loop.

Tarefa

Print numbers from -1 to -5, follow these steps:

  1. Configure the for loop to display decreasing numbers from -1 to -5.
  2. Print each number within the loop.

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

Range() in a for Loop 1/2

Imagine that we wish to print numbers from 1 to 100. Naturally, we could do this manually:

But that would be incredibly time-consuming!

If we had to print numbers from 1 to 1,000,000, we'd be counting manually for the rest of our lives!

To tackle this challenge, we'll employ the for loop in conjunction with the range() function!

Take a look at the following code:

123
# Printing all numbers from 0 to 100 for i in range(101): print(i)
copy

How does the code work?

The range() function provides a sequence of numbers that starts at 0 (by default) when the initial limit is unspecified. It increments by 1 (by default) until it reaches a final limit (the final limit itself is not included).

The format of the range() function is: range(start, end, step).

If our intention is to operate with decreasing numbers, we can achieve this by inputting a negative step argument into the range() function.

Look at the code below:

123456789
# Increasing # Printing numbers from 0 to 10 for i in range(0, 11, 1): print(i) # Decreasing # Printing numbers from 10 to 0 for i in range(10, -1, -1): print(i)
copy

Tarefa

Print numbers from -1 to -5, follow these steps:

  1. Configure the for loop to display decreasing numbers from -1 to -5.
  2. Print each number within the loop.

Tarefa

Print numbers from -1 to -5, follow these steps:

  1. Configure the for loop to display decreasing numbers from -1 to -5.
  2. Print each number within the loop.

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

Range() in a for Loop 1/2

Imagine that we wish to print numbers from 1 to 100. Naturally, we could do this manually:

But that would be incredibly time-consuming!

If we had to print numbers from 1 to 1,000,000, we'd be counting manually for the rest of our lives!

To tackle this challenge, we'll employ the for loop in conjunction with the range() function!

Take a look at the following code:

123
# Printing all numbers from 0 to 100 for i in range(101): print(i)
copy

How does the code work?

The range() function provides a sequence of numbers that starts at 0 (by default) when the initial limit is unspecified. It increments by 1 (by default) until it reaches a final limit (the final limit itself is not included).

The format of the range() function is: range(start, end, step).

If our intention is to operate with decreasing numbers, we can achieve this by inputting a negative step argument into the range() function.

Look at the code below:

123456789
# Increasing # Printing numbers from 0 to 10 for i in range(0, 11, 1): print(i) # Decreasing # Printing numbers from 10 to 0 for i in range(10, -1, -1): print(i)
copy

Tarefa

Print numbers from -1 to -5, follow these steps:

  1. Configure the for loop to display decreasing numbers from -1 to -5.
  2. Print each number within the loop.

Tarefa

Print numbers from -1 to -5, follow these steps:

  1. Configure the for loop to display decreasing numbers from -1 to -5.
  2. Print each number within the loop.

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

Tudo estava claro?

Imagine that we wish to print numbers from 1 to 100. Naturally, we could do this manually:

But that would be incredibly time-consuming!

If we had to print numbers from 1 to 1,000,000, we'd be counting manually for the rest of our lives!

To tackle this challenge, we'll employ the for loop in conjunction with the range() function!

Take a look at the following code:

123
# Printing all numbers from 0 to 100 for i in range(101): print(i)
copy

How does the code work?

The range() function provides a sequence of numbers that starts at 0 (by default) when the initial limit is unspecified. It increments by 1 (by default) until it reaches a final limit (the final limit itself is not included).

The format of the range() function is: range(start, end, step).

If our intention is to operate with decreasing numbers, we can achieve this by inputting a negative step argument into the range() function.

Look at the code below:

123456789
# Increasing # Printing numbers from 0 to 10 for i in range(0, 11, 1): print(i) # Decreasing # Printing numbers from 10 to 0 for i in range(10, -1, -1): print(i)
copy

Tarefa

Print numbers from -1 to -5, follow these steps:

  1. Configure the for loop to display decreasing numbers from -1 to -5.
  2. Print each number within the loop.

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