looking at def which statement below is true This is a topic that many people are looking for. bluevelvetrestaurant.com is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, bluevelvetrestaurant.com would like to introduce to you How To Use Functions In Python (Python Tutorial #3). Following along are instructions in the video below:
“Cs dojo its yk here in this video. I m gonna cover what functions are are and how to use them in python and using a function by the end this video. You ll be able to create a bmi calculator similar to the one we created in the last video. But you ll be able to use this one repeatedly for many different people and many different variables.
I m also gonna cover. What comments are and how to use them and as usual. I ll put the outline of this course below. So you don t have to watch the whole thing let me first begin by answering a question.
Though caliche. I think says hello cs dojo. Although i downloaded jupiter on my macbook. I was unable to open your jupiter notebook sample files so if you download a sample file for example from cs dojo dot io python for the last video.
If you just click the file that you just downloaded this one on desktop. You actually won t open it so. What you need to do instead is you need to launch jupiter notebook in any way. You d like to launch.
It i m gonna use anaconda navigator. Here. And once you launch jupiter notebook within the jupiter notebook ui you need to go to wherever you downloaded the file for example desktop and then click the file that you just downloaded within jupiter notebook and that way you ll be able to open the file and they use it and edit it and for this video you can just go to ch dojo dot. Io python 3 to download the sample files now with that out of the way let s dive into our main topic today.
Which is what are functions just like usual. I created a folder called python tutorial. 3. On desktop and a new notebook file called what are functions.
So there are actually a few different ways of looking at functions a function one way to look at it is that it s a collection of a structures or it s a collection of code and notice here that these lines. I wrote here there are not pieces of python code. There s something called comments. So comments are used to comment on the code that we re going to write below.
So you can use it to for example. Explain while variables you re defining and what functions. We re going to define and so on and we can tell that these two lines are comments. Because each of these two lines begin with the pound sign.
Or the hash tag. So a function can be simply. A collection of code let s see an example of that def function one parentheses. Colon print ah print double quotes are two and then print.
This is outside the function. This whole block says def for define so define the following function and then give it a name which we can choose and we re gonna call it function one we can call it any name. We want for example function two one two three or fun fun fun. Let s keep it at function one for now and then parentheses.
So a combination of all of these things are important def. The function name parentheses. And colon 4. Spaces print are and four spaces again print are two and these spaces in front of these two lines are really important just like with if else statements to show that these two lines are part of this function.
If you had for example four spaces here and five spaces or three spaces. It just wouldn t work and as you can see outside of this function. We have a print statement that prints this is outside the function let s see what happens once we load or run this cell. As you can see only this line has been printed this is outside the function.
What happened was this function called function. 1. Has been defined as a collection of code these two lines of code print are and print are two and after that s done this line has been executed so these two lines of code print are and print are two will not be executed will not be run until we use this function or we call as we say this function to use this function or to call this function you can just write function one open parentheses close parentheses. And that s it when you run this cell what s gonna happen is these two lines of code.
We ll be executed for the first time so ah and our two are printed. Here and the nice thing about functions is that you can reuse them over and over again. So you can write function one over here. Again and actually you can even use.
It twice in the same cell. And so when you run this cell function. One is executed twice and these two lines of code are executed twice as well so let s run the cell and let s see what s going to be printed as expected. We see our two twice okay in addition to being a collection of instructions or a collection of code.
A function can also be a mapping. So let s take a look at an example of that def function. Two parenthesis x. Colon.
Return two times x or 2. Star x. This means define a function called function. 2.
Which is going to take the input or an argument and that argument we re going to call it x and in return to whoever call this function. We re going to return two times x. So we re mapping the input x to the output 2 times. X.
Execute this cell and to use this function or to call this function you can write a equals function. 2. Parenthesis. 3.
And this says as an argument use 3 and call this function function. 2. And once this expression is evaluated function. 2 parenthesis.
3 will return 2 times x in this case 2 times. 3. Which is 6 and then that number 6 will be assigned to the variable a and that number 6. Is called a return value or output.
Let s see if this expression. Works by running the cell and by printing a and we should see. 6. And we do ok let s try using this function a few more times if you write b equals function 2 parenthesis.
4 function 2 of parenthesis 4 should return 8 so once we print b we should see. 8 and we do and if you write c. Equals function. 2 of five and then if you print see you should see it 10.
And what if you try to call this function. Without any arguments. So if you write d equals function to parenthesis with nothing inside let s try running it it ll actually give us an error saying function. Two missing.
One required positional argument. X. So. This was an example of a function.
That map s one argument to a return value is it possible to have multiple arguments in a single function. The answer is yes for that you can write def function. 3x comma space. Y.
And then let s write return x. Plus. Y. This means.
Let s define a function called function. 3. And then this function is going to take two arguments x and y. And return x.
Plus. Y. Let s load. This function.
By running the cell. And let s write a equals function. 3. Parenthesis.
1 comma space. So function 3 1. Comma space should be evaluated to 1 plus. 2.
Which is 3 so once we print e we should see. 3. Okay so we saw two different ways of using a function so far the first one was as a set of code or a collection of code and then the second one was as a mapping now it s even possible to combine them both together. Let s see how that works with this function function.
4. Of x 4. Spaces. As usual print x.
Prints. Double quotes. Still in this function. And then return 3 times.
X. Or. 3. Star.
X. This function basically says take the argument. X. And then print x.
And print this string. Still in this function and then return 3 times x. Whoever call this function let s run this cell and let s try calling this function with f equals function. 4.
This is for so what s going to happen here is when this expression is evaluated function for or for we go to this line. So x is printed. So that means for the number four will be printed and then this string. Still in this function will be printed and three times x.
Which is 12 will be returned to this expression. So that will be assigned to f. So once we run this cell. You see that four and steering this function or printed and once you print f you should see.
12. Okay let s see another example of a function deff define function five parentheses sum underscore argument four spaces print some argument and then print we in double quotes let s run the cell to load the function and of course you can call this function with function five. Four let s run the cell and then we see that four is printed because we have some argument being printed and we this string is also printed so one thing to note here is that even though function five is given an argument. We don t have a return value.
It s actually technically possible to say f equals function five of four to assign whatever is returned from function 5 f. But there isn t much of a point because we don t have any return statement here okay. Let s now create a bmi calculator. Here.
Now. Let s say we have three people here. We have a hypothetical person named yk here. Whose height is 2 meters and whose weight is.
90 kilograms and here we have yk. Sister whose height is. 18 meters and whose weight is. 70 kilograms and.
Yk its brother is 25. Meters in height and 160 kilograms in weight. What if we wanted to calculate the bmi for each person and determining if each person is overweight or not we can do this by writing a simple function. Let s call this function bmi and the score calculator and then this function is going to take three arguments named height in meters or height.
Underscore. Em and then weight in kilograms or weight. Underscore kg. In this function.
Right. Bmi weight. Underscore kg. Divided by height and the score m.
Double star. Sign. So this says of course height in meters squared. And what we could do is we could return bmi here.
But let s do something a little bit different here we re gonna write prints. Double quotes bmi and then print bmi so this is going to print the bmi of the given person and then if the bmi is less than 25. This person is not overweight. So we could return not overweight.
But to show this person s name you can just write name plus not overweight. So the assumption here is that name is a string and when you have two strings. You can concatenate them together with name. Plus double quotes.
Not overweight and then let s write else colon return name. Plus is overweight and note. Here that there are eight spaces here. Because this else statement is in the function.
Bmi. Calculator and then this line is in the out statement within the bmi calculator function. So let s run this cell. And then let s write result.
1 equals. Bmi. Calculator parentheses. Name.
One height. Underscore. M1 weight. Underscore kg.
One. So this is for the first person. And let s do the same thing for the second person and the third person let s run this cell. So here we see the bmi for the three people twenty two point five for the first person.
Twenty twenty one point six for the second person and twenty five point six for the third person and to get the result. We can print without one without two and result three and then we see why cane or overweight. Like a sister not overweight. Why kids brother is overweight.
Actually we forgot to put is before not so. Let s fix that go back to the function. And then put is here run this cell again to update. The function and then run this cell below that and then let s try printing the results again okay.
It s fixed so yk is not overweight okay. Sister is not overweight. And why case brother is overweight. Okay now i have a little task for you create a function called let s say convert that converts miles into kilometers.
So you should be able to call your function. Just like this with a function called convert. Which takes mouse as the argument and returns kilometers as a return value and here you can use this formula kilometers equals. One point six times miles okay.
If you want to get the answer to this question just go to cs dojo dot. Io python 3. To download the sample file and you can subscribe to my newsletter by going to ch dojo. Dot io news to make sure you don t miss my future tutorials.
My key from ch dojo and ” ..
Thank you for watching all the articles on the topic How To Use Functions In Python (Python Tutorial #3). All shares of bluevelvetrestaurant.com are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.