Python Tutorial 16: Understanding Python Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter from toptechboy.com and we're here today with lesson number 16. in our incredible tutorial series where you're going to learn python or you're going to die trying i'm going to need you to pour yourself a nice strong cup of coffee that is straight up black coffee no sugar no sweeteners none needed you guys watching this channel you got to remember your jet fighter aircraft this is your jet fuel you're not going to get off the ground if you don't have your jet fuel so go pour yourself a nice big cup of coffee and get ready to learn some cool new stuff i'm also going to need you to go ahead and fire up your most excellent visual studio code and as you're doing that as always i want to give a shout out to you guys who are helping me out over at patreon it is your encouragement and your support that keeps this great content coming you guys that are not helping out yet look in the description there is a link over to my patreon account think about hopping on over there and hooking a brother up but enough of this shameless self-promotion let's jump in and let's talk about what we are going to learn today and what we're going to learn is we're going to learn about something called python functions okay you've already been using functions probably without even realizing it things like print and input or built-in functions but what we're going to learn today is we're going to learn about writing your own function and the purpose of writing a function is when you start getting more complicated code it can become just burdensome and unwieldy when the program gets so long you lose track of what's what and what your variables are and what the different parts of the program are doing so the purpose of function is to break a program up into little chunks of code that do some logically sensible or some logically consistent type of function so instead of having one huge piece of code you break that into smaller chunks and then you call those smaller chunks of code and there's some just really really great advantages to that and so we're going to jump in and we're going to learn the ends and the outs of python functions and so i am here in visual studio code we need to come over here in our most excellent python files and i'm going to create a new folder and this new folder i'm not a new folder new file we are going to call this python functions dot p y the dot p y is kind of important and boom we've got a fresh new python program just waiting to be written okay so let's say we wanted to input two numbers and then sum the numbers together well what i could do is i could say a is equal to let's make it a float and then we're going to input and then we're going to say please i'm going to get this out of your way please input you endpoint in puy please employ please input your first number put a space and then we close the quote close the input and close the int and then b is equal to float input yep and then please input your second number like that all right and now what we could say is c is equal to a plus b and then a plus b and then we could say print c like that and then this shouldn't be any big surprise this should work and then we could say all right down here i'm going to take 3 second number is 2 and then it prints 5 which is the sum okay that kind of makes sense this would be the normal way of doing things but what we want to do is let's say that you might add you might subtract you might multiply you might divide and then the program gets longer and longer and longer and more complicated well what we want to do is we want to build a function to start doing this and so what i'm going to do is i'm going to tell you the first thing that we're going to define our first function very important your functions have to be at the top of the code now that's not true in all programming languages and i find it kind of confusing to have the functions at the top of the code because i want to jump in and i want to see the code in the the functions down below but in python you have to put your functions at the top of the code so up here at the top i'm going to define a function and to define a function you use the keywords d e f to find boom you see it turns that happy little color so it recognizes that it knows i'm going to create a function now i need to label or name my function i think i'll call it tally because i'm going to be tallying up numbers and now what i need to do is i need to put in parentheses parameters and those are parameters or arguments that i'm going to pass to the function and so like the main program is going to pass these uh these parameters and then the function is going to grab it how many parameters can you pass as many as you need you could print you could pass no parameters let me just show you here well no i'm not going to do something silly but what parameters do i need well i'm going to pass a and i'm going to pass b because those are the two numbers this a and b down here to do a tally okay but what you got to see is it's looking at the position it's not looking at the names of the variables so even though i'm going to pass a and pass b i'm going to cut catch them in x and y so these variable names do not have to match the variables that you're passing when you call so i'm and then let's go ahead and call the function down here so we can talk what we could do is no i'm not going to call it yet we'll do that in a minute so i'm still defining the function so i have this this function called tally it is going to catch the variable x it's going to catch the variable y and just like an if statement a while loop or a for loop we put a colon there our friend mr colon and that's going to define the start of the function now we will be in this function as long as we are indented when we come down here like this this is not in function okay and then as long as we are indented we are in the function we are in the function okay as long as we're indented those lines of code are in the function but then when we stop indenting we are not in the function so what do we want to do we want to say c our sum or let's call it z is equal to x plus y so we pass it x we pass it y it calculates c all right and so here instead of saying c is equal to a plus b what we're going to do is c is equal to tally i'm going to call the function now i've got to pass it a and i've got to pass it b so i'm going to pass it a and b so what's going to happen here c is going to call tally all right and then it is going to pass a and tally's going to catch a in x and then it's going to pass b and tally is going to catch b in y and so now the sum here is z all right now this is the thing this function knows what z is but this main program down here doesn't so we need to return it we need to return it and so what are we going to return we are going to return z now you can see that c is equal to tally of a plus b a goes to x b goes to y then z becomes x plus y and so just like we passed it a and x caught it we passed it y or we passed it b and y caught it now what it is going to do is it is going to throw back to you z and then you're going to catch z over here in c because c is equal to kind of like you could think of it as what is returned by tally of a and b and that's what we said is return here and so now we would indeed print c because c is what we caught but it was calculated as z and then it was caught in c does that make sense i hope that makes sense right let's give it a try input your first number three oops always click down here so okay three okay input your second number two and boom look at that five okay you've written your first function have a little jet fuel okay we wrote our first function now there's something that is important for you to see and that is that this is like a little independent chunk of code that is separate from the main program and it doesn't share anything with the main program except what you pass it and then what you return but i want you to see something here i'm going to say a a is equal to 23 and b is equal to 45 and so when i go in there you see i'm making a23 and b45 but then i want to come down here and after i print the tally i'm going to say print and then i'm going to say a equal and comma a and then print b print b equal comma b comma b like that okay so i'm going to call tally i'm going to pass it a and b and then it's going to go into x and y and while i'm in there i'm going to change a and b and then i'm going to return z now what do you think a and b are going to be here let's see okay first number is three ah first number is three second number is two and then boom look at this a is equal to 3 b is equal to 2 and the sum is still 5. so even though you changed a and b up here it doesn't change it outside the function because these variables have a scope and their scope is within their little domain and this a and b live inside of tally and they're totally independent of any a and b out here now let's take this let's copy this all right and let me fix like get rid of that comma there okay let's take this print a print b and then let's put it up here okay control v let's say this is a in the function and this is b in the function and i better tab this over i lost my tab when i paste it all right so you see here a and b are still three and two even though you change them and then up here we can see i'll put in three i'll put in 3 and then i'll put in 2 and then look at that inside the function a and b are 23 and 45 but outside the function a is 2 and a is is 3. i a is a is 3 and b is 2. okay must put them in backwards that time i'm sorry so you see this a and b are their own a and b so you don't share variables between what's happening in a function and what's not happening in a function you could look at it as what happens in the function stays in the function except what you pass in and catch and then what you pass back out but then you could have the same set of variables inside as outside now this is a great thing because if i have one function that's using the variable x and i have another function using the variable x this function can't mess up this function what if this function changed x well it's not going to change the x and the other function so it really has these independent standalone little chunks of of working on their own little pieces of code and they don't interact with each other except through these ways that you pass variables in and you pass variables out so that is kind of neat all right now what i also want you to see is that you could pass something uh let's let's do something else let's say that i could make an array and so i'm going to come here i'm going to get rid of that stuff let's just kind of start over here and i'm going to say that x is going to be equal to 2 4 6. i'm using periods 2 comma 4 comma 6 comma 8 comma 10 all right and then comma 10 and so i have an array and then i need to kind of know how many nums i have and so the number of nums is one two three four five and so i have five numbers in the array two four six eight ten and then what i could do is i could call tally and i could say that uh uh my sum is equal to tally i'm going to call tally and i'm going to pass it nums so it knows how many numbers there are and then i'm going to pass it x you see i can actually pass an array in so up here i might call it something different like uh num num number of numbers num num and then i might call it my array okay so what's it going to do it's going to pass nums into num num and then it is going to pass the array x into my array now what are we going to do in this program well let's say that we went for i uh in range and the range we're going to start at 0 and then we're going to go through num num and we want to step through that array and so we need to stop one less than num num because we're starting at zero but the range function does anyway so it's going to stop one less than num num because we're starting at zero it all works out i've talked about that ad nauseam in other earlier videos and then we're going to increment each time through by one and then what we're going to do is we're going to say tote is equal to tote plus my array of i all right and so what we're going to do is we're going to pass it an array and then we're going to total up the numbers in that array and so if we're going to say tote equal tote it hasn't seen tote yet when it's on the right of that equation so we need to before we move in here set tote equal to zero just so we can get it started and then it sums up up and then what am i going to do i am going to return tote and so i'm going to come down here i'm going to say my sum is the tally of nums that's how many numbers we have 5 and then it's going to sum up these five numbers that are in x and so then what we can do is we can print print and then what we can say uh array total equal space and then my sum my sum i cannot do this today s-u-m okay my sum all right so let's run this and see what happens okay the array total is thirty well six and four is ten eight and two is ten so that's twenty plus ten is thirty shazam we got it okay so do you see what's nice is that you can have these nicely defined functions and i just say i see that i have x and i see i have five numbers in x and then i have this function called tally and i'm passing it nums and x and i can kind of see that my sum this is going to come back to you know my sum is going to end up being those numbers uh added together and so like let me show you too the type something else you could do so let's go back and let's just say a is equal to float we're going to float and we're going to input and then we're going to say enter first number like that and then b is equal to i did not close that like that okay b is equal to float float input and then enter your enter second number okay and then what i'm gonna do is [Music] i'm gonna call i'm not gonna finish this yet okay i gotta come back and put something there later but i'm also i don't care about this five anymore because i got rid of that array okay so now what i'm going to do is i'm going to pass it a and b all right i'm going to pass it a and b and then what i'm going to do up here is i'm going to grab it again in x and i am going to grab it in y alright now what i can do is i can say that i can say that tote is equal to x plus y and i can say diff is equal to x minus y and then i can say the product is equal to x times y and then i can say divide the division is equal to x divided by y and now you can only have one return statement so what am i going to return class class how what am i going to return i'm going to return tote diff prod division okay now what do i better do here like what if i said x a is equal to what i'm returning four values and i only have one one uh variable here so i better say let's say uh w x x y z z now what's w going to be w is going to be their sum x is going to be their difference y is going to be their product and z is going to be their division and so what i could do here then say i could say here the sum is and that would be a and then let's kind of get fast here and do a control c and then i'm going to control v control v control v so the sum is a the difference is b the prod product is c and then the division is d oh no no no no no no no no no did you guys catch that i returned it as w x y z so these need to be w x y see just so you know it doesn't count as a mistake if you catch it before you run it keep that in mind it's not a mistake if you catch it before you run it let's have a little jet fuel here okay and let's run this thing we're going to come over here and enter the first number is three and three two okay the sum is five the difference is one the product is six and division is 1.5 look at that all right so this is getting to be pretty cool here this is definitely getting to be pretty cool i think so that is all working i hope you guys can see that down there okay let's see or if that makes it a little better try to keep it where you can try to keep it in a size that you guys can see it but sometimes uh your screens are a little bit small all right so that really worked well okay i also want to show you that you could uh you know you could just pass it let's say uh how could i do this well i'm just going to come up here and i'm not going to take any parameters in okay i'll just take nums how many numbers you have okay and then nums like that or i'll just say number i'll call it num num for number of numbers and enter how many numbers okay enter how many numbers okay and then i'm going to get rid of all this stuff okay so if i want to input four numbers what i could come up here is i'm going to catch it in nums [Music] and i've input it here so then what i'm going to say is for i in range we're gonna start at zero we're gonna start at zero we're gonna go to nums right it'll stop one before nums but that's right since we're starting at zero we wouldn't want to go all the way to nums we're going to increment in steps of one and then our friend mr colon is going to go there and then if we're going to make an array then i better create an empty array so i'm just going to say x is an empty array here and then i'm going to come down here and inside that for loop i'm going to say x i'm going to say num num i'm going to say my num is equal to float of input float of input i hate it when it covers things up like that i think that's why i made it smaller last time was that nonsense let's see okay float of input and then i have got to make it smaller or you guys are not going to be able to ever see this okay float of input and then enter your number all right and then that finishes out like that and so now i have the number now what do i want to do i want to put it in the array so i'm going to do what to x i'm going to append and then what am i going to append my num like that and so when i'm done here i should have an array of numbers that i input well what can i do with that now i can return and what can i return i can return x and then down here what i could say is i could call that function and i could say y is equal to tally and then what do i want to pass to it how many nums num num so that's the number of numbers that we're going to put in that array and then we're going to print y all right does that make sense so i go to tally and i say get me five numbers it creates the x array that has five numbers in it it passes that x array back that x array is caught in y and then y you can print out and so let's go here we are going to run this baby and then we're going to say how many numbers 5 i should have put a space there i'm going to kill this i'm getting in bad habits of not putting spaces there okay all right so here we go and how many numbers do you have i have five and then i'm gonna float object cannot be interpreted as an integer oh i know what i did i passed float as i entered the number of numbers as a float well you're not going to have five and a half numbers you're going to have round numbers there and so this should have been an int and the integers are the count or the round numbers like 1 2 3 4 five minus one minus two there are those round numbers like that because you cannot have an index you cannot do this range with a float this has to be an integer and because i'd made num num afloat it passed num num to nums and then nums became a float and then range didn't like having a float in there so that is what happened and let's give it another shot here so we're coming in how many numbers do you have i am getting really really bad at that okay i think i'll try to make it bigger now so you can see it okay we'll do it this so when i'm down here i'll make it bigger all right so that'll be five and then enter your number okay i want the number five i want the number four i want the number three i want the number two i want the number one and the array that it prints right remember this print down here the array that it prints is five four three two and one boom we got it okay so this thing so you see how we can pass you know i could also pass an array in here and i could return an array okay i could pass an array in i could return the rate there's a lot of different things that i can do but what the important thing that you guys have got to understand here is is that the variables in the main program are not shared with the variables here so you can use the same variables but it's like they're completely different this is like its own little world this function is its own little world you can also have uh you can also have other functions like i could have def and i could have reverse okay and then i could pass it an x and this could like take an array and it could reverse order it and then i could even have this array input though i could have this function input the array and then this function could call this function and this function could like sort it and put it in right order return it to this and then it would return it to that so functions it's like you can nest them a function can call a function can call a function can call a function and then this function would return to this one and this one what it returns goes to this one and what this one returns come back here so you sort of drill down into the stack and then you climb yourself back out of the stack so you can have just indefinite amount of nesting and that is okay but just make sure that each little chunk or each function is logically consistent make sure that it is logically consistent okay this is your homework do you remember that program where we input a set of grades you said like how many grades do you have and you say five and it's like 90 91 92 93 94 okay so you have those grades and then what you do is we did several things we printed the grades out that we had input and then we averaged the grades and then we print the average and then we found the high grade and then we found the low grade all right and so when you looked at that code that was like a really really long thing of code so this is what i want you to do i want you to go in and write functions so you're going to write a bunch of functions up here but logically separate those functions logically so don't don't just say define the whole enchilada and then do in just one function all those things that i just said that wouldn't make any sense and then you came down here and you called the whole enchilada that doesn't make very much sense but you might have something like input grades you might have defined you might have print grades okay of course you got to fill these things in you might then have average grades okay and then you might have a high low which would find the high and the low all right then what does your program look like your program looks like input input create grades [Music] input grades print grades average average grades and then high low of course you've got to have something equal to something you've got to do a few things down here but all of a sudden now if you made functions when i look at input grades it's like oh that makes sense what that's doing is it's inputting the grades ah ad grades that makes sense what that's doing is averaging the grades then if i look at the overall program i'm doing function calls and then what the program itself is doing is crystal clear oh look what is this guy doing i'm looking at his code looks like he's inputting grades looks like he's going to print the grades it looks like he's going to average the grades it looks like he's going to find high and low so all of a sudden your code becomes very readable and it's very much easier to debug these things and then also you can reuse the code if you ever need to average grades in the future you can come in and you can grab this function that you've already done or maybe you'll make a really cool sort function where you sort the order of the grades and then you can just call your sort function so you start building up your own little library of useful tools now understand in this assignment i didn't ask you to do the sort again that sort was kind of a hard problem so what you're going to do for this one is you're going to input the grades you're going to print the grades you're going to average the grades of course print your average and then you're going to do the hi-lo i usually don't print inside of a function i'll go out and i'll find the average and then in the main program i'll say your average is so i don't usually do printing inside of the functions unless of course it's a function called print print grades if that makes uh if that makes sense hey guys this has been a lot of fun i hope you guys are having as much fun taking these lessons as i'm having making them i just love python i love coding in python if you like this give me a thumbs up if you haven't subscribed to the channel already hit the subscribe button when you do be sure to ring that bell so you'll get notifications when my future lessons come out and then think about sharing this with your friends on social media and so forth so that we get more people learning how to do useful things instead of sitting around watching silly cat videos okay guys this has been paul mcquarter from toptechboy.com i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 8,233
Rating: undefined out of 5
Keywords:
Id: 6HGOAaNhyi8
Channel Id: undefined
Length: 34min 25sec (2065 seconds)
Published: Wed Jun 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.