{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Datatypes" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "i = 666" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings Intro" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hallo'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ein_string = 'hallo'\n", "ein_string" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo\n" ] } ], "source": [ "print(ein_string)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "ein_anderer_string = \"hallo\"" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo\n" ] } ], "source": [ "print(ein_anderer_string)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "s = 'ein paar zeichen, gefolgt von \"double quotes\", dann noch was'" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "s = \"abc ' fghjgfgh' \"" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"abc ' fghjgfgh' \"" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "s = '\"\" \\'dfghgfgh '" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\"\" \\'dfghgfgh '" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "2\n", "3\n", "3\n", "4\n", "vorbei\n" ] } ], "source": [ "for i in [1,2,3]:\n", " print(i)\n", " print(i+1)\n", "print('vorbei')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Doc Strings" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "def do_something(a, b):\n", " '''diese funktion ist komplett sinnlos, und nimmt parameter a und b, die integers\n", " sein sollten, sonst funktioniert das ganze hier nicht.\n", " \n", " der returnwert ist noch sinnloserer weise die summe der parameter.'''\n", " \n", " # hier wird die komplexe berechnung der summe vorgenommen. obacht!\n", " return a + b" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = do_something(1, 2)\n", "x" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(do_something)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'diese funktion ist komplett sinnlos, und nimmt parameter a und b, die integers\\n sein sollten, sonst funktioniert das ganze hier nicht.\\n \\n der returnwert ist noch sinnloserer weise die summe der parameter.'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "do_something.__doc__" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function do_something in module __main__:\n", "\n", "do_something(a, b)\n", " diese funktion ist komplett sinnlos, und nimmt parameter a und b, die integers\n", " sein sollten, sonst funktioniert das ganze hier nicht.\n", " \n", " der returnwert ist noch sinnloserer weise die summe der parameter.\n", "\n" ] } ], "source": [ "help(do_something)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Variablen" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name 'nix' is not defined \n" ] } ], "source": [ "try:\n", " print(nix)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "nix = 666\n", "print(nix)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Object Identity" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672599989840" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 42\n", "id(a)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672599989840" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a\n", "id(b)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "b = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672464974720" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Integers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hoechste Integer Zahl auf 64 Bit Rechnern:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551615" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num = 2**64 -1\n", "num" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "num = num +1" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "num = 2**64" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9990020930143845079440327643300335909804291390541816917715292738631458324642573483274873313324496504031643944455558549300187996607656176562908471354247492875198889629873671093246350427373112479265800278531241088737085605287228390164568691026850675923517914697052857644696801524832345475543250292786520806957770971741102232042976351205330777996897925116619870771785775955521720081320295204617949229259295623920965797873558158667525495797313144806249260261837941305080582686031535134178739622834990886357758062104606636372130587795322344972010808486369541401835851359858035603574021872908155566580607186461268972839794621842267579349638893357247588761959137656762411125020708704870465179396398710109200363934745618090601613377898560296863598558024761448933047052222860131377095958357319485898496404572383875170702242332633436894423297381877733153286944217936125301907868903603663283161502726139934152804071171914923903341874935394455896301292197256417717233543544751552379310892268182402452755752094704642185943862865632744231332084742221551493315002717750064228826211822549349600557457334964678483269180951895955769174509673224417740432840455882109137905375646772139976621785265057169854834562487518322383250318645505472114369934167981678170255122812978065194806295405339154657479941297499190348507544336414505631657396006693382427316434039580121280260984212247514207834712224831410304068603719640161855741656439472253464945249700314509890093162268952744428705476425472253167514521182231455388374308232642200633025137533129365164341725206256155311794738619142904761445654927128418175183531327052975495370561438239573227939673030106077456848477427832195349227983836436163764742969545906672369124136325932123335643135894465219101882123829740907916386023235450959388766736403229577993901152154448003637215069115591111996001530589107729421032230424262035693493216052927569625858445822354594645276923108197305806280326516736449343761732409753342333289730282959173569273013286423311759605230495171677033163709522256952460402143387655197644016528148022348331881097559421960476479388520198541017348985948511005469246617234143135309938405923268953586538886974427008607028635502085562029549352480050796521564919683265106744100967822951954161617717542997520009887307377876210685890770969411610438028623950445323789591870760289260393489826100774887672852918106468489143893649064784591211612193300707900537059042188012856559403699070888032966871611655961232331998310923225082866180321880439447572986762096935819784385927969250123326935194693207724335527365566248223787833888074999276831633440318604463618703789784313032843823470410944306591471928341190975185239212327674384990561563688432939039442002617530976850605132937101449086396141620556053547335569926700941375271829142407234267937565069765567475934101310225342830080409079587329544213551307302050171598424230760469209732907290141606353960880559202357376885647852240092777111489134492416995607171786298436533978180869474106751111353523711540436599310889697485658800887861974934357929246204051767246012250618404011966289872673803070498361217974484679100747846356194664829224736134115135567179291781968056053726484141128347858241259121954601184412409349782963317042002530418661694962318735860652485410222211869544223788289189712080514575141361964805369723164570564998479537657174548128597406077339158775332355215609435919275199351014222246963017013717419337504919295363295101115292951836282819191821651676455946515828048984256116748150367805267878662716999649296949377045794876146628110929982020737013330324451005385378551188803474148198665114579322684900993000236736168555294173442059925371965244997925483159343706343970371809611470323074186985035054722289027174850333368328300281132910841693150457389933183934593292994942796015309756118708918929528449074243284767006243171171622731766606796101967802204564589015899524704741001158110963633731329388356868949408759334176909387806398584647300588928175998844477486130063153068760070084837267527789777356830042778902772105683833021470279728595336332110564064263909724579949686162908019604141753935768876587992428549912151737924270343248648414247456838889541893241450987505759403013249697541696955330296880219304874163501097920036210238768275176369980977614979636096704348140124130683576879904997436596296495705459524735382000363770324894982103331332913562315169854410415317054193928234723398848453552173203688088312100943941434938282203549650281530751087098604681224802973825631244989331965296202372608586509050307993308652001231671915182765742095689513136184095412121473786311042897717861448158316965848766949554826252504961227044714712229620274682362909803877469376987358942125441792355298387479830450253909788733469732603097544156474805473732732767248652759034995336354126953900458854988683574927864615252040800490114785892289085443353996994780867471613519785838571456421583171193004117989440790268346357550339888086725127883577297626499213827436573992927302238792576924232785487201297255386071968303782483063725899808484638503828356258403917311872694381464553651690062530023217591343084755215901475299149215296944362366910833233693767993138209275870024246238331218236715236772098417187703860172308522448043176333602759733161201262248323085329288986154559221427378507410978822244729512663572225567169779409767341543017289268332635077451210167869121334465680739797372711461919299938118178827541421792926883790285430909942441260511945849237909966329550263865701114884142266162969810073652710928504579470861508094054577797864301504899958634164700528220562786008864025709432444254044034243140203812074857537999016066465520986980790589347320243050635907363821521280600041827529325485247927904235727598574209554632363830932428250711518801775633739811523761994686263270550635099851254333875594601540900862014293625673738331693082328854327001487476635118830885173775268819526360165345900556160767713453617655450974424979076063906093300028416964847594027046669468486593636425428625241644836652173922586528474244952363302305311413449332339822336551611431469131900170488226836525916399723912626616140205707996727383529597479125488961419287261259757561701592645823541151922177253919651034344793680369057003813056557866311011476313189571556336518727757991908862890765494952019474922148851417079252352394293801701149485239005844358329748769279941586384640877265901749104933238853465429979253900561311562288241147192158137210120267399648622831610430287268739840335142120299516610846193164688075944526965248570070554452152547493450434852917987512185973647190461515413582582139040172118295702327537027389787793506904044938553587650503557155872873201596885061331145477101575699375441097493374115991199114962726801718038950907803041184400075585468560976965669584325627283327416418044590727844680051360774154288412712456353383625469068936430902068216750459819321744513362913853983154560610459692604508787700304184579153478291725762810632722108035826060904572460619204237580363147200158749075361633785243462298769917887808671453928846572417223504887766803869453474588831907597355292800709241471370696647029530700507083091412492771404776193459007315206233634226128137074504162520473449597415678882003845446774388950379192344594171245510231738995030348421937088083329709108176561010708693158020695060096428352046647333361163476664106311247065173802510599409266908984046663298613648854871230659903565772327667696057187057276814394932559371368029375974604116075641599919402266794230681485723361363592903676841480358328093127506801111571615062761556607158236612268544268330274725849294875852089790850962835235527978491475563744318483993474633300330972497012808415900969455190375849945750379465019166009861502794606130794726898507849610303884846035423392175449505876157130344700415823080225786693300512126831846009510203543174323783292176865976076275412421892808138872880175813109296020150746331979561488146333412674896256883784351178477592660577212734269328382384711746083782209939646612308343952169576581065423771981899573840430315930973215059901371218399762585055435459516340055149080565627330475362528926945020226163130902420795006258931367813005222140742964756194053782182452833097021554210929638693005460011927178302761563505715735405672652524175925436371863471836292012162456662093642074605500842449347289830619506077570528754845277680661218358066130291463288932240701043887535007851997159198390084654596996197138399723495863749806582439384615049184048485819193560667125968018574877819561104335238420873417743385185735663101292757409280586840011804854994149478736882949368786637202682607198707656286436753775709560349718397405565505269425218354301348910785234517795519757516484711545928466003754558485470994737493796615841040414239875763335201795518644856632201598556341934286668912522153446348791218159622744525372314219184738770596659942181275403613660438538829201810204850917717791485256026242529802492309229562177062770027659288158473994804255067730903420043491632913588644627415318468517462580180901314477358637486528221274450661883667873545037139535563260349778209992416559111602097437491432360787879331015052417047437823553506205617017572175387061751192919715660363028302343819584946594328460482931960515124867123604625653903565173322856758210937541222674223847046664733620292824834065137814475367747671882220098389682019784216724015491253360436437847479770633657905418133523010804559958547379685864708937791659340223795537045273849435441105983887969741143051069401271065628507537039823308867819868298171415185218271493613110963984021912448323423901392553811725954153209435002954807640291982765741514042956666953177304003358701503703497424897898108939453026976878231557938158928996868766367603579055322794822757659104812835219745724022347569914650240636730492833286151875049129873457930874999488048681250802904606446223569562767964898914869924201946458521355165709887118378290437174375625282606140534611987395334677500936625746765638459629521872262777473480491233965194281353725068660782076683862565487279038020486778099991754380815789820825255566234983933217491493864966284116889874665005414748264599972752003370084542592544301190399041231752771993767799847551279448012913842034323154888137932524887172099381195722163148101670274877379161830968937348720168944903299658932511996504109653674618914861599481632040891930577238630396311858213341337110096389113836596895914715370925073998461682046426447290788976525593505136546978364603183820619560578517561504972661817649030304982138534738696212234626114043035600967042547012317360449724623287452575151198771801585742829389025650825988275495110865424704218337264023078045681651420517807418196096401513461760794362769612228126118610912766814880500950963889032877710837651051900076128058473969258768737937306664751387942217354694021157675557568970168734104342446525522568974329716152742558110503495045718931752447070410307760830365536714180388723602948872805590752711115590794756926903978519601939790311768070356801944936106850640568519290645048685535628256787225734544146565541187816717729850612874044620890718502108518025052924590359814117522720320552642597751984410742492179242039080014606225999422109717176118746845802673724801365603866909971071347255859723217027554055085082090418987534829222004178998475030519537179062001509333023023881806519182405550818672164711702307529922652228033820404113386625335815042934115143980939986416365633923620673874259342713444701242702722227197573203194489407856355511639619115985907995399083680129468810771595938084908111251938016414866250141095286680914828503123938960997659175977315432797173945762560365023587931559926170852315074247849814256564693008105061976397395591733545472917526739598790117477449217745771908169489543790314578152667389689410604588351445026130645637237687631129964576699475767340673583537218704935177320214779403972566532581731659202199752942824432778102107532160580104432121067208273876100778332426569624765621063126975491546224343978061253999313893915782008560011171319773443130412998215626985098895722778159524505640435534979855872234521991778419564106622122049039017886737997905270552302412002780864726282517525092332555378737792434915926182736151242592242725872699844014132567954640475742451126102857394193479716383187138370722782422619384021010896271281685732287764210298708895557148397743497418109849633633910397778254225179400022143485886207532122646613614487518735142494469575836744785028013193033901949738716163113800864093408529297729741462836142201120573027427309566658849884965134295188287937016147499504685185116851709758146686994243673140036992381232483920618628663037402351333907771907455224248515748726136075048020960976567862025232356273025557054386729189255571672396871991669651834736987440295942395220863481341484029838939485593327256627318194137735451891544384960966451285561476772564932516923822003229733483331726306200059192067441136913243720403578098676440894672367334549903905283682411422011886949265324531399323764100563996744273693606586851243936737155349696358970470706246743064681511525580772367129358691546677179280674603197443660235664811911175324239792271603213932690900431433572511935591785787438931910836722219749594385217682817205537433987939372420698422586002026702204502341484432231418159837133223467338185995720215212999066855318593765188122815644349842897804044474253173187198249880219056124756835053121936684091113613451740380400041281474527453687530122741226292549431008458868082627169312891414237466973215688306962596297824286828479758198382340345222708757345393897126910174835179971320922911746435455023926629385455742630840473245989374307836875864087914312319228307304975362481708579849870550314357368660453504099679685389802578477910419231052505144696885248726344365369453921372194715998156624454330562798282777349584666757018969900985110164491047993602922961645132995224271404908186948080818144765441476070834943179921347693205775493727581146799477819807044909830887802418354528095576268178221596410712216509995196961932368766959924336939018675830477951348564967496643006978548120861013029026482640402316621018258604172734501970838570222765188590393123627245232006535983907604127898007628220791246163222713051906678029644273228763460060289213316094057396003523608149487855784819923604033709243920895785100953666245538047118419834068589948934026518132741951749125192568729824877982938351710224389886834755562218214866075518628420508441240855088408499405226548535776678864444680504301285988469915501692621173049550268365498340637943341584797390666973271817567111123445212701138287733170703648843560692535012857745637166012927846194135756491508269323010063559087991831480179606664893606864875569154700860602703909640090514609360513037280984989976472934205971076104365706670369636731086616773064361333184164332104073479210775688703754193241101764488071673304176268034528127946729244452919161882095335945667158450941476301537032588109254921620602423998383939639570864268315557737923542830477169053850721939927266830567227442913752680237178175908437269780708099690192695002592421020485193953805155158663266418230452129371046840218806665165231438597498081621420148051355131865453014871298249938624272154345372391802215126115854861975398983110888196358875565793593310597895992053240439684590862193232015232257668969509415398393721308747247732944493053757577694362680328316055060353219685001207195191671426063364056179006218671604638684249067247572855764071631397824639188693789635247708232195940400450829593853651517642510129335911478337995658501766175785429966837417247838838645892319920041196390925354226074199412181950196251598137076467085022479201649174056794994024967593003129957099742026595785390010668925864718839026058417582396497109522944729874183273952292247592156943695682037477731049514128818431210317476600507328613045698141876738705835589680568812062901672389707603039549708273418484069037215179862266581929555420755695654139976814235027490594662592770986044588793675562143709648705744665319814772892177290937799834952769995145061157204394128687121538756842568036622321369508041915737469009917048039885947260486258684449762362318724085339500204989295963618739437408918888568903691128987832325892601331155260901131911962536529384643993465666490271108522834470184768391573623895239732781158399355178022076177450755929138381468526577309152290502509145241134006895661073761320484545645543610238773087594308408699782711315425513472058939339453013548135244176754131385012770024156261936169649709944156933541666590935759784775946468953923516229723673403650805886186209888123677098892944769654360324668874429740130365009770291603536426502844618046128137324006034947889707723262364927515545013731797277615272236008596598736693841467685030166103516988390995570659331713219574003251046303604196295276316972893990945091279545361627593486818455830168634280653205028729105026906643726170661778439311154737149759096116144493812106746037251452616271615212935043141124155849847574091878458410177325030553207237661933970254488335435069113577757765584551136714634629223114497809546405079026755854160954260557783406467356189398503160303046687522579708387993298769182986822970667356246647939653365454544268386404322650296028881298557242198187208040207296077496128886166773288838620609977925416822156713703667391333322086520871904473048593017466793578173830832406459585018177267447690376523178740142636502172093102619263046542112708094769682392207383797482057034793196525139221370199877543931178601238198924707047629587501516299912892933069025343575488251454948469328322898202956506903308470482517744131166464774750723477861518055343466858040987132273453081836794564364430155246274158100731069323080734901590517008753654169600998911569598454702926629374049393677685285723039754691181021469267219333846563053673846537824970049506209403506606244584412627902384242785280551599253188262786271531989567165398385428909311577122438618119724985644233907558812574859217687149721341833317289714977694982117227337669878361212788465289349470676995146785835955074121568375032937822352378665103266923449887690619794116574936418347914823983988002067649967289214983486424468737742286438563064425223461210639952418146166111391586408089740413945879849036682695488197808766021959964846771145698159367567904768112047055740602351237499012258567357965107013251562237843754994747236883821060645969480067316032381351192535148322216761442411144695361182838761115795911310362420741510826322048374540404146990479647073113428368300158247350712922331119789013179701651764033036649606749518628656836032556353205617386602749327417799524243899880581322620681297935615821202736455540414428705857131835238951742471239050758919353490570268302812810704750603181709274434806504843422134152201579626776393642698168851485545206897802036257496043584180735523092410264794436423951474855853980259367017205588767985309971043290404884467541701918912275745752402121744890326681160647837985004639864193645792082105266057503921195311457592621400830660301768120178560328650220171413811642155986702637531155554821958388905393544592180587272433749899242041542816666821846374863337522729077994208960545264863862516411718049112903451378140021506899966762080732651380427792460930762263446994159193223142565029996403134383564087766689537759857473403120477091886596355579944643688205981242040477802943803932459722207541739589596692034052161680837588921636048584789929663903107349945667027830610483130862872680661015300745521669893857383782690975026078691245077096697355744971063878379083608563324774011426604588797712843335089341752120843133549155805945217503766728136818989701075795034975931147113851177529084708382646930943663837851488798723688771534770333456570254004964039987760745951276145936694695508877796318400093229973736770518512573233230127134210937068680950264362225510872877141635601716276267040168996816287213326422268377855327887822310204539762272583014902609710931194995685950834598509258803363019551891846212990914920155838298607568190737375958573348261713794703592503972126314316192159571451081744034114847904386601284244643362857392442316767411036417557776991886719910511456902002905771699177497748359489810115650557253501242519595931483440417116222205504589671636754478042893488385500275211497526246421122010774862333476246182576597178403980436617669560567049936789350370893174617560282074679058110806280348893136768773133234721178541038289213080580369302853910384704528018344058313252744129928355732704028220901951560054449764635932065083760864473989692709381120504251266930834839999956860216356148940167442564078469350152730245749435722205009403680810321136898946443062041659878360406811331217133504960504944165472883926378607868385731515366373416312566122603235977742473940324793478740770352725436532455308438250865693864330020957120719122826689237609273524467576647981797622831709248187143189474449928329051087066866263901462754600186683583622062445553828867143621415773638244852570664653423002003386087185683387095282564231892337305827467825102905239214464117590700082862874915647212962223100149842773910585262712095702633425139069275541249925826898755913048489059083665047386569005980726512682643809800080768716889749767005747228285165430935469423701296901225200041775975278099341399149455675655415791289014141807180970818010668626000334746783492116354330741766556197865412944331579610856733907210970815247694360208231077377319255935591919361509992241851869762511821971598449396422923222586939583205849537600173388242916813257003198490942805178528782842560021549233235032941872842601109623432061485547579038802849228139397130979180562447703494299530585909415031237400213445431607049538809117870571045686211621069655023028886617060314580222884344906879404890744067164557565160596397488164744575409400891911083945126233990453364772541741331330821153758983077390402549739751620825837898384996200874749622331436354553599710472896396262914675542281735624665804212007275401273982269651802596130334329792865330063654175676003369688243926391280713123038760076039957285658333390838592534249337153070457471482743551037417147024006867419141874398230449202772009682282474640802779068104741602882613440803256607530230172031981070796893051954145239327661783905828943932146148894821695918579498502263858318704473290299790473193753803543920285706350672484245008920689712995308213747335255792982565803469779504974053329506220584586976782376547358092670753891736652401577525890202354883227480678961328466821865972869785592672826822656457577958670571097728396605101516387163615321918203652729897586807786443044896628670393188486921931839182927172554503767733076856308657600665342622254535057629030862718937364327448083558928220102374951534665262857504222950995446014428801859061443379679142717413278286975998259993819084477469775220567898395378570154609157695006128689610825519373486612568225010585752375050450503641627804406676905130564807191014132285063482665726828511933059433717373899623654715534816591630086838520905491060194596758223701911513581142071521220673521948777069521608531717477580542554138038978873716058241771904252401056370661792996395808706717083872950238057238547610495061732689587948240319569499000594593719849004104550034415014066154526994609094441391439071866309009249691812468813088126242358787204856089608234037677437509496768451058059579977189006605354903359961784245121096144877063187466910292356627771165203522647016166296488083808997887687769308220856530068153940856748041349469491870704992660210066455819832886130678021460369893174499939107231230405088342485383796742208847060385334943928147519685791292174441917386398739155908915859310750449419969425856920503891277550092560029225566028245733054164370482965351532695757311986033153861767127072322962670122480612322195488891848040149357757131302308897501348573600357134307046628157178720021578705983302856401851403674168523579202306440644159448586566246905477964086572462611834063213025126230896949713642334585419351128373132157232101577687884147986752364441711298472354763142715850293434746434127230225735792097083439910498394019789979870351837786010778667226102241684498686398874227580631272581474396676719404444290071284914286251867085092358453277091441099575357659812178523756848934429670368686181229928033433456696493460770903592231804072849827640955873854491455416480621098619090449331761288966135325413062313991971462332768487224184119088841054819270878948939590681778579588918587881426099241767753862382406002451821149035160124492893940578494055432173589779503867792448614769448734671863583817296627073341404027631029821562303142157743379294616649374361560459036060298581224576085252700570061877609582745974967405315882162695982851143006320897730473133461172142945805407178876223011462558510520930761771493278603541262719880522826864107055049364370555816945830840650960447321868266858216903784868032280479864021706315653687489290467679015864709379478762373013822026373788822827564163725872670921602434077279298565679825234946428376162918717556993454693724759744644374586123577768842647346924365909558553162571564807969034052671504760754742676960038255814034354716520834598864538432593908154392918939089896109083521992519053530398359081516289247571043354786424954980741388745386023113329591390636006694539437438690759589121288758007804082911975207095278133335550232776761783391381794105109412999680430103899475051243232331431120663598726618610084717254696908726637456858466646162093223427680684601718516702427671125913080059583053658180035991214447685286534985397013876080817020918472863351368783745251292936294373137797794168163130983688833624592488270042533275310174037401858155192663147588032450316349828944090941459435709839331454077937638533884824056964583661906580362312059760037127664830995125859310214290237306052000883415749724405028863402500028747055233688541068593389039662379913776485715772093749681055718277781429659244251666703110135820625316518906936462663613376783677001574909561444962828205690820330013121289873007855206280915035880334071746233511142838096550651818271177825964147342275885803162473648253508879276299074813242692461714169521068548818275361612786955216412383263246164034602771053052217127416909831714425842754359325193708367920524870186212708574531518197079797624156322369627530303625010057401503185241771381716916959066550480494267473696656051819527729740440782487416353087074803447126307550453018768734511631260002571765708294113447159058326859464022975489061868569715529325333679192904509181338989039531226114077604146341395120253199068720690450654317360799352434791082039695478061926453254644053124586362026016156957888326419910345519741792140396061817459467332211367113051675844970722080638249309285348257747082754939860608508486386549912912220391895379332321814344998458680557267915553525212113907723389037979427485209309504641599639196053296030292739274663906234190084882219800040754390250718430168480866157009022053158273857417267902310193566064243660834080202786392263652453395504706244108957791049298908397097715806857036527872932846771952274875201321856644284239116502005786314083266606564469023548865452436995428173054383481532525156311603438941440612599662264959878769003712412406305072713049361060777519351766684350979095112470377728527045181653556285621656761979382960837252084759203181114384404005114613990326273362146150667851880079327563486444803726963278475818453468509565365380723807736793526242145533088833679727074192838872685484977387306670007551965761431583941102042492623133242770270821710047137152167948872468741189259805623910487138975034203376673344989060052111727844670530521327425332189734978888307943626099125569741350839434061750712876445222039557455150019442996232739317091487905148812821582894579949748623433452865460274945133477267345362799684706610937707688048379567860499887327287273158785195505216946986571759372875984514994087366735699690322212619073372131984972873714823922056361158276719293687517950957314775347729780073870450969204201405641649670458849057466793682155521901095027966023590200629563426621697556166261956721403439803621918243177242110484823901622121433246394511832190737036276362556947535372619436126996645342229589006736952014870393865015087074414735220220770411070088328030731545041085721762448600324574529642445801550887535581445128918173686346116567111050151109714760544361537920207546115073529167935359803746233158035402507293585422134950239655004818456256327794930441926282941363007224150758144477688955569616025741423859274415740477217643474948663183182648210083785749080222199122144694117400270856159095104885845967392332602301843740328702808569782311182460392642129024729192535226787567897868564600570545108945374190649371558894253329703486982693386634574807801078719254129784907155276683405362407228975018016417486695226639468897799829747034414846605232977531914266638473906810541671168066131262918834764800639654762345192540580400257727950859061817326000188356677576249635084512266410557551645134470262083485480776173701923626406384831371842087477223094467978661221649689842322789248762605167898686475739012260361067924661728390016267344059903224026536343550301552983264519865881303942586617734987280231892544278016113318354276103258002393942607858756949485119300715853929843163967575531352484221274814864027297307155457109423432471856970523635672286551516265672389248080659279448953078708153606666741314598260798811278754656313007785743484189610809499119436037561075326359414143476882494692530787790693275290895909945065272071979419682741840819830567497688086236460819345623637702467815073133466197923119939117195979352833117496820500577299938566476984260815518940866776320855483159836188850448339911351505132909703990088919063133375804094886955895465912648104534621430295733375992898959329664444686900648674531661878821297194146191914132978673375283702995190706179924178140249356285006536273771636355569720003246996130665282451695167273161831393867558791278264933758040011059211372270839123518459709107480669852696080909181872978501574064544548264471078633386599113188810137746318984496745989011540334259315347740082110734818471521253374507271538131048296612979081477663269791394570357390537422769779633811568396223208402597025155304734389883109376" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**100000" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "38" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0b100110" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 == 2" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 < 2" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + (2 * 3)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1 + 2) * 3" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "24" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * 3 * 4" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "20 % 3" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * 10 % 3" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2 * 10) % 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exponentiation" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**64" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 * 2**64 == 3 * (2**64)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Floor Division" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 / 2 # <----------------- Python 3!!!!!" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3//2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Variablen? Datenypen?" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(do_something)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "a = do_something" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function do_something in module __main__:\n", "\n", "do_something(a, b)\n", " diese funktion ist komplett sinnlos, und nimmt parameter a und b, die integers\n", " sein sollten, sonst funktioniert das ganze hier nicht.\n", " \n", " der returnwert ist noch sinnloserer weise die summe der parameter.\n", "\n" ] } ], "source": [ "help(a)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a(2, 3)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'diese funktion ist komplett sinnlos, und nimmt parameter a und b, die integers\\n sein sollten, sonst funktioniert das ganze hier nicht.\\n \\n der returnwert ist noch sinnloserer weise die summe der parameter.'" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.__doc__" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__annotations__',\n", " '__call__',\n", " '__class__',\n", " '__closure__',\n", " '__code__',\n", " '__defaults__',\n", " '__delattr__',\n", " '__dict__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__get__',\n", " '__getattribute__',\n", " '__globals__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__kwdefaults__',\n", " '__le__',\n", " '__lt__',\n", " '__module__',\n", " '__name__',\n", " '__ne__',\n", " '__new__',\n", " '__qualname__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__']" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(a)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'do_something'" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.__name__" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.__class__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Objects, Classes" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "class Karteikarte:\n", " def __init__(self, firstname, lastname, svnr):\n", " self.firstname = firstname\n", " self.lastname = lastname\n", " self.svnr = svnr\n", " def format(self):\n", " my_formatted_person = self.firstname + ' ' + self.lastname + ' (' + self.svnr + ')'\n", " return my_formatted_person" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "joerg = Karteikarte('Joerg', 'Faschingbauer', '1037190666')\n", "caro = Karteikarte('Caro', 'Faschingbauer', '123425041997')" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Joerg Faschingbauer (1037190666)\n" ] } ], "source": [ "print(joerg.firstname + ' ' + joerg.lastname + ' (' + joerg.svnr + ')')" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Caro Faschingbauer (123425041997)\n" ] } ], "source": [ "print(caro.firstname + ' ' + caro.lastname + ' (' + caro.svnr + ')')" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "caros_string = caro.format()\n", "joergs_string = joerg.format()" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Caro Faschingbauer (123425041997)\n" ] } ], "source": [ "print(caros_string)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__class__',\n", " '__delattr__',\n", " '__dict__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__le__',\n", " '__lt__',\n", " '__module__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " '__weakref__',\n", " 'firstname',\n", " 'format',\n", " 'lastname',\n", " 'svnr']" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(caro)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.Karteikarte" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "caro.__class__" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ">" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "caro.format" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "und strings???" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "s = 'Mississippi'" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.__class__" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(s)" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.count('ss')" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [], "source": [ "pos = s.find('ss')" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pos" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "next_pos = s.find('ss', pos+1)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next_pos" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Datatype Conversions" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(str)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(Karteikarte)" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "s = str('abc')" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [], "source": [ "s = 'abc'" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [], "source": [ "i = 42" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'42'" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = str(i)\n", "s" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(int)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [], "source": [ "i = int('666')" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3735928559" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('0xdeadbeef', 16)" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.1234" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float('0.1234')" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "could not convert string to float: 'Joerg' \n" ] } ], "source": [ "try:\n", " float('Joerg')\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(1)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(42)" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(-1)" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool('Joerg')" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool('')" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Oida!\n" ] } ], "source": [ "if 'Joerg':\n", " print('Oida!')" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Oida!\n" ] } ], "source": [ "if len('Joerg') != 0:\n", " print('Oida!')" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1,2,3]\n", "type(l)" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['J', 'o', 'e', 'r', 'g']" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = list('Joerg')\n", "l" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Zen of Python, by Tim Peters\n", "\n", "Beautiful is better than ugly.\n", "Explicit is better than implicit.\n", "Simple is better than complex.\n", "Complex is better than complicated.\n", "Flat is better than nested.\n", "Sparse is better than dense.\n", "Readability counts.\n", "Special cases aren't special enough to break the rules.\n", "Although practicality beats purity.\n", "Errors should never pass silently.\n", "Unless explicitly silenced.\n", "In the face of ambiguity, refuse the temptation to guess.\n", "There should be one-- and preferably only one --obvious way to do it.\n", "Although that way may not be obvious at first unless you're Dutch.\n", "Now is better than never.\n", "Although never is often better than *right* now.\n", "If the implementation is hard to explain, it's a bad idea.\n", "If the implementation is easy to explain, it may be a good idea.\n", "Namespaces are one honking great idea -- let's do more of those!\n" ] } ], "source": [ "import this" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Boolean" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and False" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or False" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False or False" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and True" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False and True" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [], "source": [ "def wahr():\n", " print('wahr')\n", " return True\n", "def falsch():\n", " print('falsch')\n", " return False" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "wahr\n", "True\n" ] } ], "source": [ "x = wahr()\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "wahr\n", "jo eh\n" ] } ], "source": [ "if wahr():\n", " print('jo eh')\n", "else:\n", " print('nix da')" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "wahr\n", "wahr\n", "jo eh\n" ] } ], "source": [ "if wahr() and wahr():\n", " print('jo eh')" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "wahr\n", "jo eh\n" ] } ], "source": [ "if wahr() or falsch():\n", " print('jo eh')" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "falsch\n", "nix da\n" ] } ], "source": [ "if falsch() and wahr(): # False and True\n", " print('jo eh')\n", "else:\n", " print('nix da')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Compound Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Listen" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 'drei', ['4.0', 5], 666.7]" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[0]" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['4.0', 5]" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[3]" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[3][1]" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'e'" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[2][2]" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'.'" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[3][0][1]" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'float' object is not subscriptable \n" ] } ], "source": [ "try:\n", " l[4][3]\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**BTW, wie kriegt man die Nachkommastellen aus einem Float raus?**" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [], "source": [ "f = 666.789" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Erste Moeglichkeit:" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "789" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round((f - (int(f))) * 1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Zweite Moeglichkeit:" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'666.789'" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f_str = str(f)\n", "f_str" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [], "source": [ "punkt_pos = f_str.find('.')" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "punkt_pos" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [], "source": [ "nachkomma_str = f_str[punkt_pos+1:]" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "789" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(nachkomma_str)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Weiter im Text ...**" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', ['4.0', 5], 666.7]" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [], "source": [ "l.append('sieben')" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', ['4.0', 5], 666.7, 'sieben']" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [], "source": [ "l.extend(['eight', 9.0])" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', ['4.0', 5], 666.7, 'sieben', 'eight', 9.0]" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [], "source": [ "l.extend('aber hallo')" ] }, { "cell_type": "code", "execution_count": 128, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "[1,\n", " 2,\n", " 'drei',\n", " ['4.0', 5],\n", " 666.7,\n", " 'sieben',\n", " 'eight',\n", " 9.0,\n", " 'a',\n", " 'b',\n", " 'e',\n", " 'r',\n", " ' ',\n", " 'h',\n", " 'a',\n", " 'l',\n", " 'l',\n", " 'o']" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 'fuenf']" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 = [1,2,3]\n", "l2 = ['vier', 'fuenf']\n", "l = l1+l2\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuple" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [], "source": [ "t = (1,2,3)" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[0]" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 3)" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[1:]" ] }, { "cell_type": "code", "execution_count": 133, "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, type(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Single element tuples?" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = (1) # '(', ')': Operator praezedenz\n", "type(t)" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1+2)*3" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = ((1))\n", "t" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1]" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[(1)]" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1,)" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1,) " ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 'vier', 'fuenf')" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t1 = (1,2,3)\n", "t2 = ('vier', 'fuenf')\n", "t = t1+t2\n", "t" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionary" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [], "source": [ "table = {'one': 1, \n", " 'two': 2}" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table['two']" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [], "source": [ "table['three'] = 3" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'three' in table" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 144, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'four' in table" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [], "source": [ "del table['one']" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'one' in table" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'one' not in table" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 'one' in table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sequentielle Suche in Liste" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in l" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "11 in l" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 152, "metadata": {}, "output_type": "execute_result" } ], "source": [ "12 in l" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [], "source": [ "s = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "11 in s" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [], "source": [ "s.remove(11)" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "11 not in s" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "set" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Frage: wie wandele ich ein Dictionary in ein Set um? Oder gach in eine Liste?" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'two': 2, 'three': 3}" ] }, "execution_count": 158, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['J', 'o', 'e', 'r', 'g']" ] }, "execution_count": 159, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list('Joerg') # <-- ITERATION" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'J', 'e', 'g', 'o', 'r'}" ] }, "execution_count": 160, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set('Joerg')" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "two\n", "three\n" ] } ], "source": [ "for k in table.keys():\n", " print(k)" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "two\n", "three\n" ] } ], "source": [ "for k in table:\n", " print(k)" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'three', 'two'}" ] }, "execution_count": 163, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set(table)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wie mache ich aus einem Set ein Dictionary? (Hm. Woher kommen die Keys?)" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [], "source": [ "s = {1, 2, 3}" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [], "source": [ "d = {}\n", "for element in s:\n", " d[element] = random.randrange(100)" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 65, 2: 45, 3: 69}" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Standard Libarary Examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``collections``" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ``ChainMap``" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [], "source": [ "from collections import ChainMap" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [], "source": [ "d1 = {'one': 1, 'two': 2}\n", "d2 = {'three': 3, 'four': 4}" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chained = ChainMap(d1, d2)\n", "chained['one']" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 171, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chained['three']" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [], "source": [ "chained['five'] = 5" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'five': 5}" ] }, "execution_count": 173, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d1" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'three': 3, 'four': 4}" ] }, "execution_count": 174, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d2" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'ten' \n" ] } ], "source": [ "try:\n", " d['ten']\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ``deque``" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Liste vorne anhaengen:" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [], "source": [ "l = [3,4,5]\n", "l.append(6)" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 4, 5, 6]" ] }, "execution_count": 177, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.insert(0, 2)\n", "l" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [], "source": [ "del l[0]" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 4, 5, 6]" ] }, "execution_count": 179, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Aber**: langsamer als man sich wuenschen wuerde" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [], "source": [ "import collections" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [], "source": [ "d = collections.deque([3, 4, 5])" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "deque([3, 4, 5])" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "deque([3, 4, 5, 6])" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.append(6)\n", "d" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "deque([2, 3, 4, 5, 6])" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.appendleft(2)\n", "d" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "deque([2, 3, 4, 5, 6, 'J', 'o', 'e', 'r', 'g'])" ] }, "execution_count": 185, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.extend('Joerg')\n", "d" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "deque(['r',\n", " 'e',\n", " 'u',\n", " 'a',\n", " 'b',\n", " 'g',\n", " 'n',\n", " 'i',\n", " 'h',\n", " 'c',\n", " 's',\n", " 'a',\n", " 'F',\n", " 2,\n", " 3,\n", " 4,\n", " 5,\n", " 6,\n", " 'J',\n", " 'o',\n", " 'e',\n", " 'r',\n", " 'g'])" ] }, "execution_count": 186, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.extendleft('Faschingbauer')\n", "d" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'g'" ] }, "execution_count": 187, "metadata": {}, "output_type": "execute_result" } ], "source": [ "relem = d.pop()\n", "relem" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "deque(['r',\n", " 'e',\n", " 'u',\n", " 'a',\n", " 'b',\n", " 'g',\n", " 'n',\n", " 'i',\n", " 'h',\n", " 'c',\n", " 's',\n", " 'a',\n", " 'F',\n", " 2,\n", " 3,\n", " 4,\n", " 5,\n", " 6,\n", " 'J',\n", " 'o',\n", " 'e',\n", " 'r'])" ] }, "execution_count": 188, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'r'" ] }, "execution_count": 189, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.popleft()" ] }, { "cell_type": "code", "execution_count": 190, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "deque(['e',\n", " 'u',\n", " 'a',\n", " 'b',\n", " 'g',\n", " 'n',\n", " 'i',\n", " 'h',\n", " 'c',\n", " 's',\n", " 'a',\n", " 'F',\n", " 2,\n", " 3,\n", " 4,\n", " 5,\n", " 6,\n", " 'J',\n", " 'o',\n", " 'e',\n", " 'r'])" ] }, "execution_count": 190, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``os.path``" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [], "source": [ "import os.path" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a/b'" ] }, "execution_count": 192, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.path.join('a', 'b')" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a/b/x'" ] }, "execution_count": 193, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.path.normpath('a/b/c/d/../../x')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Suchalgorithmen und Datenstrukturen (Ausflug)" ] }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [], "source": [ "l = [5, 3, 1, 10, 8, 0]\n", "l.sort()" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 3, 5, 8, 10]" ] }, "execution_count": 195, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [], "source": [ "l = [5, 3, 1, 10, 8, 0]\n", "sorted_l = sorted(l)" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 3, 1, 10, 8, 0]" ] }, "execution_count": 197, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 3, 5, 8, 10]" ] }, "execution_count": 198, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted_l" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [], "source": [ "t = (5, 3, 1, 10, 8, 0)\n", "sorted_t = sorted(t)" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5, 3, 1, 10, 8, 0)" ] }, "execution_count": 200, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 3, 5, 8, 10]" ] }, "execution_count": 201, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted_t" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 202, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(sorted_t)" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [], "source": [ "s = 'Joerg'\n", "sorted_s = sorted(s)" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg'" ] }, "execution_count": 204, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['J', 'e', 'g', 'o', 'r']" ] }, "execution_count": 205, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted_s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Slicing" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [], "source": [ "text = 'Hello World'" ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 207, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(text)" ] }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello'" ] }, "execution_count": 208, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text[0:5]" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello'" ] }, "execution_count": 209, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text[:5]" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'World'" ] }, "execution_count": 210, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text[6:11]" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'World'" ] }, "execution_count": 211, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text[6:]" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text = 'Mississippi'\n", "pos = text.find('ss')\n", "pos" ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ssissippi'" ] }, "execution_count": 213, "metadata": {}, "output_type": "execute_result" } ], "source": [ "subtext = text[pos:]\n", "subtext" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copy:" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Mississippi'" ] }, "execution_count": 214, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672464726576" ] }, "execution_count": 215, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(text)" ] }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Mississippi'" ] }, "execution_count": 216, "metadata": {}, "output_type": "execute_result" } ], "source": [ "copied_text = text[:]\n", "copied_text" ] }, { "cell_type": "code", "execution_count": 217, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672464726576" ] }, "execution_count": 217, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(copied_text)" ] }, { "cell_type": "code", "execution_count": 218, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 218, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1,2,3]\n", "copied_l = l[:]\n", "copied_l" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672464784832" ] }, "execution_count": 219, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l)" ] }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672464784064" ] }, "execution_count": 220, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(copied_l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Slice Assignment" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [], "source": [ "l = [2, 3, 'a', 'b', 7] # wanted: [2,3,4,5,6,7]" ] }, { "cell_type": "code", "execution_count": 222, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b']" ] }, "execution_count": 222, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[2:4]" ] }, { "cell_type": "code", "execution_count": 223, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 4, 5, 6, 7]" ] }, "execution_count": 223, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[2:4] = [4,5,6]\n", "l" ] }, { "cell_type": "code", "execution_count": 224, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 224, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[:0]" ] }, { "cell_type": "code", "execution_count": 225, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7]" ] }, "execution_count": 225, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[:0] = [0,1]\n", "l" ] }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-1, 0, 1, 2, 3, 4, 5, 6, 7]" ] }, "execution_count": 226, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.insert(0, -1)\n", "l" ] }, { "cell_type": "code", "execution_count": 227, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 227, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[len(l):]" ] }, { "cell_type": "code", "execution_count": 228, "metadata": {}, "outputs": [], "source": [ "l[len(l):] = [8,9]" ] }, { "cell_type": "code", "execution_count": 229, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 229, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lesbarer:" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [], "source": [ "l.extend([10,11])" ] }, { "cell_type": "code", "execution_count": 231, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]" ] }, "execution_count": 231, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# References, (Im)mutability" ] }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672599989840" ] }, "execution_count": 233, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 234, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672599989840" ] }, "execution_count": 234, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a\n", "id(b)" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [], "source": [ "a += 1" ] }, { "cell_type": "code", "execution_count": 236, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "43" ] }, "execution_count": 236, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 237, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672599989872" ] }, "execution_count": 237, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 238, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672599989840" ] }, "execution_count": 238, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 239, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 239, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists are mutable" ] }, { "cell_type": "code", "execution_count": 240, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 240, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 = [1, ['abc', 'def'], 3]\n", "len(l1)" ] }, { "cell_type": "code", "execution_count": 241, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 241, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1[0]" ] }, { "cell_type": "code", "execution_count": 242, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['abc', 'def']" ] }, "execution_count": 242, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1[1]" ] }, { "cell_type": "code", "execution_count": 243, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 243, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Machen wir eine Kopie:" ] }, { "cell_type": "code", "execution_count": 244, "metadata": {}, "outputs": [], "source": [ "l2 = l1[:]" ] }, { "cell_type": "code", "execution_count": 245, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672464751872" ] }, "execution_count": 245, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1)" ] }, { "cell_type": "code", "execution_count": 246, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672464768512" ] }, "execution_count": 246, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l2)" ] }, { "cell_type": "code", "execution_count": 247, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, ['abc', 'def'], 3]" ] }, "execution_count": 247, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 248, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, ['abc', 'def'], 3]" ] }, "execution_count": 248, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 249, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, ['abc', 'def'], 3, 4]" ] }, "execution_count": 249, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1.append(4)\n", "l1" ] }, { "cell_type": "code", "execution_count": 250, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, ['abc', 'def'], 3]" ] }, "execution_count": 250, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 251, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['abc', 'def']" ] }, "execution_count": 251, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1[1]" ] }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['abc', 'def']" ] }, "execution_count": 252, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2[1]" ] }, { "cell_type": "code", "execution_count": 253, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, ['abc', 'def', 'ghi'], 3, 4]" ] }, "execution_count": 253, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1[1].append('ghi')\n", "l1" ] }, { "cell_type": "code", "execution_count": 254, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, ['abc', 'def', 'ghi'], 3]" ] }, "execution_count": 254, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 255, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672464594880" ] }, "execution_count": 255, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1[1])" ] }, { "cell_type": "code", "execution_count": 256, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140672464594880" ] }, "execution_count": 256, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l2[1])" ] }, { "cell_type": "code", "execution_count": 257, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 257, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1[1] is l2[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ist das gleiche wie ..." ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 258, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1[1]) == id(l2[1])" ] }, { "cell_type": "code", "execution_count": 259, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-100, 1, ['abc', 'def', 'ghi'], 3, 4]" ] }, "execution_count": 259, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1.insert(0, -100)\n", "l1" ] }, { "cell_type": "code", "execution_count": 260, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 260, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1[2] is l2[1]" ] }, { "cell_type": "code", "execution_count": 261, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, ['abc', 'def', 'ghi'], 3]" ] }, "execution_count": 261, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 262, "metadata": {}, "outputs": [], "source": [ "l2[1].append('jkl')" ] }, { "cell_type": "code", "execution_count": 263, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, ['abc', 'def', 'ghi', 'jkl'], 3]" ] }, "execution_count": 263, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 264, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-100, 1, ['abc', 'def', 'ghi', 'jkl'], 3, 4]" ] }, "execution_count": 264, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Shallow copy and deep copy" ] }, { "cell_type": "code", "execution_count": 265, "metadata": {}, "outputs": [], "source": [ "l1 = [1, ['abc', 'def'], 3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Shallow copy:" ] }, { "cell_type": "code", "execution_count": 266, "metadata": {}, "outputs": [], "source": [ "l2 = l1[:]" ] }, { "cell_type": "code", "execution_count": 267, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 267, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1[1] is l2[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Deep copy:" ] }, { "cell_type": "code", "execution_count": 268, "metadata": {}, "outputs": [], "source": [ "import copy" ] }, { "cell_type": "code", "execution_count": 269, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 269, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3 = copy.deepcopy(l1)\n", "l1[1] is l3[1]" ] }, { "cell_type": "code", "execution_count": 270, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, ['abc', 'def', 'ghi'], 3]" ] }, "execution_count": 270, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1[1].append('ghi')\n", "l1" ] }, { "cell_type": "code", "execution_count": 271, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, ['abc', 'def'], 3]" ] }, "execution_count": 271, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ``while`` Loops" ] }, { "cell_type": "code", "execution_count": 272, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "i = 0\n", "while i < 10:\n", " i += 1\n", "print(i)" ] }, { "cell_type": "code", "execution_count": 273, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo suesser\n", "hallo suesser\n", "hallo suesser\n", "hallo suesser\n", "hallo suesser\n", "hallo suesser\n", "hallo suesser\n", "hallo suesser\n", "hallo suesser\n", "hallo suesser\n" ] } ], "source": [ "i = 0\n", "while i < 10:\n", " print('hallo suesser')\n", " i += 1" ] }, { "cell_type": "code", "execution_count": 274, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo suesser\n", "hallo suesser\n", "hallo suesser\n", "hallo suesser\n", "hallo suesser\n", "hallo suesser\n", "hallo suesser\n", "hallo suesser\n", "hallo suesser\n", "hallo suesser\n" ] } ], "source": [ "for i in range(10):\n", " print('hallo suesser')" ] }, { "cell_type": "code", "execution_count": 275, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "lose (3)\n", "lose (1)\n", "lose (1)\n", "lose (1)\n", "lose (2)\n", "lose (3)\n", "yay!\n" ] } ], "source": [ "import random\n", "while True:\n", " number = random.randrange(1,7)\n", " if number == 6:\n", " print('yay!')\n", " break\n", " else:\n", " print(f'lose ({number})')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``else``" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Potschert: Flag" ] }, { "cell_type": "code", "execution_count": 276, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay!\n" ] } ], "source": [ "import random\n", "\n", "sechser_gewuerfelt = False\n", "n_tries = 0\n", "while n_tries < 6:\n", " n_tries += 1\n", " number = random.randrange(1,7)\n", " if number == 6:\n", " sechser_gewuerfelt = True\n", " break\n", "\n", "if sechser_gewuerfelt:\n", " print('yay!')\n", "else:\n", " print('versager!')" ] }, { "cell_type": "code", "execution_count": 277, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "versager!\n" ] } ], "source": [ "import random\n", "\n", "n_tries = 0\n", "while n_tries < 6:\n", " n_tries += 1\n", " number = random.randrange(1,7)\n", " if number == 6:\n", " print('yay!')\n", " break\n", "else:\n", " print('versager!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ``for`` Loops, ``range()``" ] }, { "cell_type": "code", "execution_count": 278, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Caro\n", "Johanna\n", "Eva\n", "Jörg\n" ] } ], "source": [ "for name in ['Caro', 'Johanna', 'Eva', 'Jörg']:\n", " print(name)" ] }, { "cell_type": "code", "execution_count": 279, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "versager!\n" ] } ], "source": [ "for _ in [0,1,2,3,4,5]:\n", " number = random.randrange(1,7)\n", " if number == 6:\n", " print('yay!')\n", " break\n", "else:\n", " print('versager!')" ] }, { "cell_type": "code", "execution_count": 280, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay!\n" ] } ], "source": [ "for _ in range(6): # python2: xrange()\n", " number = random.randrange(1,7)\n", " if number == 6:\n", " print('yay!')\n", " break\n", "else:\n", " print('versager!')" ] }, { "cell_type": "code", "execution_count": 281, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for i in range(3):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 282, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for i in range(3,10):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 283, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "5\n", "7\n", "9\n" ] } ], "source": [ "for i in range(3,10,2):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Iterator Protocol" ] }, { "cell_type": "code", "execution_count": 284, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 3)" ] }, "execution_count": 284, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = range(3)\n", "r" ] }, { "cell_type": "code", "execution_count": 285, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range" ] }, "execution_count": 285, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(r)" ] }, { "cell_type": "code", "execution_count": 286, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 286, "metadata": {}, "output_type": "execute_result" } ], "source": [ "it = iter(r)\n", "it" ] }, { "cell_type": "code", "execution_count": 287, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 287, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 288, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 288, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 289, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 289, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 290, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "try:\n", " next(it)\n", "except StopIteration as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 293, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for i in range(3):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Iteration over Compound Datatypes" ] }, { "cell_type": "code", "execution_count": 295, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "s = {1,2,3}\n", "for element in s:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 296, "metadata": {}, "outputs": [], "source": [ "d = {\n", " 0: 'zero',\n", " 1: 'one',\n", " 2: 'two',\n", "}" ] }, { "cell_type": "code", "execution_count": 297, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for element in d:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 300, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for key in d.keys():\n", " print(key)" ] }, { "cell_type": "code", "execution_count": 302, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "zero\n", "one\n", "two\n" ] } ], "source": [ "for value in d.values():\n", " print(value)" ] }, { "cell_type": "code", "execution_count": 303, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 'zero')\n", "(1, 'one')\n", "(2, 'two')\n" ] } ], "source": [ "for element in d.items():\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 304, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Key: 0, value: zero\n", "Key: 1, value: one\n", "Key: 2, value: two\n" ] } ], "source": [ "for pair in d.items():\n", " key = pair[0]\n", " value = pair[1]\n", " print(f'Key: {key}, value: {value}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuple unpacking:" ] }, { "cell_type": "code", "execution_count": 309, "metadata": {}, "outputs": [], "source": [ "a, b = 1, 2" ] }, { "cell_type": "code", "execution_count": 310, "metadata": {}, "outputs": [], "source": [ "(a, b) = (1, 2)" ] }, { "cell_type": "code", "execution_count": 307, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 307, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 308, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 308, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 311, "metadata": {}, "outputs": [], "source": [ "t = (1, 2)\n", "a, b = t" ] }, { "cell_type": "code", "execution_count": 312, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 312, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 313, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 313, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 314, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Key: 0, value: zero\n", "Key: 1, value: one\n", "Key: 2, value: two\n" ] } ], "source": [ "for key, value in d.items():\n", " print(f'Key: {key}, value: {value}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions" ] }, { "cell_type": "code", "execution_count": 317, "metadata": {}, "outputs": [], "source": [ "def maximum(a, b):\n", " if a < b:\n", " return b\n", " return a" ] }, { "cell_type": "code", "execution_count": 316, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 316, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(1,2)" ] }, { "cell_type": "code", "execution_count": 319, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'def'" ] }, "execution_count": 319, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum('abc', 'def')" ] }, { "cell_type": "code", "execution_count": 325, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 325, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc' < 'def'" ] }, { "cell_type": "code", "execution_count": 326, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 326, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Huber' > 'Hannes'" ] }, { "cell_type": "code", "execution_count": 323, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 323, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 < 2" ] }, { "cell_type": "code", "execution_count": 328, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'<' not supported between instances of 'int' and 'str' \n" ] } ], "source": [ "try:\n", " 1 < 'abc'\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 331, "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(e, type(e))" ] }, { "cell_type": "code", "execution_count": 335, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 335, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[3,1,4] < [3,4,5]" ] }, { "cell_type": "code", "execution_count": 336, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 4, 5]" ] }, "execution_count": 336, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum([3,1,4], [3,4,5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Default Parameters" ] }, { "cell_type": "code", "execution_count": 338, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hallo, Joerg\n" ] } ], "source": [ "def greet(name, phrase):\n", " return phrase + ', ' + name\n", "print(greet('Joerg', 'Hallo'))" ] }, { "cell_type": "code", "execution_count": 339, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hallo, Joerg'" ] }, "execution_count": 339, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def greet(name, phrase='Hallo'):\n", " return phrase + ', ' + name\n", "greet('Joerg')" ] }, { "cell_type": "code", "execution_count": 340, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Gruess Gott, Joerg'" ] }, "execution_count": 340, "metadata": {}, "output_type": "execute_result" } ], "source": [ "greet('Joerg', 'Gruess Gott')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Default Parameters: Pitfalls" ] }, { "cell_type": "code", "execution_count": 341, "metadata": {}, "outputs": [], "source": [ "def fill_in_user(svnr, firstname, lastname, database):\n", " if database.get(svnr):\n", " raise Exception(svnr + ' gibts scho')\n", " database[svnr] = (firstname, lastname)" ] }, { "cell_type": "code", "execution_count": 342, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'1037190666': ('Joerg', 'Faschingbauer')}" ] }, "execution_count": 342, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_db = {}\n", "fill_in_user('1037190666', 'Joerg', 'Faschingbauer', my_db)\n", "my_db" ] }, { "cell_type": "code", "execution_count": 351, "metadata": {}, "outputs": [], "source": [ "def fill_in_user(svnr, firstname, lastname, database={}):\n", " if database.get(svnr):\n", " raise Exception(svnr + ' gibts scho')\n", " database[svnr] = (firstname, lastname)" ] }, { "cell_type": "code", "execution_count": 352, "metadata": {}, "outputs": [], "source": [ "fill_in_user('1037190666', 'Joerg', 'Faschingbauer')" ] }, { "cell_type": "code", "execution_count": 353, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1037190666 gibts scho \n" ] } ], "source": [ "try:\n", " fill_in_user('1037190666', 'Joerg', 'Faschingbauer')\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ``dict.fromkeys()``" ] }, { "cell_type": "code", "execution_count": 372, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: None, 1: None, 2: None, 3: None, 4: None}" ] }, "execution_count": 372, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict.fromkeys(range(5))" ] }, { "cell_type": "code", "execution_count": 373, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 666, 1: 666, 2: 666, 3: 666, 4: 666}" ] }, "execution_count": 373, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict.fromkeys(range(5), 666)" ] }, { "cell_type": "code", "execution_count": 374, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2: None, 3: None, 1: None, 10: None, 5: None}" ] }, "execution_count": 374, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [2, 3, 1, 10, 3, 3, 1, 10, 5, 2]\n", "uniq_dict = dict.fromkeys(l)\n", "uniq_dict" ] }, { "cell_type": "code", "execution_count": 375, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n", "1\n", "10\n", "5\n" ] } ], "source": [ "for key in uniq_dict.keys():\n", " print(key)" ] }, { "cell_type": "code", "execution_count": 377, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 1, 10, 5]" ] }, "execution_count": 377, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(uniq_dict.keys())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Generators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generator, der die Quadratzahlen startend von einem konfigurierbaren Startwert ausgibt ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Start = 3\n", "9, 16, 25, 36, 49, ..." ] }, { "cell_type": "code", "execution_count": 378, "metadata": {}, "outputs": [], "source": [ "def squares(start, max):\n", " l = []\n", " i = start\n", " while True:\n", " sqnum = i**2\n", " i += 1\n", " if sqnum == max:\n", " break\n", " l.append(sqnum)\n", " return l" ] }, { "cell_type": "code", "execution_count": 381, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n", "16\n", "25\n", "36\n", "49\n", "64\n", "81\n" ] } ], "source": [ "for num in squares(3, 100):\n", " print(num)" ] }, { "cell_type": "code", "execution_count": 393, "metadata": {}, "outputs": [], "source": [ "def squares(start, max):\n", " i = start\n", " while True:\n", " sqnum = i**2\n", " i += 1\n", " if sqnum >= max:\n", " break\n", " yield sqnum" ] }, { "cell_type": "code", "execution_count": 383, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n", "16\n", "25\n", "36\n", "49\n", "64\n", "81\n" ] } ], "source": [ "for num in squares(3, 100):\n", " print(num)" ] }, { "cell_type": "code", "execution_count": 394, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 394, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sq = squares(3, 50)\n", "sq" ] }, { "cell_type": "code", "execution_count": 395, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 395, "metadata": {}, "output_type": "execute_result" } ], "source": [ "it = iter(sq)\n", "next(it)" ] }, { "cell_type": "code", "execution_count": 396, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 396, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 397, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 397, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 398, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "36" ] }, "execution_count": 398, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 399, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "49" ] }, "execution_count": 399, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 401, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "try:\n", " next(it)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Miscellaneous String Methods" ] }, { "cell_type": "code", "execution_count": 403, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 403, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = ' \\n\\r\\t '\n", "s.isspace()" ] }, { "cell_type": "code", "execution_count": 404, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 404, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abcDEF'.isalpha()" ] }, { "cell_type": "code", "execution_count": 406, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 406, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'.isidentifier()" ] }, { "cell_type": "code", "execution_count": 408, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 408, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'_'.isidentifier()" ] }, { "cell_type": "code", "execution_count": 409, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 409, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'666'.isidentifier()" ] }, { "cell_type": "code", "execution_count": 411, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Abc'" ] }, "execution_count": 411, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'.capitalize()" ] }, { "cell_type": "code", "execution_count": 413, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ABC'" ] }, "execution_count": 413, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'.upper()" ] }, { "cell_type": "code", "execution_count": 414, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' hallo '" ] }, "execution_count": 414, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hallo'.center(50)" ] }, { "cell_type": "code", "execution_count": 416, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 416, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'file.xslx'.endswith('.xslx')" ] }, { "cell_type": "code", "execution_count": 417, "metadata": {}, "outputs": [], "source": [ "s = 'mississippi'" ] }, { "cell_type": "code", "execution_count": 418, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 418, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('ss')" ] }, { "cell_type": "code", "execution_count": 419, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 419, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('ss', 3)" ] }, { "cell_type": "code", "execution_count": 420, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 420, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('xxx')" ] }, { "cell_type": "code", "execution_count": 421, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 421, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.index('ss')" ] }, { "cell_type": "code", "execution_count": 422, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 422, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.index('ss', 3)" ] }, { "cell_type": "code", "execution_count": 424, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "substring not found \n" ] } ], "source": [ "try:\n", " s.index('xxx')\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``join``, ``split``" ] }, { "cell_type": "code", "execution_count": 427, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc,def'" ] }, "execution_count": 427, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ['abc', 'def']\n", "','.join(l)" ] }, { "cell_type": "code", "execution_count": 428, "metadata": {}, "outputs": [], "source": [ "import os.path" ] }, { "cell_type": "code", "execution_count": 430, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc/def'" ] }, "execution_count": 430, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.path.join('abc', 'def')" ] }, { "cell_type": "code", "execution_count": 432, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['abc', 'def']" ] }, "execution_count": 432, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc:def'.split(':')" ] }, { "cell_type": "code", "execution_count": 437, "metadata": {}, "outputs": [], "source": [ "firstname, lastname = 'joerg:faschingbauer'.split(':')" ] }, { "cell_type": "code", "execution_count": 438, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'joerg'" ] }, "execution_count": 438, "metadata": {}, "output_type": "execute_result" } ], "source": [ "firstname" ] }, { "cell_type": "code", "execution_count": 440, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'faschingbauer'" ] }, "execution_count": 440, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lastname" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``strip``, ``lstrip``, ``rstrip``" ] }, { "cell_type": "code", "execution_count": 441, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 441, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' abc '.strip()" ] }, { "cell_type": "code", "execution_count": 442, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc '" ] }, "execution_count": 442, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' abc '.lstrip()" ] }, { "cell_type": "code", "execution_count": 443, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' abc'" ] }, "execution_count": 443, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' abc '.rstrip()" ] }, { "cell_type": "code", "execution_count": 444, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' abc '" ] }, "execution_count": 444, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'xy abc yz'.strip('xyz')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# List Comprehension" ] }, { "cell_type": "code", "execution_count": 445, "metadata": {}, "outputs": [], "source": [ "def squares(l):\n", " lret = []\n", " for i in l:\n", " lret.append(i**2)\n", " return lret" ] }, { "cell_type": "code", "execution_count": 447, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "numbers = [1,2,3,4]\n", "sqnums = squares(numbers)\n", "for i in sqnums:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 449, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "9\n", "16\n" ] } ], "source": [ "sqnums = [i**2 for i in numbers]\n", "for i in sqnums:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 450, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "16\n" ] } ], "source": [ "for i in [i**2 for i in numbers if i%2 == 0]:\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Raw Strings" ] }, { "cell_type": "code", "execution_count": 451, "metadata": {}, "outputs": [], "source": [ "s = 'C:\\irgendwas\\nochwas'" ] }, { "cell_type": "code", "execution_count": 452, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\irgendwas\n", "ochwas\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 453, "metadata": {}, "outputs": [], "source": [ "s = 'C:\\\\irgendwas\\\\nochwas'" ] }, { "cell_type": "code", "execution_count": 454, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\irgendwas\\nochwas\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 456, "metadata": {}, "outputs": [], "source": [ "s = r'C:\\irgendwas\\nochwas'" ] }, { "cell_type": "code", "execution_count": 455, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\irgendwas\\nochwas\n" ] } ], "source": [ "print(s)" ] } ], "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.9" } }, "nbformat": 4, "nbformat_minor": 4 }