WEBVTT

00:00.140 --> 00:00.830
Hello guys.

00:00.830 --> 00:03.050
We are going to continue the discussion with respect to Python.

00:03.050 --> 00:05.600
And in this video we are going to talk about loops.

00:05.600 --> 00:10.430
Already in our previous video we have uh, we have seen a lot of examples with respect to conditional

00:10.430 --> 00:13.040
statements like if else, if else.

00:13.040 --> 00:19.130
And we have seen multiple examples out there right now in loops, we will be discussing about what are

00:19.130 --> 00:24.860
the different types of loop we usually use in Python programming language, like for loop while loop

00:25.100 --> 00:27.680
loop control statements like break, continue and pass.

00:27.680 --> 00:32.180
Then we'll be discussing about nested loops, and then we'll be seeing like all these things, we will

00:32.210 --> 00:35.660
be seeing much more in with the help of practical examples.

00:35.810 --> 00:39.920
And what are the common errors we usually face and how to avoid those errors.

00:39.920 --> 00:42.320
So all these things we'll be discussing in this particular video.

00:42.320 --> 00:48.620
So first of all let's go ahead and work with for loop with a simple, uh, you know, syntax.

00:48.620 --> 00:53.780
So in order to write a for loop right you need to understand what a for loop is.

00:53.810 --> 00:55.640
A for loop is a loop right.

00:55.640 --> 00:58.220
It is used to iterate over a sequence of numbers.

00:58.250 --> 00:58.790
Okay.

00:58.790 --> 01:01.980
So let's say I will give one example like for AI.

01:02.010 --> 01:07.800
Okay, AI is just a temporary variable right now in let's say I want to create a sequence of numbers.

01:07.800 --> 01:12.270
Now, in order to create a sequence of numbers, I will show you with the help of range.

01:12.300 --> 01:12.480
Okay.

01:12.510 --> 01:14.580
So what exactly is range okay.

01:14.610 --> 01:18.270
So let's say in this particular range I give five elements.

01:18.270 --> 01:20.040
I basically want five elements over here.

01:20.040 --> 01:24.360
So this is turn actually help us to generate numbers between 0 to 5.

01:24.390 --> 01:24.720
Right.

01:24.720 --> 01:30.030
So here you can see that hey I'm getting a range between 0 to 5 over here.

01:30.030 --> 01:36.390
And let's say I want to just display all these numbers inside this right range is just giving us a sequence

01:36.390 --> 01:37.800
of numbers between 0 to 5.

01:37.830 --> 01:42.360
But if I want to iterate through this, all the numbers I can basically use for loop.

01:42.360 --> 01:46.560
So if I write for I in range five, right.

01:46.560 --> 01:52.950
And here if I go ahead and print I right now, you will be seeing that after every iteration.

01:52.950 --> 01:58.320
First of all, when we are looping inside this sequence of numbers between 0 to 5, first it will go

01:58.350 --> 01:59.790
ahead and display zero.

01:59.820 --> 02:01.900
Then it will go ahead and display one.

02:01.930 --> 02:06.460
That basically means after every iteration, this number is basically getting incremented and it is

02:06.460 --> 02:08.620
traversing through all this range of numbers.

02:08.650 --> 02:14.170
One important thing that you should understand that since I have used range between 0 to 5, the last

02:14.170 --> 02:15.700
number is not going to get included.

02:15.700 --> 02:20.290
So that is the reason you can see zero is here, one is here, two is here, three is here, and four

02:20.320 --> 02:20.800
is here.

02:20.830 --> 02:21.160
Right.

02:21.190 --> 02:24.760
But the total number of elements is five over here and here.

02:24.910 --> 02:28.570
In short I have spoken about for loop and range as a function.

02:28.600 --> 02:28.810
Okay.

02:28.840 --> 02:30.850
So range as a function is also given.

02:30.850 --> 02:34.690
Let's say I want to probably use for loop with different range of numbers.

02:34.690 --> 02:34.930
Right.

02:34.930 --> 02:40.260
Let's say if I go ahead and write one comma six, that basically means I want the number between 1 to

02:40.260 --> 02:40.510
6.

02:40.510 --> 02:42.010
Six is the last number.

02:42.010 --> 02:47.800
So we are not going to include it because at the end of the day, with respect to Python, whenever

02:47.800 --> 02:50.800
we are seeing indexing right, it always starts with zero, right.

02:50.800 --> 02:53.320
So I'll talk more about it as I go ahead.

02:53.350 --> 02:57.400
Now if I go ahead and execute this here you'll be able to see one is getting displayed, two is getting

02:57.400 --> 02:59.530
displayed, three is getting displayed, four is getting displayed.

02:59.530 --> 03:00.640
Five is getting displayed.

03:00.640 --> 03:08.020
So I can also call all my range of numbers with respect to by giving numbers between one number to the

03:08.020 --> 03:09.460
other number itself, right.

03:09.730 --> 03:13.870
Now let me just give you more examples over here by using range.

03:13.870 --> 03:16.180
So if I go ahead and write range.

03:16.480 --> 03:19.360
Range of one comma ten.

03:19.360 --> 03:23.050
And let me just go ahead and give some third parameter.

03:23.080 --> 03:23.320
Right.

03:23.320 --> 03:27.760
So third parameter over here you'll be able to see that it is something called as step.

03:27.760 --> 03:29.170
Now see see the syntax.

03:29.170 --> 03:32.800
It shows start as the first parameter inside range function.

03:32.800 --> 03:34.510
Then you have the stop parameter.

03:34.510 --> 03:36.490
Then you have the step parameter okay.

