Skip to content

Commit

Permalink
restarted a new git repository
Browse files Browse the repository at this point in the history
removed private information before public release
  • Loading branch information
ray-chew committed Mar 19, 2024
0 parents commit 9b62ab8
Show file tree
Hide file tree
Showing 108 changed files with 123,441 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.ipynb_*
*.swp
*.vscode
*.h5
*.dat
*.pyc
*w11/swe/output/frames/*
*_old
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Mathematische Modellierung in der Klimaforschung




----

## .gitignore

`.gitignore` contains files which are to be ignored by git.

Empty file added w0/Try/file.txt
Empty file.
59 changes: 59 additions & 0 deletions w0/hello_world.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "57178893-d689-4d3d-b699-e49917a47a7f",
"metadata": {},
"source": [
"# Tutorial 1\n",
"## Exercise 1: Hello world!\n",
"This exercise is to help us get started with *git* and our Python setup.\n",
"\n",
"1. Pull this notebook and open it with jupyter lab.\n",
"2. Run the cell below. Does it print \"Hello world!\"?\n",
"3. Familiarise yourself with the jupyer lab interface."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "c70e0481-afe1-4c8e-b8b3-7fae045e2a01",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello World!\n"
]
}
],
"source": [
"print(\"Hello World!\")"
]
}
],
"metadata": {
"interpreter": {
"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
},
"kernelspec": {
"display_name": "Python 3.9.5 64-bit",
"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.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file added w0/notes.pdf
Binary file not shown.
206 changes: 206 additions & 0 deletions w1/functions_and_lists.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "a8d5e426-5dff-42d2-b819-b68fdbf63f9f",
"metadata": {},
"source": [
"# Introduction to Python - 1\n",
"\n",
"---\n",
"\n",
"## Functions\n",
"\n",
"Functions are self-contained reusable code that does a specific task. Functions may take one or more arguments, and functions may return a result. The code snippet below demonstrates the function of a function."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5139ac13-2bc3-42c8-b048-95ca3cf15884",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a36cf1cf-83b6-4559-b5e9-56b90dcba828",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15\n",
"-5\n",
"(6+1j)\n"
]
}
],
"source": [
"# In Python, comments start with a hashtag\n",
"# And the keyword ``def`` introduces a function definition.\n",
"\n",
"# Let's make a function called `sum_func` that takes two values `a` and `b` as inputs, i.e. arguments:\n",
"def sum_func(a,b):\n",
" # All code belonging to a function must be indented\n",
" \n",
" # Let's calculate the sum of `a` and `b`, and store it in `c`:\n",
" c = a + b\n",
" \n",
" # finally, we want to return the result of sum:\n",
" return c\n",
"\n",
"# Then now we can reuse this code snippet by calling the function with any values of `a` and `b`:\n",
"print(sum_func(5,10))\n",
"print(sum_func(-8,3))\n",
"\n",
"# Because the Python `+` operator supports addition of complex numbers, the function also works with complex inputs\n",
"print(sum_func(5+3j,1-2j))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8b6d4766-f28f-4465-8d24-3d7a889163f5",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "sum_func() missing 1 required positional argument: 'b'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-3-0ddc42518b76>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Finally, recall that positional arguments are mandatory.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# Let's see what happens if we only input `a` but not `b`:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msum_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: sum_func() missing 1 required positional argument: 'b'"
]
}
],
"source": [
"# Finally, recall that positional arguments are mandatory.\n",
"# Let's see what happens if we only input `a` but not `b`:\n",
"print(sum_func(5,))"
]
},
{
"cell_type": "markdown",
"id": "3f45138c-cf4b-4bc1-9055-a63747a10d54",
"metadata": {},
"source": [
"### Questions:\n",
"1. Can you create a function that takes both positional and keyword arguments?\n",
"2. What happens if you change the order of the keyword arguments?\n",
"\n",
"----"
]
},
{
"cell_type": "markdown",
"id": "61a037f2-6e54-4b6f-a648-598200f85a6f",
"metadata": {},
"source": [
"## Lists\n",
"\n",
"Lists in Python store a collection of items in a single variable, for example<br>\n",
"``a=[1,2,3,4,5]``<br>\n",
"stores the values 1 to 5 in the variable `a`.\n",
"\n",
"Lists are pretty flexible in Python, see the code snippet below."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "3847780c-1b0b-42f2-971d-5145453aa3b2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 'a', 'dog', 'item', 10.0, 55.5, 1e-06]\n",
"[1, 2, 3, 'a', 'dog', 'item', 10.0, 55.5, 1e-06, <function sum_func at 0x7f045c3fa200>]\n",
"[1, 2, 3, 'a', 'dog', 'item', 10.0, 55.5, 1e-06, <function sum_func at 0x7f045c3fa200>, array([[ 0, 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14],\n",
" [15, 16, 17, 18, 19],\n",
" [20, 21, 22, 23, 24]])]\n"
]
}
],
"source": [
"# A list can store a collection (floating point) numbers, integers, and strings, etc.\n",
"lst = [1,2,3,'a','dog','item',10.0,55.5,1e-6]\n",
"\n",
"# Now, when we print `lst`, we get the collection of items we defined above.\n",
"print(lst)\n",
"\n",
"# We can even store a function (and other objects) in a list.\n",
"# Let's add the `sum_func` function into the list:\n",
"lst_func = lst + [sum_func]\n",
"print(lst_func)\n",
"\n",
"# Or a 2D array:\n",
"array = np.arange(25).reshape(5,5)\n",
"lst_func += [array]\n",
"print(lst_func)"
]
},
{
"cell_type": "markdown",
"id": "c206297a-03fd-4d41-be33-157bd12c5267",
"metadata": {},
"source": [
"----\n",
"\n",
"## Additional reading:\n",
"There are many ways to store data in Python, and `list` is just one them. Here are some other examples of [*data structures*](https://en.wikipedia.org/wiki/Data_structure): \n",
"\n",
"* In pure Python, i.e. Python without any additional libraries, bells, and whistles, we have *dictionaries* and *lists*. \n",
"* In the `numpy` library, we have [*numpy arrays*](https://numpy.org/doc/stable/reference/generated/numpy.array.html),\n",
"* while for data analysis, there are [*pandas dataframes*](https://pandas.pydata.org/docs/user_guide/dsintro.html#dataframe) and [*xarrays*](http://xarray.pydata.org/en/stable/).\n",
"* For larger-than-memory arrays, Python has the dask library with its [*dask arrays*](https://docs.dask.org/en/latest/array.html).\n",
"\n",
"These are a just a few more-popular examples of the types of data structures that Python and its libraries offers. In this course, we will stick to the [KISS principle](https://en.wikipedia.org/wiki/KISS_principle), and we will only use the standard Python lists and dictionaries. We will also be using [classes](https://docs.python.org/3/tutorial/classes.html) to create our own data containers. The most 'advanced' arrays that we will be using will be numpy arrays.\n",
"\n",
"Numpy array, dictionaries and classes will be introduced in later tutorial sessions.\n",
"\n",
"### Question: \n",
"1. What is a *list*, an *array*, a *matrix*, and a *tuple*? What are their similarities and differences?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "87d65e32-2724-4451-bc03-493fb7c03e3b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 9b62ab8

Please sign in to comment.