Python Fundamentals

while & Control Statements

Python Fundamentals

Check out a free preview of the full Python Fundamentals course

The "while & Control Statements" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates how to implement while loops, continue, and break.

Preview
Close

Transcript from the "while & Control Statements" Lesson

[00:00:00]
>> Nina Zakharenko: Now that we've talked about for loops, we're gonna talk about the different kind of loop in Python, the while loop. They're a special type of loop that run forever until some condition is met, unlike that for loop we saw earlier that just ran until the list of items that it was running through was exhausted.

[00:00:24]
So let's see while loops in practice. Let's say I have a counter variable and I have maximum value here. I'm gonna set a while loop that's gonna run while my counter is less than the maximum amount. And in here I'm going to print out the count is counter.

[00:00:49]
>> Nina Zakharenko: Now, if I left my loop just like this, It would run forever. I would have to, if I was running it in REPL, I'd have to press Ctrl+C or maybe Ctrl+Z on Windows to kill it. That's because we're not updating our counter. So in order for this loop to only run a certain number of times, we need to make sure that the condition for the while loop is gonna be met eventually.

[00:01:17]
So I can say counter = counter + 1, and now when I run this loop, it'll just run four times.
>> Nina Zakharenko: If I never put this in, remember that my loop would run forever.
>> Nina Zakharenko: A great way of controlling white loops is with break, continue, and return. These statements allow us to control the flow of our programs.

[00:01:50]
They're really a concept that beginners to Python tend to misunderstand, so I would say pay careful attention here.
>> Nina Zakharenko: A break statement will completely break out of the current loop. Meaning that it won't run any more of the statements contained inside of it.
>> Nina Zakharenko: So using break,
>> Nina Zakharenko: Let's say I had my list of names here,

[00:02:30]
>> Nina Zakharenko: Max, Rose, Nina, and Jimmy. I can say for name in names, and print out the f string of Hello, followed by name.
>> Nina Zakharenko: I'm gonna add a break statement to this. I'm gonna say if the name is equal to Nina,
>> Nina Zakharenko: break. When I run this on my list, it's gonna say Hello, Max, Hello, Rose, Hello, Nina.

[00:03:11]
But it won't print out Hello, Jimmy because this condition was met, and this break statement completely broke out of the loop. Remember that break completely breaks out of the loop. There's another statement used for the control flow of loops, and that is continue. Continue works a little bit differently.

[00:03:37]
Instead of breaking out of the loop completely, instead it goes back to the start of the loop and keeps running over it skipping any other statements that came after the continue. If I have my same list of names and I say for name in names, and this time I'm gonna check if the name is not equal to Nina, I'm going to continue.

[00:04:09]
Otherwise, I'm going to print out Hello and name.
>> Nina Zakharenko: Running this code, oops, I forgot my f string there. So let's do that one more time. For name in names, if the name is not equal to Nina, we're going to continue. Otherwise, we're gonna print out the f string Hello, name.

[00:04:37]
Okay, this only prints out Hello, Nina. Why? Looking back at our list of names, if the name is not equal to Nina, which in this case it's not a max, It's gonna continue. Which means it's gonna jump back to the top of the loop, look at the next item, and the check this condition again.

[00:04:58]
When it's Rose, the condition's not met, so it's gonna continue back to the top. Finally, when it's Nina, this condition is not met, and Hello and name will be printed out. There's a helpful diagram to visualize this on the course website, right here. So for my list of names, as I'm looping through the name, if the length of the name is not equal to 4, it's gonna continue back to the top of the loop.

[00:05:29]
So for this first name in the list, Jimmy, the length is not equal to 4, so it's just gonna come back around and look at the next name, Rose.
>> Nina Zakharenko: Now, because this condition is not met, the length is equal to 4, it's gonna print Hello, name, and then go back to the top of the loop because the name is not equal to Nina, right?

[00:05:56]
So it's just gonna keep going. Max does not fit the length of name equals 4, so continue back to the top of the loop. Now, for Nina, the length is equal to 4, so we're gonna keep going in this code, we're gonna print out Hello, Nina. But we're also gonna see this additional condition.

[00:06:15]
If the name is equal to Nina, it's gonna completely break out of the loop and not run it any further. Let's see the results of this code. We'll see Hello, Rose, Hello, Nina, and then immediately print out Done.
>> Nina Zakharenko: Something to know is that if you use break and continue inside of nested loops, you're only going to break out of the loop at the level of the break.

[00:06:48]
So if you have two nested for loops, and your break is inside that inner for loop, when you break out of it, that outer for loop is still going to continue, so something to be mindful of. You can also use break to do loop control inside of while statements.

[00:07:07]
So let's say I had a condition where I ran my while loop, while True, which means kinda run infinitely forever. And,
>> Nina Zakharenko: Sorry, to do that, I'm going to say, I'm gonna give a count, I'm gonna start the count of 0. And then I'm gonna say while True, the count, add the value 1 to it.

[00:07:32]
Plus equals here means it's the same as count equals count plus 1. And then I'm gonna say, if the count is equal to 5, I'm gonna print out the count was reached,
>> Nina Zakharenko: And I'm going to break.
>> Nina Zakharenko: Great, so when the count hit 5, it printed this out and our loop stopped.

[00:07:57]
So we got around this condition for an infinite while loop by using a break statement. Remember that, just like inside of functions, you can consider the return statement to be a hard kill switch of the loop.
>> Nina Zakharenko: Let's see this in practice.
>> Nina Zakharenko: Looking at our names list, let's make a new function.

[00:08:28]
It's going to,
>> Nina Zakharenko: Find a target, so I'm gonna call it find_target_name. And then it's gonna loop through each name in the names, and it's gonna print out the name. But we have a special condition here, if the name is equal to Nina, oops,
>> Nina Zakharenko: I pressed Enter too early, so let me do that again.

[00:09:04]
>> Nina Zakharenko: Okay, if the name is equal to Nina, I'm just gonna return the string, Found the special case.
>> Nina Zakharenko: Let's say I have a shorter list of names here that contains Max, Nina, and Rose. If I call find_target_name on my list of names, it'll print Max, Nina, and then Found the special case, but it'll never get to Rose.

[00:09:35]
So we learned about how break, continue, and return allow us to control the flow of our loops, both for loops and while loops.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now