03:36.520 --> 03:39.580
By default step parameter is usually given as one.

03:39.610 --> 03:43.870
Now start I'm saying hey start from one end till ten right.

03:43.900 --> 03:49.270
And before ten I will not say till ten, but before ten and use the step size as one.

03:49.270 --> 03:51.790
That basically means only increment one number at a time.

03:51.790 --> 03:56.200
So if I go ahead and execute it, this will basically give me a range of value between 1 to 10.

03:56.200 --> 03:57.970
But and the step size will be one.

03:58.000 --> 04:00.370
Right now let me just go ahead and make it two okay.

04:00.400 --> 04:03.980
So here you can see that I'm starting from one before ten.

04:04.010 --> 04:04.640
It will stop.

04:04.640 --> 04:05.930
And here the range is two.

04:05.960 --> 04:08.690
That basically means the two numbers will get incremented.

04:08.720 --> 04:09.980
Now let me do one thing.

04:10.010 --> 04:10.280
Okay.

04:10.310 --> 04:16.040
Let me just write one over here and let me use a for loop again for I in range of this.

04:16.040 --> 04:18.080
And let me go ahead and print I.

04:18.110 --> 04:18.770
Okay.

04:18.800 --> 04:22.190
Now when I'm printing I you'll be able to see that what I'm actually getting.

04:22.190 --> 04:24.140
123456789.

04:24.170 --> 04:27.350
Now let me make this change from step size 1 to 2.

04:27.380 --> 04:28.160
Now what will happen?

04:28.190 --> 04:33.320
See, after one directly three will get displayed because we are going to skip one number.

04:33.320 --> 04:35.840
So it is just going to jump two steps ahead.

04:35.840 --> 04:36.290
Right.

04:36.320 --> 04:37.400
Now see this.

04:37.640 --> 04:41.330
So now I can see 13579 right.

04:41.330 --> 04:46.400
So I am not probably traversing to each and every number, but instead I have given a step size of two.

04:46.430 --> 04:48.470
So it is jumping two numbers ahead.

04:48.890 --> 04:50.930
And that is how it is basically getting printed.

04:50.960 --> 04:52.730
Let me just show you one more example.

04:52.730 --> 04:59.450
Let's say that I want all the numbers between, um, 10 to 1.

04:59.450 --> 05:03.410
But now this time I will use a step size of minus one.

05:03.410 --> 05:08.630
Now what it is going to do, it is going to display a number between 10 to 1, and step size will be

05:08.630 --> 05:09.050
minus one.

05:09.050 --> 05:12.170
That basically means after ten it will become nine, then eight, then seven.

05:12.200 --> 05:12.800
See this.

05:12.800 --> 05:20.120
So here you can see 910 987654321 is not getting displayed as we know that last number will be skipped.

05:20.120 --> 05:22.100
Before this all the numbers will be displayed.

05:22.100 --> 05:25.010
One more example that we if I really want to show it to you.

05:25.010 --> 05:27.860
So here you can see minus two if I'm using minus two.

05:27.890 --> 05:31.310
So ten 86542642.

05:31.340 --> 05:31.790
Right.

05:31.790 --> 05:34.940
So I hope you have got an idea with respect to for loops.

05:34.940 --> 05:37.730
And with the help of range how we can actually move.

05:37.760 --> 05:38.270
Okay.

05:38.300 --> 05:42.650
Now let me show you with respect to strings I hope everybody has learned strings.

05:42.650 --> 05:43.010
Right.

05:43.010 --> 05:45.770
So strings are how to properly create string.

05:45.770 --> 05:48.110
Let's say that this is my string okay.

05:48.140 --> 05:49.310
This is my string.

05:49.340 --> 05:51.890
And here I am going to write Krish Nayak.

05:51.920 --> 05:52.490
Okay.

05:52.730 --> 05:55.430
Now if I want to traverse to this particular string.

05:55.460 --> 06:00.410
Now see, one amazing thing about this data structure called a string is that we can traverse to each

06:00.410 --> 06:01.710
and every character over here.

06:01.710 --> 06:09.030
So if I go ahead and write for I in str, okay, and uh, for I in str.

06:09.030 --> 06:12.060
And if I just go ahead and print I okay.

06:12.060 --> 06:14.550
So string is basically a data structure.

06:14.550 --> 06:15.360
It is just nothing.

06:15.360 --> 06:17.160
But it is a collection of characters.

06:17.160 --> 06:20.910
Right now if I'm going to print over here, here you can see k r I s h.

06:20.940 --> 06:22.950
Then again space and I k right.

06:22.980 --> 06:27.060
Let's say that I am traversing to this particular string.

06:27.060 --> 06:32.160
And I also want to do something like I may have a sentence, I may have a paragraph.

06:32.160 --> 06:34.590
So based on that also I can print it over here.

06:34.620 --> 06:34.890
Right.

06:34.920 --> 06:40.680
Just to give you an idea what exactly this is, uh, because string is a collection of characters,

06:40.680 --> 06:40.980
right?

06:41.010 --> 06:42.300
It is a collection of characters.

06:42.300 --> 06:43.800
And we can also traverse thing.

06:43.800 --> 06:49.260
So the for loop main functionality is that whenever you have any collection items, like any collection

06:49.260 --> 06:54.180
data structure, you can traverse through it very much easily by just using a for loop.

06:54.210 --> 06:54.630
Right.

06:54.630 --> 06:56.670
So I have just given you an idea about it.

06:56.670 --> 07:01.200
But as we go ahead, we'll be seeing more complicated example, uh, as we go ahead.

