Python Tutorial 17: Python Functions Examples and Solutions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter with toptechboy.com and we're here today with lesson number 17. in our incredible new tutorial series where you're going to learn python or you're going to die trying what i'm going to need you to do is pour yourself a nice strong cup of coffee that is straight up black coffee no sugar no sweeteners none needed that my friend is go juice and that's what you're going to need if you're going to get through this lesson so get your coffee i'm also going to need you to fire up your most excellent visual studio code and as you're getting that out 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 those of you who are not helping out yet look down 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 talk about what we're going to do today now in last week last week's lesson lesson 16 i taught you how to write functions in python and then what your assignment was was to go back to that earlier program where we input grades we printed the grades we averaged the grades and then we found the high and low grade and printed those out and go back and redo that but instead of one big long program break it into logical chunks and write a more modular program using python functions how many of you were able to do it if you were put a comment down below i am legend if you weren't you could say i folded up like a cheap walmart lawn chair that was like iwf c w lc you could put the abbreviation if you wanted to save yourself some embarrassment but because it's okay if you didn't get it done it's just i want you to struggle and try and and toss it with it and wrestle with it and then just finally you just can't do it and then if you watch me then you'll really learn something because you'll see how i got around maybe some of the problems that you had but if you just sit and watch me do it you don't learn anything because it looks easy when i do it but then you cannot do anything without me sitting there and you're just doing it along with me okay so so i really need you to try your best to do this home these homeworks and get as much of them done as you can and get as many of them right as you can okay so i'm going to switch over to my visual studio code and that would be right there and i will get out of your way we'll come over here and go to our python files folder and make a new file and i will call it [Music] the dot p functionfuncthw.py.py is kind of important and boom we got ourselves a fresh new python program just waiting to be written all right now i am going to probably just start with the code and then i will move up back to the top to put the functions in because the functions do have to be in the top and the first thing that we're going to need to know from the user is how many grades are there so i'm going to say that's num grades and that is going to be equal to the number of grades would be an integer because it's always going to be one two three four you wouldn't have four and a half grades so we're going to make it an int and then we are going to input and i'm going to move this out of your way so you can see more uh int input and then uh we're going to open input and say how many grades question mark space and then we close that string then we close that input and then we close that int all right so now i know how many grades well what i need to do is i need to input the grades so i'm going to say my grades is going to be equal to input grades okay so this input grades is going to be the function that goes out and gets the grades from the user well what does he need to know he needs to know numb grades so i'm going to tell the function i need you to get this many grades so in the main program i figured out how many grades there are going to be and then i go to my grades and i'm going to input that and so then i need to return the array that has all the grades in them and then that that array will go into my grades so the only thing that my grades really is going to need to know is it's only going to need to know how many grades you want so now i'm going to come up and i'm going to define that function how do we define a function d e f turns the happy little color because it recognizes it and then i need to call it input grades this is the name of the function and then what do i need well i'm just going to put nm or you know i'll just say nm for numbers that's how many numbers there's going to be and then out here i need to put who our friend mr colon our old friend mr colon and now what do i need to do here well for i in range and i want to start at 0 and then i want to go all the way to nm but i want to stop one before nm because i'm starting at zero which means if i go to nm range does stop one before nm we have gone over that ad nauseam in the past and we're gonna increment each time through by one and then again our friend mr colon and then i'm just going to say grd is equal to input and then what am i going to input i'm going to say please enter your your grade like that okay and then grade is going to be a float because you might have a 92.5 you wouldn't want to make it an int because it might be the in between numbers so i'll say float i'm going to float it and then i've got to come out to the end and close that float so i close the string i close the input and now i need to close the float like that okay now what do i do with grd i need to put it in a uh in an array and i'll just call that array grades and grades what i want to do with grades i want to append to grades and then what do i want to append i want to append grd now notice if you're calling a function you use the parentheses but if you're indexing an array you use the square brackets but here even though we're talking about grades we're not indexing grades we're appending which is a function which is why we use the round brackets there okay so now it should have it now the problem is i'm appending grd to grades but i haven't defined it yet so before i go into that for loop i just need to create grades and create it as an empty array all right so now at the end of this for loop i do believe that i should have the grades array ready so what do i do i return and what do i want to return i want to return grades now i don't need to return nm because the program down here the main program already knows what num grades is it already knows what gnome grade says so i don't need to return that to num grades because it already knows so i'm going to return grades and then what where will it end up it will end up in my grades so this grades array that the function is passing back is going to be caught in the main program in the array my grades and so now let's just run this it's not going to do anything but let's just see if it'll crash and so it says how many grades and we are going to say three grades and then i'm going to say 90 91 92. now why would i get a bad grade if you're watching this why would i get a bad grade because i didn't put a space after grade and that looks kind of cheesy good news is it didn't crash so on uh please enter your grade i need to format that a little better i'll put our friend mr colon in a space and that way as you're inputting the data it will look a little nicer let's be neat and tidy as we code okay we need a neater tidier world so let's be neat and tidy as we code all right so now i should have the grades in and so now let's print the grades and so what i'm going to say is i'm going to say print and then i'm going to say your grades are colon space and then what do i want to do i want to print grades and then what do i want to print my grades so i'm going to create a new function print grades and then i'm going to print my grades with it okay now i want you to see i didn't say anything equal to print grades because i print grades is not going to return anything to me it's not going to change anything it's just going to do the print function for an array okay now it's going to need my grades and what else is it going to need it's going to need to know how many are in there so i'm going to need num grades and let's make sure yeah num grades is what the main program is calling it in my grade so i'm going to pass it num grades so it knows how many there are and then my grades now there's a function you could actually do that that would give you the length of my grades but i just like to kind of keep a variable and just pass it rather than getting too fancy i've seen more disasters from programmers trying to be too fancy okay if i'm going to call print grades i better define it and so i will come here and then it's a new function so i've got to go all the way back i don't want to put this function inside the other function so i'm going to say define and it turns the happy little color print grades okay and then what do i need i'm just going to call it nm and x right i mean why not just call it nmx and then who did we forget our friend mr colon and now what i'm am i going to do i'm going to say 4i in range so i'm going to step through x so for i in range i'm going to start at 0 and go to nm which was the number of grades and then i'm going to go in steps of 1 right like that so it's going to step through the x array and it's going to go all the way to the numbers of of grades in x and it's going to increment one each time and then what are we going to do we're just going to do a print and then what do we want to print we want to print x of i like that okay so this is just going to step through x and it's going to print each one of those and we're not returning anything because this function all it does is just prints an array for us kind of a handy little function to have all right so let's uh i'm going to make this a little bigger make sure you guys can see it and then i'm going to come up here and run it and then down here it says how many grades and so i'm going to say 5 and so then it says please enter your grade i'm going to say 95 94 93 92 and 91 and boom look at that let me show you okay so your grades are and then it goes 95 94 93 92 91. boom all right guys we're getting this we are getting this okay so we were supposed to what all were we supposed to do we were supposed to uh we were supposed to input the grades and print the grades then we were supposed to average the grades okay and so we uh we do my grades which gets the grades or input grades which get the grades then we print the grades now what we want to do is we want to average the grades so i'm going to say avg so i'm going to return from the function i'm going to return the average so i need to catch it so i'm going to catch it in avg and then the function i'm going to call average grades and then what is it going to need well it's going to need to know how many grades there are so numb grades again and then it needs my grades if it's going to average them it's going to need them okay so average is going to be average grades of num grades my grades okay so now we need a new function and that new function def is going to be average grades and then what is it going to do let's just do nm and x again okay because these things might be averaging who knows what so i'll just call it nmnx and then who do we need here our friend mr colon and then we're going to come back over here and now we need to sum all of those up so i'll have a variable called total and set it to 0 just so that's kind of the bucket i'm going to be adding all the numbers into and then i'm going to say for i in range and where do we start we start at zero then we're going to go to nm which is the number of grades and the range function will stop one before nm which we want because we're starting to count at zero and then we have one and then who do we need here for a for loop again our friend mr colon and then we're going to come here and we are going to say total is equal to total plus okay so whatever we had before we're going to add to it the next grade which is going to be the variable x because that's what we passed it into up here we passed our my grades array into this array x and that's going to be x sub i like that so total is equal to total plus x of i then i come down here do i want to divide by the total number like the number of grades do i want to divide by the number of grades inside the loop or outside the loop well do i want to divide by the total n times or only one time only one time so we don't want it in the loop so we're going to say a v e r a g e average average is equal to what it's equal to the total divided by nm which is the number of grades right now what we need to do what what is that nonsense how did my cursor end up up there i don't know okay now what do we need to do we need to return return what average return average and now when i call average grades you're going to return average and then down here you're gonna return average who's gonna catch it mr avg is gonna catch it since so now avg has the average and so now what i can do is i can print your your average is and then a comma and then print avg all right so let's try this let's run this okay how many grades let's say five grades and then we're going to say 90 91 93 84 and 92 like that okay boom 1991 93 84 92 and your average is 90. all right boom guys i kind of want to check that so i'll take 90 i'm one above i'm four i kind of can average things just by size so how are we around 90 well i'm four above uh i am six below so that would be two below and then two above would take me back to ninety so yeah those things would average to 90. let's try it again i want to see what happens if i get like a a non-integer number and so we're going to run this thing again and then it says how many grades 3 and i'm going to go 90 91 and 93 okay and the answer is 91.3333333 and i don't like that formatting very much so i'm gonna trim this down a little bit so on this average i'm going to round average and so notice i'm opening that up i'm gonna round average and i'm going to round it to one decimal point so it'll be like 91.3 and that should be good enough so let's try this again we're gonna come down here whoa line 23 what did that not like oh i didn't close i closed my round but then i still need to close the print all right so we'll come over here and how many grades we'll say three and then i'll say 90 and then i'll say 91 and then i will say 93 and the average is 91.3 boom okay this is looking pretty cool isn't it this is really working so we've done almost the whole homework assignment we need to find the high and the low grade so we are going to make another array to do that also i think i'm going to start by just printing a blank line a few blank lines in here so after you have my grades and then it prints your grades are i'm going to just print a blank line here and you can do that which is open close quotes like that and then when after you print your grades then i'm going to print another blank line and then average num grades your average is and now what i need to do is i need to find the high low so you know what i'm going to do i'm going to i'm going to kind of do it with one function and so i'm going to return the high grade and the low grade so i need to have two variables to catch that anticipating i'm going to be passing two variables from the function back and to the call so i'm going to say high g and low g and that is going to be equal to high low and what does high low need to know it needs to know how many grades there are so it knows how long to step through it and then it needs my grades again all right and so now we need to do high low so we're going to come up here and we are going to say define hi low and then what does he need well let's just call it nm and x i'm just trying to show you you don't have to use the same variable names all right who's your friend your friend is mr colon and then what do i need to do well i need to go for i uh in range we'll start at zero we will step through nm and we will go one at a time it will stop one before because we're starting at zero and now that's gonna input them and now what i also need to do i need to initialize my high g and log g so i'll say hg i want to initialize the high grade to 0 and i want to initially initialize the low grade equal 100 and now my friend this hg and lg are becoming colors which probably mean they are a special word and so a special uh variable so we better not use those i'll just say hi hi g and then low g all right and now i'm going to step through these and if x which is our variable and i'd like to put this like this if x if x of i is less than low g what do we do if x of i is less than low g we have a new loser so now all of a sudden that new loser at low g is going to be equal to x sub i right now what else do i need to look for if x sub i is greater than if x sub i is greater than what high grade high g what are we going to do there well then height g we have a new winner is equal to x sub i that x sub i becomes the winner all right now that should give us the high grade and low grade so what do we need to do now we need to return high g and low g now we better make sure we got the order right there so i come down here yeah it's going to get high g low g so it's got them in the right order so that should work man could it really be this easy at least it's easy when you're watching me can you do it on your own all right so here we go and so now we're going to run this thing and let's see what we've got here we're going to say for we're going to say four grades click down here let me go control plus control plus and then let me kind of show you where the action is down here all right let me clear this and then run it again okay how many grades we're going to say 4 and then we are going to say 90 and then we're going to say 91 and then we're going to say 97 and then we are going to say 82. all right let's look at this so it says your it says your grades are 90 91 97 82 your average is 90 and we are denied ah you know what we did we returned them but we didn't print them we didn't print them so we need to say print and we need to say your high grade is like that and then we're going to comma high g we're going to comma high g and then we're going to copy this we're going to paste it ctrl v and then your low grade is over here low g okay low g like that all right so we did it last time but we didn't print it out so let's take a look now let me kill this and then let me run it up here okay how many grades we're gonna save five grades we're gonna say 90 we're going to say 91 we're going to say 82 we're going to say 87 and we're going to say 99 okay 1991 82 87 99 your average is 89.8 your high grade is 99 and your low grade is 82 who is your huckleberry i'm your huckleberry uh huh did you guys get it done okay guys if uh this is a lot of fun if you guys really really really tried and we're not able to do it on your own that's okay it's called learning but you've got to not just be happy oh yeah i saw him do it so i know how to do it now if you were not able to do it on your own until you watch me you need to turn this video off you need to start with a blank program and now you need to do the whole thing by yourself without watching me do it so best of all are you legend guys who do it before you see me do it but if you're kind of like folded up the first time get up dust yourself off but don't just move on to the next video you've got to do this by yourself without watching me do it okay does that sound fair all right so that's your homework is you guys that could not do it by yourself go back and do it by yourself starting from scratch without looking at this and then i do believe next week i think what we're going to do next week is we're going to look at classes at classes and methods and this is something that if you've ever looked at a python program it is just totally confusing the real pros who write python they do it with classes and if you haven't learned classes it's just like looking at a jigsaw puzzle it makes no sense well what i'm going to show you is i'm going to show you how to program in classes with very simple programs and then you're going to understand how that class method business worked it's called object oriented programming i'm going to kind of show you how to do object-oriented programming with classes and then when you look at the code written by the big boys you can understand what they're doing and it will make a lot of sense to you it really it's really very simple but if you just haven't seen it before you look at someone's code and it's class.init.self.this.that it's very very confusing we're going to make sense of it next week hey are you guys having as much fun with these lessons uh as i am i really love making these things i hope you guys will enjoy it i really love python we got some exciting stuff coming up i plan on merging python with arduino where you're taking data from the arduino but you're controlling it with python and then we're going to get into some really cool 3d graphics and a bunch of really really slick stuff so i hope you guys will stick with this because we're going to get to some very serious and very cool project based lessons coming up in the not too distant future okay guys this has been paul mcquarter with toptechboy.com if you guys like these lessons give us a thumbs up if you haven't subscribed already make sure you hit the subscribe button ring that bell when you do that so you'll get notifications when our next lessons are coming out and think about sharing this on your social media so we have more people doing something useful like coding and less people sitting around watching silly cat videos talk to you guys later
Info
Channel: Paul McWhorter
Views: 5,230
Rating: undefined out of 5
Keywords:
Id: RqM32mYUqPg
Channel Id: undefined
Length: 27min 44sec (1664 seconds)
Published: Wed Jun 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.