What are the advantages of using functions over repeating/copying code?
True or False: Functions in Python always have a return value.
False, the output of a function is optional.
True or False: Not every function has to have input parameters.
True
What’s the difference between formal and actual parameters?
Formal: in the function definition
Actual: when calling the function (also called arguments)
True or False: If a list is passed to a function and then changed in the function body, these changes aren’t reflection outside the function.
False. If a mutable object (ie a list) is changed inside a function, the change is also reflected outside of the function.
What’s the difference between positional and keyword arguments?
What are variable arguments?
They are placeholders for an arbitrary amount of arguments.
For positional arguments: *args
For keyword arguments: **kwargs
In what order can variable arguments, mixed with normal parameters appear?
For the defined function “func”, what arguments belong to what parameters in the following examples?
def func(x, *args, y, **kwargs):
For the defined function “func”, what arguments belong to what parameters in the following example?
def func(x, *args, y, **kwargs):
my_list = [1, 2, 3]
my_dict = {“y”: 4, “z”: 5}
func(*my_list, **my_dict)
x=1, args=(2, 3), y=4, kwargs={“z”: 5}
What are default argument values?
A default value can be assigned to a parameter when defining a function. If the function is called without an argument, the default argument is used instead.
What are the parameter values for the function “func” for the following examples?
def func(amount=0, price=3):
What is type hinting?
Parameters and return values can have type hints that show what your function expects as input and returns as output.
They’re only hints though, you can still pass and return any type.
True or False: Once the return keyword is encountered in a function, the program is terminated.
False. Only the execution of the function is terminated and the output is returned.
What namespaces are there and in what order are they searched when looking for a name of an object?
In the following example, define the namespaces of the given object names:
x = str(12)
def func(a):
c = 10
return a + c
x: global namespace
str: built-in namespace
func: global namespace
a: local namespace
c: local namespace