Hello guys.
So we are going to continue the discussion with respect to Python.
And now we are going to see some of the examples already.
In our previous video we saw many topics regarding uh functions.
And uh, there are a lot of advanced topics that are going to come.
But before that I thought, why not create some examples and show you how we can probably solve those.
Right.
So first example over here is that we need to create a function for temperature conversion.
So temperature conversion basically means we need to convert Celsius to Fahrenheit or Fahrenheit to
Celsius.
So how do we define a function?
We'll go ahead and see.
So let's go ahead and see this example.
So first of all I will go ahead and create my function.
Let's say I will write convert underscore temperature.
So this is my function over here.
And here I will give two parameters.
One is temperature and one is the unit that I really want to convert it.
Let's say if I give the unit over here as c, that basically means I need to convert this, uh, this
entire temperature from Celsius to Fahrenheit.
Right?
And if I give the unit as F, right.
So I need to convert the temperature from Fahrenheit to Celsius, right.
So let me just go ahead and give a docstring.
So this function.
Converts temperature between Celsius.
and Fahrenheit.
Okay.
So, uh, this is what is my definition of, uh, this particular function over here.
And, uh, we will go ahead and write our code.
Right.
So here let's say I will go ahead and write if unit is double equal to capital C.
Right.
If I'm giving this particular unit, that basically means I need to convert my temperature from Celsius
to Fahrenheit.
So I'm going to return a temperature multiplied by, and I'll do the operation nine by five.
Okay.
And here let me just give the bracket nine by five.
Uh oh sorry I need to first of all multiply it.
Then I will go ahead and do the addition plus 32.
So this is the formula of converting a Celsius to Fahrenheit with respect to the temperature okay.
So this is the simple formula that we specifically use.
And this in turn will be converting Celsius to Fahrenheit okay.
Foreign Fahrenheit.
Okay.
Then uh, let me go ahead and write Elif.
Okay.
So here we are going to use everything, whatever things you have learned, let's say there are some
examples where I'm going to use dictionaries, some examples where I'm going to use, uh, list and
all.
In this particular case, you can probably see that I'm also using if Elif.
Right.
So if, uh, Elif, the unit is double equal to Fahrenheit.
Okay, then I'm going to basically return.
Okay.
And with for this particular temperature, I need to minus first of all, 32 and then multiply it by
five by nine.
Okay.
And this will basically give us uh, our Fahrenheit to Celsius.
Right.
So this is the formula.
If you go ahead and do the Google search also to convert uh, Celsius to Fahrenheit.
So this is the formula that you usually get.
Okay.
So uh, that's it.
This is what it is right.
And if I don't give any information for our third parameter we need to also handle that.
That is if I'm not giving any unit.
So I will just go ahead and return.
None okay.
Okay.
None.
So done.
So here I'm going to basically use this function.
Now let me go ahead and call this function as convert temperature.
And if I go ahead and write like a value let's say temperature I'm going to give 25.
Right.
And this is in Celsius.
Right.
Uh then I'm going to go ahead and print it.
Right.
And I will be basically getting converted.
We are converting this from 29 degree Celsius to Fahrenheit.
The next one that I'm actually going to do is print.
Okay.
Again, I will go ahead and call my convert temperature.
And here I'm actually going to give 77 comma F.
Right.
And this I will be able to get the answer.
Let's see whether it works.
Yes 25 is nothing but 77 of degree Fahrenheit and 77 degree Fahrenheit is nothing but 25 degree Celsius.
So this is how the conversion is done.
And I feel that formula is absolutely right.
What you have.
Right.
Um, and here you can just see based on units we are able to do it.
So this is our first example.
Um, now what I'm actually going to do is that I'm going to show you one more amazing example, which
is called as password strength checker.
Okay.
Now what exactly is this?
Uh, let's say that we are creating our user ID or login.
And over there we specifically give passwords.
Right?
And it is a and most of the people just write password as 12345.
So we need to have a strong password check okay.
So I'll be giving some of the check over here to just uh, making you understand whether it is a weak
password or a true or a, uh, very strong password.
Right.
So first of all, I will go ahead and write my definition is strong password.
So this function will be responsible in checking whether a password is strong or not okay.
So here you can see I will just go ahead and write my docstring.
This function checks if the password is strong or not.
Mm.
Okay.
Now let me go ahead and write let's say first condition that first criteria I want to put with respect
to the password.
My password should be minimum of eight character.
Right.
So I will go ahead and write if length of if length of password is less than eight is less than eight.
So that basically means I'm going to just say, hey, this is a very bad password in short.
So I'll just go and say false because this, uh, function that I'm actually creating is basically checking
whether a password is strong or not.
So right now the password is not strong.
So I'm returning false.
Then let me just go ahead and write another condition.
If not okay.
So if not is one condition if not any okay.
So here we are just going to see whether this character is having any digit or not.
Right.
Whatever character whatever password we are specifically giving whether it is having any digit or not.
Right.
So what I'm actually going to do, if not, is basically like comparing whether there is not present
or not.
Right.
Something like that.
So I will go ahead and use a for loop for character in password.
So we will go ahead and see each and every character.
And here I will just go ahead and see whether this character is having any digit or not.
Okay.
So each digit is a function that is present in every characters or string.
right?
So I'm going to compare for every character in this specific password, whether it is having a digit
or not.
Right.
If there is not any digits right.
So what I will do I will again return false.
Okay.
Please see carefully what kind of condition I'm using.
Right.
So this is just like a any function or an inbuilt function that is present in Python.
And we are iterating through each and every characters in that password, and we are seeing whether
it is a digit or not.
Right.
And this is digit is a kind of function that is available in strings.
Okay.
So this is one of the check that I really want to do.
Right.
Then I will also be checking with respect to the password is lower.
If all the characters is lower then also we should return it false saying that hey it is not exactly
a good password, right?
So I will go ahead and write.
If not any.
Again I will go ahead and write my next condition.
And here this time I will say hey for cat and password.
And here I'm going to check.
Right.
And what I have to check over here.
Whether my character is lower.
So low is lower is one of the function that is again present in Python.
Sorry for character in password I have to write.
Okay.
Uh, I'm just checking whether the character is lower or not for all the characters that are present
over there.
And if it is present, I'm just going to return the false.
Okay.
So guys, now we have written the condition still here.
We have also checked, uh, like all the characters should not be lowercase.
Okay.
Now, uh, similarly all the characters should also not be, you know, uppercase, you know, and,
uh, we'll also check for that.
Right.
So here I'm going to write if, if not in if not any.
And this time I will be using cat.
Dot is upper right.
So there are a lot of functions with respect to strings which I can specifically use over here.
And let me just go ahead and write for cat in password okay.
And this we will go ahead and check it out and we'll again write return false.
Okay.
Now this is there.
Now let's think of more things, you know.
Uh, let's see for any special characters.
So for special character, uh, I would like to add many special characters that are probably present,
right.
If not any, let's say in our character, if there is no special character like this, then also I can
say that, hey, this is basically a weak password, right?
At least it should have one upper.
You know, at least it should have one digits, you know, so this kind of condition.
So here I've used all special characters over here for character in password.
And then if it is not present we are just going to return false.
If everything satisfies uh, then we will go ahead and return true.
Okay.
So here all return false are there.
And then here return true is there.
Right.
So if all this condition are false, then, uh, if all this condition does not get satisfied, then
we are just going to return true.
Saying that, hey, this is super amazing password and it is a strong password.
Now let me just go ahead and call the function.
So calling the function over here okay.
And uh let me just give some of the things over here.
Right.
So I'll go ahead and write print ease, underscore strong password.
And let me just give one example.
Let's say I have a capital letter.
And I will write a small letter again a capital letter.
And this is V password okay.
But here you can see that I'm not having any special character or uh, other than that I think everything
is getting satisfied.
Right.
And digit is also not there.
So I should definitely be getting false for this.
One more way is that I can go ahead and print okay is strong password.
And let me just go ahead and use some special characters.
So I will go ahead and write str and let me just go ahead and use uh zero over here.
Okay.
And I will be using ng capital P w d and let me use one special character like exclamation.
Okay.
Now let's see whether both of them I think this should basically be giving me false, and this should
be giving me true.
Okay, so now if I execute this here, you can see since I do not have a digit or a special character,
I'm actually getting false here.
I have both uh, a digit and a special character.
So and that is the reason I am also having a capital letter over here.
So that is the reason I'm getting true.
So this is one amazing example with respect to password strength checker.
Just with the help of one function, we are able to do this kind of, uh, validation and we are able
to execute it.
Okay.
Now uh, let's go with the third example.
Now this specific example, I have actually selected, uh, just to make sure to show you like, uh,
how in the real world application this can be used.
So calculate the total cost of items in a shopping cart.
So here we are going to define a function.
So I will go ahead and write def calculate underscore total underscore cost.
And here I'm going to give my cart okay.
Now this cart will basically be having a lot of uh you know items over there.
And we can also give in the form of dictionary.
So right now I'm just going to do pass because first of all I'll see what kind of data I have okay.
So let's just go ahead and show you some of the example with respect to card data.
And since we have already learned about dictionary.
So we will go ahead and write cart is equal to and we'll create a list.
And this will be a list of dictionaries okay.
So let's say my first item is something like with key value pairs like name is equal to uh let's say
I have Apple.
Okay.
Then I have price which is equal to 0.5.
Then I have specifically quantity okay.
And this quantity will be equal to four.
Okay.
Uh now I will go ahead and create my next, uh example.
And let's say this point $5 I will written like that, right?
Obviously.
So here let me just go ahead and and name should be banana.
Okay.
Price will be point $3.
Let's say quantity I'll say six six bananas.
This is the price.
And let me just go ahead and again add one more item over here.
Right.
Now let me just go ahead and write one more like orange.
Okay.
The price will be 0.7 and quantity I can write as three.
Okay.
So this is my cart okay.
With respect to all the items that I have, then I will just go ahead and write my function over here.
So let me just go ahead and create a temporary variable.
Total cost is equal to zero.
Now all I have to do is that I have to explore.
I have to probably go through this each and every cart.
Uh, take the price.
Okay.
And see, uh, this price is basically for one quantity.
Okay.
So we will go ahead and multiply with this many number of quantities okay.
And again we have to retrieve the data from this particular dictionary.
And then we will be doing the total sum okay.
So now let me just go ahead and write and write from item in cart.
So I will go ahead and explore this okay.
So I will just go ahead and write.
Total cost is equal to or plus equal to okay.
So just like an increment operation where we are adding something.
Then here I'm going to write item of price okay.
Item of price.
So this for every item of price we are just going to multiply with the item of quantity okay.
So let me just go ahead and write it down.
Then I'll be getting my total cost.
Uh, then I can go ahead and return the total cost.
So this function is just going to multiply the price with respect to all the quantity for each and every
item.
Now quickly let me just go ahead and call the function.
So here I'm going to write calling the function okay.
And here I will write total underscore cost is equal to calculate total cost.
And here I'm going to give my data as cart.
Now let's go ahead and execute it I'll press shift enter.
And here uh okay.
Let me just go ahead and print this total cost because we have not displayed anything.
Now here you can see 5.889.
So this is my total cost right.
With respect to all the items that I have actually bought okay.
And this is amazing I'm able to see the total cost of my entire cart.
So here we have just created a function which is taking that cart.
Items and cart can be given as a dictionary.
List whatever things you really want.
Then we'll iterate through each and everything and we'll calculate the cost.
Perfect.
Uh, let's go ahead and see one more example.
Uh, this time, what we are basically going to do over here is that we are going to check or we are
going to create a function which will be checking if a string is palindrome.
Now what does palindrome basically mean?
So if you just want to understand about palindrome, write some of the examples.
Let's say if I use this string a b a.
If you read it from the front direction it is a b a.
If you read it from the backward direction, it is again a b a only right?
So from front and back both are same.
Okay.
So we are going to basically write a function which will be able to do the check for any string from
whether from the front it is equal and from the back it is equal with respect to the characters that
we have.
It should be same.
Okay.
So here, uh, let us go ahead and define a function.
So I'm going to write is underscore palindrome.
So I'll just go ahead and write.
My function is underscore palindrome.
And here I'm going to give my string okay.
Okay.
Now the best way to do is that whatever string I write right between all the spaces.
First of all, I should remove it.
So I'll take the string.
I'll write s dot lower because I have to also do the lowering of all the characters.
And let me just replace all the empty string.
Okay.
With a string.
Right?
With like I should just remove that empty spaces.
Now the best way to do this is that I hope everybody knows in string we have, uh, discussed about,
uh, you know, slicing operations and all.
So I will just go ahead and right.
Colon.
Colon minus one.
Okay.
Now, if this string is, uh, if I'm.
See, this is what I'm getting the reverse of a string, right?
If I'm writing this particular code, I just need to compare this with my s, right?
So if I go ahead and write s double equal to, this will either give true or false.
This is basically the reverse of this specific string itself, right?
So if both of them are equal then we can definitely say that it is a palindrome.
Now let us go ahead and see here uh I will go ahead and call this palindrome.
Now I in in Google.
Also you can search for different different uh text.
Right.
So I will use a sentence which is called as a man a plan a canal Panama.
Okay.
Now if you read from here.
Right.
And if you read from the reverse, it is almost same.
See a man.
Right.
So if it is starting from a man.
So here also from backwards, if I read a man is over here right.
Then again a plan.
So A is over here and plan is still here, right?
So I if you read this particular sentence, it is reverse.
Again it is almost same.
Right.
So let's go ahead and print this.
And next one I will just go ahead and take one sentence which is not palindrome.
So let me just go ahead and write hello.
So in hello case I don't have the same sentence from forward or backward right.
It is not equal.
So first one should be giving me true and second one should be giving me false.
So here you can see it is true.
And the second one is basically false.
So this is one amazing example with respect to palindrome.
And just by using this string operation slicing operation how easily you are able to do it.
Okay.
Now the next example that we are basically going to see is something called as factorial of a number
using recursion.
Now what is recursion?
Recursion is nothing but I.
If I create a function, that function inside, the body of that function will be called again and again.
Okay, and if I talk about factorials, let's say I want to calculate five factorial.
So it is nothing but five into four into three into two into one.
Right.
So this basically is the operation of any factorials.
Right.
So I have to write a function which will be factorial.
And let's say I have given I want the factorial for five.
Right.
So first condition will be that if n is equal to zero right then what I need to do, I just need to
return one.
Right.
Because five into four into three into one right.
So three into two into one.
It should be like that.
If I'm starting from five it should end till one.
So if n becomes zero I need to just return one okay.
Else I will go ahead and just use one simple return n multiplied by factorial n minus one.
Right.
So here I am calling this particular function right.
So initially if I give five right.
So what will happen.
This condition will become false.
So it will just go ahead and write five multiplied by factorial of four.
Then again factorial of four.
When it is called it will be four into factorial of three, then three into factorial of two, then
two into factorial of one, then one into factorial of zero.
When zero comes, return one, so it will be multiplied by one, right?
So now if I just go ahead and call the function, it will be nothing.
But I can just go ahead and write factorial with respect to phi.
And here you can see this is my output.
If I want factorial for six this is the output.
Here you can see that I am able to get it right.
And this is what is called as recursion.
In recursion inside a function you are calling the same function.
Unless and until based on this condition it will keep on executing.
Okay.
Uh, so there are two more examples of function to read a file and count the frequency of the word.
Okay, I know I have not discussed much about file operation yet.
Okay, but I really want to show you like, let's say that I have one file over here.
Sample text I have written two line of code.
Hello, world.
How are you?
My name is Krish.
Okay.
Uh, as we go ahead, we'll be discussing more about, uh, reading file and doing file operation.
But here I just want to give you an example, like how we can create a function to this do this task
okay.
So first of all what I'll do.
Uh, so my task is very simple over here I will try to read a file and count the frequency of each word.
Right.
So how many times each word is present?
Okay.
So quickly we will go ahead and write definition count underscore word underscore frequency okay.
And now I'm just going to give my file path.
Uh the best way to read file path is by using with open function.
Open function is there in Python where you can specifically use and open any kind of file.
So I will go ahead and write word underscore count.
And I know that since I need to count the frequency of each word.
So I need a dictionary over here.
So this will be an empty dictionary.
Now I'll go ahead and write with open my file underscore path.
And this will open in my read mode okay.
So for read mode I will give it in string okay.
And there is also like write mode and all which will be discussing as we go ahead in the upcoming sessions.
And then we will go ahead and write as file.
Then we will go ahead and write for line in file.
Okay.
And we are just going to write words is equal to line dot split okay.
Line dot split.
So we are splitting it based on new line characters.
We are uh going through this particular file right with respect to that.
Okay, now, once I get the words right, once we by using this particular line of code, we will be
getting words because every space it will try to calculate it will split it and it will retrieve all
these words.
Okay.
Now once we get the words, what I will do is that I will go ahead and write for words in word.
Okay.
And here I'm going to basically write word is equal to word dot lower.
Okay.
So I'm going to do the lower operation.
First of all I really need to make small uh for all the words.
Okay.
Then uh, let's say I want to probably do dot strip operation.
Okay.
And if you don't know about dot strip, uh, you'll be able to understand.
Whenever I give any specific words, it should be able to split based on any kind of characters because
I need each and every word.
So I'll be using some special characters like colon slash, exclamation, everything.
I'll be using this even, um, like it should.
It should also split h e also separately.
Right.
So that is the reason I've given this also.
And if there is exclamation everything, I'll try to strip it.
And uh, I will try to get the word okay.
Then I will just go ahead and write word of count and I will take that specific word.
Okay.
Please focus on the logic.
Okay?
Logic is super important.
And logic, you become better when you keep on practicing.
Then I'm going to write word underscore count dot get.
And here I'm going to write word comma zero okay word comma zero.
And then we are going to add it with one okay.
So I'm using this get function which is available in the in the dictionary as I have already shown you.
And I'll go and search for this specific word.
Okay.
Uh, if it is not present then the value will be assigned to zero.
Otherwise we are just going to increment with one.
And we have already seen Dot get in our previous, uh videos when we discussed about dictionaries.
And finally when once it does it, I'm just going to return my word count.
I need it needs to be with respect to this particular width, right in the same line.
Now let me just go ahead and call the function here.
First of all I need to give my file path okay.
File path is very simple.
In the same working location I have sample dot txt file.
So this is the same file.
It is present in my uh same folder location that is functions.
So I'm just going to go ahead and call this.
Then I'm going to use word underscore frequency is equal to count underscore word underscore frequency.
And then I'm just going to give the file path okay.
So this will basically be my file path.
Now after this I will just go ahead and print the word frequency I see each and every thing is done.
Let me execute and always make sure guys the indentation is the main key thing many people may face.
Problem if your indentation is not correct okay, so now if I go ahead and execute it uh cannot access
local variable word where it is not associated with a value.
So uh let's see where is the error.
So what frequency of word uh frequency of file path.
Um, if I'm going inside this particular function inside this, it is going to forwards.
Inward.
Okay.
Uh, okay.
It should be opposite.
Okay.
So I'll just go ahead and write this because I have to access words.
So here you can see hello is present.
One word is present.
Everything is present one okay.
So let me just go ahead and save it and let me write again.
Crush okay.
So crush is present two times.
So if I go ahead and execute I should be getting crush two times.
So here is crush Okay, a simple example of a function to read a file and count the frequency of each
word.
So here we have taken a sample file and we have read it uh in the read mode.
And we have counted each and every word.
Amazing function again.
And I hope you are learning many things out of it.
Okay, more about the file operations we will be discussing.
There is a separate section that will be coming up as we go ahead.
Now.
final task.
This is for you.
Um, anyhow, I'll be giving you the code.
Okay, so here the question is very simple.
You need to validate the email address.
Okay.
So let's say that you are there is a subscription uh, emails that you really want to get from a company.
Let's say there is a page and people will go ahead and write any email and just click on start subscription,
right if they want the free materials or anything as such.
Uh, now the problem is that someone may not put a valid email address.
So we also need to do the valid email check.
So for this I will already give you this particular code okay.
And you can go ahead and try it out and just try to understand it here I've used every special characters
okay.
It should it should be matching this pattern.
Right.
Like let's say one of my email address is some username at the rate it can be gmail.com.
Right.
So similarly this is my first regular expression right.
It can be between A to Z capital E to z 0 to 9 plus uh then again add the rate will be there.
Again you can have Gmail.
You can have Hotmail.
You can have Yahoo anything as such.
And then finally.com.
Right.
So this will be the entire, uh, matching uh for the regular expression I will talk more about.
Regular expression, uh, as I go ahead.
Now, if I go ahead and check this, you will be able to see it here.
You can see this is my true email.
Um, this is a valid email.
This is not a valid email.
So just go through this and check it out.
Uh, the reason why I'm including some new, new things so that you get excited by just seeing all this
kind of application.
So I hope, uh, you like this particular video.
Uh, this was it from my side.
I will see you on the next video.
Thank you.