Datatypes

[1]:
i = 666
[2]:
type(i)
[2]:
int

Strings Intro

[3]:
ein_string = 'hallo'
ein_string
[3]:
'hallo'
[4]:
print(ein_string)
hallo
[5]:
ein_anderer_string = "hallo"
[6]:
print(ein_anderer_string)
hallo
[7]:
s = 'ein paar zeichen, gefolgt von "double quotes", dann noch was'
[8]:
s = "abc ' fghjgfgh' "
[9]:
s
[9]:
"abc ' fghjgfgh' "
[10]:
s = '"" \'dfghgfgh '
[11]:
s
[11]:
'"" \'dfghgfgh '
[12]:
for i in [1,2,3]:
    print(i)
    print(i+1)
print('vorbei')
1
2
2
3
3
4
vorbei

Doc Strings

[13]:
def do_something(a, b):
    '''diese funktion ist komplett sinnlos, und nimmt parameter a und b, die integers
    sein sollten, sonst funktioniert das ganze hier nicht.

    der returnwert ist noch sinnloserer weise die summe der parameter.'''

    # hier wird die komplexe berechnung der summe vorgenommen. obacht!
    return a + b
[14]:
x = do_something(1, 2)
x
[14]:
3
[15]:
type(do_something)
[15]:
function
[16]:
do_something.__doc__
[16]:
'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.'
[17]:
help(do_something)
Help on function do_something in module __main__:

do_something(a, b)
    diese funktion ist komplett sinnlos, und nimmt parameter a und b, die integers
    sein sollten, sonst funktioniert das ganze hier nicht.

    der returnwert ist noch sinnloserer weise die summe der parameter.

Variablen

[18]:
try:
    print(nix)
except Exception as e:
    print(e, type(e))
name 'nix' is not defined <class 'NameError'>
[19]:
nix = 666
print(nix)
666

Object Identity

[20]:
a = 42
id(a)
[20]:
140672599989840
[21]:
b = a
id(b)
[21]:
140672599989840
[22]:
b = [1,2,3]
[23]:
id(b)
[23]:
140672464974720

Integers

Hoechste Integer Zahl auf 64 Bit Rechnern:

