WEBVTT

00:00.200 --> 00:00.920
Hello guys.

00:00.920 --> 00:03.530
So we are going to continue the discussion with respect to Python.

00:03.530 --> 00:06.470
And now we are going to see some of the examples already.

00:06.470 --> 00:11.780
In our previous video we saw many topics regarding functions, and there are a lot of advanced topics

00:11.780 --> 00:12.650
that are going to come.

00:12.650 --> 00:17.630
But before that I thought, why not create some examples and show you how we can probably solve those?

00:17.660 --> 00:18.140
Right?

00:18.140 --> 00:22.730
So first example over here is that we need to create a function for temperature conversion.

00:22.760 --> 00:27.050
So temperature conversion basically means we need to convert Celsius to Fahrenheit or Fahrenheit to

00:27.080 --> 00:27.830
Celsius.

00:27.830 --> 00:29.450
So how do we define a function.

00:29.450 --> 00:30.350
We'll go ahead and see.

00:30.350 --> 00:32.150
So let's go ahead and see this example.

00:32.150 --> 00:34.610
So first of all I will go ahead and create my function.

00:34.610 --> 00:38.000
Let's say I will write convert underscore temperature.

00:38.000 --> 00:40.130
So this is my function over here.

00:40.250 --> 00:42.290
And here I will give two parameters.

00:42.290 --> 00:46.190
One is temperature and one is the unit that I really want to convert it.

00:46.220 --> 00:52.610
Let's say if I give the unit over here as C, that basically means I need to convert this, uh, this

00:52.640 --> 00:55.670
entire temperature from Celsius to Fahrenheit.

00:55.700 --> 00:56.210
Right.

00:56.210 --> 00:59.090
And if I give the unit as F, right.

00:59.090 --> 01:02.460
So I need to convert the temperature from Fahrenheit to Celsius.

01:03.120 --> 01:03.480
Right.

01:03.480 --> 01:06.240
So let me just go ahead and give a docstring.

01:06.240 --> 01:20.880
So this function converts temperature between Celsius and Fahrenheit.

01:20.910 --> 01:21.510
Okay.

01:21.630 --> 01:27.570
So uh this is what is my definition of this particular function over here.

01:27.570 --> 01:30.330
And uh, we will go ahead and write our code.

01:30.360 --> 01:30.630
Right.

01:30.630 --> 01:36.090
So here let's say I will go ahead and write if unit is double equal to capital C right.

01:36.090 --> 01:41.100
If I'm giving this particular unit that basically means I need to convert my temperature from Celsius

01:41.100 --> 01:41.610
to Fahrenheit.

01:41.640 --> 01:45.870
So I'm going to return, uh, temperature multiplied by.

01:45.870 --> 01:47.820
And I'll do the operation nine by five.

01:47.850 --> 01:48.180
Okay.

01:48.210 --> 01:51.660
And here let me just give the bracket nine by five.

01:51.930 --> 01:52.680
Uh oh.

01:52.680 --> 01:52.950
Sorry.

01:52.950 --> 01:54.420
I need to first of all, multiply it.

01:54.420 --> 01:57.120
Then I will go ahead and do the addition plus 32.

01:57.150 --> 02:02.800
So this is the formula of converting a Celsius to Fahrenheit with respect to the temperature.

02:02.830 --> 02:03.160
Okay.

02:03.190 --> 02:05.830
So this is the simple formula that we specifically use.

02:05.830 --> 02:12.970
And this in turn will be converting Celsius to Fahrenheit.

02:13.240 --> 02:13.480
Okay.

02:13.510 --> 02:14.290
Fahrenheit.

02:14.590 --> 02:16.000
Fahrenheit.

02:16.030 --> 02:16.450
Okay.

02:16.870 --> 02:20.110
Then, uh, let me go ahead and write, Elif.

02:20.140 --> 02:20.530
Okay.

02:20.560 --> 02:23.410
So here we are going to use everything, whatever things you have learned.

02:23.410 --> 02:27.280
Let's say there are some examples that I'm going to use dictionaries, some examples where I'm going

02:27.310 --> 02:29.110
to use, uh, list and all.

02:29.230 --> 02:32.800
In this particular case, you can probably see that I'm also using if Elif.

02:32.800 --> 02:33.160
Right.

02:33.160 --> 02:38.050
So if Elif, the unit is double equal to Fahrenheit.

02:38.080 --> 02:40.720
Okay, then I'm going to basically return.

02:40.750 --> 02:41.200
Okay.

02:41.230 --> 02:48.400
And with for this particular temperature, I need to minus first of all, 32 and then multiply it by

02:48.400 --> 02:49.750
five by nine.

02:50.230 --> 02:50.770
Okay.

02:50.770 --> 02:55.450
And this will basically give us uh, our Fahrenheit to Celsius.

02:55.450 --> 02:55.810
Right.

02:55.810 --> 02:56.980
So this is the formula.

02:56.980 --> 02:59.710
If you go ahead and do the Google search.

02:59.710 --> 03:02.400
Also how to convert Celsius to Fahrenheit.

03:02.430 --> 03:04.350
So this is the formula that you usually get.

03:04.380 --> 03:04.890
Okay.

03:04.980 --> 03:07.200
So uh that's it.

03:07.200 --> 03:08.880
This is what it is, right?

03:08.880 --> 03:14.130
And if I don't give any information for our third parameter we need to also handle that.

