Exercises
Arguments
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Call describe with keyword arguments so the order does not matter.
Python
def describe(name, age):
print(f"{name} is {age}")
# call it with age first, by name
Name each argument at the call site.
def describe(name, age):
print(f"{name} is {age}")
describe(age=36, name="Ada")Exercise 2Passed
Fix the shared default so each call starts with an empty list.
Python
def add(item, target=[]):
target.append(item)
return target
print(add("a"))
print(add("b"))Use None as the default and build the list inside.
def add(item, target=None):
if target is None:
target = []
target.append(item)
return target
print(add("a"))
print(add("b"))Exercise 3Passed
Call describe by unpacking the dictionary into keyword arguments.
Python
def describe(name, age):
print(f"{name} is {age}")
details = {"name": "Grace", "age": 45}
# call describe using details
** spreads a dictionary into keyword arguments.
def describe(name, age):
print(f"{name} is {age}")
details = {"name": "Grace", "age": 45}
describe(**details)