WEBVTT

00:00.000 --> 00:13.640
Hi everyone, in this video we are going to learn about the for loop, so the for loop can

00:13.640 --> 00:18.920
be used to iterate any sequence like string, list, tuple, etc.

00:18.920 --> 00:23.560
This index is the keyword for followed by the iterating variable, followed by the keyword

00:23.560 --> 00:27.320
in, followed by the sequence name, and finally a colon.

00:27.320 --> 00:32.740
The sequence can be a string, or a list, or a tuple, or a dictionary, etc.

00:32.740 --> 00:36.000
And below, we can write the statements properly intended.

00:36.000 --> 00:41.360
So first, let us see the case of a string, so how do we iterate a string using the for loop?

00:41.360 --> 00:47.200
We have four iterating variable, char, in string python and a colon.

00:47.200 --> 00:49.840
And the statement inside is print char.

00:49.840 --> 00:53.240
So for each character in the string, it will be printed.

00:53.240 --> 00:58.360
That is, in each iteration, a character is stored in variable char and is printed.

00:58.360 --> 01:06.840
So first, p will be printed, then y will be printed, then t, then h, and then o.

01:06.840 --> 01:09.200
And finally, n is printed.

01:09.200 --> 01:11.240
So we iterated a string here.

01:11.240 --> 01:13.840
Now let us see the case of a list.

01:13.840 --> 01:17.120
We have a list here with items 10, 20 and 30.

01:17.120 --> 01:19.360
We use a for loop to iterate it.

01:19.360 --> 01:20.840
So write.

01:20.840 --> 01:26.360
For the variable x, in list, which is the name of the list here and a colon.

01:26.360 --> 01:28.920
And next, we have the statement print x.

01:28.920 --> 01:33.560
So in each iteration, the item is taken in variable x and is printed.

01:33.560 --> 01:36.480
So in first iteration, 10 is printed.

01:36.480 --> 01:39.520
Next 20 is printed and finally 30 printed.

01:39.520 --> 01:40.840
So that's all.

01:40.840 --> 01:42.840
So we learned about the for loop.

01:42.840 --> 01:45.280
And it is used for traversing a sequence.

01:45.280 --> 01:48.440
In the next video, we will discuss about the range function in Python.

