WEBVTT

00:00.080 --> 00:01.000
Welcome back.

00:01.040 --> 00:04.280
In this video we're going to learn about the for loops.

00:04.520 --> 00:12.600
A for loop is used for iterating over a sequence that is either a list, a tuple, a dictionary, a

00:12.600 --> 00:14.080
set, or a string.

00:14.440 --> 00:19.440
Use for loops to repeat an action for each item in the sequence.

00:19.720 --> 00:26.880
For example, if we need to print numbers from 1 to 5, five is included.

00:27.080 --> 00:31.560
We use something called range and the for loop.

00:31.560 --> 00:32.840
For I.

00:33.040 --> 00:37.680
The index in range 1 to 6 six is not included.

00:37.880 --> 00:42.960
Range includes one and two, three, four, and five.

00:43.120 --> 00:50.840
For every time go increase I by one and print number I.

00:51.160 --> 00:54.360
So here we are having the formatted string.

00:54.600 --> 01:04.040
We are placing I with its value in each iteration and each iteration it increases I by one.

01:04.200 --> 01:06.800
Let me run the cell and here we go.

01:06.840 --> 01:09.880
Number one, number 2345.

01:09.880 --> 01:11.960
And here is the output.

01:11.960 --> 01:14.960
So this is the for loop in Python.

01:15.240 --> 01:21.400
For I in range 1 to 6, go and print this statement.

01:21.560 --> 01:22.840
Another loop.

01:23.000 --> 01:33.840
Using a list of names we have names list Alice, Bob, Charlie for name for each name in names.

01:33.880 --> 01:38.840
This is the meaning for each name in names.

01:38.880 --> 01:45.840
Go and print hello message and place the name of the current iteration.

01:45.840 --> 01:48.000
Run this cell and here we go.

01:48.120 --> 01:49.320
Hello, Alice.

01:49.320 --> 01:50.440
Hello, Bob.

01:50.440 --> 01:51.880
Hello, Charlie.

01:51.920 --> 01:55.880
Also, we can use the for loop with strings.

01:55.880 --> 02:05.240
So for x in Alice print X, run the cell and here we go a l I c.

02:06.000 --> 02:12.400
Every character inside this string will be printed in a new line.

02:12.400 --> 02:16.560
So here we can iterate over strings.

02:16.800 --> 02:19.560
Even strings are iterable objects.

02:19.560 --> 02:22.320
They contain a sequence of characters.

02:22.360 --> 02:26.640
Okay, so this is the usage of the for loop.

02:26.640 --> 02:35.950
There is something interesting in for loops, we can break the loop and the iterations if we satisfy

02:35.990 --> 02:36.830
a condition.

02:36.830 --> 02:46.390
For example, if I want to stop this, iterate this loop and iterations on Bob, what we need to do

02:46.590 --> 02:48.070
we can use here.

02:48.310 --> 02:57.030
If name equals to Bob, then go and break with break statement.

02:57.030 --> 03:02.990
We can stop the loop before it has looped through all the items.

03:02.990 --> 03:05.910
Let me run and you can see we are not.

03:06.190 --> 03:10.790
We are not executing all the iterations.

03:10.830 --> 03:12.230
Alice, Bob, Charlie.

03:12.470 --> 03:21.910
When the loop gets into Bob and reaches Bob, it break and stops the iteration.

03:21.950 --> 03:31.390
Okay, so this is the power of using break, and we can use it in certain conditions if they are satisfied

03:31.510 --> 03:34.230
in many ways and in many examples.

03:34.270 --> 03:34.790
Okay.

03:34.830 --> 03:43.430
So this is a very important thing in order to control and stop the loop if you reach a specific condition.
