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

Course Content

Python Loops Tutorial

Python Loops Tutorial

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

bookRange() 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

Task

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 2
toggle bottom row

bookRange() 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

Task

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 2
toggle bottom row

bookRange() 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

Task

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

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

Task

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt