{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 2022-01-17" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variables and Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comments vs Docstrings" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# this is a comment. it tries to explain that the variable 'blah' is assigned the value 42\n", "blah = 42 # this is the assignment" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "blah" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def add(l, r):\n", " 'add: takes two parameters, and return the sum of those'\n", " return l+r" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add(1, 2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "add: takes two parameters, and return the sum of those\n" ] } ], "source": [ "print(add.__doc__)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function add in module __main__:\n", "\n", "add(l, r)\n", " add: takes two parameters, and return the sum of those\n", "\n" ] } ], "source": [ "help(add)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Numbers" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 42 # int\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1.5 # float\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "complex" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = complex(3, 4)\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3+4j)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = True # boolean\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 == 1" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1 == 1 # operator precedence: a = (1 == 1)\n", "a" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'x' == 'u'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Integer (Ganze Zahl)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unlimited range ..." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4294967295" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "number = 2**32 - 1\n", "number" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4294967296" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "number += 1\n", "number" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551615" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "number = 2**64 - 1\n", "number" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "number += 1\n", "number" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "number = 2**64\n", "number" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1267650600228229401496703205376" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**100" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10**1000" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1234 == 1*10**3 + 2*10**2 + 3*10**1 + 4*10**0" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1234" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "-1234" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3735928559" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0xdeadbeef" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "147" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0b10010011" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Integer Numbers: Arithmetic" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Floor division: ``//``, Division without Rest" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3/2" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3//2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modulo: ``%``, Rest of Division" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10%4" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3%2 " ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "13%5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Some Methods" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'Mississippi'\n", "s.count('ss')" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'mississippi'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.lower()" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('ss')" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('ss', 3)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Mi', 'i', 'ippi']" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.split('ss')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(many more)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### String Formatting: f-Strings" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "firstname = 'Joerg'\n", "lastname = 'Faschingbauer'\n", "address = 'Prankergasse 33'\n", "zip = '8020'" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Joerg Faschingbauer Prankergasse 33 8020\n" ] } ], "source": [ "print(firstname, lastname, address, zip)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Joerg,Faschingbauer,Prankergasse 33,8020\n" ] } ], "source": [ "print(firstname, lastname, address, zip, sep=',')" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Joerg Faschingbauer; Prankergasse 33, 8020\n" ] } ], "source": [ "text = firstname + ' ' + lastname + '; ' + address + ', ' + zip\n", "print(text)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg Faschingbauer; Prankergasse 33, 8020'" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text = '{} {}; {}, {}'.format(firstname, lastname, address, zip)\n", "text" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg Faschingbauer; Prankergasse 33, 8020'" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text = '{a} {b}; {c}, {d}'.format(a=firstname, b=lastname, c=address, d=zip)\n", "text" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg Faschingbauer; Prankergasse 33, 8020'" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text = '{firstname} {lastname}; {address}, {zip}'.format(firstname=firstname, lastname=lastname, address=address, zip=zip)\n", "text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**f-Strings to the rescue**" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg Faschingbauer; Prankergasse 33, 8020'" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f'{firstname} {lastname}; {address}, {zip}'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Datatype Conversions" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'42'" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 42\n", "s = str(i)\n", "s" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '666'\n", "i = int(s)\n", "i" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " invalid literal for int() with base 10: 'joerg'\n" ] } ], "source": [ "try:\n", " int('joerg') # raises an exception\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3735928559" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('0xdeadbeef', 16)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('0b0101', 2)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('0101', 2)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12.3" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float('12.3')" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(1)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(42)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "teifl!\n" ] } ], "source": [ "if 42: # bool(42)\n", " print('teifl!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Boolean" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Boolean operators: ``and``, ``or``, ``not``" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 == 2 and 3 == 3" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 == 2 or 3 == 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Short Circuit Evaluation**" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [ "def fun1():\n", " print('fun1')\n", " return False\n", "def fun2():\n", " print('fun2')\n", " return True" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "fun1\n" ] } ], "source": [ "value = fun1()" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "value" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "fun1\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fun1() and fun2()" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "fun2\n", "fun1\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fun2() and fun1()" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "fun1\n", "fun2\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fun1() or fun2()" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not True" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not False" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not not False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More about Strings" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'das ist ein string'" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'das ist ein string'" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'das ist ein string'" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"das ist ein string\"" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len('das ist ein string')" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"was'n das fuer ein string?\"" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"was'n das fuer ein string?\"" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'er sagte \"DEPP\" zu mir!!!'" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'er sagte \"DEPP\" zu mir!!!'" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'er sagte \"DEPP\" zu mir, was\\'n das fuer\\'n Depp?!'" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"er sagte \\\"DEPP\\\" zu mir, was'n das fuer'n Depp?!\"" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "first line\n", "second line\n" ] } ], "source": [ "print('first line\\nsecond line') # linefeed" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "linet\n" ] } ], "source": [ "print('first\\rline') # carriage return" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Raw String (aka DOS path names, Regular Expressions)**" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\Dokumente\n", "euer ordner\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "<>:1: SyntaxWarning: invalid escape sequence '\\D'\n", "<>:1: SyntaxWarning: invalid escape sequence '\\D'\n", "/tmp/ipykernel_258993/837361033.py:1: SyntaxWarning: invalid escape sequence '\\D'\n", " windows_path = 'C:\\Dokumente\\neuer ordner'\n" ] } ], "source": [ "windows_path = 'C:\\Dokumente\\neuer ordner'\n", "print(windows_path)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\Dokumente\\neuer ordner\n" ] } ], "source": [ "windows_path = r'C:\\Dokumente\\neuer ordner'\n", "print(windows_path)" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [], "source": [ "line = 'Faschingbauer ; Joerg ; 1037190666 '" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [], "source": [ "regex_str = r'^\\s*(.+)\\s*;\\s*(.+)\\s*;\\s*(.+)\\s*$'" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [], "source": [ "import re\n", "match = re.search(regex_str, line)" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Faschingbauer ; Joerg ; 1037190666 '" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match.group(0)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Faschingbauer '" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match.group(1)" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg '" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match.group(2)" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1037190666 '" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match.group(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Multiline Strings**" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [], "source": [ "def f():\n", " '''This function is rather stupid.\n", " Rather than letting the user write a simple\n", " True/False, it calims that it is much better\n", " to call the function instead.'''\n", " return True" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function f in module __main__:\n", "\n", "f()\n", " This function is rather stupid.\n", " Rather than letting the user write a simple\n", " True/False, it calims that it is much better\n", " to call the function instead.\n", "\n" ] } ], "source": [ "help(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Kontrollstrukturen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ``if``" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yaaay!!\n" ] } ], "source": [ "answer = 42\n", "if answer == 42:\n", " print('yaaay!!')" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bad\n" ] } ], "source": [ "answer = 1\n", "if answer == 42:\n", " print('yaaay!!')\n", "else:\n", " print('bad')" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yaaay!! (43)\n" ] } ], "source": [ "# possible answers: 41, 42, 43 -> elif\n", "\n", "answer = 43\n", "\n", "if answer == 41:\n", " print('yaaay!! (41)')\n", "elif answer == 42:\n", " print('yaaay!! (42)')\n", "elif answer == 43:\n", " print('yaaay!! (43)')\n", "else:\n", " print('bad')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2022-01-18" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Miscellanea" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Braces: Single Element Tuples?" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['eins', 2, 3.0]" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ['eins', 2, 3.0]\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Mutability**" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['eins', 2, 3.0, 4]" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append(4)\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Immutability**" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'lower'" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'lower'\n", "s" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'LOWER'" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.upper()" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'lower'" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('eins', 2, 3.0)" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = ('eins', 2, 3.0) # tuple\n", "t" ] }, { "cell_type": "code", "execution_count": 96, "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": "markdown", "metadata": {}, "source": [ "**Single element tuples**" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'eins'" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = ('eins')\n", "t" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Braces are special" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2*5%3" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2*5)%3" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2*(5%3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Single element tuples, done right**" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('eins',)" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = ('eins',)\n", "t" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ranges -> ``range()`` (``hour_of_day``)" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Good morning, \n" ] } ], "source": [ "hour_of_day = 2\n", "if 0 <= hour_of_day <= 9:\n", " print('Good morning, ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Range**" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n" ] } ], "source": [ "for number in range(0,4):\n", " print(number)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Good morning, \n" ] } ], "source": [ "hour_of_day = 2\n", "if 0 <= hour_of_day < 10:\n", " print('Good morning, ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``while`` Loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sum of number 0 .. 10 (inclusive)" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [], "source": [ "# 0 + 1 + 2 + ... + 10" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "55\n" ] } ], "source": [ "sum = 0\n", "num = 0\n", "while num <= 10:\n", " sum += num\n", " num += 1\n", "print(sum)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "55\n" ] } ], "source": [ "sum = 0\n", "for num in range(0, 11):\n", " sum += num\n", "print(sum)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ``break`` and ``continue``" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "33" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randrange(100) # evenly distributed random number in range(0, 100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Search needle in infinite haystack**" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay!!\n", "found\n" ] } ], "source": [ "# search for needle in haystack\n", "needle = 42\n", "while True:\n", " number = random.randrange(100)\n", " if number == needle:\n", " print('yay!!')\n", " break\n", "print('found')" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay!!\n", "found\n" ] } ], "source": [ "# search for needle in haystack\n", "needle = 42\n", "while True:\n", " number = random.randrange(100)\n", " if number != needle:\n", " continue\n", " else:\n", " print('yay!!')\n", " break\n", " \n", "print('found')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**And finite haystacks?**" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "not found\n" ] } ], "source": [ "needle = 42\n", "haystack_size = 100\n", "run = 0\n", "found = False\n", "\n", "while run < haystack_size:\n", " run += 1\n", " \n", " number = random.randrange(100)\n", " if number == needle:\n", " found = True\n", " break\n", "\n", "if found:\n", " print('found')\n", "else:\n", " print('not found')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``while`` and ``else``" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "not found\n" ] } ], "source": [ "needle = 42\n", "haystack_size = 100\n", "run = 0\n", "\n", "while run < haystack_size:\n", " run += 1\n", " \n", " number = random.randrange(100)\n", " if number == needle:\n", " print('found')\n", " break\n", "else:\n", " print('not found')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``yield``, Generator, Iteration Protocol" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for num in range(3):\n", " print(num)" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for num in [0,1,2]:\n", " print(num)" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 3)" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = range(3)\n", "r # wtf?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Iterator protocol**" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = range(3)\n", "it = iter(r)\n", "it" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num = next(it)\n", "num" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "try:\n", " next(it)\n", "except StopIteration as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for num in range(3): # <-- iterator protocol\n", " print(num)" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [], "source": [ "l = [0,1,2]" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for num in l:\n", " print(num)" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [], "source": [ "it = iter(l)" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "it" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [], "source": [ "try:\n", " next(it)\n", "except StopIteration:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sequential Datatypes" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "b\n", "c\n" ] } ], "source": [ "s = 'abc'\n", "for unicode_code_point in s:\n", " print(unicode_code_point)" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'b'" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[1]" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "zwei\n", "3.0\n", "[0, 1, 2, 3]\n" ] } ], "source": [ "l = [1, 'zwei', 3.0, [0,1,2,3]]\n", "for element in l:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3]" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Concatenation**" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [], "source": [ "l_new = [5, 6, 'seven']" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'zwei', 3.0, [0, 1, 2, 3], 5, 6, 'seven']" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l + l_new" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Original lists remain unmodified" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'zwei', 3.0, [0, 1, 2, 3]]" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 6, 'seven']" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l_new" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As opposed to ``+=``" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [], "source": [ "l += l_new" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'zwei', 3.0, [0, 1, 2, 3], 5, 6, 'seven']" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mixing types?" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [], "source": [ "l = [1,2,3]\n", "s = 'abc'" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [], "source": [ "l += s" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'a', 'b', 'c']" ] }, "execution_count": 144, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sequence Membership" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [], "source": [ "l = [100, 2, 3, 6]" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay\n" ] } ], "source": [ "if 100 in l:\n", " print('yay')" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "100 in l" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6 in l" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2000 in l" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 2000 in l" ] }, { "cell_type": "code", "execution_count": 151, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2000 not in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compound Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ``list`` (mutable)" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [], "source": [ "l = [] # empty list\n", "l = list() # empty list" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = list('abc')\n", "l" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['abc']" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ['abc']\n", "l" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ['a', 'b', 'c']\n", "l" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'D']" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append('D')\n", "l" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'D', 'e', 'f']" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.extend(['e', 'f'])\n", "l" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'D', 'e', 'f', 'g', 'h']" ] }, "execution_count": 158, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l += ['g', 'h']\n", "l" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'D', 'e', 'f', 'g', 'h', 'i', 'j', 'k']" ] }, "execution_count": 159, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l += 'ijk'\n", "l" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'int' object is not iterable\n" ] } ], "source": [ "try:\n", " l += 666\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['b', 'c', 'D', 'e', 'f', 'g', 'h', 'i', 'j', 'k']" ] }, "execution_count": 161, "metadata": {}, "output_type": "execute_result" } ], "source": [ "del l[0]\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ``tuple`` (immutable)" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [], "source": [ "t = () # empty tuple\n", "t = tuple() # empty tuple" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 'drei')" ] }, "execution_count": 163, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = (1, 2, 'drei')\n", "t" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('a', 'b', 'c')" ] }, "execution_count": 164, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = tuple('abc')\n", "t" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'int' object is not iterable\n" ] } ], "source": [ "try:\n", " t = tuple(666)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [], "source": [ "t = (1,2,3)" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'tuple' object has no attribute 'append'\n" ] } ], "source": [ "try:\n", " t.append(666)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ``dict`` (mutable)" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [], "source": [ "d = {} # empty dictionary\n", "d = dict() # empty dictionary" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [], "source": [ "d = {\n", " 1: 'one',\n", " 2: 'two',\n", "}" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Lookup** (in constant time - independent of number of elements)" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one'" ] }, "execution_count": 171, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[1]" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 172, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in d" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 173, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[4] = 'vier'\n", "4 in d" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'one', 2: 'two', 4: 'vier'}" ] }, "execution_count": 174, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'one', 2: 'two', 4: 'four'}" ] }, "execution_count": 175, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[4] = 'four'\n", "d" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 176, "metadata": {}, "output_type": "execute_result" } ], "source": [ "del d[4]\n", "4 in d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ``set`` (mutable)" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [], "source": [ "s = {} # attention: empty DICTIONARY\n", "s = set() # empty set" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3}" ] }, "execution_count": 178, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = {1, 2, 3}\n", "s" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a', 'b', 'c'}" ] }, "execution_count": 179, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set('abc')\n", "s" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 180, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'b' in s" ] }, { "cell_type": "code", "execution_count": 181, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 in s" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a', 'b', 'c'}" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.add('b')\n", "s" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a', 'b', 'c', 'd'}" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.add('d')\n", "s" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'b', 'c', 'd'}" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.remove('a')\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**set operation** (Mengenlehre)" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [], "source": [ "s1 = {1, 2, 3}\n", "s2 = {2, 3, 4}" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4}" ] }, "execution_count": 186, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 | s2 # union" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2, 3}" ] }, "execution_count": 187, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 & s2 # intersection" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1}" ] }, "execution_count": 188, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 - s2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Not ordered**" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [], "source": [ "s = set()" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [], "source": [ "s.add(1000)\n", "s.add(10)\n", "s.add(1)" ] }, { "cell_type": "code", "execution_count": 191, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1000\n", "1\n", "10\n" ] } ], "source": [ "for i in s:\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Why Index Based Iteration is not Always the Best Way to Iterate" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [], "source": [ "l = [10, 20, 32, 2, 6]" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "20\n", "32\n", "2\n", "6\n" ] } ], "source": [ "for index in range(len(l)): # indices: 0,1,2,3,4\n", " print(l[index])" ] }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "20\n", "32\n", "2\n", "6\n" ] } ], "source": [ "for element in l:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### How to get Indexes if There are None? (``enumerate()``, and *Tuple Unpacking*)" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gunnar\n", "Liam\n", "Joerg\n" ] } ], "source": [ "names = ['Gunnar', 'Liam', 'Joerg']\n", "for name in names:\n", " print(name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And Positions?" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Gunnar\n", "1 Liam\n", "2 Joerg\n" ] } ], "source": [ "i = 0\n", "for name in names:\n", " print(i, name)\n", " i += 1" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Gunnar\n", "1 Liam\n", "2 Joerg\n" ] } ], "source": [ "for i in range(len(names)):\n", " print(i, names[i])" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Gunnar\n", "1 Liam\n", "2 Joerg\n" ] } ], "source": [ "for i, name in enumerate(names): # tuple unpacking\n", " print(i, name)" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 'Gunnar')\n", "(1, 'Liam')\n", "(2, 'Joerg')\n" ] } ], "source": [ "for element in enumerate(names):\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Gunnar\n", "1 Liam\n", "2 Joerg\n" ] } ], "source": [ "for element in enumerate(names):\n", " i, name = element # tuple unpacking\n", " print(i, name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [], "source": [ "def maximum(a, b):\n", " '''Diese extrem komplexe Funktion berechnet das Maximumum ihrer beiden Parameter.\n", " Ganz zum Unterschied der built-in Funktion max() nimmt sie nur zwei Parameter und\n", " nicht beliebig viele.'''\n", " if a < b:\n", " return b\n", " else:\n", " return a" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 202, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max = maximum(2, 3)\n", "max" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 203, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variable = 100\n", "type(variable)" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 204, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variable = [1, 2, 3]\n", "type(variable)" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [], "source": [ "variable2 = variable" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 206, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variable.append(4)\n", "variable" ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 207, "metadata": {}, "output_type": "execute_result" } ], "source": [ "variable2" ] }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 208, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(maximum)" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 209, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(3, 4)" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 210, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum2 = maximum\n", "type(maximum2)" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 211, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum2(4, 5)" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "module" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sys\n", "type(sys)" ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 213, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(2, 1)" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'bbb'" ] }, "execution_count": 214, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum('aaa', 'bbb')" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " '<' not supported between instances of 'int' and 'str'\n" ] } ], "source": [ "try:\n", " maximum(1, '2')\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Default Parameters" ] }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [], "source": [ "def greet(phrase, mrmrs, name):\n", " print(f'{phrase}, {mrmrs} {name}')" ] }, { "cell_type": "code", "execution_count": 217, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Good morning, Mr. Joerg\n" ] } ], "source": [ "greet('Good morning', 'Mr.', 'Joerg')" ] }, { "cell_type": "code", "execution_count": 218, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Good morning, Mr. Liam\n" ] } ], "source": [ "greet('Good morning', 'Mr.', 'Liam')" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " greet() missing 2 required positional arguments: 'mrmrs' and 'name'\n" ] } ], "source": [ "try:\n", " greet('Good morning')\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [], "source": [ "def greet(phrase, mrmrs='Mr.', name='Joerg'):\n", " print(f'{phrase}, {mrmrs} {name}')" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Good morning, Mr. Joerg\n" ] } ], "source": [ "greet('Good morning')" ] }, { "cell_type": "code", "execution_count": 222, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Good morning, XXX. Joerg\n" ] } ], "source": [ "greet('Good morning', 'XXX.')" ] }, { "cell_type": "code", "execution_count": 223, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Good morning, Liam Joerg\n" ] } ], "source": [ "greet('Good morning', 'Liam')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Keyword Arguments" ] }, { "cell_type": "code", "execution_count": 224, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Good evening, Mr. Liam\n" ] } ], "source": [ "greet('Good evening', 'Mr.', 'Liam')" ] }, { "cell_type": "code", "execution_count": 225, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Seas, Mr. Liam\n" ] } ], "source": [ "greet(mrmrs='Mr.', name='Liam', phrase='Seas')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More on Lists" ] }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 'eins']" ] }, "execution_count": 226, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [3, 2, 'eins']\n", "l" ] }, { "cell_type": "code", "execution_count": 227, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 'eins', 4]" ] }, "execution_count": 227, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append(4)\n", "l" ] }, { "cell_type": "code", "execution_count": 228, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 'eins', 4, 5]" ] }, "execution_count": 228, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 = l\n", "l.append(5)\n", "l" ] }, { "cell_type": "code", "execution_count": 229, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 'eins', 4, 5]" ] }, "execution_count": 229, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 'eins', 4, 5, 6, 7]" ] }, "execution_count": 230, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.extend([6, 7])\n", "l" ] }, { "cell_type": "code", "execution_count": 231, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 'eins', 4, 5, 6, 7, 'a', 'b', 'c']" ] }, "execution_count": 231, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.extend('abc')\n", "l" ] }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'int' object is not iterable\n" ] } ], "source": [ "try:\n", " l.extend(666)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['minus eins', 3, 2, 'eins', 4, 5, 6, 7, 'a', 'b', 'c']" ] }, "execution_count": 233, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.insert(0, 'minus eins')\n", "l" ] }, { "cell_type": "code", "execution_count": 234, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['minus eins', 3, 2, 'vor dem einser', 'eins', 4, 5, 6, 7, 'a', 'b', 'c']" ] }, "execution_count": 234, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.insert(3, 'vor dem einser')\n", "l" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [], "source": [ "del l[3]" ] }, { "cell_type": "code", "execution_count": 236, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['minus eins', 3, 2, 'eins', 4, 5, 6, 7, 'a', 'b', 'c']" ] }, "execution_count": 236, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 237, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 237, "metadata": {}, "output_type": "execute_result" } ], "source": [ "elem = l[4]\n", "elem" ] }, { "cell_type": "code", "execution_count": 238, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['minus eins', 3, 2, 'eins', 4, 5, 6, 7, 'a', 'b', 'c']" ] }, "execution_count": 238, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 239, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 239, "metadata": {}, "output_type": "execute_result" } ], "source": [ "elem = l.pop(4)\n", "elem" ] }, { "cell_type": "code", "execution_count": 240, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['minus eins', 3, 2, 'eins', 5, 6, 7, 'a', 'b', 'c']" ] }, "execution_count": 240, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``sorted()`` etc. wants non-mixed lists" ] }, { "cell_type": "code", "execution_count": 241, "metadata": {}, "outputs": [], "source": [ "l = [1, 2.3, 100, 10, 5]" ] }, { "cell_type": "code", "execution_count": 242, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2.3, 5, 10, 100]" ] }, "execution_count": 242, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(l)" ] }, { "cell_type": "code", "execution_count": 243, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2.3, 100, 10, 5]" ] }, "execution_count": 243, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 244, "metadata": {}, "outputs": [], "source": [ "l.sort()" ] }, { "cell_type": "code", "execution_count": 245, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2.3, 5, 10, 100]" ] }, "execution_count": 245, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List Comprehensions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Problem: transform [1,2,3,4] into [1,4,9,16]" ] }, { "cell_type": "code", "execution_count": 246, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "numbers = [1, 2, 3, 4]\n", "square_numbers = []\n", "for n in numbers:\n", " square_numbers.append(n**2)\n", "for n in square_numbers:\n", " print(n)" ] }, { "cell_type": "code", "execution_count": 247, "metadata": {}, "outputs": [], "source": [ "def squares(numbers):\n", " ret = []\n", " for n in numbers:\n", " ret.append(n**2)\n", " return ret" ] }, { "cell_type": "code", "execution_count": 248, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "numbers = [1, 2, 3, 4]\n", "square_numbers = squares(numbers)\n", "for n in square_numbers:\n", " print(n)" ] }, { "cell_type": "code", "execution_count": 249, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "numbers = [1, 2, 3, 4]\n", "square_numbers = [n**2 for n in numbers]\n", "for n in square_numbers:\n", " print(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Generator Expressions**" ] }, { "cell_type": "code", "execution_count": 250, "metadata": {}, "outputs": [], "source": [ "def squares_generator(numbers):\n", " for n in numbers:\n", " yield n**2" ] }, { "cell_type": "code", "execution_count": 251, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "numbers = [1, 2, 3, 4]\n", "square_numbers = squares_generator(numbers)\n", "for n in square_numbers:\n", " print(n)" ] }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "numbers = [1, 2, 3, 4]\n", "square_numbers = (n**2 for n in numbers)\n", "for n in square_numbers:\n", " print(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More on Dictionaries" ] }, { "cell_type": "code", "execution_count": 253, "metadata": {}, "outputs": [], "source": [ "d = {'zero': 0,\n", " 'one': 1,\n", " 'two': 2}" ] }, { "cell_type": "code", "execution_count": 254, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 254, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['zero']" ] }, { "cell_type": "code", "execution_count": 255, "metadata": {}, "outputs": [], "source": [ "d['three'] = 3" ] }, { "cell_type": "code", "execution_count": 256, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'zero': 0, 'one': 1, 'two': 2, 'three': 3}" ] }, "execution_count": 256, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 257, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'seven'\n" ] } ], "source": [ "try:\n", " d['seven']\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 258, "metadata": {}, "output_type": "execute_result" } ], "source": [ "value = d.get('zero')\n", "value" ] }, { "cell_type": "code", "execution_count": 259, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "value = d.get('seven')\n", "print(value)" ] }, { "cell_type": "code", "execution_count": 260, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nix\n" ] } ], "source": [ "value = d.get('seven')\n", "if value:\n", " print(value)\n", "else:\n", " print('nix')\n", " d['seven'] = 7" ] }, { "cell_type": "code", "execution_count": 261, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 261, "metadata": {}, "output_type": "execute_result" } ], "source": [ "value = d.get('eight', 8)\n", "value" ] }, { "cell_type": "code", "execution_count": 262, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 262, "metadata": {}, "output_type": "execute_result" } ], "source": [ "value = d.setdefault('eight', 8)\n", "value" ] }, { "cell_type": "code", "execution_count": 263, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'seven': 7, 'eight': 8}" ] }, "execution_count": 263, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 264, "metadata": {}, "outputs": [], "source": [ "other_d = {\n", " 'three': 'drei',\n", " 'four': 4,\n", " 'nine': 'nove'\n", "}" ] }, { "cell_type": "code", "execution_count": 265, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'zero': 0,\n", " 'one': 1,\n", " 'two': 2,\n", " 'three': 'drei',\n", " 'seven': 7,\n", " 'eight': 8,\n", " 'four': 4,\n", " 'nine': 'nove'}" ] }, "execution_count": 265, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.update(other_d)\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iteration over Dictionaries" ] }, { "cell_type": "code", "execution_count": 266, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'zero': 0,\n", " 'one': 1,\n", " 'two': 2,\n", " 'three': 'drei',\n", " 'seven': 7,\n", " 'eight': 8,\n", " 'four': 4,\n", " 'nine': 'nove'}" ] }, "execution_count": 266, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 267, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "zero\n", "one\n", "two\n", "three\n", "seven\n", "eight\n", "four\n", "nine\n" ] } ], "source": [ "for element in d: # iteration over keys\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 268, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "zero\n", "one\n", "two\n", "three\n", "seven\n", "eight\n", "four\n", "nine\n" ] } ], "source": [ "for element in d.keys():\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 269, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "drei\n", "7\n", "8\n", "4\n", "nove\n" ] } ], "source": [ "for element in d.values():\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 270, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('zero', 0)\n", "('one', 1)\n", "('two', 2)\n", "('three', 'drei')\n", "('seven', 7)\n", "('eight', 8)\n", "('four', 4)\n", "('nine', 'nove')\n" ] } ], "source": [ "for element in d.items():\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 271, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Key: zero, Value: 0\n", "Key: one, Value: 1\n", "Key: two, Value: 2\n", "Key: three, Value: drei\n", "Key: seven, Value: 7\n", "Key: eight, Value: 8\n", "Key: four, Value: 4\n", "Key: nine, Value: nove\n" ] } ], "source": [ "for element in d.items():\n", " key = element[0]\n", " value = element[1]\n", " print(f'Key: {key}, Value: {value}')" ] }, { "cell_type": "code", "execution_count": 272, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Key: zero, Value: 0\n", "Key: one, Value: 1\n", "Key: two, Value: 2\n", "Key: three, Value: drei\n", "Key: seven, Value: 7\n", "Key: eight, Value: 8\n", "Key: four, Value: 4\n", "Key: nine, Value: nove\n" ] } ], "source": [ "for element in d.items():\n", " key, value = element # tuple unpacking\n", " print(f'Key: {key}, Value: {value}')" ] }, { "cell_type": "code", "execution_count": 273, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Key: zero, Value: 0\n", "Key: one, Value: 1\n", "Key: two, Value: 2\n", "Key: three, Value: drei\n", "Key: seven, Value: 7\n", "Key: eight, Value: 8\n", "Key: four, Value: 4\n", "Key: nine, Value: nove\n" ] } ], "source": [ "for key, value in d.items(): # tuple unpacking\n", " print(f'Key: {key}, Value: {value}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Building Dictionaries" ] }, { "cell_type": "code", "execution_count": 274, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 2, 2: 3}" ] }, "execution_count": 274, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = {} # empty dictionary\n", "s = set() # empty set\n", "d = {1: 2, 2: 3}\n", "d" ] }, { "cell_type": "code", "execution_count": 275, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 2, 2: 3}" ] }, "execution_count": 275, "metadata": {}, "output_type": "execute_result" } ], "source": [ "items = [(1, 2), (2, 3)]\n", "d = dict(items)\n", "d" ] }, { "cell_type": "code", "execution_count": 276, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "[(1, 2), (2, 3)]" ] }, "execution_count": 276, "metadata": {}, "output_type": "execute_result" } ], "source": [ "items = list(d.items())\n", "items" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More on Sets" ] }, { "cell_type": "code", "execution_count": 277, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 277, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = {} # empty dictionary\n", "type(s)" ] }, { "cell_type": "code", "execution_count": 278, "metadata": {}, "outputs": [], "source": [ "s = set() # empty set\n", "s = {1, 2, 3}" ] }, { "cell_type": "code", "execution_count": 279, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a', 'b', 'c'}" ] }, "execution_count": 279, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set('abc')\n", "s" ] }, { "cell_type": "code", "execution_count": 280, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5}" ] }, "execution_count": 280, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set([3, 4, 2, 1, 5])\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Miscellaneous String Methods" ] }, { "cell_type": "code", "execution_count": 281, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 281, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = ' \\n\\t \\r' # only whitespace\n", "s.isspace()" ] }, { "cell_type": "code", "execution_count": 282, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 282, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'abc'\n", "s.isalpha()" ] }, { "cell_type": "code", "execution_count": 283, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 283, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'12345'.isdigit()" ] }, { "cell_type": "code", "execution_count": 284, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 284, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'buchhaltung.csv'.endswith('.csv')" ] }, { "cell_type": "code", "execution_count": 285, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 285, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'buchhaltung.csv'.startswith('buch')" ] }, { "cell_type": "code", "execution_count": 286, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' OOOO '" ] }, "execution_count": 286, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'OOOO'.center(100)" ] }, { "cell_type": "code", "execution_count": 287, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 287, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Joerg'.isupper()" ] }, { "cell_type": "code", "execution_count": 288, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 288, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Mississippi'.count('ss')" ] }, { "cell_type": "code", "execution_count": 289, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 289, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Mississippi'.find('ss')" ] }, { "cell_type": "code", "execution_count": 290, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 290, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Mississippi'.find('ss', 3)" ] }, { "cell_type": "code", "execution_count": 291, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 291, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Mississippi'.find('zz')" ] }, { "cell_type": "code", "execution_count": 292, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "traurig\n" ] } ], "source": [ "if 'Mississippi'.find('zz') == -1:\n", " print('traurig')\n", "else:\n", " print('yay')" ] }, { "cell_type": "code", "execution_count": 293, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " substring not found\n" ] } ], "source": [ "try:\n", " 'Mississippi'.index('zz')\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 294, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['jfasch',\n", " 'x',\n", " '1000',\n", " '1000',\n", " 'Joerg Faschingbauer',\n", " '/home/jfasch',\n", " '/bin/bash']" ] }, "execution_count": 294, "metadata": {}, "output_type": "execute_result" } ], "source": [ "row = 'jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash'\n", "fields = row.split(':')\n", "fields" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuple unpacking:" ] }, { "cell_type": "code", "execution_count": 295, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'jfasch'" ] }, "execution_count": 295, "metadata": {}, "output_type": "execute_result" } ], "source": [ "user, passwd, uid, gid, descr, home, shell = row.split(':')\n", "user" ] }, { "cell_type": "code", "execution_count": 296, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello-world-und-sonst-alle'" ] }, "execution_count": 296, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'-'.join(['hello', 'world', 'und', 'sonst', 'alle'])" ] }, { "cell_type": "code", "execution_count": 297, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'irgendwas'" ] }, "execution_count": 297, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' \\n \\t irgendwas \\r'.strip()" ] }, { "cell_type": "code", "execution_count": 298, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'irgendwas \\r'" ] }, "execution_count": 298, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' \\n \\t irgendwas \\r'.lstrip()" ] }, { "cell_type": "code", "execution_count": 299, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' \\n \\t irgendwas'" ] }, "execution_count": 299, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' \\n \\t irgendwas \\r'.rstrip()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``with``: Context Managers" ] }, { "cell_type": "code", "execution_count": 300, "metadata": {}, "outputs": [], "source": [ "def count_bytes(filename):\n", " f = open(filename)\n", " content = f.read() # <-- exception might be raised\n", " f.close() # <-- might not be reached, for that reason\n", " return len(content)" ] }, { "cell_type": "code", "execution_count": 301, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2706" ] }, "execution_count": 301, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nbytes = count_bytes('/etc/passwd')\n", "nbytes" ] }, { "cell_type": "code", "execution_count": 302, "metadata": {}, "outputs": [], "source": [ "def count_bytes(filename):\n", " f = open(filename)\n", " try:\n", " content = f.read() # <-- exception might be raised\n", " except Exception:\n", " f.close()\n", " raise\n", " f.close() # <-- might not be reached, for that reason\n", " return len(content)" ] }, { "cell_type": "code", "execution_count": 303, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2706" ] }, "execution_count": 303, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nbytes = count_bytes('/etc/passwd')\n", "nbytes" ] }, { "cell_type": "code", "execution_count": 304, "metadata": {}, "outputs": [], "source": [ "def count_bytes(filename):\n", " with open(filename) as f:\n", " content = f.read() # <-- exception might be raised\n", " return len(content)" ] } ], "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.12.1" } }, "nbformat": 4, "nbformat_minor": 4 }