{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Algoritmusok Python nyelven\n", "\n", "## 2. Előadás, Függvények, egyszerű adattípusok, adatszerkezetek.\n", "\n", "### 2020. február 20." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Technikai információk\n", "\n", "### Diák elérhetősége:\n", "[damasdigabor.web.elte.hu/teaching](https://damasdigabor.web.elte.hu/teaching)\n", "\n", "Óra kezdés: 14:00" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python\n", " - Indentálás, nem kell pontosvessző\n", " - Dinamikus típusok\n", " - Értékadásnál először a jobb oldal kiértékelődik, majd a bal oldalon lévő nevet hozzárendeljük az eredményhez." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Apróságok\n", " - A `#`-al kezdődő sorok kommentek, nem befolyásolják a programot. " ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+2\n", "# 2+3\n" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [], "source": [ "import this" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### break és continue\n", "\n", "- `break`: ha korábban ki akarunk lépni egy ciklusból\n", "- `continue`: ha korábban szeretnénk a következő ciklus futásra lépni" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "3\n", "5\n", "7\n", "9\n" ] } ], "source": [ "for i in range(10):\n", " if i % 2 == 0:\n", " continue\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 137, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in range(10):\n", " if i > 4:\n", " break\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Függvények" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "én egy nagyon okos függvény vagyok\n" ] } ], "source": [ "def foo():\n", " print(\"én egy nagyon okos függvény vagyok\")\n", " \n", "foo()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Függvény argumentumok, paraméterek \n", "\n", "1. pozíció szerinti (positional)\n", "2. kulcsszavas (named or keyword arguments)\n", "\n", "Először a pozíció szerintieket kell írni aztán a kulcsszavasokat" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arg1 1\n", "arg2 2\n", "arg3 3\n" ] } ], "source": [ "def foo(arg1, arg2, arg3):\n", " print(\"arg1 \", arg1)\n", " print(\"arg2 \", arg2)\n", " print(\"arg3 \", arg3)\n", " \n", "foo(1, 2,3)" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arg1 1\n", "arg2 29\n", "arg3 2\n" ] } ], "source": [ "foo(1, arg3=2, arg2=29)" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [], "source": [ "arg3=4" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Visszatérési érték, a `return` parancs\n", "\n", "- Egy függvénynek több visszatérési értéke is lehet\n", " - Ilyenkor az értékek egy tuple-be kerülnek.\n", "- ha a függvény futása úgy ér véget, hogy nem ér el `return` parancsot, a visszatérési érték automatikusan `None` lesz.\n", "- Egy üres `return` parancs is `None`-nal tér vissza. " ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "negative\n", "('positive', 3) \n", "None\n" ] } ], "source": [ "def foo(n):\n", " if n < 0:\n", " return \"negative\"\n", " if 0 <= n < 10:\n", " return \"positive\", n\n", " # return None\n", " # return\n", "\n", "print(foo(-2))\n", "print(foo(3), type(foo(3)))\n", "print(foo(12))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mit is jelent, hogy egy függvényt meghívunk valamilyen argumentumokkal?\n", " - Létrejön egy lokális változó a paraméter névvel és az argumentumra fog mutatni. \n", " - Tényleg az átadott objektumra fog mutatni és nem csak egy másolatra!\n", " - Alapvetően az át nem adott objektumokat viszont nem látja!" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['matek']\n", "['matek', 'matek']\n", "['matek', 'matek', 'matek']\n" ] } ], "source": [ "l=[\"matek\"]\n", "def add_matek(k):\n", " k.append(\"matek\")\n", "\n", "print(l)\n", "add_matek(l)\n", "print(l)\n", "add_matek(l)\n", "print(l)\n" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "matek\n", "matek\n", "matek\n" ] } ], "source": [ "s=\"matek\"\n", "def add_matek(k):\n", " k=k+\"matek\"\n", "\n", "print(s)\n", "add_matek(s)\n", "print(s)\n", "add_matek(s)\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Alapértelmezett argumentum (Default arguments)\n", "\n", "- Az argumentumoknak lehet alapértelmezett értéke. Ha nem érkezik egy ilyen paraméterhez argumentum akkor az alapértelmezett argumentumot kapja meg a függvény\n", "- Először kell megadni azokat az argumentumokat, amiknek nincs alapértelmezett értéke." ] }, { "cell_type": "code", "execution_count": 146, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arg1 1\n", "arg2 2\n", "arg3 3\n" ] } ], "source": [ "def foo(arg1, arg2, arg3=3):\n", " print(\"arg1 \", arg1)\n", " print(\"arg2 \", arg2)\n", " print(\"arg3 \", arg3)\n", "foo(1, 2)" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arg1 2\n", "arg2 1\n", "arg3 3\n" ] } ], "source": [ "foo(2, arg2=1)" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arg1 1\n", "arg2 222\n", "arg3 33\n" ] } ], "source": [ "foo(arg1=1, arg3=33, arg2=222)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Tetszőleges kihagyható közülük. " ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arg1 11\n", "arg2 33\n", "arg3 3\n", "\n", "arg1 11\n", "arg2 2\n", "arg3 33\n" ] } ], "source": [ "def foo(arg1, arg2=2, arg3=3):\n", " print(\"arg1 \", arg1)\n", " print(\"arg2 \", arg2)\n", " print(\"arg3 \", arg3)\n", " \n", "foo(11, 33)\n", "print(\"\")\n", "foo(11, arg3=33)\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Emiatt rengeteg argumentumod lehet úgy is, hogy ez nem nehezíti meg a függvény használatát. Sok könyvtárban találunk olyan függvényeket, amiknek rengeteg argumentumunk van." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Például a következő függvényt a `pandas` könyvtárban találjuk:\n", "\n", "~~~python\n", " pandas.read_csv(filepath_or_buffer, sep=', ', delimiter=None, header='infer', names=None, index_col=None, usecols=None, squeeze=False, prefix=None, mangle_dupe_cols=True, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skipinitialspace=False, skiprows=None, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False, skip_blank_lines=True, parse_dates=False, infer_datetime_format=False, keep_date_col=False, date_parser=None, dayfirst=False, iterator=False, chunksize=None, compression='infer', thousands=None, decimal=b'.', lineterminator=None, quotechar='\"', quoting=0, escapechar=None, comment=None, encoding=None, dialect=None, tupleize_cols=False, error_bad_lines=True, warn_bad_lines=True, skipfooter=0, skip_footer=0, doublequote=True, delim_whitespace=False, as_recarray=False, compact_ints=False, use_unsigned=False, low_memory=True, buffer_lines=None, memory_map=False, float_precision=None)\n", " ~~~" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Dinamikus tipusok\n", "\n", "- nem kell deklarálni a változókat\n", "- az értékadás tetszőleges objektumra működik" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(int, 140705633509808)" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 2\n", "type(i), id(i)" ] }, { "cell_type": "code", "execution_count": 151, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "(str, 3070806732848)" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = \"foo\"\n", "type(i), id(i)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Erősen típusos nyelv\n", "\n", "- a legtöbb implicit konverzió nem megengedett.\n", "- numerikus típusok között lehet konvertálni:" ] }, { "cell_type": "code", "execution_count": 152, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n", "\n", "3.2 \n" ] } ], "source": [ "i = 2\n", "f = 1.2\n", "s = i + f\n", "print(type(i), type(f))\n", "print(type(i + f))\n", "print(s,type(s))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "- konverzió stringek és numerikus típusok között nem megengedett" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [], "source": [ " #print(\"Nekem \" + 3.1415 + \" az IQ-m\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Viszont explicit konverzióval működik:" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Nekem 3.1415 az IQ-m\n" ] } ], "source": [ "print(\"Nekem \" + str(3.1415) + \" az IQ-m\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Beépített típusok és operátorok.\n", " - Boolean (Igazság)\n", " - Numerikus (Szám)\n", " - String (Szöveg)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### boolean operátorok\n", "\n", "- három boolean operátor van: `and`, `or` and `not`" ] }, { "cell_type": "code", "execution_count": 155, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 2\n", "\n", "x < 2 and x >= 2" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x > 0 and x < 10" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not x < 0" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### boolean típus\n", "\n", "- két boolean érték: `True` és `False` (nagybetűvel kell kezdeni!!!)" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 158, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = True\n", "type(x)" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 159, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and False" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 160, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or False" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 161, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not True" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 162, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and True and False" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Numerikus típusok\n", "\n", "- három numerikus típus: `int`, `float` and `complex`\n", "- a kezdeti értéktől függ az objektum típusa" ] }, { "cell_type": "code", "execution_count": 163, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "(int, float, complex)" ] }, "execution_count": 163, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 2\n", "f = 1.2\n", "c = 1+2j\n", "\n", "type(i), type(f), type(c)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "- implicit konverzió működik köztük aritmetikai műveletek esetén\n", "- az eredmény típusa mindig a legkevesebb információ veszteséggel járó típus" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(3+2j) \n" ] } ], "source": [ "c2 = i + c\n", "print(c2, type(c2))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Tartomány\n", "\n", "- Az egészek tetszőleges méretűek lehetnek, csak a számítógép hardver oldala szab határt.\n", "- Python 2-ben más a helyzet!\n" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9990020930143845079440327643300335909804291390541816917715292738631458324642573483274873313324496504031643944455558549300187996607656176562908471354247492875198889629873671093246350427373112479265800278531241088737085605287228390164568691026850675923517914697052857644696801524832345475543250292786520806957770971741102232042976351205330777996897925116619870771785775955521720081320295204617949229259295623920965797873558158667525495797313144806249260261837941305080582686031535134178739622834990886357758062104606636372130587795322344972010808486369541401835851359858035603574021872908155566580607186461268972839794621842267579349638893357247588761959137656762411125020708704870465179396398710109200363934745618090601613377898560296863598558024761448933047052222860131377095958357319485898496404572383875170702242332633436894423297381877733153286944217936125301907868903603663283161502726139934152804071171914923903341874935394455896301292197256417717233543544751552379310892268182402452755752094704642185943862865632744231332084742221551493315002717750064228826211822549349600557457334964678483269180951895955769174509673224417740432840455882109137905375646772139976621785265057169854834562487518322383250318645505472114369934167981678170255122812978065194806295405339154657479941297499190348507544336414505631657396006693382427316434039580121280260984212247514207834712224831410304068603719640161855741656439472253464945249700314509890093162268952744428705476425472253167514521182231455388374308232642200633025137533129365164341725206256155311794738619142904761445654927128418175183531327052975495370561438239573227939673030106077456848477427832195349227983836436163764742969545906672369124136325932123335643135894465219101882123829740907916386023235450959388766736403229577993901152154448003637215069115591111996001530589107729421032230424262035693493216052927569625858445822354594645276923108197305806280326516736449343761732409753342333289730282959173569273013286423311759605230495171677033163709522256952460402143387655197644016528148022348331881097559421960476479388520198541017348985948511005469246617234143135309938405923268953586538886974427008607028635502085562029549352480050796521564919683265106744100967822951954161617717542997520009887307377876210685890770969411610438028623950445323789591870760289260393489826100774887672852918106468489143893649064784591211612193300707900537059042188012856559403699070888032966871611655961232331998310923225082866180321880439447572986762096935819784385927969250123326935194693207724335527365566248223787833888074999276831633440318604463618703789784313032843823470410944306591471928341190975185239212327674384990561563688432939039442002617530976850605132937101449086396141620556053547335569926700941375271829142407234267937565069765567475934101310225342830080409079587329544213551307302050171598424230760469209732907290141606353960880559202357376885647852240092777111489134492416995607171786298436533978180869474106751111353523711540436599310889697485658800887861974934357929246204051767246012250618404011966289872673803070498361217974484679100747846356194664829224736134115135567179291781968056053726484141128347858241259121954601184412409349782963317042002530418661694962318735860652485410222211869544223788289189712080514575141361964805369723164570564998479537657174548128597406077339158775332355215609435919275199351014222246963017013717419337504919295363295101115292951836282819191821651676455946515828048984256116748150367805267878662716999649296949377045794876146628110929982020737013330324451005385378551188803474148198665114579322684900993000236736168555294173442059925371965244997925483159343706343970371809611470323074186985035054722289027174850333368328300281132910841693150457389933183934593292994942796015309756118708918929528449074243284767006243171171622731766606796101967802204564589015899524704741001158110963633731329388356868949408759334176909387806398584647300588928175998844477486130063153068760070084837267527789777356830042778902772105683833021470279728595336332110564064263909724579949686162908019604141753935768876587992428549912151737924270343248648414247456838889541893241450987505759403013249697541696955330296880219304874163501097920036210238768275176369980977614979636096704348140124130683576879904997436596296495705459524735382000363770324894982103331332913562315169854410415317054193928234723398848453552173203688088312100943941434938282203549650281530751087098604681224802973825631244989331965296202372608586509050307993308652001231671915182765742095689513136184095412121473786311042897717861448158316965848766949554826252504961227044714712229620274682362909803877469376987358942125441792355298387479830450253909788733469732603097544156474805473732732767248652759034995336354126953900458854988683574927864615252040800490114785892289085443353996994780867471613519785838571456421583171193004117989440790268346357550339888086725127883577297626499213827436573992927302238792576924232785487201297255386071968303782483063725899808484638503828356258403917311872694381464553651690062530023217591343084755215901475299149215296944362366910833233693767993138209275870024246238331218236715236772098417187703860172308522448043176333602759733161201262248323085329288986154559221427378507410978822244729512663572225567169779409767341543017289268332635077451210167869121334465680739797372711461919299938118178827541421792926883790285430909942441260511945849237909966329550263865701114884142266162969810073652710928504579470861508094054577797864301504899958634164700528220562786008864025709432444254044034243140203812074857537999016066465520986980790589347320243050635907363821521280600041827529325485247927904235727598574209554632363830932428250711518801775633739811523761994686263270550635099851254333875594601540900862014293625673738331693082328854327001487476635118830885173775268819526360165345900556160767713453617655450974424979076063906093300028416964847594027046669468486593636425428625241644836652173922586528474244952363302305311413449332339822336551611431469131900170488226836525916399723912626616140205707996727383529597479125488961419287261259757561701592645823541151922177253919651034344793680369057003813056557866311011476313189571556336518727757991908862890765494952019474922148851417079252352394293801701149485239005844358329748769279941586384640877265901749104933238853465429979253900561311562288241147192158137210120267399648622831610430287268739840335142120299516610846193164688075944526965248570070554452152547493450434852917987512185973647190461515413582582139040172118295702327537027389787793506904044938553587650503557155872873201596885061331145477101575699375441097493374115991199114962726801718038950907803041184400075585468560976965669584325627283327416418044590727844680051360774154288412712456353383625469068936430902068216750459819321744513362913853983154560610459692604508787700304184579153478291725762810632722108035826060904572460619204237580363147200158749075361633785243462298769917887808671453928846572417223504887766803869453474588831907597355292800709241471370696647029530700507083091412492771404776193459007315206233634226128137074504162520473449597415678882003845446774388950379192344594171245510231738995030348421937088083329709108176561010708693158020695060096428352046647333361163476664106311247065173802510599409266908984046663298613648854871230659903565772327667696057187057276814394932559371368029375974604116075641599919402266794230681485723361363592903676841480358328093127506801111571615062761556607158236612268544268330274725849294875852089790850962835235527978491475563744318483993474633300330972497012808415900969455190375849945750379465019166009861502794606130794726898507849610303884846035423392175449505876157130344700415823080225786693300512126831846009510203543174323783292176865976076275412421892808138872880175813109296020150746331979561488146333412674896256883784351178477592660577212734269328382384711746083782209939646612308343952169576581065423771981899573840430315930973215059901371218399762585055435459516340055149080565627330475362528926945020226163130902420795006258931367813005222140742964756194053782182452833097021554210929638693005460011927178302761563505715735405672652524175925436371863471836292012162456662093642074605500842449347289830619506077570528754845277680661218358066130291463288932240701043887535007851997159198390084654596996197138399723495863749806582439384615049184048485819193560667125968018574877819561104335238420873417743385185735663101292757409280586840011804854994149478736882949368786637202682607198707656286436753775709560349718397405565505269425218354301348910785234517795519757516484711545928466003754558485470994737493796615841040414239875763335201795518644856632201598556341934286668912522153446348791218159622744525372314219184738770596659942181275403613660438538829201810204850917717791485256026242529802492309229562177062770027659288158473994804255067730903420043491632913588644627415318468517462580180901314477358637486528221274450661883667873545037139535563260349778209992416559111602097437491432360787879331015052417047437823553506205617017572175387061751192919715660363028302343819584946594328460482931960515124867123604625653903565173322856758210937541222674223847046664733620292824834065137814475367747671882220098389682019784216724015491253360436437847479770633657905418133523010804559958547379685864708937791659340223795537045273849435441105983887969741143051069401271065628507537039823308867819868298171415185218271493613110963984021912448323423901392553811725954153209435002954807640291982765741514042956666953177304003358701503703497424897898108939453026976878231557938158928996868766367603579055322794822757659104812835219745724022347569914650240636730492833286151875049129873457930874999488048681250802904606446223569562767964898914869924201946458521355165709887118378290437174375625282606140534611987395334677500936625746765638459629521872262777473480491233965194281353725068660782076683862565487279038020486778099991754380815789820825255566234983933217491493864966284116889874665005414748264599972752003370084542592544301190399041231752771993767799847551279448012913842034323154888137932524887172099381195722163148101670274877379161830968937348720168944903299658932511996504109653674618914861599481632040891930577238630396311858213341337110096389113836596895914715370925073998461682046426447290788976525593505136546978364603183820619560578517561504972661817649030304982138534738696212234626114043035600967042547012317360449724623287452575151198771801585742829389025650825988275495110865424704218337264023078045681651420517807418196096401513461760794362769612228126118610912766814880500950963889032877710837651051900076128058473969258768737937306664751387942217354694021157675557568970168734104342446525522568974329716152742558110503495045718931752447070410307760830365536714180388723602948872805590752711115590794756926903978519601939790311768070356801944936106850640568519290645048685535628256787225734544146565541187816717729850612874044620890718502108518025052924590359814117522720320552642597751984410742492179242039080014606225999422109717176118746845802673724801365603866909971071347255859723217027554055085082090418987534829222004178998475030519537179062001509333023023881806519182405550818672164711702307529922652228033820404113386625335815042934115143980939986416365633923620673874259342713444701242702722227197573203194489407856355511639619115985907995399083680129468810771595938084908111251938016414866250141095286680914828503123938960997659175977315432797173945762560365023587931559926170852315074247849814256564693008105061976397395591733545472917526739598790117477449217745771908169489543790314578152667389689410604588351445026130645637237687631129964576699475767340673583537218704935177320214779403972566532581731659202199752942824432778102107532160580104432121067208273876100778332426569624765621063126975491546224343978061253999313893915782008560011171319773443130412998215626985098895722778159524505640435534979855872234521991778419564106622122049039017886737997905270552302412002780864726282517525092332555378737792434915926182736151242592242725872699844014132567954640475742451126102857394193479716383187138370722782422619384021010896271281685732287764210298708895557148397743497418109849633633910397778254225179400022143485886207532122646613614487518735142494469575836744785028013193033901949738716163113800864093408529297729741462836142201120573027427309566658849884965134295188287937016147499504685185116851709758146686994243673140036992381232483920618628663037402351333907771907455224248515748726136075048020960976567862025232356273025557054386729189255571672396871991669651834736987440295942395220863481341484029838939485593327256627318194137735451891544384960966451285561476772564932516923822003229733483331726306200059192067441136913243720403578098676440894672367334549903905283682411422011886949265324531399323764100563996744273693606586851243936737155349696358970470706246743064681511525580772367129358691546677179280674603197443660235664811911175324239792271603213932690900431433572511935591785787438931910836722219749594385217682817205537433987939372420698422586002026702204502341484432231418159837133223467338185995720215212999066855318593765188122815644349842897804044474253173187198249880219056124756835053121936684091113613451740380400041281474527453687530122741226292549431008458868082627169312891414237466973215688306962596297824286828479758198382340345222708757345393897126910174835179971320922911746435455023926629385455742630840473245989374307836875864087914312319228307304975362481708579849870550314357368660453504099679685389802578477910419231052505144696885248726344365369453921372194715998156624454330562798282777349584666757018969900985110164491047993602922961645132995224271404908186948080818144765441476070834943179921347693205775493727581146799477819807044909830887802418354528095576268178221596410712216509995196961932368766959924336939018675830477951348564967496643006978548120861013029026482640402316621018258604172734501970838570222765188590393123627245232006535983907604127898007628220791246163222713051906678029644273228763460060289213316094057396003523608149487855784819923604033709243920895785100953666245538047118419834068589948934026518132741951749125192568729824877982938351710224389886834755562218214866075518628420508441240855088408499405226548535776678864444680504301285988469915501692621173049550268365498340637943341584797390666973271817567111123445212701138287733170703648843560692535012857745637166012927846194135756491508269323010063559087991831480179606664893606864875569154700860602703909640090514609360513037280984989976472934205971076104365706670369636731086616773064361333184164332104073479210775688703754193241101764488071673304176268034528127946729244452919161882095335945667158450941476301537032588109254921620602423998383939639570864268315557737923542830477169053850721939927266830567227442913752680237178175908437269780708099690192695002592421020485193953805155158663266418230452129371046840218806665165231438597498081621420148051355131865453014871298249938624272154345372391802215126115854861975398983110888196358875565793593310597895992053240439684590862193232015232257668969509415398393721308747247732944493053757577694362680328316055060353219685001207195191671426063364056179006218671604638684249067247572855764071631397824639188693789635247708232195940400450829593853651517642510129335911478337995658501766175785429966837417247838838645892319920041196390925354226074199412181950196251598137076467085022479201649174056794994024967593003129957099742026595785390010668925864718839026058417582396497109522944729874183273952292247592156943695682037477731049514128818431210317476600507328613045698141876738705835589680568812062901672389707603039549708273418484069037215179862266581929555420755695654139976814235027490594662592770986044588793675562143709648705744665319814772892177290937799834952769995145061157204394128687121538756842568036622321369508041915737469009917048039885947260486258684449762362318724085339500204989295963618739437408918888568903691128987832325892601331155260901131911962536529384643993465666490271108522834470184768391573623895239732781158399355178022076177450755929138381468526577309152290502509145241134006895661073761320484545645543610238773087594308408699782711315425513472058939339453013548135244176754131385012770024156261936169649709944156933541666590935759784775946468953923516229723673403650805886186209888123677098892944769654360324668874429740130365009770291603536426502844618046128137324006034947889707723262364927515545013731797277615272236008596598736693841467685030166103516988390995570659331713219574003251046303604196295276316972893990945091279545361627593486818455830168634280653205028729105026906643726170661778439311154737149759096116144493812106746037251452616271615212935043141124155849847574091878458410177325030553207237661933970254488335435069113577757765584551136714634629223114497809546405079026755854160954260557783406467356189398503160303046687522579708387993298769182986822970667356246647939653365454544268386404322650296028881298557242198187208040207296077496128886166773288838620609977925416822156713703667391333322086520871904473048593017466793578173830832406459585018177267447690376523178740142636502172093102619263046542112708094769682392207383797482057034793196525139221370199877543931178601238198924707047629587501516299912892933069025343575488251454948469328322898202956506903308470482517744131166464774750723477861518055343466858040987132273453081836794564364430155246274158100731069323080734901590517008753654169600998911569598454702926629374049393677685285723039754691181021469267219333846563053673846537824970049506209403506606244584412627902384242785280551599253188262786271531989567165398385428909311577122438618119724985644233907558812574859217687149721341833317289714977694982117227337669878361212788465289349470676995146785835955074121568375032937822352378665103266923449887690619794116574936418347914823983988002067649967289214983486424468737742286438563064425223461210639952418146166111391586408089740413945879849036682695488197808766021959964846771145698159367567904768112047055740602351237499012258567357965107013251562237843754994747236883821060645969480067316032381351192535148322216761442411144695361182838761115795911310362420741510826322048374540404146990479647073113428368300158247350712922331119789013179701651764033036649606749518628656836032556353205617386602749327417799524243899880581322620681297935615821202736455540414428705857131835238951742471239050758919353490570268302812810704750603181709274434806504843422134152201579626776393642698168851485545206897802036257496043584180735523092410264794436423951474855853980259367017205588767985309971043290404884467541701918912275745752402121744890326681160647837985004639864193645792082105266057503921195311457592621400830660301768120178560328650220171413811642155986702637531155554821958388905393544592180587272433749899242041542816666821846374863337522729077994208960545264863862516411718049112903451378140021506899966762080732651380427792460930762263446994159193223142565029996403134383564087766689537759857473403120477091886596355579944643688205981242040477802943803932459722207541739589596692034052161680837588921636048584789929663903107349945667027830610483130862872680661015300745521669893857383782690975026078691245077096697355744971063878379083608563324774011426604588797712843335089341752120843133549155805945217503766728136818989701075795034975931147113851177529084708382646930943663837851488798723688771534770333456570254004964039987760745951276145936694695508877796318400093229973736770518512573233230127134210937068680950264362225510872877141635601716276267040168996816287213326422268377855327887822310204539762272583014902609710931194995685950834598509258803363019551891846212990914920155838298607568190737375958573348261713794703592503972126314316192159571451081744034114847904386601284244643362857392442316767411036417557776991886719910511456902002905771699177497748359489810115650557253501242519595931483440417116222205504589671636754478042893488385500275211497526246421122010774862333476246182576597178403980436617669560567049936789350370893174617560282074679058110806280348893136768773133234721178541038289213080580369302853910384704528018344058313252744129928355732704028220901951560054449764635932065083760864473989692709381120504251266930834839999956860216356148940167442564078469350152730245749435722205009403680810321136898946443062041659878360406811331217133504960504944165472883926378607868385731515366373416312566122603235977742473940324793478740770352725436532455308438250865693864330020957120719122826689237609273524467576647981797622831709248187143189474449928329051087066866263901462754600186683583622062445553828867143621415773638244852570664653423002003386087185683387095282564231892337305827467825102905239214464117590700082862874915647212962223100149842773910585262712095702633425139069275541249925826898755913048489059083665047386569005980726512682643809800080768716889749767005747228285165430935469423701296901225200041775975278099341399149455675655415791289014141807180970818010668626000334746783492116354330741766556197865412944331579610856733907210970815247694360208231077377319255935591919361509992241851869762511821971598449396422923222586939583205849537600173388242916813257003198490942805178528782842560021549233235032941872842601109623432061485547579038802849228139397130979180562447703494299530585909415031237400213445431607049538809117870571045686211621069655023028886617060314580222884344906879404890744067164557565160596397488164744575409400891911083945126233990453364772541741331330821153758983077390402549739751620825837898384996200874749622331436354553599710472896396262914675542281735624665804212007275401273982269651802596130334329792865330063654175676003369688243926391280713123038760076039957285658333390838592534249337153070457471482743551037417147024006867419141874398230449202772009682282474640802779068104741602882613440803256607530230172031981070796893051954145239327661783905828943932146148894821695918579498502263858318704473290299790473193753803543920285706350672484245008920689712995308213747335255792982565803469779504974053329506220584586976782376547358092670753891736652401577525890202354883227480678961328466821865972869785592672826822656457577958670571097728396605101516387163615321918203652729897586807786443044896628670393188486921931839182927172554503767733076856308657600665342622254535057629030862718937364327448083558928220102374951534665262857504222950995446014428801859061443379679142717413278286975998259993819084477469775220567898395378570154609157695006128689610825519373486612568225010585752375050450503641627804406676905130564807191014132285063482665726828511933059433717373899623654715534816591630086838520905491060194596758223701911513581142071521220673521948777069521608531717477580542554138038978873716058241771904252401056370661792996395808706717083872950238057238547610495061732689587948240319569499000594593719849004104550034415014066154526994609094441391439071866309009249691812468813088126242358787204856089608234037677437509496768451058059579977189006605354903359961784245121096144877063187466910292356627771165203522647016166296488083808997887687769308220856530068153940856748041349469491870704992660210066455819832886130678021460369893174499939107231230405088342485383796742208847060385334943928147519685791292174441917386398739155908915859310750449419969425856920503891277550092560029225566028245733054164370482965351532695757311986033153861767127072322962670122480612322195488891848040149357757131302308897501348573600357134307046628157178720021578705983302856401851403674168523579202306440644159448586566246905477964086572462611834063213025126230896949713642334585419351128373132157232101577687884147986752364441711298472354763142715850293434746434127230225735792097083439910498394019789979870351837786010778667226102241684498686398874227580631272581474396676719404444290071284914286251867085092358453277091441099575357659812178523756848934429670368686181229928033433456696493460770903592231804072849827640955873854491455416480621098619090449331761288966135325413062313991971462332768487224184119088841054819270878948939590681778579588918587881426099241767753862382406002451821149035160124492893940578494055432173589779503867792448614769448734671863583817296627073341404027631029821562303142157743379294616649374361560459036060298581224576085252700570061877609582745974967405315882162695982851143006320897730473133461172142945805407178876223011462558510520930761771493278603541262719880522826864107055049364370555816945830840650960447321868266858216903784868032280479864021706315653687489290467679015864709379478762373013822026373788822827564163725872670921602434077279298565679825234946428376162918717556993454693724759744644374586123577768842647346924365909558553162571564807969034052671504760754742676960038255814034354716520834598864538432593908154392918939089896109083521992519053530398359081516289247571043354786424954980741388745386023113329591390636006694539437438690759589121288758007804082911975207095278133335550232776761783391381794105109412999680430103899475051243232331431120663598726618610084717254696908726637456858466646162093223427680684601718516702427671125913080059583053658180035991214447685286534985397013876080817020918472863351368783745251292936294373137797794168163130983688833624592488270042533275310174037401858155192663147588032450316349828944090941459435709839331454077937638533884824056964583661906580362312059760037127664830995125859310214290237306052000883415749724405028863402500028747055233688541068593389039662379913776485715772093749681055718277781429659244251666703110135820625316518906936462663613376783677001574909561444962828205690820330013121289873007855206280915035880334071746233511142838096550651818271177825964147342275885803162473648253508879276299074813242692461714169521068548818275361612786955216412383263246164034602771053052217127416909831714425842754359325193708367920524870186212708574531518197079797624156322369627530303625010057401503185241771381716916959066550480494267473696656051819527729740440782487416353087074803447126307550453018768734511631260002571765708294113447159058326859464022975489061868569715529325333679192904509181338989039531226114077604146341395120253199068720690450654317360799352434791082039695478061926453254644053124586362026016156957888326419910345519741792140396061817459467332211367113051675844970722080638249309285348257747082754939860608508486386549912912220391895379332321814344998458680557267915553525212113907723389037979427485209309504641599639196053296030292739274663906234190084882219800040754390250718430168480866157009022053158273857417267902310193566064243660834080202786392263652453395504706244108957791049298908397097715806857036527872932846771952274875201321856644284239116502005786314083266606564469023548865452436995428173054383481532525156311603438941440612599662264959878769003712412406305072713049361060777519351766684350979095112470377728527045181653556285621656761979382960837252084759203181114384404005114613990326273362146150667851880079327563486444803726963278475818453468509565365380723807736793526242145533088833679727074192838872685484977387306670007551965761431583941102042492623133242770270821710047137152167948872468741189259805623910487138975034203376673344989060052111727844670530521327425332189734978888307943626099125569741350839434061750712876445222039557455150019442996232739317091487905148812821582894579949748623433452865460274945133477267345362799684706610937707688048379567860499887327287273158785195505216946986571759372875984514994087366735699690322212619073372131984972873714823922056361158276719293687517950957314775347729780073870450969204201405641649670458849057466793682155521901095027966023590200629563426621697556166261956721403439803621918243177242110484823901622121433246394511832190737036276362556947535372619436126996645342229589006736952014870393865015087074414735220220770411070088328030731545041085721762448600324574529642445801550887535581445128918173686346116567111050151109714760544361537920207546115073529167935359803746233158035402507293585422134950239655004818456256327794930441926282941363007224150758144477688955569616025741423859274415740477217643474948663183182648210083785749080222199122144694117400270856159095104885845967392332602301843740328702808569782311182460392642129024729192535226787567897868564600570545108945374190649371558894253329703486982693386634574807801078719254129784907155276683405362407228975018016417486695226639468897799829747034414846605232977531914266638473906810541671168066131262918834764800639654762345192540580400257727950859061817326000188356677576249635084512266410557551645134470262083485480776173701923626406384831371842087477223094467978661221649689842322789248762605167898686475739012260361067924661728390016267344059903224026536343550301552983264519865881303942586617734987280231892544278016113318354276103258002393942607858756949485119300715853929843163967575531352484221274814864027297307155457109423432471856970523635672286551516265672389248080659279448953078708153606666741314598260798811278754656313007785743484189610809499119436037561075326359414143476882494692530787790693275290895909945065272071979419682741840819830567497688086236460819345623637702467815073133466197923119939117195979352833117496820500577299938566476984260815518940866776320855483159836188850448339911351505132909703990088919063133375804094886955895465912648104534621430295733375992898959329664444686900648674531661878821297194146191914132978673375283702995190706179924178140249356285006536273771636355569720003246996130665282451695167273161831393867558791278264933758040011059211372270839123518459709107480669852696080909181872978501574064544548264471078633386599113188810137746318984496745989011540334259315347740082110734818471521253374507271538131048296612979081477663269791394570357390537422769779633811568396223208402597025155304734389883109376" ] }, "execution_count": 165, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**100000" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 166, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(2**63 + 1)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## float\n", "\n", "- floatok általában a C double típusán alapszanak, így a precizitásuk véges\n", "- komplex számok pedig két floatot használnak.\n", "- pontos információt a `sys.float_info` parancs segítségével kapunk" ] }, { "cell_type": "code", "execution_count": 167, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/plain": [ "sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sys\n", "sys.float_info" ] }, { "cell_type": "code", "execution_count": 168, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "sys.int_info(bits_per_digit=30, sizeof_digit=4)" ] }, "execution_count": 168, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.int_info" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Aritmetikai operátorok\n", "\n", "- összadás, kivonás, szorzás a szokásos módon működik" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6.2 \n", "(0.10000000000000053+3j) \n", "(8.2-6j) \n" ] } ], "source": [ "i = 2\n", "f = 4.2\n", "c = 4.1-3j\n", "\n", "s1 = i + f\n", "s2 = f - c\n", "s3 = i * c\n", "print(s1, type(s1))\n", "print(s2, type(s2))\n", "print(s3, type(s3))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Osztás vs benfoglalás\n", "- `/` törtet is adhat\n", "- `//` a hányados egész részét adja vissza\n", "- Python 2-ben más!\n" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 / 2" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-2.0, 1)" ] }, "execution_count": 171, "metadata": {}, "output_type": "execute_result" } ], "source": [ "-3.0 // 2, 3 // 2 " ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5.0, 5.0, 4.0, 4.0)" ] }, "execution_count": 172, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 / 0.8, 4.0 / 0.8, 4 // 0.8, 4.0 // 0.8 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mi a megoldás a ilyesmi problémák elkerülésére?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Összehasnolító operátorok" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(True, True)" ] }, "execution_count": 173, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 23\n", "x < 24, x >= 22" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Össze is lehet őket láncolni" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 174, "metadata": {}, "output_type": "execute_result" } ], "source": [ "23 < x < 100" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 175, "metadata": {}, "output_type": "execute_result" } ], "source": [ "23 <= x < 100" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Egyéb operátorok\n", "\n", "#### Maradék" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 176, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 % 3" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "#### Hatványozás" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 177, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ** 3" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.4142135623730951" ] }, "execution_count": 178, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ** 0.5" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#### Abszolút érték" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2.23606797749979, 1.4142135623730951)" ] }, "execution_count": 179, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(-2 - 1j), abs(1+1j)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "#### Kerekítés" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 2.35, 4)" ] }, "execution_count": 180, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(2.3456), round(2.3456, 2), round(3.5)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "#### Explicit konverzió" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(2)" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-2, 2)" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 0 felé kerekít\n", "int(-2.7), int(2.7)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### A `math` és `cmath` modulok még sok mást tartalmaznak\n" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2.772588722239781, 4.0, 7.38905609893065, 10.000000000000002)" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "\n", "math.log(16), math.log(16, 2), math.exp(2), math.exp(math.log(10))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Mutable vs. immutable típusok\n", "\n", "- mutable típusba tartozó objektumok helyben változtathatóak\n", "- immutable objektumok egész életükben egyetlen értékkel rendelkeznek\n", "- minden numerikus típusok immutable " ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [], "source": [ "\"asd\"+\"asd\"\n", "a=2\n", "a=3" ] }, { "cell_type": "code", "execution_count": 185, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "x = 2\n", "old_id = id(x)\n", "x = x + 1\n", "print(id(x) == old_id)" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-10\n", "-9\n", "-8\n", "-7\n", "-6\n", "257\n", "258\n", "259\n" ] } ], "source": [ "for i in range(-10, 260):\n", " x = i\n", " y = i + 1 - 1\n", " if x is not y:\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Mutable? bool értékek\n", "\n", "Egyedi immutable objektumok, csak egy példány van belőlük." ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n" ] } ], "source": [ "x = True\n", "y = False\n", "print(x is y)\n", "x = False\n", "print(x is y)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Mutable? listák" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 188, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 = [0, 1]\n", "old_id = id(l1)\n", "l1.append(2)\n", "old_id == id(l1)\n" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 189, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2] is [1,2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A listák helyben megváltoztathatóak" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Sorozat típusok\n", "\n", "- minden lista/sorozat szerű típus támogatja a következő függvényeket\n", "\n", "| operation | behaviour |\n", "| :----- | :----- |\n", "| `x in s` | \tTrue ha s egyenlő x valamelyik elemével, különben False|\n", "| `x not in s` | \tFlase ha s egyenlő x valamelyik elemével, különben True |\n", "| `s + t` | \ts és t összefűzése |\n", "| `s * n or n * s` | \tekvivalens azzal, hogy összeadjuk s-nek n darab példányát |\n", "| `s[i]` | \ts-nek az i. eleme, 0-tól kezdve |\n", "| `s[i:j]` | \t s-nek az i-től j-ig tartó szelete |\n", "| `s[i:j:k]` | \t s-nek az i-től j-ig tartó szelete k lépésközzel |\n", "| `len(s)` | \ts hossza |\n", "| `min(s)` | \t s legkisebb eleme |\n", "| `max(s)` | \t s legnagyobb eleme |\n", "| `s.index(x[, i[, j]])` | \ts első előfordulásának indexe x-ben (az i. indextől a j.-ig tartó részen) |\n", "| `s.count(x)` | \ts x-beli megjelenéseinek száma |\n", "\n", "[Table source](https://docs.python.org/3/library/stdtypes.html#common-sequence-operations)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### list\n", "\n", "- mutable sorozat típus " ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 2, 3]" ] }, "execution_count": 190, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1, 2, 2, 3]\n", "l" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[1]" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [], "source": [ " # l[4] # raises IndexError" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 3)" ] }, "execution_count": 193, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[-1], l[len(l)-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### append, insert, del" ] }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 194, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1, 2, 3]\n", "l.append(3)\n", "l.clear()\n", "l" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 5, 2, 3]" ] }, "execution_count": 195, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1, 2, 3]\n", "l.insert(1, 5)\n", "l" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 1]" ] }, "execution_count": 196, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l= [1, 2, 5, 1]\n", "del l[2]\n", "l" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Indexelés, range-k" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]" ] }, "execution_count": 197, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = []\n", "for i in range(20):\n", " l.append(2*i + 1)\n", "l[2:5]\n", "l[:10]\n" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[33, 35, 37, 39]" ] }, "execution_count": 198, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[-4:]" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 3, 5]\n", "[3, 5, 7]\n", "[5, 7, 9]\n", "[7, 9, 11]\n", "[9, 11, 13]\n", "[11, 13, 15]\n", "[13, 15, 17]\n", "[15, 17, 19]\n", "[17, 19, 21]\n", "[19, 21, 23]\n" ] } ], "source": [ "for i in range(10):\n", " print(l[i:i+3])" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 11, 17]" ] }, "execution_count": 200, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[2:11:3] " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Vajon mi az eredmény?" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[39, 37, 35, 33, 31, 29, 27, 25, 23, 21, 19, 17, 15, 13, 11, 9, 7, 5, 3, 1]" ] }, "execution_count": 201, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[::-1]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "A lista mutable, elemek megváltoztathatóak:" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n" ] }, { "data": { "text/plain": [ "[1, 2]" ] }, "execution_count": 202, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = []\n", "old_id = id(l)\n", "\n", "for element in range(1, 3):\n", " l.append(element)\n", " print(id(l) == old_id)\n", "l" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [], "source": [ "l[1] = 12" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5, [1, 2, 3, 4, 5])" ] }, "execution_count": 204, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1, 2]\n", "l.extend([3, 4, 5])\n", "len(l), l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Az = operáció egy referenciát állít be\n", "\n", "- nem jön létre új objektum" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] }, { "data": { "text/plain": [ "[1, 2]" ] }, "execution_count": 205, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l=[1,2]\n", "l2 = l.copy()\n", "print(l is l2)\n", "\n", "l2.append(42)\n", "l" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'alma'" ] }, "execution_count": 206, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=\"alma\"\n", "b=a\n", "b=b+\"alma\"\n", "a" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### az elemeknek nem kell azonos típusunak lennie " ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, -1, 'foo', 2, 'bar']" ] }, "execution_count": 207, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1, -1, \"foo\", 2, \"bar\"]\n", "l" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### listákon végigmehetünk for ciklussal" ] }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "-1\n", "foo\n", "2\n", "bar\n" ] } ], "source": [ "for element in l:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### enumerate\n", "\n", "ha szükségünk van az indexre is, a beépített `enumerate` függvény index-elem párokon fut végig" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 asd\n", "1 1\n", "2 2\n" ] } ], "source": [ "l=[\"asd\",1,2]\n", "for i, element in enumerate(l):\n", " print(i, element)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Lista rendezés\n", "\n", "- rendezni lehet a beépített `sorted` függvény segítségével" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-1\n", "2\n", "3\n", "11\n" ] } ], "source": [ "l = [3, -1, 2, 11]\n", "\n", "for e in sorted(l):\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "A `key` argumentummal megadhatjuk, hogy mi szerint rendezzünk" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 5]\n", "['milk', 1]\n", "['bread', 3]\n", "['pear', 2, 2]\n" ] } ], "source": [ "shopping_list = [\n", " [\"apple\", 5],\n", " [\"pear\", 2,2],\n", " [\"milk\", 1],\n", " [\"bread\", 3],\n", "]\n", "\n", "for product in sorted(shopping_list, key=lambda x: len(x)):\n", " print(product)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Helyben is lehet rendezni a .sort() metódussal." ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-1, 2, 3, 11]" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l=[3, -1, 2, 11]\n", "l.sort()\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A * operátor\n", " A `*` operátor segítségével \"kibonthatunk\" egy listát. A `*l` kifejezés felsorolja az `l` lista elemeit de már nem egy lista objektum. Ez tipikusan akkor hasznos, ha egy függvény sok paramétert vár. " ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['alma', 'körte', 'piskóta']\n", "alma körte piskóta\n" ] } ], "source": [ "l=[\"alma\",\"körte\",\"piskóta\"]\n", "print(l)\n", "print(*l)" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "60" ] }, "execution_count": 214, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def osszeg(x,y,z):\n", " return x+y+z\n", "osszeg(*[10,20,30])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Függvények újra. `args` és `kwargs`\n", "\n", "- mind a pozíciós és a kulcsszavas argumentumok is összegyűjthetőek a `*` és `**` operátorokkal\n", "- a pozíciós argumentumok tuplebe kerülnek" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "1\n", "2\n", "-1\n" ] } ], "source": [ "def arbitrary_positional_f(*args):\n", " print(type(args))\n", " for arg in args:\n", " print(arg)\n", " \n", "arbitrary_positional_f(1, 2, -1)\n", "# arbitrary_positional_f(1, 2, arg=-1) # raises TypeError" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Lambda kifejezések\n", "\n", "- névtelen függvények\n", "- lehet paramétere\n", "- nem fér hozzá semmihez" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l = [-1, 0, -10, 2, 3]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Rendezzünk például abszolút érték szerint. A beépített `sorted` függvény nem elég:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for e in sorted(l):\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "De megadhatjuk, hogy milyen `key` kulcsot használjon:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for e in sorted(l, key=lambda x: abs(x)):\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Bármilyen függvényt használhatunk" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "for e in sorted(l, key=abs):\n", " print(e)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }