Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Write a C-Style Loop With range() and Multiple Parameters

Give Feedback

So far in this course, you’ve seen how to avoid writing C-style loops in Python. But what if you need to write a C-style loop, and it needs to be in Python?

If you take a closer look at the range() built-in, you’ll see that you can call it with multiple parameters: start, stop, and step. So you can use range() in a way that closely maps to a C-style loop.

nelsonblue24 on June 29, 2020

Where you say “n is the right-hand side value in that range or it’s the number of elements that you’re iterating over”, I suggest dropping the last part: “or it’s the number of elements that you’re iterating over.” That part conflicts with what you say later: “if you want to iterate over every tenth element, you would just use a step size of ten and then, instead of iterating over each element individually, you would jump ahead ten elements at a time.” I believe dropping the part I suggest dropping would reduce the chances of confusing students.

kiran on Aug. 16, 2020

@Dan Bader Brilliant… & Thanks

Become a Member to join the conversation.