03:14.130 --> 03:15.870
That is if I'm not giving any unit.

03:15.870 --> 03:17.790
So I will just go ahead and return.

03:17.790 --> 03:18.900
None okay.

03:18.930 --> 03:19.770
None.

03:20.130 --> 03:20.970
So done.

03:20.970 --> 03:23.310
So here I'm going to basically use this function.

03:23.310 --> 03:26.460
Now let me go ahead and call this function as convert temperature.

03:26.460 --> 03:32.220
And if I go ahead and write like a value let's say temperature I'm going to give 25.

03:32.250 --> 03:32.520
Right.

03:32.520 --> 03:34.470
And this is in Celsius.

03:34.470 --> 03:35.100
Right.

03:35.550 --> 03:38.310
Uh then I'm going to go ahead and print it.

03:38.460 --> 03:38.940
Right.

03:38.940 --> 03:41.040
And I will be basically getting converted.

03:41.040 --> 03:45.060
We are converting this from 29 degree Celsius to Fahrenheit.

03:45.090 --> 03:48.000
The next one that I'm actually going to do is print.

03:48.030 --> 03:48.570
Okay.

03:48.600 --> 03:52.050
Again I will go ahead and call my convert temperature.

03:52.050 --> 03:56.340
And here I'm actually going to give 77 comma F.

03:56.340 --> 03:57.000
Right.

03:57.000 --> 03:58.920
And this I will be able to get the answer.

03:58.920 --> 04:00.060
Let's see whether it works.

04:00.060 --> 04:07.180
Yes 25 is nothing but 77 degree Fahrenheit, and 77 degree Fahrenheit is nothing but 25 degree Celsius.

04:07.180 --> 04:09.010
So this is how the conversion is done.

04:09.010 --> 04:11.110
And I feel that formula is absolutely right.

04:11.110 --> 04:12.190
What you have written.

04:12.520 --> 04:16.180
Um, and here you can just see based on units we are able to do it.

04:16.180 --> 04:18.520
So this is our first example.

04:18.760 --> 04:23.800
Um, now what I'm actually going to do is that I'm going to show you one more amazing example, which

04:23.800 --> 04:26.440
is called as Password strength checker.

04:26.470 --> 04:26.890
Okay.

04:26.920 --> 04:28.510
Now what exactly is this?

04:28.510 --> 04:31.930
Uh, let's say that we are creating our user ID or login.

04:31.930 --> 04:34.180
And over there we specifically give passwords.

04:34.180 --> 04:34.570
Right.

04:34.570 --> 04:35.230
And it is.

04:35.350 --> 04:38.230
And most of the people just write password as 12345.

04:38.260 --> 04:41.380
So we need to have a strong password check okay.

04:41.800 --> 04:46.810
So I'll be giving some of the check over here to just uh, making you understand whether it is a weak

04:46.810 --> 04:49.900
password or a true power or a very strong password.

04:49.930 --> 04:50.350
Right.

04:50.350 --> 04:55.120
So first of all, I will go ahead and write my definition is strong password.

04:55.930 --> 05:01.490
So this function will be responsible in checking whether a password is strong or not okay.

05:02.000 --> 05:05.780
So here you can see I will just go ahead and write my docstring.

05:05.810 --> 05:13.670
This function checks if the password is strong or not.

05:14.420 --> 05:15.110
Okay.

05:16.490 --> 05:17.660
Now let me go ahead and write.

05:17.690 --> 05:22.310
Let's say first condition that first criteria I want to put with respect to the password.

05:22.310 --> 05:25.550
My password should be minimum of eight character right.

05:25.550 --> 05:34.910
So I will go ahead and write if length of if length of password is less than eight is less than eight.

05:34.910 --> 05:39.710
So that basically means I'm going to just say, hey, this is a very bad password in short.

05:39.710 --> 05:44.870
So I will just go ahead and say false, because this function that I'm actually creating is basically

05:44.870 --> 05:46.850
checking whether a password is strong or not.

05:46.850 --> 05:48.590
So right now the password is not strong.

05:48.590 --> 05:49.880
So I'm returning false.

05:49.910 --> 05:52.580
Then let me just go ahead and write another condition.

05:52.580 --> 05:53.810
If not okay.

05:53.810 --> 05:57.350
So if not is one condition, if not any okay.

05:57.620 --> 06:02.850
So here we are just going to see whether this Character is having any digit or not.

06:02.880 --> 06:07.290
Write whatever character, whatever password we are specifically giving, whether it is having any digit

06:07.290 --> 06:07.860
or not.

06:07.890 --> 06:08.400
Right.

06:08.400 --> 06:14.940
So what I'm actually going to do, if not, is basically like comparing whether there is not present

06:14.940 --> 06:15.240
or not.

06:15.240 --> 06:15.450
Right.

06:15.480 --> 06:16.170
Something like that.

06:16.170 --> 06:20.490
So I will go ahead and use a for loop for character in password.

06:20.490 --> 06:23.100
So we will go ahead and see each and every character.

06:23.100 --> 06:28.230
And here I will just go ahead and see whether this character is having any digit or not.

06:28.350 --> 06:28.830
Okay.

06:28.860 --> 06:34.260
So each digit is a function that is present in every characters or string, right.

06:34.260 --> 06:39.030
So I'm just going to compare for every character in this specific password, whether it is having a

06:39.030 --> 06:39.840
digit or not.

06:39.870 --> 06:40.440
Right.

06:40.470 --> 06:42.990
If there is not any digits right.

06:43.020 --> 06:45.960
So what I will do I will again return false.

06:45.990 --> 06:46.530
Okay.

06:46.650 --> 06:50.040
Please see carefully what kind of condition I'm using.

06:50.070 --> 06:50.550
Right.

06:50.550 --> 06:55.320
So this is just like any function or an inbuilt function that is present in Python.

06:55.320 --> 06:58.950
And we are iterating through each and every characters in that password.

06:58.950 --> 07:00.660
And we are seeing whether it is a digit or not.

07:00.690 --> 07:01.140
Right.

07:01.150 --> 07:05.020
And this is dessert is a kind of function that is available in strings.

07:05.050 --> 07:05.260
Okay.

07:05.290 --> 07:07.600
So this is one of the check that I really want to do.

07:07.810 --> 07:08.380
Right.

07:08.830 --> 07:12.670
Then I will also be checking with respect to the password is lower.

07:12.670 --> 07:19.240
If all the characters is lower then also we should return it false saying that hey it is not exactly

07:19.240 --> 07:20.230
a good password, right?

07:20.230 --> 07:21.670
So I will go ahead and write.

07:21.700 --> 07:22.870
If not any.

07:22.900 --> 07:25.150
Again, I will go ahead and write my next condition.

07:25.150 --> 07:32.380
And here this time I will say hey for car in password and here I'm going to check.

07:32.410 --> 07:32.920
Right?

07:32.920 --> 07:34.960
I'm what I have to check over here.

07:34.990 --> 07:37.540
Whether my character is lower.

07:37.540 --> 07:40.990
So low is lower is one of the function that is again present in Python.

07:40.990 --> 07:43.720
Sorry for character in password I have to write.

07:43.750 --> 07:44.290
Okay.

07:44.620 --> 07:49.240
Uh, I'm just checking whether the character is lower or not for all the characters that are present

07:49.240 --> 07:49.810
over there.

07:49.810 --> 07:53.140
And if it is present, I'm just going to return the false.

07:53.230 --> 07:53.890
Okay.

07:54.010 --> 07:56.800
So guys, now we have written the condition still here.

07:56.800 --> 08:00.340
We have also checked like all the characters should not be lowercase.

08:00.370 --> 08:00.880
Okay.

08:00.950 --> 08:08.000
Now, similarly, all the characters should also not be, you know, uppercase, you know, and we'll

08:08.000 --> 08:09.140
also check for that.

08:09.140 --> 08:09.440
Right.

08:09.440 --> 08:14.420
So here I'm going to write if if not in if not any.

08:14.420 --> 08:17.510
And this time I will be using Cat.

08:17.540 --> 08:19.940
Dot is upper right.

08:19.940 --> 08:25.700
So there are a lot of functions with respect to strings which I can specifically use over here.

08:25.700 --> 08:30.920
And let me just go ahead and write for cat in password okay.

08:30.980 --> 08:37.100
And this we will go ahead and check it out and we'll again write a return false okay.

08:37.250 --> 08:38.840
Now this is there.

08:38.870 --> 08:40.520
Now let's think of more things.

08:40.550 --> 08:44.090
You know uh let's see for any special characters.

08:44.090 --> 08:51.170
So for special character, uh, I would like to add many special characters that are probably present,

08:51.200 --> 08:51.680
right.

08:51.710 --> 08:57.290
If not any, let's say in our character, if there is no special character like this, then also I can

08:57.290 --> 09:01.840
say that, hey, this is basically a weak password right?

09:01.840 --> 09:03.790
At least it should have one upper.

09:03.970 --> 09:04.630
You know.

09:04.660 --> 09:08.290
At least it should have one digits, you know, so this kind of condition.

09:08.290 --> 09:11.740
So here I've used all special characters over here for character in password.

09:11.740 --> 09:15.880
And then if it is not present we are just going to return false.

09:15.910 --> 09:21.070
If everything satisfies then we will go ahead and return true okay.

09:21.190 --> 09:23.200
So here all return false are there.

09:23.200 --> 09:25.150
And then here return true is there.

09:25.150 --> 09:25.420
Right.

09:25.420 --> 09:30.460
So if all this condition are false then uh, if all this condition does not get satisfied, then we

09:30.490 --> 09:31.510
are just going to return true.

09:31.510 --> 09:36.040
Saying that hey, this is super amazing password and it is a strong password.

09:36.040 --> 09:38.350
Now let me just go ahead and call the function.

09:38.350 --> 09:41.560
So calling the function over here okay.

09:41.680 --> 09:45.070
And let me just give some of the things over here.

09:45.100 --> 09:45.310
Right.

09:45.310 --> 09:46.840
So I'll go ahead and write.

09:47.530 --> 09:51.310
Print is underscore strong password.

09:51.310 --> 09:53.020
And let me just give one example.

09:53.050 --> 09:55.210
Let's say I have a capital letter.

09:55.210 --> 09:58.420
And I will write a small letter again a capital letter.

09:58.420 --> 10:00.670
And this is my password okay.

10:00.670 --> 10:05.960
But here you can see that I'm not having any special character or other than that, I think everything

10:05.960 --> 10:11.390
is getting satisfied and digit is also not there, so I should definitely be getting false for this.

10:11.420 --> 10:16.040
One more way is that I can go ahead and print okay is strong password.

