Enroll in The Complete Python 3 Course: Beginner to Advanced!
Click here to subscribe for more videos like this!
Alright guys, in this video we’re going to be talking about arguments a little further, we were going to move onto keyword arguments but before we do that it’s going to make more sense to cover default arguments first. So, let’s go ahead I’ve cleared out my script here I suggest you guys do the same just for repetition. I mean you could remodel your current function to work with this, however repetition is the key to mastering something. So let’s go ahead drop down two lines, define a new function, we’re going to name it “print_something” and it’s going to take two parameters, one is going to be “(name, age)” let’s open that with a “:” and print out one string which we’re going to concatenate with name and age. So “My name is name ” + name + ” and my age is ” + age” Now we’re going to go down here and call this function “print_something(“Nick” , 27)” I’m sure you guys already see a problem with this, so I’ll give you props for that because this is not going to work, that’s exactly why I’m showing you. We’re going to run it amd it’s going to say that it “Can’t convert ‘int’ object to a str implicity” Now there’s a few different resolutions to this, one I’ve already shown you, you can just wrap this in a string. Now you can run it and it will work. Now, this is why I mentioned this earlier because you can definitely do this, however what we’re going to do is instead of concatenating the string we’re going to remove all these extra characters, we’re going to separate things by a comma, going to remove the extra spaces around the text as well because we don’t need them and you’ll see why. Now this will work without converting this integer to a string, and the reason is because when we use commas it just says print out these four things one after another. We don’t need to concatenate different pieces of data, it’s actually printing out a different number of things. So, it’s going to print this out as a string, it’s going to print this out as a string because we’re passing it in as a string. This is going to be printed out as a string and this is going to be printed out as an integer, however when I save it here and run it you won’t know the difference. See it says my name is Nick and my age 27. You don’t notice that this is an int so this is a perfectly acceptable way to do that. Now, what if I only wanted to pass it one argument? What we can do there is we can create default arguments. Actually, let’s pass in “Nick” So, let’s go ahead and change these to accommodate only passing in some of the values. So, what we’re going to do is set a default value and how you do that is inside the parameter list of the function you’re going to assign values to these variables. These are just variables that you’re creating right here, so you can create a variable and assign a default name, and then same with age. You’re going to assign a default age which we’re going to say is “Unknown” Now we’re going to save it. Now when I run this what we have here is my name is Nick and my age is unknown because I’m only passing in this. So what happens is you think well, shouldn’t name equal someone? It would if I didn’t pass anything in so we can do that as well. We’re going to save this and run it and it’s going to say my name is someone and my age is unknown because we’re not passing in anything. However, we’re going to pass in the first argument here and this is going to take priority over this value here. This
basically means this variable is going to be equal to someone, if no value is passed in. Age is going to be equal to unknown, if no value is passed in for it. So, that’s how to use default arguments in functions. In the next video we’re going to be talking about passing in specific parameters by using keyword arguments.