Writing Functions Recorded Transcript This transcript was created with a speech to text software. Please excuse any typos or mismatches with the video. Hi, welcome back in this Python prep video. We're gonna continue learning more about Python in particular. We'll see what writing functions is all about. So let me go ahead and share the Jupiter notebook and we'll get started. So in this notebook, we're going to demonstrate how to write functions. Uh primarily we'll talk about first what a function is how to define one. Uh what are return statements, functions with arguments? What's variable scope as well as anonymous functions? So we're gonna go ahead and get started with talking about what is a function. So many times when you're coding uh either for data science or for really any sort of coding pro project you might be working on. You're gonna find yourself trying to rerun the same exact identical chunk of code multiple times. Now, if you do this once or twice, you can probably get away with just copying and pasting. But if you find yourself doing this three or more times, it's probably best to write that code chunk as a function. So essentially what that means is you'll write the chunk and then you give it a name, sort of like a variable and So that way any time you need to use the code chunk, you just call the name. Uh and then you uh then you have the code chunk. So you take copying and pasting a large code chunk, uh multiple times, you take that and just turn it into writing the name multiple times. And so any time the name is called the function is run which executes that code chunk that uh you've stored in the name. So uh we're gonna start with a somewhat silly example. Uh We're just gonna write a very simple function that we define to be uh called hello there. And what this is gonna do is just print out the phrase hello there. Uh So what you do to define a function in Python is you use the keyword D E F which stands for de or definition, you write out the function name, which for us is going to be hello there. And then you write a set of parentheses. Now we'll talk more about what should go inside parentheses a little bit later. Uh But that's the main first line is deaf function name a pair of parentheses and then a colon and just like with conditional statements or loops, you're gonna then put an indent and everything on that indent is going to be part of the function. And so here we indent with a comment and then finally an indent with what we want the function to do which is print the phrase hello there. So now I can call my function by writing hello, there followed by a pair of parentheses. And so this would work for any function name you have. So it works function name parentheses and then it runs the code chunk belonging to that function. So the code chunk for hello, there is just print hello there and there we have it. So as your first exercise, I want you to write your own function called General Kenobi uh which is going to print the phrase General Kenobi. So try and pause the video and do this on your own and then come back and watch me do it right now. So we're gonna say define and then general underscore Kenobi parentheses, colon and then remember indentation ... and then print General Kenobi. And now when I call it, I'll get General Kenobi first and then hello there. And so let me remind myself, I'm gonna delete this from your version. So you actually have to type it out. But on my version, it's already there. Uh So General Kenobi gets called first. That's why that's printed. Hello there gets called second. That's why hello, there is the second line. OK. So defining a function uh is pretty straightforward. Uh You know, we're gonna probably want to do things with functions more so than just printing out references to Star Wars prequel movies. Uh So the next thing you might be interested in having is a function that returns something. So gives you a value in return. And so the way to do that is with a return statement. So a return statement is something you'll put when you're ready to give back a value. And the reason is uh you need to be ready to give back. The value is because once the Python code hits a return statement, uh it will uh leave the function chunk no matter what else you write after it. So for us, this is gonna be a very simple function that I call, make two. So we've got our definition, make two parentheses, colon, indentation, return two. So all this does is it's going to return the number two any time it's called. And so when I do make underscore two, I call it, I should just get a two back. OK? So I want you to practice right now. Uh try and make a function called make three that returns the number three. OK? So we call death, make three parentheses. Colon enter, I'm already indented. So I call return and then three and then I can call it and I should get a three back. OK? ... All right. Uh So adding arguments. So we have a function, we know how to define a function. Now, we know how to make a function that returns something. But oftentimes the code that you're running, maybe you're running it multiple times because you want to change the value and see what happens when an input value is changed. And so that's why we need arguments. So the things that you put into a function are called arguments in Python. Uh And so now let's show you how to write a function with arguments. And so here we're going to define a function that I'm calling weird division. And we'll see why in a second. And if you want to put in an argument, you just put in a variable name for us, it will be X and then that variable name will be the stand in for whatever input you want for the rest of the function. So because I have a variable name called X everywhere in the function, when I call X, uh it's going to imagine like X stands in for all right, two or three or whatever number I try and put in and it doesn't even have to be a number. It's just uh the way I've written it doesn't have to be a number. Uh But it's just a placeholder that allows us to refer to the input later in the function. So you do define function name, the, an input name for us which is X and then you just write your code. So this is gonna go through and take X and then check if it's even and if it is even it will return X divided by two. Otherwise it's going to return X plus one divided by two. And So now I want you to try and run weird division a few times with different inputs and see what you get. So you can either do this along with me or pause the video and do it on your own. But I'm gonna start right now. So weird division. And I'll put in a three and what we should get back is a two, I'll copy and paste maybe three times. Uh Let's put in an, an eight, a one oh one and a 700 or 7891. ... Oh man, I forgot. Princes. Let me put some prints. ... OK? So we can go through and check. Well, this is what we should get here. Eight should be four based on the function we wrote one oh one should be one oh two divided by two. So that's 51. And then I'll trust that this one also worked. I don't wanna try and do the simple arithmetic, but I, I don't wanna try and do it. OK? So now uh your practice is try and write your own function with an argument. It's gonna take in an argument. You can call it X, you can call it whatever you'd like. Uh doesn't matter as long as it's not a Python keyword. Uh You're gonna write your own function that takes in an input and returns the string even if it's even and odd if the, if the uh if the integer is odd. So pause the video and try it on your own or just watch me do it now. So I'm gonna say, define and let's say even underscore odd is the name of my function doesn't have to be the name of your function. You can call it whatever you'd like. Um And I'm gonna use X again and if X% 2 is equal to zero, I print uh I return the string, even ... else I will return the string odd. And now, you know, the astute um coder might notice that this isn't probably precisely what the function should be that works for any input. I should probably do some sort of checking for type to make sure it's an integer, but we're just learning. So it's OK. Uh You and I are gonna be the only ones that use this function. Um And so we know how to use it the right way we don't have to do any checks for actual users. So even odd uh let's put it in a three ... And we can do print three And then whatever gets returned and then print. Why don't we do 88 and then even odd 88. OK. So three is odd. 88 is even. OK. So we've got functions, we've got return statements, we've got arguments. Now we're gonna show you what about if you have something that should default to a normal value, but you want to be, you want to allow the user to be able to change it at a later time. So maybe you have something that you always want to, to normally be a default value in your function. But it would be nice if maybe later when you come back and try something different, maybe you're doing some research and for now, uh you have a value that you want to be the same, but it would be nice to have the flexibility for it to be different later. Well, you can have arguments that have default values. And so to do that, the syntax is the same. Uh The only thing that changes is you're first going to have defined function name and you put out all your inputs that aren't going to have default values first. So for us, this will be X uh but if you have more than one input that you want uh with, without a default value, it would be like X comma Z comma whatever. Uh And then once you're done with all the ones that do not have a default value, you start listing off the variables that will have a default value. So in this example, it's Y equals two. So we're gonna have two inputs, X and Y and then we will default to having X without a value, but Y is defaulted to have the value two. And so then you're gonna say if X is this for this particular function, I'm checking if X is uh divisible by Y, I'm gonna print out that X is divisible by Y. And if it's not divisible, Y Y, I'm gonna print out that X is not divisible by Y. OK? And so we're gonna go through a couple of different examples here. So the 1st 13 is gonna be the input for X and because Y has a default input, I don't have to put in anything for Y. All right. So this will print out and we'll focus on the first one. So this prints out because Y has a default value, you only have to put in something for X now because Y is an input, you can also change it. So I can say divisible by three will go to X and then you can say Y equals three. So this will change the default value of two to the value of three. And that's this line right here. Yes, three is divisible by three. Alternatively, uh you don't have to name out Y equals to three when you call it because it will just follow the order of the variables that you put in here. So it will go X first, then Y so you can put in alternatively three comma three and it will still work. But I wanna highlight it's really good practice when you're writing your code to input the ones that are named with default value. So if you, if you want to change something with a default value, it's good practice to actually put the name of that variable in there. OK? So now you are gonna do a practice where you write a function that's gonna take in three arguments X Y and Z. And then I want you to just multiply X Y and Z. So what should be returned is X times Y times Z and now the thing is Z should have a default value of 17. OK? So try and pause the video and do this on your own and, and I'm gonna do it right now. So I'm gonna say define uh let's call it like weird multiply X comma Y comma Z. Now this is a default value Z of 17 colon. And then I'm just gonna have a really simple return statement of X times Y times Z. OK? So now I wanna call that function and just use the default value for Z. So I can do weird multiply and then let's say 21. And since I'm using a default value for Z, I don't need to put in an argument for that. OK? So we can check two times one times 17 is 34. Uh And then I can do weird multiply, but now I need to change the value of Z. So let's do 216. So this should give me 12 and it does. OK? So we basically now know how to write most functions that you'll probably want to write when you're writing your own code. Uh So let's take a second and maybe you're sitting there while we're doing this. And you're saying, OK, this is very nice. But what if earlier in my code or my Jupiter notebook, I have a variable that I use the name, accidentally use the name for in the definition of my function. So here's an example Where I have this variable apple that I'm gonna store in the value 10. So uh what we're gonna learn about right now is called variable scope. And so this is gonna essentially how Python orders a hierarchy of variable names. And so when you define a variable outside of any function, just as its own Apple equals 10 is going to define what's known as a global variable. So apple in the global environment is going to be set to this value of 10 unless we change it later also in the global environment. But just because it's set as a default or as a value of 10 in the global environment doesn't mean that you cannot write a function that uses apple as its input argument. So here I have a function that I'm calling dumb function and it uses apple as its input and then it says print apple. So once you create this function, this creates what's known as a local environment, a local variable where inside of the function apple is gonna be whatever the user inputs and not 10 unless the user inputs 10 obviously. Uh So what's gonna happen is you're gonna input a value for apple, let's say it's, um, in this example, peanut butter and when I run this, what's gonna get printed? Let's think about this. Uh, what's gonna get printed? Is it gonna be 10 or peanut butter? Well, in Python, what takes precedence is the local variable. So whatever the value is set as a local variable will be the one that's looked at first. And then if the a local variable did not exist. So let's say um uh well, OK, let's get to that in a second. But let's run this. And what should be printed is peanut butter. OK? And now we can go through, we're no longer running the function. Let's check what the value of apple is And it's still 10. So within the function, it defaults to look at whatever it's loc locally called. So here in the function I have Apple is an input. So apple has its local value of whatever the input is. And then once we're done running the function, Apple goes back to pointing to the global variable, which is just 10. We could get even more complicated where let's say, I also have another variable called banana. And I'm gonna make this eight and I can uh in also, in addition to printing apple, let's say apple equals and then let's do print banana equals and then banana ... space space. And so because there's no banana defined within the function or as an input, the default is going to be the global value. So when I call dumb function again, it's gonna print apple equals peanut butter and then it will print banana equals eight. ... Oh That's because I forgot to run this now. Oh print banana equals a OK. So because there is no local variable banana being defined in the function or as an input, it's going to say OK, there's no local variable banana. What's the global variable banana? And because that global variable exists, that's what's printed. So if this is confusing, I suggest that you pause the video and try. Uh usually if you're confused about how something works in coding, it's a good practice to just try and mess around with some code chunks and see if you can figure it out. So I encourage you to pause the video and try and play around and see if you can figure it out on your own, if not, if you do that. And you still think you're confused, feel free to message me on our Slack channel uh or you know, look around as a web search to try and figure it out. But just as a good tip when you're doing some coding stuff and something's unclear to you. It's usually helpful to try it out on your own in these code chunks and play around and see what you can figure out. So we're gonna end this notebook on functions by talking about anonymous or LAMBDA functions. So in the future, we'll be doing some stuff where it might be useful to have a function but not necessarily define it in our system. We just want to use it for a one off task. Um Like maybe we want to change a bunch of values of something uh at the same time. OK. Uh And so maybe we have a bunch of things that we want to multiply by two all at the same time. But we don't want uh times two isn't gonna work or um And we don't want to go through multiply it by two by hand, like with the four loop, say uh so sometimes you'll want a function that does what a function does, but you don't want to name it and store it in your memory. And so this is what a Lambda function is in Python, in coding, I believe they're just normally called anonymous functions and they're anonymous functions because they don't have a name. Uh And in Python, they're called lambda functions because of the syntax. And so a lambda function in Python is written like, so, so you call the word lambda and this is telling Python that you're gonna write a function. Now, then you put in a list of your arguments again, separated by commas. And so for us, the only argument we're gonna have here is X and then a colon and then whatever you want returned using those arguments. Uh You're gonna put to the right of the colon. So this is gonna say, all right, Python, I have a lambda function. I want you to take X colon and then return X times two. And you can see what gets returned is function uh lambda of X. So here we have a lambda function. Uh And it's of the argument X. Now, you can use this as a simple uh a quick fix for actually storing. So I said you usually nor normally use a Lambda function for not storing it, but you could use it to abuse the notation to define a function without a clear depth or a return statement. So I could store this lambda function in a variable called double and I could do that. Uh The only reason I did that in this one is because we don't currently uh as of this uh Python prep notebook, we haven't looked at applications where we might be interested in using a Lambda function. Uh We're gonna see those in a later notebook uh about Python or about Panda's data frames in particular. So as your final exercise, I want you to write a Lambda function that would multiply a number by five. So feel free to pause the video and do that on your own. Uh And I'll do it right now. So you would write the keyword lambda. It's only gonna have a single argument which again, I'll use as X And then you just want to return x times five. OK? And then we could always uh do the abuse of notation. So I'm just gonna copy and paste here. ... Uh maybe I'll call it by five And then we can see like by five of 10, which should give us 50. OK? So that's how we would write it. Um again, these will become more useful later at a later time when you've learned some more python. OK. So you now have the basics, you know how to write a function in Python. You know, a little bit more about variable scope and you know about anonymous or LAMBDA functions in Python. Uh So you have all the skills you need to write some good code. Uh in Python. Uh using base Python, we got data types, conditionals, loops functions. Uh in the next notebook, we'll learn a little bit more about Python. I hope you enjoyed this video. I enjoyed having you here in our virtual environments. Uh And I'll see you in the next video. Have a great rest of your day.