Skip to content
Snippets Groups Projects
Commit ca00aee8 authored by Remus, Dr. Steffen's avatar Remus, Dr. Steffen
Browse files

excercise

parent 027198a0
Branches
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Python Refresher
%% Cell type:markdown id: tags:
This worksheet is not about the actual content of the Research Software Engineering (RSE) course, but meant to make sure that you are ready to use Python for the assignments and
projects ahead. Therefore, it contains instructions on how to install the Anaconda framework for data science (including Python 3) and some programming exercises to revitalize your
Python coding skills.
%% Cell type:markdown id: tags:
## 1.Python Development Environment
To get started you need a working Python development environment. You can either install Anaconda, or Python directly.
For most tasks of the book, you won't need that many packages, A list will be provided below. This book won't go into detail on how to install python manually, but for those new to Python but experience in other languages you may follow the the instructions on Anaconda.
### Python Install Instructions
1. On Mac and Windows go to [Python.org](https://python.org) and download Python 3 the latest version manully or follow or use your terminal to install python. Most Linux system come with python pre installed.
2. Install a Text editor VS Code, Jetbrains Pycharm, or Neovim, Helix, Emacs, Sublime, there are no requirements, but having an install of VS Code might make it easier, since VS code can run Jupyter notebooks without you having to install the whole Anaconda ecosystem and starting jupyter notebook server. And run the Notebooks in your Browser, instead you can use VScode including all python linting etc.
### Anaconda Install Guide
We will use the Anaconda Data Science platform in the course. Anaconda is a free and open source distribution of Python and some other programming languages used in scientific
applications. It runs on all major operatin systems and provides a number of very useful tools for Python programming, some of which we will use in the course.
Here is what you need to do to get started:
1. Go to https://docs.anaconda.com/anaconda/install/ and follow the installation instructions for your operating system.
2. Go to https://docs.anaconda.com/anaconda/user-guide/getting-started/#open-nav-lin and follow the instructions how to start Spyder.
3. Start coding!
If you have trouble with the installation of Anaconda on your device, please don’t hesitate to ask your questions in the moodle forum.
%% Cell type:markdown id: tags:
## 2. Revitalize your Python
Here are a couple of exercises to revitalize your basic Python skills. Do these in your favorite IDE (e.g. Spyder, VSCode, ...), using plain .py files, **not Jupyter Notebooks**.
%% Cell type:markdown id: tags:
### 2.1 Arithmetics
Write a Python program that welcomes the user, asks for their name (string), weight in kg (integer) and height in m (float), computes the body mass index (BMI) from the information
(weight/height2) and finally displays a message to the user, saying something like:
```Bash
Hello Jim, your BMI is 23.4.
```
You can assume that the user enters correct values.
%% Cell type:code id: tags:
``` python
# Your code for testing
```
%% Cell type:markdown id: tags:
### 2.2 Conditional Branching
Extend the BMI calculation program from the previous exercise so that after informing the user about the calculated BMI, it also prints out if the BMI is within the range that is generally
considered normal (between 18.5 and 25) or higher (above 25) or lower (below 18.5) than that. The output of the modified program should then be something like:
```
Welcome to the BMI calculator.
What is your name? John Doe
What is your weight (in kg)? 78
What is your height (in m)? 1.82
Hello John Doe, your BMI is 23.5
Your BMI is normal.
```
%% Cell type:code id: tags:
``` python
# Your code for testing
```
%% Cell type:markdown id: tags:
### 2.3 While-Loops
Write a simple number-guessing game in Python. It generates a random number between 1 and 100 and asks the user to guess it. If the number guessed is too small or too big, a hint is given and the user can try again, until they guess the correct number. The output of the program should be something like:
```
Can you guess the number? 12
You guessed too small!
Try again: 23
You guessed too big!
Try again: 16
Yes!
```
%% Cell type:code id: tags:
``` python
# Your code for testing
```
%% Cell type:markdown id: tags:
### 2.4 For-Loops, Text Processing
Write a simple text analysis program that finds the (first) longest word in a text. It should work on any text, but you can use the "lorem ipsum" as an example:
>text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do \
>eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad \
>minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex \
>ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate \
>velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat \
>cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id \
>est laborum."
To check if a character `c` is an alphabetic character, you can call the `.isalpha()` function on it: `c.isalpha()`. It will return `True` or `False`.
The output of your program should report the longest word and its length, like this:
```The longest word in the text is "reprehenderit" (13 characters)```
%% Cell type:code id: tags:
``` python
# Your code for testing
```
%% Cell type:markdown id: tags:
### 2.5 Functions
In our lieftimes (unless we happen to get veeery old) a leap year occurs every four years. But actually, the rule is a bit more involved: A year is a leap year if it is a multiple of 4, but not a multiple of 100, unless it is also a multiple of 400. For example, 1984 and 2000 were leap years, but 1900 and 1985 were not.
Write a function `is_leap_year(year)` that tests if the year is a leap year. If so, the function should return `True`, and `False` otherwise. Implement the function using only one Boolean expression. You can use the code below to test your function:
```Python
tests = [1900, 1984, 1985, 2000, 2018]
for test in tests:
if is_leap_year(test):
print(f”{test} is a leap year")
else:
print(f”{test} is not a leap year")
```
%% Cell type:code id: tags:
``` python
# Your code for testing
```
%% Cell type:markdown id: tags:
### 2.6 Dataframes, Pandas
Use the file [`mcdonalds_menu.csv`](https://gitlab.rrz.uni-hamburg.de/BAT2642/rse24-exercises/-/raw/main/data/mcdonalds_menu.csv). Write a program that reads the content of the file into a dataframe, and displays simple descriptive statistics about the
numerical values in the data frame. Then add some code to create a barplot that displays the number of items per category.
The output should look something like:
![table1](https://gitlab.rrz.uni-hamburg.de/BAT2642/rse24-exercises/-/raw/main/img/table1.png)
%% Cell type:code id: tags:
``` python
# Your code for testing
```
%% Cell type:markdown id: tags:
### 2.7 Classes, Inheritance
Define two classes `Person` and `Student` with the following characteristics: The base class `Person` provides a field `name` and a function `printInfo` to print information about
the person (i.e., the name). The class `Student` is derived from `Person`. A Student is a Person that in addition has a university, a study program and a number of `creditpoints`. The class `Student` also has a `printInfo` function, which displays information about the university and program in addition to the student’s name. With the functions `setCreditPoints` and `getCreditPoints` the credit points of the student can be set and retrieved
%% Cell type:code id: tags:
``` python
# Your code for testing
```
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment