Placeholder Image

字幕列表 影片播放

  • Hey, CSOs.

  • Oh, it's like it here in this video, I'm gonna cover what functions are on how to use them in python and using a function.

  • By the end of this video, you'll be able to create a B M I 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 on 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.

  • Colucci, I think says Hello, CSL Joe.

  • 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 C s no, Joe, that I owe slash python to for the last video.

  • If you just click the file that you just don't want it, this one on this stop, it actually won't open it.

  • So what you need to do instead is you need to launch debate a notebook in any way you'd like to launch it.

  • I'm gonna use Anaconda Navigator here on once you launched jibber notebook within the Jupiter notebook.

  • You I you need to go to wherever you downloaded the file, for example, desktop and then click the file that you just don't want it within Jupiter notebook.

  • And that way you'll be able to open the file and they use it on.

  • Edit it on for this video, you can just go to see a soldier that I owe.

  • Slash Python three to download the sample files.

  • Now.

  • With that out of the way, let's dive into our main topic today, which is what our functions.

  • Just like usual.

  • I created a folder called Python Tutorial three on desktop on 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 off structures or it's a collection of coat and notice here that these lies I wrote here, they're 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 viable, is your defining on.

  • Well, functions were going to define.

  • And so and we can tell that these two lines are comments because each of these two lines begin with the pound sign or the hashtag so function can be simply a collection of could.

  • Let's see an example of that deaf functional one parentheses, colon print.

  • Ah, print double coats.

  • Ah too.

  • And then print.

  • This is outside of function.

  • This whole block says death for define so defined the falling 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 some function.

  • 123 or fun, Fun, fun.

  • Let's keep it at function one for now on them parentheses.

  • Colon So a combination off.

  • All of these things are important.

  • Deaf.

  • The function Name Francis on Colon.

  • Four spaces print are on four spaces again.

  • Print.

  • Ah, too of these spaces in front of these two lines are really important, just like with if l statements to show that these two lines are part of this function.

  • If you have, for example, four spaces here on five spaces or three spaces it just wouldn't work on.

  • As you can see, outside of this function, we have a print statement that Prince, this is outside the function.

  • Let's see what happens once we load or run this cell.

  • As you can see on Lee, this line has been printed.

  • This is outside the function.

  • What happened was dysfunction called functional.

  • One has been defined as a collection of code.

  • These two lines of code prints are on print are too.

  • And after that, son, this line has been executed.

  • So these two lines of code prints are on print.

  • Are too will not be executed, will not be run until we use dysfunction.

  • Or we call, as we say, dysfunction, to use dysfunction or to call dysfunction.

  • You can just write function one open parent disease, close parentheses and that's it.

  • When you run this cell, what's gonna happen is these two lines of code will be executed for the first time.

  • So on 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 functional 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 on.

  • Let's see what's gonna be printed.

  • As expected, we see ah to twice.

  • Okay, in addition to being a collection off instructions or a collection of code, a function can also be a mapping.

  • So let's take a look at an example of that deaf function to parentheses.

  • X colon return two times X or two star ex.

  • This means define a function called function to which is going to take the input or an argument on that argument.

  • We're going to call it X.

  • And in return to whoever called dysfunction, we're going to return two times X.

  • So we're mapping the input X to the up, put two times X, execute this cell and to use dysfunction or to call dysfunction, you can write a eco's function, too.

  • Parentheses.

  • Three.

  • And this is as an argument used three on call dysfunction function, too, And once this expression is evaluated, function to fantasise, three will return two Times X in this case two times three, which is six.

  • And then that number six will be assigned to the viable eh?

  • And that number six is called a return value or output.

  • Let's see if this expression works by running to sell on by printing A and we should say six and we do.

  • Okay, let's try using this function a few more times.

  • If you write be Eco's function to parentheses for function to of parenthesis.

  • Four.

  • She return eight.

  • So once we print be, we should see eight and we do on.

  • If you write Sea Eagles function to our five on every print.

  • C, you should see 10.

  • And what if you try to call dysfunction without any arguments?

  • So if you're right, D goes function to parentheses with nothing inside, let's try running it.

  • It will actually give us an error saying function to missing one record Positional argument.

  • X.

  • So this was an example off a function that maps 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 deaf function three X comma space.

  • Why Colon and then let's write Return X plus Why this means Let's define a function called function three.

  • And then this function is going to take two arguments X and Y on Return X plus y.

  • Let's load this function by running the cell on Let's write e Eco's function.

  • Three parentheses, one comma space, too.

  • So function 31 comma space to should be evaluated to one plus two, which is three.

  • So once we print E, we should see three.

  • Okay, so we saw two different ways off using a function so far.

  • The 1st 1 was as a set of code or a collection of code, and then the 2nd 1 waas as a mapping.

  • Now it's even possible to combine them both together.

  • Let's see how that works with this function function of four off X call on four spaces as usual, Prince X Prince double quotes still in dysfunction and then return three times X or three Star IX.

  • This function basically says Take the Argument X and then prints X and print this string stealing destruction and then return three times X to whoever called his function.

  • Let's run this cell and let's try calling this function with F.

  • Eco's function.

  • Four parentheses.

  • Four.

  • So what's gonna happen here is when this expression is violated function for four.

  • We go to this line so X is printed, so that means for the number four will be printed on.

  • Then this string still in this function will be printed on three times X was just 12 will be returned to this expression, so that will be assigned to F.

  • So once we run this cell, you see that four on stealing this function or printed on.

  • Once you print F, you should see 12.

  • Okay, let's see another example of a function def.

  • Define function.

  • Five parentheses, some on the score argument Colon four spaces Print some argument and then print we in double coats.

  • Let's run this cell to load the function.

  • And, of course, you can call this functional, dysfunctional five four Let's run the cell and then we see that four is printed because we have some argument being printed on we.

  • This string is also printed, so one thing to note here is that even though Function five is given on argument, we don't have a return body.

  • It's actually technically possible to say if Eco's function of 504 to assign whatever has returned from functional five to F But there isn't much over point because we don't have any return statement here.

  • Okay, let's not create a B M I calculator here.

  • Now let's say we have three people here.

  • We have a hypothetical person named Wai Kei here whose heights East two meters on whose weight is 90 kilograms.

  • And here we have like a sister whose height is 1.8 meters and whose weight is 70 kilograms on White case Brother is 2.5 meters in height on 160 grams.

  • A.

  • Wait.

  • What if we wanted to calculate the bm I for each person on determining if each person is overweight or not?

  • We can do this by writing a simple function.

  • Let's call dysfunction be Am I on the score calculator and then this function is going to take three arguments.

  • Name height in meters or height on the score M and then weight in kilograms or wait underscore kg in dysfunction rights.

  • B m I wait underscore kg divided by height on the score M double star sign too.

  • So this says.

  • Of course, height in meters squared on.

  • What we could do is we could return be Am I here?

  • But let's do something a little bit different here.

  • We're gonna right prince Double quotes be am I and then print be?

  • Am I So this is going to print the bm I off the given person.

  • And then if the B M A 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 on dhe.

  • When you have two strings, you can concoct innate them together.

  • Waste name, plus double quotes.

  • Not overweight a name.

  • Let's write else.

  • Colon return name plus is overweight a note here that there are eight spaces here because this l statement is in the function being my calculator.

  • And then this line is in the L statement within the B M I calculator function.

  • So let's run this cell and then let's write results.

  • One Eco's B M I calculator parentheses, name one height on the score and one wait on the score KC one.

  • So this is for the first person.

  • Let's do the same thing for the second person on the third person.

  • Let's run this cell.

  • So here we see the BM I for the three people to any 2.5 for the first person 2021.6 for the second person on 25.6 for the third person on to get the result, we can print result.

  • One result, too, on result three.

  • And then we see why k notable Wait like a sister not overweight by Kate's 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 bill.

  • That and then let's try printing the results again.

  • Okay, it's fixed.

  • So why case not overweight like a sister is not a wait and why Kate's brother is overweight.

  • OK, 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 miles as the argument on returns kilometers as a return baby.

  • And here you can use this formula.

  • Kilometers Eco's 1.6 times smiles.

  • Okay, if you want to get the answer to this question, just go to see sojo dot i'll slash price on three to download the sample file, and you can subscribe to my newsletter by going to see sojo dot i'll slash news to make sure you don't miss my future tutorials.

  • I'm waking from Siesta Joe on.

  • I'll see you in the next video.

Hey, CSOs.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

如何在Python中使用函數(Python教程#3) (How To Use Functions In Python (Python Tutorial #3))

  • 12 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字