1.5 For Loops - IP

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *

Already have an account? Sign In »

2 hours 57 minutes
Video Transcription
Hello, everyone. And welcome back to enter the python your own side, Mary on demand. I'm your instructor, Joe Perry. And this is the unfortunately pun based lesson. Lesson 44 loops. Those of you who just came from less than three will be aware that I had the
depressing realization that I had unintentionally written a pun into my lessons, and that's okay, but that's that's what happens. And I guess we just have to live with that now. So in less than four, we're going to understand the concept of looping what it actually is, what its for what it does, how we use it, we're gonna learn. And we're gonna implement a specific type of loop called a four loop.
So loops are a fundamental part of flow control. They are essentially,
they're one of the things you implement based on Boolean logic, just like you do with it ST.
Now all of programming is I've said before in all of computer science, and all of it will not open. A huge chunk of math is in some way derived or derive a ble from
from Boolean logic. But loops are a very specific implementation of it
What they do is they take a piece of code. They take some sequence of actions what we would often call an algorithm or a set of rules. And they repeat that sequence of actions until some condition is met.
So they may apply logic to every member of a cent. If you have a list of items that might apply logic to all of that, they might count to a specified index. They might just count up by one number each time or by multiple numbers, depending on how you structure it or they may cycle through some Siri's of options loops could be used to implement all sorts of things in the next list. When we talk about while loops,
you'll see how programs that are always on our programs that
last forever are executed. A lot of those will make use of wild loops to perform sort of what we call an infinite loop or basically,
ah, loop that doesn't terminate, and you could see the comic right above me. Bob is our infinite loop specialists.
Generally speaking, when you're creating a loop, you're going to give some initial value help. Set the condition we'll talk about that a little bit more in the next lesson as well. Four loops and python. You actually can get away with not using initial values pretty often just because of the way python it's structured. It will often fill in your filling your special variable for your filling your conditional for you.
So four loops an example here for all of the items in this list. Remember, I said we could apply logic to every member of a set for all of the items in this list. The number one, the letter, lower case B and the number 150. Print that item. And below that, you see pretty pretty close to what you would see in Python.
It wouldn't You wouldn't say. For I enlist. You would use whatever lists variable name is,
and you couldn't use list is variable name for reasons that will discuss later. But this is roughly python IX afore. I enlist print
all the prints statement is doing here. We're not gonna talk deep about functions. All the print statement here is doing
is taking whatever value is given to it and its rating it to the screen.
So four I enlist print I Well, let's have a look.
Is there an item in the list that is the conditional upon which this is actually being evaluated? So the four I enlist this four loop is an implementation saying,
Is there an item in the list? If yes, retrieved that, that's where our if comes in. That's where the Boolean Logic evaluation the eggs. The execution decision based upon internal logic, comes from
Is there an item in the list? Of course there is. There are three items at present
print that item. In this case, the first Adam is gonna be the number one. Generally speaking, lists are evaluated from left to right as they're written. That's largely it's just a programming standard. It's sort of the way generally implements. However, lists are not inherently sorted, so sometimes you'll want to control the way it interprets your list. But generally speaking, it's gonna be left to right as it is written.
Is there another item in the list? So we've printed our first item. We've retrieved it, we've used it, we're done with it.
So now we're going to go to the next item on the list. We're gonna increment. What we call the index, So we started at item zero. Now we're going to go to the item that is indexed at one in the same way that you would use, for example, an index card catalogue system. It all the indexes and you'll see indexes referenced often all in indexes is a number that identifies the location in the set,
in the list in the array and whatever.
So is there another item on the list? We increment our index and we see that Yes, of course there is. In this case, it is the lower case Letter B.
And then for 1/3 item, we say, OK, is there another item on the list? Sure enough, there is the number 150 so we print that. Now we reach a point. It's a little bit trickier because we've run out of items on this list. There's nothing left in the list of print. There's nothing left to retreat.
So is there another item on the list evaluates to false and your four loop will terminate execution. So the condition here is actually an inverted condition. We talked about logical inversion in earlier lesson about Willie and logic.
What's happening here is a logical inversion. It's saying, so long as there are items in the list, keep printing when they're when it becomes false, when there are no more items left to print,
then you will continue your execution and you'll end. Then you'll continue the program and end the execution of this loop.
That's just important. Understand? Again, all that's happening here is a is a piece of 1,000,000,000 logic being evaluated every time this loop is run until such time is, it evaluates to false, and then the loop terminates.
So here's another example for I where eyes each number between zero and 100 Print I. This is also similar to Python. They're not exactly the same for reasons we'll discuss when we talk about range in a later video,
but for I and the Range 100 so basically the numbers between zero and 100 will print I.
So we're gonna start out Python. We don't have to set this value. Python is going to print that value for us or not. Print is going to assign that value for us, So I was going to be set to zero at the beginning of this loop,
the logic will be evaluated. This is our case of Boolean logic. Is I less than or equal to 100? Of course. In this case it is because zero is substantially smaller than 100. So evaluates the truth and we perform the logic underneath it. We print I and then the increment. I Now, the way this four loop is designed, the way it's constructed, it will do all of the incantation
for us. We don't have to pay any attention to that.
The four loop is covering.
So we see that eye has one which, of course, is less than 100. We continue our evaluation. This is gonna happen for another 100 loops until finally we reach I equals 101.
It's important to know that sort of like with the list that we were evaluating in the last slide,
it didn't actually magically know. Okay, that was all of the items I'm done. It had to first get to a condition where the evaluation turned out to be false.
In this case, we see that 101 is still greater than 100. Therefore, it is going to evaluate defaults, and therefore we're going to end the execution of our four loop.
So here's a knowledge check. I'm gonna let you try and do this one at home four eye and arrange 10 again. Evaluate that the same way we did with range 100. If you already know Python, you'll know why that's a little bit off. Don't worry about it. Also, if you already know Python,
maybe starting the python lessons anyway
for I and Range 10. Print I times, too. So go ahead and positive video and take a couple of seconds to give it a shot.
And you're either back because you figured it out or you're back because you gave up or you just watched me sit very still for a second as my dog stored in the background. Either way, we're now going to look at what happened. So
I is going to start out equaling zero, of course, and therefore we're going to perform our operation against the number zero print I times, too. Of course. Zero times two is zero.
Then we're going to move on to one now. One times two obviously is, too. So we're going to print that here were kind of combining the two lit. We're combining the two concepts we talked about earlier of performing a piece of logic to every item in a set and incrementally a number. So as we implement this number, we're gonna multiply it by two each time until finally we get to
the last number. Which, of course, is going to be 10. In this case, we print 10 times to say, Okay, we're done
and then it's going to evaluate under the hood. This is all happening in Python. You're not seeing any of it under the hood is going to value and say, Oh, the next number 11 is not actually a valid case for this. The conditional is false and we're done executed.
That's all there is to four loops. That's the lesson in this lesson. Again, we talked about the concept of looping. We talked about what looping is used for, and we examined a few different implementations of a four loop, and our next lesson. When we come back, we'll talk about another type of Luke called a while. Luke
and I mentioned that one a few times in this lesson when you come back. We're gonna spend a little bit of time really digging into it.
I want to thank you all for being a part of this course. I want to thank you for watching. And of course, I look forward to having you back for our next lesson. As always, I'm your instructor, Joe Perry. And thank you for watching this on Cyber Eri on demand.
Up Next