Locked lesson.
About this lesson
Learn to loop using a While loop.
Exercise files
Download this lesson’s related exercise files.
While Loops.docx59.1 KB While Loops - Solution.docx
59.1 KB
Quick reference
While Loops
While loops allow us to iterate until a condition is met.
When to use
Use a while loop whenever you need to iterate.
Instructions
Creating a while loop is pretty easy:
num = 0
while (num < 10):
print(num)
num += 1
Hints & tips
- Create a counter
- Do your loop
- Don't forget to increment your counter!
- 00:04 In this video, I want to talk about while loops, and
- 00:06 Python has a couple of loops we're going to look at.
- 00:09 We're going to start with the while loop, it's one of my favorite loops,
- 00:11 I use it all the time.
- 00:12 And a loop is just what it sounds like, it loops.
- 00:15 It does a thing over, and over, and over again, until some condition is met.
- 00:20 As soon as that condition is met, the loop stops looping.
- 00:23 There's lots of different reasons why you're going to use loops.
- 00:25 They're a fundamental programming concept, you're always going to use them.
- 00:28 And the while loop is pretty easy to use, so let's just kind of jump right in and
- 00:33 build one.
- 00:34 While loops need a counter, so let's create a counter, and
- 00:37 it's just going to be a variable with a number in it.
- 00:39 And let's just call it counter, make it easy, and
- 00:42 let's set the counter equal to 0, right?
- 00:44 Now we need to create our while loop, and all we do is while, and
- 00:48 then some condition.
- 00:49 So let's say, while counter is less than 10.
- 00:53 And you'll notice, this looks an awful lot like an if statement.
- 00:56 But instead of writing if here, we just write while, but the format is the same.
- 01:00 We have this colon, and now we're going to execute some block of code.
- 01:04 So let's go print, and let's say, the count is.
- 01:10 And we want to print out the counter to the screen, right?
- 01:13 Normally, you can concatenate variables into a string, just by going like this.
- 01:19 But in this case, this is a number, right, this is an integer, and
- 01:24 you can't add an integer to a string.
- 01:26 So instead of that, we have to use this formatting thing, this %s, and
- 01:31 then afterwards, we get to define what s is.
- 01:34 So we put this percentage sign again, and then just counter,
- 01:36 same name as our variable here.
- 01:38 So what this is going to do is, going to say, the count is, and
- 01:41 then print out whatever that counter is.
- 01:43 We're almost done, let's look through here and sort of look at the flow of this, and
- 01:47 see what's going on.
- 01:48 It starts out, our counter is 0, right?
- 01:50 We come to our conditional statement, and 0 is less than 10.
- 01:54 So it's going to print out, the count is, and then right here,
- 01:57 it's going to put in 0.
- 01:58 And it's going to loop back over and start again.
- 02:01 And it's going to say, 0 is less than 10, so it's going to print out,
- 02:04 the count is 0.
- 02:05 And it's going to loop back around, and it's going to say, 0 is less than 10,
- 02:09 so it'll print out, the count is 0.
- 02:11 And it will just keep looping over, and over, and over, and over again,
- 02:14 zillions and zillions of times.
- 02:15 And you just fill our screen with the count is 0,
- 02:17 and that's what's called an infinite loop.
- 02:19 And that's bad, so we don't want that.
- 02:21 To make sure that doesn't happen,
- 02:23 we need to increment our counter every time this loops.
- 02:26 So to do that, we just go, counter = counter + 1.
- 02:30 Now, remember back, we did our assignment operators,
- 02:33 and we learned that we can add and assign at the same time.
- 02:36 So let's do that instead, += 1, and
- 02:39 now this is going to increment by 1 every time.
- 02:43 It'll start out, 0, going to print out the counter, the count is 0, it'll add 1,
- 02:48 loop back around.
- 02:49 It'll say 1 is less than 10, it'll print out, the count is 1, add 1,
- 02:53 loop back around.
- 02:54 2 is less than 10, 2, loop around, 3, 4, 5, all the way up till 10, and
- 02:58 then it'll stop.
- 02:59 So let's run this, and it doesn't quite fit, there you go, we can add it up.
- 03:05 Okay, so the count is 0, the count is 1, the count is 2, the count is 9, and
- 03:09 then it stops.
- 03:09 So why is it stopping at 9, well, 9 is less than 10.
- 03:13 So it prints out, the count is 9, and it adds 1, and our counter now becomes 10.
- 03:18 It loops back around, it says, is 10 less than 10?
- 03:21 No,it's not, so it stops, and it doesn't print out, the count is 10.
- 03:25 It loops while it's true, it's true that 1 is less than 10,
- 03:28 it's true that 2 is less than 10.
- 03:30 As soon as it becomes 10, 10 is not less than 10, so that's false, so
- 03:34 it stops looping.
- 03:35 So that's the while loop, very simple.
- 03:38 The thing you gotta remember is, you need to start with a counter.
- 03:40 And so important, you need to increment your counter every time.
- 03:44 Otherwise, your screen is going to fill up with nothing but 0, 0, 0, 0, 0, 0, 0,
- 03:48 into infinity.
- 03:49 The whole thing's going to lock up, you're going to have to reload your web browser,
- 03:51 because it's going to freeze on you.
- 03:53 So, that's pretty good, that's while loop, super useful, super easy,
- 03:56 you're going to use those a lot.
- 03:57 In the next video, we're going to look at for loops.
Lesson notes are only available for subscribers.