Hello guys.
So we are going to continue the discussion with respect to Python.
And in this video we are going to discuss about the most important module that is called as functions.
So what is the entire video outline?
In this video I will be discussing about what exactly is functions.
Why do we require functions?
Along with that, we'll also be seeing about uh, calling a function.
Defining a function.
How do we define it.
What is the syntax of it.
Then along with that we'll be discussing about functions parameter default parameters.
Then what is variable length arguments.
You know.
So there are some very important arguments like positional argument and keyword argument will be discussing
about that.
Then we will also be discussing about return statement.
So we will be covering each and every thing uh, with the help of again code with the help of Python.
Yeah.
Each and every thing will try to write it down with multiple examples.
And, uh, I'll be making sure that you understand this because this is one of the most important thing,
because whenever we are writing any data science projects or Python projects, we will be definitely
using a lot of functions.
So first of all, let's go ahead and define the function.
A function is a block of code okay.
Again I'm repeating it.
A function is a block of code that performs a specific task.
A functions help in organizing code, reusing code, and improving readability.
Okay, so it is a block of code that performs a specific task.
It helps in organizing code, reusing code, and improving readability.
One of the best syntax if you really want to check it out right.
So this is the syntax.
So here I'm just going to give you the syntax.
This function is an empty function.
I've just written it for the for just to make you understand.
So first of all, a function starts with a definition keyword.
Then you have the function name.
And if any parameters that you are defining.
So we have to define the parameters.
This is doc string.
Uh doc string is just to use to uh, you know, write some kind of comments, what the specific function
does so that any developer will be able to understand it.
Along with that, we will go ahead and write our function body.
Function body is more about all the code that we are going to use inside the function.
And then finally we return some kind of value okay.
It is not necessary that uh return should be there.
Some of the functions may not have return value, but some of the functions will definitely have a lot
of return value.
Okay.
So uh, this is the basic syntax.
Now let me just go ahead and tell or talk about why do we require function okay.
Why functions.
This is important for everybody to understand.
Let's say I am creating a number variable.
And I go ahead and write a code which will be able to check whether a number is even or odd.
So suppose if I go ahead and write if num modulus two or percentile two is double equal to zero, then
obviously I'll say hey, print the number is even right.
So I'm just going to write.
The number is even else.
What I usually do is that in this I will go ahead and print and write.
The number is odd right.
So here what exactly is basically happening.
We are just going to check whether a number is even or odd.
And obviously if I go ahead and execute this condition will be true because the number is even and I'm
going to get the answer, the number is even.
Okay.
Now let's say in my project I want to write this piece of code, and I need to reuse this piece of code
in multiple places, because in multiple places I'm going to check whether a number is odd or even right
now, in this case, what I have to do, I have to copy the same code, use it in multiple files instead
of this.
A better way of reusing the entire code will be with the help of functions.
Now what?
I will do this entire code I will write inside a function.
Now the best way to write is that let's say I will go ahead and create my definition keyword.
I will say hey, my function name is even or odd, okay?
And this will specifically take a parameter which is called as number.
And now I will go ahead and write my condition inside this.
Right.
So as soon as I go ahead and press enter, that basically means I'm inside my function.
Right?
So automatically the indentation goes inside this function block.
Right now the first thing is that I can go ahead and write my doc string.
The doc string will be like, hey, this function performs, um, finds even or odd okay, even or odd.
So this is what I'm specifically writing over here.
So this becomes my dog string.
It is not compulsory that you really need to write a dog string.
You can leave it completely empty.
Uh, and you may also not even write any of these things, but it is a good practice that we should
definitely write some information about the function itself.
Uh, now I will go ahead and write my code inside this.
Now I will go ahead and write.
If num num is nothing but the same, uh, parameter that I'm actually getting.
And this is basically my parameter.
If num percentile two is double equal to zero, I will go ahead and print the.
The the number is even or I will go ahead and write else print.
The number is odd.
Right.
So here you can see I've defined this entire function.
Now I'll go just go ahead and press it and execute it.
Right.
So as soon as I execute it I have actually created my first function.
Right.
And this is amazing.
And if you have also created it, always make sure the indentation is managed.
If the indentation is right then everything is going to work fine.
Okay, let's see if I miss out the indentation I executed.
I will be getting an error.
It will be saying hey, expected an independent block after the if statement and this is what we have
actually done okay.
So I'll just say Ctrl Z.
Yeah.
Now we are fine.
I'll go ahead and create my function even or odd.
Now in order to call this function.
So let's call this function now wherever I want to use this code to find.
Even or odd, I will just call this function okay.
In order to call this function just go.
Ahead and call the function name.
And here you just need to provide your parameter right.
So let's say the number parameter that I'm giving I want to give it as 24.
Now if I go ahead and execute it here you can see that automatically it is printing the number is even.
Because as soon as I give the parameter it went over here.
It went and checked all this condition.
Whichever is true, it printed it right.
A very simple way of creating a function and calling a function.
And why do we require a function?
Because every way we cannot write the code.
We need to reuse this kind of code.
So whenever you think, okay I have some set of code block which I really need to reuse it, I will
try to create that entire thing inside a function itself, and I'll reuse it.
Right.
So through this, the most important thing will be that it helps in organizing code, reusing code,
and improving readability, improving readability.
Why?
Because we are also writing some amazing docstring over here so that we'll be able to understand, because
in a company, many people will work, many people will write different, different code over there,
multiple modules will be built.
Okay.
So this was a basic example with respect to function.
I've also spoken about a parameter like what exactly is a parameter?
How do you give a parameter in this?
Now there may be scenario that I may also have multiple parameters.
So let's work with multiple parameter.
So here I will go ahead and write function with multiple parameters okay.
And parameters can give integer string whatever things you require.
So I will go ahead and write definition.
And let's say I want to go ahead and create a add function to add two numbers.
So I will go ahead and write definition, add a comma b.
And let's say I go ahead and write my docstring again not compulsory.
I can also leave it like this.
Okay.
Just to show it to you.
That is the reason I'm writing.
Okay.
So I will go back over here, press enter.
So I'm inside this particular code block.
Now I know that I have to add two variables right.
So I will create.
One way is that I will create a variable c a plus b okay.
And then I can return this c output from that particular function okay.
Return c plus uh c which is nothing but a plus b.
Whatever sum is there, we are just going to return it.
A better way of writing the code.
Instead of writing C, I can directly write return a plus B, right?
So here what we are doing.
We just returning the sum of a and b.
Now all I have to do is that create a variable and add call this add function.
Here I'm going to give two comma four.
Now what is going to get what value result is going to get.
As soon as we call this function two comma four it will just return two plus four.
Whatever will be the result like six it will be stored over here.
Now if I go ahead and print the result here, you'll be able to see that I am able to get the six value.
So here you can have any number of parameters and code.
Whatever things you are writing, you can basically use this parameter and write any code that you want.
Okay.
So, uh, it is up to you what kind of function that you are building.
And as we go ahead with multiple examples, if we see in the future and in the upcoming videos will
be writing very complex functions, there is there is something called as decorators lambda function.
Many things will be probably coming one by one.
We will be learning first.
We will start with basics.
Then slowly, slowly will be building the blocks with respect to the learning and then we will be continuing
on.
Okay, now let me just go ahead and show you and talk about one very important thing, which is called
as default parameters.
We can also provide default parameters within our function.
Okay.
Now what exactly is default parameters.
Let's say I create a function okay I create a function saying as definition greed okay.
definition greet.
This function is responsible.
Uh, let's say I give a name over here, right?
Let's say one parameter is name.
Okay.
And here I'm just going to write print.
Okay.
Uh I'll use the f string and say that okay I want to probably, um, write some information.
Let's say I want to say hello.
Hello, whatever the person name is.
Okay.
So I'm going to just go ahead and print this okay.
And this is what is my definition.
Grid rate is just more like a welcoming someone okay.
Welcoming someone.
Now what I will do is that I will just go ahead.
Just go ahead and call this function.
So I will go ahead and write, greet.
And let me say that Krish has entered somewhere.
So I will just give the Krish parameter like let's say you are entering a restaurant and as soon as
you write Krish over there in one of the application and suddenly I will get a message saying that.
Hello Krish.
Okay.
Or welcome something like this.
Welcome to the Paradise.
Let's say this is my message.
Hello, Krish.
Welcome to the Paradise.
So as soon as I give this parameter automatically, this will get replaced over here.
Okay.
Now I may also give one parameter.
I may also give some parameters with a default value.
Okay.
With a default value.
Now what does that particular mean okay.
Let's say I just go over here and this time I don't give Chris okay I'll just try to execute this without
giving any parameter.
So here you can see I will be getting an error.
Hey, greet.
Missing one requirement positional argument.
Okay, we'll talk about what exactly is positional argument.
More about it.
But here you can see that I have given this particular, uh, name parameter over here, but I have
not given any parameter when I'm calling it.
And that is the reason I'm getting this one.
Missing one required positional argument.
Okay.
Now, in order to solve this problem, what I can do, I will say, since I am not giving the name,
I may give a default value to the name.
Let's say if I'm not giving it over here, then what will happen?
It will first of all, go and see whether, uh, the greet name is equal to guest is given or not.
Now, if I go ahead and execute this, see, I'm not giving any parameter but the default parameter,
it will take it as guest.
Okay.
So if I go ahead and execute it here you can see.
Hello guest.
Welcome to the Paradise.
Let's say if I go ahead and give the name okay.
Over here, let's say the guest is not interested to give the name.
So by default it will take it as guest.
But if I'm giving my name, it will just replace.
The guest will get replaced with crush.
Okay, so this is, uh, some good example with respect to the default parameters.
And this is how you can also assign default parameters.
Now we are going to discuss about the main topic which you will be seeing in function.
That is nothing but variable length length arguments okay.
Now in the variable length arguments here we are going to discuss about two things.
One is positional and keyword argument okay.
Okay.
Positional and keyword argument.
Okay.
Keywords.
Arguments.
Okay.
Now let me do let me tell you one very important thing.
Let's say that I have in a function many, many parameters as such.
Okay.
And let me just define one simple function that is called as definition print underscore numbers.
Okay.
And here inside this let's say I have like this okay.
Number one.
Number two.
Number three.
Number four.
Number five.
Like this I have many, many parameters.
In short okay.
It's just not one single parameter I can I am basically having many, many, many many parameters.
Okay.
So what I will do instead of just writing all the parameter names, right?
I will use one very simple positional argument which we denote it as star args.
Okay.
Now it is not necessary.
You need to write star args.
I can go ahead and write star crush also.
Okay.
Uh, but it is always a good practice.
What is followed throughout the entire world?
The same practice we should also do.
Now, in this particular case, I will just show you it.
its star crush.
Then we will go ahead and write with the documentation like what is the best practices?
Argument we specifically use?
Like positional arguments is basically given by star args.
As I said, it need not be a args.
I can also write crush.
Okay, now I will go ahead and go ahead inside this particular function.
Now here what I will do.
As you know, the Star crush will be having multiple parameters.
So I what I will do, I will just go ahead and write for numbers in creche.
Okay.
Which is my argument.
I will go ahead and print numbers okay.
Okay.
I will go ahead and print number okay.
Now see this okay.
So this becomes my function.
Please observe very carefully okay.
Now my print underscore numbers function when I'm calling, let's say I have one comma, two comma,
three comma, four comma, five comma, six comma crush.
It can be anything okay.
So these are all the parameters that I have.
You can see it is a set of parameters.
And this parameters are basically called as positional arguments okay.
Positional arguments one after the other.
Now if I go ahead and execute now see the magic this all the parameters will be referenced by Star Crush.
Okay, so crush will be a variable which will be referring all this particular positional arguments.
Now if I go ahead and execute it now clearly here you can see for all the numbers it is being able to
iterate through this.
Right 1234.
And it is displaying it something like this okay.
Because here I just used one.
So this star crush is basically referring to all the positional arguments, right?
Positional arguments basically means all the arguments that is given over here.
Okay, now, as I always said, the best practice would be I will go ahead and write this and I will
say args.
Okay, so args is a kind of argument positional argument that we basically use.
Okay.
So here I'm going to write positional arguments okay.
Why it is important because I may have many number of arguments over there okay.
So I may directly use this specific thing okay.
Now the other thing will be that okay, this is fine if I go ahead and execute it.
And if I execute the same thing okay, I will be able to get the same answer.
Fine.
Perfect.
123456.
Crash.
Now there is also one more important function which is called as keyword argument.
Okay.
Now what exactly is keyword argument?
Okay.
Um, let me just go ahead and create, uh, one more function.
Okay.
Let's say I will go ahead and write, uh, print uh, sorry.
Definition print underscore detail.
Now how it is different from positional argument you'll be able to understand keyword argument is given
by double star.
And you can write any parameter that you have.
So usually the best uh practice that is basically used is k w a r g s k w basically means keyword argument,
right?
ARGs is a simple positional argument.
Now inside this let me do one thing.
One thing that you really need to remember in keywords argument.
All the keys or all the parameters will be in the form of key value pair.
Okay, again, I'm repeating it in keyword arguments.
All the parameters will be in the form of key value arguments like key value pairs.
Now you know if it is in the form of key value pairs.
That basically means you'll be able to access this like how we access dictionaries.
So I may go ahead and write uh for key comma value in k w a r g s dot items right dot items right.
If I go ahead and execute this now, what will happen if I go ahead and print it so it will be nothing
but f.
Key colon value.
Right.
So I'll just go ahead and execute this.
Now how do we provide a keyword argument.
It's very simple.
Let's say I want to just go ahead and print, uh, these details.
Let's say my first parameter is nothing but name.
Okay.
I have not defined it anywhere, but but during runtime I'm just giving name.
Chris, age 32 and let's say, uh, city or country.
I'll go ahead and write.
Country is nothing but India.
Okay, now, if I go ahead and print all these details so directly, see in if all my values is in the
form of key value pair, where I'm using a key name, I'm using a value name key value key value.
So all these parameters will be referenced by star star kW args okay.
Star star kW args.
If all the parameters are just written like this where it does not have any key name, then this basically
becomes a positional argument.
Okay, now let's combine both of them and let's see how things work right now.
Okay.
So I will just go ahead and write definition.
Okay.
And how do I specify my positional key argument.
Nothing but star args.
So this will be my first parameter.
This will be my second parameter.
Now in my first parameter I will go ahead and write.
And let me just go ahead and display this.
And for ARGs I'm just going to display this values.
Okay.
Let's say I'll not take numbers because I can display anything right.
For val in args print Val okay.
Uh and here also what I'm going to do just to make it much more better, I'm going to use a f string.
And here I'm just going to use this indication.
And I'm going to right over here.
Right.
So what I have done over here for val in args print val okay.
For key comma value in kW a args dot items key.
We are just going to print this.
Now the best way is that I will go ahead and write over here something like positional argument argument
okay.
And this will be my value.
Now see?
Now I'm using both of them.
Right.
Now how this function is able to identify.
Right.
Let's say I'm giving print underscore detail.
Let's say I give my positional arguments like 12345 okay.
And here I will go ahead and write Krish okay.
So this basically becomes my positional all the positional arguments.
Uh, and between them I will just go ahead and give this keyword argument, let's say go ahead and give
it anywhere, anywhere that you want to give.
Okay.
Now out of all these parameters, it will be able to identify okay.
Let's see what is the error that is probably coming over here.
Positional argument cannot uh, appear after keyword argument.
See this is the error.
Now the main problem over here when we write like this right.
First of all always remember we need to specify all my positional key arguments.
Now I will go ahead and write my keyword argument.
Now if I go ahead and execute the details see initially first argument is positional argument second
third fourth till here and this.
All that you'll be able to see is my keyword argument.
So this is very much important to understand the differences between positional and keyword argument.
In keyword argument you have all the parameters in key value pairs, like how we have specified over
here in positional.
You just have uh, something like one, two, three, four, five with respect to only the names that
will be there will not be assigning any key names.
Right.
So this is the most important thing with respect to keyword and positional arguments.
I hope you were able to understand.
I hope you are able to understand the difference between them.
And it is very much important if you know this, because next, uh, in the upcoming session you'll
be seeing, I'll be defining functions where I'll be having multiple parameters over there.
And then we will also be seeing that, okay.
Now, uh, let's talk about the last topic for this video that is called as return statement.
And I hope I've already spoken about it.
In return statement, our function returns something right after the entire statement is executed.
Right.
So let me just go ahead and do it.
So here I am going to create my multiply function.
This will be a comma b.
And here I'll just go ahead and return a multiplied by b.
Right.
So here we are returning some value after the function is getting executed now in order to execute it.
So I will go go ahead and define my multiply function.
Let's go ahead and write two comma three.
And finally I get my output so functions can return multiple values.
Also let's see whether this function will be able to return multiple value or not.
Right.
So let's say I will write c is equal to uh.
Or let me just define this function once again okay.
And let me execute it over here.
So here I will say hey return a comma B comma a.
Okay.
Let's see whether we'll be able to return this.
So in Python a function can also return multiple values.
So here we are returning a comma b comma a a comma b is nothing.
But uh let's say we two comma three have given.
So two comma three is six multiply.
When we are doing it is six and a value is two.
So we can also return multiple statements.
Uh um sorry multiple parameters.
Uh multiple multiple parameters uh, from a function.
Okay.
And that is what we basically use.
We need to use this return variable.
So I hope you are able to understand this.
This was it for my side.
This was all about functions.
In the upcoming session we are going to learn more many amazing things about function.
Uh, there are many things like lambda function, nested function, high order function.
We will be discussing about them.
So yes, this was it.
I will see you all in the next video.
Thank you.