[24]:
num = 2**64 -1
num
[24]:
18446744073709551615
[25]:
num = num +1
[26]:
num
[26]:
18446744073709551616
[27]:
num = 2**64
[28]:
num
[28]:
18446744073709551616
[29]:
2**100000
[29]:
9990020930143845079440327643300335909804291390541816917715292738631458324642573483274873313324496504031643944455558549300187996607656176562908471354247492875198889629873671093246350427373112479265800278531241088737085605287228390164568691026850675923517914697052857644696801524832345475543250292786520806957770971741102232042976351205330777996897925116619870771785775955521720081320295204617949229259295623920965797873558158667525495797313144806249260261837941305080582686031535134178739622834990886357758062104606636372130587795322344972010808486369541401835851359858035603574021872908155566580607186461268972839794621842267579349638893357247588761959137656762411125020708704870465179396398710109200363934745618090601613377898560296863598558024761448933047052222860131377095958357319485898496404572383875170702242332633436894423297381877733153286944217936125301907868903603663283161502726139934152804071171914923903341874935394455896301292197256417717233543544751552379310892268182402452755752094704642185943862865632744231332084742221551493315002717750064228826211822549349600557457334964678483269180951895955769174509673224417740432840455882109137905375646772139976621785265057169854834562487518322383250318645505472114369934167981678170255122812978065194806295405339154657479941297499190348507544336414505631657396006693382427316434039580121280260984212247514207834712224831410304068603719640161855741656439472253464945249700314509890093162268952744428705476425472253167514521182231455388374308232642200633025137533129365164341725206256155311794738619142904761445654927128418175183531327052975495370561438239573227939673030106077456848477427832195349227983836436163764742969545906672369124136325932123335643135894465219101882123829740907916386023235450959388766736403229577993901152154448003637215069115591111996001530589107729421032230424262035693493216052927569625858445822354594645276923108197305806280326516736449343761732409753342333289730282959173569273013286423311759605230495171677033163709522256952460402143387655197644016528148022348331881097559421960476479388520198541017348985948511005469246617234143135309938405923268953586538886974427008607028635502085562029549352480050796521564919683265106744100967822951954161617717542997520009887307377876210685890770969411610438028623950445323789591870760289260393489826100774887672852918106468489143893649064784591211612193300707900537059042188012856559403699070888032966871611655961232331998310923225082866180321880439447572986762096935819784385927969250123326935194693207724335527365566248223787833888074999276831633440318604463618703789784313032843823470410944306591471928341190975185239212327674384990561563688432939039442002617530976850605132937101449086396141620556053547335569926700941375271829142407234267937565069765567475934101310225342830080409079587329544213551307302050171598424230760469209732907290141606353960880559202357376885647852240092777111489134492416995607171786298436533978180869474106751111353523711540436599310889697485658800887861974934357929246204051767246012250618404011966289872673803070498361217974484679100747846356194664829224736134115135567179291781968056053726484141128347858241259121954601184412409349782963317042002530418661694962318735860652485410222211869544223788289189712080514575141361964805369723164570564998479537657174548128597406077339158775332355215609435919275199351014222246963017013717419337504919295363295101115292951836282819191821651676455946515828048984256116748150367805267878662716999649296949377045794876146628110929982020737013330324451005385378551188803474148198665114579322684900993000236736168555294173442059925371965244997925483159343706343970371809611470323074186985035054722289027174850333368328300281132910841693150457389933183934593292994942796015309756118708918929528449074243284767006243171171622731766606796101967802204564589015899524704741001158110963633731329388356868949408759334176909387806398584647300588928175998844477486130063153068760070084837267527789777356830042778902772105683833021470279728595336332110564064263909724579949686162908019604141753935768876587992428549912151737924270343248648414247456838889541893241450987505759403013249697541696955330296880219304874163501097920036210238768275176369980977614979636096704348140124130683576879904997436596296495705459524735382000363770324894982103331332913562315169854410415317054193928234723398848453552173203688088312100943941434938282203549650281530751087098604681224802973825631244989331965296202372608586509050307993308652001231671915182765742095689513136184095412121473786311042897717861448158316965848766949554826252504961227044714712229620274682362909803877469376987358942125441792355298387479830450253909788733469732603097544156474805473732732767248652759034995336354126953900458854988683574927864615252040800490114785892289085443353996994780867471613519785838571456421583171193004117989440790268346357550339888086725127883577297626499213827436573992927302238792576924232785487201297255386071968303782483063725899808484638503828356258403917311872694381464553651690062530023217591343084755215901475299149215296944362366910833233693767993138209275870024246238331218236715236772098417187703860172308522448043176333602759733161201262248323085329288986154559221427378507410978822244729512663572225567169779409767341543017289268332635077451210167869121334465680739797372711461919299938118178827541421792926883790285430909942441260511945849237909966329550263865701114884142266162969810073652710928504579470861508094054577797864301504899958634164700528220562786008864025709432444254044034243140203812074857537999016066465520986980790589347320243050635907363821521280600041827529325485247927904235727598574209554632363830932428250711518801775633739811523761994686263270550635099851254333875594601540900862014293625673738331693082328854327001487476635118830885173775268819526360165345900556160767713453617655450974424979076063906093300028416964847594027046669468486593636425428625241644836652173922586528474244952363302305311413449332339822336551611431469131900170488226836525916399723912626616140205707996727383529597479125488961419287261259757561701592645823541151922177253919651034344793680369057003813056557866311011476313189571556336518727757991908862890765494952019474922148851417079252352394293801701149485239005844358329748769279941586384640877265901749104933238853465429979253900561311562288241147192158137210120267399648622831610430287268739840335142120299516610846193164688075944526965248570070554452152547493450434852917987512185973647190461515413582582139040172118295702327537027389787793506904044938553587650503557155872873201596885061331145477101575699375441097493374115991199114962726801718038950907803041184400075585468560976965669584325627283327416418044590727844680051360774154288412712456353383625469068936430902068216750459819321744513362913853983154560610459692604508787700304184579153478291725762810632722108035826060904572460619204237580363147200158749075361633785243462298769917887808671453928846572417223504887766803869453474588831907597355292800709241471370696647029530700507083091412492771404776193459007315206233634226128137074504162520473449597415678882003845446774388950379192344594171245510231738995030348421937088083329709108176561010708693158020695060096428352046647333361163476664106311247065173802510599409266908984046663298613648854871230659903565772327667696057187057276814394932559371368029375974604116075641599919402266794230681485723361363592903676841480358328093127506801111571615062761556607158236612268544268330274725849294875852089790850962835235527978491475563744318483993474633300330972497012808415900969455190375849945750379465019166009861502794606130794726898507849610303884846035423392175449505876157130344700415823080225786693300512126831846009510203543174323783292176865976076275412421892808138872880175813109296020150746331979561488146333412674896256883784351178477592660577212734269328382384711746083782209939646612308343952169576581065423771981899573840430315930973215059901371218399762585055435459516340055149080565627330475362528926945020226163130902420795006258931367813005222140742964756194053782182452833097021554210929638693005460011927178302761563505715735405672652524175925436371863471836292012162456662093642074605500842449347289830619506077570528754845277680661218358066130291463288932240701043887535007851997159198390084654596996197138399723495863749806582439384615049184048485819193560667125968018574877819561104335238420873417743385185735663101292757409280586840011804854994149478736882949368786637202682607198707656286436753775709560349718397405565505269425218354301348910785234517795519757516484711545928466003754558485470994737493796615841040414239875763335201795518644856632201598556341934286668912522153446348791218159622744525372314219184738770596659942181275403613660438538829201810204850917717791485256026242529802492309229562177062770027659288158473994804255067730903420043491632913588644627415318468517462580180901314477358637486528221274450661883667873545037139535563260349778209992416559111602097437491432360787879331015052417047437823553506205617017572175387061751192919715660363028302343819584946594328460482931960515124867123604625653903565173322856758210937541222674223847046664733620292824834065137814475367747671882220098389682019784216724015491253360436437847479770633657905418133523010804559958547379685864708937791659340223795537045273849435441105983887969741143051069401271065628507537039823308867819868298171415185218271493613110963984021912448323423901392553811725954153209435002954807640291982765741514042956666953177304003358701503703497424897898108939453026976878231557938158928996868766367603579055322794822757659104812835219745724022347569914650240636730492833286151875049129873457930874999488048681250802904606446223569562767964898914869924201946458521355165709887118378290437174375625282606140534611987395334677500936625746765638459629521872262777473480491233965194281353725068660782076683862565487279038020486778099991754380815789820825255566234983933217491493864966284116889874665005414748264599972752003370084542592544301190399041231752771993767799847551279448012913842034323154888137932524887172099381195722163148101670274877379161830968937348720168944903299658932511996504109653674618914861599481632040891930577238630396311858213341337110096389113836596895914715370925073998461682046426447290788976525593505136546978364603183820619560578517561504972661817649030304982138534738696212234626114043035600967042547012317360449724623287452575151198771801585742829389025650825988275495110865424704218337264023078045681651420517807418196096401513461760794362769612228126118610912766814880500950963889032877710837651051900076128058473969258768737937306664751387942217354694021157675557568970168734104342446525522568974329716152742558110503495045718931752447070410307760830365536714180388723602948872805590752711115590794756926903978519601939790311768070356801944936106850640568519290645048685535628256787225734544146565541187816717729850612874044620890718502108518025052924590359814117522720320552642597751984410742492179242039080014606225999422109717176118746845802673724801365603866909971071347255859723217027554055085082090418987534829222004178998475030519537179062001509333023023881806519182405550818672164711702307529922652228033820404113386625335815042934115143980939986416365633923620673874259342713444701242702722227197573203194489407856355511639619115985907995399083680129468810771595938084908111251938016414866250141095286680914828503123938960997659175977315432797173945762560365023587931559926170852315074247849814256564693008105061976397395591733545472917526739598790117477449217745771908169489543790314578152667389689410604588351445026130645637237687631129964576699475767340673583537218704935177320214779403972566532581731659202199752942824432778102107532160580104432121067208273876100778332426569624765621063126975491546224343978061253999313893915782008560011171319773443130412998215626985098895722778159524505640435534979855872234521991778419564106622122049039017886737997905270552302412002780864726282517525092332555378737792434915926182736151242592242725872699844014132567954640475742451126102857394193479716383187138370722782422619384021010896271281685732287764210298708895557148397743497418109849633633910397778254225179400022143485886207532122646613614487518735142494469575836744785028013193033901949738716163113800864093408529297729741462836142201120573027427309566658849884965134295188287937016147499504685185116851709758146686994243673140036992381232483920618628663037402351333907771907455224248515748726136075048020960976567862025232356273025557054386729189255571672396871991669651834736987440295942395220863481341484029838939485593327256627318194137735451891544384960966451285561476772564932516923822003229733483331726306200059192067441136913243720403578098676440894672367334549903905283682411422011886949265324531399323764100563996744273693606586851243936737155349696358970470706246743064681511525580772367129358691546677179280674603197443660235664811911175324239792271603213932690900431433572511935591785787438931910836722219749594385217682817205537433987939372420698422586002026702204502341484432231418159837133223467338185995720215212999066855318593765188122815644349842897804044474253173187198249880219056124756835053121936684091113613451740380400041281474527453687530122741226292549431008458868082627169312891414237466973215688306962596297824286828479758198382340345222708757345393897126910174835179971320922911746435455023926629385455742630840473245989374307836875864087914312319228307304975362481708579849870550314357368660453504099679685389802578477910419231052505144696885248726344365369453921372194715998156624454330562798282777349584666757018969900985110164491047993602922961645132995224271404908186948080818144765441476070834943179921347693205775493727581146799477819807044909830887802418354528095576268178221596410712216509995196961932368766959924336939018675830477951348564967496643006978548120861013029026482640402316621018258604172734501970838570222765188590393123627245232006535983907604127898007628220791246163222713051906678029644273228763460060289213316094057396003523608149487855784819923604033709243920895785100953666245538047118419834068589948934026518132741951749125192568729824877982938351710224389886834755562218214866075518628420508441240855088408499405226548535776678864444680504301285988469915501692621173049550268365498340637943341584797390666973271817567111123445212701138287733170703648843560692535012857745637166012927846194135756491508269323010063559087991831480179606664893606864875569154700860602703909640090514609360513037280984989976472934205971076104365706670369636731086616773064361333184164332104073479210775688703754193241101764488071673304176268034528127946729244452919161882095335945667158450941476301537032588109254921620602423998383939639570864268315557737923542830477169053850721939927266830567227442913752680237178175908437269780708099690192695002592421020485193953805155158663266418230452129371046840218806665165231438597498081621420148051355131865453014871298249938624272154345372391802215126115854861975398983110888196358875565793593310597895992053240439684590862193232015232257668969509415398393721308747247732944493053757577694362680328316055060353219685001207195191671426063364056179006218671604638684249067247572855764071631397824639188693789635247708232195940400450829593853651517642510129335911478337995658501766175785429966837417247838838645892319920041196390925354226074199412181950196251598137076467085022479201649174056794994024967593003129957099742026595785390010668925864718839026058417582396497109522944729874183273952292247592156943695682037477731049514128818431210317476600507328613045698141876738705835589680568812062901672389707603039549708273418484069037215179862266581929555420755695654139976814235027490594662592770986044588793675562143709648705744665319814772892177290937799834952769995145061157204394128687121538756842568036622321369508041915737469009917048039885947260486258684449762362318724085339500204989295963618739437408918888568903691128987832325892601331155260901131911962536529384643993465666490271108522834470184768391573623895239732781158399355178022076177450755929138381468526577309152290502509145241134006895661073761320484545645543610238773087594308408699782711315425513472058939339453013548135244176754131385012770024156261936169649709944156933541666590935759784775946468953923516229723673403650805886186209888123677098892944769654360324668874429740130365009770291603536426502844618046128137324006034947889707723262364927515545013731797277615272236008596598736693841467685030166103516988390995570659331713219574003251046303604196295276316972893990945091279545361627593486818455830168634280653205028729105026906643726170661778439311154737149759096116144493812106746037251452616271615212935043141124155849847574091878458410177325030553207237661933970254488335435069113577757765584551136714634629223114497809546405079026755854160954260557783406467356189398503160303046687522579708387993298769182986822970667356246647939653365454544268386404322650296028881298557242198187208040207296077496128886166773288838620609977925416822156713703667391333322086520871904473048593017466793578173830832406459585018177267447690376523178740142636502172093102619263046542112708094769682392207383797482057034793196525139221370199877543931178601238198924707047629587501516299912892933069025343575488251454948469328322898202956506903308470482517744131166464774750723477861518055343466858040987132273453081836794564364430155246274158100731069323080734901590517008753654169600998911569598454702926629374049393677685285723039754691181021469267219333846563053673846537824970049506209403506606244584412627902384242785280551599253188262786271531989567165398385428909311577122438618119724985644233907558812574859217687149721341833317289714977694982117227337669878361212788465289349470676995146785835955074121568375032937822352378665103266923449887690619794116574936418347914823983988002067649967289214983486424468737742286438563064425223461210639952418146166111391586408089740413945879849036682695488197808766021959964846771145698159367567904768112047055740602351237499012258567357965107013251562237843754994747236883821060645969480067316032381351192535148322216761442411144695361182838761115795911310362420741510826322048374540404146990479647073113428368300158247350712922331119789013179701651764033036649606749518628656836032556353205617386602749327417799524243899880581322620681297935615821202736455540414428705857131835238951742471239050758919353490570268302812810704750603181709274434806504843422134152201579626776393642698168851485545206897802036257496043584180735523092410264794436423951474855853980259367017205588767985309971043290404884467541701918912275745752402121744890326681160647837985004639864193645792082105266057503921195311457592621400830660301768120178560328650220171413811642155986702637531155554821958388905393544592180587272433749899242041542816666821846374863337522729077994208960545264863862516411718049112903451378140021506899966762080732651380427792460930762263446994159193223142565029996403134383564087766689537759857473403120477091886596355579944643688205981242040477802943803932459722207541739589596692034052161680837588921636048584789929663903107349945667027830610483130862872680661015300745521669893857383782690975026078691245077096697355744971063878379083608563324774011426604588797712843335089341752120843133549155805945217503766728136818989701075795034975931147113851177529084708382646930943663837851488798723688771534770333456570254004964039987760745951276145936694695508877796318400093229973736770518512573233230127134210937068680950264362225510872877141635601716276267040168996816287213326422268377855327887822310204539762272583014902609710931194995685950834598509258803363019551891846212990914920155838298607568190737375958573348261713794703592503972126314316192159571451081744034114847904386601284244643362857392442316767411036417557776991886719910511456902002905771699177497748359489810115650557253501242519595931483440417116222205504589671636754478042893488385500275211497526246421122010774862333476246182576597178403980436617669560567049936789350370893174617560282074679058110806280348893136768773133234721178541038289213080580369302853910384704528018344058313252744129928355732704028220901951560054449764635932065083760864473989692709381120504251266930834839999956860216356148940167442564078469350152730245749435722205009403680810321136898946443062041659878360406811331217133504960504944165472883926378607868385731515366373416312566122603235977742473940324793478740770352725436532455308438250865693864330020957120719122826689237609273524467576647981797622831709248187143189474449928329051087066866263901462754600186683583622062445553828867143621415773638244852570664653423002003386087185683387095282564231892337305827467825102905239214464117590700082862874915647212962223100149842773910585262712095702633425139069275541249925826898755913048489059083665047386569005980726512682643809800080768716889749767005747228285165430935469423701296901225200041775975278099341399149455675655415791289014141807180970818010668626000334746783492116354330741766556197865412944331579610856733907210970815247694360208231077377319255935591919361509992241851869762511821971598449396422923222586939583205849537600173388242916813257003198490942805178528782842560021549233235032941872842601109623432061485547579038802849228139397130979180562447703494299530585909415031237400213445431607049538809117870571045686211621069655023028886617060314580222884344906879404890744067164557565160596397488164744575409400891911083945126233990453364772541741331330821153758983077390402549739751620825837898384996200874749622331436354553599710472896396262914675542281735624665804212007275401273982269651802596130334329792865330063654175676003369688243926391280713123038760076039957285658333390838592534249337153070457471482743551037417147024006867419141874398230449202772009682282474640802779068104741602882613440803256607530230172031981070796893051954145239327661783905828943932146148894821695918579498502263858318704473290299790473193753803543920285706350672484245008920689712995308213747335255792982565803469779504974053329506220584586976782376547358092670753891736652401577525890202354883227480678961328466821865972869785592672826822656457577958670571097728396605101516387163615321918203652729897586807786443044896628670393188486921931839182927172554503767733076856308657600665342622254535057629030862718937364327448083558928220102374951534665262857504222950995446014428801859061443379679142717413278286975998259993819084477469775220567898395378570154609157695006128689610825519373486612568225010585752375050450503641627804406676905130564807191014132285063482665726828511933059433717373899623654715534816591630086838520905491060194596758223701911513581142071521220673521948777069521608531717477580542554138038978873716058241771904252401056370661792996395808706717083872950238057238547610495061732689587948240319569499000594593719849004104550034415014066154526994609094441391439071866309009249691812468813088126242358787204856089608234037677437509496768451058059579977189006605354903359961784245121096144877063187466910292356627771165203522647016166296488083808997887687769308220856530068153940856748041349469491870704992660210066455819832886130678021460369893174499939107231230405088342485383796742208847060385334943928147519685791292174441917386398739155908915859310750449419969425856920503891277550092560029225566028245733054164370482965351532695757311986033153861767127072322962670122480612322195488891848040149357757131302308897501348573600357134307046628157178720021578705983302856401851403674168523579202306440644159448586566246905477964086572462611834063213025126230896949713642334585419351128373132157232101577687884147986752364441711298472354763142715850293434746434127230225735792097083439910498394019789979870351837786010778667226102241684498686398874227580631272581474396676719404444290071284914286251867085092358453277091441099575357659812178523756848934429670368686181229928033433456696493460770903592231804072849827640955873854491455416480621098619090449331761288966135325413062313991971462332768487224184119088841054819270878948939590681778579588918587881426099241767753862382406002451821149035160124492893940578494055432173589779503867792448614769448734671863583817296627073341404027631029821562303142157743379294616649374361560459036060298581224576085252700570061877609582745974967405315882162695982851143006320897730473133461172142945805407178876223011462558510520930761771493278603541262719880522826864107055049364370555816945830840650960447321868266858216903784868032280479864021706315653687489290467679015864709379478762373013822026373788822827564163725872670921602434077279298565679825234946428376162918717556993454693724759744644374586123577768842647346924365909558553162571564807969034052671504760754742676960038255814034354716520834598864538432593908154392918939089896109083521992519053530398359081516289247571043354786424954980741388745386023113329591390636006694539437438690759589121288758007804082911975207095278133335550232776761783391381794105109412999680430103899475051243232331431120663598726618610084717254696908726637456858466646162093223427680684601718516702427671125913080059583053658180035991214447685286534985397013876080817020918472863351368783745251292936294373137797794168163130983688833624592488270042533275310174037401858155192663147588032450316349828944090941459435709839331454077937638533884824056964583661906580362312059760037127664830995125859310214290237306052000883415749724405028863402500028747055233688541068593389039662379913776485715772093749681055718277781429659244251666703110135820625316518906936462663613376783677001574909561444962828205690820330013121289873007855206280915035880334071746233511142838096550651818271177825964147342275885803162473648253508879276299074813242692461714169521068548818275361612786955216412383263246164034602771053052217127416909831714425842754359325193708367920524870186212708574531518197079797624156322369627530303625010057401503185241771381716916959066550480494267473696656051819527729740440782487416353087074803447126307550453018768734511631260002571765708294113447159058326859464022975489061868569715529325333679192904509181338989039531226114077604146341395120253199068720690450654317360799352434791082039695478061926453254644053124586362026016156957888326419910345519741792140396061817459467332211367113051675844970722080638249309285348257747082754939860608508486386549912912220391895379332321814344998458680557267915553525212113907723389037979427485209309504641599639196053296030292739274663906234190084882219800040754390250718430168480866157009022053158273857417267902310193566064243660834080202786392263652453395504706244108957791049298908397097715806857036527872932846771952274875201321856644284239116502005786314083266606564469023548865452436995428173054383481532525156311603438941440612599662264959878769003712412406305072713049361060777519351766684350979095112470377728527045181653556285621656761979382960837252084759203181114384404005114613990326273362146150667851880079327563486444803726963278475818453468509565365380723807736793526242145533088833679727074192838872685484977387306670007551965761431583941102042492623133242770270821710047137152167948872468741189259805623910487138975034203376673344989060052111727844670530521327425332189734978888307943626099125569741350839434061750712876445222039557455150019442996232739317091487905148812821582894579949748623433452865460274945133477267345362799684706610937707688048379567860499887327287273158785195505216946986571759372875984514994087366735699690322212619073372131984972873714823922056361158276719293687517950957314775347729780073870450969204201405641649670458849057466793682155521901095027966023590200629563426621697556166261956721403439803621918243177242110484823901622121433246394511832190737036276362556947535372619436126996645342229589006736952014870393865015087074414735220220770411070088328030731545041085721762448600324574529642445801550887535581445128918173686346116567111050151109714760544361537920207546115073529167935359803746233158035402507293585422134950239655004818456256327794930441926282941363007224150758144477688955569616025741423859274415740477217643474948663183182648210083785749080222199122144694117400270856159095104885845967392332602301843740328702808569782311182460392642129024729192535226787567897868564600570545108945374190649371558894253329703486982693386634574807801078719254129784907155276683405362407228975018016417486695226639468897799829747034414846605232977531914266638473906810541671168066131262918834764800639654762345192540580400257727950859061817326000188356677576249635084512266410557551645134470262083485480776173701923626406384831371842087477223094467978661221649689842322789248762605167898686475739012260361067924661728390016267344059903224026536343550301552983264519865881303942586617734987280231892544278016113318354276103258002393942607858756949485119300715853929843163967575531352484221274814864027297307155457109423432471856970523635672286551516265672389248080659279448953078708153606666741314598260798811278754656313007785743484189610809499119436037561075326359414143476882494692530787790693275290895909945065272071979419682741840819830567497688086236460819345623637702467815073133466197923119939117195979352833117496820500577299938566476984260815518940866776320855483159836188850448339911351505132909703990088919063133375804094886955895465912648104534621430295733375992898959329664444686900648674531661878821297194146191914132978673375283702995190706179924178140249356285006536273771636355569720003246996130665282451695167273161831393867558791278264933758040011059211372270839123518459709107480669852696080909181872978501574064544548264471078633386599113188810137746318984496745989011540334259315347740082110734818471521253374507271538131048296612979081477663269791394570357390537422769779633811568396223208402597025155304734389883109376
[30]:
0b100110
[30]:
38
[31]:
1 == 2
[31]:
False
[32]:
1 < 2
[32]:
True
[33]:
1 + (2 * 3)
[33]:
7
[34]:
(1 + 2) * 3
[34]:
9
[35]:
2 * 3 * 4
[35]:
24
[36]:
20 % 3
[36]:
2
[37]:
2 * 10 % 3
[37]:
2
[38]:
(2 * 10) % 3
[38]:
2

