Hook
I cannot have you out here hitting licks and you don't know how a for loop works. But I'm going to get you right because you got to know both these ways on how to write a for loop. So we got a list of houses and we want to print the ones that we're going to rob. Trying to hit multiple licks, so we're going to use a for loop. So if we do for house in houses, then we print house, it's going to print all the houses in the list. But twin, we can't go around robbing everybody, so let's be a little selective. Let's try only robbing a house if it's an even number. And you already know we got to use an if statement inside the for loop. So we're going to do if house modulus 2 is equal to 0, and all this is saying is, is this number divisible by 2? And if it is, we're going to print it. But there's also another for loop where we can loop by index. For this one we do for i in range len houses. And look what happens when we print i. It just prints the index numbers. So we have to add our list name houses in front of that, then it's going to print out all the houses. And if we decide we want to get a little bit selective here, and if we do if i modulus 2 is equal to 0, this actually doesn't return specifically the even numbers in the list. It's going to print whatever numbers are at the even indexes. But if we want to do like earlier and actually print the even houses, all we got to do is add houses, which is our list name, in the if statement.
More breakout videos from this creator.