Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Due to its power, simplicity, and complete object model, Python has become the scripting language of choice for many large organizations, including Google, Yahoo, and IBM. In Python 3 Essential Training, Bill Weinman demonstrates how to use Python 3 to create well-designed scripts and maintain existing projects. This course covers the basics of the language syntax and usage, as well as advanced features such as objects, generators, and exceptions. Example projects include a normalized database interface and a complete working CRUD application. Exercise files accompany the course.
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.
Find answers to the most frequently asked questions about Python 3 Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
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.