Exponentiation

[39]:
2**64
[39]:
18446744073709551616
[40]:
3 * 2**64 == 3 * (2**64)
[40]:
True

Floor Division

[41]:
3 / 2    # <----------------- Python 3!!!!!
[41]:
1.5
[42]:
3//2
[42]:
1

Variablen? Datenypen?

[43]:
type(do_something)
[43]:
function
[44]:
type(a)
[44]:
int
[45]:
a = do_something
[46]:
type(a)
[46]:
function
[47]:
help(a)
Help on function do_something in module __main__:

do_something(a, b)
    diese funktion ist komplett sinnlos, und nimmt parameter a und b, die integers
    sein sollten, sonst funktioniert das ganze hier nicht.

    der returnwert ist noch sinnloserer weise die summe der parameter.

[48]:
a(2, 3)
[48]:
5
[49]:
a.__doc__
[49]:
'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.'
[50]:
dir(a)
[50]:
['__annotations__',
 '__call__',
 '__class__',
 '__closure__',
 '__code__',
 '__defaults__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__get__',
 '__getattribute__',
 '__globals__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__kwdefaults__',
 '__le__',
 '__lt__',
 '__module__',
 '__name__',
 '__ne__',
 '__new__',
 '__qualname__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__']
[51]:
a.__name__
[51]:
'do_something'
[52]:
a.__class__
[52]:
function