10:16.040 --> 10:20.210
And let me just go ahead and use some special characters.

10:20.210 --> 10:26.810
So I will go ahead and write str and let me just go ahead and use a zero over here.

10:26.840 --> 10:27.380
Okay.

10:27.410 --> 10:31.160
And I will be using ng capital P w d.

10:31.280 --> 10:33.830
And let me use one special character like exclamation.

10:33.860 --> 10:34.340
Okay.

10:34.370 --> 10:36.680
Now let's see whether both of them.

10:36.680 --> 10:40.910
I think this should basically be giving me false and this should be giving me true.

10:40.940 --> 10:45.980
Okay, so now if I execute this here, you can see since I do not have a digit or a special character,

10:45.980 --> 10:48.020
I'm actually getting false here.

10:48.020 --> 10:52.790
I have both, uh, a digit and a special character.

10:52.790 --> 10:56.150
So and that is the reason I am also having a capital letter over here.

10:56.150 --> 10:57.770
So that is the reason I am getting true.

10:57.770 --> 11:02.230
So this is one amazing example with respect to password strength checker.

11:02.230 --> 11:07.960
Just with the help of one function, we are able to do this kind of, uh, validation, and we are able

11:07.960 --> 11:08.980
to execute it.

11:09.010 --> 11:09.520
Okay.

11:09.550 --> 11:13.210
Now, uh, let's go with the third example.

11:13.240 --> 11:19.210
Now, this specific example I have actually selected, uh, just to make sure to show you like, uh,

11:19.210 --> 11:22.000
how in the real world application this can be used.

11:22.000 --> 11:26.200
So calculate the total cost of items in a shopping cart.

11:26.200 --> 11:28.330
So here we are going to define a function.

11:28.330 --> 11:34.990
So I will go ahead and write def calculate underscore total underscore cost.

11:35.140 --> 11:37.330
And here I'm going to give my cart.

11:37.360 --> 11:38.020
Okay.

11:38.050 --> 11:43.120
Now this cart will basically be having a lot of uh you know items over there.

11:43.120 --> 11:46.210
And we can also give in the form of dictionary.

11:46.210 --> 11:51.640
So right now I'm just going to do pass because first of all, I will see what kind of data I have.

11:51.670 --> 11:52.180
Okay.

11:52.180 --> 11:56.710
So let me just go ahead and show you some of the example with respect to cart data.

11:56.770 --> 11:59.170
And since we have already learned about dictionary.

11:59.170 --> 12:05.350
So we will go ahead and write cart is equal to and will create a list and this will be a list of dictionaries.

12:05.380 --> 12:13.480
Okay, so let's say my first item is something like with key value pairs like name is equal to uh let's

12:13.480 --> 12:14.860
say I have Apple.

12:15.160 --> 12:15.670
Okay.

12:15.700 --> 12:21.730
Then I have price which is equal to 0.5.

12:22.150 --> 12:26.050
Then I have specifically quantity okay.

12:26.320 --> 12:29.770
And this quantity will be equal to four okay.

12:30.130 --> 12:34.330
Uh now I will go ahead and create my next, uh example.

12:34.330 --> 12:37.780
And let's say this point $5.

12:37.780 --> 12:39.220
I have written like that right.

12:39.310 --> 12:40.240
Obviously.

12:40.300 --> 12:43.630
So here let me just go ahead and and name should be banana.

12:43.660 --> 12:44.290
Okay.

12:44.320 --> 12:46.210
Price will be $0.3.

12:46.240 --> 12:50.560
Let's say quantity I'll say six six bananas.

12:50.590 --> 12:51.880
This is the price.

12:51.880 --> 12:56.230
And let me just go ahead and again add one more item over here.

12:56.260 --> 12:56.830
Right.

12:58.300 --> 13:01.880
Now let me just go ahead and write one more like orange.

13:01.910 --> 13:02.510
Okay.

13:02.540 --> 13:06.200
The price will be 0.7 and quantity.

13:06.200 --> 13:07.610
I can write as three.

13:07.640 --> 13:08.090
Okay.

13:08.120 --> 13:10.040
So this is my cart.

13:10.070 --> 13:10.460
Okay.

13:10.490 --> 13:16.160
With respect to all the items that I have, then I will just go ahead and write my function over here.

13:16.160 --> 13:18.770
So let me just go ahead and create a temporary variable.

13:18.800 --> 13:20.360
Total cost is equal to zero.

13:20.420 --> 13:22.880
Now all I have to do is that I have to explore.

13:22.910 --> 13:25.550
I have to probably go through this each and every cart.

13:25.970 --> 13:28.100
Uh, take the price.

13:28.130 --> 13:28.760
Okay.

13:28.790 --> 13:32.870
And see, this price is basically for one quantity.

13:32.900 --> 13:33.170
Okay.

13:33.200 --> 13:36.560
So we will go ahead and multiply with this many number of quantities.

13:36.590 --> 13:37.070
Okay.

13:37.100 --> 13:40.160
And again we have to retrieve the data from this particular dictionary.

13:40.160 --> 13:42.200
And then we will be doing the total sum.

13:42.230 --> 13:42.680
Okay.

13:42.680 --> 13:46.760
So now let me just go ahead and write and write from item in cart.

13:46.760 --> 13:48.530
So I will go ahead and explore this.

13:48.560 --> 13:49.010
Okay.

13:49.100 --> 13:50.750
So I will just go ahead and write.

