And welcome back to another class of course about Python.
So in today's class our main topic would be for loops and you will see it's not that hard to understand.
It's when you practice it you'll see it's gonna be very easy.
All right.
So basically what are Froot Loops for loops are technically a loop that is used for iterating over a
sequence.
So the sequence could be a dictionary could be a list could be a string could be numbers could be a
set of numbers could be trouble could be all of those together.
So basically what it does it will execute the set this statement sorry and the ones for each item in
the list topple.
It could be numbers it could be dictionaries or whatever.
So what what does this all mean.
Very simple let's say for example you have a list of I don't know of things.
Well if when you're going to make your for loop when you for example you will use the for loop what's
going to happen is that that each element will be printed.
So let's have an example here.
Let's use for example.
I don't know colors.
So in this case we will have a variable that is will a list that will be named colors.
And in this case we will create our list.
So let's say we'll have blue red
orange
and black.
All right.
So we have a list of far forth colors right now.
And let's say you want to use our for loop.
So how exactly this for loop works.
So very simple.
Write down for.
So this will be our for loop then let's say for example for one color this case since we're talking
about colors we'll say for one color in
colors.
In this case colors is the name of our list
two points.
Print one color.
Great.
So if we run up as you can see your eyes each color will be printed.
This thing right here which is one colors can be changed so it could be just X and if I run the app
as you can see it will still work.
So it's very very simple to use again.
So basically what exactly does loops.
What exactly does.
It simply prints every element let's say off a list of a string of a number of absolutely well anything.
So let's try it just on a string to show you guys what exactly it is.
So let's say for example we have hour another time a color.
So let's say this time will say for letter since we are going to print the letters in.
In this case let's say the word blue
two points.
Print letter because we are printing letters once again could be just X and it will work.
So when we run our up as you can see blue is printed this way.
So we ask Python to print each letter of the string.
So in this case we have a string.
So we ask Python to print every letter of this string.
All right.
And there are a thing in which you can use the for loop while the loop statement will be for example
with the British statement.
So in this case how we can run all this.
Well it's very simple.
Let's say for example we once again have a set of colors.
So let's name it color colors.
In this case we write on equal and we will put our colors here.
So let's say we have black and
yeah.
And
green.
And finally the last one would be red.
All right.
We have our four colors and right now what we can use also let's say we want to stub the eighth iteration
at Green.
So everything will stop at the green so will only generate black yellow and green.
In this case so very simple.
The way it's gonna be written down.
We will use our for loop of course.
So for example in this case it's colors.
So let's name it.
Let's say for one color.
But it could be just X or Y or any letter in colors.
So in this list right here
print one color
all right.
So what.
All those were what all this means.
So very simple for one color.
So only one color in colors in this list print one color.
So what's gonna happen.
It's gonna print each color one after the other.
So if we run this up as so you have black yellow green the red.
But if we want to stop our iteration at Green for example it's very simple we can use an if statement
and simply write down one color if one color is equal to.
In this case green we want the loop to stop.
So a simple two points and write down brick.
So in this case we'll use the break statement and if we run our app as you can see we'll have black
yellow and green.
Everything is going to stop at Green.
So once again this is just by using the well the how you call it the if statement of the bridge statement
sorry.
So if you use the brake statement it's going to all stop again.
Let's again see the loop.
Well the for loop is something that is not hard the very very hard to understand and it's something
that is very simple.
The main goal of the for loop is simply to print everything that can be in a list in a in a string in
numbers.
Well absolutely anywhere.
So let's have another example.
So in this case we'll use another.
Well we'll talk about another statement which is the range statement.
And in this case let's say we have four.
Let's use let's let's call it number four.
Number in range six
print number.
All right if we run this up as you can see it's gonna print all the numbers from 0 to 6 but it's not
going to print the number 6.
And if for example we want to print just numbers that are in let's say from one number to another.
So in this case let's say want to print out the numbers between them.
I don't know it could be 20 and 30 for example you can just write it this way.
And from our app as you can see it's going to put on all the numbers that are between 20 and 30 but
just not the 30 that you can see.
It's not something that is very very hard to understand.
And those are simply the basics of the for loop.
So the way that you can use it can be way more complex.
But in this case this is just this simple the simple part of the fault.
So as you can see guys as I explained to the for loop the main reason why the for loop exists is really
to help you print all the elements of let's say you could be at least it could be a dictionary.
It could be a string could be numbers in range of numbers.
It could be well absolutely anything.
So right now you understand how it works.
So that's it for this class guys.
And see you all in our next class.