Hello to all of my students, and today we will kick off with a new topic, which is Loop's, we will
see what our loop's see, some definitions and theory, and also we will see how can we apply Loop's
in Python programming language.
So, first of all, let's discuss what our four lupe's actually.
What do they mean?
All right.
So in computer science, a loop is a control flow statement for specified specifying iteration.
Right.
What iteration means is basically that we want to apply the same action again and again and again.
And we don't want to print it.
We don't want to write it separately.
If we have the same action to be applied, we can do it in one or two lines instead of doing it in hundreds
of lines, for example, if it's the same action.
So a loop is used for executing a block of statements repeatedly until a particular condition is satisfied.
So you probably recall this sentence for from when we studied if statements, we talked about condition
satisfaction there.
If something, then do something and else do something else.
So airconditioned satisfaction is actually belongs to if statements and exactly that.
I said what I said in previous lectures.
You will see as long as we go further with the course that all topics come together and they are used
together as a machine and especially in for loops you will see that we can use for loops and other loops
as well.
You will see how can we use a loop combined with an IF statement and combined with collections, for
example, they work perfectly together.
They were made for each other.
This way we can make our code efficient and, you know, just not using spaghetti code.
Spaghetti code is a term in programming.
Once you write a lot of code, which you can write in a lot smaller amount of code.
All right.
So this is spaghetti code and we don't want to write spaghetti code.
We want to write efficient short code as possible, because later on, as you build your projects in
your career, as your code is shorter, it makes little it makes less mess to understand it for the
wrong.
All right.
So, for example, let's assume we have a list collection that contains numbers and we want to multiply
each cell value by five and add up until now, we have the tools to do it, but we had the tools to
do it in a primitive way.
And actually it's the wrong way, because once you for example, once you have a collection of lists
list collection and you have three items inside one, two and three integer, very values, very simple.
And I want to multiply each of these values by five.
I basically mention the collection and the list and I want to do a certain action upon cell index zero
and then I have the equals sign.
So whatever comes after this to the right of what I just marked will insert will be inserted to sell
index zero.
So for example, if I write Houtan afterwards, if I print Selinda Zero, I will have the value 10 inside
instead of one.
But in our case I'm just thinking the cell and multiplying what's inside by five.
So after this action will be executed, if I print sell index zero, I will have the value five in here.
Same thing goes for here.
Selling the X1 multiplied by five and sound index two multiplied by five.
So basically we have three lines here for three separate actions.
Once you can you can see here that all of them are just multiplied by five.
Again and again.
Now let's see the efficient way to do it.
Don't get scared.
If you see now, you will see no for loops, probably for the first time.
Or maybe you have encountered full in other programming languages or in some articles.
But right after we see the quick example here, we will discuss thoroughly Waterfall Loops and how to
use them and what what each what each word here means.
All right.
So Option B, which is the right way.
You can see here a list.
All right.
For now is the same list.
And here we will see the keyword for which indicates that we are using a for loop.
All right.
And whatever comes right after is the items which will be iterated inside the for loop.
So before talking about cell and in will just.
Look at this thing you see here, numbers, and you see that this is the name of the list, so remember
that whatever comes after the in, whatever comes after the in is the whole package, the whole package
that we will be focused on inside the four.
Look, this is the whole package.
And whatever comes after the four is the single item inside the whole package.
All right.
So in our case, we have here the list, which is the whole package.
We have three items here.
All right.
This is the whole package.
It comes after the Æon.
And what comes after the four is the single item.
So in our case, cell indicates the cell value.
All right.
Cell indicates the cell value.
So you can see here that I'm taking the cell value and I'm taking its existing value and multiplying
it by five.
And it happens again and again and again until the whole items here are iterated.
All right.
So basically, we're taking the same action that we did right here, but we're doing it in two lines
instead of three, or even though if you even if you have here, for example, four and five and six.
So you probably can guess that these lines would grow exponentially.
All right.
Because as we go here, we have zero cell cells, Europe cell one cell to sell three or four and zero
five.
So this thing is going to grow.
And as as we have much more items inside the list, this thing is going to grow.
And it's not good for us because we're going to have spaghetti code here.
And for example, if we have one hundred items here, we're going to have one hundred one hundred lines
here.
And it's not good for us.
But in the follow up case, if we're having here a lot of items, it doesn't matter.
This code stays the same each time that we take the cell and we multiply the cell value on five.
All right.
So it stays two lines.
You see how efficient this is.
All right.
This is the way to use for loops.
And from here, once we've seen the quick example, let's go over and talk about more specifically regarding
for loops, because there are several types of loops.
Let's start off with a for loop, which is the most popular one.
So what these are for loop in programming A for loop is used to iterate several times upon certain items.
It doesn't matter if it's variables, list cells and so on and applying actions in each iteration.
So each time the loop will run in each iteration, we will have a certain action taken.
All right.
At this point we will not discuss if statements inside for loops because it's a bit more advanced.
I think that we'll discuss it in the next lecture on maybe the lecture after.
All right.
So as we saw in the previous example, we can take a collection and apply actions on its items.
So full structure looks like this, right?
You can see here the example from above.
So first of all, you can see the list here, numbers B, one, two, three.
So and here the explanation for loop will be A for loop will extract one cell on each iteration.
All right.
So if the goal here down, you can see the for loop and you can see the word four and then cell.
So the word that comes after four is one cell.
All right.
Choose any words you want.
You can switch this word with box, with item, whatever you want.
It doesn't matter because the cell, the word here, and you can replace it with any word and it indicates
of a single item out of what comes after in.
All right.
So whatever comes after in is the whole package.
And whatever comes after four is a single item out of the whole package.
This is really simple explanation and I think it's really easy to understand as well.
So from here, let's see a live example of the theory that we have just discussed and let's see what's
going on up here.
All right.
So I have the example that we just discussed, option A and option B, the wrong way and the right way.
So this will be the wrong way, this bulk of code, and this will be the right way, this block of code.
All right.
By the way, just just for you to know that if you put quotation marks and inside it, you will put
it and you see it's marked in.
Orange same is four and the same as in it is a saved key, and it means like go one line below an empty
line, go one empty line below.
All right.
Because I want to separate this gold and this gold once they print it out.
So at the top here, you can see the wrong example.
I just declared here a list with three items, same as we discussed.
And I'm just going to take cell zero, sell one and sell two and multiply all of them by five.
And afterwards, I would like to print the output.
Right.
So just going to comment this out, for example, for a second like that.
And let's just run it.
All right, so as you can see, the initial values here are one, two, three, and once I multiply
it by five and the expected values of print right here, print one first print second and third print
are five, 10 and 15.
Basically, these values right here.
Well, multiply by five.
All right.
So this is the way that we've done it until this far.
But right here below declared I you can see the right example.
First of all, I declared a second list, which is called numbers B.
Also, I've set here one to three items inside each.
So you can see here, one, two, three.
And right after I'm going to use the for loop to iterate the same action I did here.
Multiply more motivation by five and I'll just take the four.
You can see it marked in orange, which is a saved keyword.
Same as the comma here.
All right.
So four and then I have after the in.
All right.
As we discussed after the end, I had the whole package and you can see one side double tappet.
The numbers B list is colored as well with a brownish background.
So it indicates the in here what comes after the N gets the value of this list.
So this is the whole package.
And what comes after the four is the single item.
In our case, it's the sale value.
All right.
Right after inside the for loop, once we're doing an indentation, you can see here, I'm thinking
taking the cell value and multiplying it by five and right after, I'm just going to print the value
after it's been multiplied by five.
All right.
So once I run it, all right, you can see here rightway print out.
This is this thing.
And right after I also multiply each sale by five.
So what's happened here?
The for loop, the code, the code, it goes like this we printed right now and then we go to the for
loop.
All right.
So then the for loop took the first item out of this list right here.
It took the first item and multiply it by five and printed the output and then it went again.
All right.
From here, took the second cell out of this whole package, as we mentioned.
And he took this item right here.
The second cell also multiplied by five print out the output.
And then he got to the last cell, which is value three, and also multiply it by five.
So basically what's going on here is the full loop will take all the items one by one in a loop.
All right.
This is why it's called the loop is going to go around and around again and again until each of these
cells separately will be executed.
All right.
And each of these cells, each of these single items would have this action applied on them.
And then after we go to the print, that one after it will be executed.
All right.
So as you can see, there is only three items here.
So you can say to yourself, what's the difference if I do it?
This way or this way, so we have three lines here.
We have two lines here, maybe three lines here as well.
So what's the difference is the same thing.
But what if I'm having here like a million numbers, right?
Maybe not a million, maybe just 15.
So what happens then?
So if I run it, you can see easily that the values are here multiplied by five automatically.
And I don't need to add any lines.
But on the other hand, if I'm going to add values here.
All right.
In this list.
So each item that I will add here, I will have to add additional additional a line right here, red
cell number three and additional line here so you can see it growing and growing without any control
endlessly.
So if I have like 100 items right here, I have 200 lines right here and it's not efficient.
It's going to be long and spaghetti and you will not see the end of it while here using the follow up.
It's going to stay the same length.
It's going to stay three lines forever.
All right.
So again, what comes after the end is the whole package and what comes after the four is the single
item out of the whole package.
So I think this concludes our introduction video regarding loops.
You can also use the attachment PDF file to this lecture and to use this this slide to read it again.
I think it sums up the introduction pretty well.
I will see you in the next video with deeper explanations regarding for loops and right after we go
over the other loops as well.
I'll see you in the next video.
Thank you and bye bye.