Hands On 1¶
Questions¶
- How do you import a module in python?
- How do you assign a value to a variable?
- What is the difference between
a = 42anda == 42 - How do you call a function?
- How do you call a function from a module?
- How do you define a list?
- What is the difference between
res = thing(1)andres = thing[1]?
Tasks¶
- repeat the things we did, type in the commands yourself and experiment with them.
- What does the following program print
a = -1 b = 2 c, d = b, a print(c > 0) - Implement a simple formula from your own domain as a python script.
- Calculate the mean of the following numbers:
2,3,5,7,11,13- manually by tipping the calculation
- using a list and
numpy.mean
Hands On 2¶
Questions¶
- What does
plot([0, 0, 1, 1], [0, 1, 0, 1])display? - What kind of attributes does an object have in python? What is the difference when accessing them?
- Explain in your own words what this code prints and why:
my_list = [1,2,3] my_other_list = my_list my_other_list[1] = 0 print(my_list) - What methods are available to get help?
- How can you add a comment to python code?
- Slicing also works on lists and strings. Given
element = 'oxygen'what do the following commands return:element[:4]element[4:]element[:]element[-1]element[-2]element[1:-1]
- What is the result of the two print statments in the following code:
my_list = [1,2,3] my_array = np.array([1,2,3]) print(my_list * 2) print(my_array * 2)
Tasks¶
- Repeat the things we did, type in the commands yourself and experiment with them.
- Replace
refugees-europe_0.txtwithrefugees-europe_1.csv, adapt the call toloadtxtaccording to the new input format. - Get a general idea of the functions provided by numpy. (use tab-completion and the help-system)
- Plot the refugee population of Germany over the periode from 2000 unitl 2010.
- Use the
axes.set_ylimandarray.maxto make sure the y axis of your plot starts at0and includes all data. - Draw a horizontal line into your plot, indicating the mean.
- Use
numpy.diffto plot the change in refugees instead of the absolut value.
Hands On 3¶
Questions¶
- What is the basic structure of a for-loop?
- What type of objects can you use a for loop on?
- Explain the functions
enumerateandzip. - How do you use
if,elif,else? - How do you define a function?
- What variables are availabel in the different functions of the last example.
- What is the difference between
returnandprint? - What is intendation good for?
Tasks¶
- Repeat the things we did, type in the commands yourself and experiment with them.
- Write a loop that calculates
2 ** 8with simple multiplication. - Look at the help for
rangeand create a loop that prints a countdown. - Adapt the compound interest code:
- let it use a sensible default value for
balanceandtimeif these values are not specified. - let it calculate the result for a fixed list of interest rates (e.g.: 0.5, 1., 2., 4., 8.) instead of reading a single value from the terminal.
- calculate the results for each interest up to 15 years and plot the result.
- let it use a sensible default value for
- Define a function that calculates some basic statistics (min, max, mean, standard deviation) for the passed data and returns a list with them.
- Use the function from the previous task to print these statistics for all countries where the mean is above 10000.
- Adapt the refugees code such that the user can choose the time range to plot.