Exercise
Contents
Exercise¶
Answer the Questions¶
Which value does
hello_world
return?
def hello_world():
print("Hello World")
what is the difference between
hello_word
andhello_world_string
?
def hello_world_string():
return "Hello World"
What is the type of
hello_world_string
andhello_world_string()
?
Write a function with three arguments. Execute this function with different combinations of keyword and positional arguments. What is allowed, what is not? What rules can be derived from that result?
Write a function with two arguments. One should have a default value. At which position does it need to be placed in the signature?
Describe the behaviour of the following program. Change the code such that the function always returns the same data.
def append_element(e, l=[1]):
l.append(e)
return l
print(append_element(2))
print(append_element(2, [5]))
print(append_element(2))
print(append_element(2))
Of what type are the parameter
args
andkwargs
in the following function?
def func_pos_args(*args, **kwargs):
print(args, kwargs)
Read PEP257 and write a proper
docstring
for the functionappend_element
from above.
Swap the order of instructions in the body of the function below. What happens?
s3 = "I hate {}"
def f(city):
s3 = "I love {}"
print(s3.format(city))
f('Paris')
print(s3.format('Paris'))
Coding exercise¶
Write the following functions with reasonable default values and a docstring:
A function which checks, if a coordinate tuple lies within a circle with centre
M
and radiusr
(in 2D).
A function which returns the angle between two vectors in radians.
A congruencial generator with default values from “Numerical Recipes”.
A function which returns the refractive index of water and ice for wavelengths between 0.2 μm and 1.2 μm. Conditions as they are typically on earths surface (e.g. right now) should be used as default.