Now that you know how to use while loop.
Okay, now let's go see how does for loop work.
Okay, so let's create a new file here.
Name it for dot pi.
And here I am going to create a simple example that you could learn.
Okay.
A for loop is used for iterating over a sequence that is either a list tuple a dictionary a set, or
even a string.
Okay.
So very easy.
Now let's go.
We have seen a lot of examples about for loop.
Now let's go and see how we can use for loop to iterate through a string.
Okay.
Let's use for x in for example we have banana.
And here let's print x.
Very easy.
Let me use Python.
And then we have for dot pi hit enter.
Now you see we are printing all and everything which is inside here.
And that is banana.
We are printing each letter here.
Okay.
This is one thing that we could use.
And also it has a lot of usage.
Okay.
This for loop for example we are going to search for something inside a list or inside a dictionary,
inside a tuple.
Anything.
See it or something else.
Okay.
Let's go with uh simple search program okay.
Let's use again this fluids.
It is equal to.
For example uh we have apple.
We have banana.
We have Jerry.
Okay.
Let's just imagine we have this three part okay here.
And I am trying to search for for example for Jerry.
If this cherry is inside here, if it is inside then we can print it.
If not.
So do not print it okay.
So let's use for x in Roids.
And here we can use a if condition.
If.
X is equal to for example banana.
Okay.
If it is equals to banana then print x for me and then use break statement.
Very easy.
Now we have a simple search engine that is searching banana inside the fruit.
Okay.
And I also can do something at a text here like the banana is here.
And use concatenation.
Save it.
Come here.
Use clear and then run this.
You see the banana is here.
And it is.
Printing the banana for me.
What if I search for something else?
For example, a phone or something?
Okay.
And now it will do nothing for me.
You see that?
Okay.
Also, we can do one more thing.
And that is the else part.
Okay.
Else print.
For example, the banana.
Banana is not in the list.
Now, if I execute this, you see that the banana is not in the list and why it is printing this three
times.
That is because we have three items in the list.
So for each item it is printing that for me.
And.
So what if I.
Okay that is it.
How we can use a for loop to go through uh, a list uh string anything.
Okay.
Dictionary or even a tuple and set and a lot of things.
Okay.
It's very simple example that you learn how to create your own, uh, searching machine inside a list.
Or it could be a string or even it could be a very big list of, for example, names or anything.
Okay.
So let's go and see some other thing.