Objects, Classes

[53]:
class Karteikarte:
    def __init__(self, firstname, lastname, svnr):
        self.firstname = firstname
        self.lastname = lastname
        self.svnr = svnr
    def format(self):
        my_formatted_person = self.firstname + ' ' + self.lastname + ' (' + self.svnr + ')'
        return my_formatted_person
[54]:
joerg = Karteikarte('Joerg', 'Faschingbauer', '1037190666')
caro = Karteikarte('Caro', 'Faschingbauer', '123425041997')
[55]:
print(joerg.firstname + ' ' + joerg.lastname + ' (' + joerg.svnr + ')')
Joerg Faschingbauer (1037190666)
[56]:
print(caro.firstname + ' ' + caro.lastname + ' (' + caro.svnr + ')')
Caro Faschingbauer (123425041997)
[57]:
caros_string = caro.format()
joergs_string = joerg.format()
[58]:
print(caros_string)
Caro Faschingbauer (123425041997)
[59]:
dir(caro)
[59]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'firstname',
 'format',
 'lastname',
 'svnr']
[60]:
caro.__class__
[60]:
__main__.Karteikarte
[61]:
caro.format
[61]:
<bound method Karteikarte.format of <__main__.Karteikarte object at 0x7ff0dc48b490>>

und strings???

