Join Bill Weinman for an in-depth discussion in this video Repeating code with a loop, part of Python 3 Essential Training.
- View Offline
There are two fundamentals types of loops in Python. There is while loops and for loops, and let's start by looking at the while loop. So we'll take our whileloop.py and make a little copy of it, and call it whileloop-working. Go ahead and open that, and you'll see this is a simple Fibonacci series. It's a common exercise for while loops. So here we are assigning two values to a and b: 0 and 1. The 0 will get assigned to a, and the 1 will get assigned to b. We say while b < 50 and we go ahead and do things we'll make b grow in a Fibonacci series and we print it each time using the print function.
So if I go ahead and run this and select the Python Run, you'll see here we get the Fibonacci series. So while loops just work like that. They say while, and then there is a condition. And as long as this condition is true, the while loop will continue to execute. And as soon as this condition becomes not true, then the while loop will end and execution will continue after it if there is any code after it, which in this case we don't have. Down here I could say print ("Done") and if I save that and run it, you'll see we get Done at the end.
You'll notice that the print ("Done") is not indented. If I were to indent it here, then it would become part of the loop. But by having it back here, at the same indention level as the while statement itself, then it is no longer part of this suite of code that is controlled by the while loop. So that's how while loops work. Let's take a look at for loops. Go ahead and make a copy of forloop.py, and I'll name it forloop-working and we'll open that.
What a for loop does is it works with what's called an iterator. An iterator is an object that each time you call it, it gives you the next value. So in this case, we open a file and it's this lines.txt, which we can see right here; it just has five lines of text. Then for line in fh, that's the file handle, which is actually an object and it has this method readlines, which is an iterator.
So for each time this fh.readlines is called, it will put the next value in the line variable, which will be the next line of text, and then we'll print it. So when I run it, we get these lines of text. Now, you'll notice that there is an extra line ending, an extra new line, in each one of these, and that's because the print function puts a new line at the end, and each of these lines actually has a new line at the end of it, so we get two for each one.
If you hover the mouse in Eclipse, you hover the mouse over the print function, you get a little bit of documentation for it. You'll notice that there is an end option, and it defaults to having this new line. So if I just say end= ' ', empty string like that and I save it and run it, now, we don't have that effect anymore. So, the for loop works likes this. It calls an iterator, and its values get put in this variable here.
It has a colon, like all the control structures in Python, and the block of code underneath it is indented. So for each line of readlines, it will take the line, put it in the line variable, and then print it, just like we see here. So that's how for loops work in Python, and those are the two types of loops that we'll see in Python. We have while loops, which are traditional, conditional loops, and for loops, which work with iterators.
- A Python 3 quick start for experienced developers
- Creating functions and objects
- Using Python's built-in objects and classes
- Repeating code with loops and iterators
- Understanding and using conditional expressions
- Creating sequences with generators
- Reusing code with objects and libraries
- Handling errors with exceptions