{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Basics (2023-12-18 - 2023-12-20)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "2\n", "1\n", "3\n", "2\n", "4\n", "3\n", "5\n", "4\n", "6\n", "habe fertig\n" ] } ], "source": [ "for i in range(5):\n", " print(i)\n", " print(i+2)\n", "print('habe fertig')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "was jetzt geschehen wird" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "j = 42" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "j" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(j)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " name 'a' is not defined\n" ] } ], "source": [ "try:\n", " a\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(j)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "j = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(j)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "j = 42.45" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(j)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "translation_table = {\n", " 'one': 1,\n", " 'two': 2,\n", "}" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "translation_table['one']" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**1000" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nein\n" ] } ], "source": [ "if 1 == '1':\n", " print('ja')\n", "else:\n", " print('nein')" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def max(a, b):\n", " '''\n", " das ist eine hochkomplexe blah\n", " blah\n", " bla\n", "\n", " '''\n", " return a + b" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(1, 2)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\n das ist eine hochkomplexe blah\\n blah\\n bla\\n\\n '" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max.__doc__" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function max in module __main__:\n", "\n", "max(a, b)\n", " das ist eine hochkomplexe blah\n", " blah\n", " bla\n", "\n" ] } ], "source": [ "help(max)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(max)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(j)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "j = max" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(j)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "j(1, 2)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "import sys" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['/home/jfasch/venv/jfasch-home/lib64/python3.10/site-packages/ipykernel_launcher.py',\n", " '-f',\n", " '/home/jfasch/.local/share/jupyter/runtime/kernel-98f8b9be-ad8f-4023-81a2-d1dc67d0282c.json']" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.argv" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'linux'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.platform" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['/home/jfasch/work/jfasch-home/trainings/log/detail/2023-12-18--Python-Basics',\n", " '/usr/lib64/python310.zip',\n", " '/usr/lib64/python3.10',\n", " '/usr/lib64/python3.10/lib-dynload',\n", " '',\n", " '/home/jfasch/venv/jfasch-home/lib64/python3.10/site-packages',\n", " '/home/jfasch/venv/jfasch-home/lib/python3.10/site-packages',\n", " '/home/jfasch/venv/jfasch-home/lib64/python3.10/site-packages/IPython/extensions',\n", " '/home/jfasch/.ipython']" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.path" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "module" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(sys)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "sys = 666" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'int' object has no attribute 'argv'\n" ] } ], "source": [ "try:\n", " sys.argv\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variables" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "a = 1.5" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "a = ['blah', 1.5]" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "a, b, c = 1, 'eins', 1.0" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'eins'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "a, b = 1, 2" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "tmp = a" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "a = b" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "b = tmp" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "a, b = 1, 2" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "a, b = b, a" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "(a, b) = (b, a)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139783143835152" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0x7f21cca24610'" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(id(a))" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139783143835152" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a) == id(b)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a is b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Integers" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [], "source": [ "i = 42" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "i = -42" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [], "source": [ "i = 2**64-1" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b1111111111111111111111111111111111111111111111111111111111111111'" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(i)" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "66" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(bin(i))" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [], "source": [ "i += 1" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "67" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(bin(i))" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b10000000000000000000000000000000000000000000000000000000000000000'" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(i)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [], "source": [ "i += 2**100" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b10000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000'" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(i)" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1000000" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1_000_000" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3/2" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3//2" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3//2" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3%2" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "16/4" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "16%4" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "17%4" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**3" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(3**2)-1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Datatype Conversion" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "i = 42" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [], "source": [ "j = 42.666" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "i = j" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42.666" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "i = 42" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [], "source": [ "s = 'abc'" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(s)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [], "source": [ "s = str(i)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'42'" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [], "source": [ "j = int(s)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "j" ] }, { "cell_type": "code", "execution_count": 94, "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": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2748" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('abc', 16)" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " invalid literal for int() with base 10: '42.666'\n" ] } ], "source": [ "try:\n", " int('42.666')\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42.666" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float('42.666')" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [], "source": [ "s = 'abc'" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "jo\n" ] } ], "source": [ "if type(s) is str:\n", " print('jo')" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [], "source": [ "d = {\n", " 'one': 1,\n", " 'two': 2,\n", " 'three': 3,\n", "}" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"{'one': 1, 'two': 2, 'three': 3}\"" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(d)" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3267" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('12345', 7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compound Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [], "source": [ "l.append('vier')" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier']" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [], "source": [ "l.append(5.0)" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0]" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [], "source": [ "l.append([6, 7, 'acht'])" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0, [6, 7, 'acht']]" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [], "source": [ "l.append(l)" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0, [6, 7, 'acht'], [...]]" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[0]" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0, [6, 7, 'acht'], [...]]" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[6]" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0, [6, 7, 'acht'], [...]]" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[-1]" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[6, 7, 'acht']" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[-2]" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[4]" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[-3]" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [], "source": [ "del l[-1]" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0, [6, 7, 'acht']]" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [], "source": [ "l.extend([10, 20, 30])" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0, [6, 7, 'acht'], 10, 20, 30]" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " '<' not supported between instances of 'str' and 'int'\n" ] } ], "source": [ "try:\n", " l.sort()\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0, [6, 7, 'acht'], 10, 20, 30]" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [], "source": [ "l1 = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [], "source": [ "l1.extend([4, 5, 6])" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6]" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [], "source": [ "l1.extend(l1)" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [], "source": [ "l1.sort()" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [], "source": [ "l1 = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [], "source": [ "l2 = [3, 4, [5, 6, 7]]" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [], "source": [ "l1.extend(l2)" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 3, 4, [5, 6, 7]]" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 6, 7]" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2[-1]" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [], "source": [ "l2[-1].append(666)" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 4, [5, 6, 7, 666]]" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 3, 4, [5, 6, 7, 666]]" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [], "source": [ "l = [3, 1, 2, 4, 5, 88, 42]" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 in l" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in l" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "666 in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuple" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139782834179584" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l)" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [], "source": [ "l.append(4)" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139782834179584" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l)" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [], "source": [ "i = 42" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139783143835152" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(i)" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [], "source": [ "i += 1" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "43" ] }, "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139783143835184" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(i)" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [], "source": [ "t = (1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'tuple' object has no attribute 'append'\n" ] } ], "source": [ "try:\n", " t.append(4)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 159, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[1]" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'tuple' object does not support item assignment\n" ] } ], "source": [ "try:\n", " t[1] += 5\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [], "source": [ "t = (1, 2, [3, 4])" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 4]" ] }, "execution_count": 162, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[2]" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [], "source": [ "t[2].append(666)" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, [3, 4, 666])" ] }, "execution_count": 164, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'tuple' object does not support item assignment\n" ] } ], "source": [ "try:\n", " t[0] = 7\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [], "source": [ "d = {\n", " 'one': 1,\n", " 'two': 2,\n", "}" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['one']" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'three'\n" ] } ], "source": [ "try:\n", " d['three']\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [], "source": [ "d['three'] = 3" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'three' in d" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [], "source": [ "d1 = {\n", " 'three': 33,\n", " 'four': 'vier'\n", "}" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [], "source": [ "d.update(d1)" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 33, 'four': 'vier'}" ] }, "execution_count": 173, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [], "source": [ "d = {}" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 175, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(d)" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [], "source": [ "d = dict()" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 177, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [], "source": [ "menge = {1, 2, 3}" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [], "source": [ "menge.add(4)" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [], "source": [ "menge.add('fuenf')" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 'fuenf'}" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "menge" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [], "source": [ "menge.remove(2)" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 3, 4, 'fuenf'}" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "menge" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 in menge" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [], "source": [ "menge.add(5)" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 3, 4, 5, 'fuenf'}" ] }, "execution_count": 186, "metadata": {}, "output_type": "execute_result" } ], "source": [ "menge" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [], "source": [ "menge.remove(5)" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 3, 4, 'fuenf'}" ] }, "execution_count": 188, "metadata": {}, "output_type": "execute_result" } ], "source": [ "menge" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [], "source": [ "m1 = {1, 2, 3, 4}" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [], "source": [ "m2 = {3, 4, 5, 6}" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5, 6}" ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m1 | m2" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{3, 4}" ] }, "execution_count": 192, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m1 & m2" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2}" ] }, "execution_count": 193, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m1 - m2" ] }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{5, 6}" ] }, "execution_count": 194, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m2 - m1" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "set" ] }, "execution_count": 195, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(m2)" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 196, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 in m1" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [], "source": [ "leeres_set = set()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Boolean" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [], "source": [ "b = True" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 199, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(b)" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [], "source": [ "i = 666" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 201, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(i)" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 202, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 203, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool('')" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 204, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = []\n", "len(l)" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 205, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(l)" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [], "source": [ "l = None" ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "NoneType" ] }, "execution_count": 207, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(l)" ] }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [], "source": [ "l = []" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "na\n" ] } ], "source": [ "if l:\n", " print('jo')\n", "else:\n", " print('na')" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "na\n" ] } ], "source": [ "if len(l):\n", " print('jo')\n", "else:\n", " print('na')" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "jo\n" ] } ], "source": [ "if len(l) == 0:\n", " print('jo')\n", "else:\n", " print('na')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Weirdness (Comprehensions)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### List Comprehension" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [], "source": [ "numbers = [0, 1, 2, 3, 4, 5]" ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [], "source": [ "squares = []\n", "for n in numbers:\n", " squares.append(n**2)" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 25]" ] }, "execution_count": 214, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squares" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 25]" ] }, "execution_count": 215, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[n**2 for n in numbers]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dictionary Comprehension (Exchange key and Value of dict)" ] }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [], "source": [ "d = {'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9}" ] }, { "cell_type": "code", "execution_count": 217, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for k in d.keys():\n", " print(d[k])" ] }, { "cell_type": "code", "execution_count": 218, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for v in d.values():\n", " print(v)" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('zero', 0)\n", "('one', 1)\n", "('two', 2)\n", "('three', 3)\n", "('four', 4)\n", "('five', 5)\n", "('six', 6)\n", "('seven', 7)\n", "('eight', 8)\n", "('nine', 9)\n" ] } ], "source": [ "for item in d.items():\n", " print(item)" ] }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "zero 0\n", "one 1\n", "two 2\n", "three 3\n", "four 4\n", "five 5\n", "six 6\n", "seven 7\n", "eight 8\n", "nine 9\n" ] } ], "source": [ "for item in d.items():\n", " k = item[0]\n", " v = item[1]\n", " print(k, v)" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "zero 0\n", "one 1\n", "two 2\n", "three 3\n", "four 4\n", "five 5\n", "six 6\n", "seven 7\n", "eight 8\n", "nine 9\n" ] } ], "source": [ "for k, v in d.items():\n", " print(k, v)" ] }, { "cell_type": "code", "execution_count": 222, "metadata": {}, "outputs": [], "source": [ "d_reversed = {}" ] }, { "cell_type": "code", "execution_count": 223, "metadata": {}, "outputs": [], "source": [ "for k, v in d.items():\n", " d_reversed[v] = k" ] }, { "cell_type": "code", "execution_count": 224, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 'zero',\n", " 1: 'one',\n", " 2: 'two',\n", " 3: 'three',\n", " 4: 'four',\n", " 5: 'five',\n", " 6: 'six',\n", " 7: 'seven',\n", " 8: 'eight',\n", " 9: 'nine'}" ] }, "execution_count": 224, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d_reversed" ] }, { "cell_type": "code", "execution_count": 225, "metadata": {}, "outputs": [], "source": [ "d_reversed = {v:k for k, v in d.items()}" ] }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 'zero',\n", " 1: 'one',\n", " 2: 'two',\n", " 3: 'three',\n", " 4: 'four',\n", " 5: 'five',\n", " 6: 'six',\n", " 7: 'seven',\n", " 8: 'eight',\n", " 9: 'nine'}" ] }, "execution_count": 226, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d_reversed" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Functional Programming" ] }, { "cell_type": "code", "execution_count": 227, "metadata": {}, "outputs": [], "source": [ "l = [0, 1, 2, 3, 4, 5]" ] }, { "cell_type": "code", "execution_count": 228, "metadata": {}, "outputs": [], "source": [ "def square(n):\n", " return n**2" ] }, { "cell_type": "code", "execution_count": 229, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n", "25\n" ] } ], "source": [ "for s in map(square, l):\n", " print(s)" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n", "25\n" ] } ], "source": [ "for s in map(lambda n: n**2, l):\n", " print(s)" ] }, { "cell_type": "code", "execution_count": 231, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "2\n", "4\n" ] } ], "source": [ "for s in filter(lambda n: n%2==0, l):\n", " print(s)" ] }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'zero': 0,\n", " 'one': 1,\n", " 'two': 2,\n", " 'three': 3,\n", " 'four': 4,\n", " 'five': 5,\n", " 'six': 6,\n", " 'seven': 7,\n", " 'eight': 8,\n", " 'nine': 9}" ] }, "execution_count": 232, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [], "source": [ "l1 = [1, 2, 3]\n", "l2 = ['one', 'two', 'three']" ] }, { "cell_type": "code", "execution_count": 234, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 'one')\n", "(2, 'two')\n", "(3, 'three')\n" ] } ], "source": [ "for elem in zip(l1, l2):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('zero', 0)\n", "('one', 1)\n", "('two', 2)\n", "('three', 3)\n", "('four', 4)\n", "('five', 5)\n", "('six', 6)\n", "('seven', 7)\n", "('eight', 8)\n", "('nine', 9)\n" ] } ], "source": [ "for elem in zip(d.keys(), d.values()):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 236, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 'zero')\n", "(1, 'one')\n", "(2, 'two')\n", "(3, 'three')\n", "(4, 'four')\n", "(5, 'five')\n", "(6, 'six')\n", "(7, 'seven')\n", "(8, 'eight')\n", "(9, 'nine')\n" ] } ], "source": [ "for elem in zip(d.values(), d.keys()):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 237, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 'zero',\n", " 1: 'one',\n", " 2: 'two',\n", " 3: 'three',\n", " 4: 'four',\n", " 5: 'five',\n", " 6: 'six',\n", " 7: 'seven',\n", " 8: 'eight',\n", " 9: 'nine'}" ] }, "execution_count": 237, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{v: k for v, k in zip(d.values(), d.keys())}" ] }, { "cell_type": "code", "execution_count": 238, "metadata": {}, "outputs": [], "source": [ "l = [(1, 'one'), (2, 'two')]" ] }, { "cell_type": "code", "execution_count": 239, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'one', 2: 'two'}" ] }, "execution_count": 239, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict(l)" ] }, { "cell_type": "code", "execution_count": 240, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 'zero',\n", " 1: 'one',\n", " 2: 'two',\n", " 3: 'three',\n", " 4: 'four',\n", " 5: 'five',\n", " 6: 'six',\n", " 7: 'seven',\n", " 8: 'eight',\n", " 9: 'nine'}" ] }, "execution_count": 240, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict(zip(d.values(), d.keys()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### ``set()``, ``list()``, ``dict()``" ] }, { "cell_type": "code", "execution_count": 241, "metadata": {}, "outputs": [], "source": [ "l = [2, 4, 10, 9, 1]" ] }, { "cell_type": "code", "execution_count": 242, "metadata": {}, "outputs": [], "source": [ "s = set(l)" ] }, { "cell_type": "code", "execution_count": 243, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 4, 9, 10}" ] }, "execution_count": 243, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 244, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 4, 9, 10]" ] }, "execution_count": 244, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(s)" ] }, { "cell_type": "code", "execution_count": 245, "metadata": {}, "outputs": [], "source": [ "s1 = {2, 3, 4}\n", "s2 = {4, 5, 6}" ] }, { "cell_type": "code", "execution_count": 246, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2, 3}" ] }, "execution_count": 246, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 - s2" ] }, { "cell_type": "code", "execution_count": 247, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2, 3, 4, 5, 6}" ] }, "execution_count": 247, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set(s1|s2)" ] }, { "cell_type": "code", "execution_count": 248, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2, 3}" ] }, "execution_count": 248, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set(s1 - s2)" ] }, { "cell_type": "code", "execution_count": 249, "metadata": {}, "outputs": [], "source": [ "diff = s1 - s2" ] }, { "cell_type": "code", "execution_count": 250, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2, 3}" ] }, "execution_count": 250, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff" ] }, { "cell_type": "code", "execution_count": 251, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2, 3}" ] }, "execution_count": 251, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set(diff)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``while`` (and ``else``)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Summe von 1..100 (beide inklusive)" ] }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5050\n" ] } ], "source": [ "summe = 0\n", "n = 1\n", "while n <= 100:\n", " summe += n\n", " n += 1\n", "print(summe)" ] }, { "cell_type": "code", "execution_count": 253, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5050" ] }, "execution_count": 253, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(range(1, 101)) # <--- pythonic" ] }, { "cell_type": "code", "execution_count": 254, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5050" ] }, "execution_count": 254, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(100*101/2)" ] }, { "cell_type": "code", "execution_count": 255, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 256, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "lose 4\n", "yaay\n" ] } ], "source": [ "while True:\n", " eyes = random.randrange(1, 7)\n", " if eyes == 6:\n", " print('yaay')\n", " break\n", " else:\n", " print('lose', eyes)" ] }, { "cell_type": "code", "execution_count": 257, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "lose in versuch 0 , 1\n", "lose in versuch 1 , 3\n", "lose in versuch 2 , 1\n", "lose in versuch 3 , 1\n", "lose in versuch 4 , 5\n", "lose in versuch 5 , 3\n", "lose in versuch 6 , 4\n", "lose in versuch 7 , 3\n", "kompletter lose\n" ] } ], "source": [ "n_tries = 0\n", "while n_tries < 8:\n", " eyes = random.randrange(1, 7)\n", " if eyes == 6:\n", " print('yaay')\n", " break\n", " else:\n", " print('lose in versuch', n_tries, ',', eyes)\n", " n_tries += 1\n", "else:\n", " print('kompletter lose')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``range()``" ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "l = [0, 1, 2, 3, 4]\n", "for elem in l:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 259, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo\n", "hallo\n", "hallo\n", "hallo\n", "hallo\n" ] } ], "source": [ "for elem in l:\n", " print('hallo')" ] }, { "cell_type": "code", "execution_count": 260, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for elem in range(5):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 261, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for elem in range(2, 10):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 262, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "4\n", "6\n", "8\n" ] } ], "source": [ "for elem in range(2, 10, 2):\n", " print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``range()``, And Generators, The Iterator Protocol" ] }, { "cell_type": "code", "execution_count": 263, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "l = [0, 1, 2, 3, 4]\n", "for elem in l:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 264, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(l))" ] }, { "cell_type": "code", "execution_count": 265, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "r = range(5)\n", "for elem in r:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 266, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 5)" ] }, "execution_count": 266, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r" ] }, { "cell_type": "code", "execution_count": 267, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(r))" ] }, { "cell_type": "code", "execution_count": 268, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for elem in r:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 269, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for elem in l:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 270, "metadata": {}, "outputs": [], "source": [ "it = iter(l)" ] }, { "cell_type": "code", "execution_count": 271, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(it))" ] }, { "cell_type": "code", "execution_count": 272, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 272, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 273, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 273, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 274, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 274, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 275, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 275, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 276, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 276, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 277, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "try:\n", " next(it)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 278, "metadata": {}, "outputs": [], "source": [ "r = range(3)" ] }, { "cell_type": "code", "execution_count": 279, "metadata": {}, "outputs": [], "source": [ "it = iter(r)" ] }, { "cell_type": "code", "execution_count": 280, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 280, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 281, "metadata": {}, "outputs": [], "source": [ "def foo(bar):\n", " return bar**2" ] }, { "cell_type": "code", "execution_count": 282, "metadata": {}, "outputs": [], "source": [ "def my_deppertes_range():\n", " print('start')\n", " yield 0\n", " print('yield 1')\n", " yield 1\n", " print('yield 2')\n", " yield 2\n", " print('end')" ] }, { "cell_type": "code", "execution_count": 283, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "start\n", "got 0\n", "yield 1\n", "got 1\n", "yield 2\n", "got 2\n", "end\n" ] } ], "source": [ "mdr = my_deppertes_range()\n", "for elem in mdr:\n", " print('got', elem)" ] }, { "cell_type": "code", "execution_count": 284, "metadata": {}, "outputs": [], "source": [ "mdr = my_deppertes_range()" ] }, { "cell_type": "code", "execution_count": 285, "metadata": {}, "outputs": [], "source": [ "it = iter(mdr)" ] }, { "cell_type": "code", "execution_count": 286, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "start\n" ] }, { "data": { "text/plain": [ "0" ] }, "execution_count": 286, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 287, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yield 1\n" ] }, { "data": { "text/plain": [ "1" ] }, "execution_count": 287, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 288, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yield 2\n" ] }, { "data": { "text/plain": [ "2" ] }, "execution_count": 288, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 289, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "end\n", " \n" ] } ], "source": [ "try:\n", " next(it)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 290, "metadata": {}, "outputs": [], "source": [ "numbers = range(20)" ] }, { "cell_type": "code", "execution_count": 291, "metadata": {}, "outputs": [], "source": [ "def even(seq):\n", " for n in seq:\n", " if n%2 == 0:\n", " yield n" ] }, { "cell_type": "code", "execution_count": 292, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "2\n", "4\n", "6\n", "8\n", "10\n", "12\n", "14\n", "16\n", "18\n" ] } ], "source": [ "for elem in even(numbers):\n", " print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions" ] }, { "cell_type": "code", "execution_count": 293, "metadata": {}, "outputs": [], "source": [ "def maximum(a, b):\n", " '''\n", " blah\n", " \n", " * a: links\n", " * b: rechts\n", " '''\n", " \n", " if a < b:\n", " return b\n", " return a" ] }, { "cell_type": "code", "execution_count": 294, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 294, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(1, 2)" ] }, { "cell_type": "code", "execution_count": 295, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'bbb'" ] }, "execution_count": 295, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum('aaa', 'bbb')" ] }, { "cell_type": "code", "execution_count": 296, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "i = 666\n", "print(type(i))" ] }, { "cell_type": "code", "execution_count": 297, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(maximum))" ] }, { "cell_type": "code", "execution_count": 298, "metadata": {}, "outputs": [], "source": [ "i = maximum" ] }, { "cell_type": "code", "execution_count": 299, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 299, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i(1, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Return Value? Parameters?" ] }, { "cell_type": "code", "execution_count": 300, "metadata": {}, "outputs": [], "source": [ "def foo():\n", " return True" ] }, { "cell_type": "code", "execution_count": 301, "metadata": {}, "outputs": [], "source": [ "x = foo()" ] }, { "cell_type": "code", "execution_count": 302, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 302, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 303, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ja\n" ] } ], "source": [ "if foo():\n", " print('ja')\n", "else:\n", " print('nein')" ] }, { "cell_type": "code", "execution_count": 304, "metadata": {}, "outputs": [], "source": [ "def foo():\n", " return False" ] }, { "cell_type": "code", "execution_count": 305, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nein\n" ] } ], "source": [ "if foo():\n", " print('ja')\n", "else:\n", " print('nein')" ] }, { "cell_type": "code", "execution_count": 306, "metadata": {}, "outputs": [], "source": [ "def is_even(number):\n", " if number % 2 == 0:\n", " return True\n", " else:\n", " return False" ] }, { "cell_type": "code", "execution_count": 307, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nein\n" ] } ], "source": [ "if is_even(9):\n", " print('ja')\n", "else:\n", " print('nein')" ] }, { "cell_type": "code", "execution_count": 308, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 308, "metadata": {}, "output_type": "execute_result" } ], "source": [ "is_even(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More About Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### References, Immutability" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Integer objeckte sind Singletons bis 256 (?)" ] }, { "cell_type": "code", "execution_count": 309, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 310, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139783143835152" ] }, "execution_count": 310, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 311, "metadata": {}, "outputs": [], "source": [ "b = 42" ] }, { "cell_type": "code", "execution_count": 312, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139783143835152" ] }, "execution_count": 312, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 313, "metadata": {}, "outputs": [], "source": [ "l1 = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 314, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139782799029888" ] }, "execution_count": 314, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1)" ] }, { "cell_type": "code", "execution_count": 315, "metadata": {}, "outputs": [], "source": [ "l2 = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 316, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139782798781696" ] }, "execution_count": 316, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l2)" ] }, { "cell_type": "code", "execution_count": 317, "metadata": {}, "outputs": [], "source": [ "a = 666" ] }, { "cell_type": "code", "execution_count": 318, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139782798895952" ] }, "execution_count": 318, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 319, "metadata": {}, "outputs": [], "source": [ "b = 666" ] }, { "cell_type": "code", "execution_count": 320, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139782798895792" ] }, "execution_count": 320, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 321, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 322, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139783143835152" ] }, "execution_count": 322, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 323, "metadata": {}, "outputs": [], "source": [ "a += 1" ] }, { "cell_type": "code", "execution_count": 324, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139783143835184" ] }, "execution_count": 324, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 325, "metadata": {}, "outputs": [], "source": [ "def foo(n):\n", " n += 1" ] }, { "cell_type": "code", "execution_count": 326, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "43" ] }, "execution_count": 326, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 327, "metadata": {}, "outputs": [], "source": [ "foo(a)" ] }, { "cell_type": "code", "execution_count": 328, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "43" ] }, "execution_count": 328, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 329, "metadata": {}, "outputs": [], "source": [ "l = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 330, "metadata": {}, "outputs": [], "source": [ "def foo(seq):\n", " seq.append(666)" ] }, { "cell_type": "code", "execution_count": 331, "metadata": {}, "outputs": [], "source": [ "foo(l)" ] }, { "cell_type": "code", "execution_count": 332, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 666]" ] }, "execution_count": 332, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 333, "metadata": {}, "outputs": [], "source": [ "t = (1,2,3)" ] }, { "cell_type": "code", "execution_count": 334, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'tuple' object has no attribute 'append'\n" ] } ], "source": [ "try:\n", " foo(t)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Und Schuetzen?" ] }, { "cell_type": "code", "execution_count": 335, "metadata": {}, "outputs": [], "source": [ "l1 = [1, 2, 3]\n", "def avg(seq):\n", " rv = sum(seq)/len(seq)\n", " seq.append(666)\n", " return rv" ] }, { "cell_type": "code", "execution_count": 336, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 336, "metadata": {}, "output_type": "execute_result" } ], "source": [ "avg(l1)" ] }, { "cell_type": "code", "execution_count": 337, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 666]" ] }, "execution_count": 337, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 338, "metadata": {}, "outputs": [], "source": [ "l1 = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 339, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139782798980096" ] }, "execution_count": 339, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1)" ] }, { "cell_type": "code", "execution_count": 340, "metadata": {}, "outputs": [], "source": [ "l1_copy = l1[:]" ] }, { "cell_type": "code", "execution_count": 341, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 341, "metadata": {}, "output_type": "execute_result" } ], "source": [ "avg(l1_copy)" ] }, { "cell_type": "code", "execution_count": 342, "metadata": {}, "outputs": [], "source": [ "l1 = [1, 2, ['abc', 'def'], 3, 4]" ] }, { "cell_type": "code", "execution_count": 343, "metadata": {}, "outputs": [], "source": [ "l2 = l1[:]" ] }, { "cell_type": "code", "execution_count": 344, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 344, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 is l2" ] }, { "cell_type": "code", "execution_count": 345, "metadata": {}, "outputs": [], "source": [ "l2.append(666)" ] }, { "cell_type": "code", "execution_count": 346, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['abc', 'def'], 3, 4]" ] }, "execution_count": 346, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 347, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['abc', 'def'], 3, 4, 666]" ] }, "execution_count": 347, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 348, "metadata": {}, "outputs": [], "source": [ "l2[2].append('ghi')" ] }, { "cell_type": "code", "execution_count": 349, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['abc', 'def', 'ghi'], 3, 4, 666]" ] }, "execution_count": 349, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 350, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['abc', 'def', 'ghi'], 3, 4]" ] }, "execution_count": 350, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 351, "metadata": {}, "outputs": [], "source": [ "t1 = (1, 2, ['abc', 'def'], 3, 4)" ] }, { "cell_type": "code", "execution_count": 352, "metadata": {}, "outputs": [], "source": [ "t2 = t1[:4]" ] }, { "cell_type": "code", "execution_count": 353, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 353, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t1 is t2" ] }, { "cell_type": "code", "execution_count": 354, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 354, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t1[2] is t2[2]" ] }, { "cell_type": "code", "execution_count": 355, "metadata": {}, "outputs": [], "source": [ "t2[2].append('teifl')" ] }, { "cell_type": "code", "execution_count": 356, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, ['abc', 'def', 'teifl'], 3, 4)" ] }, "execution_count": 356, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More About Strings" ] }, { "cell_type": "code", "execution_count": 357, "metadata": {}, "outputs": [], "source": [ "s = 'blah'" ] }, { "cell_type": "code", "execution_count": 358, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'blah'" ] }, "execution_count": 358, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 359, "metadata": {}, "outputs": [], "source": [ "s = \"blah\"" ] }, { "cell_type": "code", "execution_count": 360, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'blah'" ] }, "execution_count": 360, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 361, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Es ist Joerg's Schuld\"" ] }, "execution_count": 361, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Es ist Joerg\\'s Schuld'" ] }, { "cell_type": "code", "execution_count": 362, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Es ist Joerg's Schuld\"" ] }, "execution_count": 362, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Es ist Joerg's Schuld\"" ] }, { "cell_type": "code", "execution_count": 363, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'erste zeile\\nzweite zeile'" ] }, "execution_count": 363, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"erste zeile\\nzweite zeile\"" ] }, { "cell_type": "code", "execution_count": 364, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "erste zeile\n", "zweite zeile\n" ] } ], "source": [ "print(\"erste zeile\\nzweite zeile\")" ] }, { "cell_type": "code", "execution_count": 365, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\tb\n" ] } ], "source": [ "print(\"a\\tb\")" ] }, { "cell_type": "code", "execution_count": 366, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\\b\n" ] } ], "source": [ "print(\"a\\\\b\")" ] }, { "cell_type": "code", "execution_count": 367, "metadata": {}, "outputs": [], "source": [ "dos_path = \"C:\\Programs\\Meine Programme\\neues programm\"" ] }, { "cell_type": "code", "execution_count": 368, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\Programs\\Meine Programme\n", "eues programm\n" ] } ], "source": [ "print(dos_path)" ] }, { "cell_type": "code", "execution_count": 369, "metadata": {}, "outputs": [], "source": [ "dos_path = \"C:\\Programs\\Meine Programme\\\\neues programm\"" ] }, { "cell_type": "code", "execution_count": 370, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\Programs\\Meine Programme\\neues programm\n" ] } ], "source": [ "print(dos_path)" ] }, { "cell_type": "code", "execution_count": 371, "metadata": {}, "outputs": [], "source": [ "dos_path = r\"C:\\Programs\\Meine Programme\\neues programm\"" ] }, { "cell_type": "code", "execution_count": 372, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\Programs\\Meine Programme\\neues programm\n" ] } ], "source": [ "print(dos_path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Formatting" ] }, { "cell_type": "code", "execution_count": 373, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'blah 666 blah'" ] }, "execution_count": 373, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"blah %d blah\" % 666" ] }, { "cell_type": "code", "execution_count": 374, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'blah 666 blah 42.300000'" ] }, "execution_count": 374, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"blah %d blah %.6f\" % (666, 42.3)" ] }, { "cell_type": "code", "execution_count": 375, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'blah 666 blah 42.3 blah 666'" ] }, "execution_count": 375, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'blah {0} blah {1} blah {0}'.format(666, 42.3)" ] }, { "cell_type": "code", "execution_count": 376, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'blah 42.3 blah 666 blah 42.3'" ] }, "execution_count": 376, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'blah {a} blah {b} blah {a}'.format(b=666, a=42.3)" ] }, { "cell_type": "code", "execution_count": 377, "metadata": {}, "outputs": [], "source": [ "a, b = 42.3, 666" ] }, { "cell_type": "code", "execution_count": 378, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'blah 42.3 blah 666 42.3'" ] }, "execution_count": 378, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f'blah {a} blah {b} {a}'" ] }, { "cell_type": "code", "execution_count": 379, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'blah 43.3'" ] }, "execution_count": 379, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f'blah {a+1}'" ] }, { "cell_type": "code", "execution_count": 380, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'blah [0, 1, 4, 9, 16]'" ] }, "execution_count": 380, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f'blah {[i**2 for i in range(5)]}'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Regular Expressions" ] }, { "cell_type": "code", "execution_count": 381, "metadata": {}, "outputs": [], "source": [ "line = ' 666 ; Joerg ;Faschingbauer '" ] }, { "cell_type": "code", "execution_count": 382, "metadata": {}, "outputs": [], "source": [ "pattern = r'^\\s*(\\d+)\\s*;\\s*([A-Za-z]+)\\s*;\\s*([A-Za-z]+)\\s*$'" ] }, { "cell_type": "code", "execution_count": 383, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "^\\s*(\\d+)\\s*;\\s*([A-Za-z]+)\\s*;\\s*([A-Za-z]+)\\s*$\n" ] } ], "source": [ "print(pattern)" ] }, { "cell_type": "code", "execution_count": 384, "metadata": {}, "outputs": [], "source": [ "import re" ] }, { "cell_type": "code", "execution_count": 385, "metadata": {}, "outputs": [], "source": [ "compiled_pattern = re.compile(pattern)" ] }, { "cell_type": "code", "execution_count": 386, "metadata": {}, "outputs": [], "source": [ "match = compiled_pattern.search(line)" ] }, { "cell_type": "code", "execution_count": 387, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'666'" ] }, "execution_count": 387, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match.group(1)" ] }, { "cell_type": "code", "execution_count": 388, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg'" ] }, "execution_count": 388, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match.group(2)" ] }, { "cell_type": "code", "execution_count": 389, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Faschingbauer'" ] }, "execution_count": 389, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match.group(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Das gleich ohne regex" ] }, { "cell_type": "code", "execution_count": 390, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[' 666 ', ' Joerg ', 'Faschingbauer ']" ] }, "execution_count": 390, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line.split(';')" ] }, { "cell_type": "code", "execution_count": 391, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['666', 'Joerg', 'Faschingbauer']" ] }, "execution_count": 391, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fields = []\n", "for field in line.split(';'):\n", " fields.append(field.strip())\n", "fields" ] }, { "cell_type": "code", "execution_count": 392, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['666', 'Joerg', 'Faschingbauer']" ] }, "execution_count": 392, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[field.strip() for field in line.split(';')]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Miscellaneous String Methods" ] }, { "cell_type": "code", "execution_count": 393, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 393, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = ' '\n", "s.isspace()" ] }, { "cell_type": "code", "execution_count": 394, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 394, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'XXX'.isupper()" ] }, { "cell_type": "code", "execution_count": 395, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 395, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'xxx'.islower()" ] }, { "cell_type": "code", "execution_count": 396, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 396, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'666'.isdigit()" ] }, { "cell_type": "code", "execution_count": 397, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 397, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len('')" ] }, { "cell_type": "code", "execution_count": 398, "metadata": {}, "outputs": [], "source": [ "s = 'xxx'" ] }, { "cell_type": "code", "execution_count": 399, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "was drin\n" ] } ], "source": [ "if len(s):\n", " print('was drin')" ] }, { "cell_type": "code", "execution_count": 400, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "was drin\n" ] } ], "source": [ "if s:\n", " print('was drin')" ] }, { "cell_type": "code", "execution_count": 401, "metadata": {}, "outputs": [], "source": [ "s = ''" ] }, { "cell_type": "code", "execution_count": 402, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nix drin\n" ] } ], "source": [ "if s:\n", " print('was drin')\n", "else:\n", " print('nix drin')" ] }, { "cell_type": "code", "execution_count": 403, "metadata": {}, "outputs": [], "source": [ "s = 'mississippi'" ] }, { "cell_type": "code", "execution_count": 404, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 404, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.count('ss')" ] }, { "cell_type": "code", "execution_count": 405, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'MISSISSIPPI'" ] }, "execution_count": 405, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.upper()" ] }, { "cell_type": "code", "execution_count": 406, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 406, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('ss')" ] }, { "cell_type": "code", "execution_count": 407, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 407, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('ss', 2+1)" ] }, { "cell_type": "code", "execution_count": 408, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 408, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('xxx')" ] }, { "cell_type": "code", "execution_count": 409, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 409, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.index('ss')" ] }, { "cell_type": "code", "execution_count": 410, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " substring not found\n" ] } ], "source": [ "try:\n", " s.index('xxx')\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 411, "metadata": {}, "outputs": [], "source": [ "filename = 'data.csv'" ] }, { "cell_type": "code", "execution_count": 412, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 412, "metadata": {}, "output_type": "execute_result" } ], "source": [ "filename.endswith('.csv')" ] }, { "cell_type": "code", "execution_count": 413, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' 666 ; Joerg ;Faschingbauer '" ] }, "execution_count": 413, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line" ] }, { "cell_type": "code", "execution_count": 414, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[' 666 ', ' Joerg ', 'Faschingbauer ']" ] }, "execution_count": 414, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line.split(';')" ] }, { "cell_type": "code", "execution_count": 415, "metadata": {}, "outputs": [], "source": [ "items = ['666', 'Joerg', 'Faschingbauer']" ] }, { "cell_type": "code", "execution_count": 416, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'666;Joerg;Faschingbauer'" ] }, "execution_count": 416, "metadata": {}, "output_type": "execute_result" } ], "source": [ "';'.join(items)" ] }, { "cell_type": "code", "execution_count": 417, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' hallo '" ] }, "execution_count": 417, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hallo'.center(80)" ] }, { "cell_type": "code", "execution_count": 418, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' hallo'" ] }, "execution_count": 418, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'hallo'\n", "' '*(80-len(s)) + s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More About Lists" ] }, { "cell_type": "code", "execution_count": 419, "metadata": {}, "outputs": [], "source": [ "l = [1, 'two', 3.0]" ] }, { "cell_type": "code", "execution_count": 420, "metadata": {}, "outputs": [], "source": [ "l.append(666)" ] }, { "cell_type": "code", "execution_count": 421, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'two', 3.0, 666]" ] }, "execution_count": 421, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 422, "metadata": {}, "outputs": [], "source": [ "l1 = [8, 'neun']" ] }, { "cell_type": "code", "execution_count": 423, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'two', 3.0, 666, 8, 'neun']" ] }, "execution_count": 423, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.extend(l1)\n", "l" ] }, { "cell_type": "code", "execution_count": 424, "metadata": {}, "outputs": [], "source": [ "del l[3]" ] }, { "cell_type": "code", "execution_count": 425, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'two', 3.0, 8, 'neun']" ] }, "execution_count": 425, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 427, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 427, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[2]" ] }, { "cell_type": "code", "execution_count": 428, "metadata": {}, "outputs": [], "source": [ "l.insert(0, -1)" ] }, { "cell_type": "code", "execution_count": 429, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-1, 1, 'two', 3.0, 8, 'neun']" ] }, "execution_count": 429, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 430, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-1, 1, 1.5, 'two', 3.0, 8, 'neun']" ] }, "execution_count": 430, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.insert(2, 1.5)\n", "l" ] }, { "cell_type": "code", "execution_count": 431, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['neun', 8, 3.0, 'two', 1.5, 1, -1]" ] }, "execution_count": 431, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.reverse()\n", "l" ] }, { "cell_type": "code", "execution_count": 434, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-1\n", "1\n", "1.5\n", "two\n", "3.0\n", "8\n", "neun\n" ] } ], "source": [ "for elem in reversed(l):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 436, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 436, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.pop(5)" ] }, { "cell_type": "code", "execution_count": 437, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['neun', 8, 3.0, 'two', 1.5, -1]" ] }, "execution_count": 437, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More About Dictionaries" ] }, { "cell_type": "code", "execution_count": 438, "metadata": {}, "outputs": [], "source": [ "d = dict()" ] }, { "cell_type": "code", "execution_count": 439, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{}" ] }, "execution_count": 439, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 440, "metadata": {}, "outputs": [], "source": [ "d = {'one': 1, 'two': 2}" ] }, { "cell_type": "code", "execution_count": 441, "metadata": {}, "outputs": [], "source": [ "d = {}" ] }, { "cell_type": "code", "execution_count": 443, "metadata": {}, "outputs": [], "source": [ "l = [('one', 1), ('two', 2)]\n", "d = dict(l)" ] }, { "cell_type": "code", "execution_count": 444, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2}" ] }, "execution_count": 444, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 445, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 445, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['one']" ] }, { "cell_type": "code", "execution_count": 447, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'three'\n" ] } ], "source": [ "try:\n", " d['three']\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 448, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 448, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = d.get('two')\n", "v" ] }, { "cell_type": "code", "execution_count": 449, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "v = d.get('three')\n", "print(v)" ] }, { "cell_type": "code", "execution_count": 450, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nein\n" ] } ], "source": [ "if d.get('three'):\n", " print('ja')\n", "else:\n", " print('nein')" ] }, { "cell_type": "code", "execution_count": 451, "metadata": {}, "outputs": [], "source": [ "d['three'] = 3" ] }, { "cell_type": "code", "execution_count": 452, "metadata": {}, "outputs": [], "source": [ "del d['three']" ] }, { "cell_type": "code", "execution_count": 453, "metadata": {}, "outputs": [], "source": [ "d['three'] = None" ] }, { "cell_type": "code", "execution_count": 454, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 454, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'three' in d" ] }, { "cell_type": "code", "execution_count": 455, "metadata": {}, "outputs": [], "source": [ "del d['three']" ] }, { "cell_type": "code", "execution_count": 456, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 456, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = d.get('three')\n", "if v is None:\n", " v = 3\n", "v" ] }, { "cell_type": "code", "execution_count": 458, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 458, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = d.get('three', 3)\n", "v" ] }, { "cell_type": "code", "execution_count": 463, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 463, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = d.get('three')\n", "if v is None:\n", " d['three'] = 3\n", " v = d['three']\n", "v" ] }, { "cell_type": "code", "execution_count": 464, "metadata": {}, "outputs": [], "source": [ "del d['three']" ] }, { "cell_type": "code", "execution_count": 465, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 465, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = d.setdefault('three', 3)\n", "v" ] }, { "cell_type": "code", "execution_count": 467, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3}" ] }, "execution_count": 467, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 468, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n", "three\n" ] } ], "source": [ "for elem in d:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 469, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n", "three\n" ] } ], "source": [ "for elem in d.keys():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 470, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for elem in d.values():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 471, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('one', 1)\n", "('two', 2)\n", "('three', 3)\n" ] } ], "source": [ "for elem in d.items():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 472, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one 1\n", "two 2\n", "three 3\n" ] } ], "source": [ "for elem in d.items():\n", " k = elem[0]\n", " v = elem[1]\n", " print(k, v)" ] }, { "cell_type": "code", "execution_count": 473, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one 1\n", "two 2\n", "three 3\n" ] } ], "source": [ "for k, v in d.items():\n", " print(k, v)" ] }, { "cell_type": "code", "execution_count": 474, "metadata": {}, "outputs": [], "source": [ "a, b = 1, 2" ] }, { "cell_type": "code", "execution_count": 475, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 475, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 476, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 476, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 477, "metadata": {}, "outputs": [], "source": [ "(a, b) = (b, a)" ] }, { "cell_type": "code", "execution_count": 479, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 479, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 480, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 480, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 481, "metadata": {}, "outputs": [], "source": [ "line = '666;Joerg;Faschingbauer'\n", "id, firstname, lastname = line.split(';')" ] }, { "cell_type": "code", "execution_count": 483, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'666'" ] }, "execution_count": 483, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id" ] }, { "cell_type": "code", "execution_count": 486, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'str' object is not callable\n" ] } ], "source": [ "try:\n", " id(a)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 487, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 487, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('666')" ] }, { "cell_type": "code", "execution_count": 488, "metadata": {}, "outputs": [], "source": [ "int = 666" ] }, { "cell_type": "code", "execution_count": 490, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'int' object is not callable\n" ] } ], "source": [ "try:\n", " int('666')\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 491, "metadata": {}, "outputs": [], "source": [ "del int" ] }, { "cell_type": "code", "execution_count": 492, "metadata": {}, "outputs": [], "source": [ "a = 666" ] }, { "cell_type": "code", "execution_count": 493, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 493, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 494, "metadata": {}, "outputs": [], "source": [ "int = type(a)" ] }, { "cell_type": "code", "execution_count": 497, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 497, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('666')" ] }, { "cell_type": "code", "execution_count": 498, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 498, "metadata": {}, "output_type": "execute_result" } ], "source": [ "__builtins__" ] }, { "cell_type": "code", "execution_count": 499, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 499, "metadata": {}, "output_type": "execute_result" } ], "source": [ "__builtin__.id" ] }, { "cell_type": "code", "execution_count": 500, "metadata": {}, "outputs": [], "source": [ "id = __builtins__.id" ] }, { "cell_type": "code", "execution_count": 501, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139782799135312" ] }, "execution_count": 501, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 502, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2: None, 3: None, 1: None, 5: None}" ] }, "execution_count": 502, "metadata": {}, "output_type": "execute_result" } ], "source": [ "keys = [2, 3, 1, 5, 1]\n", "dict.fromkeys(keys)" ] }, { "cell_type": "code", "execution_count": 503, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n", "1\n", "5\n" ] } ], "source": [ "for k in dict.fromkeys(keys):\n", " print(k)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More About Sets" ] }, { "cell_type": "code", "execution_count": 505, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a', 'b', 'c'}" ] }, "execution_count": 505, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set('abc')\n", "s" ] }, { "cell_type": "code", "execution_count": 506, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "b\n", "c\n" ] } ], "source": [ "for elem in 'abc':\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 508, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'abc', 'def'}" ] }, "execution_count": 508, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set(['abc', 'def'])" ] }, { "cell_type": "code", "execution_count": 510, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'int' object is not iterable\n" ] } ], "source": [ "try:\n", " set(666)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comprehensions (Generator Expressions)" ] }, { "cell_type": "code", "execution_count": 511, "metadata": {}, "outputs": [], "source": [ "def squares(l):\n", " sqs = []\n", " for elem in l:\n", " sqs.append(elem**2)\n", " return sqs" ] }, { "cell_type": "code", "execution_count": 512, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "for elem in squares(range(5)):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 513, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "for elem in [i**2 for i in range(5)]:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 514, "metadata": {}, "outputs": [], "source": [ "def squares(l):\n", " for elem in l:\n", " yield elem**2" ] }, { "cell_type": "code", "execution_count": 515, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "for elem in squares(range(5)):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 517, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "for elem in (i**2 for i in range(5)):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 518, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " at 0x7f21b81bbe60>" ] }, "execution_count": 518, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(i**2 for i in range(5))" ] }, { "cell_type": "code", "execution_count": 519, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16]" ] }, "execution_count": 519, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[i**2 for i in range(5)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## File I/O" ] }, { "cell_type": "code", "execution_count": 525, "metadata": {}, "outputs": [], "source": [ "import os" ] }, { "cell_type": "code", "execution_count": 527, "metadata": {}, "outputs": [], "source": [ "cwd = os.getcwd()" ] }, { "cell_type": "code", "execution_count": 529, "metadata": {}, "outputs": [], "source": [ "os.chdir('/etc')" ] }, { "cell_type": "code", "execution_count": 533, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/etc'" ] }, "execution_count": 533, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.getcwd()" ] }, { "cell_type": "code", "execution_count": 534, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['extlinux.conf',\n", " 'favicon.png',\n", " 'grub2-efi.cfg',\n", " 'grub2.cfg',\n", " 'issue',\n", " 'issue.net',\n", " 'opensc-x86_64.conf',\n", " '.java',\n", " 'NetworkManager',\n", " 'PackageKit',\n", " 'UPower',\n", " 'X11',\n", " 'abrt',\n", " 'alsa',\n", " 'alternatives',\n", " 'anaconda',\n", " 'audit',\n", " 'authselect',\n", " 'avahi',\n", " 'bash_completion.d',\n", " 'binfmt.d',\n", " 'bluetooth',\n", " 'brltty',\n", " 'ceph',\n", " 'chkconfig.d',\n", " 'chromium',\n", " 'cifs-utils',\n", " 'containers',\n", " 'crypto-policies',\n", " 'cups',\n", " 'cupshelpers',\n", " 'dbus-1',\n", " 'dconf',\n", " 'debuginfod',\n", " 'default',\n", " 'depmod.d',\n", " 'dhcp',\n", " 'dnf',\n", " 'dnsmasq.d',\n", " 'dracut.conf.d',\n", " 'egl',\n", " 'exports.d',\n", " 'firefox',\n", " 'firewalld',\n", " 'flatpak',\n", " 'flexiblasrc.d',\n", " 'fonts',\n", " 'fwupd',\n", " 'gcrypt',\n", " 'gdbinit.d',\n", " 'gdm',\n", " 'geoclue',\n", " 'glvnd',\n", " 'gnupg',\n", " 'groff',\n", " 'grub.d',\n", " 'gss',\n", " 'gssproxy',\n", " 'hp',\n", " 'httpd',\n", " 'iproute2',\n", " 'iscsi',\n", " 'issue.d',\n", " 'java',\n", " 'jvm-common',\n", " 'jvm',\n", " 'kdump',\n", " 'kernel',\n", " 'krb5.conf.d',\n", " 'ld.so.conf.d',\n", " 'libblockdev',\n", " 'libibverbs.d',\n", " 'libnl',\n", " 'libpaper.d',\n", " 'libreport',\n", " 'libssh',\n", " 'libvirt',\n", " 'logrotate.d',\n", " 'lvm',\n", " 'mcelog',\n", " 'mdevctl.d',\n", " 'modprobe.d',\n", " 'modules-load.d',\n", " 'motd.d',\n", " 'my.cnf.d',\n", " 'ndctl.conf.d',\n", " 'ndctl',\n", " 'nftables',\n", " 'openldap',\n", " 'openvpn',\n", " 'opt',\n", " 'ostree',\n", " 'pam.d',\n", " 'pkcs11',\n", " 'pkgconfig',\n", " 'pki',\n", " 'plymouth',\n", " 'pm',\n", " 'polkit-1',\n", " 'popt.d',\n", " 'ppp',\n", " 'profile.d',\n", " 'pulse',\n", " 'qemu-ga',\n", " 'qemu',\n", " 'rc.d',\n", " 'reader.conf.d',\n", " 'request-key.d',\n", " 'rpm',\n", " 'rwtab.d',\n", " 'samba',\n", " 'sane.d',\n", " 'sasl2',\n", " 'security',\n", " 'selinux',\n", " 'sgml',\n", " 'skel',\n", " 'sos',\n", " 'speech-dispatcher',\n", " 'ssh',\n", " 'ssl',\n", " 'sssd',\n", " 'statetab.d',\n", " 'sudoers.d',\n", " 'swid',\n", " 'sysconfig',\n", " 'sysctl.d',\n", " 'systemd',\n", " 'terminfo',\n", " 'thermald',\n", " 'tmpfiles.d',\n", " 'tpm2-tss',\n", " 'udev',\n", " 'udisks2',\n", " 'unbound',\n", " 'vmware-tools',\n", " 'vpnc',\n", " 'vulkan',\n", " 'wireplumber',\n", " 'wpa_supplicant',\n", " 'xdg',\n", " 'xml',\n", " 'yum.repos.d',\n", " 'zfs-fuse',\n", " '.pwd.lock',\n", " 'DIR_COLORS',\n", " 'DIR_COLORS.lightbgcolor',\n", " 'GREP_COLORS',\n", " 'adjtime',\n", " 'bindresvport.blacklist',\n", " 'brlapi.key',\n", " 'chrony.conf',\n", " 'chrony.keys',\n", " 'dleyna-renderer-service.conf',\n", " 'dleyna-server-service.conf',\n", " 'dracut.conf',\n", " 'fprintd.conf',\n", " 'fstab',\n", " 'fuse.conf',\n", " 'jwhois.conf',\n", " 'libaudit.conf',\n", " 'libuser.conf',\n", " 'locale.conf',\n", " 'login.defs',\n", " 'magic',\n", " 'mailcap',\n", " 'man_db.conf',\n", " 'mime.types',\n", " 'mke2fs.conf',\n", " 'nanorc',\n", " 'netconfig',\n", " 'papersize',\n", " 'passwdqc.conf',\n", " 'pinforc',\n", " 'request-key.conf',\n", " 'sestatus.conf',\n", " 'shells',\n", " 'sudoers',\n", " 'tcsd.conf',\n", " 'usb_modeswitch.conf',\n", " 'vconsole.conf',\n", " 'wgetrc',\n", " 'xattr.conf',\n", " 'crypttab',\n", " 'machine-id',\n", " 'machine-info',\n", " 'localtime',\n", " 'hostname',\n", " 'mtab',\n", " 'subuid-',\n", " 'subgid-',\n", " 'timidity.cfg',\n", " 'timidity++.cfg',\n", " 'openal',\n", " 'vdpau_wrapper.cfg',\n", " 'ld.so.conf',\n", " 'rpc',\n", " 'asound.conf',\n", " 'krb5.conf',\n", " 'idmapd.conf',\n", " 'sysctl.conf',\n", " 'logrotate.conf',\n", " 'mtools.conf',\n", " 'brltty.conf',\n", " 'flexiblasrc',\n", " 'ts.conf',\n", " 'gdbinit',\n", " 'rsyncd.conf',\n", " 'anthy-unicode.conf',\n", " 'kdump.conf',\n", " 'Trolltech.conf',\n", " 'my.cnf',\n", " 'rygel.conf',\n", " 'virc',\n", " 'swtpm-localca.conf',\n", " 'swtpm-localca.options',\n", " 'swtpm_setup.conf',\n", " 'uresourced.conf',\n", " 'opensc.conf',\n", " 'aliases',\n", " 'environment',\n", " 'ethertypes',\n", " 'exports',\n", " 'filesystems',\n", " 'host.conf',\n", " 'inputrc',\n", " 'motd',\n", " 'networks',\n", " 'printcap',\n", " 'profile',\n", " 'protocols',\n", " 'services',\n", " 'csh.login',\n", " 'hosts',\n", " 'nfsmount.conf',\n", " 'nfs.conf',\n", " 'dnsmasq.conf',\n", " 'resolv.conf',\n", " 'nsswitch.conf',\n", " 'group-',\n", " 'gshadow-',\n", " 'passwd-',\n", " 'shadow-',\n", " 'thunderbird',\n", " 'rhashrc',\n", " 'ImageMagick-6',\n", " 'bashrc',\n", " 'csh.cshrc',\n", " 'gimp',\n", " 'updatedb.conf',\n", " '.updated',\n", " 'mplayer',\n", " 'youtube-dl.conf',\n", " 'appstream.conf',\n", " 'mosquitto',\n", " 'containerd',\n", " 'docker',\n", " 'at.deny',\n", " 'lftp.conf',\n", " 'shadow',\n", " 'group',\n", " 'gshadow',\n", " 'subuid',\n", " 'subgid',\n", " 'passwd~',\n", " 'passwd',\n", " 'init.d',\n", " 'rc0.d',\n", " 'rc1.d',\n", " 'rc2.d',\n", " 'rc3.d',\n", " 'rc4.d',\n", " 'rc5.d',\n", " 'rc6.d',\n", " 'fedora-release',\n", " 'os-release',\n", " 'redhat-release',\n", " 'system-release',\n", " 'system-release-cpe',\n", " 'inittab',\n", " 'sudo.conf',\n", " 'trusted-key.key',\n", " 'makedumpfile.conf.sample',\n", " 'sensors.d',\n", " 'sensors3.conf',\n", " 'ld.so.cache']" ] }, "execution_count": 534, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.listdir()" ] }, { "cell_type": "code", "execution_count": 531, "metadata": {}, "outputs": [], "source": [ "f = open('passwd', encoding='utf-8')" ] }, { "cell_type": "code", "execution_count": 535, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<_io.TextIOWrapper name='passwd' mode='r' encoding='utf-8'>" ] }, "execution_count": 535, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f" ] }, { "cell_type": "code", "execution_count": 537, "metadata": {}, "outputs": [], "source": [ "os.chdir(cwd)" ] }, { "cell_type": "code", "execution_count": 538, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/home/jfasch/work/jfasch-home/trainings/log/detail/2023-12-18--Python-Basics'" ] }, "execution_count": 538, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.getcwd()" ] }, { "cell_type": "code", "execution_count": 539, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd', encoding='utf-8')" ] }, { "cell_type": "code", "execution_count": 541, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<_io.TextIOWrapper name='/etc/passwd' mode='r' encoding='utf-8'>" ] }, "execution_count": 541, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f" ] }, { "cell_type": "code", "execution_count": 542, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'root:'" ] }, "execution_count": 542, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read(5)" ] }, { "cell_type": "code", "execution_count": 544, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "':root'" ] }, "execution_count": 544, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read(5)" ] }, { "cell_type": "code", "execution_count": 545, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "':/root:/bin/bash\\n'" ] }, "execution_count": 545, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.readline()" ] }, { "cell_type": "code", "execution_count": 546, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'bin:x:1:1:bin:/bin:/sbin/nologin\\n'" ] }, "execution_count": 546, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.readline()" ] }, { "cell_type": "code", "execution_count": 547, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'daemon: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\\ndbus:x:81:81:System message bus:/:/sbin/nologin\\napache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin\\ntss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin\\nsystemd-network:x:192:192:systemd Network Management:/:/usr/sbin/nologin\\nsystemd-oom:x:999:999:systemd Userspace OOM Killer:/:/usr/sbin/nologin\\nsystemd-resolve:x:193:193:systemd Resolver:/:/usr/sbin/nologin\\nqemu:x:107:107:qemu user:/:/sbin/nologin\\npolkitd:x:998:997:User for polkitd:/:/sbin/nologin\\navahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin\\nunbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin\\nnm-openconnect:x:996:994:NetworkManager user for OpenConnect:/:/sbin/nologin\\ngeoclue:x:995:993:User for geoclue:/var/lib/geoclue:/sbin/nologin\\nusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin\\ngluster:x:994:992:GlusterFS daemons:/run/gluster:/sbin/nologin\\nrtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin\\nchrony:x:993:990::/var/lib/chrony:/sbin/nologin\\nsaslauth:x:992:76:Saslauthd user:/run/saslauthd:/sbin/nologin\\ndnsmasq:x:991:989:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin\\nrpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin\\ncolord:x:990:988:User for colord:/var/lib/colord:/sbin/nologin\\nrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin\\nopenvpn:x:989:987:OpenVPN:/etc/openvpn:/sbin/nologin\\nnm-openvpn:x:988:986:Default user for running openvpn spawned by NetworkManager:/:/sbin/nologin\\npipewire:x:987:985:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin\\nabrt:x:173:173::/etc/abrt:/sbin/nologin\\nflatpak:x:986:983:User for flatpak system helper:/:/sbin/nologin\\ngdm:x:42:42:GNOME Display Manager:/var/lib/gdm:/sbin/nologin\\ngnome-initial-setup:x:985:982::/run/gnome-initial-setup/:/sbin/nologin\\nvboxadd:x:984: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:Jörg Faschingbauer:/home/jfasch:/bin/bash\\nsystemd-coredump:x:978:978:systemd Core Dumper:/:/usr/sbin/nologin\\nsystemd-timesync:x:977:977:systemd Time Synchronization:/:/usr/sbin/nologin\\nmosquitto:x:976:976:Mosquitto Broker:/etc/mosquitto:/sbin/nologin\\n'" ] }, "execution_count": 547, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read()" ] }, { "cell_type": "code", "execution_count": 548, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 548, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read()" ] }, { "cell_type": "code", "execution_count": 549, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 549, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.seek(0)" ] }, { "cell_type": "code", "execution_count": 550, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'root:'" ] }, "execution_count": 550, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read(5)" ] }, { "cell_type": "code", "execution_count": 552, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd', encoding='utf-8')" ] }, { "cell_type": "code", "execution_count": 553, "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", "dbus:x:81:81:System message bus:/:/sbin/nologin\n", "\n", "apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin\n", "\n", "tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin\n", "\n", "systemd-network:x:192:192:systemd Network Management:/:/usr/sbin/nologin\n", "\n", "systemd-oom:x:999:999:systemd Userspace OOM Killer:/:/usr/sbin/nologin\n", "\n", "systemd-resolve:x:193:193:systemd Resolver:/:/usr/sbin/nologin\n", "\n", "qemu:x:107:107:qemu user:/:/sbin/nologin\n", "\n", "polkitd:x:998:997: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:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin\n", "\n", "nm-openconnect:x:996:994:NetworkManager user for OpenConnect:/:/sbin/nologin\n", "\n", "geoclue:x:995:993:User for geoclue:/var/lib/geoclue:/sbin/nologin\n", "\n", "usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin\n", "\n", "gluster:x:994:992:GlusterFS daemons:/run/gluster:/sbin/nologin\n", "\n", "rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin\n", "\n", "chrony:x:993:990::/var/lib/chrony:/sbin/nologin\n", "\n", "saslauth:x:992:76:Saslauthd user:/run/saslauthd:/sbin/nologin\n", "\n", "dnsmasq:x:991:989:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin\n", "\n", "rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin\n", "\n", "colord:x:990:988:User for colord:/var/lib/colord:/sbin/nologin\n", "\n", "rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin\n", "\n", "openvpn:x:989:987:OpenVPN:/etc/openvpn:/sbin/nologin\n", "\n", "nm-openvpn:x:988:986:Default user for running openvpn spawned by NetworkManager:/:/sbin/nologin\n", "\n", "pipewire:x:987:985:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin\n", "\n", "abrt:x:173:173::/etc/abrt:/sbin/nologin\n", "\n", "flatpak:x:986:983:User for flatpak system helper:/:/sbin/nologin\n", "\n", "gdm:x:42:42:GNOME Display Manager:/var/lib/gdm:/sbin/nologin\n", "\n", "gnome-initial-setup:x:985:982::/run/gnome-initial-setup/:/sbin/nologin\n", "\n", "vboxadd:x:984: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:Jörg Faschingbauer:/home/jfasch:/bin/bash\n", "\n", "systemd-coredump:x:978:978:systemd Core Dumper:/:/usr/sbin/nologin\n", "\n", "systemd-timesync:x:977:977:systemd Time Synchronization:/:/usr/sbin/nologin\n", "\n", "mosquitto:x:976:976:Mosquitto Broker:/etc/mosquitto:/sbin/nologin\n", "\n" ] } ], "source": [ "for line in f:\n", " print(line)" ] }, { "cell_type": "code", "execution_count": 557, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd', encoding='ascii')" ] }, { "cell_type": "code", "execution_count": 558, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'ascii' codec can't decode byte 0xc3 in position 2440: ordinal not in range(128)\n" ] } ], "source": [ "try:\n", " for line in f:\n", " print(line)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``pathlib``, ``os.path``" ] }, { "cell_type": "code", "execution_count": 559, "metadata": {}, "outputs": [], "source": [ "directory = '/etc'\n", "filename = 'passwd'" ] }, { "cell_type": "code", "execution_count": 561, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/etc/passwd'" ] }, "execution_count": 561, "metadata": {}, "output_type": "execute_result" } ], "source": [ "directory + '/' + filename" ] }, { "cell_type": "code", "execution_count": 562, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/etc/passwd'" ] }, "execution_count": 562, "metadata": {}, "output_type": "execute_result" } ], "source": [ "directory + os.sep + filename" ] }, { "cell_type": "code", "execution_count": 563, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/etc/passwd'" ] }, "execution_count": 563, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.path.join(directory, filename)" ] }, { "cell_type": "code", "execution_count": 564, "metadata": {}, "outputs": [], "source": [ "import pathlib" ] }, { "cell_type": "code", "execution_count": 565, "metadata": {}, "outputs": [], "source": [ "directory = pathlib.Path('/etc')" ] }, { "cell_type": "code", "execution_count": 567, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "PosixPath('/etc')" ] }, "execution_count": 567, "metadata": {}, "output_type": "execute_result" } ], "source": [ "directory" ] }, { "cell_type": "code", "execution_count": 569, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "PosixPath('/etc/passwd')" ] }, "execution_count": 569, "metadata": {}, "output_type": "execute_result" } ], "source": [ "directory / filename" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Regexen" ] }, { "cell_type": "code", "execution_count": 574, "metadata": {}, "outputs": [], "source": [ "import re" ] }, { "cell_type": "code", "execution_count": 575, "metadata": {}, "outputs": [], "source": [ "rex = r'[^A-F]'" ] }, { "cell_type": "code", "execution_count": 577, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 577, "metadata": {}, "output_type": "execute_result" } ], "source": [ "re.search(rex, 'X')" ] }, { "cell_type": "code", "execution_count": 578, "metadata": {}, "outputs": [], "source": [ "re.search(rex, 'D')" ] }, { "cell_type": "code", "execution_count": 579, "metadata": {}, "outputs": [], "source": [ "rex = r'^\\s*(#.*|$)'" ] }, { "cell_type": "code", "execution_count": 580, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 580, "metadata": {}, "output_type": "execute_result" } ], "source": [ "re.search(rex, ' # xxx')" ] }, { "cell_type": "code", "execution_count": 581, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 581, "metadata": {}, "output_type": "execute_result" } ], "source": [ "re.search(rex, ' ')" ] }, { "cell_type": "code", "execution_count": 582, "metadata": {}, "outputs": [], "source": [ "re.search(rex, ' kadjckk # xxddjh')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``distill.py``" ] }, { "cell_type": "code", "execution_count": 585, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 585, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = ' \\t abc \\n \\n'\n", "s.strip()" ] }, { "cell_type": "code", "execution_count": 588, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'# blah'" ] }, "execution_count": 588, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = ' # blah '\n", "s.strip()" ] }, { "cell_type": "code", "execution_count": 589, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "blah\n" ] } ], "source": [ "print('blah')" ] }, { "cell_type": "code", "execution_count": 590, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "blah\n", "\n" ] } ], "source": [ "print('blah\\n')" ] }, { "cell_type": "code", "execution_count": 593, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "blahblahblahblah\n" ] } ], "source": [ "print('blah', end='blahblah')\n", "print('blah')" ] }, { "cell_type": "code", "execution_count": 595, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "blahblah\n" ] } ], "source": [ "print('blah', end='')\n", "print('blah')" ] } ], "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.10.11" } }, "nbformat": 4, "nbformat_minor": 4 }