Skip to content

Commit

Permalink
Reworked the first part of the Python ipynb to better match the struc…
Browse files Browse the repository at this point in the history
…ture of later exercises (e.g. added tasks)
  • Loading branch information
dominik-probst committed Nov 22, 2023
1 parent 750de7e commit b9ef099
Show file tree
Hide file tree
Showing 3 changed files with 337 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python & pandas\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python is a high-level, general-purpose programming language that is highly flexible. For example, Python can be used effortlessly for simple mathematical calculations.\n",
"\n",
"<div class=\"alert alert-info\" role=\"alert\">\n",
"\n",
"**Task 1**:\n",
" \n",
"In the box below, enter a simple mathematical calculation (such as `1+2`) and press the execute button.\n",
"\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"1 + 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Values can be defined elsewhere, rather than directly in the calculation. The placeholders for these values are called `variables`.\n",
"\n",
"<div class=\"alert alert-info\" role=\"alert\">\n",
"\n",
"**Task 2**:\n",
" \n",
"Add a line to the code below where the variables `x` and `y` are added together, and execute the code.\n",
"\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 1\n",
"y = 2\n",
"\n",
"x + y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is important to note that variables are always internally associated with a so-called data type. Thus, in addition to variables containing numerical values, there are also variables that contain text."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"text = \"Woof\" # Variable containing a text\n",
"number = 1 # Variable containing a number"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is important to know the data type of a variable, as different operations may function differently or may not work at all with different data types.\n",
"\n",
"<div class=\"alert alert-info\" role=\"alert\">\n",
"\n",
"**Task 3**:\n",
" \n",
"Execute the code examples provided below and describe how the `+` operation behaves with the different data types.\n",
"\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"number + number # Addition"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"text + text # Concatenation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"text + number # Error"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"number + text # Error"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To state that `number` is of the data type Number and `text` is of the data type Text, however, is a simplification.\n",
"\n",
"<div class=\"alert alert-info\" role=\"alert\">\n",
"\n",
"**Task 4**:\n",
" \n",
"Execute the code examples provided below and get to know the true data types of `number` and `text`.\n",
"\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Variable text =\", text, \"is of type\", type(text))\n",
"print(\"Variable number =\", number, \"is of type\", type(number))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Thus, `number` is an `int` (short for integer), and `text` is a `str` (short for string). In addition to these data types, Python directly offers several others.\n",
"\n",
"<div class=\"alert alert-info\" role=\"alert\">\n",
"\n",
"**Task 5**:\n",
" \n",
"Use the [Python documentation](https://docs.python.org/3.8/library/stdtypes.html) to find out which numeric data types other than `int` are built into Python. Enter an example of each as code.\n",
"\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"float_number = 1.0 # Variable containing a float number\n",
"print(\"Variable float_number =\", float_number, \"is of type\", type(float_number))\n",
"\n",
"complex_number = 1 + 2j # Variable containing a complex number\n",
"print(\"Variable complex_number =\", complex_number, \"is of type\", type(complex_number))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In Python, numerical data types are also utilized to store truth values.\n",
"\n",
"<div class=\"alert alert-info\" role=\"alert\">\n",
"\n",
"**Task 6**:\n",
" \n",
"Determine which data type is used for the truth values listed below. Refer to the [Python documentation](https://docs.python.org/3.8/library/stdtypes.html) to understand why this data type is also considered a numerical data type.\n",
"\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"truth_value_true = True\n",
"truth_value_false = False\n",
"\n",
"print(\n",
" \"Variable truth_value_true =\",\n",
" truth_value_true,\n",
" \"is of type\",\n",
" type(truth_value_true),\n",
")\n",
"print(\n",
" \"Variable truth_value_false =\",\n",
" truth_value_false,\n",
" \"is of type\",\n",
" type(truth_value_false),\n",
")\n",
"# Boolean is a special case of integer (https://docs.python.org/3.8/library/functions.html?highlight=bool#bool)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In addition to individual values, a variable can also store multiple values.\n",
"\n",
"<div class=\"alert alert-info\" role=\"alert\">\n",
"\n",
"**Task 7**:\n",
" \n",
"Take a look at the data types `list`, `tuple`, `set`, and `dictionary` in the Python documentation. Provide a brief example for each data type below and explain the differences between these data types.\n",
"\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# List\n",
"list_variable = [1, 2, 3]\n",
"# A list is a collection which is ordered and changeable. It allows duplicate members. Different data types are allowed.\n",
"\n",
"# Tuple\n",
"tuple_variable = (1, 2, 3)\n",
"# A tuple is a collection which is ordered and unchangeable. It allows duplicate members. Different data types are allowed.\n",
"\n",
"# Set\n",
"set_variable = {1, 2, 3}\n",
"# A set is a collection which is unordered and unindexed. No duplicate members. Different data types are allowed.\n",
"\n",
"# Dictionary\n",
"dictionary_variable = {\"key1\": 1, \"key2\": 2, \"key3\": 3}\n",
"# A dictionary is a collection which is unordered, changeable and indexed. No duplicate members. Different data types are allowed."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-warning\">\n",
"\n",
"**TODO:**\n",
" \n",
"- Functions\n",
"- Classes\n",
"- Control Structures\n",
" \n",
"</div>"
]
}
],
"metadata": {
"celltoolbar": "Tags",
"interpreter": {
"hash": "d634d9120e07fcb10ea4e2485f922980f44c65c5790b35d436a0dd63f2b90915"
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit b9ef099

Please sign in to comment.