You may not always know how many arguments will be passed to a given function. Variable argument lists provide the ability to pass an arbitrary number of arguments to a function.
- [Instructor] Python functions allow variable length,…argument lists like the variadic arguments…in C and other languages.…Here in Komodo I've opened a working copy…of args.py from chapter seven of the exercise files.…And you'll notice down here,…in our definition for our function,…we have a variable name with an asterisk before it.…This is the variable length argument list.…And you'll notice that we can treat it like a sequence,…it's actually a tuple.…
So I check and see if the length is greater than zero.…Remember that zero is false,…anything greater than zero is one,…and if it is then I print each of the items in the list,…otherwise, I just print meow.…And so when I run this you notice we get the three arguments…that we had passed it.…And if I call without those arguments,…we just get meow with a period,…just as it is here in this print argument.…It's traditional to name your list args, A-R-G-S,…and I recommend that you follow that tradition.…
That way, when people are reading your code,…it's obvious what you're doing.…
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficiently (2017)with Michele Vallisneri2h 15m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.