07:01.240 --> 07:01.600
Right.

07:01.600 --> 07:05.740
But just to understand the basic example, this is what a for loop is all about.

07:05.770 --> 07:09.310
Now let's go ahead and discuss about something called as while loop okay.

07:09.340 --> 07:11.770
So while loop.

07:11.950 --> 07:15.580
So now let's see something about while.

07:16.180 --> 07:18.220
So now I have a while loop.

07:18.250 --> 07:18.700
Okay.

07:18.730 --> 07:20.950
Now with respect to while loop right.

07:20.950 --> 07:28.900
If I really want to give a small definition, this while loop is nothing, but it continues to execute

07:28.900 --> 07:31.000
as long as the condition is true.

07:31.030 --> 07:31.540
Okay.

07:31.570 --> 07:36.670
That basically means unless and until this while loop, whatever condition I'm writing in while loop,

07:36.850 --> 07:38.980
it is true it is just going to execute it.

07:39.010 --> 07:39.400
Okay.

07:39.430 --> 07:40.570
Now let me do one thing.

07:40.570 --> 07:43.570
Let me create a temporary variable called as count.

07:43.600 --> 07:44.170
Okay.

07:44.710 --> 07:45.310
Count.

07:45.340 --> 07:45.730
Okay.

07:45.760 --> 07:52.150
Now with respect to this particular count, let me just go ahead and write while count is less than

07:52.150 --> 07:52.780
five.

07:54.160 --> 07:54.760
Okay.

07:54.760 --> 07:59.080
And now I have gone inside my while loop while block or while loop over here.

07:59.590 --> 08:05.530
So I'm putting this particular condition, unless until this count is not, is less than five.

08:05.560 --> 08:07.510
Just go ahead and print it.

08:07.540 --> 08:08.050
Okay.

08:08.080 --> 08:10.450
Let's say I'm going to print the count.

08:10.480 --> 08:10.990
Okay.

08:11.020 --> 08:17.080
And after this, if I'm executing C, I also need to increment the count over here.

08:17.080 --> 08:19.480
So I will go ahead and write count plus one.

08:19.570 --> 08:20.110
Okay.

08:20.140 --> 08:24.130
So what I'm doing unless and until this count is less than five I'm going to print the count.

08:24.130 --> 08:28.780
And for every iteration, because we have initially initialized, count is equal to zero.

08:28.810 --> 08:31.900
For every iteration I am writing, count is equal to count plus one.

08:31.930 --> 08:32.350
Okay.

08:32.380 --> 08:34.600
Now see what will happen if I execute this.

08:34.630 --> 08:36.490
You'll be seeing that zero is getting displayed.

08:36.490 --> 08:38.020
Initially I will be having zero.

08:38.020 --> 08:40.510
So when I write while count is less than five, this is true.

08:40.540 --> 08:42.340
It is just going to print the count.

08:42.340 --> 08:43.930
So I'm going to get zero over here.

08:43.930 --> 08:46.300
And then it is going to increment the count.

08:46.330 --> 08:49.300
Now my temporary variable count will have one value right.

08:49.330 --> 08:55.000
I'll not say temporary, but my count value from this zero has got incremented to one.

08:55.030 --> 08:57.160
Then again this file while loop will run.

08:57.160 --> 08:57.340
Why?

08:57.340 --> 09:00.430
It will run because this condition is still true, right?

09:00.430 --> 09:03.140
So now here you can see when one is less than five.

09:03.140 --> 09:04.430
So one is obviously less than five.

09:04.460 --> 09:05.240
In the second loop.

09:05.240 --> 09:06.800
So again this will get executed.

09:06.800 --> 09:08.330
Then again this will get incremented.

09:08.330 --> 09:09.770
Then again it will go to the next loop.

09:09.770 --> 09:11.870
Then this will become 234.

09:11.900 --> 09:12.440
Then.

09:12.440 --> 09:15.050
But once it becomes five right.

09:15.080 --> 09:17.750
While five is less than five now this becomes false.

09:17.750 --> 09:19.970
It is just going to come outside the loop, right.

09:19.970 --> 09:24.080
So that is how you are getting displayed with 01234.

09:24.110 --> 09:24.500
Right.

09:24.500 --> 09:30.020
So this is a simple way of probably displaying or understanding about the while loop.

09:30.050 --> 09:30.560
Okay.

09:30.860 --> 09:34.730
Uh, I may also write something like this while okay.

09:34.760 --> 09:36.080
Let's go ahead and write.

09:36.110 --> 09:38.750
Count is equal to zero okay.

09:40.700 --> 09:52.520
Count is equal to zero while count while count percentile two is double equal to zero okay.

09:52.550 --> 09:54.020
So now see this condition.

09:54.020 --> 09:59.210
What I'm saying while count percentile two is double equal to zero.

09:59.210 --> 10:04.200
If this is true right I will probably go ahead and print the count over here.

10:04.620 --> 10:05.130
Right?

10:05.460 --> 10:05.880
Right.

10:05.880 --> 10:11.160
And let's say that I will go ahead and or let me do one thing.

10:11.190 --> 10:11.490
Okay.

10:11.520 --> 10:13.590
If I'm starting from count is equal to zero.

10:13.620 --> 10:17.130
And this condition is basically to check whether a number is even number or not.

10:17.160 --> 10:17.580
Right.

10:17.610 --> 10:21.690
So if I just go ahead and execute it, how many times this will get displayed.

10:21.720 --> 10:23.880
See zero is getting displayed over here right.

10:23.880 --> 10:25.440
And it is continuously getting displayed.

