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

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands 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 your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

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

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.

00:00 So, I really want to make everyone happy with this video about writing more Pythonic loops, and I know that a common question is, “Hey, so what if I absolutely have to write a C-style loop? Like, what if I have this and I need to convert that to Python in some way, maybe because I’m using a different step size here, how would I do this? Just assuming I would have to do this, how can I do this?” And for that, we’re going to come back to the range() function.

00:31 So what you would do here is you would use the range() function in a way that actually maps pretty closely to the C-style loop here. range() can take several more parameters, so you can pass the initial value to it, which I called a here, and you can pass it the number—like, the right-hand-side value in that range, or sort of the number of elements that you’re iterating over. I called it n here, and you can see how that maps.

00:59 Then, you can also pass a third parameter that is going to be the step size. So, with that s parameter, you can influence how big of a step the range() is going to take between elements.

01:11 So if you want to iterate over every 10th element in the list, you would just use a step size of 10, and then instead of iterating over each element individually, you would jump ahead 10 elements at a time. And that would allow you to pretty much adapt any kind of C-style for loop that is using this indexing stuff, and you would be able to take that and turn it into a sort of Pythonic 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.