13:50.780 --> 13:54.830
Total cost is equal to or plus equal to.

13:54.860 --> 13:55.190
Okay.

13:55.220 --> 13:58.460
So just like an increment operation where we are adding something.

13:58.490 --> 14:01.750
Then here I'm going to write item of Price.

14:01.780 --> 14:02.470
Okay.

14:02.500 --> 14:03.820
Item of price.

14:03.820 --> 14:11.200
So this for every item of price we are just going to multiply with the item of quantity.

14:11.230 --> 14:11.770
Okay.

14:11.770 --> 14:14.170
So let me just go ahead and write it down.

14:14.290 --> 14:16.510
Then I'll be getting my total cost.

14:16.870 --> 14:19.810
Then I can go ahead and return the total cost.

14:19.810 --> 14:25.540
So this function is just going to multiply the price with respect to all the quantity for each and every

14:25.540 --> 14:26.050
item.

14:26.050 --> 14:28.960
Now quickly let me just go ahead and call the function.

14:28.960 --> 14:33.160
So here I'm going to write calling the function okay.

14:33.190 --> 14:39.820
And here I will write total underscore cost is equal to calculate total cost.

14:39.820 --> 14:42.640
And here I'm going to give my data as cart.

14:42.730 --> 14:45.760
Now let's go ahead and execute it I'll press shift enter.

14:46.150 --> 14:48.040
And here uh okay.

14:48.040 --> 14:52.780
Let me just go ahead and print this total cost because we have not displayed anything.

14:52.930 --> 14:55.750
Now here you can see 5.889.

14:55.750 --> 14:59.080
So this is my total cost right.

14:59.080 --> 15:01.850
With respect to all the items that I have actually bought.

15:01.940 --> 15:02.390
Okay.

15:02.420 --> 15:04.010
And this is amazing.

15:04.010 --> 15:07.130
I'm able to see the total cost of my entire cart.

15:07.130 --> 15:10.370
So here we have just created a function which is taking the cart.

15:10.370 --> 15:12.590
Items and cart can be given as a dictionary.

15:12.590 --> 15:14.420
List whatever things you really want.

15:14.450 --> 15:17.960
Then we'll iterate through each and everything and we'll calculate the cost.

15:18.080 --> 15:18.800
Perfect.

15:18.800 --> 15:21.320
Uh, let's go ahead and see one more example.

15:21.320 --> 15:26.570
Uh, this time, what we are basically going to do over here is that we are going to check or we are

15:26.570 --> 15:31.910
going to create a function which will be checking if a string is palindrome.

15:31.910 --> 15:33.830
Now what does palindrome basically mean?

15:33.830 --> 15:37.250
So if you just want to understand about palindrome right.

15:37.280 --> 15:43.460
Some of the examples let's say if I use this string a b a, if you read it from the front direction,

15:43.970 --> 15:44.870
it is ABBA.

15:44.900 --> 15:48.230
If you read it from the backward direction, it is again ABBA only, right?

15:48.230 --> 15:50.900
So from front and back both are same.

15:50.930 --> 15:56.090
Okay, so we are going to basically write a function which will be able to do the check for any string

15:56.090 --> 16:01.340
from whether from the front it is equal and from the back it is equal with respect to the characters

16:01.340 --> 16:02.670
that we have, it should be safe.

16:02.700 --> 16:05.790
Okay, so here let us go ahead and define a function.

16:05.790 --> 16:08.430
So I'm going to write is underscore palindrome.

16:08.430 --> 16:11.370
So I'll just go ahead and write.

16:11.370 --> 16:13.530
My function is underscore palindrome.

16:13.620 --> 16:16.410
And here I'm going to give my string okay.

16:16.680 --> 16:23.070
Now the best way to do is that whatever string I write right between all the spaces.

16:23.070 --> 16:24.690
First of all I should remove it.

16:25.020 --> 16:26.190
So I'll take the string.

16:26.190 --> 16:30.390
I'll write s dot lower because I have to also do the lowering of all the characters.

16:30.390 --> 16:34.020
And let me just replace all the empty string.

16:34.050 --> 16:34.680
Okay.

16:34.710 --> 16:36.930
With a string, right?

16:36.960 --> 16:39.720
With like I should just remove that empty spaces.

16:40.440 --> 16:47.670
Now, the best way to do this is that I hope everybody knows in string we have discussed about, uh,

16:47.730 --> 16:49.800
you know, slicing operations and all.

16:49.800 --> 16:51.810
So I will just go ahead and write colon.

16:51.810 --> 16:53.070
Colon minus one.

16:53.100 --> 16:53.610
Okay.

16:53.640 --> 17:01.470
Now, if this string is, uh, if I see this is what I'm getting the reverse of a string.

17:01.480 --> 17:01.990
right?

17:02.020 --> 17:06.940
If I'm writing this particular code, I just need to compare this with my s, right?

17:06.970 --> 17:11.410
So if I go ahead and write s double equal to, this will either give true or false.

17:11.410 --> 17:15.190
This is basically the reverse of this specific string itself, right?

17:15.220 --> 17:19.540
So if both of them are equal then we can definitely say that it is a palindrome.

17:19.570 --> 17:24.400
Now let us go ahead and see here, uh, I will go ahead and call this palindrome.

17:24.400 --> 17:26.770
Now I in in Google.

17:26.770 --> 17:29.710
Also you can search for different different uh text.

