Commit 5b4930c2 authored by christian.foerster's avatar christian.foerster

intital tutorial files

parent acd227c6
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python Tutorial Part 2 - Advanced Topics\n",
"\n",
"## Object Oriented Programming (OOP)\n",
"\n",
"OOP is a programming paradigm based on the concept of \"objects\", which can contain data, in the form of attributes and code, in the form of methods.\n",
"It's a different way of you thinking about your model/problem. Instead of thinking of a problem as a sequence of commands that need to be executed (procedural, **linear**), you identify single processes/actors and define their attributes and functionalities, so that they can interact with other actors (**nonlinear**).\n",
"\n",
"**Example Problem:\n",
"Create a plotting library**\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Structering your code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Performance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Large Projects\n",
"Pycharm, Git, Debugging, Naming conventions, goot practice"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Ein kurzes Intro zu Python\n",
"\n",
"\n",
"## Some nice-to-know's\n",
"* Die Indexierung von Python started bei 0\n",
"* Der Syntax wird vor allem über tabs (4 Leerzeichen) gesteuert.\n",
"\n",
"## Variablen"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"25.0\n",
"6.7\n",
"5\n",
"5.6\n",
"vdsfdsafdsaffsaf\n"
]
},
{
"data": {
"text/plain": [
"25"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Integer\n",
"a=25\n",
"\n",
"# Float\n",
"b=5.6\n",
"\n",
"# String\n",
"c=\"6.7\"\n",
"\n",
"#Boolean\n",
"d=True\n",
"\n",
"# Die Umrechnung der Variablentypen funktioniert so (der int() Befehl rundet immer ab)\n",
"print(float(a),float(c),int(b),str(b),sep=\"\\n\")\n",
"print(\"vdsfdsafdsaffsaf\")\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"That's a: <class 'float'>\n",
"And that's a <class 'int'>\n"
]
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Achtung!\n",
"print(\"That's a: \", type(6/3))\n",
"print(\"And that's a \",type(6//3))\n",
"\n",
"6//3"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ich bin 29.00 alt.\n",
"Ich bin 29 alt.\n",
"Ich bin 29 alt.\n",
"C:\\adg\\ggj\n"
]
}
],
"source": [
"'''\n",
"dsfdsafdsa\n",
"sdfdsafsa\n",
"sdfsafdsa\n",
"asdf\n",
"\n",
"'''\n",
"\n",
"# das ist eine Möglichkeit string und float oder integer zu verbinden\n",
"print(\"Ich bin %.2f alt.\" %29) # % für hier muss noch was rein, .2 für 2 Kommastellen, f für float\n",
"print(\"Ich bin %i alt.\"%29) # ...\n",
"\n",
"# das ist eine andere, bei der man die kommastellen festlegen kann\n",
"print(\"Ich bin \"+str(29)+\" alt.\")\n",
"\n",
"\n",
"#bei verzeichnissen \n",
"print(r\"C:\\adg\\ggj\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'abc efg'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"abc \"+\"efg\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"aa,asfdsafd b,dsfs c\n",
"a, b, c\n",
"c, b, a\n",
"c, b, a\n",
"firstsecondfirst\n",
"4-4=0\n"
]
}
],
"source": [
"# oder auch so\n",
"print('a{0},asfdsafd {1},dsfs {2}'.format('a', 'b', 'c'))\n",
"\n",
"print('{}, {}, {}'.format('a', 'b', 'c'))\n",
"\n",
"print('{2}, {1}, {0}'.format('a', 'b', 'c',\"d\"))\n",
"\n",
"print('{2}, {1}, {0}'.format(*'abcd'))\n",
"\n",
"print('{0}{1}{0}'.format('first', 'second'))\n",
"\n",
"print(\"{x}-{x}={z[0]}\".format(x=4,z=[0,7]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Die Verschiedenen Datentypen\n",
"Die hilfreichste Form der Datenspeicherung ist in Listen. In Listen können alle Daten- und Variablentypen beherrbergt werden. Initiert wird eine Liste mit [ ]. Außerdem gibt es Tupel, Dictionaries und vieles mehr.\n",
"### Listen"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[]\n",
"[2, 4]\n",
"[2, 4, 2, 4, 2, 4]\n",
"[2, 4, 2, 4, 2, 4]\n",
"Länge: 6\n"
]
}
],
"source": [
"# eine leere Liste\n",
"mylist=[]\n",
"\n",
"print(mylist)\n",
"# wir hängen den Wert 2 und den Wert 4 mit der Methode .append() and die Liste an\n",
"mylist.append(2)\n",
"mylist.append(4)\n",
"\n",
"print(mylist)\n",
"# oder \n",
"mylist.extend([2,4]) \n",
"\n",
"#oder\n",
"mylist=mylist+[2,4]\n",
"\n",
"print(mylist)\n",
"# jetzt drucken wir die Liste und danach ihre Länge\n",
"print(mylist)\n",
"print(\"Länge: \"+str(len(mylist)))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[3, 4, 5]\n",
"[1, 2, 3, 4, 5]\n",
"1\n",
"[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n"
]
}
],
"source": [
"Numbers=[1,2,3,4,5,6,7,8,9,10]\n",
"# Beschneiden von Listen\n",
"print(Numbers[2:5])\n",
"print(Numbers[:5])\n",
"\n",
"# Indexierung von hinten\n",
"print(Numbers[0]) # gibt den letzten Wert aus\n",
"\n",
"# Listenreihenfolge umdrehen\n",
"print(Numbers[::-1])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n"
]
}
],
"source": [
"#Liste sortieren\n",
"Numbers.sort()\n",
"print(Numbers)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'int'>\n",
"<class 'float'>\n",
"<class 'list'>\n"
]
}
],
"source": [
"# wir erstellen eine neue Liste mit verschiedenen Daten- und Variablentypen und lassen uns die typen ausdrucken\n",
"newlist=[2,3.2,[3],True]\n",
"\n",
"# drucken der einzelnen Elemente durch Indexierung\n",
"print(type(newlist[0]),type(newlist[1]),type(newlist[2]),sep=\"\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['a', 'b', 'c', 'd', 'e', 'f', 'g']"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(\"abcdefg\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tupel"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Tupel sind unveränderliche Listen. Für ein Tupel wird () statt [] verwendet.\n",
"x = (1, 2, 3)\n",
"len(x)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Indexierung\n",
"y = (4, 5, 6)\n",
"y[2]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(1, 2, 3), (4, 5, 6)]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"listOfTuples = [x, y]\n",
"listOfTuples"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"32\n",
"120000\n",
"129999\n"
]
}
],
"source": [
"# erstelle 2 variablen zur selben Zeit (beide string variablen)\n",
"(age, income) = \"32,120000\".split(',')\n",
"print(age)\n",
"print(income)\n",
"print(income.replace(\"0\",\"9\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dictionaries"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Jakob': 'Benisch', 'Stefanie': 'Wiek', 'Winnie': 'Puh', 'Christian': 'Koch', 'blaa': [1, 2, 3, 43, 12, 21, 3, 43]}\n",
"Koch\n"
]
}
],
"source": [
"# Eine ungeordnete Liste, bei der man über Schlüsselwörter indexiert und die entsprechende attribute erhält\n",
"\n",
"lastnames = {}\n",
"lastnames[\"Jakob\"] = \"Benisch\"\n",
"lastnames[\"Stefanie\"] = \"Wiek\"\n",
"lastnames[\"Winnie\"] = \"Puh\"\n",
"lastnames[\"Christian\"] = \"Koch\"\n",
"lastnames[\"blaa\"]=[1,2,3,43,12,21,3,43]\n",
"\n",
"print(lastnames)\n",
"\n",
"print(lastnames[\"Christian\"])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"print(lastnames.get(\"Bruno\"))\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"# wenn wir nach etwas suchen, was nicht im dict ist, dann bekommen wir None ausgegeben\n",
"print(lastnames.get(\"NX-01\"))\n",
"\n",
"# Achtung: captains[\"NX-01\"] -> diese Art des zugriffs würde einen error verursachen"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Jakob: Benisch\n",
"Stefanie: Wiek\n",
"Winnie: Puh\n",
"Christian: Koch\n"
]
}