Posted on

Infinite Arguments in Python

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

Click here to subscribe for more videos like this!

Alright guys let’s go ahead and talk about an infinite number of arguments being passed into a function. So what we’re going to do is define a function called “print_people” and here’s where we’re going to pass in the arguments for a function but we don’t know how many were going to get, we may get 3, we may get a 100. So what we’re going to do is start off we’re going to only pass one argument into this function and that’s going to begin with an “asterisk” and this “*” tells this argument that it’s going to be an array of all of the arguments that are passed into the function, so you’re going to see more how this works in a moment. Now with an array we need some way to loop over it and so we’re going to be using a for statement and we’re going to go more into this in the future, but for right now all you need to know is you’re going to write “for person in people:” and you’re going to notice that when i drop down to the new line after putting the colon here it further indented my code which means what I’m writing here is going to be a different block, it’s going to be one level deeper than right here so, and that’s not sure why that’s underlying red maybe because it’s expecting me to type something here. So what I’m going to do is “for person in people:” I’m going to “print(“This person is” , person)” there we go. So that’s our function, we’re done with that. Basically how a for loop works is this is a list or an array as we’re defining right here. This is going to take in all of the values we’re passing to this function and create a list called people. Now you can iterate over the people list by using a for statement, so for person in people and this person can be any you could write this for item in people, but where we are using the variable name person that is going to be available in this block of code as the next person in the list and so you guys are going to see how this works. Again if you’re familiar with other programming languages this is not a new concept, it’s just a slightly different way of doing it Let’s go ahead and call the function “print people” and let’s pass it some names so “(“Nick” , “Dan” , “Jack” , “King” , “Smiley”)” I’m not even sure if this is a real persons name but we’re going to pass it into this function anyway. So what we’re doing is we’re passing in 1, 2, 3, 4, 5 arguments. Now if we knew we were always going to expect five arguments we could accept each individual argument as its own variable, however we don’t so we’re capturing all these, we’re going to create an array out of them, and the array is going to be stored in the variable called people. So we’re going to run this and you’re going to see that for each person it prints out this person is and then the name of the person. So that’s how to include an infinite number, or a flexible, it’s not really infinite eventually if you were going to pass in an infinite amount of parameters you would never be done with your program, you could pass it on to your descendants they would never be done, and so on. This is truly a flexible number of arguments. So, with that being said we’re all done with that. In the next video we’re going to be discussing return values and then we’re going to build a calculator.