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.
Introduction & For Loop Paradigms
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.

Ross on July 31, 2024
The first slide shows the format of a definite for loop and an indefinite while loop. The comment under the while loop is: Runs until a condition is met. It should say: Runs until the condition is not true. (Or the loop body contains a break condition that becomes true.)