{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# BlahBlah" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "print('Hello World')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "i = 666" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "i = 'hallo'" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i.find('l')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def f(a):\n", " return a**2" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(f)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "36" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(6)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in range(5):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "code_str = '''\n", "for i in range(5):\n", " print(i)\n", "'''" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(code_str)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "exec(code_str)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "code_str = '''\n", "import random\n", "rnd = random.randrange(6)\n", "'''" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "context = {}\n", "exec(code_str, context)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "context['rnd']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings und Quotes" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "s = 'abc'" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "s = \"abc\"" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hallo, du \"Suesser\"\n" ] } ], "source": [ "s = 'Hallo, du \"Suesser\"'\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hallo, du 'Suesser'\n" ] } ], "source": [ "s = \"Hallo, du 'Suesser'\"\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hallo, du \"Suesser\" 'du'\n" ] } ], "source": [ "s = 'Hallo, du \"Suesser\" \\'du\\''\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Comments vs. Docstrings" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "# \n", "# dieser extrem komplexe algorithmus verwendet ein naeherungsverfahren,\n", "# um die beiden paramete a und b zu addieren.\n", "# die summe ist dann auch der wert des funktionsaufrufs \n", "# \n", "def add(a,b):\n", " return a+b" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = add(2, 5)\n", "s" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "def add(a,b):\n", " '''\n", " dieser extrem komplexe algorithmus verwendet ein naeherungsverfahren,\n", " um die beiden paramete a und b zu addieren.\n", " die summe ist dann auch der wert des funktionsaufrufs \n", " '''\n", " return a+b" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\n dieser extrem komplexe algorithmus verwendet ein naeherungsverfahren,\\n um die beiden paramete a und b zu addieren.\\n die summe ist dann auch der wert des funktionsaufrufs \\n '" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add.__doc__" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function add in module __main__:\n", "\n", "add(a, b)\n", " dieser extrem komplexe algorithmus verwendet ein naeherungsverfahren,\n", " um die beiden paramete a und b zu addieren.\n", " die summe ist dann auch der wert des funktionsaufrufs\n", "\n" ] } ], "source": [ "help(add)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Variables and Types" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1.5\n", "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Assignment Fun" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "a = 1\n", "b = 2" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1,2\n" ] } ], "source": [ "print(a, b, sep=',')" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "a, b = 1, 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Swap" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "tmp = a\n", "a = b\n", "b = tmp" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2,1\n" ] } ], "source": [ "print(a, b, sep=',')" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "a, b = 1, 2" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "a, b = b, a" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2,1\n" ] } ], "source": [ "print(a, b, sep=',')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assignment Details" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140606563548752" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 42\n", "id(a)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140606563548752" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a\n", "id(b)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a is b" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "b += 1" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "43" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "a = 5" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "43" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "a = 42\n", "b = 42" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140606563548752" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140606563548752" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Boolean" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1.5 < 42" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(1.5 < 42)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "jo eh\n" ] } ], "source": [ "if 42:\n", " print('jo eh')" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "na wirklich net\n" ] } ], "source": [ "if 0:\n", " print('jo eh')\n", "else:\n", " print('na wirklich net')" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1.5 < 42\n", "a" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1.5 > 42\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Integers" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "i = 1" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 1 * 2**0 \n", "i" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 * 2**1 + 1 * 2**0" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0b11" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0b100" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 * 2**2 + 0 * 2**1 + 0 * 2**0" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0x10" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 * 16**1 + 0 * 16**0" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0o10" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 == 2" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 != 2" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3/2" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3//2" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5%2" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "11%3" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 ist durch 5 teilbar\n" ] } ], "source": [ "if 10%5 == 0:\n", " print('10 ist durch 5 teilbar')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Integers are Infinitely long" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [], "source": [ "i = 2**64 - 1" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551615" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [], "source": [ "i += 1" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**1000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operator Precedence" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2*10%3" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2*10)%3" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2*(10%3)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2*11%3" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2*11)%3" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2*(11%3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Floating Point Comparison" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [], "source": [ "a = 2/3\n", "b = 4/6" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a == b # ERROR!" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(a-b) < 0.00001" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Datatype Conversions" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [], "source": [ "s = '666'\n", "i = 666" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i == s" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i) == type(s)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i == int(s)" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(s)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " invalid literal for int() with base 10: 'abc'\n" ] } ], "source": [ "try:\n", " int('abc')\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2748" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('abc', 16)" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.234" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float('1.234')" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(i)" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [], "source": [ "if i == 0:\n", " print('falsch')" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "true\n" ] } ], "source": [ "if i:\n", " print('true')" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type('abc')" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "i = 666\n", "s = str(i)\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(666)" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(666.666)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666.0" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(666)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Compound Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [], "source": [ "l = [1,2,3,4]" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len('abc')" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [], "source": [ "l.append(5)" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [], "source": [ "l = [1.9, 'drei', 'hansi']" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [], "source": [ "l.extend([1, 'one'])" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1.9, 'drei', 'hansi', 1, 'one']" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [], "source": [ "l.extend('abc')" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1.9, 'drei', 'hansi', 1, 'one', 'a', 'b', 'c']" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'int' object is not iterable\n" ] } ], "source": [ "try:\n", " l.extend(666)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 = [1,2,3]\n", "l2 = [2,3,4]\n", "l1 < l2" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 = [1,2,3]\n", "l2 = [1,2,4]\n", "l1 < l2" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 = 'abc'\n", "s2 = 'abd'\n", "s1 < s2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mutability/Immutability" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [], "source": [ "l1 = [1,2,3,4]\n", "l2 = l1" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 is l2" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1) == id(l2)" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [], "source": [ "l1.append(5)" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i1 = 666\n", "i2 = i1\n", "i1 is i2" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [], "source": [ "i1 += 1" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "667" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i1" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i2" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140606428182128" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(i2)" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140606428182896" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(i1)" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 is l2" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [], "source": [ "l1 = [1,2,3,4]\n", "l2 = l1[:]" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1.append(5)\n", "l1" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [], "source": [ "l1 = [1, 2, ['a', 'b'], 3, 4]\n", "l2 = l1[:]" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [], "source": [ "l1.append(5)" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['a', 'b'], 3, 4, 5]" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['a', 'b'], 3, 4]" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [], "source": [ "l2[2].append('c')" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['a', 'b', 'c'], 3, 4]" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['a', 'b', 'c'], 3, 4, 5]" ] }, "execution_count": 144, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuple" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [], "source": [ "t = (1,2,3,4)" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for element in t:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'tuple' object has no attribute 'append'\n" ] } ], "source": [ "try:\n", " t.append(5)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Liste mit einem Element:" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [], "source": [ "l = [1]" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(l)" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1]" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuple mit einem Element?" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [], "source": [ "t = (1)" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 152, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(t)" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3*(4%2)" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [], "source": [ "t = (1,)" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``in`` Operator" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [], "source": [ "l = [4,1,8,2,44,0]" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 in l" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 158, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 not in l" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 159, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionary" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [], "source": [ "people = {\n", " '1037190666': ('Joerg', 'Faschingbauer', '19.6.1966'),\n", " '1234250497': ('Caro', 'Faschingbauer', '25.4.1997'),\n", "}" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('Joerg', 'Faschingbauer', '19.6.1966')" ] }, "execution_count": 161, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people['1037190666']" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " '2345010101'\n" ] } ], "source": [ "try:\n", " people['2345010101']\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [], "source": [ "people['2345010101'] = ('Elizabeth', 'Queen', '1.1.1901')" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('Elizabeth', 'Queen', '1.1.1901')" ] }, "execution_count": 164, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people['2345010101']" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [], "source": [ "del people['2345010101']" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 166, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'2345010101' in people" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [], "source": [ "s = {'Joerg', 'Caro', 'Johanna', 'Philipp'}" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 168, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Joerg' in s" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [], "source": [ "s.add('Evi')" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Evi' in s" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [], "source": [ "s.remove('Joerg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise: Digit To English Word" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [], "source": [ "translation_table = { 0: 'zero', 1: 'one', 2: 'two' }" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [], "source": [ "zahl = '2'\n", "zahl = int(zahl)" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [], "source": [ "translation = translation_table[zahl]" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "two\n" ] } ], "source": [ "print(translation)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise: Determine Maximum of Two Numbers" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [], "source": [ "l, r = 200, 99 # tuple unpacking" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "200\n" ] } ], "source": [ "if l < r:\n", " print(r)\n", "else:\n", " print(l)" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "200" ] }, "execution_count": 178, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(l, r)" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "356" ] }, "execution_count": 179, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(200, 99, 356, 1)" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [], "source": [ "numbers = [200, 99, 356, 1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Iterable?**" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "200\n", "99\n", "356\n", "1\n" ] } ], "source": [ "for element in numbers:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "b\n", "c\n" ] } ], "source": [ "for element in 'abc':\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "356" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(numbers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings are iterable" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'c'" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max('abc')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Slicing**" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[200, 99, 356, 1]" ] }, "execution_count": 185, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numbers" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "99" ] }, "execution_count": 186, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numbers[1]" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "356" ] }, "execution_count": 187, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numbers[2]" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " list index out of range\n" ] } ], "source": [ "try:\n", " numbers[4]\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[99, 356]" ] }, "execution_count": 189, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numbers[1:3]" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[99, 356, 1]" ] }, "execution_count": 190, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numbers[1:4]" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[99, 356, 1]" ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numbers[1:len(numbers)]" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[99, 356, 1]" ] }, "execution_count": 192, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numbers[1:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ``while`` Loops" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5050\n" ] } ], "source": [ "summe = 0\n", "i = 1\n", "\n", "while i <= 100:\n", " summe += i\n", " i += 1\n", "\n", "print(summe)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Pythonicity**" ] }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5050" ] }, "execution_count": 194, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(range(1, 101))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Use Case**: maximal sechs mal wuerfeln, sich freuen, wenn eine Sechser faellt, und sonst jammern" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Version 1: potschert, weil wir nicht jammern" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay!, nach 2 versuchen\n", "ende\n" ] } ], "source": [ "import random\n", "\n", "versuch_nummer = 1\n", "while versuch_nummer <= 6:\n", " augen = random.randrange(1,7)\n", " if augen == 6:\n", " print('yay!, nach', versuch_nummer, 'versuchen')\n", " break\n", " versuch_nummer += 1\n", " \n", "print('ende')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Version 2: potschertes Jammern" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Jammertal!!!!\n" ] } ], "source": [ "import random\n", "\n", "versuch_nummer = 1\n", "yay = False\n", "while versuch_nummer <= 6:\n", " augen = random.randrange(1,7)\n", " if augen == 6:\n", " print('yay!, nach', versuch_nummer, 'versuchen')\n", " yay = True\n", " break\n", " versuch_nummer += 1\n", "\n", "if not yay:\n", " print('Jammertal!!!!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Version 3: Pythonic jammern" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay!, nach 1 versuchen\n" ] } ], "source": [ "import random\n", "\n", "versuch_nummer = 1\n", "while versuch_nummer <= 6:\n", " augen = random.randrange(1,7)\n", " if augen == 6:\n", " print('yay!, nach', versuch_nummer, 'versuchen')\n", " break\n", " versuch_nummer += 1\n", "else:\n", " print('Jammertal!!!!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Ranges" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [], "source": [ "numbers = [3, 2, 7, 99, 5, 3]" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[7, 99, 5]" ] }, "execution_count": 199, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numbers[2:5]" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [], "source": [ "s = 'Hello World'" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Worl'" ] }, "execution_count": 201, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[6:10]" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello'" ] }, "execution_count": 202, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[0:5]" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hlo'" ] }, "execution_count": 203, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[0:5:2]" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in range(5):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in range(0,5):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n", "4\n" ] } ], "source": [ "for i in range(2,5):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "4\n", "6\n" ] } ], "source": [ "for i in range(2,7,2):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise: Sort out Duplicates" ] }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [], "source": [ "output_liste = [2, 3, 1]" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "jo eh\n" ] } ], "source": [ "element = 1\n", "if element in output_liste:\n", " print('jo eh')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Set**" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [], "source": [ "have = set()" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [], "source": [ "have.add(2)\n", "have.add(3)\n", "have.add(1)" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 in have" ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 213, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 in have" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``dict.fromkeys()``" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set does not preserve insertion order" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "50\n", "100\n" ] } ], "source": [ "s = set()\n", "s.add(100)\n", "s.add(1)\n", "s.add(50)\n", "for element in s:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionary preserves Insertion Order" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\n", "1\n", "50\n" ] } ], "source": [ "d = {}\n", "d[100] = 'x'\n", "d[1] = 'x'\n", "d[50] = 'x'\n", "for element in d:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [], "source": [ "input_list = [2, 3, 1, 10, 3, 3, 1, 10, 5, 2]" ] }, { "cell_type": "code", "execution_count": 217, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2: None, 3: None, 1: None, 10: None, 5: None}" ] }, "execution_count": 217, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = dict.fromkeys(input_list)\n", "d" ] }, { "cell_type": "code", "execution_count": 218, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n", "1\n", "10\n", "5\n" ] } ], "source": [ "for element in d:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 1, 10, 5]" ] }, "execution_count": 219, "metadata": {}, "output_type": "execute_result" } ], "source": [ "output_list = list(d)\n", "output_list" ] }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 220, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list('abc')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Algorithmic Complexity" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "O(n)" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 221, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [2,3,4,1,5,7,8]\n", "100 in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# More on Lists" ] }, { "cell_type": "code", "execution_count": 222, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 1, 5, 6, 3000]" ] }, "execution_count": 222, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [3,2,1,5,6]\n", "l.append(3000)\n", "l" ] }, { "cell_type": "code", "execution_count": 223, "metadata": {}, "outputs": [], "source": [ "l.insert(2, 'blah')" ] }, { "cell_type": "code", "execution_count": 224, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 'blah', 1, 5, 6, 3000]" ] }, "execution_count": 224, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 225, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['erstes/nulltes', 3, 2, 'blah', 1, 5, 6, 3000]" ] }, "execution_count": 225, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.insert(0, 'erstes/nulltes')\n", "l" ] }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3000, 6, 5, 1, 'blah', 2, 3, 'erstes/nulltes']" ] }, "execution_count": 226, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.reverse()\n", "l" ] }, { "cell_type": "code", "execution_count": 227, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['erstes/nulltes', 3, 2, 'blah', 1, 5, 6, 3000]" ] }, "execution_count": 227, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.reverse()\n", "l" ] }, { "cell_type": "code", "execution_count": 228, "metadata": {}, "outputs": [], "source": [ "r = reversed(l)" ] }, { "cell_type": "code", "execution_count": 229, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list_reverseiterator" ] }, "execution_count": 229, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(r)" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3000\n", "6\n", "5\n", "1\n", "blah\n", "2\n", "3\n", "erstes/nulltes\n" ] } ], "source": [ "for element in r:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 231, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " '<' not supported between instances of 'int' and 'str'\n" ] } ], "source": [ "try:\n", " l.sort()\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [], "source": [ "l = [2, 6, 1, 89, 1, 34567654567]\n", "l.sort()" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 1, 2, 6, 89, 34567654567]" ] }, "execution_count": 233, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 234, "metadata": {}, "outputs": [], "source": [ "l = [2, 6, 1, 89, 34567654567]" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 6, 89, 34567654567]" ] }, "execution_count": 235, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = sorted(l)\n", "s" ] }, { "cell_type": "code", "execution_count": 236, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 6, 1, 89, 34567654567]" ] }, "execution_count": 236, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 237, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 4]" ] }, "execution_count": 237, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [2,1,3,4]\n", "del l[1]\n", "l" ] }, { "cell_type": "code", "execution_count": 238, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 238, "metadata": {}, "output_type": "execute_result" } ], "source": [ "el = l.pop(2)\n", "el" ] }, { "cell_type": "code", "execution_count": 239, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3]" ] }, "execution_count": 239, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Type names are callable**" ] }, { "cell_type": "code", "execution_count": 282, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 282, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list('abc')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# More On Dictionaries" ] }, { "cell_type": "code", "execution_count": 240, "metadata": {}, "outputs": [], "source": [ "table = { 'one': 1, 'two': 2 }" ] }, { "cell_type": "code", "execution_count": 241, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 241, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table['one']" ] }, { "cell_type": "code", "execution_count": 242, "metadata": {}, "outputs": [], "source": [ "table['three'] = 3" ] }, { "cell_type": "code", "execution_count": 243, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 243, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'three' in table" ] }, { "cell_type": "code", "execution_count": 244, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'four'\n" ] } ], "source": [ "try:\n", " table['four']\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 245, "metadata": {}, "outputs": [], "source": [ "value = table.get('four')" ] }, { "cell_type": "code", "execution_count": 246, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(value)" ] }, { "cell_type": "code", "execution_count": 247, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "leider nicht\n" ] } ], "source": [ "if value is None:\n", " print('leider nicht')\n", "else:\n", " print('gsd')" ] }, { "cell_type": "code", "execution_count": 248, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "value = table.get('four', 4)\n", "print(value)" ] }, { "cell_type": "code", "execution_count": 249, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 249, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'four' in table" ] }, { "cell_type": "code", "execution_count": 250, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 250, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'one' in table" ] }, { "cell_type": "code", "execution_count": 251, "metadata": {}, "outputs": [], "source": [ "del table['one']" ] }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 252, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'one' in table" ] }, { "cell_type": "code", "execution_count": 253, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'two': 2, 'three': 3}" ] }, "execution_count": 253, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table" ] }, { "cell_type": "code", "execution_count": 254, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 254, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table.setdefault('three', 3)" ] }, { "cell_type": "code", "execution_count": 255, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 255, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table.setdefault('one', 1)" ] }, { "cell_type": "code", "execution_count": 256, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'two': 2, 'three': 3, 'one': 1}" ] }, "execution_count": 256, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Dictionary Iteration**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Keys ..." ] }, { "cell_type": "code", "execution_count": 257, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "two\n", "three\n", "one\n" ] } ], "source": [ "for element in table:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "two\n", "three\n", "one\n" ] } ], "source": [ "for k in table.keys():\n", " print(k)" ] }, { "cell_type": "code", "execution_count": 259, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n", "1\n" ] } ], "source": [ "for v in table.values():\n", " print(v)" ] }, { "cell_type": "code", "execution_count": 260, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('two', 2)\n", "('three', 3)\n", "('one', 1)\n" ] } ], "source": [ "for element in table.items():\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Tuple unpacking**" ] }, { "cell_type": "code", "execution_count": 265, "metadata": {}, "outputs": [], "source": [ "a, b = 1, 2" ] }, { "cell_type": "code", "execution_count": 264, "metadata": {}, "outputs": [], "source": [ "a, b = b, a" ] }, { "cell_type": "code", "execution_count": 263, "metadata": {}, "outputs": [], "source": [ "(a, b) = (1, 2)" ] }, { "cell_type": "code", "execution_count": 267, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Key: two , Value: 2\n", "Key: three , Value: 3\n", "Key: one , Value: 1\n" ] } ], "source": [ "for element in table.items():\n", " k, v = element\n", " print('Key:', k, ', Value:', v)" ] }, { "cell_type": "code", "execution_count": 268, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Key: two , Value: 2\n", "Key: three , Value: 3\n", "Key: one , Value: 1\n" ] } ], "source": [ "for k, v in table.items():\n", " print('Key:', k, ', Value:', v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Tuple unpacking --- CSV**" ] }, { "cell_type": "code", "execution_count": 269, "metadata": {}, "outputs": [], "source": [ "csv = [\n", " ['1037190666', 'Joerg', 'Faschingbauer'],\n", " ['1234250497', 'Caro', 'Faschingbauer'],\n", "]" ] }, { "cell_type": "code", "execution_count": 270, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SVNr: 1037190666, Firstname: Joerg, Lastname: Faschingbauer\n", "SVNr: 1234250497, Firstname: Caro, Lastname: Faschingbauer\n" ] } ], "source": [ "for svnr, firstname, lastname in csv:\n", " print(f'SVNr: {svnr}, Firstname: {firstname}, Lastname: {lastname}')" ] }, { "cell_type": "code", "execution_count": 272, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " too many values to unpack (expected 2)\n" ] } ], "source": [ "try:\n", " for svnr, firstname in csv:\n", " pass\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 274, "metadata": {}, "outputs": [], "source": [ "for svnr, firstname, _ in csv:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**``dict.update()``**" ] }, { "cell_type": "code", "execution_count": 275, "metadata": {}, "outputs": [], "source": [ "table = { 'one': 1, 'two': 2 }" ] }, { "cell_type": "code", "execution_count": 276, "metadata": {}, "outputs": [], "source": [ "another_table = {'one': 100, 'three': 300}" ] }, { "cell_type": "code", "execution_count": 277, "metadata": {}, "outputs": [], "source": [ "table.update(another_table)" ] }, { "cell_type": "code", "execution_count": 278, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 100, 'two': 2, 'three': 300}" ] }, "execution_count": 278, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Type names are callable**" ] }, { "cell_type": "code", "execution_count": 283, "metadata": {}, "outputs": [], "source": [ "pairs = [('1037190666', 'Joerg'), ('1234250497', 'Caro')]" ] }, { "cell_type": "code", "execution_count": 284, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'1037190666': 'Joerg', '1234250497': 'Caro'}" ] }, "execution_count": 284, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict(pairs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# More On Sets" ] }, { "cell_type": "code", "execution_count": 279, "metadata": {}, "outputs": [], "source": [ "s1 = {1, 2, 3, 4}\n", "s2 = {3, 4, 5, 6}" ] }, { "cell_type": "code", "execution_count": 281, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 5, 6}" ] }, "execution_count": 281, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 ^ s2 # symmetische differenz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Type names are callable**" ] }, { "cell_type": "code", "execution_count": 288, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a', 'b', 'c'}" ] }, "execution_count": 288, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set('abc')" ] }, { "cell_type": "code", "execution_count": 287, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 5, 6}" ] }, "execution_count": 287, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set([1,3,2,5,6])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ``eval()``, ``json``" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``eval()``" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "s = '666'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(s)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval(s)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666.66" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval('666.66')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval('\"abc\"')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'one', 2: 'two'}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval('{1: \"one\", 2: \"two\"}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**WARUM TF?** Netzwerkkommunikation, vielleicht?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Client ..." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "import time\n", "\n", "request_to_send = {\n", " 'action': 'scan',\n", " 'timestamp': time.time(),\n", "}\n", "\n", "request_str_to_send = repr(request_to_send)\n", "\n", "# send off request_str_to_send" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"{'action': 'scan', 'timestamp': 1651046407.3633313}\"" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "repr(request_to_send)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Server ... (reads request off the cable)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "# read request (as str) off cable\n", "request_str_from_cable = request_str_to_send\n", "\n", "request_from_cable = eval(request_str_from_cable)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'action': 'scan', 'timestamp': 1651046335.8477345}" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "request_from_cable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``json``" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "import json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Client ..." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'action': 'scan', 'timestamp': 1651046407.3633313}" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "request_to_send" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'{\"action\": \"scan\", \"timestamp\": 1651046407.3633313}'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "request_str_to_send = json.dumps(request_to_send)\n", "request_str_to_send" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "sends off request_str_to_send ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Server ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "reads request from cable" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "request_str_from_cable = request_str_to_send" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'action': 'scan', 'timestamp': 1651046407.3633313}" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "request_from_cable = json.loads(request_str_from_cable)\n", "request_from_cable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# References, (Im)mutability" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "t = ('abc', 2, {}, [])" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'tuple' object has no attribute 'append'\n" ] } ], "source": [ "try:\n", " t.append(666)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'tuple' object does not support item assignment\n" ] } ], "source": [ "try:\n", " t[2] = 0\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "d = t[2]\n", "l = t[3]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{}" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "l.append(666)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('abc', 2, {}, [666])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "d['one'] = 1" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('abc', 2, {'one': 1}, [666])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139909434561408" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(t[3])" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139909434561408" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "i = 42" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139909544402512" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(i)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "i += 1" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139909544402544" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(i)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139909543772528" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'abc'\n", "id(s)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139909433666288" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s += 'def'\n", "id(s)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sys\n", "sys.getrefcount(s)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "l = [1,2,3]\n", "l.append(l)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, [...]]" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, [...]]" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import copy\n", "copy.deepcopy(l)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "del l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# File I/O" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open for reading ..." ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd', mode='r')" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'root:x:0:0'" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read(10)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "':root:/roo'" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read(10)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'t:/bin/bash\\nbin:x:1:1:bin:/bin:/sbin/nologin\\ndaemon:x:2:2:daemon:/sbin:/sbin/nologin\\nadm:x:3:4:adm:/var/adm:/sbin/nologin\\nlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\\nsync:x:5:0:sync:/sbin:/bin/sync\\nshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\\nhalt:x:7:0:halt:/sbin:/sbin/halt\\nmail:x:8:12:mail:/var/spool/mail:/sbin/nologin\\noperator:x:11:0:operator:/root:/sbin/nologin\\ngames:x:12:100:games:/usr/games:/sbin/nologin\\nftp:x:14:50:FTP User:/var/ftp:/sbin/nologin\\nnobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin\\napache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin\\nsystemd-network:x:192:192:systemd Network Management:/:/sbin/nologin\\nsystemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin\\nsystemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin\\nsystemd-oom:x:998:996:systemd Userspace OOM Killer:/:/sbin/nologin\\nsystemd-timesync:x:997:995:systemd Time Synchronization:/:/sbin/nologin\\ntss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin\\ndbus:x:81:81:System message bus:/:/sbin/nologin\\npolkitd:x:996:994:User for polkitd:/:/sbin/nologin\\navahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin\\nunbound:x:995:992:Unbound DNS resolver:/etc/unbound:/sbin/nologin\\ndnsmasq:x:994:991:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin\\nnm-openconnect:x:993:989:NetworkManager user for OpenConnect:/:/sbin/nologin\\nusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin\\ngluster:x:992:988:GlusterFS daemons:/run/gluster:/sbin/nologin\\nrtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin\\npipewire:x:991:987:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin\\ngeoclue:x:990:986:User for geoclue:/var/lib/geoclue:/sbin/nologin\\nchrony:x:989:984::/var/lib/chrony:/sbin/nologin\\nsaslauth:x:988:76:Saslauthd user:/run/saslauthd:/sbin/nologin\\nradvd:x:75:75:radvd user:/:/sbin/nologin\\nrpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin\\nqemu:x:107:107:qemu user:/:/sbin/nologin\\nopenvpn:x:987:982:OpenVPN:/etc/openvpn:/sbin/nologin\\nnm-openvpn:x:986:981:Default user for running openvpn spawned by NetworkManager:/:/sbin/nologin\\ncolord:x:985:980:User for colord:/var/lib/colord:/sbin/nologin\\nrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin\\nabrt:x:173:173::/etc/abrt:/sbin/nologin\\nflatpak:x:984:979:User for flatpak system helper:/:/sbin/nologin\\ngdm:x:42:42::/var/lib/gdm:/sbin/nologin\\ngnome-initial-setup:x:983:978::/run/gnome-initial-setup/:/sbin/nologin\\nvboxadd:x:982:1::/var/run/vboxadd:/sbin/nologin\\nsshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin\\ntcpdump:x:72:72::/:/sbin/nologin\\njfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash\\nmosquitto:x:981:974:Mosquitto Broker:/etc/mosquitto:/sbin/nologin\\nsomeone-else:x:1001:1001::/home/someone-else:/bin/bash\\n'" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read()" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read() # EOF (end of file)" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd')" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'root:x:0:0:root:/root:/bin/bash\\n'" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.readline()" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'bin:x:1:1:bin:/bin:/sbin/nologin\\n'" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.readline()" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['daemon:x:2:2:daemon:/sbin:/sbin/nologin\\n',\n", " 'adm:x:3:4:adm:/var/adm:/sbin/nologin\\n',\n", " 'lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\\n',\n", " 'sync:x:5:0:sync:/sbin:/bin/sync\\n',\n", " 'shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\\n',\n", " 'halt:x:7:0:halt:/sbin:/sbin/halt\\n',\n", " 'mail:x:8:12:mail:/var/spool/mail:/sbin/nologin\\n',\n", " 'operator:x:11:0:operator:/root:/sbin/nologin\\n',\n", " 'games:x:12:100:games:/usr/games:/sbin/nologin\\n',\n", " 'ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin\\n',\n", " 'nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin\\n',\n", " 'apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin\\n',\n", " 'systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin\\n',\n", " 'systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin\\n',\n", " 'systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin\\n',\n", " 'systemd-oom:x:998:996:systemd Userspace OOM Killer:/:/sbin/nologin\\n',\n", " 'systemd-timesync:x:997:995:systemd Time Synchronization:/:/sbin/nologin\\n',\n", " 'tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin\\n',\n", " 'dbus:x:81:81:System message bus:/:/sbin/nologin\\n',\n", " 'polkitd:x:996:994:User for polkitd:/:/sbin/nologin\\n',\n", " 'avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin\\n',\n", " 'unbound:x:995:992:Unbound DNS resolver:/etc/unbound:/sbin/nologin\\n',\n", " 'dnsmasq:x:994:991:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin\\n',\n", " 'nm-openconnect:x:993:989:NetworkManager user for OpenConnect:/:/sbin/nologin\\n',\n", " 'usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin\\n',\n", " 'gluster:x:992:988:GlusterFS daemons:/run/gluster:/sbin/nologin\\n',\n", " 'rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin\\n',\n", " 'pipewire:x:991:987:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin\\n',\n", " 'geoclue:x:990:986:User for geoclue:/var/lib/geoclue:/sbin/nologin\\n',\n", " 'chrony:x:989:984::/var/lib/chrony:/sbin/nologin\\n',\n", " 'saslauth:x:988:76:Saslauthd user:/run/saslauthd:/sbin/nologin\\n',\n", " 'radvd:x:75:75:radvd user:/:/sbin/nologin\\n',\n", " 'rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin\\n',\n", " 'qemu:x:107:107:qemu user:/:/sbin/nologin\\n',\n", " 'openvpn:x:987:982:OpenVPN:/etc/openvpn:/sbin/nologin\\n',\n", " 'nm-openvpn:x:986:981:Default user for running openvpn spawned by NetworkManager:/:/sbin/nologin\\n',\n", " 'colord:x:985:980:User for colord:/var/lib/colord:/sbin/nologin\\n',\n", " 'rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin\\n',\n", " 'abrt:x:173:173::/etc/abrt:/sbin/nologin\\n',\n", " 'flatpak:x:984:979:User for flatpak system helper:/:/sbin/nologin\\n',\n", " 'gdm:x:42:42::/var/lib/gdm:/sbin/nologin\\n',\n", " 'gnome-initial-setup:x:983:978::/run/gnome-initial-setup/:/sbin/nologin\\n',\n", " 'vboxadd:x:982:1::/var/run/vboxadd:/sbin/nologin\\n',\n", " 'sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin\\n',\n", " 'tcpdump:x:72:72::/:/sbin/nologin\\n',\n", " 'jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash\\n',\n", " 'mosquitto:x:981:974:Mosquitto Broker:/etc/mosquitto:/sbin/nologin\\n',\n", " 'someone-else:x:1001:1001::/home/someone-else:/bin/bash\\n']" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.readlines()" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd')" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "root:x:0:0:root:/root:/bin/bash\n", "\n", "bin:x:1:1:bin:/bin:/sbin/nologin\n", "\n", "daemon:x:2:2:daemon:/sbin:/sbin/nologin\n", "\n", "adm:x:3:4:adm:/var/adm:/sbin/nologin\n", "\n", "lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n", "\n", "sync:x:5:0:sync:/sbin:/bin/sync\n", "\n", "shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\n", "\n", "halt:x:7:0:halt:/sbin:/sbin/halt\n", "\n", "mail:x:8:12:mail:/var/spool/mail:/sbin/nologin\n", "\n", "operator:x:11:0:operator:/root:/sbin/nologin\n", "\n", "games:x:12:100:games:/usr/games:/sbin/nologin\n", "\n", "ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin\n", "\n", "nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin\n", "\n", "apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin\n", "\n", "systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin\n", "\n", "systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin\n", "\n", "systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin\n", "\n", "systemd-oom:x:998:996:systemd Userspace OOM Killer:/:/sbin/nologin\n", "\n", "systemd-timesync:x:997:995:systemd Time Synchronization:/:/sbin/nologin\n", "\n", "tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin\n", "\n", "dbus:x:81:81:System message bus:/:/sbin/nologin\n", "\n", "polkitd:x:996:994:User for polkitd:/:/sbin/nologin\n", "\n", "avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin\n", "\n", "unbound:x:995:992:Unbound DNS resolver:/etc/unbound:/sbin/nologin\n", "\n", "dnsmasq:x:994:991:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin\n", "\n", "nm-openconnect:x:993:989:NetworkManager user for OpenConnect:/:/sbin/nologin\n", "\n", "usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin\n", "\n", "gluster:x:992:988:GlusterFS daemons:/run/gluster:/sbin/nologin\n", "\n", "rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin\n", "\n", "pipewire:x:991:987:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin\n", "\n", "geoclue:x:990:986:User for geoclue:/var/lib/geoclue:/sbin/nologin\n", "\n", "chrony:x:989:984::/var/lib/chrony:/sbin/nologin\n", "\n", "saslauth:x:988:76:Saslauthd user:/run/saslauthd:/sbin/nologin\n", "\n", "radvd:x:75:75:radvd user:/:/sbin/nologin\n", "\n", "rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin\n", "\n", "qemu:x:107:107:qemu user:/:/sbin/nologin\n", "\n", "openvpn:x:987:982:OpenVPN:/etc/openvpn:/sbin/nologin\n", "\n", "nm-openvpn:x:986:981:Default user for running openvpn spawned by NetworkManager:/:/sbin/nologin\n", "\n", "colord:x:985:980:User for colord:/var/lib/colord:/sbin/nologin\n", "\n", "rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin\n", "\n", "abrt:x:173:173::/etc/abrt:/sbin/nologin\n", "\n", "flatpak:x:984:979:User for flatpak system helper:/:/sbin/nologin\n", "\n", "gdm:x:42:42::/var/lib/gdm:/sbin/nologin\n", "\n", "gnome-initial-setup:x:983:978::/run/gnome-initial-setup/:/sbin/nologin\n", "\n", "vboxadd:x:982:1::/var/run/vboxadd:/sbin/nologin\n", "\n", "sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin\n", "\n", "tcpdump:x:72:72::/:/sbin/nologin\n", "\n", "jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash\n", "\n", "mosquitto:x:981:974:Mosquitto Broker:/etc/mosquitto:/sbin/nologin\n", "\n", "someone-else:x:1001:1001::/home/someone-else:/bin/bash\n", "\n" ] } ], "source": [ "for line in f:\n", " print(line)" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " [Errno 13] Permission denied: '/etc/passwd'\n" ] } ], "source": [ "try:\n", " f = open('/etc/passwd', 'w')\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Encoding" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [], "source": [ "s = 'abc'\n", "bytes = s.encode('ascii')" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(bytes)" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "97" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bytes[0]" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "98" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bytes[1]" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'ascii' codec can't encode character '\\xf6' in position 1: ordinal not in range(128)\n" ] } ], "source": [ "s = 'Jörg'\n", "try:\n", " s.encode('ascii')\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bytes = s.encode('iso-8859-1')\n", "len(bytes)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "246" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bytes[1]" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Jörg'" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [], "source": [ "joerg_latin = s.encode('iso-8859-1')" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bytes" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(joerg_latin)" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(bytes)" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'J\\xf6rg'" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joerg_latin" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Jіrg'" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joerg_latin.decode('iso-8859-5')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ``enumerate()``" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Joerg\n", "Caro\n", "Philipp\n", "Johanna\n" ] } ], "source": [ "names = ['Joerg', 'Caro', 'Philipp', 'Johanna']\n", "for n in names:\n", " print(n)" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 'Joerg')\n", "(1, 'Caro')\n", "(2, 'Philipp')\n", "(3, 'Johanna')\n" ] } ], "source": [ "for element in enumerate(names):\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Joerg\n", "1 Caro\n", "2 Philipp\n", "3 Johanna\n" ] } ], "source": [ "for lineno, n in enumerate(names):\n", " print(lineno, n)" ] } ], "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.12" } }, "nbformat": 4, "nbformat_minor": 4 }