课程: Python Essential Training

For

- [Instructor] I love the syntax of the for loop in Python. My list equals 1, 2, 3, 4. For item in myList, print item. For item in my list reads like plain English. It lets you declare a new variable to hold the value of each element as you loop through it in Python. It's short and to the point and I'm not alone in my love of Python's for loop. In fact, it's the most common loop you'll use and see others use as you program in Python. So like the for loop, I'm going to keep this video short and to the point. All the statements we covered in the video on while loops, you can use in the for loop as well. So if we want to write a stub for a for loop and maybe come back and fill it in later, we can use pass. for letter, animals. That's going to be our animal list, in animalLookup.items, pass. We can also skip the rest of the loop during each iteration if we want, using the continue statement. So if length of animals is greater than one, continue. Then outside of our if block, print Only one animal. And let's print out the animals here. We can loop through and stop partway if we found what we're looking for using the break statement. So pasting our code over here and then under here, print Found length animals. We're just going to count the number of animals in our list. Animals. And then break. Note that if there are more instances of two or more animals in our list, it will just stop after the first instance because of the break statement. Now, there is one really cool thing that we haven't covered yet, and that is the break else statement or the for else statement. And one of my favorite examples to demonstrate this with is finding prime numbers, something computers do all the time for cryptography and security. And we can do it too in just a few lines of Python. So we're going to find all of the primes between two and 104. for number in range(2, 100). So number is the number that we're going to be testing for primality. And then we need to loop through all the potential factors of that number to test each one and see if it's divisible by that factor. So for factor in range(2, int(number int number**0.5) + 1). We only need to go up to the square root of the number or that number raised to the half power to test to see if it's prime. If we haven't found a factor by the square root of the number by the time we haven't reached the square root, then we know that it's prime. So inside here, I'm going to test each factor. If number % factor == 0, break. Okay, so we can use a modulus to see if it's evenly divisible. And if it is evenly divisible, we know that the number isn't prime because it has a factor. So at that point, we just break and then we add our else statement. So I'm going to add it, oops, in line with this inner for loop right there. So we have the break that breaks out of this for loop and then we have an else statement. And under here, we say print number is prime. All right, so this else statement will only be entered if a break didn't happen in the previous loop. So if our number had a factor, this break happens and we skip the else. Else, we didn't find any factors, the break didn't occur and it's prime. So let's check this out. Yeah, those all look prime to me. Note that this break-else pattern can also be used with while loops. If you put a break in your while loop, you can add an else statement at the end of it immediately after that loop. And that else only happens if the break wasn't thrown. So very often, you'll see code written by Python programmers less knowledgeable than yourself that look like this, with an extra variable. So for number in range, two through 100, found_factors = False. For factor in range(2, int(number**0.5) + 1), if number % factor == 0, found_factors = True. And then we break. And then outside here, if not found_factors, print number is prime. Okay. And if we run this, we get the exact same output. And actually, these are two equivalent pieces of code. We're just using this found_factors to keep track of whether or not a break happened. And instead of having an else statement there, we have an if not found_factors, but they do basically the same thing. But this down here is obviously a lot more code. It's messy, you have an extra check. Understanding and using pass, continue, break, and else will help keep your loops clean, elegant, and as we say, Pythonic.

内容