10:25.440 --> 10:26.220
I'll stop this.

10:26.250 --> 10:28.350
See because I have not written any condition over here.

10:28.350 --> 10:29.910
So let me just go ahead and write.

10:29.940 --> 10:32.730
Count is equal to count plus one okay.

10:32.790 --> 10:34.860
Now how many times it will get displayed.

10:34.860 --> 10:36.060
You just see this okay.

10:36.090 --> 10:37.050
Only one time.

10:37.050 --> 10:39.660
Because the next time it became one this count became one.

10:39.690 --> 10:41.460
Then one percentile two is not equal to zero.

10:41.460 --> 10:45.840
So it is just going to come outside of the loop right of this particular loop.

10:45.840 --> 10:48.030
This is just to show you an example over here.

10:48.030 --> 10:50.940
But we will be seeing with more amazing examples.

10:50.940 --> 10:55.290
First of all, let's go ahead and just see the basic syntax of for while.

10:55.290 --> 11:00.060
And now we are going to go ahead with respect to loop control statements okay.

11:00.060 --> 11:02.790
Loop control statements.

11:03.720 --> 11:05.070
You'll be seeing that crash.

11:05.100 --> 11:05.580
Uh.

11:05.760 --> 11:06.960
Uh, please give more examples.

11:06.990 --> 11:07.470
Don't worry.

11:07.470 --> 11:12.210
After I probably make you understand all these things, then we will be seeing more examples as we go

11:12.240 --> 11:12.450
ahead.

11:12.480 --> 11:17.250
Okay, now with respect to the loop control statement, first of all, we will discuss about something

11:17.250 --> 11:18.180
called as break.

11:18.210 --> 11:18.750
Okay.

11:18.780 --> 11:23.850
Now if you really want to understand break I'll just go ahead and write this definition.

11:23.850 --> 11:26.550
The break statement exists.

11:26.730 --> 11:29.670
Exits the loop.

11:30.810 --> 11:31.380
Permanent.

11:33.030 --> 11:36.420
Uh, exits the loop prematurely.

11:36.450 --> 11:36.780
Okay.

11:36.810 --> 11:38.160
What does this basically mean?

11:38.160 --> 11:39.270
I will just make you understand.

11:39.300 --> 11:39.810
Okay.

11:40.800 --> 11:43.980
Let's say I want to write a break statement.

11:44.010 --> 11:45.990
Now, how do I write a break statement?

11:46.410 --> 11:50.340
So first of all, let's say I am going to run for I in range.

11:50.370 --> 11:51.030
Okay.

11:51.240 --> 11:54.840
Please make sure that you understand the syntax for I in range.

11:54.840 --> 11:56.010
Range of the collection.

11:56.010 --> 11:57.660
This is nothing, but this is collection.

11:57.660 --> 11:58.590
It can be a list.

11:58.590 --> 11:59.370
It can be a dictionary.

11:59.400 --> 12:00.060
It can be range.

12:00.060 --> 12:00.810
It can be string.

12:00.810 --> 12:01.750
Anything it can be.

12:01.780 --> 12:03.430
And let's say I'm writing ten.

12:03.430 --> 12:07.060
So ten basically means it is going to iterate from 0 to 1 okay.

12:07.180 --> 12:12.730
For one of the condition I'll say if I is double equal to five okay.

12:12.820 --> 12:19.750
If this I is equal to five I don't want to probably or I want to come outside this particular loop okay.

12:19.780 --> 12:22.300
I want to come outside this entire for loop.

12:22.300 --> 12:24.940
So I will go ahead and just write break okay.

12:25.060 --> 12:27.550
And once we probably write break okay.

12:27.550 --> 12:31.480
It is probably going to come outside of this particular for loop itself.

12:31.480 --> 12:31.900
Right.

12:31.930 --> 12:33.190
Now one more thing.

12:33.190 --> 12:36.640
If I is not equal to five at that time, we just need to print I.

12:36.670 --> 12:36.880
Okay.

12:36.910 --> 12:41.710
So now see what is this entire code that I have actually written for I in range of ten.

12:41.710 --> 12:46.330
If I is double equal to five, I am saying only when I is equal to five do.

12:46.330 --> 12:49.360
The break statement comes outside of this particular for loop.

12:49.360 --> 12:52.660
When I is not equal to five, you just need to keep on printing the numbers.

12:52.690 --> 12:53.020
Okay?

12:53.050 --> 12:57.700
So now if I go ahead and execute it here, you'll be able to see 012345.

12:57.880 --> 13:02.690
Then when five came automatically we came outside of this particular for loop.

13:02.720 --> 13:02.930
Okay.

13:02.960 --> 13:05.240
And then after that, nothing is getting printed.

13:05.240 --> 13:09.530
So we are executing this particular loop permanently.

13:09.560 --> 13:10.040
Okay.

13:10.070 --> 13:12.800
Permanently with the help of break over here.

13:13.220 --> 13:17.990
But this break will only get applied on any statement on any condition as such.

13:18.020 --> 13:18.410
Right.

13:18.410 --> 13:22.220
So that is why we are specifically using this break statement.

13:22.250 --> 13:22.790
Okay.

13:23.180 --> 13:25.670
Uh, this is one of the good examples.

13:25.670 --> 13:30.950
Uh, again, uh, now with respect to the another loop control statement, we also have something called

13:30.950 --> 13:31.790
as continue.

13:32.150 --> 13:32.630
Okay.

13:32.630 --> 13:35.120
So now we will go ahead and write continue.

13:35.150 --> 13:37.400
Now what exactly continue is okay.

