{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 2021-11-17" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variables" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 42\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1.7\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 'drei']\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "_a = 666\n", "_123 = 1" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "a = a" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "a = 1\n", "b = 2\n", "c = 3" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3\n" ] } ], "source": [ "print(a, b, c)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "a, b, c = 1, 2, 3" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3\n" ] } ], "source": [ "print(a, b, c)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1,2,3\n" ] } ], "source": [ "print(a, b, c, sep=',')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Enter Tuples" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b, c" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "t = a, b, c" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(t)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exchange values of a and b, the traditional way" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 1)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = a\n", "a = b\n", "b = c\n", "a, b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exchange values, Pythonic" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "a = 1\n", "b = 2" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "a, b = b, a" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 1)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Name of this: **Tuple Unpacking**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### References" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "a = 42\n", "b = a" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140575811317328" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140575811317328" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140575811316208" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = 7\n", "id(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Datatypes" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "i = 0xffffffff;" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4294967295" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "i += 1" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4294967296" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "i = 2**64 - 1 " ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551615" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "i += 1" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1267650600228229401496703205376" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**100" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "19950631168807583848837421626835850838234968318861924548520089498529438830221946631919961684036194597899331129423209124271556491349413781117593785932096323957855730046793794526765246551266059895520550086918193311542508608460618104685509074866089624888090489894838009253941633257850621568309473902556912388065225096643874441046759871626985453222868538161694315775629640762836880760732228535091641476183956381458969463899410840960536267821064621427333394036525565649530603142680234969400335934316651459297773279665775606172582031407994198179607378245683762280037302885487251900834464581454650557929601414833921615734588139257095379769119277800826957735674444123062018757836325502728323789270710373802866393031428133241401624195671690574061419654342324638801248856147305207431992259611796250130992860241708340807605932320161268492288496255841312844061536738951487114256315111089745514203313820202931640957596464756010405845841566072044962867016515061920631004186422275908670900574606417856951911456055068251250406007519842261898059237118054444788072906395242548339221982707404473162376760846613033778706039803413197133493654622700563169937455508241780972810983291314403571877524768509857276937926433221599399876886660808368837838027643282775172273657572744784112294389733810861607423253291974813120197604178281965697475898164531258434135959862784130128185406283476649088690521047580882615823961985770122407044330583075869039319604603404973156583208672105913300903752823415539745394397715257455290510212310947321610753474825740775273986348298498340756937955646638621874569499279016572103701364433135817214311791398222983845847334440270964182851005072927748364550578634501100852987812389473928699540834346158807043959118985815145779177143619698728131459483783202081474982171858011389071228250905826817436220577475921417653715687725614904582904992461028630081535583308130101987675856234343538955409175623400844887526162643568648833519463720377293240094456246923254350400678027273837755376406726898636241037491410966718557050759098100246789880178271925953381282421954028302759408448955014676668389697996886241636313376393903373455801407636741877711055384225739499110186468219696581651485130494222369947714763069155468217682876200362777257723781365331611196811280792669481887201298643660768551639860534602297871557517947385246369446923087894265948217008051120322365496288169035739121368338393591756418733850510970271613915439590991598154654417336311656936031122249937969999226781732358023111862644575299135758175008199839236284615249881088960232244362173771618086357015468484058622329792853875623486556440536962622018963571028812361567512543338303270029097668650568557157505516727518899194129711337690149916181315171544007728650573189557450920330185304847113818315407324053319038462084036421763703911550639789000742853672196280903477974533320468368795868580237952218629120080742819551317948157624448298518461509704888027274721574688131594750409732115080498190455803416826949787141316063210686391511681774304792596709376" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**10000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Floor Division" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3/2" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3//2" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3%2" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4%2" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "even number\n" ] } ], "source": [ "if 22%2 == 0:\n", " print('even number')" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "16%3" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+2*3" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1+2)*3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Datatype Conversions, Type Names" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'abc'\n", "type(s)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '666'\n", "type(s)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " can only concatenate str (not \"int\") to str\n" ] } ], "source": [ "try:\n", " s += 1\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = int(s, 10)\n", "i" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "72" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '0x48'\n", "int(s, 16)" ] }, { "cell_type": "code", "execution_count": 330, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.234" ] }, "execution_count": 330, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '1.234'\n", "float(s)" ] }, { "cell_type": "code", "execution_count": 332, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " could not convert string to float: '1,234'\n" ] } ], "source": [ "s = '1,234' # hm. comma?\n", "try:\n", " float(s)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 333, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1.234'" ] }, "execution_count": 333, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '1,234' # hm. comma? have to correct this!\n", "s.replace(',', '.')" ] }, { "cell_type": "code", "execution_count": 334, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.234" ] }, "execution_count": 334, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(s.replace(',', '.'))" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'666'" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 666\n", "s = str(i)\n", "s" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"{'eins': 1, 'zwei': 2}\"" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = {'eins': 1, 'zwei': 2}\n", "dstr = str(d)\n", "dstr" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "i = 666\n", "print(type(i))" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666.0" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = float(i)\n", "f" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = 666.777\n", "i = int(f)\n", "i" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "667" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(f)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'abc'\n", "l = list(s)\n", "l" ] }, { "cell_type": "code", "execution_count": 56, "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(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Type names are variables** (or so)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(666)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 666\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(int)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(bool)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(dict)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('666')" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(int)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At this point, the name \"int\" refer to the integer type (\"int\" is a variable that refers to a type object).\n", "Now, we assign an int object to the \"int\" variable." ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "int = 666" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The type int is apparently gone!" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(int)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'int' object is not callable\n" ] } ], "source": [ "try:\n", " i = int('666')\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "WTF?? How to repair this??!!" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(666) # int is still alive!!" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [], "source": [ "int = type(666)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(int)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('666')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compound Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1, 2, 3]\n", "l" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: is something in the list?" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in l" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 2 in l" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 not in l" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "100 in l" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [], "source": [ "l.append(4)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [], "source": [ "l.extend([5, 6, 7])" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7]" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Attention**" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, [8, 9, 10]]" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append([8, 9, 10]) # appends only one element of type list!\n", "l" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, [8, 9, 10], 'a', 'b', 'c']" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.extend('abc') # appends three elements of type str (one character each)\n", "l" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, [8, 9, 10], 'a', 'b', 'c', 0, 1, 2, 3]" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.extend(range(4))\n", "l" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuple" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [], "source": [ "t = (1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3)" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(t)" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'tuple' object has no attribute 'append'\n" ] } ], "source": [ "try:\n", " t.append(4)\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map = {\n", " 'eins': 1,\n", " 'zwei': 2,\n", "}\n", "type(map)" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(map)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'zwei' in map" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'drei' in map" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map['eins']" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [], "source": [ "map['drei'] = 3" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'eins': 1, 'zwei': 2, 'drei': 3}" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [], "source": [ "map['zwei'] = 2.0" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'eins': 1, 'zwei': 2.0, 'drei': 3}" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'vier'\n" ] } ], "source": [ "try:\n", " map['vier']\n", "except KeyError as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [], "source": [ "s = {1, 2, 3}" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(s)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in s" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [], "source": [ "s.add(100)" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 100}" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [], "source": [ "s.add('/path/to/something')" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'/path/to/something', 1, 100, 2, 3}" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1}" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{1, 2, 3} - {2, 3, 4} # difference" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: can I use a string as a set?" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " unsupported operand type(s) for -: 'str' and 'str'\n" ] } ], "source": [ "try:\n", " \"abc\" - \"bcd\"\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a', 'b', 'c'}" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set(\"abc\")\n", "s" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set(range(10))\n", "s" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1000, 3, 'vier'}" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set([3, 1000, 'vier'])\n", "s" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 in s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Boolean" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 < 2" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(1<2)" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [], "source": [ "b = True" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hm\n" ] } ], "source": [ "if 1:\n", " print('hm')" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hm\n" ] } ], "source": [ "if 100:\n", " print('hm')" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [], "source": [ "if 0:\n", " print('hm')" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Attention**: boolean values of compound types" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = {}\n", "bool(s)" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool('')" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool('abc')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## NoneType" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "NoneType" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = None\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [], "source": [ "def f(s):\n", " if s:\n", " print('jo')\n", " else:\n", " print('na')" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "jo\n" ] } ], "source": [ "f({1, 2, 3})" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "na\n" ] } ], "source": [ "f({})" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "na\n" ] } ], "source": [ "f(None)" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [], "source": [ "def f(s):\n", " if s is None:\n", " print('na')\n", " else:\n", " print('ja')" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "na\n" ] } ], "source": [ "f(None)" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ja\n" ] } ], "source": [ "f({})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Short Circuit Evaluation of Boolean Expression " ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or False" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "jo\n" ] } ], "source": [ "if 1 in {1, 2} or type('abc') is int:\n", " print('jo')" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False or True" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and False" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False and True" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "na\n" ] } ], "source": [ "if 1 in {1, 2} and type('abc') is int:\n", " print('jo')\n", "else:\n", " print('na')" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False or False" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and True" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(print('hallo')) # print has a boolean value" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 == 2 and print('hallo') # note: we do not see 'hallo'" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo\n" ] } ], "source": [ "1 == 2 or print('hallo')" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise: Mixed List" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [], "source": [ "d = {'eins': 1, 'zwei': 2} # dict" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [], "source": [ "xxx = 1, 2" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xxx" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(xxx)" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [], "source": [ "d = {'eins':1}, {'zwei': 2} # tuple :-) :-) :-)" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Day 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python Documentation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**String examples**" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [], "source": [ "s = 'mississippi'" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.count('ss')" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Mississippi'" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.capitalize()" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'mississippi'" ] }, "execution_count": 152, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [], "source": [ "s = '1,2,3,4'" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [], "source": [ "items = s.split(',')" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['1', '2', '3', '4']" ] }, "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ "items" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1-2-3-4'" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'-'.join(items)" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1-2-3-4'" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.replace(',', '-')" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'dusan+alex'" ] }, "execution_count": 158, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 'dusan'\n", "b = 'alex'\n", "'+'.join([a, b])" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [], "source": [ "s = ' das sind die nutzdaten, alles andere ist mist '" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'das sind die nutzdaten, alles andere ist mist'" ] }, "execution_count": 160, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.strip()" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['das', 'sind', 'die', 'nutzdaten,', 'alles', 'andere', 'ist', 'mist']" ] }, "execution_count": 161, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.strip().split(' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Regular Expressions**" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [], "source": [ "s = '2021-11-17' # hm: year, month, day" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 163, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import re\n", "match = re.search('^(\\d{4})-(\\d+)-(\\d+)$', s)\n", "match" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2021'" ] }, "execution_count": 164, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match.group(1)" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'11'" ] }, "execution_count": 165, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match.group(2)" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'17'" ] }, "execution_count": 166, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match.group(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Where is ``pi``**" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.141592653589793" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "math.pi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Random numbers**" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "61" ] }, "execution_count": 169, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randrange(10, 100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Paths, Filenames, Directories**" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [], "source": [ "import os.path" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [], "source": [ "dirname = '/home/jfasch'\n", "filename = '.bashrc'" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/home/jfasch/.bashrc'" ] }, "execution_count": 172, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.path.join(dirname, filename)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``while``" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n", "the sum is: 55\n" ] } ], "source": [ "# sum of number 1..10\n", "i = 1\n", "s = 0 # the future sum\n", "while i <= 10:\n", " print(i)\n", " s += i\n", " i += 1\n", "print('the sum is:', s)" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay!\n" ] } ], "source": [ "# roll dice (at most 10 times) until we see 6\n", "import random\n", "\n", "win = False\n", "n = 0\n", "while n < 10:\n", " rnd = random.randrange(1, 7)\n", " if rnd == 6:\n", " win = True\n", " break\n", " n += 1\n", "if win:\n", " print('yay!')\n", "else:\n", " print('you lost!')" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay!\n" ] } ], "source": [ "# roll dice (at most 10 times) until we see 6\n", "import random\n", "\n", "n = 0\n", "while n < 10:\n", " rnd = random.randrange(1, 7)\n", " if rnd == 6:\n", " print('yay!')\n", " break\n", " n += 1\n", "else:\n", " print('you lost!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``for``" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay!\n" ] } ], "source": [ "# roll dice (at most 10 times) until we see 6\n", "import random\n", "\n", "for _ in range(10): # use '_' to conventionally state that we do not use this variable\n", " rnd = random.randrange(1, 7)\n", " if rnd == 6:\n", " print('yay!')\n", " break\n", "else:\n", " print('you lost!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iteration over Compound Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**List**" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "drei\n" ] } ], "source": [ "l = [1, 2, 'drei']\n", "for element in l:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Tuple**" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "drei\n" ] } ], "source": [ "t = (1, 2, 'drei')\n", "for element in t:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Set**" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0, 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 3, 4, 5, 6, 7, 8, 9, 'drei'}" ] }, "execution_count": 179, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set()\n", "for i in range(20):\n", " s.add(i)\n", "s.add('drei')\n", "s" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n", "11\n", "12\n", "13\n", "14\n", "15\n", "16\n", "17\n", "18\n", "19\n", "drei\n" ] } ], "source": [ "for element in s:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Dictionary**" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [], "source": [ "map = {\n", " 'eins': 1,\n", " 'zwei': 2,\n", " 'drei': 3,\n", "}" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eins\n", "zwei\n", "drei\n" ] } ], "source": [ "for element in map: # implicitly iterates over keys\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eins\n", "zwei\n", "drei\n" ] } ], "source": [ "for key in map.keys(): # explicitly iterates over keys (better)\n", " print(key)" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for value in map.values():\n", " print(value)" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('eins', 1) \n", "('zwei', 2) \n", "('drei', 3) \n" ] } ], "source": [ "for element in map.items(): # pairwise iteration -> tuples\n", " print(element, type(element))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Tuple unpacking**" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 186, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b = 1, 2\n", "a, b" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [], "source": [ "a, b = b, a" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: eins , value: 1\n", "key: zwei , value: 2\n", "key: drei , value: 3\n" ] } ], "source": [ "for element in map.items():\n", " key = element[0]\n", " value = element[1]\n", " print('key:', key, ', value:', value)" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: eins , value: 1\n", "key: zwei , value: 2\n", "key: drei , value: 3\n" ] } ], "source": [ "for key, value in map.items():\n", " print('key:', key, ', value:', value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Tuple unpacking, Database-wise**" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SVNr: 1, Firstname: Joerg, Lastname: Faschingbauer\n", "SVNr: 3, Firstname: Johanna, Lastname: Faschingbauer\n", "SVNr: 2, Firstname: Caro, Lastname: Faschingbauer\n" ] } ], "source": [ "resultset = [ # SVNr, Firstname, Lastname\n", " (1, 'Joerg', 'Faschingbauer'),\n", " (3, 'Johanna', 'Faschingbauer'),\n", " (2, 'Caro', 'Faschingbauer'),\n", "]\n", "for svnr, first, last in resultset:\n", " print(f'SVNr: {svnr}, Firstname: {first}, Lastname: {last}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## File IO" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [], "source": [ "f = open('passwd')" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<_io.TextIOWrapper name='passwd' mode='r' encoding='UTF-8'>" ] }, "execution_count": 192, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'root:x:0:0:root:/root:/bin/bash\\n'" ] }, "execution_count": 193, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line = f.readline()\n", "line" ] }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'bin:x:1:1:bin:/bin:/sbin/nologin\\n'" ] }, "execution_count": 194, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line = f.readline()\n", "line" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [], "source": [ "f = open('passwd')" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "root:x:0:0:root:/root:/bin/bash\n", "\n", "bin:x:1:1:bin:/bin:/sbin/nologin\n", "\n", "daemon:x:2:2:daemon:/sbin:/sbin/nologin\n", "\n", "adm:x:3:4:adm:/var/adm:/sbin/nologin\n", "\n", "lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n", "\n", "sync:x:5:0:sync:/sbin:/bin/sync\n", "\n", "shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\n", "\n", "halt:x:7:0:halt:/sbin:/sbin/halt\n", "\n", "mail:x:8:12:mail:/var/spool/mail:/sbin/nologin\n", "\n", "operator:x:11:0:operator:/root:/sbin/nologin\n", "\n", "games:x:12:100:games:/usr/games:/sbin/nologin\n", "\n", "ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin\n", "\n", "nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin\n", "\n", "apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin\n", "\n", "systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin\n", "\n", "systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin\n", "\n", "systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin\n", "\n", "systemd-oom:x:998:996:systemd Userspace OOM Killer:/:/sbin/nologin\n", "\n", "systemd-timesync:x:997:995:systemd Time Synchronization:/:/sbin/nologin\n", "\n", "tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin\n", "\n", "dbus:x:81:81:System message bus:/:/sbin/nologin\n", "\n", "polkitd:x:996:994:User for polkitd:/:/sbin/nologin\n", "\n", "avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin\n", "\n", "unbound:x:995:992:Unbound DNS resolver:/etc/unbound:/sbin/nologin\n", "\n", "dnsmasq:x:994:991:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin\n", "\n", "nm-openconnect:x:993:989:NetworkManager user for OpenConnect:/:/sbin/nologin\n", "\n", "usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin\n", "\n", "gluster:x:992:988:GlusterFS daemons:/run/gluster:/sbin/nologin\n", "\n", "rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin\n", "\n", "pipewire:x:991:987:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin\n", "\n", "geoclue:x:990:986:User for geoclue:/var/lib/geoclue:/sbin/nologin\n", "\n", "chrony:x:989:984::/var/lib/chrony:/sbin/nologin\n", "\n", "saslauth:x:988:76:Saslauthd user:/run/saslauthd:/sbin/nologin\n", "\n", "radvd:x:75:75:radvd user:/:/sbin/nologin\n", "\n", "rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin\n", "\n", "qemu:x:107:107:qemu user:/:/sbin/nologin\n", "\n", "openvpn:x:987:982:OpenVPN:/etc/openvpn:/sbin/nologin\n", "\n", "nm-openvpn:x:986:981:Default user for running openvpn spawned by NetworkManager:/:/sbin/nologin\n", "\n", "colord:x:985:980:User for colord:/var/lib/colord:/sbin/nologin\n", "\n", "rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin\n", "\n", "abrt:x:173:173::/etc/abrt:/sbin/nologin\n", "\n", "flatpak:x:984:979:User for flatpak system helper:/:/sbin/nologin\n", "\n", "gdm:x:42:42::/var/lib/gdm:/sbin/nologin\n", "\n", "gnome-initial-setup:x:983:978::/run/gnome-initial-setup/:/sbin/nologin\n", "\n", "vboxadd:x:982:1::/var/run/vboxadd:/sbin/nologin\n", "\n", "sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin\n", "\n", "tcpdump:x:72:72::/:/sbin/nologin\n", "\n", "jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash\n", "\n", "mosquitto:x:981:974:Mosquitto Broker:/etc/mosquitto:/sbin/nologin\n", "\n" ] } ], "source": [ "for line in f:\n", " print(line)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Day 3" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [], "source": [ "def maximum(a, b):\n", " if a < b:\n", " return b\n", " else:\n", " return a" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 198, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(1, 2)" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'b'" ] }, "execution_count": 199, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum('a', 'b')" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 5, 6]" ] }, "execution_count": 200, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum([1, 2, 3], [4, 5, 6])" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " '<' not supported between instances of 'int' and 'str'\n" ] } ], "source": [ "try:\n", " maximum(1, 'abc')\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [], "source": [ "class A:\n", " def __init__(self, a, b):\n", " self.a = a\n", " self.b = b\n", " def doit(self):\n", " print(self.a, self.b)" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 3\n" ] } ], "source": [ "a = A(2, 3)\n", "a.doit()" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 204, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(A)" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "mappingproxy({'__module__': '__main__',\n", " '__init__': ,\n", " 'doit': ,\n", " '__dict__': ,\n", " '__weakref__': ,\n", " '__doc__': None})" ] }, "execution_count": 205, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.__dict__" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [], "source": [ "# s = 'def xxx(self): print(\"xxx jessas\")'\n", "# exec(s, A.__dict__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The ``range()`` Function" ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for i in [0,1,2,3,4,5,6,7,8,9]:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for i in range(10):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [], "source": [ "for i in range(10, 2): # begin 10, end 2 -> nothing in between\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "2\n", "4\n", "6\n", "8\n" ] } ], "source": [ "for i in range(0, 10, 2):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "2\n", "4\n", "6\n", "8\n", "10\n" ] } ], "source": [ "# 10+1 = 11\n", "for i in range(0, 10+1, 2):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generators, Iterator Protocol, ``for``" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 3)" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range(3)" ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 3)" ] }, "execution_count": 213, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = range(3)\n", "r" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range" ] }, "execution_count": 214, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(r)" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for i in r:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [], "source": [ "r = range(3)" ] }, { "cell_type": "code", "execution_count": 217, "metadata": {}, "outputs": [], "source": [ "it = iter(r)" ] }, { "cell_type": "code", "execution_count": 218, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range_iterator" ] }, "execution_count": 218, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(it)" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 219, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = next(it)\n", "i" ] }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 220, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = next(it)\n", "i" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 221, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = next(it)\n", "i" ] }, { "cell_type": "code", "execution_count": 222, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "try:\n", " i = next(it)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Common Sequence Operation" ] }, { "cell_type": "code", "execution_count": 223, "metadata": {}, "outputs": [], "source": [ "s = 'abc'" ] }, { "cell_type": "code", "execution_count": 224, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abcabc'" ] }, "execution_count": 224, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s + s " ] }, { "cell_type": "code", "execution_count": 225, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abcabc'" ] }, "execution_count": 225, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s*2" ] }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 1, 2, 3]" ] }, "execution_count": 226, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1, 2, 3]*2" ] }, { "cell_type": "code", "execution_count": 227, "metadata": {}, "outputs": [], "source": [ "l = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Slicing**" ] }, { "cell_type": "code", "execution_count": 228, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['c', 'd']" ] }, "execution_count": 228, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[2:4]" ] }, { "cell_type": "code", "execution_count": 229, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'cd'" ] }, "execution_count": 229, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abcde'[2:4]" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['b', 'd', 'f']" ] }, "execution_count": 230, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[1:6:2]" ] }, { "cell_type": "code", "execution_count": 231, "metadata": {}, "outputs": [], "source": [ "l[2:4] = [1, 2, 3, 4]" ] }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 1, 2, 3, 4, 'e', 'f', 'g', 'h']" ] }, "execution_count": 232, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comprehensions" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3, 4, 5]" ] }, { "cell_type": "code", "execution_count": 234, "metadata": {}, "outputs": [], "source": [ "def squares(inputlist):\n", " ret = []\n", " for i in inputlist:\n", " ret.append(i**2)\n", " return ret" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 16, 25]" ] }, "execution_count": 235, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sq = squares(l)\n", "sq" ] }, { "cell_type": "code", "execution_count": 236, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 16, 25]" ] }, "execution_count": 236, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squares(range(1, 6))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**List Comprehension**" ] }, { "cell_type": "code", "execution_count": 237, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 237, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sq = [i**2 for i in l]\n", "type(sq)" ] }, { "cell_type": "code", "execution_count": 238, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 16, 25]" ] }, "execution_count": 238, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sq" ] }, { "cell_type": "code", "execution_count": 239, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" ] }, "execution_count": 239, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[i**2 for i in range(10)]" ] }, { "cell_type": "code", "execution_count": 240, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 4, 16, 36, 64]" ] }, "execution_count": 240, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[i**2 for i in range(10) if i%2 == 0]" ] }, { "cell_type": "code", "execution_count": 241, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 4, 16, 36, 64]" ] }, "execution_count": 241, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[i**2 for i in range(0, 10, 2)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Set Comprehensions**" ] }, { "cell_type": "code", "execution_count": 242, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "set" ] }, "execution_count": 242, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wtf = {i**2 for i in range(10) if i%2 == 0}\n", "type(wtf)" ] }, { "cell_type": "code", "execution_count": 243, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0, 4, 16, 36, 64}" ] }, "execution_count": 243, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wtf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Dictionary Comprehension**" ] }, { "cell_type": "code", "execution_count": 244, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 244, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wtf = {i: i**2 for i in range(10)}\n", "type(wtf)" ] }, { "cell_type": "code", "execution_count": 245, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}\n" ] } ], "source": [ "print(wtf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List, Iterable? WTF?" ] }, { "cell_type": "code", "execution_count": 246, "metadata": {}, "outputs": [], "source": [ "l = list(range(10))" ] }, { "cell_type": "code", "execution_count": 247, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 247, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 248, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 248, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = list('abc')\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dict and iterable" ] }, { "cell_type": "code", "execution_count": 249, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eins 1\n", "zwei 2\n", "drei 3\n" ] } ], "source": [ "l = [('eins', 1), ('zwei', 2), ('drei', 3)]\n", "for k, v in l:\n", " print(k, v)" ] }, { "cell_type": "code", "execution_count": 250, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'eins': 1, 'zwei': 2, 'drei': 3}" ] }, "execution_count": 250, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map = dict(l)\n", "map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More on Lists" ] }, { "cell_type": "code", "execution_count": 251, "metadata": {}, "outputs": [], "source": [ "l = [3, 2, 1, 4, 7, 5]" ] }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [], "source": [ "del l[2]" ] }, { "cell_type": "code", "execution_count": 253, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 4, 7, 5]" ] }, "execution_count": 253, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 254, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 4, 5, 7]" ] }, "execution_count": 254, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(l) # returns a sorted list" ] }, { "cell_type": "code", "execution_count": 255, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 4, 7, 5]" ] }, "execution_count": 255, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 256, "metadata": {}, "outputs": [], "source": [ "l.sort() # sort l in place" ] }, { "cell_type": "code", "execution_count": 257, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 4, 5, 7]" ] }, "execution_count": 257, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 258, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reversed(l)" ] }, { "cell_type": "code", "execution_count": 259, "metadata": {}, "outputs": [], "source": [ "l.reverse()" ] }, { "cell_type": "code", "execution_count": 260, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[7, 5, 4, 3, 2]" ] }, "execution_count": 260, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More on Dictionaries" ] }, { "cell_type": "code", "execution_count": 261, "metadata": {}, "outputs": [], "source": [ "map = {'eins': 1, 'zwei': 2}" ] }, { "cell_type": "code", "execution_count": 262, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 262, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map['eins']" ] }, { "cell_type": "code", "execution_count": 263, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'drei'\n" ] } ], "source": [ "try:\n", " map['drei']\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 264, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "not found\n" ] } ], "source": [ "if map.get('drei') is None: # \n", " print('not found')\n", "else:\n", " print('found')" ] }, { "cell_type": "code", "execution_count": 265, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "found\n" ] } ], "source": [ "if map.get('zwei') is None: # \n", " print('not found')\n", "else:\n", " print('found')" ] }, { "cell_type": "code", "execution_count": 266, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 266, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = map.get('zwei')\n", "v" ] }, { "cell_type": "code", "execution_count": 267, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 267, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v = map.get('drei', 3)\n", "v" ] }, { "cell_type": "code", "execution_count": 268, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'eins': 1, 'zwei': 2}" ] }, "execution_count": 268, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map" ] }, { "cell_type": "code", "execution_count": 269, "metadata": {}, "outputs": [], "source": [ "del map['zwei']" ] }, { "cell_type": "code", "execution_count": 270, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'eins': 1}" ] }, "execution_count": 270, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map" ] }, { "cell_type": "code", "execution_count": 271, "metadata": {}, "outputs": [], "source": [ "other_map = {'eins': 1, 'zwei': 2, 'vier': 4}" ] }, { "cell_type": "code", "execution_count": 272, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'eins': 1, 'zwei': 2, 'vier': 4}" ] }, "execution_count": 272, "metadata": {}, "output_type": "execute_result" } ], "source": [ "map.update(other_map)\n", "map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### References, Mutability" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Integers are Immutable**" ] }, { "cell_type": "code", "execution_count": 273, "metadata": {}, "outputs": [], "source": [ "a = 10\n", "b = a" ] }, { "cell_type": "code", "execution_count": 274, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140575811316304" ] }, "execution_count": 274, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 275, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140575811316304" ] }, "execution_count": 275, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 276, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 276, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = 42\n", "a" ] }, { "cell_type": "code", "execution_count": 277, "metadata": {}, "outputs": [], "source": [ "a = 10\n", "b = a" ] }, { "cell_type": "code", "execution_count": 278, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 278, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 42\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Lists are Mutable**" ] }, { "cell_type": "code", "execution_count": 279, "metadata": {}, "outputs": [], "source": [ "l1 = [1, 2, 3]\n", "l2 = l1" ] }, { "cell_type": "code", "execution_count": 280, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140575692459008" ] }, "execution_count": 280, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1)" ] }, { "cell_type": "code", "execution_count": 281, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140575692459008" ] }, "execution_count": 281, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l2)" ] }, { "cell_type": "code", "execution_count": 282, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 282, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2.append(4)\n", "l2" ] }, { "cell_type": "code", "execution_count": 283, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 283, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Protect against such errors?**" ] }, { "cell_type": "code", "execution_count": 284, "metadata": {}, "outputs": [], "source": [ "l1 = [1, 2, 3]\n", "l2 = l1[:] # slice [begin:end] -> elementwise copy" ] }, { "cell_type": "code", "execution_count": 285, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 285, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 286, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 286, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 287, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140575692438848" ] }, "execution_count": 287, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1)" ] }, { "cell_type": "code", "execution_count": 288, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140575692448960" ] }, "execution_count": 288, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l2)" ] }, { "cell_type": "code", "execution_count": 289, "metadata": {}, "outputs": [], "source": [ "l2.append(4)" ] }, { "cell_type": "code", "execution_count": 290, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 290, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 292, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 292, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Hm ...**" ] }, { "cell_type": "code", "execution_count": 293, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 293, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 = [1, 2, ['drei', 'vier'], 5, 6]\n", "len(l1)" ] }, { "cell_type": "code", "execution_count": 295, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['drei', 'vier'], 5, 6]" ] }, "execution_count": 295, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2 = l1[:]\n", "l2" ] }, { "cell_type": "code", "execution_count": 296, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['drei', 'vier', 'fuenf'], 5, 6]" ] }, "execution_count": 296, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2[2].append('fuenf')\n", "l2" ] }, { "cell_type": "code", "execution_count": 298, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['drei', 'vier', 'fuenf'], 5, 6]" ] }, "execution_count": 298, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---> **Shallow copy**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Deep copy?**" ] }, { "cell_type": "code", "execution_count": 300, "metadata": {}, "outputs": [], "source": [ "l1 = [1, 2, ['drei', 'vier'], 5, 6]" ] }, { "cell_type": "code", "execution_count": 301, "metadata": {}, "outputs": [], "source": [ "import copy" ] }, { "cell_type": "code", "execution_count": 302, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['drei', 'vier'], 5, 6]" ] }, "execution_count": 302, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2 = copy.deepcopy(l1)\n", "l2" ] }, { "cell_type": "code", "execution_count": 303, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['drei', 'vier', 'fuenf'], 5, 6]" ] }, "execution_count": 303, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2[2].append('fuenf')\n", "l2" ] }, { "cell_type": "code", "execution_count": 304, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, ['drei', 'vier'], 5, 6]" ] }, "execution_count": 304, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### CSV" ] }, { "cell_type": "code", "execution_count": 305, "metadata": {}, "outputs": [], "source": [ "import csv" ] }, { "cell_type": "code", "execution_count": 314, "metadata": {}, "outputs": [], "source": [ "f = open('code/beispiel.csv')" ] }, { "cell_type": "code", "execution_count": 315, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['FUND_NAME', 'FUND_ISIN', 'FUND_TICKER', 'AS_OF_DATE', 'FUND_IN_UNIVERSE', 'FUND_OF_FUNDS', 'FUND_HOLDINGS_COUNT', 'FUND_HOLDING_FUNDS_COUNT']\n", "['American Funds American Balanced Fund;A', 'US0240711020', 'ABALX', '20210103', 'T', '', '2,084', '0']\n", "['Dodge & Cox Balanced Fund', 'US2562011047', 'DODBX', '20210103', 'T', 'T', '410', '1']\n", "['Dodge & Cox Stock Fund', 'US2562191062', 'DODGX', '20210103', 'T', 'T', '74', '1']\n", "['Franklin Income Fund;A1', 'US3534963000', 'FKINX', '20210103', 'T', 'T', '302', '1']\n", "['American Funds Growth Fund of America;A', 'US3998741066', 'AGTHX', '20210103', 'T', '', '378', '0']\n", "['T Rowe Price Growth Stock Fund', 'US7414791092', 'PRGFX', '20210103', 'T', 'T', '87', '1']\n", "['Fidelity Puritan Fund', 'US3163451079', 'FPURX', '20210103', 'T', '', '1,124', '0']\n", "['Fidelity Contrafund', 'US3160711095', 'FCNTX', '20210103', 'T', '', '363', '0']\n", "['T Rowe Price Capital Appreciation Fund', 'US77954M1053', 'PRWCX', '20210103', 'T', 'T', '165', '2']\n", "['Old Westbury Large Cap Strategies Fund', 'US6804141090', 'OWLSX', '20210103', 'T', 'T', '199', '2']\n", "['American Funds Capital Income Builder;A', 'US1401931035', 'CAIBX', '20210103', 'T', '', '1,582', '0']\n", "['Harbor Capital Appreciation Fund;Institutional', 'US4115115044', 'HACAX', '20210103', 'T', '', '55', '0']\n", "['T Rowe Price Blue Chip Growth Fund', 'US77954Q1067', 'TRBCX', '20210103', 'T', 'T', '123', '1']\n", "['Dreyfus Treasury Securities Cash Management;Inst', 'US2619411083', 'DIRXX', '20210103', 'T', '', '50', '0']\n", "['BlackRock Global Allocation Fund;Institutional', 'US09251T5092', 'MALOX', '20210103', 'T', 'T', '1,241', '16']\n", "['Fidelity Low-Priced Stock Fund', 'US3163453059', 'FLPSX', '20210103', 'T', '', '799', '0']\n", "['BlackRock Liquidity Treasury Trust Fund;Inst', 'US09248U5517', 'TTTXX', '20210103', 'T', '', '63', '0']\n", "['DFA Five-Year Global Fixed Income Portfolio;Inst', 'US2332038841', 'DFGBX', '20210103', 'T', '', '350', '0']\n", "['Federated Hermes US Treasury Cash Reserves;Inst', 'US60934N6821', 'UTIXX', '20210103', 'T', '', '55', '0']\n", "['Oakmark International Fund;Investor', 'US4138382027', 'OAKIX', '20210103', 'T', '', '80', '0']\n", "['Advanced Srs T Rowe Price Asset Allocation Port', 'US00767H4939', '', '20210103', 'T', '', '2,787', '0']\n", "['FPA Crescent Fund;Inst', 'US30254T7596', 'FPACX', '20210103', 'T', '', '138', '0']\n", "['Franklin Income Fund;C', 'US3534968058', 'FCISX', '20210103', 'T', 'T', '302', '1']\n", "['MFS Value Fund;I', 'US5529836943', 'MEIIX', '20210103', 'T', '', '76', '0']\n", "['Goldman Sachs FS Treasury Instruments Fd;Inst', 'US38142B5003', 'FTIXX', '20210103', 'T', '', '67', '0']\n", "['Schwab S&P 500 Index Fund', 'US8085098551', 'SWPPX', '20210103', 'T', '', '508', '0']\n", "['Templeton Global Bond Fund;Advisor', 'US8802084009', 'TGBAX', '20210103', 'T', 'T', '82', '1']\n", "['First Eagle Global Fund;I', 'US32008F6060', 'SGIIX', '20210103', 'T', 'T', '225', '1']\n", "['iShares Core S&P 500 ETF', 'US4642872000', 'IVV', '20210103', 'T', 'T', '507', '1']\n", "['Vanguard 500 Index Fund;Admiral', 'US9229087104', 'VFIAX', '20210103', 'T', '', '510', '0']\n", "['Vanguard Total Stock Market Index Fund;Admiral', 'US9229087286', 'VTSAX', '20210103', 'T', '', '3,370', '0']\n", "['SPDR S&P MidCap 400 ETF', 'US78467Y1073', 'MDY', '20210103', 'T', '', '400', '0']\n", "['SPDR Dow Jones Industrial Average ETF Trust', 'US78467X1090', 'DIA', '20210103', 'T', '', '31', '0']\n", "['Health Care Select Sector SPDR Fund', 'US81369Y2090', 'XLV', '20210103', 'T', 'T', '64', '1']\n", "['Consumer Discretionary Select Sector SPDR Fund', 'US81369Y4070', 'XLY', '20210103', 'T', 'T', '62', '1']\n", "['Energy Select Sector SPDR Fund', 'US81369Y5069', 'XLE', '20210103', 'T', 'T', '28', '1']\n", "['Financial Select Sector SPDR Fund', 'US81369Y6059', 'XLF', '20210103', 'T', 'T', '66', '1']\n", "['Technology Select Sector SPDR Fund', 'US81369Y8030', 'XLK', '20210103', 'T', 'T', '74', '1']\n", "['Invesco QQQ Trust Series 1', 'US46090E1038', 'QQQ', '20210103', 'T', '', '104', '0']\n", "['Dodge & Cox International Stock Fund', 'US2562061034', 'DODFX', '20210103', 'T', 'T', '80', '1']\n", "['iShares MSCI EAFE ETF', 'US4642874659', 'EFA', '20210103', 'T', 'T', '897', '1']\n", "['JPMorgan 100% US Treasury Secs Mny Mkt Fd;Inst', 'US4812A28358', 'JTSXX', '20210103', 'T', '', '41', '0']\n", "['Invesco S&P 500 Eql Wght ETF', 'US46137V3574', 'RSP', '20210103', 'T', 'T', '507', '1']\n", "['JPMorgan 100% US Treasury Secs Mny Mkt Fd;Capital', 'US4812A03757', 'CJTXX', '20210103', 'T', '', '41', '0']\n", "['Invesco Developing Markets Fund;Y', 'US00143W8753', 'ODVYX', '20210103', 'T', 'T', '72', '3']\n", "['SPDR S&P Dividend ETF', 'US78464A7634', 'SDY', '20210103', 'T', 'T', '118', '1']\n", "['DFA International Core Equity Portfolio;Inst', 'US2332033719', 'DFIEX', '20210103', 'T', '', '4,888', '0']\n", "['Edgewood Growth Fund;Institutional', 'US0075W07594', 'EGFIX', '20210103', 'T', 'T', '23', '1']\n", "['Advanced Series Prudential Growth Allocation Port', 'US00767H7585', '', '20210103', 'T', 'T', '2,400', '3']\n", "['MFS Value Fund;R6', 'US55273H3536', 'MEIKX', '20210103', 'T', '', '76', '0']\n" ] } ], "source": [ "rdr = csv.reader(f, delimiter=';')\n", "for row in rdr:\n", " print(row)" ] }, { "cell_type": "code", "execution_count": 327, "metadata": {}, "outputs": [], "source": [ "f = open('code/beispiel.csv')" ] }, { "cell_type": "code", "execution_count": 328, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'FUND_NAME': 'American Funds American Balanced Fund;A', 'FUND_ISIN': 'US0240711020', 'FUND_TICKER': 'ABALX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '2,084', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Dodge & Cox Balanced Fund', 'FUND_ISIN': 'US2562011047', 'FUND_TICKER': 'DODBX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '410', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'Dodge & Cox Stock Fund', 'FUND_ISIN': 'US2562191062', 'FUND_TICKER': 'DODGX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '74', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'Franklin Income Fund;A1', 'FUND_ISIN': 'US3534963000', 'FUND_TICKER': 'FKINX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '302', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'American Funds Growth Fund of America;A', 'FUND_ISIN': 'US3998741066', 'FUND_TICKER': 'AGTHX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '378', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'T Rowe Price Growth Stock Fund', 'FUND_ISIN': 'US7414791092', 'FUND_TICKER': 'PRGFX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '87', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'Fidelity Puritan Fund', 'FUND_ISIN': 'US3163451079', 'FUND_TICKER': 'FPURX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '1,124', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Fidelity Contrafund', 'FUND_ISIN': 'US3160711095', 'FUND_TICKER': 'FCNTX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '363', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'T Rowe Price Capital Appreciation Fund', 'FUND_ISIN': 'US77954M1053', 'FUND_TICKER': 'PRWCX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '165', 'FUND_HOLDING_FUNDS_COUNT': '2'}\n", "{'FUND_NAME': 'Old Westbury Large Cap Strategies Fund', 'FUND_ISIN': 'US6804141090', 'FUND_TICKER': 'OWLSX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '199', 'FUND_HOLDING_FUNDS_COUNT': '2'}\n", "{'FUND_NAME': 'American Funds Capital Income Builder;A', 'FUND_ISIN': 'US1401931035', 'FUND_TICKER': 'CAIBX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '1,582', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Harbor Capital Appreciation Fund;Institutional', 'FUND_ISIN': 'US4115115044', 'FUND_TICKER': 'HACAX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '55', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'T Rowe Price Blue Chip Growth Fund', 'FUND_ISIN': 'US77954Q1067', 'FUND_TICKER': 'TRBCX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '123', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'Dreyfus Treasury Securities Cash Management;Inst', 'FUND_ISIN': 'US2619411083', 'FUND_TICKER': 'DIRXX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '50', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'BlackRock Global Allocation Fund;Institutional', 'FUND_ISIN': 'US09251T5092', 'FUND_TICKER': 'MALOX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '1,241', 'FUND_HOLDING_FUNDS_COUNT': '16'}\n", "{'FUND_NAME': 'Fidelity Low-Priced Stock Fund', 'FUND_ISIN': 'US3163453059', 'FUND_TICKER': 'FLPSX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '799', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'BlackRock Liquidity Treasury Trust Fund;Inst', 'FUND_ISIN': 'US09248U5517', 'FUND_TICKER': 'TTTXX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '63', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'DFA Five-Year Global Fixed Income Portfolio;Inst', 'FUND_ISIN': 'US2332038841', 'FUND_TICKER': 'DFGBX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '350', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Federated Hermes US Treasury Cash Reserves;Inst', 'FUND_ISIN': 'US60934N6821', 'FUND_TICKER': 'UTIXX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '55', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Oakmark International Fund;Investor', 'FUND_ISIN': 'US4138382027', 'FUND_TICKER': 'OAKIX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '80', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Advanced Srs T Rowe Price Asset Allocation Port', 'FUND_ISIN': 'US00767H4939', 'FUND_TICKER': '', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '2,787', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'FPA Crescent Fund;Inst', 'FUND_ISIN': 'US30254T7596', 'FUND_TICKER': 'FPACX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '138', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Franklin Income Fund;C', 'FUND_ISIN': 'US3534968058', 'FUND_TICKER': 'FCISX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '302', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'MFS Value Fund;I', 'FUND_ISIN': 'US5529836943', 'FUND_TICKER': 'MEIIX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '76', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Goldman Sachs FS Treasury Instruments Fd;Inst', 'FUND_ISIN': 'US38142B5003', 'FUND_TICKER': 'FTIXX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '67', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Schwab S&P 500 Index Fund', 'FUND_ISIN': 'US8085098551', 'FUND_TICKER': 'SWPPX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '508', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Templeton Global Bond Fund;Advisor', 'FUND_ISIN': 'US8802084009', 'FUND_TICKER': 'TGBAX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '82', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'First Eagle Global Fund;I', 'FUND_ISIN': 'US32008F6060', 'FUND_TICKER': 'SGIIX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '225', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'iShares Core S&P 500 ETF', 'FUND_ISIN': 'US4642872000', 'FUND_TICKER': 'IVV', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '507', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'Vanguard 500 Index Fund;Admiral', 'FUND_ISIN': 'US9229087104', 'FUND_TICKER': 'VFIAX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '510', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Vanguard Total Stock Market Index Fund;Admiral', 'FUND_ISIN': 'US9229087286', 'FUND_TICKER': 'VTSAX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '3,370', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'SPDR S&P MidCap 400 ETF', 'FUND_ISIN': 'US78467Y1073', 'FUND_TICKER': 'MDY', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '400', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'SPDR Dow Jones Industrial Average ETF Trust', 'FUND_ISIN': 'US78467X1090', 'FUND_TICKER': 'DIA', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '31', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Health Care Select Sector SPDR Fund', 'FUND_ISIN': 'US81369Y2090', 'FUND_TICKER': 'XLV', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '64', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'Consumer Discretionary Select Sector SPDR Fund', 'FUND_ISIN': 'US81369Y4070', 'FUND_TICKER': 'XLY', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '62', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'Energy Select Sector SPDR Fund', 'FUND_ISIN': 'US81369Y5069', 'FUND_TICKER': 'XLE', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '28', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'Financial Select Sector SPDR Fund', 'FUND_ISIN': 'US81369Y6059', 'FUND_TICKER': 'XLF', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '66', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'Technology Select Sector SPDR Fund', 'FUND_ISIN': 'US81369Y8030', 'FUND_TICKER': 'XLK', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '74', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'Invesco QQQ Trust Series 1', 'FUND_ISIN': 'US46090E1038', 'FUND_TICKER': 'QQQ', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '104', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Dodge & Cox International Stock Fund', 'FUND_ISIN': 'US2562061034', 'FUND_TICKER': 'DODFX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '80', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'iShares MSCI EAFE ETF', 'FUND_ISIN': 'US4642874659', 'FUND_TICKER': 'EFA', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '897', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'JPMorgan 100% US Treasury Secs Mny Mkt Fd;Inst', 'FUND_ISIN': 'US4812A28358', 'FUND_TICKER': 'JTSXX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '41', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Invesco S&P 500 Eql Wght ETF', 'FUND_ISIN': 'US46137V3574', 'FUND_TICKER': 'RSP', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '507', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'JPMorgan 100% US Treasury Secs Mny Mkt Fd;Capital', 'FUND_ISIN': 'US4812A03757', 'FUND_TICKER': 'CJTXX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '41', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Invesco Developing Markets Fund;Y', 'FUND_ISIN': 'US00143W8753', 'FUND_TICKER': 'ODVYX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '72', 'FUND_HOLDING_FUNDS_COUNT': '3'}\n", "{'FUND_NAME': 'SPDR S&P Dividend ETF', 'FUND_ISIN': 'US78464A7634', 'FUND_TICKER': 'SDY', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '118', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'DFA International Core Equity Portfolio;Inst', 'FUND_ISIN': 'US2332033719', 'FUND_TICKER': 'DFIEX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '4,888', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n", "{'FUND_NAME': 'Edgewood Growth Fund;Institutional', 'FUND_ISIN': 'US0075W07594', 'FUND_TICKER': 'EGFIX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '23', 'FUND_HOLDING_FUNDS_COUNT': '1'}\n", "{'FUND_NAME': 'Advanced Series Prudential Growth Allocation Port', 'FUND_ISIN': 'US00767H7585', 'FUND_TICKER': '', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': 'T', 'FUND_HOLDINGS_COUNT': '2,400', 'FUND_HOLDING_FUNDS_COUNT': '3'}\n", "{'FUND_NAME': 'MFS Value Fund;R6', 'FUND_ISIN': 'US55273H3536', 'FUND_TICKER': 'MEIKX', 'AS_OF_DATE': '20210103', 'FUND_IN_UNIVERSE': 'T', 'FUND_OF_FUNDS': '', 'FUND_HOLDINGS_COUNT': '76', 'FUND_HOLDING_FUNDS_COUNT': '0'}\n" ] } ], "source": [ "rdr = csv.DictReader(f, delimiter=';')\n", "for elem in rdr:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 4 }