Keshava Harshith Manikyala
PRO
2 months ago
Keshavacountry asked

Could you provide an explanation of the step argument?

Kelish Rai
Expert
2 months ago
Kelish Rai answered

Since you already know how range() works, let’s look at an example to understand how the step argument works.

Consider this code:

print(list(range(1, 6)))

Output

[1, 2, 3, 4, 5]

Here, the list goes up by 1 each time by default.

Now let’s add 2 as the step:

print(list(range(1, 6, 2)))

Output

[1, 3, 5]

In this case, the list still starts at 1, but it jumps by 2 instead of 1.

So, the step argument simply controls how much the value increases by in each step.

Let me know if you need more clarification on this.

Python
This question was asked as part of the Learn Python Basics course.