Range() in a for Loop 1/2Range() 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:

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:


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.

Everything was clear?

Section 1. Chapter 2
toggle bottom row