The most useful form is to specify a default value for one or more arguments. This creates a function that can be called with fewer arguments than it is defined to allow.
Finally, the least frequently used option is to specify that a function can be called with an arbitrary number of arguments. These arguments will be wrapped up in a tuple
python
def add_all(*numbers): print(type(numbers)) total = 0 for n in numbers: total += n return totalprint(add_all(1, 2, 3))
Output
<class 'tuple'>
6
Your output
Arbitrary Keyword Arguments (**kwargs)
When a final formal parameter of the form **name is present, it receives a dictionary β¦ containing all keyword arguments except for those corresponding to a formal parameter.
python
def print_info(**info): print(type(info)) for k, v in info.items(): print(f"{k}: {v}")print_info(name="Alice", age=30)
Output
<class 'dict'>
name: Alice
age: 30
Your output
Check yourself
Functions always have to return a value using the return keyword.
Reveal answer
If a return statement is omitted, Python automatically returns None. β If a return statement is omitted, Python automatically returns None.
You can mix positional and keyword arguments in any order when calling a function.
Reveal answer
In a function call, keyword arguments must always follow positional arguments. β In a function call, keyword arguments must always follow positional arguments.
Variables created inside a function are available globally.
Reveal answer
Variables created in a function live in a local symbol table and disappear when the function exits. β Variables created in a function live in a local symbol table and disappear when the function exits.
*args and **kwargs are reserved keywords.
Reveal answer
The single and double asterisks do the work. The names args and kwargs are just strong community conventions. β The single and double asterisks do the work. The names args and kwargs are just strong community conventions.
Challenges
Challenge 1 +15 XP
Write a function make_profile that takes a name and an optional status that defaults to active. It should return a dictionary with those keys.
Write a function build_url that takes a base string and arbitrary keyword arguments (**kwargs). It should print the base url followed by a question mark and the kwargs dictionary.
python
Test 1 β expects "http://test.com?{'q': 'python', 'page': 2}\n"
Need a hint? (β25% XP)
The **kwargs parameter captures all extra keyword arguments as a dictionary.
The Same Seven Numbers, Two Trees: Why Insert Order Decides Lookup Cost β Insert seven numbers into a binary search tree in sorted order and you get a seven-level chain. Insert the same seven in a different order and you get three levels. Build both here, and watch what the shape costs on every lookup afterwards.
A Rotation Is Just Re-Hanging Three Subtrees, and the Order Never Changes β Self-balancing trees are usually taught as four case names before anyone says what a rotation does. Insert the same values with rebalancing on and off here, and watch a tree that would be seven levels deep stay at three.
Why Databases Use B-Trees: Fewer Levels Beats Fewer Comparisons β A balanced binary tree of a billion keys is thirty levels deep. If every level is a disk read, that is thirty reads for one lookup. Drag the branching factor here and watch the same keys collapse from a tall thin tree into a short wide one.
Find the Wasted Calls: the Skill Behind Every Memoisation β Memoisation is easy to explain and hard to apply, because the difficult part is noticing that a subproblem repeats at all. Here the recursion tree is drawn and you have to find the repeats yourself, scored, with wrong clicks counted.