All content is publicly available under the Creative Commons Attribution License: https://creativecommons.org/licenses/by/4.0/ --- # Software Carpentry Workshop - HIFIS - Helmholtz Federated IT Services - April 22/23, 2020 ## Important Links ### Zoom Join Zoom Meeting https://zoom.us/j/552801192?pwd=jqUVTd-TCY9LwBqvWvLi-4SnJA02Bw Meeting ID: 552 801 192 Password: 210933 One tap mobile +493056795800,,552801192#,,#,210933# Germany +496950502596,,552801192#,,#,210933# Germany Dial by your location +49 30 5679 5800 Germany +49 695 050 2596 Germany +49 69 7104 9922 Germany +41 43 210 70 42 Switzerland +41 43 210 71 08 Switzerland +41 22 591 00 05 Switzerland +41 22 591 01 56 Switzerland +41 31 528 09 88 Switzerland +43 12 535 501 Austria +43 12 535 502 Austria +43 670 309 0165 Austria +43 72 011 5988 Austria Meeting ID: 552 801 192 Password: 210933 Find your local number: https://zoom.us/u/aiI7xleSD ### Event Page https://hifis.gitlab.io/2020-02-22-hzdr ## Day 1 - Python ### Quick Check: Is IPython Working? Put in your initials below once you have started IPython: * KW * MD * AS * PE * GH * SP (i'm using jupyter notebook) * BN * JH * RT * LP * TA * JR (trying with Jupyter) * IF * IJ * NA * ### How to Exit IPython? Use the command `exit()`. Confirm with the _Enter_ key my_weithle ## Episode 3 - Built-In Functions ### Individual Exercises #### Exercise 3.1: What Happens When? 1) Explain in simple terms the order of operations in the following program: when does the addition happen, when does the subtraction happen, when is each function called, etc. ```python radiance = 1.0 radiance = max(2.1, 2.0 + min(radiance, 1.1 * radiance - 0.5)) ``` 2) What is the final value of radiance? ##### Post your solutions: * GH: 2.6 * IJ: 2.6 * TA: 2.6 * NA: 2.6 * SP: 2.6 * IF: 2.6 * RT: 2.6 * PE: 2.6 * LP: 2.6 * JR: 2.6 * AS: 2.6 * BN: 2.6 * MD: 2.6 #### Exercise 3.2 - Spot the Difference 1) Predict what each of the print statements in the program below will print. ```python easy_string = "abc" print(max(easy_string)) rich = "gold" poor = "tin" print(max(rich, poor)) print(max(len(rich), len(poor))) ``` 2) Does `max(len(rich), poor)` run or produce an error message? If it runs, does its result make any sense? ##### Post your solutions: * IF: 1) 'c' ; 'tin' ; 4 * 2) an error, since it compares the number 4 to the string "tin" * BN: 1) first print: c; second print: tin; third print: 4 2) two different types of variables (int, str) --> error * TA: 1) print(max(easy_string)) # return "c" print(max(rich,poo TAr)) # return "tin" print(max(len(rich), len(poor))) # return 4 2) error # cannot compare two different types (int and str) * MD: 1) c, tin, 4 * 2) it should show an error message * SP: 1) c, tin, 4 2) It produces an error. if we add len() to both rich and poor it gives 4 * JH: 1) c; tin ;4 2) Error, different types? * IJ: 1) c; tin; 4 2) error because len is int and poor is str * LP: 1) it prints "c", 2) prints "tin", 3) prints 4 * NA: 1)c, 2)print tin, 3) prints 4 (no error) * RT: c, tin, 4 * KW: 4) 4, * AS: 1) c, 'tin', 4 2) Produces error, comparison two different types #### Exercise 3.3 - Last Character of a String If Python starts counting from zero, and `len(…)` returns the number of characters in a string, what index expression will get the last character in the string name? ##### Post your solutions: * IF: len(..) - 1 * TA: len(...)-1 * GH: len(...)-1 * KW: my_name[len(my_name)-1] * IJ: len(...) - 1 * MD: int(len(name)-1) * AS: [-1] * BN: len(var)-1 * JH: [-1] * NA: my_name[-1] * RT: len(variable)-1 * JR: myString[len(myString)-1] * PE: len(var_name)-1 ## Episode 4 - Lists ### Individual Exercises #### Exercise 4.1 - Fill in the Blanks Fill in the blanks so that the program below produces the output shown. ```python values = ____ values.____(1) values.____(3) values.____(5) print('first time:', values) values = values[____] print('second time:', values) ``` **Output** ``` first time: [1, 3, 5] second time: [3, 5] ``` ##### Post your solutions: * BN ```python values = [] values.append(1) values.append(3) values.append(5) print('first time:', values) values = values[1:] print('second time:', values) ``` * AS ```python values = [] values.append(1) values.append(3) values.append(5) print('first time:', values) values = values[1:] print('second time:', values) ``` * JR ```python values = []; values.append(1) values.append(3) values.append(5) print('first time:', values) values = values[1:] print('second time:', values) ``` * SP: ```python values = [] values.append(1) values.append(3) values.append(5) print("first time", values) values = values[1:] print("second time", values) ``` * KW ```python values = [] values.append(1) values.append(3) values.append(5) print('first time:', values) values = values[1:2] print('second time:', values) ``` * IF ```python values = [] values.append(1) values.append(3) values.append(5) print('first time:', values) values = values[1:3] print('second time:', values) ``` **Output** first time: [1, 3, 5] second time: [3, 5] * JH: ```python values = [] values.append(1) values.append(3) values.append(5) print('first time:', values) values = values[1:] print('second time:', values) ``` * MD: values = list() values.append(1) values.append(3) values.append(5) print('first time:', values) values = values[1:] print('second time:', values) * TA: ``` python values = [] values.append(1) values.append(3) values.append(5) print('first time:', values) values = values[1:] print('second time:', values) ``` * PE ```python values = [] values.append(1) values.append(3) values.append(5) print('first time:', values) values = values[1:] print('second time:', values) ``` * GH ```python values = [] values.append(1) values.append(3) values.append(5) print('first time:', values) values = values[1:] print('second time:', values) ``` * LP ```python values = [] values.append(1) values.append(3) values.append(5) print('first time:', values) values = values[1:] print('second time:', values) ``` *NA values = [] ...: ...: values.append(1) ...: values.append(3) ...: values.append(5) ...: print('first time:', values) ...: values = values[1:] ...: print('second time:', values) first time: [1, 3, 5] second time: [3, 5] ## Things, we liked: * LP: very clear and detailed. It is quite important to understand the basic sintax of the language * SP: it was really clear, thank you! * BN: thanks, everything clear, plenty of informative demonstrations. I am looking forward to the if-exercises * MD: exercises, hands on experience doing some programming * JR: good speed, nice examples * NA: Great, thx, I learned a lot...was always affraid of python (:), more about plots and graphics * TA: acceptable speed for beginner, excercises help a lot to review what have been covered. Thank you * IJ*: was nice to try some Python programming * IF: everything is clear and well presented, you can go even faster :) * * MV : very good hands -on traning. Nice teaching . thanks. cu * KW Thank you all, very good programme * * improved: ## Improvements: * LP: please go a bit faster, and do more exercises * KW nothing, everything fine - maybe topic for tomorrow: what about dataframes? * :) * MD: since python usually work in scripts, maybe show us some programming in whole script (making some small scripts ourselves) like in spyder * SP: maybe more exercises * TA: more exercises, may be more structured exercises that belong to a larger project * * * NA: more about plots and graphics, more exercises, and possible extend the workshop * IJ: more exercises would be nice * * JR: more exercises, that are based on the Software Carpentries website -> helps to follow up the lessons * ## Day 2 - Python ### Please Say Hi on day 2! Just add your name in the list **below**! * NA * PE checks in * GH * MD * TA * IJ * IF * BN * SP * JR * AS * MV * CK * JH * LP #### Exercise 6.2 - Trimming Values Fill in the blanks so that this program creates a new list containing `0` where the original list’s values were negative and `1` where the original list’s values were positive. ```python original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = ____ for value in original: if ____: result.append(0) else: ____ print(result) ``` **Desired Output:** ``` [0, 1, 1, 1, 0, 1] ``` ##### Post your solutions: * LP: ```python original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = [] for value in original: if value <0: result.append(0) else: result.append(1) ``` * KW ```python original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = [] for value in original: if value < 0: result.append(0) else: result.append(1) print(result) ``` * MD ```python original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = [] for value in original: if value < 0: result.append(0) else: result.append(1) print(result) ``` * JR ```python #Trimming values original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = [] for value in original: if value < 0: result.append(0) else: result.append(1) print(result) ``` * AS ```python original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = [] for value in original: if value < 0: result.append(0) else: result.append(1) print(result) ``` **Desired Output:** ``` [0, 1, 1, 1, 0, 1] ``` * GH ```python original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = [] for value in original: if value<0: result.append(0) else: result.append(1) print(result) ``` * IF ```python original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = [] for value in original: if value < 0: result.append(0) else: result.append(1) print(result) ``` **Desired Output:** ``` [0, 1, 1, 1, 0, 1] ``` * TA: ```python original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = [] for value in original: if value < 0: result.append(0) else: result.append(1) print(result) ``` * BN ```python original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = [] for value in original: if value < 0: result.append(0) else: result.append(1) print(result) ``` * SP ```python original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = [] for value in original: if value < 0: result.append(0) else: result.append(1) print(result) ``` * IJ ```python original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = [] for value in original: if value < 0: result.append(0) else: result.append(1) print(result) [0, 1, 1, 1, 0, 1] ``` * CK ```python original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = [] for value in original: if value < 0: result.append(0) else: result.append(1) print(result) ``` * PE ```python original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = [] for value in original: if value < 0: result.append(0) else: result.append(1) print(result) ``` * JH ```python f ``` * RT Nice summary for Finding in lists in Python: https://stackoverflow.com/a/9542768 ### Episode 7 - #### Exercise 7.3 - Beatbox Write a function called `beatbox(…)` that takes a number and prints output as follows: * If this number can be divided by _3_ without remainder, print the text `"Dum"`. * If the number can be divided by _5_ without remainder, print the text `"Tsss"` * If the number can be divided by _3_ and by _5_ without remainder print the text `"DumTsss"` * Otherwise print nothing You can test your function with the following code: ```python for number in range(0, 20): beatbox(number) ``` **Expected Output:** ``` DumTsss Dum Tsss Dum Dum Tsss Dum DumTsss Dum ``` > **Hint:** You can check if a number can be divided by another number by using the `%`-operator (called _modulo_), which gives you the remainder of a division. > If `a % b == 0` then `a` could be divided by `b` without remainder. ##### Post your solutions * IF ```python def beatbox(number): if (((number%3) == 0) and ((number%5) == 0)): print("DumTsss") elif ((number%3) == 0): print("Dum") pass elif ((number%5) == 0): print("Tsss") for number in range(0, 20): beatbox(number) ``` * JH ```python def beatbox(number): if int(number)%3 == 0 and int(number)%5 == 0: print("DumTsss") elif int(number)%3 == 0: print("Dum") elif int(number)%5 == 0: print("Tsss") ``` * MD: ```python def beatbox(number): if number % 3 == 0 and not number % 5 == 0: print("Dum") if number % 5 ==0 and not number % 3 == 0: print("Tsss") if number % 3 == 0 and number % 5 ==0: print("Dumtss") else: return for number in range(0,20): beatbox(number) #Output is: Dumtsss Dum Tsss Dum Dum Tsss Dum Dumtsss Dum ``` * KW ```python def beatbox(number): if number%3 == 0 and number%5 != 0: print("Dum") elif number%5 == 0 and number%3 !=0: print("Tsss") elif number%5 == 0 and number%3 ==0: print("DumTsss") else: print("") ``` * GH ```python def beatbox(number): div3=number/3-int(number/3) div5=number/5-int(number/5) if (div3==0 and div5==0): print("DumTsss") elif div3==0: print("Dum") elif div5==0: print("Tss") ``` * AS ```python def beatbox(number): result = '' if number % 3 == 0: result = 'Dum' if number % 5 == 0: result = result + 'Tssss' if len(result) > 0: print(result) ``` * IJ ```python def beatbox(number): if number % 3 == 0 and not number % 5 == 0: print("Dum") elif number % 5 == 0 and not number % 3 ==0: print("Tsss") elif number % 3 == 0 and number % 5 == 0: print("Dumtsss") ``` * CK ```python def beatbox(number): if number == 0: print("DumTsss") elif ((number%3) == 0) and ((number%5) == 0): print("DumTsss") elif (number%3) == 0: print("Dum") elif (number%5) == 0: print("Tsss") for number in range(0,20): beatbox(number) ``` * JR ```python def beatbox(numberToCheck): dividableByThree = numberToCheck % 3 == 0 dividableByFive = numberToCheck % 5 == 0 if dividableByThree: if dividableByThree and dividableByFive: print('DumTsss') else: print('Dum') elif dividableByFive: print('Tsss') ``` * LP ````python def beatbox(num): if(num % 3 == 0 and num % 5 == 0): print("DumTsss") elif(num % 3 == 0): print("Dum") elif(num % 5 == 0): print("Tsss") ```` * SP: ```python a=3 b=5 def beatbox(number): if number % a == 0: print('Dum') elif number % b == 0: print('Tsss') elif number % a & b == 0: print('DumTsss') else: return ``` * TA: ```python def beatbox(number): my_list = [] my_list.append(number) for index in my_list: if index % 3 == 0 and index % 5 == 0: print("DumTsss") elif index % 3 == 0: print("Dum") elif index % 5 == 0: print("Tsss") ``` * MV : ``` ...: In [30]: def beatbox(n): ...: if (n % 3 == 0): ...: print("Dum") ...: elif (n % 5 == 0): ...: print("Tsss") ...: elif ( (n % 3==0) | (n % 5 == 0) ): ...: print("DumTss") ...: else : ...: print("provide a number") ...: In [31]: beatbox(5) Tsss ``` * SA : ```python def beatbox(arg): if int(arg)%15==0: print("DumTss") elif int(arg)%5==0: print("Tss") elif int(arg)%3==0: print("Dum") ``` NA: ```python def beatbox(number): if (number % 3) == 0 and not (number % 5) == 0: print("Dum") elif number % 5 and not number % 3==0: print("Tess") elif number % 5 and number % 3==0: print("DumTess") ``` * BN ```python= def beatbox(sound_numbers): for number in sound_numbers: if ((number%3) == 0) and ((number%5) == 0): print("DumTsss") elif ((number % 3) == 0): print("Dum") elif ((number % 5) == 0): print("Tsss") ``` * PE ```python def beatbox(N): for n in range(0, N-1): if (n % 3) == 0 and (n % 5) == 0: print("DumTsss") elif (n % 3) == 0: print("Dum") elif (n % 5) == 0: print("Tsss") # end PE_beatbox definition ``` ![Array Layout Visualization from Carpentries](https://swcarpentry.github.io/python-novice-inflammation/fig/python-zero-index.png) ![Axis visualization from Carpentries](https://swcarpentry.github.io/python-novice-inflammation/fig/python-operations-across-axes.png) ## Day 2 morning feedback Below, list something that you liked or learned this morning: - LP: interesting overview of the language structure, also nice to solve problems together, they happen all the time :+1: :+1: - MD: learned how to use pyplot and how images are created (really useful tool) - IF: Excersises were interesting. We learned and got overview about many things in short time. - JR: more excercises than yesterday - IJ pretty interesting to see how easily one can create images with pyplot easily - SP: everything good. a little harder than yesterday but good - TA: interesting execise (more like that will be much appreciated), solution from expert also very helpful. - RT: feel good on programming. - CK: it's all good, pace will be upgraded after library problems solved, excited for matplot related data interaction especially 3D graph plots - AS: Nice exercises :) NA: many thanks for your patience for solving some technical issues which were important, that would be great to have some homework which we can look at after today and get some feedback after doing them - KW: Thank you very much for helping with the shell problems - this often happens with using various OS ;(But its very fine to have support for all of them :) - BN the simplicity of drawing graphs has blown me out of the water! Below, list something that you didn't like, that you want us to improve or that you didn't understand: - AS: More details and exercises regarding numpy and matplotlb - LP: It is a bit tricky to solve specific problems of each operating system online, I have no clue how can that be improved in a online course, but it would add fluidity. Maybe you could start with mindblowing examples, and then explain how it works step by step. - MD: maybe generate a list of commands/useful things we could use in the future; also maybe we can improve on trougleshooting in the future before the workshop starts? - SP: I was a little confused when using git. I managed later in jupyter notebook but I am not sure if it is the correct way. I downloaded the programs from the carpentries website but it seems that we all have different versions. This of course causes difficulties. - IF: Differences between operating systems are drawing our attention from more important things. But I guess there is nothing more that could have been done about it. -out it. - TA: a lot of unnecessarry interuption, solving the excercise also took lots of time NA: we are not experts so I think sometimes solving those issues are unavoidable even thought it might take time, - JR: using Jupyter notebook would help to solve most of the technical issues, as the installation and python version is same for Win, Mac, Linux * PE - you might want to have a "standard" tool for participants to show their status i.e. when then are done with a current task or if they want/need more time by themselves or if they need help - RT: I am here to learn some automation of experiments. I know workshop is not for particular tasks. But is it possible to help us to sort out specific taks in near future? or can you give some examples where or how or what ways python is used in large scale research? - BN: advanced python lessons would definitely be helpful. Could you also recommend a simple and instructive development tool/interface? ## Let's continue at 13:15 ## Version Control Lesson HackMD -> https://hackmd.io/RY8JT6bQSy6l4LTwXELywA?edit