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.

Looping: Definite Iteration

Give Feedback

The most common use for the range function is looping. In computer science, a loop is a structure that allows you to run a block of code multiple times without having to write that code over and over again. One type of loop that Python supports is the for loop:

for name in names:
    check_name(name)

This loop will execute for each name in the list. name is simply a temporary variable that will change on each loop.

00:00 The most common use for the range() function is looping. In computer science, a loop is a structure that will allow us to run a block of code multiple times without having to write that code over and over again. For example, say we have a list of first and last names and for each name in the list, we want to determine if the name is capitalized correctly—that is, the first and last names start with capital letters.

00:32 One way we could accomplish this is by writing a function that will check to make sure the name is capitalized correctly. It will return True if it is, and False if it is not.

00:44 We can simply call this function multiple times, once for each name we have in our names list.

00:52 And when I run this code,

00:55 you see that it works just as we’d expect. It detects the names that are not capitalized correctly. This code is fully functional but, as you might’ve guessed, it’s going to get pretty messy if we start adding more names to our names list.

01:12 Think about what our code would look like if we had 1,000 names. Then we’d need to call the check_name() function 1,000 times, which would require 1,000 lines of code.

01:26 Instead, let’s use a loop. One type of loop Python supports is called a for loop. The for loop is used for definite iteration. In other words, it’s used to visit each element in a collection where we know upfront how many elements that collection has.

01:47 One example of this collection is the Python list, which is why the list is an iterable type. It’s a collection of elements that can be visited one element at a time. To improve this program, I’m going to first remove all of these function calls to check_name().

02:07 Now, underneath my list of names, I’ll create a new for loop that will iterate through each name in the list. That looks like this.

02:17 for name in names:.

02:23 This loop will execute once for each name in the list. Here, name is simply a temporary variable that will change on each loop. In other words, the first iteration of the loop will set name equal to "John Smith", and then the second iteration will change it to "will Williams", and so on.

02:46 This is going to allow us to perform some set of operations on each element of the list, one by one. Here, all we want to do is call the check_name() function, passing in whatever the name is on this current iteration of the loop.

03:04 And if I run this,

03:07 you’ll see we get the exact same output as before. So, that’s one way we can loop in Python—specifically, definite iteration with for loops.

03:19 There’s also indefinite iteration with while loops, but I won’t be covering that in this course because it’s not as commonly used with the range() function. If you’d like to learn more about either type of loop we have great courses on both of them here at realpython.com.

03:37 Now, let’s see all the different ways we can use the range() function to create slightly different loops.

mosc0 on Oct. 29, 2019

great info. i knew a some about it but it unclogged my mind. one question though, I have seen them using range in def functions functions. do you have a video about this?

Austin Cepalia RP Team on Nov. 2, 2019

@mosc0 I’m not exactly sure what you mean? Can you provide a code example?

Become a Member to join the conversation.