{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python (2022-11-16 - 2022-11-18)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Object Oriented" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "print('Hello World')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(a))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sys\n", "sys.getsizeof(a)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(print))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(type))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"print(value, ..., sep=' ', end='\\\\n', file=sys.stdout, flush=False)\\n\\nPrints the values to a stream, or to sys.stdout by default.\\nOptional keyword arguments:\\nfile: a file-like object (stream); defaults to the current sys.stdout.\\nsep: string inserted between values, default a space.\\nend: string appended after the last value, default a newline.\\nflush: whether to forcibly flush the stream.\"" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print.__doc__" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function print in module builtins:\n", "\n", "print(...)\n", " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", " \n", " Prints the values to a stream, or to sys.stdout by default.\n", " Optional keyword arguments:\n", " file: a file-like object (stream); defaults to the current sys.stdout.\n", " sep: string inserted between values, default a space.\n", " end: string appended after the last value, default a newline.\n", " flush: whether to forcibly flush the stream.\n", "\n" ] } ], "source": [ "help(print)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "class Person:\n", " '''Some pointless and repetitive implementation of\n", " yet another Person class (for didactical purposes only)'''\n", " def __init__(self, firstname, lastname):\n", " self.firstname = firstname\n", " self.lastname = lastname\n", " def fullname(self):\n", " return self.firstname + ' ' + self.lastname" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "joerg = Person('Joerg', 'Faschingbauer')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.Person" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(joerg)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joerg.firstname" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg Faschingbauer'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joerg.fullname()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.Person" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Person" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(Person)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Person'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Person.__name__" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__class__',\n", " '__delattr__',\n", " '__dict__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__le__',\n", " '__lt__',\n", " '__module__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " '__weakref__',\n", " 'fullname']" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(Person)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Some pointless and repetitive implementation of\\n yet another Person class (for didactical purposes only)'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Person.__doc__" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on class Person in module __main__:\n", "\n", "class Person(builtins.object)\n", " | Person(firstname, lastname)\n", " | \n", " | Some pointless and repetitive implementation of\n", " | yet another Person class (for didactical purposes only)\n", " | \n", " | Methods defined here:\n", " | \n", " | __init__(self, firstname, lastname)\n", " | Initialize self. See help(type(self)) for accurate signature.\n", " | \n", " | fullname(self)\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data descriptors defined here:\n", " | \n", " | __dict__\n", " | dictionary for instance variables (if defined)\n", " | \n", " | __weakref__\n", " | list of weak references to the object (if defined)\n", "\n" ] } ], "source": [ "help(Person)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Integers" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "i = 666" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "i = 2**64 - 1" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b1111111111111111111111111111111111111111111111111111111111111111'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(i)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "66" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(bin(i))" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "i += 1" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b10000000000000000000000000000000000000000000000000000000000000000'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(i)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "67" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(bin(i))" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**1000" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a == 42" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hurra\n" ] } ], "source": [ "if a == 42:\n", " print('hurra')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'abc'\n", "s" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"abc\"\n", "s" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ab\"c'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'ab\"c'\n", "s" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"ab'c\"" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"ab'c\"\n", "s" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ab\"\\'c'" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"ab\\\"'c\"\n", "s" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ab\"\\'c'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'ab\"\\'c'\n", "s" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\nerste zeile\\nzweite zeile\\n'" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"\"\"\n", "erste zeile\n", "zweite zeile\n", "\"\"\"\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Datatype Conversions" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(12.9)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(12.1)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "invalid literal for int() with base 10: 'abc'\n" ] } ], "source": [ "try:\n", " int('abc')\n", "except ValueError as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2748" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('abc', 16)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "invalid literal for int() with base 10: 'abc'\n" ] } ], "source": [ "try:\n", " int('abc')\n", "except BaseException as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 46, "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(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists and Tuples" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei']" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1,2,'drei']\n", "l" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 4.0]" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append(4.0)\n", "l" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "andere_liste = [5, 6, 7]" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 4.0, 5, 6, 7]" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "neue_liste = l + andere_liste\n", "neue_liste" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 4.0]" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 6, 7]" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "andere_liste" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 4.0, 5, 6, 7]" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l += andere_liste\n", "l" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 4.0, 5, 6, 7, [8, 9, 10]]" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append([8,9,10])\n", "l" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 4.0, 5, 6, 7, [8, 9, 10], 11, 12, 13, 14]" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.extend([11,12,13,14])\n", "l" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'drei'" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[2]" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "del l[2]" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 4.0, 5, 6, 7, [8, 9, 10], 11, 12, 13, 14]" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 in l" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "14 in l" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "666 in l" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3)" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = (1,2,3)\n", "t" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[2]" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(t)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'tuple' object has no attribute 'append'\n" ] } ], "source": [ "try:\n", " t.append(4)\n", "except AttributeError as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionary" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "d = {1:'one', 2:'two'}" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 in d" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one'" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[1]" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "try:\n", " d[3]\n", "except KeyError as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'one', 2: 'two', 3: 'drei'}" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[3] = 'drei'\n", "d" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2: 'two', 3: 'drei'}" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "del d[1]\n", "d" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n" ] } ], "source": [ "for elem in d:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n" ] } ], "source": [ "for elem in d.keys():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "two\n", "drei\n" ] } ], "source": [ "for elem in d.values():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2, 'two')\n", "(3, 'drei')\n" ] } ], "source": [ "for elem in d.items():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 two\n", "3 drei\n" ] } ], "source": [ "for key, value in d.items():\n", " print(key, value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3}" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = {1,2,3}\n", "s" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 in s" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "666 in s" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for elem in s:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 'vier'}" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.add('vier')\n", "s" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [], "source": [ "s.remove(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``for``, and Iteration, and Generators" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "l = [1,2,3,4]\n", "for elem in l:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for elem in range(1,5):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n" ] } ], "source": [ "d = {'one': 1, 'two': 2}\n", "for elem in d:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n" ] } ], "source": [ "for elem in d.keys():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n" ] } ], "source": [ "for elem in d.values():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('one', 1)\n", "('two', 2)\n" ] } ], "source": [ "for elem in d.items():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Key=one, Value=1\n", "Key=two, Value=2\n" ] } ], "source": [ "for elem in d.items():\n", " key = elem[0]\n", " value = elem[1]\n", " print(f'Key={key}, Value={value}')" ] }, { "cell_type": "code", "execution_count": 91, "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:Joerg 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": [ "f = open('/etc/passwd')\n", "for line in f:\n", " print(line)" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for elem in range(3):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = range(3)\n", "type(r)" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = range(10**1000)\n", "r" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 3)" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = range(3)\n", "r" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for elem in r:\n", " print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iterator Protocol" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [], "source": [ "r = range(3)" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [], "source": [ "it = iter(r)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range_iterator" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(it)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [], "source": [ "try:\n", " next(it)\n", "except StopIteration:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``enumerate()``" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Joerg\n", "Caro\n", "Johanna\n", "Philipp\n" ] } ], "source": [ "names = ['Joerg', 'Caro', 'Johanna', 'Philipp']\n", "for name in names:\n", " print(name)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Joerg\n", "1 Caro\n", "2 Johanna\n", "3 Philipp\n" ] } ], "source": [ "for i in range(len(names)):\n", " print(i, names[i])" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Joerg\n", "1 Caro\n", "2 Johanna\n", "3 Philipp\n" ] } ], "source": [ "i = 0\n", "while i < len(names):\n", " print(i, names[i])\n", " i += 1" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 'Joerg')\n", "(1, 'Caro')\n", "(2, 'Johanna')\n", "(3, 'Philipp')\n" ] } ], "source": [ "for elem in enumerate(names):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Joerg\n", "1 Caro\n", "2 Johanna\n", "3 Philipp\n" ] } ], "source": [ "for i, name in enumerate(names):\n", " print(i, name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists, Dictionaries, Generators, Constructors" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for i in range(3):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2]" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = []\n", "for i in range(3):\n", " l.append(i)\n", "l" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2]" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(3))" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "b\n", "c\n" ] } ], "source": [ "for i in 'abc':\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list('abc')" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n" ] } ], "source": [ "d = {'one': 1, 'two': 2}\n", "for elem in d:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['one', 'two']" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(d)" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one 1\n", "two 2\n" ] } ], "source": [ "l = [('one', 1), ('two', 2)]\n", "for k, v in l:\n", " print(k, v)" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2}" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Slicing" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text = 'Hello World'\n", "text[-1:-5]" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "2\n", "4\n", "6\n" ] } ], "source": [ "for i in range(0,7,2):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## References, (Im)mutability" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [], "source": [ "a = 42\n", "b = a" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140323934455312" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140323934455312" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "43" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a += 1\n", "a" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140323934455344" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140323934455376" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a += 1\n", "id(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### And Lists? Mutable!" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 = [1,2,3]\n", "l2 = l1\n", "l2" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1.append(4)\n", "l1" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuples?" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [], "source": [ "t1 = (1,2,3)\n", "t2 = t1" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140323852147712" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(t1)" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140323852147712" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(t2)" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'tuple' object has no attribute 'append'\n" ] } ], "source": [ "try:\n", " t1.append(4)\n", "except AttributeError as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'tuple' object doesn't support item deletion\n" ] } ], "source": [ "try:\n", " del t[1]\n", "except TypeError as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strings" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140323933649904" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 = 'abc'\n", "s2 = s1\n", "id(s1)" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140323933649904" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(s2)" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140323833601584" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 += 'def'\n", "id(s1)" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ``set``" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 = {1,2,3}\n", "s2 = s1\n", "id(s1) == id(s2)" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4}" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1.add(4)\n", "s1" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4}" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s2" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "frozenset({1, 2, 3})" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 = frozenset((1,2,3))\n", "s1" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'frozenset' object has no attribute 'add'\n" ] } ], "source": [ "try:\n", " s1.add(4)\n", "except AttributeError as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [], "source": [ "s2 = set(s1)\n", "s2.add(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b = [1,2]\n", "a" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [], "source": [ "def f():\n", " return 1, 2\n", "i, j = f()" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "j" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [], "source": [ "def f():\n", " return (1,2)\n", "(i, j) = f()" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [], "source": [ "def maximum(a, b):\n", " if a < b:\n", " return b\n", " return a" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 152, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(maximum)" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = maximum\n", "a(1,2)" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'maximum'" ] }, "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum.__name__" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__annotations__',\n", " '__builtins__',\n", " '__call__',\n", " '__class__',\n", " '__closure__',\n", " '__code__',\n", " '__defaults__',\n", " '__delattr__',\n", " '__dict__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__get__',\n", " '__getattribute__',\n", " '__globals__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__kwdefaults__',\n", " '__le__',\n", " '__lt__',\n", " '__module__',\n", " '__name__',\n", " '__ne__',\n", " '__new__',\n", " '__qualname__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__']" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(maximum)" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [], "source": [ "def greet(who, phrase='Grüß Gott'):\n", " print(phrase, who)" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Grüß Gott Jörg\n" ] } ], "source": [ "greet('Jörg')" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Seas Jörg\n" ] } ], "source": [ "greet('Jörg', 'Seas')" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [], "source": [ "def f(x=[]):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "f()" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n" ] } ], "source": [ "f([1,2,3])" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [], "source": [ "def f(x=[]):\n", " x.append(42)\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[42]\n" ] } ], "source": [ "f()" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[42, 42]\n" ] } ], "source": [ "f()" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([42, 42],)" ] }, "execution_count": 166, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.__defaults__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'abc'\n", "s" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 168, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"abc\"\n", "s" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc\"def'" ] }, "execution_count": 169, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'abc\"def'\n", "s" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"abc'def\"" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"abc'def\"\n", "s" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc\"\\'def'" ] }, "execution_count": 171, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'abc\"\\'def'\n", "s" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc\"'def\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'erste zeile\\nzweite zeile'" ] }, "execution_count": 173, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'erste zeile\\nzweite zeile'\n", "s" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "erste zeile\n", "zweite zeile\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc\tdef\n" ] } ], "source": [ "s = 'abc\\tdef'\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\Programme\n", "ocheinprogramm\n" ] } ], "source": [ "doze_path = 'C:\\Programme\\nocheinprogramm'\n", "print(doze_path)" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\Programme\\nocheinprogramm\n" ] } ], "source": [ "doze_path = 'C:\\\\Programme\\\\nocheinprogramm'\n", "print(doze_path)" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\Programme\\nocheinprogramm\n" ] } ], "source": [ "doze_path = r'C:\\Programme\\nocheinprogramm'\n", "print(doze_path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Regular Expressions" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [], "source": [ "line = ' dfghgfdfghj. 123 . '" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [], "source": [ "line = 'jhghgh .123. '" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [], "source": [ "import re" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [], "source": [ "matchstr = r'^\\s*([a-z]+)\\s*\\.\\s*(\\d+)\\s*\\.\\s*$'" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [], "source": [ "compiled_match = re.compile(matchstr)" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [], "source": [ "match = compiled_match.search('jhghgh .123. ')" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'jhghgh'" ] }, "execution_count": 185, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match.group(1)" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "123" ] }, "execution_count": 186, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(match.group(2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Miscellaneous String Methods" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 187, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '123'\n", "s.isdigit()" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 188, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = ' \\t \\r\\n'\n", "s.isspace()" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 189, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'abc'\n", "s.isalpha()" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 190, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.islower()" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ABC'" ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.upper()" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [], "source": [ "s = s.upper()" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ABC'" ] }, "execution_count": 193, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 194, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.lower()" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 195, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'a'\n", "s.isidentifier()" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 196, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '_a'\n", "s.isidentifier()" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Abc'" ] }, "execution_count": 197, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'abc'\n", "s.capitalize()" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' abc '" ] }, "execution_count": 198, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.center(50)" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 199, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'mississippi'\n", "s.count('ss')" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 200, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'ein.csv'\n", "s.endswith('.csv')" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' print(\"hallo\")'" ] }, "execution_count": 201, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '\\tprint(\"hallo\")'\n", "s.expandtabs(4)" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 202, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'mississippi'\n", "s.find('ss')" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ss gefunden: 2\n" ] } ], "source": [ "num_ss = 0\n", "pos = 0\n", "while True:\n", " pos = s.find('ss', pos)\n", " if pos == -1:\n", " break\n", " num_ss += 1\n", " pos += 1\n", "print('ss gefunden:', num_ss)" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'mississippi'" ] }, "execution_count": 204, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 205, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.index('ss')" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "substring not found\n" ] } ], "source": [ "try:\n", " s.index('xyz')\n", "except ValueError as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [], "source": [ "s = ' abc '" ] }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 208, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.strip()" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc '" ] }, "execution_count": 209, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.lstrip()" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' abc'" ] }, "execution_count": 210, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.rstrip()" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [], "source": [ "s = 'line\\n'" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'line'" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.rstrip('\\n')" ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg,Caro,Philipp,Johanna,Isi'" ] }, "execution_count": 213, "metadata": {}, "output_type": "execute_result" } ], "source": [ "items = ['Joerg', 'Caro', 'Philipp', 'Johanna', 'Isi']\n", "','.join(items)" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg,Caro,Philipp,Johanna,Isi'" ] }, "execution_count": 214, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line = ','.join(items)\n", "line" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Joerg', 'Caro', 'Philipp', 'Johanna', 'Isi']" ] }, "execution_count": 215, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line.split(',')" ] }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [], "source": [ "line = ' dfghgfdfghj. 123 . '" ] }, { "cell_type": "code", "execution_count": 217, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[' dfghgfdfghj', ' 123 ', ' ']" ] }, "execution_count": 217, "metadata": {}, "output_type": "execute_result" } ], "source": [ "items = line.split('.')\n", "items" ] }, { "cell_type": "code", "execution_count": 218, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['dfghgfdfghj', '123', '']" ] }, "execution_count": 218, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strippeditems = []\n", "for item in items:\n", " strippeditems.append(item.strip())\n", "strippeditems" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[' dfghgfdfghj', ' 123 ', ' ']" ] }, "execution_count": 219, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strippeditems = [item.strip() for item in items]\n", "items" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More About Lists" ] }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [], "source": [ "l = [3, 2, 5]" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [], "source": [ "l.append(5)" ] }, { "cell_type": "code", "execution_count": 222, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 5, 5]" ] }, "execution_count": 222, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 223, "metadata": {}, "outputs": [], "source": [ "l1 = ['noch', 'was']" ] }, { "cell_type": "code", "execution_count": 224, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 5, 5, 'noch', 'was']" ] }, "execution_count": 224, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.extend(l1)\n", "l" ] }, { "cell_type": "code", "execution_count": 225, "metadata": {}, "outputs": [], "source": [ "l.append(l1)" ] }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 5, 5, 'noch', 'was', ['noch', 'was']]" ] }, "execution_count": 226, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 227, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'<' not supported between instances of 'str' and 'int'\n" ] } ], "source": [ "try:\n", " l.sort()\n", "except TypeError as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 228, "metadata": {}, "outputs": [], "source": [ "l = [4, 1, 6, 666, 42]" ] }, { "cell_type": "code", "execution_count": 229, "metadata": {}, "outputs": [], "source": [ "l.sort()" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 6, 42, 666]" ] }, "execution_count": 230, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 231, "metadata": {}, "outputs": [], "source": [ "l = [4, 1, 6, 666, 42]" ] }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 6, 42, 666]" ] }, "execution_count": 232, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(l)" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [], "source": [ "l.reverse()" ] }, { "cell_type": "code", "execution_count": 234, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 666, 6, 1, 4]" ] }, "execution_count": 234, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 1, 6, 666, 42]" ] }, "execution_count": 235, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(reversed(l))" ] }, { "cell_type": "code", "execution_count": 236, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 236, "metadata": {}, "output_type": "execute_result" } ], "source": [ "666 in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More About Dictionaries" ] }, { "cell_type": "code", "execution_count": 237, "metadata": {}, "outputs": [], "source": [ "d = {}" ] }, { "cell_type": "code", "execution_count": 238, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 238, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(d)" ] }, { "cell_type": "code", "execution_count": 239, "metadata": {}, "outputs": [], "source": [ "d = dict()" ] }, { "cell_type": "code", "execution_count": 240, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{}" ] }, "execution_count": 240, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 241, "metadata": {}, "outputs": [], "source": [ "d['one'] = 1\n", "d['two'] = 2" ] }, { "cell_type": "code", "execution_count": 242, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2}" ] }, "execution_count": 242, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 243, "metadata": {}, "outputs": [], "source": [ "d = {'one': 1, 'two': 2}" ] }, { "cell_type": "code", "execution_count": 244, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 244, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['one']" ] }, { "cell_type": "code", "execution_count": 245, "metadata": {}, "outputs": [], "source": [ "d['three'] = 3" ] }, { "cell_type": "code", "execution_count": 246, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 246, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['three']" ] }, { "cell_type": "code", "execution_count": 247, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'four'\n" ] } ], "source": [ "try:\n", " d['four']\n", "except KeyError as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 248, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 248, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.get('three')" ] }, { "cell_type": "code", "execution_count": 249, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "value = d.get('four')\n", "print(value)" ] }, { "cell_type": "code", "execution_count": 250, "metadata": {}, "outputs": [], "source": [ "value = d.get('four')\n", "if value is None:\n", " d['four'] = 4\n", "else:\n", " print(value)" ] }, { "cell_type": "code", "execution_count": 251, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3, 'four': 4}" ] }, "execution_count": 251, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 252, "metadata": {}, "output_type": "execute_result" } ], "source": [ "value = d.get('five', 5)\n", "value" ] }, { "cell_type": "code", "execution_count": 253, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3, 'four': 4}" ] }, "execution_count": 253, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 254, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 254, "metadata": {}, "output_type": "execute_result" } ], "source": [ "value = d.setdefault('five', 5)\n", "value" ] }, { "cell_type": "code", "execution_count": 255, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}" ] }, "execution_count": 255, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 256, "metadata": {}, "outputs": [], "source": [ "other_d = {'hundred': 100, 'thousand': 1000}" ] }, { "cell_type": "code", "execution_count": 257, "metadata": {}, "outputs": [], "source": [ "d.update(other_d)" ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1,\n", " 'two': 2,\n", " 'three': 3,\n", " 'four': 4,\n", " 'five': 5,\n", " 'hundred': 100,\n", " 'thousand': 1000}" ] }, "execution_count": 258, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 259, "metadata": {}, "outputs": [], "source": [ "yet_another_d = {'thousand': 1, 'one': 1000}" ] }, { "cell_type": "code", "execution_count": 260, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1000,\n", " 'two': 2,\n", " 'three': 3,\n", " 'four': 4,\n", " 'five': 5,\n", " 'hundred': 100,\n", " 'thousand': 1}" ] }, "execution_count": 260, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.update(yet_another_d)\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More About Sets" ] }, { "cell_type": "code", "execution_count": 261, "metadata": {}, "outputs": [], "source": [ "s = set()" ] }, { "cell_type": "code", "execution_count": 262, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "set()" ] }, "execution_count": 262, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 263, "metadata": {}, "outputs": [], "source": [ "s = {1, 2, 3}" ] }, { "cell_type": "code", "execution_count": 264, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 264, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 in s" ] }, { "cell_type": "code", "execution_count": 265, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 265, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 not in s" ] }, { "cell_type": "code", "execution_count": 266, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 266, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 not in s" ] }, { "cell_type": "code", "execution_count": 267, "metadata": {}, "outputs": [], "source": [ "s.add(4)" ] }, { "cell_type": "code", "execution_count": 268, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 268, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 in s" ] }, { "cell_type": "code", "execution_count": 269, "metadata": {}, "outputs": [], "source": [ "s.remove(2)" ] }, { "cell_type": "code", "execution_count": 270, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 270, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in s" ] }, { "cell_type": "code", "execution_count": 271, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 271, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s == s" ] }, { "cell_type": "code", "execution_count": 272, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 3, 4}" ] }, "execution_count": 272, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 273, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 273, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 = {1,2,3}\n", "s2 = {3,1,2}\n", "s1 == s2" ] }, { "cell_type": "code", "execution_count": 274, "metadata": {}, "outputs": [], "source": [ "s3 = {1,2,3,4}" ] }, { "cell_type": "code", "execution_count": 275, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 275, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 == s3" ] }, { "cell_type": "code", "execution_count": 276, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 276, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 < s3" ] }, { "cell_type": "code", "execution_count": 277, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 277, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s3 > s1" ] }, { "cell_type": "code", "execution_count": 278, "metadata": {}, "outputs": [], "source": [ "s4 = {1000, 2000}" ] }, { "cell_type": "code", "execution_count": 279, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 279, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1.isdisjoint(s4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comprehensions (List, Dictionary, Set)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List" ] }, { "cell_type": "code", "execution_count": 280, "metadata": {}, "outputs": [], "source": [ "squares = []\n", "for num in range(5):\n", " squares.append(num**2)" ] }, { "cell_type": "code", "execution_count": 281, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "for sq in squares:\n", " print(sq)" ] }, { "cell_type": "code", "execution_count": 282, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "for sq in [num**2 for num in range(5)]:\n", " print(sq)" ] }, { "cell_type": "code", "execution_count": 283, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "4\n", "16\n", "36\n", "64\n" ] } ], "source": [ "for sq in [num**2 for num in range(10) if num%2==0]:\n", " print(sq)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary" ] }, { "cell_type": "code", "execution_count": 284, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2}" ] }, "execution_count": 284, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict([('one', 1), ('two', 2)])" ] }, { "cell_type": "code", "execution_count": 285, "metadata": {}, "outputs": [], "source": [ "squares = {}\n", "for num in range(5):\n", " squares[num] = num**2" ] }, { "cell_type": "code", "execution_count": 286, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}" ] }, "execution_count": 286, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squares" ] }, { "cell_type": "code", "execution_count": 287, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}" ] }, "execution_count": 287, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squares = {num: num**2 for num in range(5)}\n", "squares" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set" ] }, { "cell_type": "code", "execution_count": 288, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0, 1, 4, 9, 16}" ] }, "execution_count": 288, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squares = set()\n", "for num in range(5):\n", " squares.add(num**2)\n", "squares" ] }, { "cell_type": "code", "execution_count": 289, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0, 1, 4, 9, 16}" ] }, "execution_count": 289, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squares = {num**2 for num in range(5)}\n", "squares" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generator Expressions" ] }, { "cell_type": "code", "execution_count": 290, "metadata": {}, "outputs": [], "source": [ "def squares(seq):\n", " squares = []\n", " for num in range(5):\n", " squares.append(num**2)\n", " return squares" ] }, { "cell_type": "code", "execution_count": 291, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "for sq in squares(range(5)):\n", " print(sq)" ] }, { "cell_type": "code", "execution_count": 292, "metadata": {}, "outputs": [], "source": [ "def squares(seq):\n", " for num in range(5):\n", " yield num**2" ] }, { "cell_type": "code", "execution_count": 293, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "for sq in squares(range(5)):\n", " print(sq)" ] }, { "cell_type": "code", "execution_count": 294, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "for sq in [num**2 for num in range(5)]:\n", " print(sq)" ] }, { "cell_type": "code", "execution_count": 295, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "for sq in (num**2 for num in range(5)):\n", " print(sq)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``eval`` and ``exec``" ] }, { "cell_type": "code", "execution_count": 296, "metadata": {}, "outputs": [], "source": [ "s = '42'" ] }, { "cell_type": "code", "execution_count": 298, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 298, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(s) # BORING!" ] }, { "cell_type": "code", "execution_count": 299, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 299, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval(s)" ] }, { "cell_type": "code", "execution_count": 300, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 300, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '[1,2,3]'\n", "eval(s)" ] }, { "cell_type": "code", "execution_count": 301, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 301, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '\" \".strip()'\n", "eval(s)" ] }, { "cell_type": "code", "execution_count": 302, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo\n" ] } ], "source": [ "value = print('hallo')\n", "value" ] }, { "cell_type": "code", "execution_count": 305, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(value)" ] }, { "cell_type": "code", "execution_count": 306, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo\n" ] } ], "source": [ "s = 'print(\"hallo\")'\n", "value = eval(s)" ] }, { "cell_type": "code", "execution_count": 307, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(value)" ] }, { "cell_type": "code", "execution_count": 311, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'[1,2,\"drei\"]'" ] }, "execution_count": 311, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '['\n", "s += '1,'\n", "s += '2,'\n", "s += '\"drei\"]'\n", "s" ] }, { "cell_type": "code", "execution_count": 312, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei']" ] }, "execution_count": 312, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval(s)" ] }, { "cell_type": "code", "execution_count": 314, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "s = 'for i in range(5): print(i)'\n", "exec(s)" ] }, { "cell_type": "code", "execution_count": 315, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "s = '''\n", "a = 666\n", "print(a)\n", "'''\n", "exec(s)" ] }, { "cell_type": "code", "execution_count": 316, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 316, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 317, "metadata": {}, "outputs": [], "source": [ "del a" ] }, { "cell_type": "code", "execution_count": 319, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\na = 666\\nprint(a)\\n'" ] }, "execution_count": 319, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 320, "metadata": {}, "outputs": [], "source": [ "context = {}" ] }, { "cell_type": "code", "execution_count": 323, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "exec(s, context)" ] }, { "cell_type": "code", "execution_count": 324, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'__builtins__': {'__name__': 'builtins',\n", " '__doc__': \"Built-in functions, exceptions, and other objects.\\n\\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices.\",\n", " '__package__': '',\n", " '__loader__': _frozen_importlib.BuiltinImporter,\n", " '__spec__': ModuleSpec(name='builtins', loader=, origin='built-in'),\n", " '__build_class__': ,\n", " '__import__': ,\n", " 'abs': ,\n", " 'all': ,\n", " 'any': ,\n", " 'ascii': ,\n", " 'bin': ,\n", " 'breakpoint': ,\n", " 'callable': ,\n", " 'chr': ,\n", " 'compile': ,\n", " 'delattr': ,\n", " 'dir': ,\n", " 'divmod': ,\n", " 'eval': ,\n", " 'exec': ,\n", " 'format': ,\n", " 'getattr': ,\n", " 'globals': ,\n", " 'hasattr': ,\n", " 'hash': ,\n", " 'hex': ,\n", " 'id': ,\n", " 'input': >,\n", " 'isinstance': ,\n", " 'issubclass': ,\n", " 'iter': ,\n", " 'aiter': ,\n", " 'len': ,\n", " 'locals': ,\n", " 'max': ,\n", " 'min': ,\n", " 'next': ,\n", " 'anext': ,\n", " 'oct': ,\n", " 'ord': ,\n", " 'pow': ,\n", " 'print': ,\n", " 'repr': ,\n", " 'round': ,\n", " 'setattr': ,\n", " 'sorted': ,\n", " 'sum': ,\n", " 'vars': ,\n", " 'None': None,\n", " 'Ellipsis': Ellipsis,\n", " 'NotImplemented': NotImplemented,\n", " 'False': False,\n", " 'True': True,\n", " 'bool': bool,\n", " 'memoryview': memoryview,\n", " 'bytearray': bytearray,\n", " 'bytes': bytes,\n", " 'classmethod': classmethod,\n", " 'complex': complex,\n", " 'dict': dict,\n", " 'enumerate': enumerate,\n", " 'filter': filter,\n", " 'float': float,\n", " 'frozenset': frozenset,\n", " 'property': property,\n", " 'int': int,\n", " 'list': list,\n", " 'map': map,\n", " 'object': object,\n", " 'range': range,\n", " 'reversed': reversed,\n", " 'set': set,\n", " 'slice': slice,\n", " 'staticmethod': staticmethod,\n", " 'str': str,\n", " 'super': super,\n", " 'tuple': tuple,\n", " 'type': type,\n", " 'zip': zip,\n", " '__debug__': True,\n", " 'BaseException': BaseException,\n", " 'Exception': Exception,\n", " 'TypeError': TypeError,\n", " 'StopAsyncIteration': StopAsyncIteration,\n", " 'StopIteration': StopIteration,\n", " 'GeneratorExit': GeneratorExit,\n", " 'SystemExit': SystemExit,\n", " 'KeyboardInterrupt': KeyboardInterrupt,\n", " 'ImportError': ImportError,\n", " 'ModuleNotFoundError': ModuleNotFoundError,\n", " 'OSError': OSError,\n", " 'EnvironmentError': OSError,\n", " 'IOError': OSError,\n", " 'EOFError': EOFError,\n", " 'RuntimeError': RuntimeError,\n", " 'RecursionError': RecursionError,\n", " 'NotImplementedError': NotImplementedError,\n", " 'NameError': NameError,\n", " 'UnboundLocalError': UnboundLocalError,\n", " 'AttributeError': AttributeError,\n", " 'SyntaxError': SyntaxError,\n", " 'IndentationError': IndentationError,\n", " 'TabError': TabError,\n", " 'LookupError': LookupError,\n", " 'IndexError': IndexError,\n", " 'KeyError': KeyError,\n", " 'ValueError': ValueError,\n", " 'UnicodeError': UnicodeError,\n", " 'UnicodeEncodeError': UnicodeEncodeError,\n", " 'UnicodeDecodeError': UnicodeDecodeError,\n", " 'UnicodeTranslateError': UnicodeTranslateError,\n", " 'AssertionError': AssertionError,\n", " 'ArithmeticError': ArithmeticError,\n", " 'FloatingPointError': FloatingPointError,\n", " 'OverflowError': OverflowError,\n", " 'ZeroDivisionError': ZeroDivisionError,\n", " 'SystemError': SystemError,\n", " 'ReferenceError': ReferenceError,\n", " 'MemoryError': MemoryError,\n", " 'BufferError': BufferError,\n", " 'Warning': Warning,\n", " 'UserWarning': UserWarning,\n", " 'EncodingWarning': EncodingWarning,\n", " 'DeprecationWarning': DeprecationWarning,\n", " 'PendingDeprecationWarning': PendingDeprecationWarning,\n", " 'SyntaxWarning': SyntaxWarning,\n", " 'RuntimeWarning': RuntimeWarning,\n", " 'FutureWarning': FutureWarning,\n", " 'ImportWarning': ImportWarning,\n", " 'UnicodeWarning': UnicodeWarning,\n", " 'BytesWarning': BytesWarning,\n", " 'ResourceWarning': ResourceWarning,\n", " 'ConnectionError': ConnectionError,\n", " 'BlockingIOError': BlockingIOError,\n", " 'BrokenPipeError': BrokenPipeError,\n", " 'ChildProcessError': ChildProcessError,\n", " 'ConnectionAbortedError': ConnectionAbortedError,\n", " 'ConnectionRefusedError': ConnectionRefusedError,\n", " 'ConnectionResetError': ConnectionResetError,\n", " 'FileExistsError': FileExistsError,\n", " 'FileNotFoundError': FileNotFoundError,\n", " 'IsADirectoryError': IsADirectoryError,\n", " 'NotADirectoryError': NotADirectoryError,\n", " 'InterruptedError': InterruptedError,\n", " 'PermissionError': PermissionError,\n", " 'ProcessLookupError': ProcessLookupError,\n", " 'TimeoutError': TimeoutError,\n", " 'open': ,\n", " 'copyright': Copyright (c) 2001-2022 Python Software Foundation.\n", " All Rights Reserved.\n", " \n", " Copyright (c) 2000 BeOpen.com.\n", " All Rights Reserved.\n", " \n", " Copyright (c) 1995-2001 Corporation for National Research Initiatives.\n", " All Rights Reserved.\n", " \n", " Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.\n", " All Rights Reserved.,\n", " 'credits': Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands\n", " for supporting Python development. See www.python.org for more information.,\n", " 'license': Type license() to see the full license text,\n", " 'help': Type help() for interactive help, or help(object) for help about object.,\n", " 'execfile': ,\n", " 'runfile': ,\n", " '__IPYTHON__': True,\n", " 'display': ,\n", " 'get_ipython': >},\n", " 'a': 666}" ] }, "execution_count": 324, "metadata": {}, "output_type": "execute_result" } ], "source": [ "context" ] }, { "cell_type": "code", "execution_count": 326, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "print(context['a'])" ] }, { "cell_type": "code", "execution_count": 327, "metadata": {}, "outputs": [], "source": [ "config_file = '''\n", "SRCDIR = '/tmp/src'\n", "DSTDIR = '/tmp/dst'\n", "INTERVAL = 3\n", "'''\n", "\n", "config = {}\n", "exec(config_file, config)" ] }, { "cell_type": "code", "execution_count": 329, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/tmp/src'" ] }, "execution_count": 329, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config['SRCDIR']" ] }, { "cell_type": "code", "execution_count": 330, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/tmp/dst'" ] }, "execution_count": 330, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config['DSTDIR']" ] }, { "cell_type": "code", "execution_count": 331, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 331, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config['INTERVAL']" ] } ], "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.7" } }, "nbformat": 4, "nbformat_minor": 4 }