0d: 00h: 00m: 00s

šŸŽ Get 3 extra months of learning — completely FREE

That's 90 extra days to master new skills, build more projects, and land your dream job.

Become a PRO
Background Image

šŸŽ Get 3 extra months of learning completely FREE

Become a PRO
Keshava Harshith Manikyala
last year
Keshavacountry asked

Could you provide an explanation of the step argument?

Kelish Rai
Expert
last year
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.