WEBVTT

00:00.680 --> 00:03.040
Now that you know how to use while loop.

00:03.160 --> 00:06.920
Okay, now let's go see how does for loop work.

00:06.960 --> 00:11.080
Okay, so let's create a new file here.

00:11.120 --> 00:13.720
Name it for dot pi.

00:14.200 --> 00:18.960
And here I am going to create a simple example that you could learn.

00:19.000 --> 00:19.760
Okay.

00:19.800 --> 00:30.480
A for loop is used for iterating over a sequence that is either a list tuple a dictionary a set, or

00:30.520 --> 00:31.520
even a string.

00:31.560 --> 00:31.960
Okay.

00:32.400 --> 00:33.440
So very easy.

00:33.720 --> 00:34.600
Now let's go.

00:35.240 --> 00:38.080
We have seen a lot of examples about for loop.

00:38.080 --> 00:45.040
Now let's go and see how we can use for loop to iterate through a string.

00:45.080 --> 00:45.840
Okay.

00:45.880 --> 00:52.880
Let's use for x in for example we have banana.

00:54.040 --> 00:56.840
And here let's print x.

00:57.720 --> 00:58.560
Very easy.

01:00.480 --> 01:03.280
Let me use Python.

01:03.280 --> 01:06.440
And then we have for dot pi hit enter.

01:06.480 --> 01:12.360
Now you see we are printing all and everything which is inside here.

01:12.360 --> 01:13.490
And that is banana.

01:13.490 --> 01:16.850
We are printing each letter here.

01:16.890 --> 01:17.250
Okay.

01:17.810 --> 01:19.850
This is one thing that we could use.

01:20.330 --> 01:22.050
And also it has a lot of usage.

01:22.090 --> 01:22.330
Okay.

01:22.370 --> 01:31.250
This for loop for example we are going to search for something inside a list or inside a dictionary,

01:31.250 --> 01:32.410
inside a tuple.

01:32.450 --> 01:33.010
Anything.

01:33.050 --> 01:35.530
See it or something else.

01:35.570 --> 01:36.290
Okay.

01:36.330 --> 01:42.410
Let's go with uh simple search program okay.

01:43.170 --> 01:45.250
Let's use again this fluids.

01:45.370 --> 01:46.330
It is equal to.

01:46.370 --> 01:51.410
For example uh we have apple.

01:52.730 --> 01:54.130
We have banana.

01:55.970 --> 01:59.090
We have Jerry.

01:59.130 --> 01:59.570
Okay.

02:00.010 --> 02:03.570
Let's just imagine we have this three part okay here.

02:03.570 --> 02:09.570
And I am trying to search for for example for Jerry.

02:09.610 --> 02:16.730
If this cherry is inside here, if it is inside then we can print it.

02:16.770 --> 02:17.450
If not.

02:17.450 --> 02:20.730
So do not print it okay.

02:20.730 --> 02:24.690
So let's use for x in Roids.

02:26.530 --> 02:30.170
And here we can use a if condition.

02:30.490 --> 02:30.890
If.

02:33.210 --> 02:37.170
X is equal to for example banana.

02:37.290 --> 02:37.690
Okay.

02:38.490 --> 02:46.970
If it is equals to banana then print x for me and then use break statement.

02:47.010 --> 02:47.850
Very easy.

02:47.890 --> 02:52.930
Now we have a simple search engine that is searching banana inside the fruit.

02:53.090 --> 02:53.530
Okay.

02:54.130 --> 03:05.130
And I also can do something at a text here like the banana is here.

03:07.290 --> 03:08.770
And use concatenation.

03:08.810 --> 03:09.450
Save it.

03:10.330 --> 03:11.090
Come here.

03:11.090 --> 03:15.410
Use clear and then run this.

03:15.650 --> 03:16.850
You see the banana is here.

03:16.850 --> 03:17.530
And it is.

03:18.130 --> 03:19.730
Printing the banana for me.

03:20.010 --> 03:22.050
What if I search for something else?

03:22.050 --> 03:24.210
For example, a phone or something?

03:24.250 --> 03:25.090
Okay.

03:25.090 --> 03:27.730
And now it will do nothing for me.

03:28.250 --> 03:29.050
You see that?

03:29.050 --> 03:29.530
Okay.

03:29.690 --> 03:32.010
Also, we can do one more thing.

03:32.010 --> 03:33.970
And that is the else part.

03:34.010 --> 03:34.450
Okay.

03:35.690 --> 03:37.340
Else print.

03:37.820 --> 03:39.860
For example, the banana.

03:41.420 --> 03:45.220
Banana is not in the list.

03:49.580 --> 03:56.260
Now, if I execute this, you see that the banana is not in the list and why it is printing this three

03:56.260 --> 03:56.620
times.

03:56.620 --> 04:00.660
That is because we have three items in the list.

04:00.660 --> 04:03.940
So for each item it is printing that for me.

04:04.860 --> 04:05.580
And.

04:07.660 --> 04:10.460
So what if I.

04:10.460 --> 04:11.460
Okay that is it.

04:11.900 --> 04:21.260
How we can use a for loop to go through uh, a list uh string anything.

04:21.300 --> 04:22.140
Okay.

04:22.180 --> 04:27.820
Dictionary or even a tuple and set and a lot of things.

04:27.860 --> 04:28.220
Okay.

04:28.260 --> 04:36.380
It's very simple example that you learn how to create your own, uh, searching machine inside a list.

04:36.380 --> 04:43.700
Or it could be a string or even it could be a very big list of, for example, names or anything.

04:43.860 --> 04:44.460
Okay.

04:44.500 --> 04:48.460
So let's go and see some other thing.