[62]:
s = 'Mississippi'
[63]:
s.__class__
[63]:
str
[64]:
type(s)
[64]:
str
[65]:
s.count('ss')
[65]:
2
[66]:
pos = s.find('ss')
[67]:
pos
[67]:
2
[68]:
next_pos = s.find('ss', pos+1)
[69]:
next_pos
[69]:
5

Datatype Conversions

[70]:
type(str)
[70]:
type
[71]:
type(Karteikarte)
[71]:
type
[72]:
s = str('abc')
[73]:
s
[73]:
'abc'
[74]:
s = 'abc'
[75]:
s
[75]:
'abc'
[76]:
i = 42
[77]:
s = str(i)
s
[77]:
'42'
[78]:
type(int)
[78]:
type
[79]:
i = int('666')
[80]:
i
[80]:
666
[81]:
type(i)
[81]:
int
[82]:
int('0xdeadbeef', 16)
[82]:
3735928559
[83]:
float('0.1234')
[83]:
0.1234
[84]:
try:
    float('Joerg')
except Exception as e:
    print(e, type(e))
could not convert string to float: 'Joerg' <class 'ValueError'>
[85]:
bool(1)
[85]:
True
[86]:
bool(42)
[86]:
True
[87]:
bool(-1)
[87]:
True
[88]:
bool(0)
[88]:
False
[89]:
bool('Joerg')
[89]:
True
[90]:
bool('')
[90]:
False
[91]:
if 'Joerg':
    print('Oida!')
Oida!
[92]:
if len('Joerg') != 0:
    print('Oida!')
Oida!
[93]:
l = [1,2,3]
type(l)
[93]:
list
[94]:
l = list('Joerg')
l
[94]:
['J', 'o', 'e', 'r', 'g']
[95]:
import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Boolean

[96]:
True and False
[96]:
False
[97]:
True or False
[97]:
True
[98]:
False or False
[98]:
False
[99]:
True and True
[99]:
True
[100]:
False and True
[100]:
False
[101]:
def wahr():
    print('wahr')
    return True
def falsch():
    print('falsch')
    return False
[102]:
x = wahr()
print(x)
wahr
True
[103]:
if wahr():
    print('jo eh')
else:
    print('nix da')
wahr
jo eh
[104]:
if wahr() and wahr():
    print('jo eh')
wahr
wahr
jo eh
[105]:
if wahr() or falsch():
    print('jo eh')
wahr
jo eh
[106]:
if falsch() and wahr():    # False and True
    print('jo eh')
else:
    print('nix da')
falsch
nix da

Compound Datatypes

Listen

[107]:
l = [1, 2, 'drei', ['4.0', 5], 666.7]
[108]:
len(l)
[108]:
5
[109]:
l[0]
[109]:
1
[110]:
l[3]
[110]:
['4.0', 5]
[111]:
l[3][1]
[111]:
5
[112]:
l[2][2]
[112]:
'e'
[113]:
l[3][0][1]
[113]:
'.'
[114]:
try:
    l[4][3]
except Exception as e:
    print(e, type(e))
'float' object is not subscriptable <class 'TypeError'>

BTW, wie kriegt man die Nachkommastellen aus einem Float raus?

[115]:
f = 666.789

Erste Moeglichkeit:

[116]:
round((f - (int(f))) * 1000)
[116]:
789

Zweite Moeglichkeit:

[117]:
f_str = str(f)
f_str
[117]:
'666.789'
[118]:
punkt_pos = f_str.find('.')
[119]:
punkt_pos
[119]:
3
[120]:
nachkomma_str = f_str[punkt_pos+1:]
[121]:
int(nachkomma_str)
[121]:
789

Weiter im Text …

[122]:
l
[122]:
[1, 2, 'drei', ['4.0', 5], 666.7]
[123]:
l.append('sieben')
[124]:
l
[124]:
[1, 2, 'drei', ['4.0', 5], 666.7, 'sieben']
[125]:
l.extend(['eight', 9.0])
[126]:
l
[126]:
[1, 2, 'drei', ['4.0', 5], 666.7, 'sieben', 'eight', 9.0]
[127]:
l.extend('aber hallo')
[128]:
l
[128]:
[1,
 2,
 'drei',
 ['4.0', 5],
 666.7,
 'sieben',
 'eight',
 9.0,
 'a',
 'b',
 'e',
 'r',
 ' ',
 'h',
 'a',
 'l',
 'l',
 'o']
[129]:
l1 = [1,2,3]
l2 = ['vier', 'fuenf']
l = l1+l2
l
[129]:
[1, 2, 3, 'vier', 'fuenf']

Tuple

[130]:
t = (1,2,3)
[131]:
t[0]
[131]:
1
[132]:
t[1:]
[132]:
(2, 3)
[133]:
try:
    t.append(4)
except Exception as e:
    print(e, type(e))
