Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Sometimes when you're defining a function, you might need an arbitrary number of arguments. Arguments that aren't necessarily going to be used every time and aren't necessarily going to even be named, perhaps a list of things. Python has a facility for this and let's see how that works. We'll go ahead and make a working copy of functions.py, call it functions-working.py and we'll open that working copy up and here we have our little test function.
And let's say that we just want to give it an arbitrary list of arguments and so that's done simply like that. The asterisk is special in this place as it means that this is just a list of optional arguments. You can have before it-- you can have a named argument or several of them. you could have this, that and the other, and then after all of that you can have your optional arguments, and so when you call it you might need to give it some actual values there and then you could have your arbitrary number of optional arguments like that.
So if we want to list those, we can just get rid of this text here and say this, that, and other, and if we save that and run it, you'll notice that we get the 1, 2 and the 3. What about these other ones? If I say args, what I'll get is I'll get a tuple with those values. Save that and run it and there is our tuple. So you can see that it's just a normal tuple, as in Python, and so I can use it as an Iterator if I want to. I can say for n in args like that, print n, end, equals blank or let's give it a space like that. And I can save it and run it and now we have our arbitrary arguments after our named arguments.
So, it's good to keep in mind that this is a tuple. That means that it is immutable, can't add to it or change anything in it, but it is an excellent way to get an arbitrary list of arguments into a function.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98235 Viewers
61 Video lessons · 85544 Viewers
71 Video lessons · 69495 Viewers
56 Video lessons · 101838 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.