Functions in Python - Beginner Python Tutorial #3 (with Exercises)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys and girls and welcome back to another Python tutorial video in this video we're going to be having a lot of fun and that's because we are going over functions functions allow you to simplify your code by breaking it into small reusable components and they're used super frequently when you're programming before I go through functions will do that challenge problem that I introduced in the last video and then finally we'll end this video by doing a couple of practical function examples where we kind of have we we use all of the skills we've learned thus far and try to apply it to solve some problems as always the video outline will be posted in the comments so make sure to check that out if you want to find a specific thing that we go over and please if you like this video or learning anything from this series throw me a big thumbs up and also subscribe so that you don't miss any future tutorials alright as I always open up a new sublime text 3 window and save a new Python file I'm just calling this lesson three dot py and to begin we're going over that problem that I introduced in the last video so we're trying to find the we're gonna try to write a set of conditional statements that identifies numbers as special numbers and special numbers are defined as numbers less than 100 or greater than or equal to 300 that are perfectly divisible by 3 7 or both so let's begin just by defining a number as and then like and around 123 we'll miss around with this as we get it going so what we're gonna do for this set of conditional statements is I think it's important we should begin by just identifying whether or not it is a special number and then we'll go into breaking down case by case so if it's a special number then it has to be begin with this line here has to be either less than 100 or number has to be greater than or equal to 300 and that's just this line right here so I'm gonna group that together because only one of these things has to be true but if it is a special number one of these like def does have to be true so we got that then the second part of something being a special number is that it's perfectly divisible by three Sevenfold so we need to do and so we're now leading it to the second condition we have number in mod three so that does divide it by three then take the remainder equals equals zero so that makes it perfectly divisible or number mod seven equals equals zero and this statement over here will be true if it's either divisible by three divisible by seven or if these are both true it still is true so this now identify is if it's a special number so I'm just going to say print a special number and then we can write it down here hopefully you can see this else print and then I'm just gonna copy what I told you to say not a special number whoa these quotes are weird copy and pasting didn't work for me all right not a special number so we should all have this on the screen right now so 123 would not be a special murmur because it's not less than 104 greater than equal to 300 so if I try let's say 317 that now it's just not perfectly divisible by 3 or 7 but it would meet this qualification so let's just try something we know that's divisible by 3 so I'm gonna do 303 and that would be 101 times 3 so this should say special number and that looks good and when we're writing something like this make sure you play around and try to test all the edge cases to make sure it's actually working properly so we have it identifying it's a special number or not now we just need to write a set of nested if statements to figure out if what condition it is so I'm gonna start with if number mod 3 equals equals 0 and number mod 7 equals equals zero then it would be this case right here divisible by both so I'm just going to copy that in and paste it here then we could do else if so Elif number mod 3 equals equals zero so right now if it didn't pass this condition then we know it's one or the other because it did pass this condition so I'm going to now just so number mod 3 equals equals zero and if that's the case then because it didn't pass this above condition it must only be divisible by perfectly divisible by three so I'm going to copy that and print that out we need quotations there we go visible y3 and then finally you could either write Elif number mod seven equals equals zero but that's actually not necessary you can just do else because we know it's either divisible by three or seven from this statement up above so we can do prints divisible by seven and then we should just this is this is the completed challenge problem but you should always just confirm that it's working divisible by three cool if we think about it let's say that's what does the number that is divisible I'm gonna just comment all this out real quick I'm gonna find a number that's divisible by seven perfectly so we'll just do like seven times seven years or 50 that'll be three hundred fifty and we know that that works that's above three hundred so we're actually we could just do a number like twenty one just to make sure it does the both condition because that's less than 100 but divisible by both so we'll try a number equals twenty one and see if that works divisible by both cool and then a number that wouldn't be divisible by three would be someone like 70 but it would be divisible by seven cool it all looks good and you could more robustly test this if you wanted to but I think it looks good and also testing is a feature of programming that is super important and I'll make sure that I'd make a video on like the proper way to test your code in the future okay now that we've done that let's move into functions the main focus of this video so I wanted to delete all of this code here I might post it on my github if you're looking to just get the final code so check the description for that delete save okay now we have a brent blank slate I'll make this a little bigger too okay so functions so to introduce the functions I want to show you a concrete example of why they're so useful so here I have a turtle program and what this turtle program does is it builds this animation so it's just drawing Pentagon's on the screen and if you've never seen the Python turtle package I really recommend checking it out it's a fun way to play around with your Python skills I've made a video kind of going over everything you can do in it and I'll post that link straight above me right here so check it out if you haven't already okay so that's what happened it ran these Pentagon's and the code for that is all over here on the left side so as you can see it's a lot of code it's a hundred and forty five lines here but if you look closely you know I'm moving the pen I'm making a turtle called Bob setting his speed in color and then I'm setting his position initial position and I'm drawing Pentagon right here and then I move locations draw another Pentagon here move locations again draw a Pentagon and every time I draw this Pentagon it's the same exact thing like it's kind of seems silly that I have to copy and paste so many times when I'm doing the same exact thing to draw a Penta so this is why we use functions so this was our kind of our code without functions and now I over here on this tab I wrote the same exact code it does the same exact thing you can see here wow this is so fun does the same exact thing but now we have this function called draw Pentagon which takes in the turtle which would be the turtles name which in this case would be Bob then has an x and y variable that tells it where to start the Pentagon so this is the exact same thing as we're doing here with this go to but now we've replaced 50 and negative 50 with X&Y placeholders you know if you don't understand this fully right now don't worry about it I'm gonna break down how to actually write a function in a second I'm just showing you that I took this same exact code from this put it over here made this a function and then I was able to get everything in there within 36 lines so we just cut down over a hundred lines by using function so pretty useful and also it's easier to read now now I just see I'm drawing a bunch of Pentagon's as opposed to like all this kind of messy set of lines okay so that's a function I'll put this code in the description as well on my github page okay let's write our first function on this blank file so we're gonna define a function called a span and to do that we would write def spam parentheses and then colon so this def whenever you writing a function you have to start with def which means defined then saying we're defining a function called spam so this can be whatever name you want it to be here and then open and close parenthesis and then if you need to like pass things into your function you can like list them off here and I'll show you how to do that in a second we're doing a really basic exempt right now and you're gonna see why it's called spam in a second so measured I'm trying to write a function to just spam you guys with with messages I can do define spam and then I could have it print out something like subscribe Keith he is the best so if I was trying to spam you I could now save that and also know that just like if statements this has to be indented inside the function name so now what this means is that whenever I call spam parentheses spam parentheses it will actually print out subscribe keith he is the best so if I really wanted to spam you I could just keep copying and pasting that so right now I have it like 10 times save that and as you can see it copied that 10 times and printed it out so that's a really basic function let's that move into something a little bit more I useful I guess unless I guess this maybe is useful for me all right got spam let's uh let's write a function that cubes a number so we pass in a number and it returns that number cubed so like if we passed in 3 it would do 3 times 3 times 3 which is 27 so we're gonna call this cube or we'll call it cube number Oh what the heck happened cube number and in this case we have to pass in the number we want cubed so in these parentheses we write like something like X and this can be anything that you write in this parenthesis it doesn't have to be X it could be number or something it's just this this spot right here just acts as a placeholder so def cube number and I'm just going to make it X just to be to make it simple and I could do something like so print X and then to exponent are two asterisks is to the Q to the root of three so this is X to the third power and now if I call cube number of three it right it prints out 27 so there's one thing you that you would kind of I feel like I soon when you're writing functions you use this print statement so it's actually not the case that we want to use the print statement in the functions we actually want to use a statement called return and I'm gonna show you why right now so if I left this as print let's say I wanted to set a variable just called a or something like as the cube of three now if I try to print a you'll see that if I leave this as print it doesn't actually print out a so I'm gonna it says none here right and the reason is is yes it printed out 27 but printing is not allowing us to capture that value so we really want to capture that value what we should use in functions and this is like you definitely use this in functions you don't use printing functions unless you're just like checking something you use return so I'm gonna do return X to the third power and now if I run this you see it just says 27 and that's because right now it didn't print return doesn't print it out automatically it only printed it once we set it to a and now it leaves us with 27 ok so that's a very simple example and just to note like as I told you before you can change this X to anything you want I could change it to something like dilly dilly and if I returned dilly dilly to the third power it still doesn't give me an error and I could even show you I could raise this like 7th power so you know it's like actually changing it up and as you can see working with dilly dilly it would work with something like John Cena really anything you want to put in here it would work as long as you're consistent with this is what you pass in this is what you passed into the seventh power that's what we want back so this would still work so let's do the square could be nine that still works but as the convention you probably shouldn't use John Cena as your variable name you should pass in things that are descriptive so X is like descriptive enough it just knows that it's just like a number of placeholder you could also do something like numb and you am to specify this is the number you're passing in so be descriptive with these variable names that's the basic example alright let's move into it a little bit more of an exam a little bit more complicated in examples still pretty straightforward you could also do something like define get some so let's say we wanted to maybe take a second and try this on your own say we wanted to be able to write a function that took three numbers and added them all together so whatever free numbers we took it would just combine them and return the combined sum so try to write that function real quick okay I'm gonna write it right now so I'm gonna define a function called get some that's kind of a fun name get some and this will take in three values I'm just gonna call them for simplicity sake a B and C and so now if I wanted to find the sum of a B and C I would return so these are placeholders for three numbers I would have just returned a plus B plus C and if I then I would have so what am i what's happening if I ran this doesn't do anything because we haven't actually called the function we've just defined it but I could do get some let's do one seven and like three or something so one plus seven would be eight plus three would be 11 so this should hopefully return us 11 we run that oh my god I was just fell back it doesn't actually say anything yet because return does not print it just returns the value so that we can print it out so we could do something like print get some 173 and as I said before this year return 11 see if it does yes it does cool so as you can see get some 173 turns 11 if I tried something like maybe I try to add my name in there that's gonna give us an error because you can't add a string in a name or numbers in a string but we actually could change this function to something like sergeant on this gun I could change this function to be something called like combined name so instead of now adding numbers let's say we wanted to like take someone's name we had their first the middle and their last name and we wanted to just shorten it and like combine it as one word so I could do pretty much the same thing so when we're adding strings if I did something like teeth plus galley that's my last name I could do print Keith plus galley and as you can see down here it combines those two things and I also could add a space in there by doing Keith oh my god what happened I can't type so hard with all this pressure of being filmed Keith space galley so we could take something like combining of a B and C and I could do like and let's say so I could do something like let's say our name was Neil Patrick Harris we'll take that as our name so a combined name of passing in the a B and C here Neil Patrick Harris and to be more descriptive we should instead of calling us a B and C we should call this first middle and last it doesn't matter what we call it but now it's like okay you're gonna just add together the first middle and last names here last and if I run this didn't print anything yet but I could do full name equals combined name Neil Patrick Harris and I could print full name and as you see it says you know how true Harris if we wanted to add the spaces we just do quotations with space in between Oh what the heck happened I didn't add here now I got this and one thing I'll show you that's just something we'll be doing in the future just kind of a cool little thing to kind of we'll work towards is you can do all sorts of things on strings so I could just get the first letter of Patrick by doing middle bracket zero and don't worry about understanding this at all I'm gonna cover it in a video on strings that I'll post in a few weeks okay so this is functions in a nutshell let's actually like test our skills and do some practice problems will be going on a site called coding bat comm I think this is a really good site to kind of work on the skills that you've been learning in these videos so definitely try to do some of these exercises on your own I'll post the link in the description so we're on coding bat comm slash Python and we're gonna click on the warm-up section so click warm-up one and we'll start out by doing the problem monkey trouble nice it's fun named monkey trouble alright so we have two monkeys a and B and the parameters a smile and B smile indicate if each is smiling we are in trouble if they are both smiling or if neither of them is smile return true if we are trouble okay so take a sec try this problem and then unpause the video when you're ready to see the answer okay solve this problem we're gonna have to use our knowledge of if and else if and else statements that we've learned in the previous video so we have the function monkey trouble a smile B smile and it says return true if they're both the same or return false if they're opposites so false is we're not in trouble so we'll return false first if they're not the same so we can say something like if a smile and not B smile so this would say if this is true and this is not true so this is false then we want to return false and then we could write L if not a smile and B smile so this would be a smiles false and B smile is true we could write it return false and this is just the first way we can solve it I'm going to show a simpler way to solve that in a second and we could finally say the other the other case would be they're equal to each other so they're both false are both true so we just write else return false return true and if I run this what happened I try to save it oops go it did get all cracked cool but we're actually doing a little bit more than we needed to here so the kind of neat thing to see is that we don't have to do this not stuff here we can just do if a smile equals equals B smile so this would mean if they're both true or if they're both false we want to return true because that's when we're in trouble otherwise we want to return false oh my god it just did control eyes again and now if I run this they're still all correct and we're still not quite done we can do it one or even simpler way what I could write is instead of even having any if statements I could just write return a smile equals equals B smile because this will evaluate to true or false and we want this to be false if we if they're equal to each other oh what the heck turn a smile equals P smile yeah so or we want it to be true they're equal to each other we are in trouble so we could all we have the return is just whether or not they're equal to each other because the cases that they're not equal to each other this statement right here would be false and we get all of the answers correct so this is the simple list best answer you could have for this one all right let's do one more problem before we end this video so we'll do some double so given two int values return their song unless the two values are the same then return double their song so to begin let's just do the first thing we'll just return the song and this is the same thing as get some but we just wrote so we can just do return a plus B right oh my god I keep pressing ctrl s and as you can see it didn't quite work and that's because we didn't take into account if the values are the same so what we can do is we'll do if a equals equals B then we want to do one thing and then if a is not equal to B then we just return the sum as is normal so if a equals equals B we want it to return double the sum so we would return we do this multiple ways we could write this line and that's kind of a cool thing about these exercises and about coding in general there's not just one answer you can do things tons of different ways so I'm going to do it this way I do two times a plus B but note that because a equals B you could just do like two times two times two times a or four times a there's many ways to do it so a equals being returned two times to some otherwise trying to some run that we got it all working cool cool alright guys that's all I'm gonna cover in this video hopefully you get a you feel like you have a good grasp of what functions are definitely recommend keep doing problems on this coding bat website and if you have any questions on how to do specific examples in here feel free to let me know in the comments and I'll help you out I'll post the next tutorial video by next week we'll play go over either four and while loops or we'll go over lists and Python so be sure to subscribe to not miss that if you learned something throw a big thumbs up this video I think that's it so peace
Info
Channel: Keith Galli
Views: 26,174
Rating: 4.9546742 out of 5
Keywords: KGMIT, Keith Galli, python 3, python programming, python tutorial, easy python tutorial, quick python tutorial, functions in python, how to write a function in python, python for kids, simple, best, 2018, python tutorial 2018, python programming series, how to program in python, how to program, python 3 for beginners, python for beginners, functions, sublime text 3, sublime text, programming, easy, studying python, python exercises, python tutorial with exercises, python practice
Id: 5U95tRdYySA
Channel Id: undefined
Length: 27min 1sec (1621 seconds)
Published: Sat Mar 03 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.