- Repeating code over and over again, via a construct known as a loop, is also a fairly common scenario in programming. Python provides a couple of ways of doing that, which we're going to take a look at now. So in your editor open up loops_start.pui And you can see that I've got a stubbed out function here named main and a variable named x, which I'm initializing to zero. So, let's start by taking a look at the while loop. To write a while loop, I use the keyword while, and then I give it a condition.
So, I'll write while x is less than five and then my colon. And again you can see that the code is being indented under the colon there. And I'll write print x and then x equals x plus one. A while loop executes a block of code while a particular condition evaluates true. So, while x is less than five, we're going to print the value of x, and then we're going to increment x by one. Some languages, like C, provide a whole bunch of ways of doing loops.
Python likes to keep things simple. It's only got two ways of doing loops, while and for. And we'll get to for in a moment. So, let's run this. I'll go to the debug tab and click run. And you can see that x starts out as zero, so it prints zero, one, two, three, and four, and then it increments to five, the condition in the loop is no longer true, and so the loop terminates. Alright, so now let's try a for loop. So, I'll write for x in range five comma 10.
What you have is this loop counter variable that's controlling the execution of the for loop. So Python works a little bit differently. Python's for loops are what are called iterators. In this case, I want to have x loop over a range of numbers. I used the range function to give me that range of numbers. So, I've got a range going from five to 10, and I'm going to print x each time. So it's probably easier to understand this once you've seen it in action.
So, let's go ahead and save, and then I will run this. And here in the output window you can see that the results are five, six, seven, eight, and then nine. And ten is not inclusive in the range. So, the range starts at this number, and then stops just short of this one. So, it prints out five through nine. But for loops operate over sets of things, not just numbers. So, let's try something a little bit different.
So let's supposed I had a array names including Monday, Tuesday, Wednesday... So, just the days of the week. I could do something like for d in days and then print d. Now, obviously this has nothing to do with numbers. In this case, my for loop is iterating over each element in the days list. And in each iteration d will be set to the current item that it's looking at that time through the loop.
So, if I save and run this, you can see in the output that it's printing out Monday, Tuesday, Wednesday, and so on. So, it's looping over the contents of the list. So, now let's take a look at using the break and continue statements. Comment out some of the previous ones. Alright, so now I'll create a for loop that says for x in range, and once again we'll use five to 10. And I'll write if is equal to seven, then break, otherwise print x.
So this is the same for loop we saw earlier, when we had x in the range of five to 10, but now we're going to do a couple different things. First, let's look at the break statement. The break statement is used to break the execution of a loop, if a condition is met. So, what I'm doing here is saying if x is equal to seven, then break. The break statement will cause this for loop to terminate and fall through to the next block of code. In this case that's the end of the function. There's nothing else to execute. So, we'll save, and run.
And you can see that what's happening is it prints five and six, and then gets to seven, and then the break statement kicks in and causes the loop to terminate. So, it never gets to seven, eight, nine. Alright, so now let's look at the continue statement. The continue statement skips the rest of the loop, for that particular iteration. So, I'm going to comment out this. And I'm going to write if x modulo two is equal to zero, then continue.
Take x, divide it by two, and if the value left over is zero, then continue. Continue basically means skip the rest of the execution of this loop. So, just go back up to the top of the loop, and start with the next value. So, don't do this statement right here that's in the loop. Let's go ahead and save and run this. And you can see that six and eight were skipped, because those are even numbers and my loop only printed out five, seven, and nine.
Well, you can get one if you really need it. Let me add the code here. I'll write for i comma d in days, but I'm going to put the word days inside an enumerate function. The enumerate function will iterate over this collection like loop normally would, but in addition to returning the value of the item being looked at, it also returns a value that is the index of the item in question. This function is going to return two values. It's going to return the index of the member of the collection that we're looking at, as well as the actual member of the collection.
And so I'm going to print something a little bit differently this time. So, I'm going to print i comma d alright? Then I'll save. Alright, so let's go ahead and run this. And now you can see I'm printing out both the index counter variable, as well as the member of the array. So, I'm getting zero and Monday, one and Tuesday, and so on. Alright, that's a brief look at loops in Python.
- Installing Python
- Choosing an editor or IDE
- Working with variables and expressions
- Writing loops
- Using the date, time, and datetime classes
- Reading and writing files
- Fetching internet data
- Parsing and processing HTML