13:37.430 --> 13:42.470
And you also need to know this if I really want to define continue.

13:42.500 --> 13:43.040
Okay.

13:43.070 --> 13:43.730
Continue.

13:43.760 --> 13:47.570
Statement skips the current iteration and continues with the next.

13:47.600 --> 13:48.080
Okay.

13:48.080 --> 13:51.710
So that basically means let's say I'm going to write for I in range.

13:51.740 --> 13:52.370
Okay.

13:52.400 --> 13:55.910
For I in range for I in range.

13:55.910 --> 13:58.640
And let's say I write some elements over here ten.

13:58.670 --> 13:59.210
Okay.

13:59.210 --> 14:06.350
And now if I go ahead and write if I modulus two right is double equal to zero okay.

14:06.800 --> 14:10.610
And this condition is basically just to check whether the number is even or not.

14:10.640 --> 14:15.380
If the number is even let's say that I will go ahead and write continue okay.

14:15.410 --> 14:17.120
I will just go ahead and write continue.

14:17.150 --> 14:23.090
And then I will just go over here and print I okay.

14:23.120 --> 14:29.660
Now in short, if I really want to make you understand, this is just like displaying all the odd numbers

14:29.660 --> 14:31.880
between 0 to 10, right?

14:31.910 --> 14:33.350
Do you agree with me or not?

14:33.380 --> 14:38.990
See what I'm doing over here for I in range of ten if I percentile if I modulus two is equal to zero,

14:39.020 --> 14:40.430
that basically means this is an even number.

14:40.460 --> 14:41.630
We are just going to continue.

14:41.900 --> 14:44.960
Otherwise if it is not which is an odd number, we are just going to print it.

14:44.990 --> 14:50.660
So if I go ahead and execute this you'll be able to see that I will only get an odd number 13579 between

14:50.660 --> 14:51.470
0 to 10.

14:51.500 --> 14:51.830
Right.

14:51.860 --> 14:55.220
And that is what is a continue example with respect to continue.

14:55.250 --> 14:57.650
We are just we are just skipping the current iteration.

14:57.650 --> 14:58.640
This will just skip.

14:58.670 --> 15:00.530
It will even not go to the print statement.

15:00.560 --> 15:02.610
It will just go ahead and continue the loop.

15:02.640 --> 15:03.060
Right.

15:03.060 --> 15:06.960
So this is what is the power with respect to the continuous statement.

15:06.990 --> 15:12.300
There is also one more amazing flow control that we use that is basically called as pass.

15:12.330 --> 15:12.930
Okay.

15:12.930 --> 15:17.190
And you should know all these things because some or the other way we will be using all these things

15:17.190 --> 15:17.760
right.

15:18.000 --> 15:23.760
Further when we'll be seeing multiple example, the pass statement is a null operation and it does nothing

15:23.790 --> 15:24.210
okay.

15:24.240 --> 15:25.320
It does nothing.

15:25.320 --> 15:27.120
Nothing means nothing right.

15:27.150 --> 15:31.440
It will just say, hey, I don't want to do anything on a specific condition, okay?

15:31.470 --> 15:41.460
So now if we go ahead and write for I in range for I in range of five, let's say okay for I in range

15:41.460 --> 15:41.940
of five.

15:41.940 --> 15:44.520
Let's say I'm giving this particular condition okay.

15:44.550 --> 15:52.230
I'm saying hey if I is equal to three, if I is double equal to three okay.

15:52.260 --> 15:55.350
Then I'm saying hey just pass okay.

15:55.380 --> 15:56.760
Do nothing okay.

15:56.790 --> 15:59.940
I don't want you to do anything over here, okay?

15:59.970 --> 16:03.320
And then we are going to print I.

16:03.350 --> 16:03.620
Okay.

16:03.650 --> 16:06.290
Now see what will happen in this particular case.

16:06.290 --> 16:10.490
Whenever I say hey, whenever the number is even, just skip this for loop.

16:10.490 --> 16:13.310
But here it is saying do nothing, just go ahead.

16:13.340 --> 16:13.820
Right?

16:13.820 --> 16:16.400
Just go ahead and skip it right now.

16:16.400 --> 16:18.590
If I go ahead and execute it now, see what is happening.

16:18.620 --> 16:22.910
Zero is getting displayed, one is getting displayed, two is getting displayed.

16:22.910 --> 16:24.050
When I is three.

16:24.080 --> 16:25.970
Again, I'm just saying hey, skip it.

16:25.970 --> 16:27.980
So it has just skipped this if loop, right.

16:28.010 --> 16:33.080
Or the best way will be to print something like this okay.

16:33.170 --> 16:39.680
The number is okay I will write number is over here as I okay.

16:39.710 --> 16:41.300
Now you'll be able to understand this.

16:41.330 --> 16:46.220
Now this pass as soon as we do the pass right at that time, what will happen now?

16:46.220 --> 16:49.040
First of all, see when I is equal to three it only this will get printed.

16:49.040 --> 16:50.750
And we are just going to skip this okay.

16:50.780 --> 16:52.400
Or do nothing as such.

16:52.400 --> 16:54.320
So here you can see 0123.

16:54.320 --> 16:56.450
The number is 334.

16:56.480 --> 16:56.960
Right.

16:56.990 --> 16:58.040
Pass in.

16:58.070 --> 16:59.540
It's just like a null statement.

16:59.540 --> 17:01.670
Suppose I don't have anything to give over here.

17:01.670 --> 17:04.370
I will just remove this and I will just write pass.

