0
0
Pythonprogramming~5 mins

Variable-length arguments (*args) in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does *args mean in a Python function?

*args allows a function to accept any number of positional arguments as a tuple.

Click to reveal answer
beginner
How do you access the third argument passed using *args?

You access it like a tuple element: args[2] (index starts at 0).

Click to reveal answer
beginner
Can a function have both regular parameters and *args?

Yes, regular parameters come first, then *args to catch extra positional arguments.

Click to reveal answer
beginner
What type is args inside the function when using *args?

args is a tuple containing all extra positional arguments passed.

Click to reveal answer
beginner
Write a simple function using *args that prints all arguments.
def print_all(*args): for arg in args: print(arg)
Click to reveal answer
What does *args collect inside a function?
AA dictionary of keyword arguments
BA tuple of extra positional arguments
CA list of all arguments
DA single string argument
How do you define a function that accepts any number of positional arguments?
Adef func(args):
Bdef func(args*):
Cdef func(**args):
Ddef func(*args):
If a function is defined as def f(a, *args):, what happens when you call f(1, 2, 3)?
AError because of too many arguments
Ba=(1, 2, 3), args=empty
Ca=1, args=(2, 3)
Da=1, args=2
What is the type of args inside the function?
Atuple
Blist
Cdict
Dset
Can *args be used to accept keyword arguments?
ANo, <code>**kwargs</code> is for keyword arguments
BYes, <code>*args</code> accepts keywords
COnly if keywords are strings
DOnly if keywords are numbers
Explain how *args works in a Python function and give an example.
Think about how to catch extra inputs without naming them all.
You got /3 concepts.
    Describe the difference between regular parameters and *args in function definitions.
    Consider how many arguments each can take.
    You got /3 concepts.