17:29.740 --> 17:30.310
Right.

17:30.310 --> 17:39.730
So I will use a sentence which is called as a man a plan a canal Panama.

17:39.760 --> 17:40.180
Okay.

17:40.210 --> 17:42.310
Now if you read from here.

17:42.340 --> 17:42.820
Right.

17:42.820 --> 17:45.460
And if you read from the reverse, it is almost same.

17:45.490 --> 17:46.660
See a man.

17:46.690 --> 17:46.990
Right.

17:46.990 --> 17:49.120
So if it is starting from a man.

17:49.150 --> 17:54.400
So here also from backwards, if I read a man is over here, right.

17:54.400 --> 17:55.870
Then again a plan.

17:55.900 --> 17:58.480
So A is over here and plan is still here right.

17:58.480 --> 18:01.240
So I if you read this particular sentence it is reverse.

18:01.270 --> 18:03.020
Again it is almost same, right?

18:03.050 --> 18:04.670
So let's go ahead and print this.

18:04.670 --> 18:09.980
And next one I will just go ahead and take one sentence which is not palindrome.

18:09.980 --> 18:11.720
So let me just go ahead and write hello.

18:11.720 --> 18:17.150
So in hello case I don't have the same sentence from forward or backward right.

18:17.180 --> 18:17.900
It is not equal.

18:17.900 --> 18:21.350
So first one should be giving me true and one should be giving me false.

18:21.350 --> 18:22.880
So here you can see it is true.

18:22.880 --> 18:24.920
And the second one is basically false.

18:24.920 --> 18:28.880
So this is one amazing example with respect to palindrome.

18:28.880 --> 18:34.520
And just by using this string operation slicing operation how easily you are able to do it.

18:34.550 --> 18:35.060
Okay.

18:35.090 --> 18:40.760
Now the next example that we are basically going to see is something called as factorial of a number

18:40.760 --> 18:41.570
using recursion.

18:41.570 --> 18:42.710
Now what is recursion?

18:42.740 --> 18:44.540
Recursion is nothing but I.

18:44.570 --> 18:49.520
If I create a function, that function inside, the body of that function will be called again and again.

18:49.550 --> 18:53.900
Okay, and if I talk about factorials, let's say I want to calculate five factorial.

18:53.900 --> 18:59.420
So it is nothing but five into four into three into two into one.

18:59.420 --> 18:59.690
Right.

18:59.690 --> 19:02.670
So this basically is the operation of any factorials.

19:02.700 --> 19:03.060
Right.

19:03.060 --> 19:06.210
So I have to write a function which will be factorial.

19:06.210 --> 19:09.300
And let's say I have given I want a factorial for five.

19:09.330 --> 19:09.930
Right.

19:09.930 --> 19:16.800
So first condition will be that if n is equal to zero right then what I need to do, I just need to

19:16.800 --> 19:17.460
return one.

19:17.490 --> 19:17.700
Right.

19:17.700 --> 19:20.160
Because five into four into three into one right.

19:20.160 --> 19:21.450
So three into two into one.

19:21.450 --> 19:22.470
It should be like that.

19:22.590 --> 19:24.780
If I'm starting from five it should end till one.

19:24.780 --> 19:28.200
So if n becomes zero I need to just return one okay.

19:28.230 --> 19:36.660
Else I will go ahead and just use one simple return n multiplied by factorial n minus one.

19:36.690 --> 19:37.380
Right?

19:37.380 --> 19:41.490
So here I am calling this particular function right.

19:41.490 --> 19:43.290
So initially if I give five right.

19:43.290 --> 19:44.340
So what will happen.

19:44.340 --> 19:45.630
This condition will become false.

19:45.630 --> 19:49.500
So it will just go ahead and write five multiplied by factorial of four.

19:49.530 --> 19:51.300
Then again factorial of four.

19:51.300 --> 19:56.160
When it is called it will be four into factorial of three, then three into factorial of two, then

19:56.160 --> 19:59.430
two into factorial of one, then one into factorial of zero.

19:59.430 --> 20:02.630
When zero comes, return one, so it will be multiplied by one, right?

20:02.660 --> 20:06.920
So now if I just go ahead and call the function, it will be nothing.

20:06.920 --> 20:10.970
But I can just go ahead and write factorial with respect to phi.

20:10.970 --> 20:13.340
And here you can see this is my output.

20:13.340 --> 20:16.340
If I want factorial for six this is the output.

20:16.370 --> 20:18.680
Here you can see that I'm able to get it right.

20:18.710 --> 20:21.410
And this is what is called as recursion and recursion.

20:21.410 --> 20:26.720
Inside a function you're calling the same function unless and until based on this condition it will

20:26.720 --> 20:27.830
keep on executing.

20:27.860 --> 20:28.370
Okay.

20:29.210 --> 20:34.670
Uh, so there are two more examples of function to read a file and count the frequency of the word.

20:34.700 --> 20:39.560
Okay, I know I have not discussed much about file operation yet.

20:39.590 --> 20:40.100
Okay.

20:40.100 --> 20:44.600
But I really want to show you like, let's say that I have one file over here.

20:44.630 --> 20:47.660
Sample text I have written two line of code.

20:47.660 --> 20:48.350
Hello, world.

20:48.350 --> 20:48.800
How are you?

20:48.800 --> 20:49.580
My name is Krish.

20:49.610 --> 20:50.150
Okay.