17:04.400 --> 17:05.420
Do nothing as such.

17:05.450 --> 17:05.870
Okay.

17:05.870 --> 17:08.060
So this can also be useful.

17:08.060 --> 17:09.740
And again I will talk about it.

17:09.800 --> 17:12.470
What exactly will be the use case over here.

17:12.620 --> 17:13.100
Right.

17:13.100 --> 17:19.910
But most of them if I probably consider pass continue or let's say if I'm probably defining an empty

17:19.940 --> 17:25.760
function and if I define an empty function, and if I internally write write just pass.

17:25.760 --> 17:27.620
That basically means that the function does nothing.

17:27.650 --> 17:28.070
Okay.

17:28.100 --> 17:32.000
So this is some of the amazing things that have been brought by Python.

17:32.210 --> 17:36.890
Uh, if I talk about break, this will be very handy because on a certain condition we are just going

17:36.890 --> 17:38.840
to break this or come out of this particular loop.

17:38.840 --> 17:39.800
We can use break.

17:39.830 --> 17:41.030
Then we have continue.

17:41.060 --> 17:42.590
Then we have pass.

17:42.620 --> 17:47.090
Right now let's talk about nested loops okay.

17:47.090 --> 17:50.180
So let's talk about nested loops.

17:51.530 --> 17:57.530
Nested loop is nothing but a loop inside a loop right.

17:58.400 --> 18:05.060
Let's say I will run a loop for I in range three.

18:05.660 --> 18:06.320
Okay.

18:06.380 --> 18:09.950
And let's say for J in range two.

18:10.520 --> 18:10.850
Okay.

18:10.880 --> 18:12.050
Now what I'm doing.

18:12.740 --> 18:14.720
And let's say I go ahead and print.

18:15.560 --> 18:16.520
Print.

18:16.520 --> 18:21.320
And here with respect to the print statement let me show you how you can use f string also.

18:21.350 --> 18:23.570
So f f string is nothing.

18:23.570 --> 18:25.940
But here I'm basically writing it out.

18:25.970 --> 18:35.270
And here I'll write I is equal to will be equal to I and j equal to it will be equal to j okay.

18:35.300 --> 18:41.210
So whenever we use this f string formatted statement to display the variables we have to use this curly

18:41.210 --> 18:41.930
braces.

18:41.930 --> 18:46.310
And in short this is just a formatting option of displaying the string.

18:46.310 --> 18:52.220
I can use this particular string together along with uh you know, flower braces so that I can display

18:52.220 --> 18:53.240
that particular variable.

18:53.270 --> 18:55.370
Now, what is exactly happening over here?

18:56.210 --> 18:57.560
See, it is very simple.

18:57.890 --> 19:02.280
Uh, over here, you'll be able to see for I in range of three.

19:02.310 --> 19:02.790
Right.

19:02.790 --> 19:05.220
I'm iterating through another range over here.

19:05.220 --> 19:07.110
Another iterate over here.

19:07.110 --> 19:10.140
Then I'm also running another iteration over here.

19:10.170 --> 19:10.440
Right.

19:10.440 --> 19:12.060
So there are three elements over here.

19:12.060 --> 19:13.740
So first it will run this.

19:13.740 --> 19:15.090
Then it will go inside this.

19:15.090 --> 19:20.040
And it will complete the inner loop right inner inner loop inner inner for loop.

19:20.040 --> 19:21.750
Then it will go to the outside for loop.

19:21.750 --> 19:25.710
So first time when it gets displayed this is going to get executed for the two times.

19:25.710 --> 19:29.430
Then it will going to go and increment the I to two.

19:29.460 --> 19:34.890
Then again it is going to uh run it for two times for this particular loop.

19:34.890 --> 19:39.300
If you don't know, if you don't understand, see over here, I'll show you with respect to the output.

19:39.300 --> 19:42.870
So here initially you can see when I is zero J is zero.

19:42.900 --> 19:45.660
Because initially when we are running this particular loop.

19:45.690 --> 19:46.020
Right.

19:46.050 --> 19:49.230
And then when we go inside this particular loop J will run for two times.

19:49.230 --> 19:51.930
So j is running for j is equal to zero, j is equal to one.

19:51.960 --> 19:53.610
Then you have I is equal to one.

19:53.610 --> 19:55.740
Then again j is equal to zero, j is equal to one.

19:55.740 --> 19:56.310
Then you have.

19:56.310 --> 19:59.310
When I is incremented to two, j is equal to zero and j is equal to one.

19:59.310 --> 20:02.680
So it is basically going to run with respect to all the inner loops.

20:02.710 --> 20:03.070
Right.

20:03.070 --> 20:07.990
And here this for loop, every time it will be executed based on the number of range that I've actually

20:07.990 --> 20:08.800
given over here.

20:08.830 --> 20:09.190
Right.

20:09.190 --> 20:13.030
Whenever we insert enter the upper for loop itself.

20:13.060 --> 20:13.420
Right.

20:13.420 --> 20:17.590
So this is just an example of a for loops over here.

20:17.710 --> 20:18.520
Uh sorry.

20:18.550 --> 20:20.950
Nested loops over here right now.

20:20.950 --> 20:23.650
Let us go ahead and see some examples okay.

20:23.680 --> 20:26.500
And this is where we will practice multiple things.

20:26.980 --> 20:32.770
Uh, the first examples that we are going to do is that, uh, let's say I am going to write over here,

20:32.800 --> 20:46.270
calculate the sum of first n natural numbers using a while loop, using a while and for loop.

20:46.660 --> 20:47.230
Okay.