'tuple' object has no attribute 'append' <class 'AttributeError'>

Single element tuples?

[134]:
t = (1)     # '(', ')': Operator praezedenz
type(t)
[134]:
int
[135]:
(1+2)*3
[135]:
9
[136]:
t = ((1))
t
[136]:
1
[137]:
[(1)]
[137]:
[1]
[138]:
(1,)
[138]:
(1,)
[139]:
t1 = (1,2,3)
t2 = ('vier', 'fuenf')
t = t1+t2
t
[139]:
(1, 2, 3, 'vier', 'fuenf')

Dictionary

[140]:
table = {'one': 1,
        'two': 2}
[141]:
table['two']
[141]:
2
[142]:
table['three'] = 3
[143]:
'three' in table
[143]:
True
[144]:
'four' in table
[144]:
False
[145]:
del table['one']
[146]:
'one' in table
[146]:
False
[147]:
'one' not in table
[147]:
True
[148]:
not 'one' in table
[148]:
True

Set

Sequentielle Suche in Liste

[149]:
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[150]:
2 in l
[150]:
True
[151]:
11 in l
[151]:
True
[152]:
12 in l
[152]:
False
[153]:
s = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
[154]:
11 in s
[154]:
True
[155]:
s.remove(11)
[156]:
11 not in s
[156]:
True
[157]:
type(s)
[157]:
set

Frage: wie wandele ich ein Dictionary in ein Set um? Oder gach in eine Liste?

[158]:
table
[158]:
{'two': 2, 'three': 3}
[159]:
list('Joerg')   # <-- ITERATION
[159]:
['J', 'o', 'e', 'r', 'g']
[160]:
set('Joerg')
[160]:
{'J', 'e', 'g', 'o', 'r'}
[161]:
for k in table.keys():
    print(k)
two
three
[162]:
for k in table:
    print(k)
two
three
[163]:
set(table)
[163]:
{'three', 'two'}

Wie mache ich aus einem Set ein Dictionary? (Hm. Woher kommen die Keys?)

[164]:
import random
[165]:
s = {1, 2, 3}
[166]:
d = {}
for element in s:
    d[element] = random.randrange(100)
[167]:
d
[167]:
{1: 65, 2: 45, 3: 69}

Python Standard Libarary Examples

collections

ChainMap

[168]:
from collections import ChainMap
[169]:
d1 = {'one': 1, 'two': 2}
d2 = {'three': 3, 'four': 4}
[170]:
chained = ChainMap(d1, d2)
chained['one']
[170]:
1
[171]:
chained['three']
[171]:
3
[172]:
chained['five'] = 5
[173]:
d1
[173]:
{'one': 1, 'two': 2, 'five': 5}
[174]:
d2
[174]:
{'three': 3, 'four': 4}
[175]:
try:
    d['ten']
except Exception as e:
    print(e, type(e))
'ten' <class 'KeyError'>

deque

Liste vorne anhaengen:

[176]:
l = [3,4,5]
l.append(6)
[177]:
l.insert(0, 2)
l
[177]:
[2, 3, 4, 5, 6]
[178]:
del l[0]
[179]:
l
[179]:
[3, 4, 5, 6]

Aber: langsamer als man sich wuenschen wuerde

[180]:
import collections
[181]:
d = collections.deque([3, 4, 5])
[182]:
d
[182]:
deque([3, 4, 5])
[183]:
d.append(6)
d
[183]:
deque([3, 4, 5, 6])
[184]:
d.appendleft(2)
d
[184]:
deque([2, 3, 4, 5, 6])
[185]:
d.extend('Joerg')
d
[185]:
deque([2, 3, 4, 5, 6, 'J', 'o', 'e', 'r', 'g'])
[186]:
d.extendleft('Faschingbauer')
d
[186]:
deque(['r',
       'e',
       'u',
       'a',
       'b',
       'g',
       'n',
       'i',
       'h',
       'c',
       's',
       'a',
       'F',
       2,
       3,
       4,
       5,
       6,
       'J',
       'o',
       'e',
       'r',
       'g'])
[187]:
relem = d.pop()
relem
[187]:
'g'
[188]:
d
[188]:
deque(['r',
       'e',
       'u',
       'a',
       'b',
       'g',
       'n',
       'i',
       'h',
       'c',
       's',
       'a',
       'F',
       2,
       3,
       4,
       5,
       6,
       'J',
       'o',
       'e',
       'r'])
[189]:
d.popleft()
[189]:
'r'
[190]:
d
[190]:
deque(['e',
       'u',
       'a',
       'b',
       'g',
       'n',
       'i',
       'h',
       'c',
       's',
       'a',
       'F',
       2,
       3,
       4,
       5,
       6,
       'J',
       'o',
       'e',
       'r'])

os.path

[191]:
import os.path
[192]:
os.path.join('a', 'b')
[192]:
'a/b'
[193]:
os.path.normpath('a/b/c/d/../../x')
[193]:
'a/b/x'

Suchalgorithmen und Datenstrukturen (Ausflug)

[194]:
l = [5, 3, 1, 10, 8, 0]
l.sort()
[195]:
l
[195]:
[0, 1, 3, 5, 8, 10]
[196]:
l = [5, 3, 1, 10, 8, 0]
sorted_l = sorted(l)
[197]:
l
[197]:
[5, 3, 1, 10, 8, 0]
[198]:
sorted_l
[198]:
[0, 1, 3, 5, 8, 10]
[199]:
t = (5, 3, 1, 10, 8, 0)
sorted_t = sorted(t)
[200]:
t
[200]:
(5, 3, 1, 10, 8, 0)
[201]:
sorted_t
[201]:
[0, 1, 3, 5, 8, 10]
[202]:
type(sorted_t)
[202]:
list
[203]:
s = 'Joerg'
sorted_s = sorted(s)
[204]:
s
[204]:
'Joerg'
[205]:
sorted_s
[205]:
['J', 'e', 'g', 'o', 'r']

Slicing

[206]:
text = 'Hello World'
[207]:
len(text)
[207]:
11
[208]:
text[0:5]
[208]:
'Hello'
[209]:
text[:5]
[209]:
'Hello'
[210]:
text[6:11]
[210]:
'World'
[211]:
text[6:]
[211]:
'World'
[212]:
text = 'Mississippi'
pos = text.find('ss')
pos
[212]:
2
[213]:
subtext = text[pos:]
subtext
[213]:
'ssissippi'

Copy:

[214]:
text
[214]:
'Mississippi'
[215]:
id(text)
[215]:
140672464726576
[216]:
copied_text = text[:]
copied_text
[216]:
'Mississippi'
[217]:
id(copied_text)
[217]:
140672464726576
[218]:
l = [1,2,3]
copied_l = l[:]
copied_l
[218]:
[1, 2, 3]
[219]:
id(l)
[219]:
140672464784832
[220]:
id(copied_l)
[220]:
140672464784064

Slice Assignment