20:50.480 --> 20:55.370
Uh, as we go ahead, we'll be discussing more about, uh, reading file and doing file operation.

20:55.370 --> 20:59.780
But here I just want to give you an example, like how we can create a function to this.

20:59.810 --> 21:01.130
Do this task okay.

21:01.130 --> 21:02.730
So first of all, what I'll do.

21:02.940 --> 21:05.010
So my task is very simple over here.

21:05.160 --> 21:08.310
I will try to read a file and count the frequency of each word.

21:08.340 --> 21:08.700
Right.

21:08.730 --> 21:10.530
So how many times each word is present?

21:10.560 --> 21:11.070
Okay.

21:11.070 --> 21:19.050
So quickly we will go ahead and write definition count underscore word underscore frequency okay.

21:19.380 --> 21:22.530
And now I'm just going to give my file path.

21:23.160 --> 21:27.570
Uh the best way to read file path is by using with open function.

21:27.570 --> 21:32.640
Open function is there in Python where you can specifically use and open any kind of file.

21:33.330 --> 21:35.520
So I will go ahead and write word underscore count.

21:35.520 --> 21:38.880
And I know that since I need to count the frequency of each word.

21:38.880 --> 21:40.650
So I need a dictionary over here.

21:40.650 --> 21:42.330
So this will be an empty dictionary.

21:42.750 --> 21:47.130
Now I'll go ahead and write with open my file underscore path.

21:47.130 --> 21:49.680
And this will open in my read mode.

21:49.710 --> 21:50.400
Okay.

21:50.940 --> 21:53.430
So for reading mode I will give it in string okay.

21:53.460 --> 21:57.810
And there is also like write mode and all which we will be discussing as we go ahead in the upcoming

21:57.810 --> 21:58.470
sessions.

21:58.470 --> 22:01.170
And then we will go ahead and write as file.

22:01.200 --> 22:04.390
Then we will go ahead and write for line in file.

22:04.420 --> 22:05.050
Okay.

22:05.170 --> 22:11.680
And we are just going to write words is equal to line dot split okay.

22:12.040 --> 22:13.360
Line dot split.

22:13.870 --> 22:17.680
So we are splitting it based on new line characters.

22:17.680 --> 22:22.540
We are going through this particular file right with respect to that.

22:22.570 --> 22:22.990
Okay.

22:23.020 --> 22:28.240
Now once I get the words right, once we by using this particular line of code, we will be getting

22:28.240 --> 22:34.420
words because every space it will try to calculate it will split it and it will retrieve all these words.

22:34.450 --> 22:34.960
Okay.

22:34.990 --> 22:40.960
Now once we get the words, what I will do is that I will go ahead and write for words in word.

22:40.990 --> 22:41.620
Okay.

22:41.650 --> 22:48.700
And here I'm going to basically write word is equal to word dot lower.

22:48.730 --> 22:49.030
Okay.

22:49.060 --> 22:50.890
So I'm going to do the lower operation.

22:50.890 --> 22:55.930
First of all I really need to make small uh for all the words okay.

22:55.960 --> 23:01.630
Then uh, let's say I want to probably do dot strip operation.

23:01.880 --> 23:02.240
Okay.

23:02.270 --> 23:05.930
And if you don't know about dot strip, uh, you'll be able to understand.

23:05.930 --> 23:12.500
Whenever I give any specific words, it should be able to split based on any kind of characters because

23:12.500 --> 23:13.790
I need each and every word.

23:13.790 --> 23:19.100
So I'll be using some special characters like colon slash, exclamation, everything.

23:19.130 --> 23:22.280
I'll be using this even, uh, like it should.

23:22.310 --> 23:24.590
It should also split h e also separately.

23:24.620 --> 23:24.830
Right.

23:24.830 --> 23:26.600
So that is the reason I've given this also.

23:26.600 --> 23:31.970
And if there is exclamation everything, I'll try to strip it and I will try to get the word okay.

23:32.000 --> 23:38.270
Then I will just go ahead and write word of count and I will take that specific word okay.

23:38.300 --> 23:40.190
Please focus on the logic okay.

23:40.220 --> 23:41.810
Logic is super important.

23:41.810 --> 23:44.690
And logic, you become better when you keep on practicing.

23:44.690 --> 23:48.800
Then I'm going to write word underscore count dot get.

23:48.890 --> 23:55.640
And here I'm going to write word comma zero okay word comma zero.

23:55.640 --> 23:59.330
And then we are going to add it with one okay.

23:59.360 --> 24:06.000
So I'm using this get function which is available in the in the dictionary, as I have already shown

24:06.000 --> 24:06.330
you.

24:06.330 --> 24:08.880
And I will go and search for this specific word.

24:08.910 --> 24:09.510
Okay.

24:09.930 --> 24:12.990
Uh, if it is not present, then the value will be assigned to zero.

24:13.020 --> 24:15.000
Otherwise we are just going to increment with one.

24:15.030 --> 24:21.120
And we have already seen Dot get in our previous, uh videos when we discussed about dictionaries.

24:21.150 --> 24:24.990
And finally when once it does it, I'm just going to return my word count.

24:24.990 --> 24:29.580
And it needs to be with respect to this particular width right in the same line.

24:29.640 --> 24:33.150
Now let me just go ahead and call the function here.

24:33.150 --> 24:36.360
First of all I need to give my file path okay.

24:36.390 --> 24:37.830
File path is very simple.