20:47.230 --> 20:51.670
So let me just go ahead and first of all show you with the help of uh while loop okay.

20:51.670 --> 20:54.070
So first we'll see with respect to the while loop.

20:54.070 --> 20:58.210
And then we will go ahead and see with respect to the for loop okay.

20:58.240 --> 21:02.260
What we need to do, calculate the sum of first n and natural numbers.

21:02.290 --> 21:02.530
Okay.

21:02.560 --> 21:03.880
First n natural numbers.

21:03.880 --> 21:04.690
Very simple.

21:04.720 --> 21:11.230
So now I will just go ahead and say, let's say that the number of a n number the natural number is

21:11.230 --> 21:11.710
ten.

21:11.740 --> 21:15.280
Then initially I will define my some variable to zero.

21:15.310 --> 21:20.020
This will be my temporary variable because the addition that I'm going to do with respect to all the

21:20.020 --> 21:24.100
natural numbers, I'm going to save it in this particular variable called as sum.

21:24.190 --> 21:29.140
Then I will keep this temporary variable called as count is equal to one okay.

21:29.470 --> 21:31.660
Now let's go ahead.

21:31.690 --> 21:31.900
Okay.

21:31.930 --> 21:39.130
So here I'm going to write while okay count is less than or equal to n right.

21:39.430 --> 21:45.550
Unless until the count is less than or equal to n I'm going to just go ahead and I'm going to use this

21:45.550 --> 21:48.940
sum is equal to sum plus count.

21:48.970 --> 21:49.510
Right.

21:49.510 --> 21:52.660
So what we are doing over here we are keep on adding the count.

21:52.690 --> 21:52.990
Right.

21:53.020 --> 21:55.990
Unless and until this is not equal to less than or equal to n.

21:55.990 --> 21:57.910
And after this we will go ahead and write.

21:57.940 --> 22:00.370
Count is equal to count plus one.

22:00.370 --> 22:02.420
We are incrementing the count variable also.

22:02.450 --> 22:07.100
And finally, when this for loop is, a while loop is getting completed.

22:07.130 --> 22:14.390
I will write sum of first ten natural numbers.

22:15.080 --> 22:21.770
Okay, natural numbers will be equal to nothing but whatever sum we are probably getting over here,

22:21.800 --> 22:22.190
right?

22:22.220 --> 22:23.930
Just go through the statements here.

22:23.930 --> 22:27.200
We have initialized how many number of natural numbers we are going to consider.

22:27.200 --> 22:29.930
Initially, the sum is equal to zero, count is equal to one.

22:29.930 --> 22:35.090
Now I am saying while count is less than or equal to n, I'm going to increment this count continuously

22:35.090 --> 22:40.670
unless and until this gets satisfied, then automatically internally we are doing the sum also.

22:40.700 --> 22:41.120
Okay.

22:41.150 --> 22:44.510
So here you can actually see the sum of first n ten.

22:44.510 --> 22:47.480
Natural numbers is nothing but 55 perfect.

22:47.480 --> 22:53.420
So this is just like one plus two plus three plus four plus five plus six plus seven plus eight plus

22:53.420 --> 22:54.410
nine plus ten.

22:54.440 --> 22:55.010
Right.

22:55.250 --> 22:56.450
Uh, it will not go.

22:56.600 --> 22:59.810
Yeah, it will go till ten because we have written less than or equal to n.

22:59.840 --> 23:00.500
Okay.

23:00.500 --> 23:01.250
Perfect.

23:01.250 --> 23:03.060
So this was one of the example.

23:03.180 --> 23:05.700
Let's do it the same thing with the help of for loop.

23:05.700 --> 23:06.570
So I will write.

23:06.600 --> 23:12.330
Uh, so let me just go ahead and define this three variables again okay.

23:12.930 --> 23:17.460
So let's say for I in range.

23:17.490 --> 23:24.450
And here also I can just go ahead and write till 11 because I want to probably do from zero to 0 to

23:24.450 --> 23:25.410
10 okay.

23:25.440 --> 23:29.970
And here I'll just go ahead and write sum is equal to sum plus I.

23:30.000 --> 23:30.840
That's it.

23:30.870 --> 23:31.380
Right.

23:31.380 --> 23:33.480
And here I don't even have to write.

23:33.480 --> 23:35.010
This count is equal to one okay.

23:35.040 --> 23:39.360
Because automatically this will get integrated in uh uh incremented.

23:39.360 --> 23:42.450
So here let me just go ahead and print the sum.

23:42.450 --> 23:46.590
And if I go ahead and execute I'm also able to get it 55 okay.

23:46.590 --> 23:53.550
So here I've shown you with the help of both while and for loop how you can calculate the first ten

23:53.550 --> 23:56.160
natural uh numbers sum.

23:56.160 --> 23:57.930
And here you are able to see it.

23:57.960 --> 23:58.440
Okay.

23:58.500 --> 23:59.850
Now let me do one thing.

23:59.850 --> 24:02.070
Let me show you one more example.

24:02.100 --> 24:10.740
Okay, now this time the examples will be talk about the prime numbers or display the prime numbers

24:10.740 --> 24:12.960
between 1 and 100.

24:12.990 --> 24:14.220
Now what is prime number?

24:14.220 --> 24:18.300
Prime number are something which is divisible by one or the number itself.

24:18.330 --> 24:18.870
Right.

24:18.900 --> 24:22.560
Now, uh, here I'll just go ahead and write it down.

24:22.560 --> 24:24.510
And again for this I will use a for loop.

24:24.510 --> 24:29.700
So I'll write for num in range of one comma 101.