[221]:
l = [2, 3, 'a', 'b', 7]    # wanted: [2,3,4,5,6,7]
[222]:
l[2:4]
[222]:
['a', 'b']
[223]:
l[2:4] = [4,5,6]
l
[223]:
[2, 3, 4, 5, 6, 7]
[224]:
l[:0]
[224]:
[]
[225]:
l[:0] = [0,1]
l
[225]:
[0, 1, 2, 3, 4, 5, 6, 7]
[226]:
l.insert(0, -1)
l
[226]:
[-1, 0, 1, 2, 3, 4, 5, 6, 7]
[227]:
l[len(l):]
[227]:
[]
[228]:
l[len(l):] = [8,9]
[229]:
l
[229]:
[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Lesbarer:

[230]:
l.extend([10,11])
[231]:
l
[231]:
[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

References, (Im)mutability

[232]:
a = 42
[233]:
id(a)
[233]:
140672599989840
[234]:
b = a
id(b)
[234]:
140672599989840
[235]:
a += 1
[236]:
a
[236]:
43
[237]:
id(a)
[237]:
140672599989872
[238]:
id(b)
[238]:
140672599989840
[239]:
b
[239]:
42

Lists are mutable

[240]:
l1 = [1, ['abc', 'def'], 3]
len(l1)
[240]:
3
[241]:
l1[0]
[241]:
1
[242]:
l1[1]
[242]:
['abc', 'def']
[243]:
l1[2]
[243]:
3

Machen wir eine Kopie:

[244]:
l2 = l1[:]
[245]:
id(l1)
[245]:
140672464751872
[246]:
id(l2)
[246]:
140672464768512
[247]:
l1
[247]:
[1, ['abc', 'def'], 3]
[248]:
l2
[248]:
[1, ['abc', 'def'], 3]
[249]:
l1.append(4)
l1
[249]:
[1, ['abc', 'def'], 3, 4]
[250]:
l2
[250]:
[1, ['abc', 'def'], 3]
[251]:
l1[1]
[251]:
['abc', 'def']
[252]:
l2[1]
[252]:
['abc', 'def']
[253]:
l1[1].append('ghi')
l1
[253]:
[1, ['abc', 'def', 'ghi'], 3, 4]
[254]:
l2
[254]:
[1, ['abc', 'def', 'ghi'], 3]
[255]:
id(l1[1])
[255]:
140672464594880
[256]:
id(l2[1])
[256]:
140672464594880
[257]:
l1[1] is l2[1]
[257]:
True

ist das gleiche wie …

[258]:
id(l1[1]) == id(l2[1])
[258]:
True
[259]:
l1.insert(0, -100)
l1
[259]:
[-100, 1, ['abc', 'def', 'ghi'], 3, 4]
[260]:
l1[2] is l2[1]
[260]:
True
[261]:
l2
[261]:
[1, ['abc', 'def', 'ghi'], 3]
[262]:
l2[1].append('jkl')
[263]:
l2
[263]:
[1, ['abc', 'def', 'ghi', 'jkl'], 3]
[264]:
l1
[264]:
[-100, 1, ['abc', 'def', 'ghi', 'jkl'], 3, 4]

Shallow copy and deep copy

[265]:
l1 = [1, ['abc', 'def'], 3]

Shallow copy:

[266]:
l2 = l1[:]
[267]:
l1[1] is l2[1]
[267]:
True

Deep copy:

[268]:
import copy
[269]:
l3 = copy.deepcopy(l1)
l1[1] is l3[1]
[269]:
False
[270]:
l1[1].append('ghi')
l1
[270]:
[1, ['abc', 'def', 'ghi'], 3]
[271]:
l3
[271]:
[1, ['abc', 'def'], 3]

while Loops

[272]:
i = 0
while i < 10:
    i += 1
print(i)
10
[273]:
i = 0
while i < 10:
    print('hallo suesser')
    i += 1
hallo suesser
hallo suesser
hallo suesser
hallo suesser
hallo suesser
hallo suesser
hallo suesser
hallo suesser
hallo suesser
hallo suesser
[274]:
for i in range(10):
    print('hallo suesser')
hallo suesser
hallo suesser
hallo suesser
hallo suesser
hallo suesser
hallo suesser
hallo suesser
hallo suesser
hallo suesser
hallo suesser
[275]:
import random
while True:
    number = random.randrange(1,7)
    if number == 6:
        print('yay!')
        break
    else:
        print(f'lose ({number})')
lose (3)
lose (1)
lose (1)
lose (1)
lose (2)
lose (3)
yay!

else

Potschert: Flag

[276]:
import random

sechser_gewuerfelt = False
n_tries = 0
while n_tries < 6:
    n_tries += 1
    number = random.randrange(1,7)
    if number == 6:
        sechser_gewuerfelt = True
        break

if sechser_gewuerfelt:
    print('yay!')
else:
    print('versager!')
yay!
[277]:
import random

n_tries = 0
while n_tries < 6:
    n_tries += 1
    number = random.randrange(1,7)
    if number == 6:
        print('yay!')
        break
else:
    print('versager!')
versager!

for Loops, range()

[278]:
for name in ['Caro', 'Johanna', 'Eva', 'Jörg']:
    print(name)
Caro
Johanna
Eva
Jörg
[279]:
for _ in [0,1,2,3,4,5]:
    number = random.randrange(1,7)
    if number == 6:
        print('yay!')
        break
else:
    print('versager!')
versager!
[280]:
for _ in range(6):     # python2: xrange()
    number = random.randrange(1,7)
    if number == 6:
        print('yay!')
        break
else:
    print('versager!')
yay!
[281]:
for i in range(3):
    print(i)
0
1
2
[282]:
for i in range(3,10):
    print(i)
3
4
5
6
7
8
9
[283]:
for i in range(3,10,2):
    print(i)
3
5
7
9

Iterator Protocol

[284]:
r = range(3)
r
[284]:
range(0, 3)
[285]:
type(r)
[285]:
range
[286]:
it = iter(r)
it
[286]:
<range_iterator at 0x7ff0dc48bed0>
[287]:
next(it)
[287]:
0
[288]:
next(it)
[288]:
1
[289]:
next(it)
[289]:
2
[290]:
try:
    next(it)
except StopIteration as e:
    print(e, type(e))
 <class 'StopIteration'>
[293]:
for i in range(3):
    print(i)
0
1
2

Iteration over Compound Datatypes

[295]:
s = {1,2,3}
for element in s:
    print(element)
1
2
3
[296]:
d = {
    0: 'zero',
    1: 'one',
    2: 'two',
}
[297]:
for element in d:
    print(element)
0
1
2
[300]:
for key in d.keys():
    print(key)
0
1
2
[302]:
for value in d.values():
    print(value)
zero
one
two
[303]:
for element in d.items():
    print(element)
(0, 'zero')
(1, 'one')
(2, 'two')
[304]:
for pair in d.items():
    key = pair[0]
    value = pair[1]
    print(f'Key: {key}, value: {value}')
Key: 0, value: zero
Key: 1, value: one
Key: 2, value: two

Tuple unpacking:

[309]:
a, b = 1, 2
[310]:
(a, b) = (1, 2)
[307]:
a
[307]:
1
[308]:
b
[308]:
2
[311]:
t = (1, 2)
a, b = t
[312]:
a
[312]:
1
[313]:
b
[313]:
2
[314]:
for key, value in d.items():
    print(f'Key: {key}, value: {value}')
Key: 0, value: zero
Key: 1, value: one
Key: 2, value: two

Functions

[317]:
def maximum(a, b):
    if a < b:
        return b
    return a
[316]:
maximum(1,2)
[316]:
2
[319]:
maximum('abc', 'def')
[319]:
'def'
[325]:
'abc' < 'def'
[325]:
True
[326]:
'Huber' > 'Hannes'
[326]:
True
[323]:
1 < 2
[323]:
True
[328]:
try:
    1 < 'abc'
except Exception as e:
    print(e, type(e))
'<' not supported between instances of 'int' and 'str' <class 'TypeError'>
[331]:
try:
    maximum(1, 'abc')
except Exception as e:
    print(e, type(e))
'<' not supported between instances of 'int' and 'str' <class 'TypeError'>
[335]:
[3,1,4] < [3,4,5]
[335]:
True
[336]:
maximum([3,1,4], [3,4,5])
[336]:
[3, 4, 5]

Default Parameters

[338]:
def greet(name, phrase):
    return phrase + ', ' + name
print(greet('Joerg', 'Hallo'))
Hallo, Joerg
[339]:
def greet(name, phrase='Hallo'):
    return phrase + ', ' + name
greet('Joerg')
[339]:
'Hallo, Joerg'
[340]:
greet('Joerg', 'Gruess Gott')
[340]:
'Gruess Gott, Joerg'

Default Parameters: Pitfalls

[341]:
def fill_in_user(svnr, firstname, lastname, database):
    if database.get(svnr):
        raise Exception(svnr + ' gibts scho')
    database[svnr] = (firstname, lastname)
[342]:
my_db = {}
fill_in_user('1037190666', 'Joerg', 'Faschingbauer', my_db)
my_db
[342]:
{'1037190666': ('Joerg', 'Faschingbauer')}
[351]:
def fill_in_user(svnr, firstname, lastname, database={}):
    if database.get(svnr):
        raise Exception(svnr + ' gibts scho')
    database[svnr] = (firstname, lastname)
[352]:
fill_in_user('1037190666', 'Joerg', 'Faschingbauer')
[353]:
try:
    fill_in_user('1037190666', 'Joerg', 'Faschingbauer')
except Exception as e:
    print(e, type(e))
1037190666 gibts scho <class 'Exception'>

dict.fromkeys()

[372]:
dict.fromkeys(range(5))
[372]:
{0: None, 1: None, 2: None, 3: None, 4: None}
[373]:
dict.fromkeys(range(5), 666)
[373]:
{0: 666, 1: 666, 2: 666, 3: 666, 4: 666}
[374]:
l = [2, 3, 1, 10, 3, 3, 1, 10, 5, 2]
uniq_dict = dict.fromkeys(l)
uniq_dict
[374]:
{2: None, 3: None, 1: None, 10: None, 5: None}
[375]:
for key in uniq_dict.keys():
    print(key)
2
3
1
10
5
[377]:
list(uniq_dict.keys())
[377]:
[2, 3, 1, 10, 5]

Generators

Generator, der die Quadratzahlen startend von einem konfigurierbaren Startwert ausgibt …

Start = 3 9, 16, 25, 36, 49, …

[378]:
def squares(start, max):
    l = []
    i = start
    while True:
        sqnum = i**2
        i += 1
        if sqnum == max:
            break
        l.append(sqnum)
    return l
[381]:
for num in squares(3, 100):
    print(num)
9
16
25
36
49
64
81
[393]:
def squares(start, max):
    i = start
    while True:
        sqnum = i**2
        i += 1
        if sqnum >= max:
            break
        yield sqnum
[383]:
for num in squares(3, 100):
    print(num)
9
16
25
36
49
64
81
[394]:
sq = squares(3, 50)
sq
[394]:
<generator object squares at 0x7ff014b01820>
[395]:
it = iter(sq)
next(it)
[395]:
9
[396]:
next(it)
[396]:
16
[397]:
next(it)
[397]:
25
[398]:
next(it)
[398]:
36
[399]:
next(it)
[399]:
49
[401]:
try:
    next(it)
except Exception as e:
    print(e, type(e))
 <class 'StopIteration'>

Miscellaneous String Methods

[403]:
s = '    \n\r\t   '
s.isspace()
[403]:
True
[404]:
'abcDEF'.isalpha()
[404]:
True
[406]:
'abc'.isidentifier()
[406]:
True
[408]:
'_'.isidentifier()
[408]:
True
[409]:
'666'.isidentifier()
[409]:
False
[411]:
'abc'.capitalize()
[411]:
'Abc'
[413]:
'abc'.upper()
[413]:
'ABC'
[414]:
'hallo'.center(50)
[414]:
'                      hallo                       '
[416]:
'file.xslx'.endswith('.xslx')
[416]:
True
[417]:
s = 'mississippi'
[418]:
s.find('ss')
[418]:
2
[419]:
s.find('ss', 3)
[419]:
5
[420]:
s.find('xxx')
[420]:
-1
[421]:
s.index('ss')
[421]:
2
[422]:
s.index('ss', 3)
[422]:
5
[424]:
try:
    s.index('xxx')
except Exception as e:
    print(e, type(e))
substring not found <class 'ValueError'>

join, split

[427]:
l = ['abc', 'def']
','.join(l)
[427]:
'abc,def'
[428]:
import os.path
[430]:
os.path.join('abc', 'def')
[430]:
'abc/def'
[432]:
'abc:def'.split(':')
[432]:
['abc', 'def']
[437]:
firstname, lastname = 'joerg:faschingbauer'.split(':')
[438]:
firstname
[438]:
'joerg'
[440]:
lastname
[440]:
'faschingbauer'

strip, lstrip, rstrip

[441]:
'         abc       '.strip()
[441]:
'abc'
[442]:
'         abc       '.lstrip()
[442]:
'abc       '
[443]:
'         abc       '.rstrip()
[443]:
'         abc'
[444]:
'xy   abc    yz'.strip('xyz')
[444]:
'   abc    '

List Comprehension

[445]:
def squares(l):
    lret = []
    for i in l:
        lret.append(i**2)
    return lret
[447]:
numbers = [1,2,3,4]
sqnums = squares(numbers)
for i in sqnums:
    print(i)
1
4
9
16
[449]:
sqnums = [i**2 for i in numbers]
for i in sqnums:
    print(i)
1
4
9
16
[450]:
for i in [i**2 for i in numbers if i%2 == 0]:
    print(i)
4
16

Raw Strings

[451]:
s = 'C:\irgendwas\nochwas'
[452]:
print(s)
C:\irgendwas
ochwas
[453]:
s = 'C:\\irgendwas\\nochwas'
[454]:
print(s)
C:\irgendwas\nochwas
[456]:
s = r'C:\irgendwas\nochwas'
[455]:
print(s)
C:\irgendwas\nochwas