Creating Custom Functions
- 07:51
Learn how to create and utilize custom functions in Python for more efficient code reuse and flexibility, incorporating arguments for broader applicability.
Downloads
Transcript
You've used a lot of built-in functions in Python to complete different tasks. So for example, you learned how to use the max function and the min function. Right here we have a list and I've already written out max and min to find the maximum and the minimum value in that list. So if I execute that cell, it gives me 1.39 and 0.81. These are the built-in functions that come as a default with Python.
In addition to Python's built-in functions, you can also create custom functions so that you can package and reuse your own code. At a bare minimum, every custom function contains three parts. First, the def keyword, which indicates that a function is being defined. Then the new name of the function followed by parentheses and a colon. And finally, an indented code block that defines what the function does. Let's say that you're using the min and max function often together for efficiency you could create your own custom function to combine those two built-in functions together. So I'd start with the def keyword to indicate that I'm defining a new function. Then I'd write the name, a new name for the function. Let's call this one min max, and I'm gonna have a pair of parentheses and we'll get into what those are for a little bit later. And then we're gonna add a colon. And below that we're just gonna have a code block. So here I wanna combine the two actions that I have in the code block above or in the cell above rather. So I'm just gonna copy those. So there it is. I have created a new custom function called min max. And whenever I wanna call it, I call it just like a normal function. So I can just say min max. And then when I execute that, I'm gonna get the same exact values, those same exact objects from the list beta values.
However, this function has one major limitation. Can you spot it? It's that this function will only work on the list beta values. For a more flexible function, you need to add one more ingredient, an argument. You're already familiar with arguments from Excel. They're the values that you enter in the parentheses of an Excel function in Python, you create an argument in the parentheses after defining the function name. Then inside the indented code block, the argument is used as a variable. Let's create a new min max function that incorporates an argument so that it's more flexible and can be used to calculate the min and the max for different lists. I'm gonna start with that def keyword and then let's say new min max. And in parentheses, this is where we name our argument. And the name of our argument can be anything here. You could use the letter A or B or C, but here I'm going to use a more descriptive name. Let's just call it list argument.
And then the colon. And here is our code block. I'm just going to copy that from above and paste it because we want it to do almost exactly the same thing.
But where it says beta values, I'm going to replace that with my argument list argument.
So now how this works is if I want to call this function, I've defined new min max, I write the name new min max, and then I open my parentheses in min max. We didn't put any arguments in those parentheses because we already indicated in the code block right here what list we're calculating the min and the max of. In new min max, we only have this list argument. So we have to input that argument here to tell the function where to apply the different steps that are in our code block. So here I'm gonna write beta values, the list that we want it to apply to, and now it's going to replace everywhere that says list argument here, it's gonna replace that with our list beta values.
So when I execute that, I get exactly the same answer. But if I were to create a new list, let's say we have list one and I'll make it something easy, one, two, and three. Now I can use new min max and instead of beta values, I can use this new list one, and now it gives me one and three. So that's a much more flexible custom function.
As you know from previous lessons, you can use python's built-in functions to define new variables. For example, if I wanted to create a variable called betamin, that was the minimum value from that list beta values, I could just use the min function, which is a built-in Python function, pass it the beta values list.
And then when I print beta in my new variable, it gives me 0.81. However, if I try to define a variable using the custom function that we just made, I'm gonna run into a problem. So here I've defined a new variable beta min max, which is using our new min max function applied to the beta values list. When I print beta min max, it gives me 0.81, which we know is the minimum value 1.39, which we know is the maximum. And then it gives us this none output that looks kind of funny. Let's print out beta min max by itself now. The value that's been stored in that variable I created is none. Why is that? To solve this problem, we need to tell our function to return a value using the return keyword. And in this case, we're going to remove the print commands and we're gonna define a min variable and a max variable and tell the function to return both those variables as a list. So let's define new new min max. And we're gonna give it list argument so it's flexible and can work on different lists instead of just a single one. And in our code block, we're gonna create a variable for our minimum value, and we're gonna set that variable equal to the minimum that built-in function. The minimum of whatever list we pass it. Then we're gonna create a new max variable, and that's gonna be the max of our list. And then finally, we're gonna return in the form of a list that minimum variable that we created followed by the max variable. So now when I try to define this variable beta bin max, it gives me the minimum and the maximum and the format of a list. In fact, as long as we're including a return value, we actually don't need the code block before it. We could rewrite this function in only two lines. So here, instead of creating those two variables, min var and max var, I'm just gonna start this code block with return and then in square bracket so that it gives us a list. I'm gonna do min of list argument, and then max of list argument. So when I redefine beta min max and print it out, the answer is exactly the same. And we've accomplished that with a custom function written in only two lines.