{ "cells": [ { "cell_type": "markdown", "id": "af63b5be", "metadata": {}, "source": [ "# 2023-03-13" ] }, { "cell_type": "code", "execution_count": 1, "id": "67f3ce4a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "fuenf\n" ] } ], "source": [ "l = [1,2,3,4, 'fuenf']\n", "for element in l:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 2, "id": "521d3dc9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 'fuenf']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 3, "id": "e552d00f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(l))" ] }, { "cell_type": "code", "execution_count": 4, "id": "07bc7023", "metadata": {}, "outputs": [], "source": [ "i = 42" ] }, { "cell_type": "code", "execution_count": 5, "id": "77776072", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 6, "id": "8f80d0ad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(i))" ] }, { "cell_type": "code", "execution_count": 7, "id": "a68f6528", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo\n" ] } ], "source": [ "exec('print(\"hallo\")')" ] }, { "cell_type": "markdown", "id": "fd157afe", "metadata": {}, "source": [ "## Syntax" ] }, { "cell_type": "markdown", "id": "5a24a4aa", "metadata": {}, "source": [ "### Comment vs Docstring" ] }, { "cell_type": "code", "execution_count": 8, "id": "fe61a0b0", "metadata": {}, "outputs": [], "source": [ "# das ist ein kommentar" ] }, { "cell_type": "code", "execution_count": 9, "id": "5ba67e4a", "metadata": {}, "outputs": [], "source": [ "i = 42 # das ist auch ein kommentar" ] }, { "cell_type": "code", "execution_count": 10, "id": "58c93e04", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 11, "id": "7513129a", "metadata": {}, "outputs": [], "source": [ "# das ist die funktion foo, die hoechst komplex ist und \n", "# die parameter a und b nimmt, die idealerweise integers sein sollten\n", "def foo(a, b):\n", " return a+b" ] }, { "cell_type": "code", "execution_count": 12, "id": "11c18c06", "metadata": {}, "outputs": [], "source": [ "def foo(a, b):\n", " 'das ist die funktion foo, die hoechst komplex ist und die parameter a und b nimmt, die idealerweise integers sein sollten'\n", " return a+b" ] }, { "cell_type": "code", "execution_count": 13, "id": "74c8c907", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'das ist die funktion foo, die hoechst komplex ist und die parameter a und b nimmt, die idealerweise integers sein sollten'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo.__doc__" ] }, { "cell_type": "code", "execution_count": 14, "id": "e5a18139", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function foo in module __main__:\n", "\n", "foo(a, b)\n", " das ist die funktion foo, die hoechst komplex ist und die parameter a und b nimmt, die idealerweise integers sein sollten\n", "\n" ] } ], "source": [ "help(foo)" ] }, { "cell_type": "code", "execution_count": 15, "id": "24995482", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo(1, 2)" ] }, { "cell_type": "code", "execution_count": 16, "id": "bc3c661e", "metadata": {}, "outputs": [], "source": [ "add = foo" ] }, { "cell_type": "code", "execution_count": 17, "id": "ca1df00e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add(2, 3)" ] }, { "cell_type": "code", "execution_count": 18, "id": "14d875af", "metadata": {}, "outputs": [], "source": [ "add = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 19, "id": "8fdcd3e0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'list' object is not callable \n" ] } ], "source": [ "try:\n", " add(2,3)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "markdown", "id": "aceb3210", "metadata": {}, "source": [ "### Tuple Unpacking" ] }, { "cell_type": "code", "execution_count": 20, "id": "86d71faa", "metadata": {}, "outputs": [], "source": [ "l = [1, 'zwei', 3.0]" ] }, { "cell_type": "code", "execution_count": 21, "id": "8daf7620", "metadata": {}, "outputs": [], "source": [ "a, b, c = l" ] }, { "cell_type": "code", "execution_count": 22, "id": "080d1479", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 23, "id": "1b1fc75c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'zwei'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 24, "id": "3b095e2c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "code", "execution_count": 25, "id": "167c3ab8", "metadata": {}, "outputs": [], "source": [ "a, *rest = l" ] }, { "cell_type": "code", "execution_count": 26, "id": "73442da2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 27, "id": "58bd04dd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['zwei', 3.0]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rest" ] }, { "cell_type": "code", "execution_count": 28, "id": "d14f0b8a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "too many values to unpack (expected 2) \n" ] } ], "source": [ "try:\n", " a, b = l\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 29, "id": "0703bca4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1, 2" ] }, { "cell_type": "code", "execution_count": 30, "id": "105d6d4c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 'zwei')" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b" ] }, { "cell_type": "code", "execution_count": 31, "id": "bd37a9bd", "metadata": {}, "outputs": [], "source": [ "tmp = a\n", "a = b\n", "b = tmp" ] }, { "cell_type": "code", "execution_count": 32, "id": "9459d1f3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('zwei', 1)" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b" ] }, { "cell_type": "code", "execution_count": 33, "id": "4447d50e", "metadata": {}, "outputs": [], "source": [ "a, b = b, a" ] }, { "cell_type": "markdown", "id": "4f2932d6", "metadata": {}, "source": [ "### Mutable? Immutable?" ] }, { "cell_type": "code", "execution_count": 34, "id": "2feff260", "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 35, "id": "f2d0aed2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140480285361680" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 36, "id": "520a4621", "metadata": {}, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 37, "id": "b0b918e8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140480285361680" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 38, "id": "9aa20939", "metadata": {}, "outputs": [], "source": [ "a += 1" ] }, { "cell_type": "code", "execution_count": 39, "id": "e859af5d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 40, "id": "b004ade9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "43" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "id": "144a5868", "metadata": {}, "source": [ "## Integers" ] }, { "cell_type": "code", "execution_count": 41, "id": "3fcfb80d", "metadata": {}, "outputs": [], "source": [ "i = 2**64 - 1" ] }, { "cell_type": "code", "execution_count": 42, "id": "a74d53fc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551615" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 43, "id": "290cd8cd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0xffffffffffffffff'" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(i)" ] }, { "cell_type": "code", "execution_count": 44, "id": "fe373ad0", "metadata": {}, "outputs": [], "source": [ "i += 1" ] }, { "cell_type": "code", "execution_count": 45, "id": "5a84a029", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 46, "id": "91c332a6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0x10000000000000000'" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(i)" ] }, { "cell_type": "code", "execution_count": 47, "id": "8dbed731", "metadata": {}, "outputs": [], "source": [ "i += 1" ] }, { "cell_type": "code", "execution_count": 48, "id": "11d4383a", "metadata": {}, "outputs": [], "source": [ "i = 2**100" ] }, { "cell_type": "code", "execution_count": 49, "id": "2f1af2c9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1267650600228229401496703205376" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 50, "id": "3bd3fe3b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "19950631168807583848837421626835850838234968318861924548520089498529438830221946631919961684036194597899331129423209124271556491349413781117593785932096323957855730046793794526765246551266059895520550086918193311542508608460618104685509074866089624888090489894838009253941633257850621568309473902556912388065225096643874441046759871626985453222868538161694315775629640762836880760732228535091641476183956381458969463899410840960536267821064621427333394036525565649530603142680234969400335934316651459297773279665775606172582031407994198179607378245683762280037302885487251900834464581454650557929601414833921615734588139257095379769119277800826957735674444123062018757836325502728323789270710373802866393031428133241401624195671690574061419654342324638801248856147305207431992259611796250130992860241708340807605932320161268492288496255841312844061536738951487114256315111089745514203313820202931640957596464756010405845841566072044962867016515061920631004186422275908670900574606417856951911456055068251250406007519842261898059237118054444788072906395242548339221982707404473162376760846613033778706039803413197133493654622700563169937455508241780972810983291314403571877524768509857276937926433221599399876886660808368837838027643282775172273657572744784112294389733810861607423253291974813120197604178281965697475898164531258434135959862784130128185406283476649088690521047580882615823961985770122407044330583075869039319604603404973156583208672105913300903752823415539745394397715257455290510212310947321610753474825740775273986348298498340756937955646638621874569499279016572103701364433135817214311791398222983845847334440270964182851005072927748364550578634501100852987812389473928699540834346158807043959118985815145779177143619698728131459483783202081474982171858011389071228250905826817436220577475921417653715687725614904582904992461028630081535583308130101987675856234343538955409175623400844887526162643568648833519463720377293240094456246923254350400678027273837755376406726898636241037491410966718557050759098100246789880178271925953381282421954028302759408448955014676668389697996886241636313376393903373455801407636741877711055384225739499110186468219696581651485130494222369947714763069155468217682876200362777257723781365331611196811280792669481887201298643660768551639860534602297871557517947385246369446923087894265948217008051120322365496288169035739121368338393591756418733850510970271613915439590991598154654417336311656936031122249937969999226781732358023111862644575299135758175008199839236284615249881088960232244362173771618086357015468484058622329792853875623486556440536962622018963571028812361567512543338303270029097668650568557157505516727518899194129711337690149916181315171544007728650573189557450920330185304847113818315407324053319038462084036421763703911550639789000742853672196280903477974533320468368795868580237952218629120080742819551317948157624448298518461509704888027274721574688131594750409732115080498190455803416826949787141316063210686391511681774304792596709376" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**10000" ] }, { "cell_type": "code", "execution_count": 51, "id": "b915d82c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3/2" ] }, { "cell_type": "code", "execution_count": 52, "id": "33890d7a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3//2" ] }, { "cell_type": "code", "execution_count": 53, "id": "1989f3f3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10%2" ] }, { "cell_type": "markdown", "id": "15cdfd96", "metadata": {}, "source": [ "## Lists" ] }, { "cell_type": "code", "execution_count": 54, "id": "1827a1f4", "metadata": {}, "outputs": [], "source": [ "l = list()\n", "l = []\n", "l = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 55, "id": "c7aaacb7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 56, "id": "447a9630", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140479638276224" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l)" ] }, { "cell_type": "code", "execution_count": 57, "id": "a5712dcf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140479638276224" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append('vier')\n", "id(l)" ] }, { "cell_type": "code", "execution_count": 58, "id": "80d7eb3b", "metadata": {}, "outputs": [], "source": [ "l.extend([5, 'sechs'])" ] }, { "cell_type": "code", "execution_count": 59, "id": "4a5a38cc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5, 'sechs']" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 60, "id": "c77a3bda", "metadata": {}, "outputs": [], "source": [ "l.append([7, 'acht'])" ] }, { "cell_type": "code", "execution_count": 61, "id": "fae6c846", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5, 'sechs', [7, 'acht']]" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 62, "id": "454dbfbf", "metadata": {}, "outputs": [], "source": [ "l.append(l)" ] }, { "cell_type": "code", "execution_count": 63, "id": "e975b80d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5, 'sechs', [7, 'acht'], [...]]" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 64, "id": "3be6870f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[1]" ] }, { "cell_type": "code", "execution_count": 65, "id": "1bc1e509", "metadata": {}, "outputs": [], "source": [ "l[1] = 'zwei'" ] }, { "cell_type": "code", "execution_count": 66, "id": "4296c1ad", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'zwei', 3, 'vier', 5, 'sechs', [7, 'acht'], [...]]" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 67, "id": "4776d8f3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'zwei' in l" ] }, { "cell_type": "code", "execution_count": 68, "id": "952ee30a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay\n" ] } ], "source": [ "if 'zwei' in l:\n", " print('yay')" ] }, { "cell_type": "markdown", "id": "478505a4", "metadata": {}, "source": [ "## Tuples" ] }, { "cell_type": "code", "execution_count": 69, "id": "e7e7fa73", "metadata": {}, "outputs": [], "source": [ "t = (1,2,3)" ] }, { "cell_type": "code", "execution_count": 70, "id": "85cf36db", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(t)" ] }, { "cell_type": "code", "execution_count": 71, "id": "9edadc3b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140480203220736" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(t)" ] }, { "cell_type": "code", "execution_count": 72, "id": "e2bbc067", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[1]" ] }, { "cell_type": "code", "execution_count": 73, "id": "309bcea2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'tuple' object does not support item assignment \n" ] } ], "source": [ "try:\n", " t[1] = 'zwei'\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 74, "id": "52812012", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'tuple' object has no attribute 'append' \n" ] } ], "source": [ "try:\n", " t.append('vier')\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 75, "id": "c8f51a2d", "metadata": {}, "outputs": [], "source": [ "t += (1,2,3)" ] }, { "cell_type": "code", "execution_count": 76, "id": "8cbefe30", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140480210406176" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(t)" ] }, { "cell_type": "markdown", "id": "cbbc048c", "metadata": {}, "source": [ "## Dictionary" ] }, { "cell_type": "code", "execution_count": 77, "id": "bb54d8e3", "metadata": {}, "outputs": [], "source": [ "d = { 'one': 1, 'two': 2 }" ] }, { "cell_type": "code", "execution_count": 78, "id": "9bcfa07f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(d)" ] }, { "cell_type": "code", "execution_count": 79, "id": "4727fb76", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['one']" ] }, { "cell_type": "code", "execution_count": 80, "id": "883ada93", "metadata": {}, "outputs": [], "source": [ "d['drei'] = 'three'" ] }, { "cell_type": "code", "execution_count": 81, "id": "78e11b8f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'three'" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['drei']" ] }, { "cell_type": "code", "execution_count": 82, "id": "85caf82e", "metadata": {}, "outputs": [], "source": [ "d[4] = 'vier'" ] }, { "cell_type": "code", "execution_count": 83, "id": "42e3f988", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash(4)" ] }, { "cell_type": "code", "execution_count": 84, "id": "561107fa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-7940943354778932823" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hash('drei')" ] }, { "cell_type": "code", "execution_count": 85, "id": "a2c852ad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "unhashable type: 'list' \n" ] } ], "source": [ "try:\n", " d[[1,2,3]] = True\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 86, "id": "16c27367", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'drei': 'three', 4: 'vier'}" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 87, "id": "cb35285f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(d)" ] }, { "cell_type": "code", "execution_count": 88, "id": "373ebeec", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'two' in d" ] }, { "cell_type": "code", "execution_count": 89, "id": "b78bbd2f", "metadata": {}, "outputs": [], "source": [ "del d['two']" ] }, { "cell_type": "code", "execution_count": 90, "id": "14afdbb2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'two' in d" ] }, { "cell_type": "markdown", "id": "5952c2af", "metadata": {}, "source": [ "Search by value?" ] }, { "cell_type": "code", "execution_count": 91, "id": "8f2b52fd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n" ] } ], "source": [ "for k, v in d.items():\n", " if v == 1:\n", " print(k)" ] }, { "cell_type": "markdown", "id": "e0cfd10f", "metadata": {}, "source": [ "## Exercises" ] }, { "cell_type": "code", "execution_count": 92, "id": "063d764e", "metadata": {}, "outputs": [], "source": [ "l = []" ] }, { "cell_type": "code", "execution_count": 93, "id": "99b28f00", "metadata": {}, "outputs": [], "source": [ "l.append(True)" ] }, { "cell_type": "code", "execution_count": 94, "id": "29c6110e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[True]" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 95, "id": "d6afada6", "metadata": {}, "outputs": [], "source": [ "s = '99'" ] }, { "cell_type": "code", "execution_count": 96, "id": "99727df6", "metadata": {}, "outputs": [], "source": [ "i = int(s)" ] }, { "cell_type": "code", "execution_count": 97, "id": "d1acbfb2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "99" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "markdown", "id": "8043a0ed", "metadata": {}, "source": [ "## DataType Conversiosn" ] }, { "cell_type": "code", "execution_count": 98, "id": "6d7fb555", "metadata": {}, "outputs": [], "source": [ "s = '123'" ] }, { "cell_type": "code", "execution_count": 99, "id": "fe09de41", "metadata": {}, "outputs": [], "source": [ "i = int(s)" ] }, { "cell_type": "code", "execution_count": 100, "id": "336cf134", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(i))" ] }, { "cell_type": "code", "execution_count": 101, "id": "0cba3386", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "123" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 102, "id": "f01c77fd", "metadata": {}, "outputs": [], "source": [ "s = 'abc'" ] }, { "cell_type": "code", "execution_count": 103, "id": "fcdb3803", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "invalid literal for int() with base 10: 'abc' \n" ] } ], "source": [ "try:\n", " int(s)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 104, "id": "a5a541b2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2748" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(s, 16)" ] }, { "cell_type": "code", "execution_count": 105, "id": "d82b9add", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "123" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 106, "id": "d05e6af5", "metadata": {}, "outputs": [], "source": [ "s = str(i)" ] }, { "cell_type": "code", "execution_count": 107, "id": "3167d2d0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(s))" ] }, { "cell_type": "code", "execution_count": 108, "id": "fa353f2f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "l = [1,2,3, 'vier']\n", "print(type(l))" ] }, { "cell_type": "code", "execution_count": 109, "id": "b8985538", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'123'" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 110, "id": "4ca9c30e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2'" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[1]" ] }, { "cell_type": "code", "execution_count": 111, "id": "0ee66956", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list()" ] }, { "cell_type": "code", "execution_count": 112, "id": "f74ddf30", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list('abc')" ] }, { "cell_type": "code", "execution_count": 113, "id": "673f0672", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'int' object is not iterable \n" ] } ], "source": [ "try:\n", " list(666)\n", "except Exception as e:\n", " print(e, type(s))" ] }, { "cell_type": "code", "execution_count": 114, "id": "db780192", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['abc']" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = list()\n", "l.append('abc')\n", "l" ] }, { "cell_type": "code", "execution_count": 115, "id": "3f749a68", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['abc']" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(['abc'])" ] }, { "cell_type": "code", "execution_count": 116, "id": "0fb12951", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['abc']" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "['abc']" ] }, { "cell_type": "code", "execution_count": 117, "id": "719fc73b", "metadata": {}, "outputs": [], "source": [ "l = list(range(3))" ] }, { "cell_type": "code", "execution_count": 118, "id": "a3abdd27", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2]" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 119, "id": "1d1884f8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for element in l:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 120, "id": "4e75f30e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 in l" ] }, { "cell_type": "code", "execution_count": 121, "id": "86c2c575", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in l" ] }, { "cell_type": "code", "execution_count": 122, "id": "00bc19ac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 in l" ] }, { "cell_type": "markdown", "id": "99670ed8", "metadata": {}, "source": [ "## ``if``" ] }, { "cell_type": "code", "execution_count": 123, "id": "8a1b6497", "metadata": {}, "outputs": [], "source": [ "i = 2" ] }, { "cell_type": "code", "execution_count": 124, "id": "4bba2cad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay\n" ] } ], "source": [ "if i == 2:\n", " print('yay')" ] }, { "cell_type": "code", "execution_count": 125, "id": "eba15162", "metadata": {}, "outputs": [], "source": [ "if i == 3:\n", " print('yay')" ] }, { "cell_type": "markdown", "id": "93d11133", "metadata": {}, "source": [ "## ``while``" ] }, { "cell_type": "markdown", "id": "eb887b39", "metadata": {}, "source": [ "sum of numbers 1..100" ] }, { "cell_type": "code", "execution_count": 128, "id": "93d812a3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5050" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "summe = 0\n", "number = 1\n", "while number <= 100:\n", " summe += number\n", " number += 1\n", "summe" ] }, { "cell_type": "code", "execution_count": 129, "id": "804a8c86", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5050" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(range(1, 101))" ] }, { "cell_type": "code", "execution_count": 130, "id": "9f5ad577", "metadata": {}, "outputs": [], "source": [ "i = 42\n", "while i != 42:\n", " print('noch immer keine antwort')" ] }, { "cell_type": "code", "execution_count": 133, "id": "8640440c", "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 144, "id": "16b2587b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "mist: 4\n", "hooray!\n" ] } ], "source": [ "while True:\n", " eyes = random.randrange(1, 7)\n", " if eyes == 6:\n", " print('hooray!')\n", " break\n", " else:\n", " print('mist:', eyes) " ] }, { "cell_type": "code", "execution_count": 165, "id": "be76f33d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "versager!\n" ] } ], "source": [ "n_tries = 0\n", "while n_tries < 6:\n", " eyes = random.randrange(1, 7)\n", " if eyes == 6:\n", " print('hooray!')\n", " break\n", " n_tries += 1\n", "else:\n", " print('versager!')" ] }, { "cell_type": "markdown", "id": "0c3fb6c0", "metadata": {}, "source": [ "## ``range()``" ] }, { "cell_type": "code", "execution_count": 167, "id": "9b37bb2a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for element in range(5):\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 173, "id": "de45916b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for element in [0,1,2,3,4]:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 179, "id": "65d0d60e", "metadata": {}, "outputs": [], "source": [ "r = range(5)" ] }, { "cell_type": "code", "execution_count": 180, "id": "e50e800d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 5)" ] }, "execution_count": 180, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r" ] }, { "cell_type": "code", "execution_count": 181, "id": "e84c6102", "metadata": {}, "outputs": [], "source": [ "it = iter(r)" ] }, { "cell_type": "code", "execution_count": 183, "id": "ca86d2ed", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "it" ] }, { "cell_type": "code", "execution_count": 184, "id": "edf98193", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 185, "id": "f923f814", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 185, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 186, "id": "18c4c3ec", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 186, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 187, "id": "630e8db6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 187, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 188, "id": "1f48b994", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 188, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 190, "id": "c5f98d63", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "try:\n", " next(it)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "markdown", "id": "a48b042e", "metadata": {}, "source": [ "## ``datetime``" ] }, { "cell_type": "code", "execution_count": 217, "id": "91728d38", "metadata": {}, "outputs": [], "source": [ "import datetime" ] }, { "cell_type": "code", "execution_count": 192, "id": "57a89a12", "metadata": {}, "outputs": [], "source": [ "start = datetime.datetime(year=2023, month=3, day=13, hour=9)" ] }, { "cell_type": "code", "execution_count": 194, "id": "124bc49d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2023, 3, 13, 9, 0)" ] }, "execution_count": 194, "metadata": {}, "output_type": "execute_result" } ], "source": [ "start" ] }, { "cell_type": "code", "execution_count": 196, "id": "9cec9dce", "metadata": {}, "outputs": [], "source": [ "step = datetime.timedelta(hours=8)" ] }, { "cell_type": "code", "execution_count": 198, "id": "39772e2a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.timedelta(seconds=28800)" ] }, "execution_count": 198, "metadata": {}, "output_type": "execute_result" } ], "source": [ "step" ] }, { "cell_type": "code", "execution_count": 199, "id": "3cc13d81", "metadata": {}, "outputs": [], "source": [ "start += step" ] }, { "cell_type": "code", "execution_count": 201, "id": "063bb5b0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2023, 3, 13, 17, 0)" ] }, "execution_count": 201, "metadata": {}, "output_type": "execute_result" } ], "source": [ "start" ] }, { "cell_type": "code", "execution_count": 202, "id": "3f1967d3", "metadata": {}, "outputs": [], "source": [ "start += datetime.timedelta(days=1)" ] }, { "cell_type": "code", "execution_count": 204, "id": "4cad411e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2023, 3, 14, 17, 0)" ] }, "execution_count": 204, "metadata": {}, "output_type": "execute_result" } ], "source": [ "start" ] }, { "cell_type": "code", "execution_count": 210, "id": "4c800eff", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2023, 3, 14, 10, 6, 17, 237988)" ] }, "execution_count": 210, "metadata": {}, "output_type": "execute_result" } ], "source": [ "datetime.datetime.now()" ] }, { "cell_type": "code", "execution_count": 215, "id": "1e45a04e", "metadata": {}, "outputs": [], "source": [ "def produce_days(start):\n", " next = start\n", " while True:\n", " yield next\n", " next += datetime.timedelta(days=1)" ] }, { "cell_type": "code", "execution_count": 216, "id": "c8e01a62", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2023-03-14 10:11:25.374557\n", "2023-03-15 10:11:25.374557\n", "2023-03-16 10:11:25.374557\n", "2023-03-17 10:11:25.374557\n", "2023-03-18 10:11:25.374557\n", "2023-03-19 10:11:25.374557\n", "2023-03-20 10:11:25.374557\n", "2023-03-21 10:11:25.374557\n", "2023-03-22 10:11:25.374557\n", "2023-03-23 10:11:25.374557\n", "2023-03-24 10:11:25.374557\n", "2023-03-25 10:11:25.374557\n", "2023-03-26 10:11:25.374557\n", "2023-03-27 10:11:25.374557\n", "2023-03-28 10:11:25.374557\n", "2023-03-29 10:11:25.374557\n", "2023-03-30 10:11:25.374557\n", "2023-03-31 10:11:25.374557\n", "2023-04-01 10:11:25.374557\n" ] } ], "source": [ "for day in produce_days(start=datetime.datetime.now()):\n", " print(day)\n", " if day > datetime.datetime(year=2023, month=4, day=1):\n", " break" ] }, { "cell_type": "code", "execution_count": 218, "id": "7b599237", "metadata": {}, "outputs": [], "source": [ "l = [666, 42]\n", "i = 42" ] }, { "cell_type": "code", "execution_count": 220, "id": "9d735363", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 220, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i != l" ] }, { "cell_type": "code", "execution_count": 221, "id": "3f97d9a2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 221, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i in l" ] }, { "cell_type": "code", "execution_count": 223, "id": "100c07d7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 223, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i not in l" ] }, { "cell_type": "code", "execution_count": 224, "id": "fd533f28", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 224, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not i in l" ] }, { "cell_type": "code", "execution_count": 225, "id": "016120cc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 225, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6 % 3" ] }, { "cell_type": "code", "execution_count": 226, "id": "42348c35", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 226, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6 % 2" ] }, { "cell_type": "code", "execution_count": 227, "id": "6a40107d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 227, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 % 2" ] }, { "cell_type": "code", "execution_count": 228, "id": "f782b416", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 228, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 % 3" ] }, { "cell_type": "code", "execution_count": 229, "id": "facdc6b3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 229, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 % 4" ] }, { "cell_type": "code", "execution_count": 230, "id": "3aca1b59", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 230, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 % 5" ] }, { "cell_type": "code", "execution_count": 231, "id": "db9ff6cb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 231, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 % 6" ] }, { "cell_type": "markdown", "id": "d74f09a8", "metadata": {}, "source": [ "## Comprehensions" ] }, { "cell_type": "code", "execution_count": 233, "id": "f065da08", "metadata": {}, "outputs": [], "source": [ "l = [3, 4, 1, 6]" ] }, { "cell_type": "code", "execution_count": 235, "id": "85044688", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[6, 8, 2, 12]" ] }, "execution_count": 235, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l_mal_2 = []\n", "for element in l:\n", " l_mal_2.append(element*2)\n", "l_mal_2" ] }, { "cell_type": "code", "execution_count": 238, "id": "e4c50aff", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[6, 8, 2, 12]" ] }, "execution_count": 238, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[element*2 for element in l]" ] }, { "cell_type": "code", "execution_count": 239, "id": "7429a391", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[8, 12]" ] }, "execution_count": 239, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[element*2 for element in l if element%2==0]" ] }, { "cell_type": "markdown", "id": "3a5451c8", "metadata": {}, "source": [ "## References, (Im)mutability" ] }, { "cell_type": "code", "execution_count": 248, "id": "8eea05b4", "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 249, "id": "c99aab46", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140480285361680" ] }, "execution_count": 249, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 250, "id": "8aace4a4", "metadata": {}, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 251, "id": "47950868", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140480285361680" ] }, "execution_count": 251, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 252, "id": "9635b8b9", "metadata": {}, "outputs": [], "source": [ "a += 1" ] }, { "cell_type": "code", "execution_count": 253, "id": "6c50cba0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140480285361712" ] }, "execution_count": 253, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 254, "id": "b5016cf4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 254, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 255, "id": "1e6ac9c9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140480285361680" ] }, "execution_count": 255, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 256, "id": "3e23f93d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140479638477760" ] }, "execution_count": 256, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 = [42, 'xy', 13]\n", "id(l1)" ] }, { "cell_type": "code", "execution_count": 257, "id": "c4862379", "metadata": {}, "outputs": [], "source": [ "l2 = l1" ] }, { "cell_type": "code", "execution_count": 258, "id": "8d39e01b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140479638477760" ] }, "execution_count": 258, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l2)" ] }, { "cell_type": "code", "execution_count": 259, "id": "edb2197a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 'xy', 13, 666]" ] }, "execution_count": 259, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1.append(666)\n", "l1" ] }, { "cell_type": "code", "execution_count": 260, "id": "661ef644", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140479638477760" ] }, "execution_count": 260, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1)" ] }, { "cell_type": "code", "execution_count": 261, "id": "1a836bc9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140479638477760" ] }, "execution_count": 261, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l2)" ] }, { "cell_type": "code", "execution_count": 262, "id": "c04256cd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 'xy', 13, 666]" ] }, "execution_count": 262, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 263, "id": "f9e90869", "metadata": {}, "outputs": [], "source": [ "l3 = l1[:]" ] }, { "cell_type": "code", "execution_count": 264, "id": "9244fc2c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 'xy', 13, 666]" ] }, "execution_count": 264, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3" ] }, { "cell_type": "code", "execution_count": 265, "id": "c95cc13f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 'xy', 13, 666]" ] }, "execution_count": 265, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 267, "id": "9bd3d5ca", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140479638477760" ] }, "execution_count": 267, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1)" ] }, { "cell_type": "code", "execution_count": 268, "id": "92847d26", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140479638427520" ] }, "execution_count": 268, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l3)" ] }, { "cell_type": "code", "execution_count": 269, "id": "07669cad", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 'xy', 13, 666, 'nochwas']" ] }, "execution_count": 269, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3.append('nochwas')\n", "l3" ] }, { "cell_type": "code", "execution_count": 270, "id": "ba5b5213", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 'xy', 13, 666]" ] }, "execution_count": 270, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "markdown", "id": "be7f7fc2", "metadata": {}, "source": [ "Nested lists?" ] }, { "cell_type": "code", "execution_count": 271, "id": "d06d8ddb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 271, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l4 = [1, [2,3,4], 5]\n", "len(l4)" ] }, { "cell_type": "code", "execution_count": 272, "id": "54655971", "metadata": {}, "outputs": [], "source": [ "l5 = l4[:]" ] }, { "cell_type": "code", "execution_count": 273, "id": "ca9a13d7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140480202528192" ] }, "execution_count": 273, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l4)" ] }, { "cell_type": "code", "execution_count": 274, "id": "a0f8cd22", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140479638428736" ] }, "execution_count": 274, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l5)" ] }, { "cell_type": "code", "execution_count": 275, "id": "15edf9d0", "metadata": {}, "outputs": [], "source": [ "l4[1].append(666)" ] }, { "cell_type": "code", "execution_count": 276, "id": "7c1485da", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, [2, 3, 4, 666], 5]" ] }, "execution_count": 276, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l5" ] }, { "cell_type": "code", "execution_count": 278, "id": "cfeed00d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140480203212992" ] }, "execution_count": 278, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l4[1])" ] }, { "cell_type": "code", "execution_count": 279, "id": "f0e26361", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140480203212992" ] }, "execution_count": 279, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l5[1])" ] }, { "cell_type": "code", "execution_count": 280, "id": "87ae8cea", "metadata": {}, "outputs": [], "source": [ "import copy" ] }, { "cell_type": "code", "execution_count": 281, "id": "67a4c4e7", "metadata": {}, "outputs": [], "source": [ "l6 = copy.deepcopy(l5)" ] }, { "cell_type": "code", "execution_count": 282, "id": "e329e951", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, [2, 3, 4, 666], 5]" ] }, "execution_count": 282, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l6" ] }, { "cell_type": "markdown", "id": "f8edf179", "metadata": {}, "source": [ "## Functions" ] }, { "cell_type": "code", "execution_count": 296, "id": "ca033ad8", "metadata": {}, "outputs": [], "source": [ "def maximum(a, b):\n", " 'extremely complex function'\n", " if a < b:\n", " return b\n", " else:\n", " return a" ] }, { "cell_type": "code", "execution_count": 285, "id": "121c02d1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 285, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(1, 2)" ] }, { "cell_type": "code", "execution_count": 287, "id": "80f0b381", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 287, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(1.5, 2.5)" ] }, { "cell_type": "code", "execution_count": 288, "id": "5a4033a2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'def'" ] }, "execution_count": 288, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum('abc', 'def')" ] }, { "cell_type": "code", "execution_count": 289, "id": "545d1cc9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 4]" ] }, "execution_count": 289, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum([1,2], [3,4])" ] }, { "cell_type": "code", "execution_count": 290, "id": "a7a29ebd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4]" ] }, "execution_count": 290, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum([1,2], [1,4])" ] }, { "cell_type": "code", "execution_count": 292, "id": "8cd6b6be", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'<' not supported between instances of 'str' and 'int' \n" ] } ], "source": [ "try:\n", " maximum('abc', 666)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 293, "id": "e1b5f059", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 293, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True < False" ] }, { "cell_type": "code", "execution_count": 295, "id": "22b18e3f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(maximum))" ] }, { "cell_type": "code", "execution_count": 298, "id": "03d88045", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'extremely complex function'" ] }, "execution_count": 298, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum.__doc__" ] }, { "cell_type": "code", "execution_count": 299, "id": "38f3032e", "metadata": {}, "outputs": [], "source": [ "a = maximum" ] }, { "cell_type": "code", "execution_count": 301, "id": "2a575bfd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 301, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a(1,2)" ] }, { "cell_type": "code", "execution_count": 302, "id": "f5816a5c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140479598069936" ] }, "execution_count": 302, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 303, "id": "16dc130d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140479598069936" ] }, "execution_count": 303, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(maximum)" ] }, { "cell_type": "markdown", "id": "04194124", "metadata": {}, "source": [ "Classes?" ] }, { "cell_type": "code", "execution_count": 313, "id": "bf94c4e3", "metadata": {}, "outputs": [], "source": [ "class Person:\n", " def __init__(self, firstname, lastname, age):\n", " self.firstname = firstname\n", " self.lastname = lastname\n", " self.age = age\n", " def fullname(self):\n", " return self.firstname + ' ' + self.lastname" ] }, { "cell_type": "code", "execution_count": 312, "id": "99215081", "metadata": {}, "outputs": [], "source": [ "joerg = Person('Joerg', 'Faschingbauer', 56)" ] }, { "cell_type": "code", "execution_count": 309, "id": "91958a94", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.Person" ] }, "execution_count": 309, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(joerg)" ] }, { "cell_type": "code", "execution_count": 314, "id": "90a83541", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg Faschingbauer'" ] }, "execution_count": 314, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joerg.fullname()" ] }, { "cell_type": "code", "execution_count": 317, "id": "421f846c", "metadata": {}, "outputs": [], "source": [ "def is_old(p):\n", " if p.age > 50:\n", " return True\n", " else:\n", " return False" ] }, { "cell_type": "code", "execution_count": 318, "id": "1cb59901", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 318, "metadata": {}, "output_type": "execute_result" } ], "source": [ "is_old(joerg)" ] }, { "cell_type": "code", "execution_count": 320, "id": "6ad15f89", "metadata": {}, "outputs": [], "source": [ "Person.old = is_old" ] }, { "cell_type": "code", "execution_count": 321, "id": "f7ea3fb6", "metadata": {}, "outputs": [], "source": [ "joerg = Person('Joerg', 'Faschingbauer', 56)" ] }, { "cell_type": "code", "execution_count": 322, "id": "ba30bd61", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 322, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joerg.old()" ] }, { "cell_type": "code", "execution_count": 325, "id": "e9ddc269", "metadata": {}, "outputs": [], "source": [ "d = {'one': 1, 'two': 2}" ] }, { "cell_type": "code", "execution_count": 327, "id": "e9a0979d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 327, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['one']" ] }, { "cell_type": "code", "execution_count": 328, "id": "c2bc4935", "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "'three'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_370861/4290492544.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'three'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 'three'" ] } ], "source": [ "d['three']" ] }, { "cell_type": "markdown", "id": "53494b88", "metadata": {}, "source": [ "## Strings" ] }, { "cell_type": "code", "execution_count": 329, "id": "e280790e", "metadata": {}, "outputs": [], "source": [ "s = 'abc'" ] }, { "cell_type": "code", "execution_count": 330, "id": "52098750", "metadata": {}, "outputs": [], "source": [ "s = \"abc\"" ] }, { "cell_type": "code", "execution_count": 332, "id": "5b01e32e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"abc'def\"" ] }, "execution_count": 332, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'abc\\'def'\n", "s" ] }, { "cell_type": "code", "execution_count": 333, "id": "f9d80fad", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"abc'def\"" ] }, "execution_count": 333, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"abc'def\"\n", "s" ] }, { "cell_type": "code", "execution_count": 334, "id": "f82820af", "metadata": {}, "outputs": [], "source": [ "s = 'C:\\some\\name'" ] }, { "cell_type": "code", "execution_count": 335, "id": "c253e30d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\some\n", "ame\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 337, "id": "5bb8ec89", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\some\\name\n" ] } ], "source": [ "s = r'C:\\some\\name'\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 338, "id": "d27db9e9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 338, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '12345'\n", "s.isdigit()" ] }, { "cell_type": "code", "execution_count": 340, "id": "dd00b9ee", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 340, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'abc123'\n", "s.isdigit()" ] }, { "cell_type": "code", "execution_count": 341, "id": "a4fc5189", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' abc '" ] }, "execution_count": 341, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'.center(50)" ] }, { "cell_type": "code", "execution_count": 342, "id": "a48b1fbf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 342, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'Mississippi'\n", "s.count('ss')" ] }, { "cell_type": "code", "execution_count": 343, "id": "6b2bdc5b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 343, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.count('pp')" ] }, { "cell_type": "code", "execution_count": 344, "id": "7c8e289e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 344, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'file.csv'.endswith('.csv')" ] }, { "cell_type": "code", "execution_count": 345, "id": "f8a6ce2d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 345, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'boot'.endswith('t')" ] }, { "cell_type": "code", "execution_count": 347, "id": "dae087c0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 347, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'boot'.count('oo') > 0" ] }, { "cell_type": "code", "execution_count": 350, "id": "fbf46d2a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 350, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'boot'.find('oo') >= 0" ] }, { "cell_type": "code", "execution_count": 352, "id": "8fea8545", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 352, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'oo' in 'boot'" ] }, { "cell_type": "code", "execution_count": 353, "id": "8e59b4b7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 353, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'boot'.find('oo')" ] }, { "cell_type": "code", "execution_count": 355, "id": "1a1435cc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 355, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'boot'.find('xxx')" ] }, { "cell_type": "code", "execution_count": 357, "id": "325ff574", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "substring not found \n" ] } ], "source": [ "try:\n", " 'boot'.index('xxx')\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 358, "id": "40ccf081", "metadata": {}, "outputs": [], "source": [ "s = 'Bezahlung Karte blah |BILLA DANKT blah blah |BILL blah'" ] }, { "cell_type": "code", "execution_count": 362, "id": "b628bad3", "metadata": {}, "outputs": [], "source": [ "fields = s.split('|')" ] }, { "cell_type": "code", "execution_count": 363, "id": "ee59c9be", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Bezahlung Karte blah ', 'BILLA DANKT blah blah ', 'BILL blah']" ] }, "execution_count": 363, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fields" ] }, { "cell_type": "code", "execution_count": 431, "id": "b9b5b714", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Bezahlung Karte blah ;BILLA DANKT blah blah ;BILL blah'" ] }, "execution_count": 431, "metadata": {}, "output_type": "execute_result" } ], "source": [ "';'.join(fields)" ] }, { "cell_type": "code", "execution_count": 432, "id": "dc09486e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'xxx'" ] }, "execution_count": 432, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = ' \\t\\n xxx \\n\\n\\n\\t '\n", "s.strip()" ] }, { "cell_type": "markdown", "id": "51fa1efa", "metadata": {}, "source": [ "## Lists" ] }, { "cell_type": "code", "execution_count": 369, "id": "b991d0b4", "metadata": {}, "outputs": [], "source": [ "l = [1,2,'drei']\n", "l.append(666)" ] }, { "cell_type": "code", "execution_count": 366, "id": "53f72f2f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 666]" ] }, "execution_count": 366, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 370, "id": "6c593f79", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 666, 'noch', 'was']" ] }, "execution_count": 370, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.extend(['noch', 'was'])\n", "l" ] }, { "cell_type": "code", "execution_count": 371, "id": "7173f6ff", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 666, 'noch', 'was', 'a', 'b', 'c']" ] }, "execution_count": 371, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.extend('abc')\n", "l" ] }, { "cell_type": "code", "execution_count": 372, "id": "ececf6d4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 372, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.pop(3)" ] }, { "cell_type": "code", "execution_count": 373, "id": "5d4289eb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 'noch', 'was', 'a', 'b', 'c']" ] }, "execution_count": 373, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 375, "id": "076ed8aa", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'<' not supported between instances of 'str' and 'int' \n" ] } ], "source": [ "try:\n", " l.sort()\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 376, "id": "a504c086", "metadata": {}, "outputs": [], "source": [ "l.reverse()" ] }, { "cell_type": "code", "execution_count": 377, "id": "20061fe0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['c', 'b', 'a', 'was', 'noch', 'drei', 2, 1]" ] }, "execution_count": 377, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 378, "id": "b5e27ef8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 378, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reversed(l)" ] }, { "cell_type": "code", "execution_count": 379, "id": "a53d5439", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 49, 10000]" ] }, "execution_count": 379, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1,2,3,7,100]\n", "[elem**2 for elem in l]" ] }, { "cell_type": "code", "execution_count": 380, "id": "58e394c5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 10000]" ] }, "execution_count": 380, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[elem**2 for elem in l if elem%2==0]" ] }, { "cell_type": "markdown", "id": "c9e07114", "metadata": {}, "source": [ "## Dictionaries" ] }, { "cell_type": "code", "execution_count": 381, "id": "a3fd109e", "metadata": {}, "outputs": [], "source": [ "d = {}" ] }, { "cell_type": "code", "execution_count": 382, "id": "8bd97db3", "metadata": {}, "outputs": [], "source": [ "d['one'] = 1" ] }, { "cell_type": "code", "execution_count": 383, "id": "e53c1838", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1}" ] }, "execution_count": 383, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 384, "id": "b58a750a", "metadata": {}, "outputs": [], "source": [ "d['one'] = 1.1" ] }, { "cell_type": "code", "execution_count": 385, "id": "c785a799", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1.1}" ] }, "execution_count": 385, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 386, "id": "2916978a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.1" ] }, "execution_count": 386, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['one']" ] }, { "cell_type": "code", "execution_count": 388, "id": "c454ea0e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'two' \n" ] } ], "source": [ "try:\n", " d['two']\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 391, "id": "ded82003", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nicht drin\n" ] } ], "source": [ "if d.get('two') is None:\n", " print('nicht drin')\n", "else:\n", " print('yay')" ] }, { "cell_type": "code", "execution_count": 392, "id": "809b3fe0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 392, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = d.get('two', 2)\n", "v" ] }, { "cell_type": "code", "execution_count": 393, "id": "38b94393", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1.1}" ] }, "execution_count": 393, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 394, "id": "e9f9f053", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 394, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = d.setdefault('two', 2)\n", "v" ] }, { "cell_type": "code", "execution_count": 395, "id": "80b09ee9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1.1, 'two': 2}" ] }, "execution_count": 395, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 396, "id": "c962b43e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1.1}" ] }, "execution_count": 396, "metadata": {}, "output_type": "execute_result" } ], "source": [ "del d['two']\n", "d" ] }, { "cell_type": "code", "execution_count": 401, "id": "23edb941", "metadata": {}, "outputs": [], "source": [ "d.update({'two': 2, 'three': 3, 'one': 1})" ] }, { "cell_type": "code", "execution_count": 402, "id": "5706c380", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3}" ] }, "execution_count": 402, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 404, "id": "6eb70e3b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n", "three\n" ] } ], "source": [ "for elem in d:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 406, "id": "61663ee9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n", "three\n" ] } ], "source": [ "for k in d.keys():\n", " print(k)" ] }, { "cell_type": "code", "execution_count": 407, "id": "cec1e5f1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for v in d.values():\n", " print(v)" ] }, { "cell_type": "code", "execution_count": 408, "id": "8f0935af", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('one', 1)\n", "('two', 2)\n", "('three', 3)\n" ] } ], "source": [ "for elem in d.items():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 409, "id": "123abf54", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: one , value: 1\n", "key: two , value: 2\n", "key: three , value: 3\n" ] } ], "source": [ "for elem in d.items(): # manual tuple unpacking\n", " k = elem[0]\n", " v = elem[1]\n", " print('key:', k, ', value:', v)" ] }, { "cell_type": "code", "execution_count": 410, "id": "2ef4532b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: one , value: 1\n", "key: two , value: 2\n", "key: three , value: 3\n" ] } ], "source": [ "for elem in d.items(): # semi-manual tuple unpacking\n", " k, v = elem\n", " print('key:', k, ', value:', v)" ] }, { "cell_type": "code", "execution_count": 411, "id": "f5f9ba10", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: one , value: 1\n", "key: two , value: 2\n", "key: three , value: 3\n" ] } ], "source": [ "for k, v in d.items():\n", " print('key:', k, ', value:', v)" ] }, { "cell_type": "code", "execution_count": 413, "id": "435cc1d7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 413, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'one' in d" ] }, { "cell_type": "code", "execution_count": 414, "id": "d3b60b23", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 414, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict" ] }, { "cell_type": "code", "execution_count": 416, "id": "c9ba3b23", "metadata": {}, "outputs": [], "source": [ "l = [('one', 1), ('two', 2)]\n", "d = dict(l)" ] }, { "cell_type": "code", "execution_count": 417, "id": "38e9b4a6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2}" ] }, "execution_count": 417, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "markdown", "id": "8fc909a7", "metadata": {}, "source": [ "## Set" ] }, { "cell_type": "code", "execution_count": 418, "id": "b313f2c3", "metadata": {}, "outputs": [], "source": [ "s1 = {1,2,3}\n", "s2 = {1,2,3,4}" ] }, { "cell_type": "code", "execution_count": 419, "id": "2ba992e7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 419, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1.isdisjoint(s2)" ] }, { "cell_type": "code", "execution_count": 420, "id": "a731ebb2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 420, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 < s2" ] }, { "cell_type": "code", "execution_count": 421, "id": "2e04c5ee", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 421, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s3 = {0,1,2,3}\n", "s3 < s2" ] }, { "cell_type": "code", "execution_count": 423, "id": "5fe30692", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0, 1, 2, 3, 4}" ] }, "execution_count": 423, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s3 | s2" ] }, { "cell_type": "markdown", "id": "a70ec9a6", "metadata": {}, "source": [ "## File IO" ] }, { "cell_type": "code", "execution_count": 424, "id": "abbb86f0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<_io.TextIOWrapper name='/etc/passwd' mode='r' encoding='UTF-8'>" ] }, "execution_count": 424, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = open('/etc/passwd')\n", "f" ] }, { "cell_type": "code", "execution_count": 425, "id": "879a730b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'root:x:0:0:root:/root:/bin/bash\\nbin:x:1:1:bin:/bin:/sbin/nologin\\ndaemon:x:2:2:daemon:/sbin:/sbin/nologin\\nadm:x:3:4:adm:/var/adm:/sbin/nologin\\nlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\\nsync:x:5:0:sync:/sbin:/bin/sync\\nshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\\nhalt:x:7:0:halt:/sbin:/sbin/halt\\nmail:x:8:12:mail:/var/spool/mail:/sbin/nologin\\noperator:x:11:0:operator:/root:/sbin/nologin\\ngames:x:12:100:games:/usr/games:/sbin/nologin\\nftp:x:14:50:FTP User:/var/ftp:/sbin/nologin\\nnobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin\\ndbus:x:81:81:System message bus:/:/sbin/nologin\\napache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin\\ntss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin\\nsystemd-network:x:192:192:systemd Network Management:/:/usr/sbin/nologin\\nsystemd-oom:x:999:999:systemd Userspace OOM Killer:/:/usr/sbin/nologin\\nsystemd-resolve:x:193:193:systemd Resolver:/:/usr/sbin/nologin\\nqemu:x:107:107:qemu user:/:/sbin/nologin\\npolkitd:x:998:997:User for polkitd:/:/sbin/nologin\\navahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin\\nunbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin\\nnm-openconnect:x:996:994:NetworkManager user for OpenConnect:/:/sbin/nologin\\ngeoclue:x:995:993:User for geoclue:/var/lib/geoclue:/sbin/nologin\\nusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin\\ngluster:x:994:992:GlusterFS daemons:/run/gluster:/sbin/nologin\\nrtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin\\nchrony:x:993:990::/var/lib/chrony:/sbin/nologin\\nsaslauth:x:992:76:Saslauthd user:/run/saslauthd:/sbin/nologin\\ndnsmasq:x:991:989:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin\\nrpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin\\ncolord:x:990:988:User for colord:/var/lib/colord:/sbin/nologin\\nrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin\\nopenvpn:x:989:987:OpenVPN:/etc/openvpn:/sbin/nologin\\nnm-openvpn:x:988:986:Default user for running openvpn spawned by NetworkManager:/:/sbin/nologin\\npipewire:x:987:985:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin\\nabrt:x:173:173::/etc/abrt:/sbin/nologin\\nflatpak:x:986:983:User for flatpak system helper:/:/sbin/nologin\\ngdm:x:42:42:GNOME Display Manager:/var/lib/gdm:/sbin/nologin\\ngnome-initial-setup:x:985:982::/run/gnome-initial-setup/:/sbin/nologin\\nvboxadd:x:984:1::/var/run/vboxadd:/sbin/nologin\\nsshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin\\ntcpdump:x:72:72::/:/sbin/nologin\\njfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash\\nsystemd-coredump:x:978:978:systemd Core Dumper:/:/usr/sbin/nologin\\nsystemd-timesync:x:977:977:systemd Time Synchronization:/:/usr/sbin/nologin\\nmosquitto:x:976:976:Mosquitto Broker:/etc/mosquitto:/sbin/nologin\\n'" ] }, "execution_count": 425, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read()" ] }, { "cell_type": "code", "execution_count": null, "id": "60998ae4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 427, "id": "a08166a4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<_io.TextIOWrapper name='/etc/passwd' mode='r' encoding='UTF-8'>" ] }, "execution_count": 427, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = open('/etc/passwd')\n", "f" ] }, { "cell_type": "code", "execution_count": 428, "id": "db2043b8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['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']" ] }, "execution_count": 428, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.readlines()" ] }, { "cell_type": "code", "execution_count": 429, "id": "f952fced", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<_io.TextIOWrapper name='/etc/passwd' mode='r' encoding='UTF-8'>" ] }, "execution_count": 429, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = open('/etc/passwd')\n", "f" ] }, { "cell_type": "code", "execution_count": 430, "id": "84447a4a", "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": [ "for line in f:\n", " print(line)" ] }, { "cell_type": "markdown", "id": "414047a4", "metadata": {}, "source": [ "## String, File IO Exercise" ] }, { "cell_type": "code", "execution_count": 433, "id": "efb4d632", "metadata": {}, "outputs": [], "source": [ "line = ''" ] }, { "cell_type": "code", "execution_count": 434, "id": "8819154a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "die ist leer\n" ] } ], "source": [ "if len(line) == 0:\n", " print('die ist leer')" ] }, { "cell_type": "code", "execution_count": 436, "id": "49bb3379", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 436, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line = ' \\t '\n", "len(line)" ] }, { "cell_type": "code", "execution_count": 437, "id": "5ad8ecad", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 437, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line.strip()" ] }, { "cell_type": "code", "execution_count": 439, "id": "b4b3e24c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'xxx'" ] }, "execution_count": 439, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line = ' xxx '\n", "line.strip()" ] }, { "cell_type": "code", "execution_count": 440, "id": "d838cb0f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'# comment'" ] }, "execution_count": 440, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line = ' # comment'\n", "line.strip()" ] }, { "cell_type": "code", "execution_count": 441, "id": "424dda7e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'# comment'" ] }, "execution_count": 441, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eine_variable = line.strip()\n", "eine_variable" ] }, { "cell_type": "code", "execution_count": 442, "id": "4439aaa5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 442, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eine_variable.startswith('#')" ] }, { "cell_type": "code", "execution_count": 446, "id": "ff7d4242", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'xy'" ] }, "execution_count": 446, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line = ' x y '\n", "line.replace(' ', '')" ] }, { "cell_type": "code", "execution_count": 447, "id": "3d3f847e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 447, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line = ' # '\n", "'#' in line" ] }, { "cell_type": "code", "execution_count": 448, "id": "64e93b88", "metadata": {}, "outputs": [], "source": [ "l = [2,3,4,1,8,666]" ] }, { "cell_type": "code", "execution_count": 450, "id": "a4870b99", "metadata": {}, "outputs": [], "source": [ "n = len(l)" ] }, { "cell_type": "code", "execution_count": 452, "id": "e29e73f7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 452, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n" ] }, { "cell_type": "code", "execution_count": 454, "id": "617a6cac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 454, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 in l" ] }, { "cell_type": "code", "execution_count": 456, "id": "1178e02c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 456, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1000 in l # O(n)" ] }, { "cell_type": "code", "execution_count": 461, "id": "662a0520", "metadata": {}, "outputs": [], "source": [ "s = {2,3,4,1,8,666}" ] }, { "cell_type": "code", "execution_count": 458, "id": "26f4eb9c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 458, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 in s" ] }, { "cell_type": "code", "execution_count": 460, "id": "102e6acd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 460, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1000 in s # O(1)" ] }, { "cell_type": "code", "execution_count": 462, "id": "302c0f73", "metadata": {}, "outputs": [], "source": [ "line = 'xxx yyy'" ] }, { "cell_type": "code", "execution_count": 464, "id": "ca8b2ef6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 464, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(line.split(' '))" ] }, { "cell_type": "code", "execution_count": 465, "id": "b9b70ccd", "metadata": {}, "outputs": [], "source": [ "line = 'xxx yyy'" ] }, { "cell_type": "code", "execution_count": 466, "id": "a2e9b03a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['xxx', '', 'yyy']" ] }, "execution_count": 466, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line.split(' ')" ] }, { "cell_type": "code", "execution_count": 467, "id": "c3e94839", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['xxx', 'yyy']" ] }, "execution_count": 467, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line.split()" ] }, { "cell_type": "markdown", "id": "996e17b5", "metadata": {}, "source": [ "## Pandas" ] }, { "cell_type": "code", "execution_count": 468, "id": "173980a8", "metadata": {}, "outputs": [], "source": [ "persons = {\n", " 'firstname': ['Joerg', 'Johanna', 'Caro', 'Philipp' ],\n", " 'lastname': ['Faschingbauer', 'Faschingbauer', 'Faschingbauer', 'Lichtenberger' ],\n", " 'email': ['jf@faschingbauer.co.at', 'caro@email.com', 'johanna@email.com', 'philipp@email.com'],\n", " 'age': [56, 27, 25, 37 ],\n", "}" ] }, { "cell_type": "code", "execution_count": 469, "id": "3443e45b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'firstname': ['Joerg', 'Johanna', 'Caro', 'Philipp'],\n", " 'lastname': ['Faschingbauer',\n", " 'Faschingbauer',\n", " 'Faschingbauer',\n", " 'Lichtenberger'],\n", " 'email': ['jf@faschingbauer.co.at',\n", " 'caro@email.com',\n", " 'johanna@email.com',\n", " 'philipp@email.com'],\n", " 'age': [56, 27, 25, 37]}" ] }, "execution_count": 469, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons" ] }, { "cell_type": "code", "execution_count": 470, "id": "b6b40e10", "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 471, "id": "b221be4b", "metadata": {}, "outputs": [], "source": [ "data = pd.DataFrame(persons)" ] }, { "cell_type": "code", "execution_count": 472, "id": "96c4f3cc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
firstnamelastnameemailage
0JoergFaschingbauerjf@faschingbauer.co.at56
1JohannaFaschingbauercaro@email.com27
2CaroFaschingbauerjohanna@email.com25
3PhilippLichtenbergerphilipp@email.com37
\n", "
" ], "text/plain": [ " firstname lastname email age\n", "0 Joerg Faschingbauer jf@faschingbauer.co.at 56\n", "1 Johanna Faschingbauer caro@email.com 27\n", "2 Caro Faschingbauer johanna@email.com 25\n", "3 Philipp Lichtenberger philipp@email.com 37" ] }, "execution_count": 472, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 475, "id": "52dc3a1c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 Joerg\n", "1 Johanna\n", "2 Caro\n", "3 Philipp\n", "Name: firstname, dtype: object" ] }, "execution_count": 475, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data['firstname']" ] }, { "cell_type": "code", "execution_count": 476, "id": "5e20c32f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pandas.core.series.Series" ] }, "execution_count": 476, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(data['firstname'])" ] }, { "cell_type": "code", "execution_count": 474, "id": "0a44a68f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
firstnamelastname
0JoergFaschingbauer
1JohannaFaschingbauer
2CaroFaschingbauer
3PhilippLichtenberger
\n", "
" ], "text/plain": [ " firstname lastname\n", "0 Joerg Faschingbauer\n", "1 Johanna Faschingbauer\n", "2 Caro Faschingbauer\n", "3 Philipp Lichtenberger" ] }, "execution_count": 474, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[['firstname', 'lastname']]" ] }, { "cell_type": "code", "execution_count": 477, "id": "374c4213", "metadata": {}, "outputs": [], "source": [ "persons = data" ] }, { "cell_type": "code", "execution_count": 479, "id": "57f944d4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "firstname Johanna\n", "lastname Faschingbauer\n", "email caro@email.com\n", "age 27\n", "Name: 1, dtype: object" ] }, "execution_count": 479, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons.iloc[1]" ] }, { "cell_type": "code", "execution_count": 480, "id": "697d832d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'caro@email.com'" ] }, "execution_count": 480, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons.iloc[1, 2]" ] }, { "cell_type": "code", "execution_count": 481, "id": "3c4fa136", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['firstname', 'lastname', 'email', 'age'], dtype='object')" ] }, "execution_count": 481, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons.columns" ] }, { "cell_type": "code", "execution_count": 482, "id": "a632637b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['firstname', 'lastname', 'email', 'age']" ] }, "execution_count": 482, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(persons.columns)" ] }, { "cell_type": "code", "execution_count": 483, "id": "51ebd5ae", "metadata": {}, "outputs": [], "source": [ "persons2 = persons.set_index('email')" ] }, { "cell_type": "code", "execution_count": 486, "id": "ea0af459", "metadata": {}, "outputs": [], "source": [ "persons2.sort_values('firstname', inplace=True)" ] }, { "cell_type": "code", "execution_count": 487, "id": "e04ef04e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
firstnamelastnameage
email
johanna@email.comCaroFaschingbauer25
jf@faschingbauer.co.atJoergFaschingbauer56
caro@email.comJohannaFaschingbauer27
philipp@email.comPhilippLichtenberger37
\n", "
" ], "text/plain": [ " firstname lastname age\n", "email \n", "johanna@email.com Caro Faschingbauer 25\n", "jf@faschingbauer.co.at Joerg Faschingbauer 56\n", "caro@email.com Johanna Faschingbauer 27\n", "philipp@email.com Philipp Lichtenberger 37" ] }, "execution_count": 487, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons2" ] }, { "cell_type": "code", "execution_count": 488, "id": "f6335a19", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 True\n", "1 True\n", "2 True\n", "3 False\n", "Name: lastname, dtype: bool" ] }, "execution_count": 488, "metadata": {}, "output_type": "execute_result" } ], "source": [ "flt = (persons['lastname'] == 'Faschingbauer')\n", "flt" ] }, { "cell_type": "code", "execution_count": 494, "id": "4f917b73", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 494, "metadata": {}, "output_type": "execute_result" } ], "source": [ "flt.count()" ] }, { "cell_type": "code", "execution_count": 497, "id": "1151d990", "metadata": {}, "outputs": [], "source": [ "persons.set_index('email', inplace=True)" ] }, { "cell_type": "code", "execution_count": 498, "id": "cb464a24", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "firstname Joerg\n", "lastname Faschingbauer\n", "age 56\n", "Name: jf@faschingbauer.co.at, dtype: object" ] }, "execution_count": 498, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons.loc['jf@faschingbauer.co.at']" ] }, { "cell_type": "code", "execution_count": 504, "id": "414fe076", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg'" ] }, "execution_count": 504, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons.loc['jf@faschingbauer.co.at', 'firstname']" ] }, { "cell_type": "code", "execution_count": 502, "id": "4768b5c7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "email\n", "jf@faschingbauer.co.at True\n", "caro@email.com True\n", "johanna@email.com True\n", "philipp@email.com False\n", "Name: lastname, dtype: bool" ] }, "execution_count": 502, "metadata": {}, "output_type": "execute_result" } ], "source": [ "flt = (persons['lastname'] == 'Faschingbauer')\n", "flt" ] }, { "cell_type": "code", "execution_count": 505, "id": "b8d99f37", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "email\n", "jf@faschingbauer.co.at Joerg\n", "caro@email.com Johanna\n", "johanna@email.com Caro\n", "Name: firstname, dtype: object" ] }, "execution_count": 505, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons.loc[flt, 'firstname']" ] }, { "cell_type": "code", "execution_count": 506, "id": "13401599", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "email\n", "jf@faschingbauer.co.at False\n", "caro@email.com False\n", "johanna@email.com True\n", "philipp@email.com True\n", "Name: firstname, dtype: bool" ] }, "execution_count": 506, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons['firstname'].isin(['Caro', 'Philipp'])" ] }, { "cell_type": "code", "execution_count": 508, "id": "739e69d4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "email\n", "jf@faschingbauer.co.at False\n", "caro@email.com False\n", "johanna@email.com True\n", "philipp@email.com True\n", "Name: firstname, dtype: bool" ] }, "execution_count": 508, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(persons['firstname'] == 'Caro') | (persons['firstname'] == 'Philipp')" ] }, { "cell_type": "code", "execution_count": 512, "id": "51f25644", "metadata": {}, "outputs": [], "source": [ "persons.reset_index(inplace=True)" ] }, { "cell_type": "code", "execution_count": 513, "id": "af064d58", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
emailfirstnamelastnameage
0jf@faschingbauer.co.atJoergFaschingbauer56
1caro@email.comJohannaFaschingbauer27
2johanna@email.comCaroFaschingbauer25
3philipp@email.comPhilippLichtenberger37
\n", "
" ], "text/plain": [ " email firstname lastname age\n", "0 jf@faschingbauer.co.at Joerg Faschingbauer 56\n", "1 caro@email.com Johanna Faschingbauer 27\n", "2 johanna@email.com Caro Faschingbauer 25\n", "3 philipp@email.com Philipp Lichtenberger 37" ] }, "execution_count": 513, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons" ] }, { "cell_type": "code", "execution_count": 515, "id": "5702f9a7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 jf@faschingbauer.co.at\n", "1 caro@email.com\n", "2 johanna@email.com\n", "3 philipp@email.com\n", "Name: email, dtype: object" ] }, "execution_count": 515, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons['email']" ] }, { "cell_type": "code", "execution_count": 516, "id": "9ec5397c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
emailfirstnamelastnameage
2johanna@email.comCaroFaschingbauer25
3philipp@email.comPhilippLichtenberger37
\n", "
" ], "text/plain": [ " email firstname lastname age\n", "2 johanna@email.com Caro Faschingbauer 25\n", "3 philipp@email.com Philipp Lichtenberger 37" ] }, "execution_count": 516, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons.loc[persons['firstname'].isin(['Caro', 'Philipp'])]" ] }, { "cell_type": "code", "execution_count": 517, "id": "c84eb3f1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2 johanna@email.com\n", "3 philipp@email.com\n", "Name: email, dtype: object" ] }, "execution_count": 517, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons.loc[persons['firstname'].isin(['Caro', 'Philipp']), 'email']" ] }, { "cell_type": "code", "execution_count": 518, "id": "852caef4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2 JOHANNA@EMAIL.COM\n", "3 PHILIPP@EMAIL.COM\n", "Name: email, dtype: object" ] }, "execution_count": 518, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons.loc[persons['firstname'].isin(['Caro', 'Philipp']), 'email'].str.upper()" ] }, { "cell_type": "code", "execution_count": 520, "id": "39f501f4", "metadata": {}, "outputs": [], "source": [ "persons['email'] = persons.loc[persons['firstname'].isin(['Caro', 'Philipp']), 'email'].str.upper()" ] }, { "cell_type": "code", "execution_count": 521, "id": "83a19579", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
emailfirstnamelastnameage
0NaNJoergFaschingbauer56
1NaNJohannaFaschingbauer27
2JOHANNA@EMAIL.COMCaroFaschingbauer25
3PHILIPP@EMAIL.COMPhilippLichtenberger37
\n", "
" ], "text/plain": [ " email firstname lastname age\n", "0 NaN Joerg Faschingbauer 56\n", "1 NaN Johanna Faschingbauer 27\n", "2 JOHANNA@EMAIL.COM Caro Faschingbauer 25\n", "3 PHILIPP@EMAIL.COM Philipp Lichtenberger 37" ] }, "execution_count": 521, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons" ] }, { "cell_type": "code", "execution_count": 524, "id": "e789b908", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
emailfirstnamelastnameage
2JOHANNA@EMAIL.COMCaroFaschingbauer25
3PHILIPP@EMAIL.COMPhilippLichtenberger37
\n", "
" ], "text/plain": [ " email firstname lastname age\n", "2 JOHANNA@EMAIL.COM Caro Faschingbauer 25\n", "3 PHILIPP@EMAIL.COM Philipp Lichtenberger 37" ] }, "execution_count": 524, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons.dropna()" ] }, { "cell_type": "code", "execution_count": 525, "id": "d729daf6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
emailfirstnamelastnameage
0NaNJoergFaschingbauer56
1NaNJohannaFaschingbauer27
2JOHANNA@EMAIL.COMCaroFaschingbauer25
3PHILIPP@EMAIL.COMPhilippLichtenberger37
\n", "
" ], "text/plain": [ " email firstname lastname age\n", "0 NaN Joerg Faschingbauer 56\n", "1 NaN Johanna Faschingbauer 27\n", "2 JOHANNA@EMAIL.COM Caro Faschingbauer 25\n", "3 PHILIPP@EMAIL.COM Philipp Lichtenberger 37" ] }, "execution_count": 525, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons" ] }, { "cell_type": "code", "execution_count": 531, "id": "eedb0c1f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True 2\n", "False 2\n", "Name: firstname, dtype: int64" ] }, "execution_count": 531, "metadata": {}, "output_type": "execute_result" } ], "source": [ "persons['firstname'].str.startswith('J').value_counts()" ] } ], "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": 5 }