24:37.830 --> 24:40.830
In the same working location I have sample dot txt file.

24:40.860 --> 24:42.150
So this is the same file.

24:42.150 --> 24:46.920
It is present in my uh same folder location that is functions.

24:46.920 --> 24:49.800
So I'm just going to go ahead and call this.

24:49.800 --> 24:51.990
Then I'm going to use word underscore.

24:51.990 --> 24:57.540
Frequency is equal to count underscore word underscore frequency.

24:57.540 --> 25:01.260
And then I'm just going to give the file path okay.

25:01.260 --> 25:03.490
So this will basically be my file path.

25:03.520 --> 25:08.380
Now after this, I will just go ahead and print the word frequency.

25:08.410 --> 25:10.060
Now see each and every thing is done.

25:10.150 --> 25:15.190
Let me execute and always make sure guys indentation is the main key thing many people may face problem

25:15.190 --> 25:16.720
if your indentation is not correct.

25:16.750 --> 25:22.750
Okay, so now if I go ahead and execute it cannot access local variable word where it is not associated

25:22.750 --> 25:23.560
with a value.

25:23.560 --> 25:27.070
So let's see where is the error.

25:27.070 --> 25:31.870
So word frequency of word uh frequency of file path.

25:32.110 --> 25:38.740
Um if I'm going inside this particular function inside this it is going to for words in word okay.

25:38.920 --> 25:39.370
Uh okay.

25:39.370 --> 25:40.570
It should be opposite.

25:40.600 --> 25:41.110
Okay.

25:41.140 --> 25:44.800
So I will just go ahead and write this because I have to access words.

25:44.800 --> 25:46.540
So here you can see hello is present.

25:46.540 --> 25:47.920
One word is present.

25:47.950 --> 25:49.240
Everything is present one.

25:49.270 --> 25:49.480
Okay.

25:49.510 --> 25:53.050
So let me just go ahead and save it and let me write again.

25:53.050 --> 25:53.890
Crush.

25:54.460 --> 25:54.700
Okay.

25:54.730 --> 25:56.080
So crush is present two times.

25:56.080 --> 25:59.140
So if I go ahead and execute I should be getting crush two times.

25:59.140 --> 26:05.160
So here is crush okay a simple example of a function to read a file and count the frequency of each

26:05.160 --> 26:05.490
word.

26:05.520 --> 26:09.540
So here we have taken a sample file, and we have read it in the read mode.

26:09.540 --> 26:11.460
And we have counted each and every word.

26:11.490 --> 26:15.030
Amazing function again and I hope you are learning many things out of it.

26:15.060 --> 26:18.540
Okay, more about the file operations we will be discussing.

26:18.540 --> 26:22.710
There is a separate section that will be coming up as we go ahead.

26:23.460 --> 26:25.590
Now final task.

26:25.650 --> 26:26.700
This is for you.

26:27.120 --> 26:29.580
Um, anyhow, I'll be giving you the code.

26:29.610 --> 26:33.030
Okay, so here, the question is very simple.

26:33.060 --> 26:34.800
You need to validate the email address.

26:34.830 --> 26:35.430
Okay.

26:35.430 --> 26:41.640
So let's say that you are there is a subscription, uh, emails that you really want to get from a company.

26:41.640 --> 26:46.590
Let's say there is a page and people will go ahead and write any email and just click on start subscription,

26:46.620 --> 26:49.440
right if they want the free materials or anything as such.

26:49.710 --> 26:53.790
Uh, now the problem is that someone may not put a valid email address.

26:53.790 --> 26:56.070
So we also need to do the valid email check.

26:56.070 --> 27:00.210
So for this I will already give you this particular code okay.

27:00.210 --> 27:04.180
And you can go ahead and try it out and just try to understand it.

27:04.210 --> 27:06.790
Here I've used every special characters okay.

27:06.820 --> 27:09.400
It should it should be matching this pattern.

27:09.400 --> 27:09.820
Right.

27:09.850 --> 27:16.240
Like let's say one of my email address is some username at the rate it can be gmail.com.

27:16.270 --> 27:16.540
Right.

27:16.540 --> 27:19.750
So similarly this is my first regular expression right.

27:19.750 --> 27:26.560
It can be between A to z capital E to z 0 to 9 plus uh then again add that it will be there again.

27:26.560 --> 27:30.610
You can have Gmail, you can have Hotmail, you can have Yahoo anything as such.

27:30.610 --> 27:32.170
And then finally.com.

27:32.170 --> 27:32.530
Right.

27:32.530 --> 27:39.220
So this will be the entire, uh, matching uh for the regular expression I will talk more about regular

27:39.220 --> 27:40.930
expression as I go ahead.

27:40.960 --> 27:43.660
Now if I go ahead and check this you will be able to see it.

27:43.690 --> 27:46.240
Here you can see this is my true email.

27:46.270 --> 27:47.440
And this is a valid email.

27:47.440 --> 27:48.370
This is not a valid email.

27:48.370 --> 27:50.560
So just go through this and check it out.

27:50.560 --> 27:55.480
Uh the reason why I'm including some new new things so that you get excited by just seeing all this

27:55.480 --> 27:56.710
kind of application.

27:56.710 --> 27:59.440
So I hope you like this particular video.

27:59.470 --> 28:01.090
Uh, this was it from my side.

28:01.090 --> 28:02.680
I will see you in the next video.

28:02.680 --> 28:03.070
Thank you.
