136 views
 owned this note
# TEMPLATE - From zero to hero with Python ## General information **Date**: 21-11-2023 **Time**: 18:00 - 21:00 **Location**: Online ([Zoom](change_the_link)) **Code of Conduct**: [The Carpentries code of conduct](https://docs.carpentries.org/topic_folders/policies/code-of-conduct.html) ### Before Introduction: who is who? Introduction to BioNT ### Schedule for the workshop | Day | Tutorial | Instructor | | -------- | -------- | -------- | | 1 - Tuesday | [Programming environment setup](http://swcarpentry.github.io/python-novice-gapminder/index.html) | Wolfgang | | 2 - Wednesday | [Tabular data manipulation and visualisation](http://swcarpentry.github.io/python-novice-gapminder/07-reading-tabular.html) | Renato | | 3 - Thursday | [Advanced programming](https://swcarpentry.github.io/python-novice-gapminder/11-lists.html) | Rabea | | 4 - Friday | [Build a website using Jupyter Book](https://workshop-building-websites-with-gitlab-biont-eu-2e49af9c9c94c62.gitlab.io/)| Lisanna | [TOC] ### How will the workshop be run? Why a specific setup for this workshop? - We want to welcome participants for different areas and (e.g. SMEs, job seeker, academia) - Importance of privacy - We would like to create a interactive atmosphere How will we do? - Zoom with panel view - Panelist: instructors & helpers - Only trainers will be visible - No personal data will be displayed - This [HedgeDoc](xxx) document in Markdown for interactions - Markdown: lightweight markup language - [Documentation](https://biont.biobyte.de/features#Edit) ### How to participate? **Ask your questions, raise issues, interact with us in this Document** In addition, to help you navigate this document, we followed the structure of the tutorial and included: - Each Hands-on section (✏️ - where you will have to work) of the tutorial, including a part to ask questions or post issues you might face :::warning ✏️ Hands-on: Topic ##### ❓ Are you finished with this section? Add a '+' below - Yes: - Waiting for the job to be done: - Need help: ##### Your questions Q: A: ##### Do you need help? Please describe your issue - ::: An helper will help you - Question sections (❓ - where we ask you something ) for answering :::success ❓ We have some question: - ::: Let's try now! ::: warning ##### ✏️ Hands-on: Access this HedgeDoc main document ##### ❓ Are you on this HedgeDoc? (Add a + when done) - Yes: - No (please sent a e-mail to one of the helpers) ##### ❓ Do you need help? Please describe your issue - - - - ::: :::success ##### ❓ Have you ever used Markdown? (Add a +) - Yes - No - What is Markdown? ::: :::success ##### ❓ We plan to adjust the break times according to the workshop's progression. Does this arrangement work well for everyone? - Yes - No ::: ### Programming languages and why learning Python? :::success ##### ❓ Have you ever used a programming language? (Add a +) - Yes - No ::: :::success ##### ❓ What programming languages do you know? - - - - - - - ::: :::success ##### ❓ Do you have Anaconda installed? (Add a +) - Yes - No ::: ### Version control system :::success ##### ❓ Do you have a GitLab account? (Add a +) - Yes - No You will need it for day 4 ::: :::success ##### ❓ Do have Jupyter Books installed? (Add a +) - Yes - No You will need it for day 4 ::: ## Day 1 - Tuesday ### Schedule | Starting time| Duration | Content | | -------- | -------- | ---------------- | | 18:20 | 10 min | [Summary and Setup](http://swcarpentry.github.io/python-novice-gapminder/index.html)| | 18:30 | 20 min |[Running and Quitting](http://swcarpentry.github.io/python-novice-gapminder/01-run-quit.html) | | 18:50 | 25 min |[Variables and Assignment](http://swcarpentry.github.io/python-novice-gapminder/02-variables.html)| | 19:15 | 25 min | [Data Types and Type Conversion](http://swcarpentry.github.io/python-novice-gapminder/03-types-conversion.html)| | 19:40 | 15 min | Break | | 19:55 | 30 min | [Built-in Functions and Help](http://swcarpentry.github.io/python-novice-gapminder/04-built-in.html) | | 20:25 | 25 min | [Libraries](http://swcarpentry.github.io/python-novice-gapminder/06-libraries.html) | | 20:50 | 10 min | Summary + Feedback| :::success ##### ❓ Before we start: Is the screen clearly visible (add '+') Yes - Please zoom in - ::: ### Start your programming environment We will learn Python using Jupyter notebooks as our programming environment. ::: warning ##### ✏️ Hands-on: Start up your JupyterLab instance and bring up the JupyterLab main page in your browser JupyterLab and the possible ways to launch it are explained in the first two sections of the tutorial's ["Running and quitting" chapter](http://swcarpentry.github.io/python-novice-gapminder/01-run-quit.html). JupyterLab should be installed on your machine as part of Anaconda. ::: :::success ##### ❓Can you see the JupyterLab main page in your browser? Add a '+' below - Yes: - Need time: - Need help: ##### ❓ Do you need help? Please describe your issue - - - - ::: ::: warning ##### ✏️ Hands-on: Explore the elements of the main page, then launch a Jupyter Notebook see the [JupyterLab interface](http://swcarpentry.github.io/python-novice-gapminder/01-run-quit.html#the-jupyterlab-interface) and the [Creating a Jupyter notebook](http://swcarpentry.github.io/python-novice-gapminder/01-run-quit.html#creating-a-jupyter-notebook) sections of the tutorial chapter ::: ::: success ##### ❓ Are you inside an empty Jupyter notebook? Add a '+' below - Yes: - Need time: - Need help: ##### ❓ Do you need help? Please describe your issue - - - - ::: ### First steps with Python #### Variables and Assignment This part corresponds to the contents of the [Variables and Assignment](http://swcarpentry.github.io/python-novice-gapminder/02-variables.html) chapter of the tutorial. ::: warning ##### ✏️ Hands-on: Work through the chapter until you're done with the section [Variables can be used in calculations](http://swcarpentry.github.io/python-novice-gapminder/02-variables.html#variables-can-be-used-in-calculations.). ##### ❓ Do you need help? Please describe your issue - - - - ::: ::: success ##### ✏️ Explore arithmetic operations in Python 1. Define two variables `a` and `b` and assign them the integer values `5` and `3`. Instead of `a` and `b` you can, of course, use any other valid variable name :-) 2. Print the result of adding the two values. 3. Print the result of subtracting the two values. 4. The operators for multiplication and division are `*` and `/`, respectively. Use them with your variables. 5. Compare the result of `(a + b) * b` and `a + b * b`. 6. Use also the additional operators `**`, `//` and `%`. What are they doing? **💡 Hint**: the last two operators might be hard to understand. Try running: `help(divmod)` to display the help for another built-in function `divmod`, which combines the functionality of both `//` and `%`. ##### ❓ Do you think you have the answers to at least some of the above questions? Add a '+' below - Yes: - Need time: - Need help: ##### ❓ Do you need help? Please describe your issue - - - ##### ❓ What are your answers? - - - ::: ::: warning ##### ✏️ Hands-on: Continue with the remainder of this chapter ::: ::: success ##### ❓ Let's test your understanding of the contents of this chapter! Work through all the yellow practice boxes and, if something is unclear, if you cannot solve a particular task, or don't understand the solution, please report below! ---- **💡 A better way to swap**: I do not particularly like the very first of the practice boxes (the one on swapping values). The truly common idiom for this in Python is simply: `y, x = x, y`, which lets Python handle the intermediate storage part. Try this out yourself! ---- ##### ❓ Are you done with the practice boxes? Add a '+' below - Yes: - Need time: - Need help: ##### ❓ Do you need with any of them? Please state which practice box you're having a problem with and what that problem is: - - - - ::: #### Data Types and Type Conversion This part corresponds to the same-named [chapter 3](http://swcarpentry.github.io/python-novice-gapminder/03-types-conversion.html). ::: warning ##### ✏️ Hands-on: Work through the chapter up to [Can mix integers and floats freely in operations](http://swcarpentry.github.io/python-novice-gapminder/03-types-conversion.html#can-mix-integers-and-floats-freely-in-operations.). ::: ::: success ##### ✏️ Don't use floats just because you can! While it is technically true that you can mix integers and float freely, don't do this when it is not required! The `int` type in Python is a very sophisticated type highly optimized for the task of storing integer numbers of *any* size. Try it yourself: ``` a = 3*10**16 b = a + 1 print(a, b) ``` Nothing special to see here, you think? Well, try with floats: ``` a = float(3*10**16) b = a + 1 print(a, b) ``` ❓ Do you have an explanation for this behavior? - Yes: - I think I got it: - I am lost: --- **Conclusion**: There are good reasons for using floats in programs, but don't give up the safety of Python integer arithmetics if you don't have to. ::: ::: warning ##### ✏️ Hands-on: Continue with the remainder of this [chapter](http://swcarpentry.github.io/python-novice-gapminder/03-types-conversion.html) ##### ❓ Do you have questions? ::: ::: success ##### ❓ Let's test your understanding of the contents of this chapter! Work through all the yellow practice boxes and, if something is unclear, if you cannot solve a particular task, or don't understand the solution, please report below! ##### ❓ Are you done with the practice boxes? Add a '+' below - Yes: - Need time: - Need help: ##### ❓ Do you need help with any of the content? Please state which part you're having a problem with and what that problem is: - - - - ::: #### Built-in Functions and Help This part corresponds to the same-named [chapter 4](http://swcarpentry.github.io/python-novice-gapminder/04-built-in.html). ::: warning ##### ✏️ Hands-on: Work through the chapter up to and including the section [Functions attached to objects are called methods](http://swcarpentry.github.io/python-novice-gapminder/04-built-in.html#functions-attached-to-objects-are-called-methods). ##### ❓ Do you have questions? - - - ::: ::: success ##### ✏️ Bonus info and task One of the advantages of methods is that they provide a natural way of bundling an object with actions you may want to perform with it. This makes it easy to discover functionality. If, for example, you're wondering if there is a built-in way to strip leading/trailing whitespace from a string, you can just look through the methods of the string. The built-in function `dir()` lists all methods and attributes of an object. It works with a concrete object, but also with types, so if `user_input = "Yes "` both of these can be used to look for a suitable method: 1. `dir(user_input)` 2. `dir(str)` ❓ Can you find and use a method that will turn the *user_inputs* `" Yes"`, `"Yes "`, and many more variations into just `"Yes"`? - Yes, found it: - A bit more time please: - Cannot find it: ::: ::: warning ##### ✏️ Hands-on: Continue with the remainder of this chapter ::: ::: success ##### ❓ Let's test your understanding of the contents of this chapter! Work through all the yellow practice boxes and, if something is unclear, if you cannot solve a particular task, or don't understand the solution, please report below! ##### ❓ Are you done with the practice boxes? Add a '+' below - Yes: - Need time: - Need help: ##### ❓ Do you need help with any of the content? Please state which part you're having a problem with and what that problem is: - - - - ::: #### Libraries This part corresponds to the same-named [chapter 6](http://swcarpentry.github.io/python-novice-gapminder/06-libraries.html). ::: warning ##### ✏️ Hands-on: Work through all of the chapter ##### ❓ Do you have questions? - - - - - ::: ::: success ##### ❓ Let's test your understanding of the contents of this chapter! Work through all the yellow practice boxes and, if something is unclear, if you cannot solve a particular task, or don't understand the solution, please report below! ##### ❓ Are you done with the practice boxes? Add a '+' below - Yes: - Need time: - Need help: ##### ❓ Do you need help with any of the content? Please state which part you're having a problem with and what that problem is: - - - - :::info **💡 Some notes from my side**: - The first section distinguishes between the *Standard Library* and libraries (*a.k.a.* packages) available from *PyPI*. The difference between the two is less obvious with an Anaconda install of Python because Anaconda already bundles many PyPI packages. In general though, you can rely only on the Standard Library being available on every system that has Python installed. - Large parts of the Standard Library are written in Python themselves and since Python is an open-source project developed in the open, you can study that [code](https://github.com/python/cpython/tree/main/Lib) and learn from it. - If in doubt about the many possible ways of importing modules in Python, use the following rule of thumb: *Unless you have a good reason not to do so, import just the module itself under its original name!* In other words, prefer the simple `import math` over all other forms unless you know *why* you are prefering one of the alternatives. ::: #### Outlook We have covered only the most essential language features today. Tomorrow, you'll shift gears a bit and will learn about a few of the most-widely used PyPI packages for data science before returning to Python language features again. ::: info **💡 A suggestion**: By the end of day 3 you will already be able to solve many daily data analysis tasks with Python. Then because it's almost that time of the year you might be able to practice what you learnt with https://adventofcode.com/ 🎄. ::: ### Feedback :::success ##### ❓ One thing that was good about today - - - - ##### ❓ One thing to improve - - - - ##### ❓ Any other comments? - - - - ::: ## Day 2 - Wednesday ### Schedule | Starting time | Duration | Content | | ------------- | -------- | ---------------- | | 18:00 | 10 min | Welcome + Summary | | 18:10 | 25 min |[Reading Tabular Data into DataFrames](http://swcarpentry.github.io/python-novice-gapminder/07-reading-tabular.html) | | 18:35 | 35 min | [Pandas DataFrames](http://swcarpentry.github.io/python-novice-gapminder/08-data-frames.html)| | 19:10 | 35 min | [Plotting](http://swcarpentry.github.io/python-novice-gapminder/09-plotting.html) | | 19:45 | 25 min | [Lists](http://swcarpentry.github.io/python-novice-gapminder/11-lists.html) | | 20:10 | 15 min | Break | | 20:25 | 30 min | [For Loops](http://swcarpentry.github.io/python-novice-gapminder/12-for-loops.html) | | 20:55 | 5 min | Summary + Feedback | :::success ##### ❓ Before we start: Is the screen clearly visible (add '+') Yes - Please zoom in - ::: :::warning ##### ❓ Do you have any questions about yesterday's content? You can see all questions and answers from yesterday [here](https://biont.biobyte.de/s/TfzaOoBp0#) Q: A: Q: A: Q: A: Q: A: ::: ### Reading Tabular Data into DataFrames :::success ##### ❓ Jumping in: Were you able to import pandas and load the data? (add `+`) Yes - No (please copy-paste any error message you received) - ::: :::success ##### ❓ Inspecting our data: How many years do we have data for? Add a number after a `-` or a `+` after the number if it exists - - - - - - - - ::: ::: warning ##### ✏️ Hands-on: Reading Files in Other Directories The data for your current project is stored in a file called `microbes.csv`, which is located in a folder called `field_data`. You are doing analysis in a notebook called `analysis.ipynb` in a sibling folder called `thesis`: ``` your_home_directory +-- field_data/ | +-- microbes.csv +-- thesis/ +-- analysis.ipynb ``` What value(s) should you pass to `read_csv` to read `microbes.csv` in `analysis.ipynb`? ::: ::: warning ##### ✏️ Hands-on: Writing Data As well as the `read_csv` function for reading data from a file, Pandas provides a `to_csv` function to write dataframes to files. Applying what you’ve learned about reading from files, write one of your dataframes to a file called `processed.csv`. You can use `help` to get information on how to use `to_csv`. --- I'm done (add a `+`) - ::: ### Pandas DataFrames :::success ##### ❓ Where are we: is the difference between `iloc` and `loc` clear? (add `+`) Yes - No (can you elaborate?) - ::: :::success ##### ❓ Are we Zorro or Superman: Is a mask always composed of booleans (TRUE/FALSE)? (add `+`) Yes - No - ::: ::: warning ##### ✏️ Hands-on: Extent of Slicing Given the code: ```python print(data_europe.iloc[0:2, 0:2]) print(data_europe.loc['Albania':'Belgium', 'gdpPercap_1952':'gdpPercap_1962']) ``` Explain or add a `+` to an existing explanation in the following questions. 1. Do the two statements below produce the same output? - - - - - 2. Based on this, what rule governs what is included (or not) in numerical slices and named slices in Pandas? - - - - - ::: :::warning ##### ✏️ Hands-on: Selecting Indices Explain in simple terms what `idxmin` and `idxmax` do in the short program below. When would you use these methods? ```python data = pd.read_csv('data/gapminder_gdp_europe.csv', index_col='country') print(data.idxmin()) print(data.idxmax()) ``` Explain below or add a `+` to an existing explanation - - - - - - - ::: :::warning ##### ✏️ Hands-on: Practice with Selection Assume Pandas has been imported and the Gapminder GDP data for Europe has been loaded. Write an expression to select each of the following: Explain or add a `+` to an existing explanation in the following questions. 1. GDP per capita for all countries in 1982. - - - - - - 2. GDP per capita for Denmark for all years. - - - - - - 3. GDP per capita for all countries for years after 1985. - - - - - 4. GDP per capita for each country in 2007 as a multiple of GDP per capita for that country in 1952. - - - - - ::: ### Plotting :::success ##### ❓ Jumping in: Were you able to import matplotlib? (add `+`) Yes - No (please copy-paste any error message you received) - ::: :::success ##### ❓ Clarification: Why did we have to add the `.str.` attribute to our `.replace()` function? (add a `-` with your explanation or a `+` to existing explanations) - - - - - - - - - ::: :::warning ##### ✏️ Comparing axis: Correlations Using what we saw so far, create a scatter plot showing the relationship between the `min`imum and `max`imum GDP per capita among the countries in Asia for each year in the data set. What relationship do you see (if any)? Add your explanation or a `+` to an existing one - - - - - - - ::: :::warning ##### ✏️ Help!!!: More Correlations This short program creates a plot showing the correlation between GDP and life expectancy for 2007, normalizing marker size by population: ```python data_all = pd.read_csv('data/gapminder_all.csv', index_col='country') data_all.plot(kind='scatter', x='gdpPercap_2007', y='lifeExp_2007', s=data_all['pop_2007']/1e6) ``` Using `help()`, `jupyter` completion, online help or other resources, explain what each argument to `plot` does. Explain or add a `+` to an existing explanation in the following sections: ###### kind= - - - - - ###### x= and y= - - - - - - - ###### s= - - - - - - - ::: :::success ##### ❓ You are safe! Were you able to save your plot to a file and open it? (add `+`) Yes - No (please elaborate/justify) - ::: ### Lists :::success ##### ❓ Understanding: Does a list need to have a fixed length when created? (add `+`) Yes - No - ::: :::success ##### ❓ Understanding: Does a list have a maximum length? (add `+`) Yes - No - ::: :::success ##### ❓ Understanding: What can we not store inside a list? (add a `-` and elaborate) - - - - - - - - - - - - - - - - ::: :::success ##### ❓ Errors are my friends! What information can you learn from an error message? (add a `-` and elaborate) - - - - - - - - - - - - - - - - ::: :::warning ##### ✏️ From Strings to Lists and Back Given this: ```python print('string to list:', list('tin')) print('list to string:', ''.join(['g', 'o', 'l', 'd'])) ``` ``` string to list: ['t', 'i', 'n'] list to string: gold ``` Answer or add a `+` to answers in the following questions. 1. What does `list('some string')` do? - - - - - - - - 2. What does `'-'.join(['x', 'y', 'z'])` generate? - - - - - - - - ::: :::warning ##### ✏️ Working With the End What does the following program print? ```python element = 'helium' print(element[-1]) ``` Explain or add a `+` to an existing explanation in the following questions. 1. How does Python interpret a negative index? - - - - - - - - 2. If a list or string has N elements, what is the most negative index that can safely be used with it, and what location does that index represent? - - - - - - - - - - 3. If values is a list, what does `del values[-1]` do? - - - - - - - - - - 4. How can you display all elements but the last one without changing values? (Hint: you will need to combine slicing and negative indexing.) - - - - - - - - - - ::: :::warning ##### ✏️ Copying (or not) What do these two programs print? In simple terms, explain the difference between new = old and new = old[:]. ```python # Program A old = list('gold') new = old # simple assignment new[0] = 'D' print('new is', new, 'and old is', old) ``` ```python # Program B old = list('gold') new = old[:] # assigning a slice - same as old[0:4] or old[:4] or old[0:] new[0] = 'D' print('new is', new, 'and old is', old) ``` Explain or add a `+` to an existing explanation - - - - - - - - - - - - ::: ### For Loops :::success ##### ❓ How often do you copy-paste when you need to execute an instruction more than once? (add a `+`) Never - Rarely - Frequenty - An embarrassing number of times - ::: :::success ##### ❓ Looping our heads: Can we have a `for` loop inside a `for` loop? (add a `+`) No - Yes - ::: :::warning ##### ✏️ Reversing a string Fill in the blanks in the program below so that it prints “racecar nit” (the reverse of the original character string “tin racecar”). ```python original = "tin racecar" result = ____ for char in original: result = ____ print(result) ``` --- I'm done (add a `+`) - ::: :::warning ##### ✏️ Practice Accumulating Fill in the blanks in each of the programs below to produce the indicated result. ```python # Total length of the strings in the list: ["red", "green", "blue"] => 12 total = 0 for word in ["red", "green", "blue"]: ____ = ____ + len(word) print(total) ``` --- I'm done (add a `+`) - ::: :::warning ##### ✏️ Cumulative Sum Reorder and properly indent the lines of code below so that they print a list with the cumulative sum of data. The result should be `[1, 3, 5, 10]`. ```python cumulative.append(total) for number in data: cumulative = [] total = total + number total = 0 print(cumulative) data = [1,2,2,5] ``` --- I'm done (add a `+`) - ::: :tada: That's all for today :tada: ### Feedback :::success ##### ❓ One thing that was good about today - - - - ##### ❓ One thing to improve - - - - ##### ❓ Any other comments? - - - - ::: ## Day 3 - Thursday ### Schedule | Day | Tutorial | Instructor | | -------- | -------- | -------- | | 3 - Thursday | [Advanced programming](https://swcarpentry.github.io/python-novice-gapminder/11-lists.html) | Rabea | | Starting time | Duration | Content | | ------------- | -------- | --------------------- | | 18:00 | 10 min | Welcome + Summary | | 18:10 | 30 min | [Lists](https://swcarpentry.github.io/python-novice-gapminder/11-lists.html) | | 18:40 | 20 min |[For Loops](https://swcarpentry.github.io/python-novice-gapminder/12-for-loops.html) and [Looping Over Data Set](http://swcarpentry.github.io/python-novice-gapminder/14-looping-data-sets.html) | | 19:00 | 10 min | Break | | 19:10 | 30 min | [Conditionals](http://swcarpentry.github.io/python-novice-gapminder/13-conditionals.html) | | 19:40 | 20 min | [Writing Functions](http://swcarpentry.github.io/python-novice-gapminder/16-writing-functions.html) | | 20:00 | 10 min | Break | | 20:10 | 35 min | [Variable Scope](http://swcarpentry.github.io/python-novice-gapminder/17-scope.html) | | 20:45 | 15 min | Summary + Feedback | :::warning ##### ❓ Do you have any questions about yesterday's content? Q: A: Q: A: Q: A: Q: A: ::: :::success ##### ❓ Before we start: Is the screen clearly visible (add '+') Yes - Please zoom in - ::: :::warning ##### ✏️ Hands-on: Start up your or Jupyterlab Start your Jupyterlab instance and bring up the main page in your browser. Create a new Jupyter Notebook and name it `Day3` ##### ❓ Are you inside an empty Jupyter notebook? Add a '+' below - Yes: - Need time: - Need help: ##### ❓ Do you need help? Please describe your issue - - - - ::: ### 1. Lists :::success ##### ✏️ Lists Type along with Rabea to learn about lists ##### ❓ Do you need help? Please describe your issue or question - - - - ::: :::warning ##### ✏️ Exercise 1 - Working With the End What does the following program print? ```python element = 'helium' print(element[-1]) ``` - - - - 1. How does Python interpret a negative index? - - - - 2. If `values` is a list, what does `del values[-1]` do? - - - - 3. How can you display all elements but the last one without changing `values`? (Hint: you will need to combine slicing and negative indexing.) - - - - ::: :::warning ##### ✏️ Exercise 2 - Slice Bounds What does the following program print? ```python element = 'lithium' print(element[0:20]) print(element[-1:3]) ``` - - - - ::: :::info ##### ✏️ KEYPOINTS Lists - A list stores many values in a single structure. - use an item’s index to fetch it from a list. - Lists’ values can be replaced by assigning to them. - Appending items to a list lengthens it. - Use del to remove items from a list entirely. - The empty list contains no values. - Lists may contain values of different types. - Character strings can be indexed like lists. - Indexing beyond the end of the collection is an error. ::: ### 2. For Loops :::success ##### ✏️ For Loops Type along with Rabea to learn about for loops ##### ❓ Do you need help? Please describe your issue or question - - - - ::: :::warning ##### ✏️ Exercise 3 - Slice Bounds Is an indentation error a syntax error or a runtime error? Write your answer below and explain why. - - - - ::: :::warning ##### ✏️ Exercise 4 PRACTICE ACCUMULATING ``` # Concatenate all words: ["red", "green", "blue"] => "redgreenblue" words = ["red", "green", "blue"] result = ____ for ____ in ____: ____ print(result) ``` ::: :::info ##### KEYPOINTS Conditionals - A for loop executes commands once for each value in a collection. - A for loop is made up of a collection, a loop variable, and a body. - The first line of the for loop must end with a colon, and the body must be indented. - Indentation is always meaningful in Python. - Loop variables can be called anything (but it is strongly advised to have a meaningful name to the looping variable). - The body of a loop can contain many statements. - Use range to iterate over a sequence of numbers. ::: ### 3. Conditionals ::: success ##### ✏️ If Statements / Conditionals Type along with the instructor to learn about conditionals ##### ❓ Do you need help? Please describe your issue or question - - - - ::: :::warning ##### ❓ Exercise 5 TRACING EXECUTION: What does this program print? ``` pressure = 71.9 if pressure > 50.0: pressure = 25.0 elif pressure <= 50.0: pressure = 0.0 print(pressure) ``` - - - - - - - - ::: :::info ##### ✏️ KEYPOINTS Conditionals - Use if statements to control whether or not a block of code is executed. - Conditionals are often used inside loops. - Use else to execute a block of code when an if condition is not true. - Use elif to specify additional tests. - Conditions are tested once, in order. ::: ### 4. Looping Over Data Sets ::: success ##### ✏️ Looping Over Data Sets Type along with the instructor to learn about looping over Data Sets. Make sure to import pandas in your Notebook with `import pandas as pd` ##### ❓ Do you need help? Please describe your issue or question - - - - ::: :::warning ##### ❓ Exercise 6 DETERMINING MATCHES: Which of these files is not matched by the expression glob.glob('data/\*as\*.csv')? ``` data/gapminder_gdp_africa.csv data/gapminder_gdp_americas.csv data/gapminder_gdp_asia.csv ``` - - - - - - - - ::: :::info ##### ✏️ KEYPOINTS Looping over Data Set - Use a for loop to process files given a list of their names. - Use glob.glob to find sets of files whose names match a pattern. ::: ### 5. Writing Function ::: success ##### ✏️ Writing Functions Type along with the instructor to learn about how to write functions. ##### ❓ Do you need help? Please describe your issue or question - - - - ::: :::warning ##### ❓ Exercise 7 ENCAPSULATION Fill in the blanks to create a function that takes a single filename as an argument, loads the data in the file named by the argument, and returns the minimum value in that data. ``` import pandas as pd def min_in_data(____): data = ____ return ____ ``` ``` ``` ::: :::warning ##### ❓ Exercise 8 DEFINITION AND USE What does the following program print? ``` def report(pressure): print('pressure is', pressure) print('calling', report, 22.5) ``` - - - - - ::: :::info ##### ✏️ KEYPOINTS Writing Functions - Break programs down into functions to make them easier to understand. - Define a function using def with a name, parameters, and a block of code. - Defining a function does not run it. - Arguments in a function call are matched to its defined parameters. - Functions may return a result to their caller using return. ::: ### 6. Variable Scope ::: success ##### ✏️ Variable Scope Type along with the instructor to learn about variable scope. ##### ❓ Do you need help? Please describe your issue or question - - - - ::: :::info ##### ✏️ KEYPOINTS Variable Scope - The scope of a variable is the part of a program that can ‘see’ that variable. ::: ### 7. Programming Style ::: success ##### ✏️ Programming Style Type along with the instructor to learn about programming style. ##### ❓ Do you need help? Please describe your issue or question - - - - ::: :::info ##### ✏️ KEYPOINTS Programming Style - Follow standard Python style in your code like [PEP](https://peps.python.org/pep-0008/) ::: ### Feedback :::success ##### ❓ One thing that was good about today - - - - ##### ❓ One thing to improve - - - - ##### ❓ Any other comments? - - - - ::: ## Day 4 - Friday ### Schedule | Starting time | Duration | Content | | ------------- | -------- | ----------------- | | 18:00 | 10 min | Welcome + Summary | | 18:10 | 20 min | [Introduction](https://workshop-building-websites-with-gitlab-biont-eu-2e49af9c9c94c62.gitlab.io/01-introduction/index.html) | | 18:30 | 20 min | [Authoring With Markdown](https://workshop-building-websites-with-gitlab-biont-eu-2e49af9c9c94c62.gitlab.io/02-markdown/index.html)| | 18:50 | 40 min | [Hosting Pages on GitLab](https://workshop-building-websites-with-gitlab-biont-eu-2e49af9c9c94c62.gitlab.io/03-gitlab-pages/index.html) | | 19:30 | 10 min | Break | | 19:40 | 30 min | [Work with Jupyter books locally](https://workshop-building-websites-with-gitlab-biont-eu-2e49af9c9c94c62.gitlab.io/05-jupyter-books/index.html) | | 20:10 | 20 min | [Host Jupyter Books in GitLab](https://workshop-building-websites-with-gitlab-biont-eu-2e49af9c9c94c62.gitlab.io/06-jupyter-books-in-gitlab/index.html)| | 20:30 | 20 min | Add your notebook as a chapter | | 20:50 | 10 min | [Final remarks](https://docs.google.com/presentation/d/1QC6kOESKD84Bzq4UIxrdgbw7PL5WGq_ncUG9TXmM8jE) | :::success ##### ❓ Before we start: Is the screen clearly visible (add '+') Yes - Please zoom in - The Installation Instructions can be found on the workshops CECAM page. When you open the Document tab you will find the 'Installation Instructions' document. ::: ### Introduction ::: warning ##### ✏️ Hands-on: Set you up - Login to GitLab ##### ❓ Do you need help? Please describe your issue - - - - ::: :::success ##### ❓ Watch the instructor creating an HTML file and opening it in the browser. Don't follow along, it's not needed. Do you have any question about HTML files? - - - - - - ::: :::success ##### ❓ Given the following types of websites, reason if a static site generator is an appropriate solution to implement them. Add an x next to those that you think are a good example of static website case. - (1) A personal website with About and Projects sections - (2) A forum or discussion platform - (3) A community blog or news website - (4) A search engine (such as google.com) - (5) A wiki (such as wikipedia.com) - (6) An online book ::: ::: warning ##### ✏️ Hands-on: Your first project - Create a new project - Call it `Data analysis`, the project slug will automatically be set to `data-analysis` - Check the `Initialize repository with a README` option ##### ❓ Do you need help? Please describe your issue - - - - ::: ### Authoring With Markdown ::: warning ##### ✏️ Hands-on: Write your README - Write your `README.md` file in Markdown (the same language of this collaborative notepad) - Save a version of this project with your first commit ##### ❓ Do you need help? Please describe your issue - - - - ::: :::success ##### ❓ Optional exercise: Use [this cheatsheet](https://docs.gitlab.com/ee/user/markdown.html) to add the following to your README.md. Add an x next to the step when you have done it sucessfully (check it in the preview, no need to commit a version for every step) - Another second level heading - Some text under that second level heading that includes an link and strikethrough text - A numbered list ::: ### Hosting Pages on GitLab ::: warning ##### ✏️ Hands-on: Set up the gitlab pipeline - Create and fill the `.gitlab-ci.yml` file - You can validate it with [CI Lint](https://docs.gitlab.com/ee/ci/lint.html) - Create the file `index.html` - Create a new commit with both the files ##### ❓ Do you need help? Please describe your issue (do not share your gitlab account ID unless strictly necessary) - - - - ::: :::success ##### ❓ Did you notice how a commit is something more than a simple save? We will not discuss Git in details today, but it is a wonderful tool to manage the versions of a coding project. Next thing to learn on your list! Please add comments below. - - - - - - - - ::: ::: warning ##### ✏️ Hands-on: Check your website: it's alive! - Navigate to the pages to see your live website ##### ❓ Do you need help? Please describe your issue - - - - ::: :::success ##### ❓ Does the website you just deployed look as you thought it would, given the html code in the index file? Did you think that something else would be shown? Add here your questions. - - - - - - - - ::: :::success ##### ❓ Optional exercise: The plain-html template GitLab provides a series of templates of web pages deployed through Pages. One of them is called “plain-html”, you can access it at [this link](https://gitlab.com/pages/plain-html). The general structure is quite similar to the one we just used. Go to the `public` folder. There are two files here, one `style.css` file and one `index.html` file. We will now go into the detail of `.css` file functioning in this lesson, but it might be interesting now to have a look at its syntax and content. This type of file is used to style HTML content. This specific file provides styling instructions for three elements: the `body`, the `navbar` and the link text (`a`) within the navbar, that changes color when the mouse is over it (`a:hover`). Don’t worry now about understanding how this works exactly, but when you will visualise this page, remember to go with the mouse over the navbar links to see this in action. Now open the `index.html` file. Its content is reported below. ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="generator" content="GitLab Pages"> <title>Plain HTML site using GitLab Pages</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="navbar"> <a href="https://pages.gitlab.io/plain-html/">Plain HTML Example</a> <a href="https://gitlab.com/pages/plain-html/">Repository</a> <a href="https://gitlab.com/pages/">Other Examples</a> </div> <h1>Hello World!</h1> <p> This is a simple plain-HTML website on GitLab Pages, without any fancy static site generator. </p> </body> </html> ``` Time to sketch! Draw the resulting webpage, provided this HTML file content. Hint: the navbar is a bar on the top of the page, that allows us to navigate the website content. Optional question: how is the `.css` file used? How does the website know which is the right file to read? Add your comments below: - - - - - - - - ::: ### Work with Jupyter books locally ::: warning ##### ✏️ Hands-on: Creating a Jupyterbook locally - Create a jupyter book template with `jupyter-book create ...` - Run it locally with `jupyter-book build ...` ##### ❓ Do you need help? Please describe your issue - - - - ::: :::success ##### ❓ Add one of the notebooks your created in the previous days to the chapters of the Jupyter Book. Do you have any question? - - - - - - - - ::: ### Host Jupyter Books in GitLab ::: warning ##### ✏️ Hands-on: Hosting your Jupyterbook remotely - Create a new project in GitLab - Push your local version of the book to the remote project to make it public ##### ❓ Do you need help? Please describe your issue - - - - ::: :::success ##### ❓ Optional exercise: Your time to experiment with a template This template is deliberately minimal to give you the opportunity to test your documentation reading skills. Check the topic guides at [jupyterbook.org](https://jupyterbook.org/intro.html) and find a way to: 1. Add another page called "About" and linked from the table of contents. 2. Play with the file format of this new page, add the same type of content in MarkDown, reStructuredTex and Notebook formats. 3. Add one figure and a figure caption. 4. Insert a code cell. If you are familiar with any programming language, add a simple plot and visualise the output of such plot into your page. 5. For more advanced code features, check [how to make the code executable](https://jupyterbook.org/interactive/thebe.html) 6. Check the [gallery of Jupyter books](https://executablebooks.org/en/latest/gallery.html) for inspiration! Add your comments below: - - - - - - - - ::: 4. Looping Over Data Sets ### Feedback :::success ##### ❓ One thing that was good about today - - - - ##### ❓ One thing to improve - - - - ##### ❓ Any other comments? - - - - ::: ::: warning ##### ✏️ How to get a certificate If you would like to get a certificate for this workshop please follow these three steps: 1. Complete the post-workshop survey: https://survey.bio-it.embl.de/284869?lang=en 2. Find your notebooks of day 2 and day 3. If you call them different as 'day2' and 'day3' please rename the files to 'day2.ipynb' and 'day3.ipynb'. 3. Send an E-mail with your unique personal identifier and the two notebooks attached to: muellert@informatik.uni-freiburg.de Deadline: Monday 27th of November at 15:00 (CET). Please kindly note that this deadline is final and later submissions will **not** be taken into account! ##### ❓ Questions? - - - - - :::