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.

Introduction & For Loop Paradigms

Give Feedback

What is iteration? What’s the difference between definite and indefinite iteration? These questions are answered in this introductory lesson to loops in Python. This lesson covers the different types of loops used in different programming languages. The other lessons in this course will focus on Python’s for loop.

00:01 Hello! I’m Darren from Real Python. Welcome to this video where we’re going to be taking a look at definite iteration in Python. We’re going to take a look at for loop paradigms, iterables and iterators, for loops in Python, and range(), break, and continue.

00:18 But what is iteration? Put simply, iteration is executing the same piece of code more than one time. There’s two main types. We’ve got definite iteration, with a for loop which runs a certain number of times that’s known at the construction of the loop.

00:35 And we’ve got indefinite iteration, the while loop, which runs until a condition is met, and we don’t know how many times that will run. But today we’re going to be looking at the definite iteration side of things with the for loop.

00:49 There’s three main types of definite iteration. We’ve got the numeric range loop, the three-expression loop, and the collection-based loop. Let’s take a look at the first one, the numeric range loop.

01:00 The numeric range loop is the simplest of all of these kinds of loops. It just takes a simple numeric value, as its name suggests. So its first time around, i will take the value of 1. It will then execute the loop,

01:15 and then each time around i will increase in value—2, 3, 4, and so on—until it gets to the final value, 10. So in this case, we’ll have a loop that will execute 10 times.

01:28 This sort of for loop is used in languages such as BASIC and Pascal. Next up, we have the three-expression loop. And as its name suggests, there are three expressions which are part of creating the loop.

01:42 The first one sets the initial value of i as our loop is executed. The second one is a condition which must be True for the loop to be executed each time.

01:54 Then the loop is executed, and the third expression comes into play. This is what happens at the end of the loop. And in this case, i is incremented. So, that’s the C form of incrementing.

02:05 But if you’re more used to Python, that will probably make more sense to you.

02:10 Three-expression loops are more flexible than the numeric range loop, and as a result, are common in many more languages, such as C++, Java, PHP, and Perl, amongst others.

02:22 The third form of definite iteration is the collection-based loop. Here, each time around the loop, i takes its value as the next value from the <collection>, which can be a collection of objects of any kind. The loop body is executed, and then the next value comes out of the <collection>.

02:39 Once there are no more values for i to take from the <collection>, the loop is finished. This kind of for loop is arguably the most generalized and abstract.

02:48 Perl and PHP also support this kind of loop, but it’s called foreach instead of for. However, importantly for us, this is the kind of loop that Python implements and that’s what we’re going to be looking at next.

Become a Member to join the conversation.