Time
2 hours 57 minutes
Difficulty
Beginner
CEU/CPE
3

Video Transcription

00:00
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
00:15
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.
00:34
So loops are a fundamental part of flow control. They are essentially,
00:40
they're one of the things you implement based on Boolean logic, just like you do with it ST.
00:44
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
00:53
from Boolean logic. But loops are a very specific implementation of it
00:57
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.
01:08
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,
01:26
you'll see how programs that are always on our programs that
01:30
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,
01:38
ah, loop that doesn't terminate, and you could see the comic right above me. Bob is our infinite loop specialists.
01:45
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.
02:06
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.
02:23
It wouldn't You wouldn't say. For I enlist. You would use whatever lists variable name is,
02:29
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
02:37
all the prints statement is doing here. We're not gonna talk deep about functions. All the print statement here is doing
02:42
is taking whatever value is given to it and its rating it to the screen.
02:46
So four I enlist print I Well, let's have a look.
02:49
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,
02:59
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
03:09
Is there an item in the list? Of course there is. There are three items at present
03:14
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.
03:34
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.
03:38
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,
03:58
in the list in the array and whatever.
04:00
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.
04:08
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.
04:23
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.
04:34
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,
04:45
then you will continue your execution and you'll end. Then you'll continue the program and end the execution of this loop.
04:51
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.
05:02
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,
05:14
but for I and the Range 100 so basically the numbers between zero and 100 will print I.
05:19
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,
05:30
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
05:49
for us. We don't have to pay any attention to that.
05:53
The four loop is covering.
05:54
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.
06:03
It's important to know that sort of like with the list that we were evaluating in the last slide,
06:09
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.
06:15
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.
06:27
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,
06:39
maybe starting the python lessons anyway
06:42
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.
06:50
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
07:01
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.
07:12
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
07:31
the last number. Which, of course, is going to be 10. In this case, we print 10 times to say, Okay, we're done
07:39
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.
07:51
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
08:07
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.
08:11
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

Intro to Python

This is an introductory course on Python for cyber security, giving students the ability to understand the basics of the language, solve problems with scripts and identify useful Python modules.

Instructed By

Instructor Profile Image
Joe Perry
Senior Technical Instructor at FireEye, Inc
Instructor