Posted on 1 Comment

Arguments explained in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Click here to subscribe for more videos like this!

Alright guys, welcome back. In this video we’re going to expand on this function here and we’re actually going to implement arguments into the function. So what this means that this is very static right now, nothing would change this even if we wanted to change it. I could call this function let’s say 10 times and each time it’s going to print out very static strings. So how we can change this to make it more dynamic is by using arguments and the arguments again go in the brackets here at the end of the function name, and what you’re doing is creating a variable so we don’t even need to know the value that’s being passed in we just let’s decide to use two arguments here in this function, and each one is going to be used in its own print statement. So we’re going to name it “str1, and str2” there and this basically we’ve abbreviated string 1 and string 2. It also tells us right now it’s not being used. You’re going to find PyCharm pretty smart. So what we’re going to do is change this line of code to print out string 1, we’re going to use this print statement to print out string 2. Let’s go ahead and save it and run it. We’re going to get an error and the reason is because we’re calling this function without passing anything into it, and it actually tells us right here parameter string 1 unfilled. So it knows that this function is looking for two values here separated by a comma, one of them is string one and one of them is string two, we haven’t passed anything into it so it’s calling this function and immediately the function is freaking out saying I can’t find this information. So what we need to do is pass in two strings here and how we do that is similar to how it looks in here except each one is going to be the value and not the variable name. So, the first one is going to be called “This is argument 1”, “This is the second argument which is also a string.”) Now if we save, go-ahead run, you’re going to see that this is now printing out string 1 as defined right here, and string 2 as defined right here. So, why would we need to do this? Well, let’s say we’re always going to want to print out two things. Let’s go ahead and call the function again, but this time we’re going to pass in different strings. Let’s fall back to the good ole hello world. Now we’re going to save this and what’s gonna happen is it’s going to print out four lines. It’s going to print out “This is argument 1”, “This is the second argument which is also a string.” being called from the first call of that function, and then “Stringy” , “Hello World” from the second call that function and so that’s how to use arguments. Now in the next video we’re going to be discussing keyword arguments and what those are.

1 thought on “Arguments explained in Python

  1. Exactly what I was looking for, thankyou for putting up.

Comments are closed.