Control flow
- [Instructor] This is where programming really starts to get powerful. There are three main types of control flow that we're going to cover and the first of these is the if statement. So this is pretty straightforward, a equals true, if a: print It is true. And if I change a to false, nothing prints out. So notice that I've indented in the line after my if statement here. So this colon followed by an indent means that everything indented under this line belongs under the if statement. It sort of belongs to the if statement. So any indented code is executed only if a is true. I could add another indented print line, Also print this, and now both of these lines belong under the if statement. We say we're in the if block, a block of code as these indented lines under here. Now, I could also outdent and add another line, Always print this. We see that even if a is false, this gets printed out because it doesn't belong to this if statement here. And, of course, if a is true, all three of these lines get printed. I can also add an else statement here, else: print It is false. Okay, so if a is true, do this block. Otherwise, do this block. So the indenting in Python is very important here. It really controls the structure of your program. Things can really get indented quite a bit, like let's bring this down into a new cell here and let's add another variable. Let's call it b. And we can actually indent under here and make a completely new block. If b: Both are true. And if we run this, we see Both are true printed out. We can even add a third variable, c. Just keep indenting, if c: print All three are true and you see that gets printed out. Of course, if we set any of these to false along the way, we never reach that line there. So you can see how this could start to get a little bit ridiculous. In order to prevent excessive indenting and convoluted structures like this, Python has another tool and that's called the loop. Let's look at the for loop. So for loops iterate over, well, what we call in Python iterables. A list is a type of iterable. So we can make a list like 1,2,3,4,5 for item in my list, my iterable list a, print item. Now keep in mind that the in here is not the same as we did previously. So 4 in a, that gives us a Boolean that says that yes, it is true that the number four is in this list of items here. This in is actually very different and it's just part of the for loop syntax. You say for each item in the list, do something with that item. The other thing to note is that the item here is just a variable that we're declaring in this line. You could use anything in here, for b in a, for number in a, you get to name it in that for loop. Just remember to change the variable name inside your for loop. Otherwise, you'll get an error. A while loop is similar to a for loop except that it doesn't iterate over items in a list. It just keeps looping until the Boolean you pass is false. So say a is equal to zero, while a is less than five, print a. Now here's a very important line, a is equal to a plus one, and we see that we get basically the same results as the for loop. We're starting with zero, of course, but it's very important that you have this line in here because without it, the loop would never end and we'd be printing the number zero forever. More than one programmer has made that mistake. So generally you want to use while loops when conditions are changing or that are being impacted by the block under the while loop.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。