24:29.730 --> 24:29.910
Right.

24:29.940 --> 24:33.300
Because we need the numbers between 1 and 100 right.

24:33.510 --> 24:38.940
If the first condition will be that, I will go ahead and write if num is greater than one, right.

24:38.970 --> 24:41.070
Obviously the number has to be greater than one.

24:41.070 --> 24:42.510
So one condition is over here.

24:42.540 --> 24:45.690
We are going to use if nested if nested for loops.

24:45.690 --> 24:45.810
And.

24:45.840 --> 24:46.530
All right.

24:46.650 --> 24:50.940
Please pause the video, try it by yourself and then probably see the solution.

24:50.970 --> 24:51.420
Okay.

24:51.450 --> 24:57.690
Now if this is true then what I'll do for I in range two comma num.

24:57.720 --> 24:58.380
Okay.

24:58.890 --> 25:02.530
Uh, for I in range two common norm.

25:02.530 --> 25:07.240
That basically means from this number, from two to this particular number, whichever number we are

25:07.240 --> 25:13.510
specifically discussing, or from this for loop, which number I'm able to increment, I will just go

25:13.540 --> 25:15.880
ahead and write this particular condition.

25:15.880 --> 25:23.770
If num percentile I write, I is the same number over there, right is equal to zero.

25:23.800 --> 25:26.740
Then we are just going to break it okay.

25:26.740 --> 25:31.870
And then we are just going to break and come outside this for loop.

25:31.870 --> 25:35.980
And I'm going to say else print number okay.

25:36.940 --> 25:38.200
We are just need to print this.

25:38.200 --> 25:40.210
Because if this gets satisfied right.

25:40.210 --> 25:44.050
This is basically indicating that hey I've got a prime number.

25:44.080 --> 25:44.560
Right.

25:44.560 --> 25:46.990
And then it is just going to display the prime number.

25:46.990 --> 25:49.780
So again I'm repeating first of all I'm going to check a condition.

25:49.780 --> 25:55.030
If number is greater than one then we are going to write for I in range of two comma num okay two comma

25:55.030 --> 25:55.420
num.

25:55.420 --> 25:59.740
So we are incrementing the so so we are running through this particular two comma num.

25:59.740 --> 26:01.000
Whatever num is there.

26:01.030 --> 26:03.080
Let's say the number is three.

26:03.080 --> 26:05.540
So two comma three for range in two comma three.

26:05.540 --> 26:09.740
Then we can go ahead and write three percentile two is double equal to zero.

26:09.770 --> 26:11.360
The answer is no right.

26:11.360 --> 26:14.210
If it is no we are just going to go ahead and display this number.

26:14.240 --> 26:14.720
Right.

26:14.720 --> 26:19.700
And if it breaks it is just going to go outside this particular for loop okay.

26:19.700 --> 26:23.570
So that basically means if this condition is becoming true right.

26:23.570 --> 26:26.660
That basically means it is not a prime number.

26:26.690 --> 26:29.780
Otherwise we are just going to display the number in the else block.

26:29.810 --> 26:30.320
Okay.

26:30.380 --> 26:35.780
And one more thing about for loop is that along with the for loop also you can write else block right

26:35.810 --> 26:41.930
else block if something uh you don't want to execute it, but you want to execute compulsory at the

26:41.930 --> 26:42.950
end of the for loop.

26:42.950 --> 26:44.990
So you can also use this else block okay.

26:45.020 --> 26:47.960
So once I execute this here you'll be able to see two three.

26:47.960 --> 26:54.260
So two is a prime number three is a prime number five seven is a prime number 11 1317 1923, 29.

26:54.290 --> 26:55.580
All these are prime numbers.

26:55.580 --> 26:57.710
You can probably go ahead and check it out okay.

26:58.010 --> 27:02.150
Uh these are like numbers which are only divisible by one or the number itself.

27:02.180 --> 27:02.390
Right.

27:02.420 --> 27:04.310
So here you can actually see the example.

27:04.370 --> 27:04.880
Okay.

27:05.060 --> 27:10.430
Now some more uh uh examples that you will be seeing as we go ahead.

27:10.460 --> 27:16.520
But where we'll be using when we learn more about uh, different different data structures like lists

27:16.520 --> 27:17.300
and dictionary.

27:17.300 --> 27:22.100
But at the end, uh, conclusion that I really want to give after every lesson.

27:22.130 --> 27:22.610
Right.

27:22.610 --> 27:25.910
And let me make a markdown cell over here.

27:25.970 --> 27:26.600
Okay.

27:27.260 --> 27:31.010
So conclusion is that loops are really powerful.

27:31.340 --> 27:31.850
Okay.

27:31.880 --> 27:35.000
Loops are definitely very, very powerful.

27:35.030 --> 27:35.720
Okay.

27:35.990 --> 27:41.450
Um, in Python that allows you to execute block of code multiple times by understanding, using for

27:41.450 --> 27:44.240
and while loops along with the loop control statement like break, continue, and pass.

27:44.240 --> 27:49.370
You can handle a wide range of programming tasks efficiently, and you'll be seeing that when we will

27:49.400 --> 27:51.140
be discussing about more examples.

27:51.140 --> 27:56.510
But I really want to keep the video between 20 to 25 minutes, where I show you multiple examples where

27:56.510 --> 27:59.990
I write each line of code in front of you so that you will also be able to understand.

27:59.990 --> 28:01.430
So yes, this was it from my side.

28:01.430 --> 28:02.630
I'll see you all in the next video.

28:02.630 --> 28:02.930
Thank you.
