Hello guys. Welcome to LearnVern. Last topic we saw was the Dictionary.
How we used Dictionary in Python as well as its multiple functions.
Today we will see Functions where we will learn all about what we will do with functions, how to use Functions, how to create them, what is the theory & practical..
We are going to see all of it today in this video.
Firstly we will see what is the meaning of Function.
Function is a block of code in which the code is written & it only runs when it is called.
If you don’t call it, the code will not run it.
Any code written in function is also used for reusability under Python.
Meaning if you’ve written any code & you want to use it in future, you can create a function out of it and write the code in function.
For eg: you wrote a code of 2 + 2. Now if you want to reuse the code, you won’t need to write it again...you will just call the function in which you’ve written the code.
Function starts with the Def keyword.
The Def keyword is used for definition.
Now we will see Types of Functions.
1 is a built-in function which means the predefined functions which are available in Python for us to use.
Another is User-defined function.
User-defined function is the function in which the user will make a function for himself according to his requirement.
Last is the Anonymous function.
Anonymous function goes by its name. There is no name of this function, it is only a one line function in Python.
First of all we will see what are inbuilt functions.
Inbuilt functions are the ones which are already created in Python.
Such as the Print function. Print function is used to Print any values.
Next is the range function which helps in finding out any value’s range.
In the previous videos if you remember we had used range in For Loop method.
We had rotated a loop according to the range. That is how the range method is used.
Len method. Len method is used to know any particular thing’s length.
Suppose we want to know the value of a list, we will use the Len function.
Next is the Round Off function. What does Round off function do?
It rounds off any value. Meaning if the value is 2.5, it’ll make it 3, 3.5 will be 4.
Maximum & Minimum. These are used to find out Maximum & Minimum value.
There are many inbuilt functions in Python.
Now it’s time for the User Defined function.
User Defined function is created by the user itself according to his requirement.
It is made through the Def function. And this function works only & only if it is called.
Now what can be done with function?
We will see how to create a function, how to call it, how to pass an argument in function, how to use an inbuilt function etc.
We will see all of that practically now. Moving onto the practicals.
The Jupter is open. I’ll mark down our inbuilt functions.
Our first inbuilt function is Print.
I’ll take print here & write Hello World.
This is our Print function which is inbuilt & is used to print the values.
Next is the range function.
How to use the range function?
I’ll take For I in range. We rotated it from 1 to 9.
Then we got I printed.
You can see it printed all the values from 1 to 8.
The For Loop worked according to the range function.
Now we will see the Round off function.
Let’s see how Round function is used.
Enter a value here in our round function. Like I entered 53.7865.
And I want 2 digits after round off. So I wrote 2.
I want 2 digits after the point.
Now if I print X, it comes out rounding off by keeping 2 digits after the point.
This is how the inbuilt functions are used in Python.
Moving forward we will see how we will use the User Defined functions.
We will markdown User Defined function first of all.
First off...how to create a function.
You have to simply use the Def keyword.
Def. your function name… suppose I wrote my function.
And I wrote in body, print hello.
Your function is created.
Now when I run the function, it wont run it.
Why? Because we did not call that function.
I had told you that whenever you make a function, it is compulsory to call the function otherwise your function won’t work..
Now how will we call the function?
I’ll write here, calling a function.
Write your function’s name that is myfunction.
And as soon as you run it, you can see that function is called here.
Now how to pass any argument in any function.
Function with one argument.
Let’s see how to pass an argument in function.
I’ll write here...creating a function.
Def my function.
Now I want to pass an argument in the function.
Where will I pass it? Here in the round brackets.
Suppose I passed an argument A.
We will print the value which was passed under A.
Now I’ll write about calling a function.
My function’s name which is myfunction.
Now I want to pass an argument, suppose 10.
I’ll write down passing argument as a 10.
Now when I run it, our code will print our value which is inside the function.
10 passed A here & then it went inside & printed it.
This is how you pass 1 argument in function.
Moving forward, let’s check how to pass multiple arguments in function.
First of all we will create a function.
I’ll write creating a function.
We have to make a function using the Def keyword only.
I’ll write my function’s name & here multiple arguments.
Multiple arguments meaning I’ll Print it here like this.
A and then B. I’ll call both of the values in this function.
As soon as this function is called, both the values will be printed.
Now I’ll call this function. Calling a function.
Our function’s name, myfunction. In which we will pass 2 arguments 10, 20.
I’ll write here passing 2 arguments.
Then as soon as you run it, 10 got assigned to A and 20 got assigned to B.
This is how you can pass multiple arguments into a function.
I hope you are understanding so far.
Now I want to pass arguments according to keywords in the function.
I’ll write here Keywords Arguments.
How to do it?
I’ll write here once again. Creating a function .
I’ll take a Def keyword here. My function’s name which is myfunction
Suppose I wrote here child 3, child 2, child 1.
I passed 3 arguments here. Print Youngest child is…
Whichever child suits here the most. Suppose I wrote child 3.
I added a keyword afterwards here.
Now what will we do?
When I am getting my function called, calling a function.
Our function’s name is myfunction.
Now I’ll pass the keywords we’ve passed in our argument.
Like I passed Ritesh in child 1. Child 2 will be Ram & child 3 is Geeta.
I passed the 3 names and now I’ll run it & check what is the outcome.
After running it, you can see the keyword we printed was printed here.
If I’ll write child 2 here then it will be changed to Ram.
Child 1 then Ritesh.
So this is how you can call arguments through particular keywords.
This is how keywords work under function.
Moving ahead. I’ll pass a default parameter in a function.
Default parameter value.
Meaning I not only passed a parameter but also assigned its by default value.
First of all, creating a function. Def myfunction.
In which I took state as an argument & passed Gujarat in it.
Now I’ll print the body that I am from.. And then wrote + state.
Which means whichever state I’ve written here will be automatically added.
Next I’ll call my function multiple times.
Calling a function.
Myfunction. I printed Rajasthan here.
In the same way I printed Punjab.
Again in the same way, I printed Uttar Pradesh.
But in the last one, I didn’t get anything printed.
I didn’t pass anything as an argument.
The one where I passed, it replaced this value by overwriting it.
Rajasthan, Punjab, Uttar Pradesh and where I had not passed any value, it printed Gujarat.
This is how your default parameter works with value in function.
Now how to pass a list in function.
Passing a list into function.
Let’s see how to do that.
Creating a function. I made a function named Def myfunction.
In which I passed Fruit which is my argument.
Now I want to iterate whichever fruit I get.
I’ll take a For Loop here.
For I in fruit. Then I’ll print I.
It will be For Loop when we call that function.
I have to pass a list and for that I need to bring the list here.
Suppose I took a variable F in which I passed a list Apple, Mango & Orange.
I passed these 3 strings in the list.
And now I want to call this function, my myfunction.
And I’ll pass F here.
I’ll comment here Calling a function.
Now when I run it, you can see Apple, Mango & Orange have been iterated according to our function.
What I did here was I passed my whole list in function.
Now we will see how the function returns the values.
Return values from function.
First of all, we will create a function.
Creating a function. I wrote Def myfunction here.
I passed an argument X here.
Now if I have to return it, I’ll only need to use the return keyword.
Where I wrote 5 multiplied by X.
Whichever argument we will pass, will be returned after getting multiplied by 5.
Let’s see how to do it.
Calling a function. Suppose it is myfunction.
Okay, let’s do one thing. I’ll print as well.
Myfunction. I’ll pass 3 & call it multiple times in the same way. 3, 4 & 5.
We got the answers 15, 20 & 25.
You can return these values in this way in function.
Understood how to return the values in function?
Next up is the recursion function.
I’ll write down the Recursion function.
First of all, what is the meaning of the Recursion function?
The function which calls itself is called Recursion function.
Let’s see how to use it.
Creating a function.
I made a function named Def factorial in which I passed an argument called X.
Now I want to check if X == 1, 1 doesn’t have any factorial so it will return 1.
It will pass 1.
But… else the value of X is not 1, then it will return X multiplied by factorial.
This is our function's name.
I’ll write here calling recursion function.
We used the function which we made ourselves. And X -1.
Whatever is X’s value, suppose it is 5 then firstly it gets multiplied and then X - 1.
Then the function got called again and it continued getting called until the value of X was (minus equal to) - =1.
What did we do at last? We took a user input num in which the input should only be integers.
I asked for user input and I’ll write a simple message here that...
The factorial of num whose input we took, we will call our function here, the factorial function we made, and passed that num. Alright.
Now we will run that num and I’ll write 5.
120 should be 5’s factorial. Run. And see the factorial of 5 is 120.
So what happened here? The function only called itself.
The recursion took place until the didn’t become X ==1.
In recursion, the function only calls itself.
Moving ahead to our last function that is the Anonymous function.
Anonymous function can also be called a Lambda function.
This function does not have any name.
Any function which does not have a name can be called Anonymous.
Let’s see how to create & how to use it.
Creating a function.
This function is called a one line function.
X = lambda. Did you see that as soon as we wrote lambda, it turned green which means it is a predefined keyword which is already available.
Afterwards we have to pass an argument
I passed an argument as A. and I wrote A + 10.
Now when I call it, print X. and I passed an argument.
It gave me 15.
We created a lambda function with 1 argument.
In the same way, lambda function with multiple arguments.
I took the X variable again. No, let’s change the variable to Y.
Took lambda, that is our keyword. Passed an argument here A, B.
And passed the expression A multiplied by B.
Now we will print them. Y and 5 multiplied by 10.
We got the answer 50.
So this is how the Anonymous function that is our Lambda function is used.
Moving forward in function.
We shall see how to use Global variable & Local variable.
Let’s start with the Global variable.
What does the Global variable mean?
It means that any variable is accessible from anywhere in the entire program.
It means the variable can be used anywhere in the program.
Firstly we will see how to use the Global variable.
I took a variable Z = 25. This is my global variable.
I have presented it outside only.
Now I’ll create a function named myfunction.
I made the variable Z into a Global Z.
That means that the Z can be used in this function as well.
I’ll print Z. And call myfunction which is our function’s name.
It printed 25.
We printed Z inside the function, even then it is working.
Now I’ll initialise a new value to Z.
And print Z afterwards.
It can access the values inside the function as well as the values outside the function.
Which proves that this Z is a Global variable & can be used anywhere in the whole cell.
In the same way, there is a Local variable.
What is the Local variable? The variable which can be used in its scope.
Suppose I used it in function, then it will work only & only for function.
We won’t be able to use it outside the function.
Let’s see how. I made a function here called sum in which I wrote X & Y parameters.
I took a variable, sum and I initialised X + Y.
Now I’ll return the sum & print - sum, passing 2 values.
And it printed 15 here.
But now when I want to access the X, it won’t let me have the access.
Why? Because the variable X is a Local variable.
Meaning X will function only under which it was used.
So what did we learn today? We learnt Inbuilt functions, USer Defined function, Function with 1 Argument, Multiple Arguments into Function, Keyword Argument, Default Parameter Value, Passing a list into function, Return values from function, Recursion function, Anonymous function, Global & Local variables.
You can apply all of these things in function.
In the next video, we will learn about the Random module.
See you in the next video everyone. Thank you.
Share a personalized message with your friends.