{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "anaconda-cloud": {}, "hide_input": false, "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" }, "latex_envs": { "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 0 }, "name": "python-bevezeto.ipynb", "colab": { "name": "kl_py_okt_01.ipynb", "provenance": [], "collapsed_sections": [ "cdvwEF0EtY7I", "qpYnxKh_tY7c", "8ZEQeiGwtY8D", "1vORS_zItY9b", "eZaWbkNFtY99", "JFBe4mFJtY-L" ], "include_colab_link": true } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "1Q37xkBGtY6N", "colab_type": "text" }, "source": [ "

\n", " \n", " \n", "

\n", "\n", "\n", "

\n", "\n", "\n", "\n", "# Python alapok 1\n", "\n", "\n", "\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": { "id": "Ou5oaEXJtY6P", "colab_type": "text" }, "source": [ "# Első lépések Pythonban, ismerkedés a változókkal\n", "Ebben a notebookban rövid példákon keresztül megismerkedünk a Python nyelv szintaxisával, és néhány alapvető adattípussal és adatstruktúrával. Később ezeket és az ezekből származtatott adatstruktúrákat fogjuk lépten-nyomon használni.\n", "\n", "\n", "## Megjegyzések (Comment-ek)\n", "A Pythonban a megjegyzések kettőskereszttel ('#') kezdődnek, és a sor végéig tartanak. Egy megjegyzés lehet sor elején, vagy követhet szóközt, tabulátor karaktert, de ha egy karakterlánc (string) belsejébe teszed, az nem lesz megjegyzés (lásd a példában!). A kettőskereszt karakter egy karakterláncon belül csak egy kettőskeresztet jelent." ] }, { "cell_type": "code", "metadata": { "id": "Hxn8UjixtY6P", "colab_type": "code", "colab": {} }, "source": [ "# ez az első megjegyzés\n", "SPAM = 1 # ez a második megjegyzés\n", " # ... és ez a harmadik.\n", "STRING = \"# Ez nem megjegyzés, mert idézőjelekben van.\"" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "o5uP9xi6tY6S", "colab_type": "text" }, "source": [ "A komment csak a programozónak szól emlékeztetőül, vagy más programozók megértését segíti. Noha gyakran elmarad, a kódok rendszeres dokumentálása megjegyzésekkel később (amikor már a kód részleteit a feledés homálya borítja) nagyon kifizetődő lehet.\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": { "id": "FduA_abqtY6T", "colab_type": "text" }, "source": [ "## A Python használata számológépként\n", "\n", "A notebookot lehet úgy használni, mint egy sima számológépet: be lehet írni egy kifejezést, és az kiszámolja az értékét. A kifejezések nyelvtana a szokásos: a +, -, * és / műveletek ugyanúgy működnek, mint a legtöbb nyelvben (például Fortran vagy C). A zárójeleket értelemszerűen az elvégzendő műveletek csoportosítására, a műveleti sorrend meghatározására használjuk. Például:" ] }, { "cell_type": "code", "metadata": { "id": "8vkfgGgXtY6T", "colab_type": "code", "colab": {}, "outputId": "8d53549f-aafe-4a8a-c696-307b9e221997" }, "source": [ "2 + 2" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "4" ] }, "metadata": { "tags": [] }, "execution_count": 2 } ] }, { "cell_type": "code", "metadata": { "id": "d_nVHxw8tY6W", "colab_type": "code", "colab": {}, "outputId": "22da73fd-1986-46bc-e4f1-86f07b8bd401" }, "source": [ "50 - 5*6" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "20" ] }, "metadata": { "tags": [] }, "execution_count": 3 } ] }, { "cell_type": "code", "metadata": { "id": "7u5jvHLQtY6Y", "colab_type": "code", "colab": {}, "outputId": "39f2c62d-0594-4554-d8c1-7600471aeecb" }, "source": [ "(50 - 5*6) / 4" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "5.0" ] }, "metadata": { "tags": [] }, "execution_count": 4 } ] }, { "cell_type": "code", "metadata": { "id": "Ta6PmlbFtY6c", "colab_type": "code", "colab": {}, "outputId": "36ecb40c-63b9-4aaf-df26-a5ecbd4e21ba" }, "source": [ "8 / 5 # az osztás egész számok esetén is lebegőpontos eredményt ad. " ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1.6" ] }, "metadata": { "tags": [] }, "execution_count": 5 } ] }, { "cell_type": "markdown", "metadata": { "id": "0inePIqGtY6e", "colab_type": "text" }, "source": [ "Az egész számok (pl. 2, 4, 20) alkotják az **int** típust, a valós számok (pl 5.0, 1.6) pedig a **float** típust. \n", "A Python 3-ban az osztás (/) mindig lebegőpontos értékeket ad vissza. Lefelé kerekítő egészosztás elvégzéséhez, amellyel egész értéket kapunk, a // operátor használható; az osztás maradékához a %-t hívhatjuk segítségül:" ] }, { "cell_type": "code", "metadata": { "id": "II4NphO4tY6f", "colab_type": "code", "colab": {}, "outputId": "2d8cff40-747d-4918-de74-6887ab8c8ce2" }, "source": [ "17 / 3 # hagyományos osztás lebegőpontos eredménnyel" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "5.666666666666667" ] }, "metadata": { "tags": [] }, "execution_count": 6 } ] }, { "cell_type": "code", "metadata": { "id": "gJABaUfetY6h", "colab_type": "code", "colab": {}, "outputId": "9b2b5a0b-07e5-4add-91b1-d60df1faa88a" }, "source": [ "17 // 3 # az egészosztással megszabadulunk a törtrésztől" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "5" ] }, "metadata": { "tags": [] }, "execution_count": 7 } ] }, { "cell_type": "code", "metadata": { "id": "su1K2SWhtY6j", "colab_type": "code", "colab": {}, "outputId": "9ab08122-811a-4e8a-803b-5da6ac68482f" }, "source": [ "17 % 3 # a % operátor az osztás maradékával tér vissza" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2" ] }, "metadata": { "tags": [] }, "execution_count": 8 } ] }, { "cell_type": "code", "metadata": { "id": "JZNgqRjItY6l", "colab_type": "code", "colab": {}, "outputId": "a2e79753-5656-4cfe-ddb4-2aa6a85cbce6" }, "source": [ "5 * 3 + 2 # eredmény * osztó + maradék" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "17" ] }, "metadata": { "tags": [] }, "execution_count": 9 } ] }, { "cell_type": "markdown", "metadata": { "id": "ClVIsDgLtY6n", "colab_type": "text" }, "source": [ "A Pythonban a ** operátor használható a hatvány kiszámítására:" ] }, { "cell_type": "code", "metadata": { "id": "NhO9QflwtY6o", "colab_type": "code", "colab": {}, "outputId": "16ec2034-6c31-41ea-a7fc-cc833549d27f" }, "source": [ "5 ** 2 # 5 négyzete" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "25" ] }, "metadata": { "tags": [] }, "execution_count": 10 } ] }, { "cell_type": "code", "metadata": { "id": "wu1R2Q2utY6q", "colab_type": "code", "colab": {}, "outputId": "f89e017e-52f7-4838-94cc-28b1728c4c38" }, "source": [ "2 ** 7 # 2 7-dik hatványa" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "128" ] }, "metadata": { "tags": [] }, "execution_count": 11 } ] }, { "cell_type": "markdown", "metadata": { "id": "UAFbpACMtY6t", "colab_type": "text" }, "source": [ "A `Python` a lebegőpontos számokat bizonyos tekintetben előnyben részesíti az egész számokhoz képest. Azok a műveletek, amelyeknél keverednek a típusok, az egészeket lebegőpontossá alakítják:" ] }, { "cell_type": "code", "metadata": { "id": "kows_uXutY6t", "colab_type": "code", "colab": {}, "outputId": "1404153b-ac65-49db-d51e-45688c2fa4ea" }, "source": [ "3 * 3.75 / 1.5" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "7.5" ] }, "metadata": { "tags": [] }, "execution_count": 12 } ] }, { "cell_type": "code", "metadata": { "id": "_cW2L0GFtY6v", "colab_type": "code", "colab": {}, "outputId": "c726c5de-23a8-4967-dfd2-840ce1a01f74" }, "source": [ "7.0 / 2" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "3.5" ] }, "metadata": { "tags": [] }, "execution_count": 13 } ] }, { "cell_type": "markdown", "metadata": { "id": "YWOhNpzMtY6x", "colab_type": "text" }, "source": [ "Előfordul néha, hogy egyes függvények `int` típusú változót, várnak és ezt egy `float` típusúból kell előállítanunk. A típusok közti konverziót az int() és float() függvények segítségével tehetjük meg." ] }, { "cell_type": "code", "metadata": { "id": "nUH0TKPRtY6x", "colab_type": "code", "colab": {}, "outputId": "971b5f05-c77f-4e07-ef20-636971e8b3d9" }, "source": [ "int(3.1415)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "3" ] }, "metadata": { "tags": [] }, "execution_count": 14 } ] }, { "cell_type": "code", "metadata": { "id": "XBVGug_CtY6z", "colab_type": "code", "colab": {}, "outputId": "db62342e-147b-4916-9055-812516d52953" }, "source": [ "float(42)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "42.0" ] }, "metadata": { "tags": [] }, "execution_count": 15 } ] }, { "cell_type": "markdown", "metadata": { "id": "FK20z58wtY61", "colab_type": "text" }, "source": [ "A `Python` komplex számokat is tud kezelni – a képzetes részt a j vagy J jellel képezhetjük. A komplex számot (valós+képzetes j) formában írhatjuk:" ] }, { "cell_type": "code", "metadata": { "id": "aCOf1I7CtY61", "colab_type": "code", "colab": {}, "outputId": "d0c213cc-5614-4391-e854-8065935b11f5" }, "source": [ "1j * 1J" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(-1+0j)" ] }, "metadata": { "tags": [] }, "execution_count": 16 } ] }, { "cell_type": "code", "metadata": { "id": "ZU5vLZRVtY63", "colab_type": "code", "colab": {}, "outputId": "fc860dbd-5262-4735-9a65-b397c2464f78" }, "source": [ "3+1j*3" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(3+3j)" ] }, "metadata": { "tags": [] }, "execution_count": 17 } ] }, { "cell_type": "code", "metadata": { "id": "DumQCoNytY65", "colab_type": "code", "colab": {}, "outputId": "c12aa8e1-30ff-497a-8555-eeaedbba7ade" }, "source": [ "(3+1j)*3" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(9+3j)" ] }, "metadata": { "tags": [] }, "execution_count": 18 } ] }, { "cell_type": "code", "metadata": { "id": "m6y2scrhtY69", "colab_type": "code", "colab": {}, "outputId": "63cf4722-6751-47d5-c361-bae99d4aa666" }, "source": [ "(1+2j)/(1+1j)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(1.5+0.5j)" ] }, "metadata": { "tags": [] }, "execution_count": 19 } ] }, { "cell_type": "markdown", "metadata": { "id": "th8ZtQJktY6_", "colab_type": "text" }, "source": [ "A komplex számokat gyakran két lebegőpontos számmal ábrázolják: a képzetes és a valós résszel. A `z` komplex számnak ezeket a részeit talán kicsit szokatlan módon (a változó után írt `.` és a megfelelő szó ) a `z.real` és `z.imag` utasításokkal olvashatjuk vissza." ] }, { "cell_type": "code", "metadata": { "id": "Fk9KW4bltY7A", "colab_type": "code", "colab": {}, "outputId": "0d6479bd-5e13-46a3-ac30-c618d9438fc2" }, "source": [ "(1.5+0.5j).real" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1.5" ] }, "metadata": { "tags": [] }, "execution_count": 20 } ] }, { "cell_type": "code", "metadata": { "id": "vPqy3foptY7C", "colab_type": "code", "colab": {}, "outputId": "0c688e05-8733-4da5-d1ca-d7b0aca76160" }, "source": [ "(1.5+0.5j).imag" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0.5" ] }, "metadata": { "tags": [] }, "execution_count": 21 } ] }, { "cell_type": "markdown", "metadata": { "id": "oSlklr2ftY7D", "colab_type": "text" }, "source": [ "A lebegőpontos és egész típusú konverziós függvények (`float()` és `int()`) nem működnek komplex számokra.\n", "A komplex-valós átalakításnak több lehetséges módja is van: ahhoz, hogy egy komplex számból valósat csinálj, használd az abs(z) utasítást, hogy megkapd a nagyságát, vagy a fennt már megismert `z.real` és `z.imag` utasítást, ha a valós része vagy a képzetes része kell. " ] }, { "cell_type": "code", "metadata": { "id": "hg_SJcVstY7E", "colab_type": "code", "colab": {}, "outputId": "841fb6c2-7cfe-4bc7-80a2-07f3ede197f7" }, "source": [ "float(3.0+4.0j) # ez nem működik" ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "TypeError", "evalue": "can't convert complex to float", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3.0\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m4.0j\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# ez nem működik\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: can't convert complex to float" ] } ] }, { "cell_type": "code", "metadata": { "id": "Qfdiuj38tY7G", "colab_type": "code", "colab": {} }, "source": [ "abs(3.0+4.0j) # sqrt(a.real**2 + a.imag**2)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "dYU4L_uitY7H", "colab_type": "text" }, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": { "id": "cdvwEF0EtY7I", "colab_type": "text" }, "source": [ "## Változók\n", "\n", "Programozási feladatok során rendszerint változókban tároljuk a hasznos információkat. A megírt programok nem konkrét számértékekre hanem, változókra hivatkoznak, ez teszi lehetővé, hogy különböző bemeneti adatokra ugyanazt az algoritmust ha kell, többször is lefuttathassuk. A változók neveit rendszerint az angol ABC betűiből és esetleg néhány egyéb karakterből szokás összeállítani. Például: \n", "```python\n", "szelesseg # ebben csak betűk vannak\n", "a_folyoso_hossza # ebben a változó névben szerepel a _ karakter is\n", "```\n", "\n", "A változók neveit célszerű úgy megválasztani hogy később könnyen kitalálható legyen, mit is jelölünk vele.\n", "A legtöbb programozási nyelvhez hasonlóan az egyenlőségjellel (=) lehet értéket adni egy változónak. Az értékadás után az értelmező újabb utasításra vár, látszólag nem történik semmi:" ] }, { "cell_type": "code", "metadata": { "collapsed": true, "id": "PhIQh1HVtY7I", "colab_type": "code", "colab": {} }, "source": [ "szelesseg = 20\n", "magassag = 5*9" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "0IXak-xOtY7K", "colab_type": "text" }, "source": [ "A háttérben viszont létrejöttek a `szelesseg` és `magassag` változók, amikkel most már hasznos dolgokat tudunk művelni. Számítsuk ki például a két változó szorzatát:" ] }, { "cell_type": "code", "metadata": { "id": "WSW7YTshtY7K", "colab_type": "code", "colab": {}, "outputId": "56ebca66-a362-4b75-a44f-726f7b3defa8" }, "source": [ "szelesseg * magassag" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "900" ] }, "metadata": { "tags": [] }, "execution_count": 24 } ] }, { "cell_type": "markdown", "metadata": { "id": "EZlWD-WstY7M", "colab_type": "text" }, "source": [ "Ha egy változó nincs definiálva (nincs érték rendelve hozzá), és használni próbáljuk, akkor hibaüzenetet kapunk:" ] }, { "cell_type": "code", "metadata": { "id": "V4kXmWMStY7N", "colab_type": "code", "colab": {}, "outputId": "09292f5c-2b9d-4075-f2b2-dcd5880989c9" }, "source": [ "n # Az n változót még nem definiáltuk" ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "NameError", "evalue": "name 'n' is not defined", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mn\u001b[0m \u001b[0;31m# Az n változót még nem definiáltuk\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'n' is not defined" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "z3vdTq_5tY7Q", "colab_type": "text" }, "source": [ "Az utoljára kiírt kifejezés értéke a _ (alsóvonás) változóban van. Így ha a Pythont asztali számológépként használod, akkor egyszerűbb folytatni a számolásokat, például:" ] }, { "cell_type": "code", "metadata": { "collapsed": true, "id": "z5WHmzh8tY7Q", "colab_type": "code", "colab": {} }, "source": [ "ado = 12.5 / 100" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "collapsed": true, "id": "ps6LwW2htY7T", "colab_type": "code", "colab": {} }, "source": [ "ar = 100.50" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "fq8UvsaatY7U", "colab_type": "code", "colab": {}, "outputId": "13bcf572-b822-4abc-993e-b19f379700b5" }, "source": [ "ar * ado" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "12.5625" ] }, "metadata": { "tags": [] }, "execution_count": 28 } ] }, { "cell_type": "code", "metadata": { "id": "TPI5iEIPtY7W", "colab_type": "code", "colab": {}, "outputId": "df7d4081-28fb-44cd-b4b6-97baef2e1a2d" }, "source": [ "ar + _" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "113.0625" ] }, "metadata": { "tags": [] }, "execution_count": 29 } ] }, { "cell_type": "code", "metadata": { "id": "1dPboMs0tY7X", "colab_type": "code", "colab": {}, "outputId": "8fbefcc9-fe36-4506-a2a6-451df2a5726b" }, "source": [ "round(_, 2) #kerekítés 2 tizedes jegyre" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "113.06" ] }, "metadata": { "tags": [] }, "execution_count": 30 } ] }, { "cell_type": "markdown", "metadata": { "id": "7bDpJokQtY7Z", "colab_type": "text" }, "source": [ "Ezt a változót csak olvasható változóként kezelhetjük. Ne adjunk értéket neki, mert ha adunk, akkor létrehozunk egy független helyi változót azonos névvel, amely meggátolja a beépített változó elérését, amely mágikus módon viselkedik. (Ha egy globális változó nevével létrehozunk egy helyi változót, akkor az értelmező a helyit használja.)" ] }, { "cell_type": "markdown", "metadata": { "id": "BdUOyqIhtY7Z", "colab_type": "text" }, "source": [ "A notebookban az előző eredményen kívül jóval korábbi eredményekre is hivatkozhatunk. Például 2. parancs kimenetére a következőképpen:" ] }, { "cell_type": "code", "metadata": { "id": "-7JSVGtZtY7a", "colab_type": "code", "colab": {}, "outputId": "b3031913-31aa-4827-e1ee-8fe7d700de38" }, "source": [ "Out[2]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "4" ] }, "metadata": { "tags": [] }, "execution_count": 31 } ] }, { "cell_type": "markdown", "metadata": { "id": "nmWCDkCFtY7b", "colab_type": "text" }, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": { "id": "qpYnxKh_tY7c", "colab_type": "text" }, "source": [ "## Logikai Bool-változók és feltételes végrehajtás\n", "Ahhoz, hogy a program reagálni tudjon bemenetekre, vagy egy már kiszámolt részeredménytől függhessen további működése, szükség van feltételek vizsgálatára. Gondoljunk például a másodfokú egyenlet megoldóképletére! Attól függően, hogy a diszkrimináns pozitív vagy negatív, léteznek, ill. nem léteznek valós gyökök, így értelemszerűen egy ilyen programnak \"döntenie\" kell. A döntés egy feltétel vizsgálatán alapul (pl. poz. vagy neg. diszkrimináns), ami lehet igaz, vagy hamis, angolul *True*, ill. *False*. Az igaz és hamis értéket tárolni képes változó típusa: **boolean** (Bool-változó). (Emlékezzünk vissza az **int** ill. **float** típusokra)." ] }, { "cell_type": "markdown", "metadata": { "id": "U5DABccitY7d", "colab_type": "text" }, "source": [ "Két objektum azonosságáról dupla egyenlőségjellel, azaz a == operátor segítségével tájékozódhatunk. " ] }, { "cell_type": "code", "metadata": { "id": "ri-BKf95tY7d", "colab_type": "code", "colab": {}, "outputId": "a34a7c49-31b3-4844-fe2f-c7c03f1d1738" }, "source": [ "1 == 1 # 1 megeggyezik 1-el ?" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 32 } ] }, { "cell_type": "code", "metadata": { "id": "XJFveD6JtY7f", "colab_type": "code", "colab": {}, "outputId": "cacdb887-ff07-4dfa-93f7-e30a4348520f" }, "source": [ "1 == 2 # 1 megeggyezik 2-vel ?" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 33 } ] }, { "cell_type": "markdown", "metadata": { "id": "IiDLiEYntY7h", "colab_type": "text" }, "source": [ "Ha arról akarunk meggyőződni hogy két objektum nem ugyanaz, akkor ezt a != operátor segítségével tehetjük meg:" ] }, { "cell_type": "code", "metadata": { "id": "fpnC3NvVtY7i", "colab_type": "code", "colab": {}, "outputId": "e6199ba3-c865-4a50-ba36-f56d176d84ae" }, "source": [ "1!=2 #1 nem 2 ?" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 34 } ] }, { "cell_type": "markdown", "metadata": { "id": "07OfN5Y5tY7j", "colab_type": "text" }, "source": [ "Természetszerűen számok esetén a <,> operátorok segítségével dönthetjük el, hogy melyik szám a nagyobb:" ] }, { "cell_type": "code", "metadata": { "id": "aIFxSKzvtY7k", "colab_type": "code", "colab": {}, "outputId": "342bfb71-a0fc-4f4f-b917-892089e6c6a8" }, "source": [ "1>2" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 35 } ] }, { "cell_type": "code", "metadata": { "id": "91YbGJjrtY7m", "colab_type": "code", "colab": {}, "outputId": "9e727850-f735-48e7-8c67-cd2d3fa5717f" }, "source": [ "1<2" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 36 } ] }, { "cell_type": "code", "metadata": { "id": "qlmeRID_tY7q", "colab_type": "code", "colab": {}, "outputId": "1f6df023-b0a3-412f-fae4-9faf535681f9" }, "source": [ "1j+3>2 # complex számokra nem értelmezzük a <> jeleket!!" ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "TypeError", "evalue": "unorderable types: complex() > int()", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m1j\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;36m2\u001b[0m \u001b[0;31m# complex számokra nem értelmezzük a <> jeleket!!\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unorderable types: complex() > int()" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "5hfg3nFhtY7r", "colab_type": "text" }, "source": [ "Egy igaz/hamis értéket változóban is tárolhatunk:" ] }, { "cell_type": "code", "metadata": { "collapsed": true, "id": "EMDBFUi2tY7s", "colab_type": "code", "colab": {} }, "source": [ "igazvagyhamis= 2*2==5" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "-Ln73SZLtY7t", "colab_type": "code", "colab": {}, "outputId": "3204a548-7ba7-4961-dfed-ffece75375fb" }, "source": [ "igazvagyhamis" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 39 } ] }, { "cell_type": "markdown", "metadata": { "id": "yWs8G92ntY7v", "colab_type": "text" }, "source": [ "Az **is** és **is not** relációk összehasonlítják, hogy két dolog valóban azonos-e. Minden összehasonlító relációnak azonos precedenciája van, mely magasabb, mint a számokkal végzett műveleteké." ] }, { "cell_type": "code", "metadata": { "id": "zrurmMubtY7v", "colab_type": "code", "colab": {}, "outputId": "ef4442bc-d5ca-4aec-847d-e42c8d07454d" }, "source": [ "1==1.0 # A számegyenesen történő összehasonlítás" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 40 } ] }, { "cell_type": "code", "metadata": { "id": "y_SHgYfttY70", "colab_type": "code", "colab": {}, "outputId": "4232cb3e-5859-4c1d-b57f-a3de4d5df86f" }, "source": [ "1 is 1.0 # Objektum típusa és értéke szerint történő összehasonlítás: int vs float" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 41 } ] }, { "cell_type": "markdown", "metadata": { "id": "lDNLJz_3tY71", "colab_type": "text" }, "source": [ "Egy objektumról az **isinstance** parancs segítségével tudjuk eldönteni, hogy milyen típusú:" ] }, { "cell_type": "code", "metadata": { "id": "qcWa_4LatY72", "colab_type": "code", "colab": {}, "outputId": "fbffb84b-d06a-4c35-fea7-8f7257896b96" }, "source": [ "isinstance(1,int) # Vajon az '1' az int típusú ?" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 42 } ] }, { "cell_type": "code", "metadata": { "id": "UqlF7bf5tY73", "colab_type": "code", "colab": {}, "outputId": "9f1f0f98-4cf6-4be1-cd8c-52e3e18d4455" }, "source": [ "isinstance(2,float) # Vajon a '2' float típusú ?" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 43 } ] }, { "cell_type": "markdown", "metadata": { "id": "vU9vH7V7tY74", "colab_type": "text" }, "source": [ "Relációkat láncolhatunk is, például: az a < b == c megvizsgálja, hogy az a kisebb-e mint b, és ezen felül b egyenlő-e c-vel." ] }, { "cell_type": "code", "metadata": { "id": "93j-RyE5tY74", "colab_type": "code", "colab": {}, "outputId": "e834a5a1-fa21-44ca-e37f-74a0a868cf3b" }, "source": [ "b=2\n", "1 < 2 == b" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 44 } ] }, { "cell_type": "code", "metadata": { "id": "YErC1P7GtY76", "colab_type": "code", "colab": {}, "outputId": "2e726064-ff2f-4302-bd36-5ff9bd47d4bb" }, "source": [ "b=3\n", "1 < 2 == b" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 45 } ] }, { "cell_type": "markdown", "metadata": { "id": "Atv2ig56tY7-", "colab_type": "text" }, "source": [ "A relációkat összefűzhetjük **and** és **or** logikai műveletekkel is. Reláció erdményét (vagy bármely logikai műveletét) ellentettjére változtathatjuk a **not** művelettel." ] }, { "cell_type": "code", "metadata": { "id": "DGA30H3mtY7_", "colab_type": "code", "colab": {}, "outputId": "582fcbd4-4cf9-49dc-a0c5-4972b1fb8070" }, "source": [ "not 2*2==5" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 46 } ] }, { "cell_type": "markdown", "metadata": { "id": "5iv_s4ZqtY8B", "colab_type": "text" }, "source": [ "Az **and**, **or** és **not** fügvények, ha zárójelezést nem alkalmazunk, akkor a relációjelek után kerülnek kiértékelésre (más szóval mondva a relációjelekhez képest alacsonyabb a precedenciájuk). \n", "A három logikai művelet közül a **not** értékelődik ki legelőször (neki a magasabb a precedenciája a másik kettőhöz képest), és az **or** értékelődik ki utolsónak. \n", "\n", "Tehát az A and not B or C ugyanazt jelenti, mint az (A and (not B)) or C. Természetesen a zárójeleket használhatjuk a kívánt feltétel eléréséhez." ] }, { "cell_type": "code", "metadata": { "id": "tPDrFdT9tY8C", "colab_type": "code", "colab": {}, "outputId": "dde2e2bb-86b7-404d-e6f9-63607291d825" }, "source": [ "b=3\n", "((1==2) and (3==4)) or (b==3)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 47 } ] }, { "cell_type": "markdown", "metadata": { "id": "H-julPUItY8D", "colab_type": "text" }, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": { "id": "8ZEQeiGwtY8D", "colab_type": "text" }, "source": [ "## Karakterláncok (Stringek)\n", "\n", "A számok mellett a Python karakterláncokkal, például szavakkal is tud műveleteket végezni. A karakterláncokat egyszeres ('...') vagy dupla idézőjelek (\"...\") közé lehet zárni. A két jelölés között nincs jelentős különbség. A \\\\ használható arra, hogy a karakterláncbeli idézőjeleket levédjük:" ] }, { "cell_type": "code", "metadata": { "id": "BESXjBkYtY8E", "colab_type": "code", "colab": {}, "outputId": "6467855f-a084-417d-8400-1988ba76c7e9" }, "source": [ "'spam eggs'" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'spam eggs'" ] }, "metadata": { "tags": [] }, "execution_count": 48 } ] }, { "cell_type": "code", "metadata": { "id": "-rhDUxyJtY8F", "colab_type": "code", "colab": {}, "outputId": "0362ce33-427c-40fe-aa2a-058475871a76" }, "source": [ "'doesn\\'t'" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "\"doesn't\"" ] }, "metadata": { "tags": [] }, "execution_count": 49 } ] }, { "cell_type": "code", "metadata": { "id": "KzyIn0hStY8G", "colab_type": "code", "colab": {}, "outputId": "5022d72a-3df3-41cf-f62a-6a38b404c79e" }, "source": [ "\"doesn't\"" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "\"doesn't\"" ] }, "metadata": { "tags": [] }, "execution_count": 50 } ] }, { "cell_type": "code", "metadata": { "id": "k2WZ3-A1tY8H", "colab_type": "code", "colab": {}, "outputId": "67391b15-6f95-4ccd-9c32-219e54f87dc5" }, "source": [ "'\"Yes,\" he said.'" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'\"Yes,\" he said.'" ] }, "metadata": { "tags": [] }, "execution_count": 51 } ] }, { "cell_type": "code", "metadata": { "id": "_I_lT7uxtY8K", "colab_type": "code", "colab": {}, "outputId": "d17be3a9-d611-4607-de75-cbaa78219fb6" }, "source": [ "\"\\\"Yes,\\\" he said.\"" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'\"Yes,\" he said.'" ] }, "metadata": { "tags": [] }, "execution_count": 52 } ] }, { "cell_type": "code", "metadata": { "id": "zpRJpJNPtY8L", "colab_type": "code", "colab": {}, "outputId": "530da34b-e88e-4f77-cf36-1a7e973ee323" }, "source": [ "'\"Isn\\'t,\" she said.'" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'\"Isn\\'t,\" she said.'" ] }, "metadata": { "tags": [] }, "execution_count": 53 } ] }, { "cell_type": "markdown", "metadata": { "id": "6-_KGm7ztY8M", "colab_type": "text" }, "source": [ "A kimeneti karakterlánc idézőjelekben jelenik meg, a speciális karakterek vissza-perrel (\\\\) levédve. Bár ez néha különbözőnek látszik a bemenettől (az idézőjel fajtája megváltozhat), a két karakterlánc egyenértékű. A print() függvény egy sokkal olvashatóbb kimenetet eredményez, elhagyva az idézőjeleket és kiírva a levédett és speciális karaktereket:" ] }, { "cell_type": "code", "metadata": { "id": "7qbIn6yYtY8N", "colab_type": "code", "colab": {}, "outputId": "67d836e5-723b-4bca-f3de-653805542ed0" }, "source": [ "print('\"Isn\\'t,\" she said.')" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "\"Isn't,\" she said.\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "collapsed": true, "id": "Ca3qZAk8tY8Q", "colab_type": "code", "colab": {} }, "source": [ "s = 'First line.\\nSecond line.' # \\n újsort jelent" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "3lZP7ubotY8S", "colab_type": "code", "colab": {}, "outputId": "5c45c507-2465-44da-9954-2d73ac13af70" }, "source": [ "s # print() nélkül a \\n benne van a kimenetben" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'First line.\\nSecond line.'" ] }, "metadata": { "tags": [] }, "execution_count": 56 } ] }, { "cell_type": "code", "metadata": { "id": "aB7dhPCRtY8V", "colab_type": "code", "colab": {}, "outputId": "b00b0ce2-b631-4e27-b294-f8265ddbcbfd" }, "source": [ "print(s) # print()-tel az \\n újsort hoz létre" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "First line.\n", "Second line.\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "KLv19c0wtY8Y", "colab_type": "text" }, "source": [ "Ha nem akarod, hogy a \\ jellel kezdődő karakterek speciális karakterként értelmeződjenek, akkor nyers karakterláncokat használhatsz egy r-et helyezve a kezdő idézőjel elé:" ] }, { "cell_type": "code", "metadata": { "id": "pEMDKjh2tY8Y", "colab_type": "code", "colab": {}, "outputId": "5120a20c-c12b-45ef-8fc1-5c6fbd326def" }, "source": [ "print('C:\\some\\name') # \\n újsort jelent" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "C:\\some\n", "ame\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "XGPqn6_ctY8Z", "colab_type": "code", "colab": {}, "outputId": "1d2bf92d-91ca-4673-e99d-f0e61d8c1439" }, "source": [ "print(r'C:\\some\\name') # r van az idézőjel előtt" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "C:\\some\\name\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "8HATSzj6tY8b", "colab_type": "text" }, "source": [ "A literális karakterláncok többsorosak is lehetnek. Egy lehetőség erre a hármas idézőjelek használata: \"\"\"...\"\"\" vagy '''...'''. Ilyenkor az újsorok automatikusan belekerülnek a karakterláncba, de ez a viselkedés megszüntethető, ha a sor végére egy \\\\-t adsz. A következő példa" ] }, { "cell_type": "code", "metadata": { "id": "m9p5XqcktY8b", "colab_type": "code", "colab": {}, "outputId": "eedeb9b2-c50d-4917-bc22-5375887b348e" }, "source": [ "print(\"\"\"\\\n", "Usage: thingy [OPTIONS]\n", " -h Display this usage message\n", " -H hostname Hostname to connect to\n", "\"\"\")" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Usage: thingy [OPTIONS]\n", " -h Display this usage message\n", " -H hostname Hostname to connect to\n", "\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "2xSr0Rm7tY8d", "colab_type": "text" }, "source": [ "Karakterláncokat a + művelettel ragaszthatunk össze, és *-gal ismételhetünk." ] }, { "cell_type": "code", "metadata": { "id": "dtxbCNhttY8d", "colab_type": "code", "colab": {}, "outputId": "b6bff8c0-da3e-453b-c05a-02eab6df2132" }, "source": [ "3 * 'un' + 'ium'" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'unununium'" ] }, "metadata": { "tags": [] }, "execution_count": 61 } ] }, { "cell_type": "markdown", "metadata": { "id": "B6rOPtURtY8h", "colab_type": "text" }, "source": [ "Két egymást követő literális karakterláncot (azokat, amik idézőjelben vannak, és nem egy változóban, vagy nem egy függvény hoz létre) az értelmező magától összefűz:" ] }, { "cell_type": "code", "metadata": { "id": "1arZW3wHtY8h", "colab_type": "code", "colab": {}, "outputId": "c9115348-d88b-473e-b17b-60ad82f4ffd5" }, "source": [ "'Py' 'thon'" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Python'" ] }, "metadata": { "tags": [] }, "execution_count": 62 } ] }, { "cell_type": "markdown", "metadata": { "id": "8_4JY2rFtY8k", "colab_type": "text" }, "source": [ "De ez csak két literálissal működik, változóval vagy kifejezéssel nem:" ] }, { "cell_type": "code", "metadata": { "id": "T8P2rRqStY8k", "colab_type": "code", "colab": {}, "outputId": "3fe64b75-3d8d-4a72-e864-b6f7b39aa685" }, "source": [ "prefix = 'Py'\n", "prefix 'thon' # nem tud változót és literális karakterláncot összefűzni" ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "SyntaxError", "evalue": "invalid syntax (, line 2)", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m prefix 'thon' # nem tud változót és literális karakterláncot összefűzni\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "fcXq_55GtY8m", "colab_type": "text" }, "source": [ "Ha változókat akarsz összefűzni, vagy változót literálissal, használj + műveletet:" ] }, { "cell_type": "code", "metadata": { "id": "QyMsWcGttY8n", "colab_type": "code", "colab": {}, "outputId": "93025fba-aa60-4ba0-8bd6-e92caa4b0deb" }, "source": [ "prefix = 'Py'\n", "prefix + 'thon'" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Python'" ] }, "metadata": { "tags": [] }, "execution_count": 64 } ] }, { "cell_type": "markdown", "metadata": { "id": "_Qv0IP55tY8o", "colab_type": "text" }, "source": [ "A literálisok összefűzése különösen hasznos hosszú sorok széttörésére:" ] }, { "cell_type": "code", "metadata": { "id": "lcRp9ksGtY8o", "colab_type": "code", "colab": {}, "outputId": "6e1c21c1-db20-4ebc-9ba6-16c94e863f2b" }, "source": [ "text = ('Több karakterláncot írhatunk zárójelbe, '\n", " 'hogy összefűzhessük azokat.')\n", "text" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Több karakterláncot írhatunk zárójelbe, hogy összefűzhessük azokat.'" ] }, "metadata": { "tags": [] }, "execution_count": 65 } ] }, { "cell_type": "markdown", "metadata": { "id": "84j1MD-gtY8p", "colab_type": "text" }, "source": [ "Minden karakterhez a karakterláncban a `Python` rendel egy sorszámot (indexet). Az első karakterhez tartozik a `0` index, a következőhöz az `1`-es index és így tovább. (Ez a konvenció tükrözi a C-ben megszokottakat.) " ] }, { "cell_type": "code", "metadata": { "collapsed": true, "id": "-rvoqzArtY8q", "colab_type": "code", "colab": {} }, "source": [ "szo = 'Python'" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "8cU4ZN88tY8s", "colab_type": "code", "colab": {}, "outputId": "d6bcf099-179f-4f04-aae2-d894f81c70d9" }, "source": [ "szo[0] # karakter a 0 pozícióban" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'P'" ] }, "metadata": { "tags": [] }, "execution_count": 67 } ] }, { "cell_type": "code", "metadata": { "id": "fw5b0Al7tY8t", "colab_type": "code", "colab": {}, "outputId": "36dc2e1c-0e5a-4b65-abc1-4f649d4466dc" }, "source": [ "szo[5] # karakter a 5 pozícióban" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'n'" ] }, "metadata": { "tags": [] }, "execution_count": 68 } ] }, { "cell_type": "markdown", "metadata": { "id": "c8gUgtXJtY82", "colab_type": "text" }, "source": [ "Az indexek negatívak is lehetnek, ilyenkor jobbról kezdünk el számolni:" ] }, { "cell_type": "code", "metadata": { "id": "f8CvuybetY82", "colab_type": "code", "colab": {}, "outputId": "cd6efc70-16ba-4d05-d6e8-5075c86b5da2" }, "source": [ "szo[-1] # utolsó karakter" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'n'" ] }, "metadata": { "tags": [] }, "execution_count": 69 } ] }, { "cell_type": "code", "metadata": { "id": "snf8mvDktY84", "colab_type": "code", "colab": {}, "outputId": "cfdea668-7438-415d-aa32-1317059ae1e4" }, "source": [ "szo[-2] # utolsó előtti karakter" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'o'" ] }, "metadata": { "tags": [] }, "execution_count": 70 } ] }, { "cell_type": "code", "metadata": { "id": "FNiv_anXtY86", "colab_type": "code", "colab": {}, "outputId": "5aa1d03e-23f1-46c8-cbe5-397d74005e13" }, "source": [ "szo[-6]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'P'" ] }, "metadata": { "tags": [] }, "execution_count": 71 } ] }, { "cell_type": "markdown", "metadata": { "id": "8bH2hBC0tY88", "colab_type": "text" }, "source": [ "Az indexelésen felül a szeletelés is támogatott. Míg az indexelés egyetlen karaktert jelöl ki, a szeletelés egy részkarakterláncot:" ] }, { "cell_type": "code", "metadata": { "id": "WxYh4LgYtY89", "colab_type": "code", "colab": {}, "outputId": "9488059a-b1c7-47b4-bab2-74ec92c5362d" }, "source": [ "szo[0:2] # karakterek a 0 pozíciótól (benne van) a 2-esig (az már nem)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Py'" ] }, "metadata": { "tags": [] }, "execution_count": 72 } ] }, { "cell_type": "code", "metadata": { "id": "3o3WLgxztY8-", "colab_type": "code", "colab": {}, "outputId": "523692a8-fd01-4a5d-8639-c646546e0ecd" }, "source": [ "szo[2:5] # karakterek a 2 pozíciótól (benne van) a 5-esig (az már nem)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'tho'" ] }, "metadata": { "tags": [] }, "execution_count": 73 } ] }, { "cell_type": "markdown", "metadata": { "id": "uY8aoRXctY8_", "colab_type": "text" }, "source": [ "A kezdő index mindig beleértendő az eredménybe, a végső index pedig nem. Ez teszi lehetővé, hogy s[:i] + s[i:] mindig egyenlő s-el:" ] }, { "cell_type": "code", "metadata": { "id": "UWYrzO2GtY9A", "colab_type": "code", "colab": {}, "outputId": "2a73c2e0-7fdd-403e-d61f-a9c8722c2878" }, "source": [ "szo[:2] + szo[2:]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Python'" ] }, "metadata": { "tags": [] }, "execution_count": 74 } ] }, { "cell_type": "code", "metadata": { "id": "uaGk3xaMtY9E", "colab_type": "code", "colab": {}, "outputId": "a825a287-dae7-458f-ef0f-c15eae937f38" }, "source": [ "szo[:4] + szo[4:]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Python'" ] }, "metadata": { "tags": [] }, "execution_count": 75 } ] }, { "cell_type": "markdown", "metadata": { "id": "2xR5BocstY9G", "colab_type": "text" }, "source": [ "A szeletek indexeinek hasznos alapértékei vannak; a kihagyott első index alapértéke 0, az elhagyott második index alapértéke a szeletelendő karakterlánc hossza:" ] }, { "cell_type": "code", "metadata": { "id": "gMEH1f-HtY9H", "colab_type": "code", "colab": {}, "outputId": "1492b5ac-0a63-4eec-c9ad-6d0a3c88f36f" }, "source": [ "szo[:2] # karakterek az elejétől a 2-esig (már nincs benne)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Py'" ] }, "metadata": { "tags": [] }, "execution_count": 76 } ] }, { "cell_type": "code", "metadata": { "id": "BlZGk8lOtY9K", "colab_type": "code", "colab": {}, "outputId": "ffcd79f0-4c27-4c2b-cf3e-5a2ee193ec75" }, "source": [ "szo[4:] # karekterek a 4-es pozíciótól (benne van) a végéig" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'on'" ] }, "metadata": { "tags": [] }, "execution_count": 77 } ] }, { "cell_type": "code", "metadata": { "id": "mA7F0RIMtY9L", "colab_type": "code", "colab": {}, "outputId": "02a13322-7769-4fed-e780-d0a986f4209b" }, "source": [ "szo[-2:] # karakterek az utolsó előttitől (beleértve) a végéig" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'on'" ] }, "metadata": { "tags": [] }, "execution_count": 78 } ] }, { "cell_type": "markdown", "metadata": { "id": "xAxN-Vv5tY9O", "colab_type": "text" }, "source": [ "Jegyezd meg, hogy a -0 valóban azonos a 0-val, így ez nem jobbról számol!" ] }, { "cell_type": "code", "metadata": { "id": "WC9bjOA5tY9O", "colab_type": "code", "colab": {}, "outputId": "085a40bc-34f4-45be-fbe4-5737fb3a1ee3" }, "source": [ "szo[-0] # mivel -0 és 0 egyenlőek" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'P'" ] }, "metadata": { "tags": [] }, "execution_count": 79 } ] }, { "cell_type": "markdown", "metadata": { "id": "PML4D9UvtY9Q", "colab_type": "text" }, "source": [ "A nem negatív indexek esetén a szelet hossza az indexek különbségével egyenlő, ha mindkettő a valódi szóhatárokon belül van. Például a szo[1:3] hossza 2.\n", "Ha olyan indexet használunk, amely túl nagy, az eredmény hibát ad:" ] }, { "cell_type": "code", "metadata": { "id": "4NphZSqAtY9Q", "colab_type": "code", "colab": {}, "outputId": "e4a81362-85ee-4b8e-f06f-f78c088561a9" }, "source": [ "szo[42] # a szo csak 7 karakteres" ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "IndexError", "evalue": "string index out of range", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mszo\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m42\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# a szo csak 7 karakteres\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: string index out of range" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "0z7XpV8dtY9V", "colab_type": "text" }, "source": [ "A Python karakterláncait nem lehet megváltoztatni, azok megváltoztathatatlanok. Ezért, ha egy adott indexű helyhez értéket rendelünk, hibát kapunk:" ] }, { "cell_type": "code", "metadata": { "id": "boYL5K2TtY9W", "colab_type": "code", "colab": {}, "outputId": "c6038173-0732-4762-89bc-2dccc8b7061d" }, "source": [ "szo[0] = 'J'" ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "TypeError", "evalue": "'str' object does not support item assignment", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mszo\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'J'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "yADh0Z8ttY9X", "colab_type": "text" }, "source": [ "Ha másik karakterláncra van szükség, alkothatunk egy újat:" ] }, { "cell_type": "code", "metadata": { "id": "AwDRBAGGtY9X", "colab_type": "code", "colab": {}, "outputId": "d2faee29-6a86-40d5-eed3-8c66f921a42d" }, "source": [ "'J' + szo[1:]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Jython'" ] }, "metadata": { "tags": [] }, "execution_count": 82 } ] }, { "cell_type": "code", "metadata": { "id": "gCzN7LxutY9Y", "colab_type": "code", "colab": {}, "outputId": "c465ba75-c5be-4a41-c529-ef4b895782ee" }, "source": [ "szo[:2] + 'py'" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Pypy'" ] }, "metadata": { "tags": [] }, "execution_count": 83 } ] }, { "cell_type": "markdown", "metadata": { "id": "lmCj7tXttY9Z", "colab_type": "text" }, "source": [ "A beépített len() függvény a karakterlánc hosszával tér vissza:" ] }, { "cell_type": "code", "metadata": { "id": "-O-rJYkotY9Z", "colab_type": "code", "colab": {}, "outputId": "3cc0b356-24c1-4431-8842-93792fb8f88e" }, "source": [ "s = 'legeslegelkáposztásíthatatlanságoskodásaitokért'\n", "len(s)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "47" ] }, "metadata": { "tags": [] }, "execution_count": 84 } ] }, { "cell_type": "markdown", "metadata": { "id": "K4udMp2mtY9b", "colab_type": "text" }, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": { "id": "1vORS_zItY9b", "colab_type": "text" }, "source": [ "## Listák (List)" ] }, { "cell_type": "markdown", "metadata": { "id": "kLih0wMrtY9b", "colab_type": "text" }, "source": [ "A Python többfajta összetett adattípust ismer, amellyel több különböző értéket csoportosíthatunk. Egy igen sokoldalú összetett adattípus a lista, amelyet vesszőkkel elválasztott értékekként írhatunk be szögletes zárójelbe zárva. Az alábbiakban a listákkal kapcsolatos műveletek alapjait tekintjük át. Listákról magyarul [itt](http://django.arek.uni-obuda.hu/python3-doc/html/tutorial/datastructures.html#a-listakrol-bovebben) egy kicsit bővebben olvashattok. A lista elemeinek nem kell azonos típusúaknak lenniük, bár sok alkalmazásban minden elem azonos típusú." ] }, { "cell_type": "code", "metadata": { "id": "F4nuDpmNtY9b", "colab_type": "code", "colab": {}, "outputId": "27e1cf6d-f93c-47ac-ec07-5bc4eded707b" }, "source": [ "a = ['spam', 'tojások', 100, 1234]\n", "a\n", "['spam', 'tojások', 100, 1234]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['spam', 'tojások', 100, 1234]" ] }, "metadata": { "tags": [] }, "execution_count": 85 } ] }, { "cell_type": "markdown", "metadata": { "id": "l7rCegLctY9d", "colab_type": "text" }, "source": [ "Ahogy a karakterláncokat, a listákat is indexelhetjük és szeletelhetjük:" ] }, { "cell_type": "code", "metadata": { "id": "B9EV8DW8tY9d", "colab_type": "code", "colab": {}, "outputId": "b3613789-0cf1-4375-a1a6-6094ba72a86e" }, "source": [ "a[0]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'spam'" ] }, "metadata": { "tags": [] }, "execution_count": 86 } ] }, { "cell_type": "code", "metadata": { "id": "sCqS6qDItY9f", "colab_type": "code", "colab": {}, "outputId": "fde5738b-b26f-48cc-c3d7-5f79ae14b278" }, "source": [ "a[3]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1234" ] }, "metadata": { "tags": [] }, "execution_count": 87 } ] }, { "cell_type": "code", "metadata": { "id": "CI3460BbtY9g", "colab_type": "code", "colab": {}, "outputId": "e9f53ff9-bda1-4467-b27c-68a0216286de" }, "source": [ "a[-2] " ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "100" ] }, "metadata": { "tags": [] }, "execution_count": 88 } ] }, { "cell_type": "code", "metadata": { "id": "6r0SmOtxtY9l", "colab_type": "code", "colab": {}, "outputId": "cd829f88-0f9d-436e-ca9b-d462e25922b6" }, "source": [ "a[1:-1] # a szeletelés új listát ad" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['tojások', 100]" ] }, "metadata": { "tags": [] }, "execution_count": 89 } ] }, { "cell_type": "markdown", "metadata": { "id": "37iGx3dptY9m", "colab_type": "text" }, "source": [ "A listák az összefűzést is támogatják:" ] }, { "cell_type": "code", "metadata": { "id": "w7pIVCM8tY9m", "colab_type": "code", "colab": {}, "outputId": "5e90a80b-6ac6-4f47-c903-a7317b97ab33" }, "source": [ "a[:2] + ['sonka', 2*2]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['spam', 'tojások', 'sonka', 4]" ] }, "metadata": { "tags": [] }, "execution_count": 90 } ] }, { "cell_type": "code", "metadata": { "id": "wtQTnd9wtY9n", "colab_type": "code", "colab": {}, "outputId": "5b25c8e1-a058-4f1a-e79c-3392df13b178" }, "source": [ "3*a[:3] + ['Boe!']" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['spam',\n", " 'tojások',\n", " 100,\n", " 'spam',\n", " 'tojások',\n", " 100,\n", " 'spam',\n", " 'tojások',\n", " 100,\n", " 'Boe!']" ] }, "metadata": { "tags": [] }, "execution_count": 91 } ] }, { "cell_type": "markdown", "metadata": { "id": "DtEXgpdOtY9o", "colab_type": "text" }, "source": [ "A karakterláncokkal ellentétben – amelyek megváltoztathatatlanok – a listák egyes elemeit módosíthatjuk:" ] }, { "cell_type": "code", "metadata": { "id": "ZA4iHzHPtY9p", "colab_type": "code", "colab": {}, "outputId": "8163a3cd-821c-4c28-cc01-d7a08abd2ab2" }, "source": [ "a[2] = a[2] + 23\n", "a" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['spam', 'tojások', 123, 1234]" ] }, "metadata": { "tags": [] }, "execution_count": 92 } ] }, { "cell_type": "markdown", "metadata": { "id": "67B_E4M4tY9q", "colab_type": "text" }, "source": [ "A szeleteknek értékeket is adhatunk, és ez akár a lista elemszámát is megváltoztathatja:" ] }, { "cell_type": "code", "metadata": { "id": "4RHu_vnDtY9q", "colab_type": "code", "colab": {}, "outputId": "79b49689-42ea-4b95-cc0e-f6c3c196aaa6" }, "source": [ "a[0:2] = [1, 12] # Pár elem átírása:\n", "a" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[1, 12, 123, 1234]" ] }, "metadata": { "tags": [] }, "execution_count": 93 } ] }, { "cell_type": "code", "metadata": { "id": "jse7Xk6-tY9r", "colab_type": "code", "colab": {}, "outputId": "fc9c3221-5e62-4b8f-cb04-1e31599d5a2a" }, "source": [ "a[0:2] = [] # Pár elem törlése:\n", "a" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[123, 1234]" ] }, "metadata": { "tags": [] }, "execution_count": 94 } ] }, { "cell_type": "code", "metadata": { "id": "Zfp8bip6tY9t", "colab_type": "code", "colab": {}, "outputId": "4f675bfc-7489-4c11-ae2c-07b4de730b6e" }, "source": [ "a[1:1] = ['bletch', 'xyzzy'] # Pár elem beszúrása:\n", "a" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[123, 'bletch', 'xyzzy', 1234]" ] }, "metadata": { "tags": [] }, "execution_count": 95 } ] }, { "cell_type": "code", "metadata": { "id": "47Acx6LYtY9u", "colab_type": "code", "colab": {}, "outputId": "502b1570-9dad-4c48-d393-04358002b76c" }, "source": [ "a[:0] = a # Beszúrja magát (pontosabban egy másolatát) a saját elejére.\n", "a" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]" ] }, "metadata": { "tags": [] }, "execution_count": 96 } ] }, { "cell_type": "markdown", "metadata": { "id": "fE6CqwlbtY9x", "colab_type": "text" }, "source": [ "A beépített len() függvény listákra is alkalmazható:" ] }, { "cell_type": "code", "metadata": { "id": "Hfa2Ak43tY9x", "colab_type": "code", "colab": {}, "outputId": "4f37d7c1-4707-4921-e3e2-ae25dff8f569" }, "source": [ "len(a)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "8" ] }, "metadata": { "tags": [] }, "execution_count": 97 } ] }, { "cell_type": "markdown", "metadata": { "id": "RIvWQgeytY90", "colab_type": "text" }, "source": [ "A listák egymásba ágyazhatóak, azaz listába elhelyezhetünk listát elemként:" ] }, { "cell_type": "code", "metadata": { "id": "oYg_TdV4tY90", "colab_type": "code", "colab": {}, "outputId": "72e0e1c5-6bfd-4cdf-8a38-c2959b2375f4" }, "source": [ "a = ['a', 'b', 'c']\n", "n = [1, 2, 3]\n", "x = [a, n]\n", "x" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[['a', 'b', 'c'], [1, 2, 3]]" ] }, "metadata": { "tags": [] }, "execution_count": 98 } ] }, { "cell_type": "code", "metadata": { "id": "LsQInWW0tY91", "colab_type": "code", "colab": {}, "outputId": "50789318-c8c0-44f1-ac4d-2740f7966a5c" }, "source": [ "x[0]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "metadata": { "tags": [] }, "execution_count": 99 } ] }, { "cell_type": "code", "metadata": { "id": "QCk3_VUltY92", "colab_type": "code", "colab": {}, "outputId": "b92771b8-4da7-4006-e30d-07a2421ab3c0" }, "source": [ "x[0][1]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'b'" ] }, "metadata": { "tags": [] }, "execution_count": 100 } ] }, { "cell_type": "code", "metadata": { "id": "VAHsccn3tY93", "colab_type": "code", "colab": {}, "outputId": "33e26c1a-1469-455e-b541-de6c2eee06c7" }, "source": [ "x[0][1]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'b'" ] }, "metadata": { "tags": [] }, "execution_count": 101 } ] }, { "cell_type": "markdown", "metadata": { "id": "jn3swG8ntY94", "colab_type": "text" }, "source": [ "Előfordul hogy meg szeretnénk tudni, hogy egy adott elem hanyadik helyen van a listában ezt az `.index()` függvény alkalmazásával kaphatjuk meg. Például nézzük meg hogy a lent definiált `autok` lista hanyadik eleme a `'merci'` karakter:" ] }, { "cell_type": "code", "metadata": { "id": "oS9Mb2r7tY94", "colab_type": "code", "colab": {} }, "source": [ "autok=['skoda','vw','merci','mazda']" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "JxWFmHiGtY96", "colab_type": "code", "colab": {}, "outputId": "a1e5ddc7-d04f-46e1-e47a-11300b343586" }, "source": [ "autok.index('merci')" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2" ] }, "metadata": { "tags": [] }, "execution_count": 103 } ] }, { "cell_type": "markdown", "metadata": { "id": "u-TrJV1xtY96", "colab_type": "text" }, "source": [ "Mint ahogy azt láttuk, a karakterláncok is indexelhetőek. Karakterláncban szereplő karakterek indexére is rákérdezhetünk az `.index()` paranccsal:" ] }, { "cell_type": "code", "metadata": { "collapsed": true, "id": "1CODLMI9tY97", "colab_type": "code", "colab": {} }, "source": [ "betuk='abcdef'" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "gb7alWuatY97", "colab_type": "code", "colab": {}, "outputId": "e2925d5d-fa86-4b8e-b2af-885965b7174c" }, "source": [ "betuk.index('e')" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "4" ] }, "metadata": { "tags": [] }, "execution_count": 105 } ] }, { "cell_type": "markdown", "metadata": { "id": "QE0-l1FQtY98", "colab_type": "text" }, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": { "id": "eZaWbkNFtY99", "colab_type": "text" }, "source": [ "### Még egy hasznos adatstruktúra: Szótárak (Dict-ek)" ] }, { "cell_type": "markdown", "metadata": { "id": "8189qqijtY99", "colab_type": "text" }, "source": [ "Sokszor előfordul, hogy adatokat nem valamilyen sorrendben, hanem inkább valamilyen címke szerint szeretnénk tárolni. Tipikus példája ennek egy idegen nyelvű szótár, ahol egy magyar kifejezést egy más nyelvbeli kifejezéssel párosítunk. Ezeknél a struktúráknál sokszor nem lényeges a sorrend, gondoljunk itt például valamilyen tárgy tulajdonságaira. Legyen mondjuk egy autónk ami \n", "\n", "- piros színű\n", "- 1976-ban gyártották\n", "- 3m hosszú \n", "- aok621 rendszámú\n", "\n", "Ezek a tulajdonságok egyértelműen meghatározzák az autót attól függetlenül, hogy a felsorolást milyen sorrendben tettük. \n", "Ilyen jellegű adatstruktúra a Python nyelvben a [szótár](http://django.arek.uni-obuda.hu/python3-doc/html/tutorial/datastructures.html#szotarak) vagy angolul a dictionary, röviden dict. \n", "A szótárak létrehozása kulcsszavak és a hozzájuk rendelt érték segítségével az alábbi szintaxis alapján történik:" ] }, { "cell_type": "code", "metadata": { "id": "AkhiE9HCtY9-", "colab_type": "code", "colab": {}, "outputId": "6f38f167-39db-47e4-c55f-c95718829cb1" }, "source": [ "tel = {'János': 4098, 'Simon': 4139}\n", "tel" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'János': 4098, 'Simon': 4139}" ] }, "metadata": { "tags": [] }, "execution_count": 106 } ] }, { "cell_type": "markdown", "metadata": { "id": "gW-Tz0xJtY9_", "colab_type": "text" }, "source": [ "A listákkal ellenben, ahol sorszám alapján hivatkozunk egy elemre, itt kulcsszó alapján:" ] }, { "cell_type": "code", "metadata": { "id": "5h_G-gWLtY9_", "colab_type": "code", "colab": {}, "outputId": "4dd28936-a26e-472a-d715-00e820eb881f" }, "source": [ "tel['János']" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "4098" ] }, "metadata": { "tags": [] }, "execution_count": 107 } ] }, { "cell_type": "markdown", "metadata": { "id": "CTmh8Fe5tY-A", "colab_type": "text" }, "source": [ "Új elemet is kulcsszó alapján adhatunk egy dict-hez:" ] }, { "cell_type": "code", "metadata": { "id": "xs3e3fm2tY-A", "colab_type": "code", "colab": {}, "outputId": "f8b5c8be-cc9c-434a-9e0b-56022ace994f" }, "source": [ "tel['Géza'] = 4127\n", "tel" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'Géza': 4127, 'János': 4098, 'Simon': 4139}" ] }, "metadata": { "tags": [] }, "execution_count": 108 } ] }, { "cell_type": "markdown", "metadata": { "id": "FtwoI6VJtY-B", "colab_type": "text" }, "source": [ "Természetesen nem csak számokat adhatunk meg a szótárunkba mint hozzárendelt értéket:" ] }, { "cell_type": "code", "metadata": { "id": "Uo5dbV0wtY-C", "colab_type": "code", "colab": {}, "outputId": "09ebfacf-40b8-4569-e025-dac82b29fa4d" }, "source": [ "tel['Brünhilda']='NEM TUDOM A SZÁMÁT'\n", "tel" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'Brünhilda': 'NEM TUDOM A SZÁMÁT', 'Géza': 4127, 'János': 4098, 'Simon': 4139}" ] }, "metadata": { "tags": [] }, "execution_count": 109 } ] }, { "cell_type": "markdown", "metadata": { "id": "XMjI5om9tY-C", "colab_type": "text" }, "source": [ "Így törlünk valakit a telefonkönyvből:" ] }, { "cell_type": "code", "metadata": { "id": "ETv-EYFctY-D", "colab_type": "code", "colab": {}, "outputId": "ea6bba17-7e40-45eb-c5af-306ca678e5b5" }, "source": [ "del tel['Simon']\n", "tel" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'Brünhilda': 'NEM TUDOM A SZÁMÁT', 'Géza': 4127, 'János': 4098}" ] }, "metadata": { "tags": [] }, "execution_count": 110 } ] }, { "cell_type": "markdown", "metadata": { "id": "JIedlELmtY-D", "colab_type": "text" }, "source": [ "A dict-ben szereplő kulcsszavakat a \".keys()\" metódus alkalmazásával tudjuk elérni. " ] }, { "cell_type": "code", "metadata": { "id": "amzGggYOtY-E", "colab_type": "code", "colab": {}, "outputId": "526359ba-0a53-4f02-e3ee-12dec79a433e" }, "source": [ "tel.keys()" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "dict_keys(['Géza', 'Brünhilda', 'János'])" ] }, "metadata": { "tags": [] }, "execution_count": 111 } ] }, { "cell_type": "markdown", "metadata": { "id": "QvHLodwYtY-E", "colab_type": "text" }, "source": [ "A fenti parancs kimenete látszólag tartalmazza az általunk kért információt. Sokszor azonban célszerű ezt az outputot a `list()` parancs segítségével listává konvertálni:" ] }, { "cell_type": "code", "metadata": { "id": "JvK5mMBPtY-F", "colab_type": "code", "colab": {}, "outputId": "1402f015-3753-47d9-ac91-6d64669ee967" }, "source": [ "list(tel.keys())" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Géza', 'Brünhilda', 'János']" ] }, "metadata": { "tags": [] }, "execution_count": 112 } ] }, { "cell_type": "markdown", "metadata": { "id": "1G_qCi4RtY-F", "colab_type": "text" }, "source": [ "Vajon Géza benne van a telefonkönyvben? Ezt a listáknál megismert \"in\" parancsal tudjuk ellenőrizni:" ] }, { "cell_type": "code", "metadata": { "id": "JnK6GfPWtY-G", "colab_type": "code", "colab": {}, "outputId": "5bc21ac2-ea0d-420d-8365-fd963207d01e" }, "source": [ "'Géza' in tel" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 113 } ] }, { "cell_type": "markdown", "metadata": { "id": "AUEP9URutY-G", "colab_type": "text" }, "source": [ "*Megjegyzés:* Nyilván attól, mert Brünhilda benne van a telefonkönyvben, nem biztos, hogy tudjuk a telefonszámát:" ] }, { "cell_type": "code", "metadata": { "id": "2ZlYwrXDtY-H", "colab_type": "code", "colab": {}, "outputId": "cf7e58e7-4216-419e-914b-fbeb3d2c6ec9" }, "source": [ "'Brünhilda' in tel" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 114 } ] }, { "cell_type": "code", "metadata": { "id": "FyrBeNjbtY-I", "colab_type": "code", "colab": {}, "outputId": "e74f2c7b-7b0f-44c3-bbe3-a6bca6c29784" }, "source": [ "tel['Brünhilda']" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'NEM TUDOM A SZÁMÁT'" ] }, "metadata": { "tags": [] }, "execution_count": 115 } ] }, { "cell_type": "markdown", "metadata": { "id": "2gBYUQ16tY-J", "colab_type": "text" }, "source": [ "A **dict** függvény segítségével az alábbi módon is definiálhatunk szótárakat:" ] }, { "cell_type": "code", "metadata": { "id": "Xl7gTyJ7tY-J", "colab_type": "code", "colab": {}, "outputId": "d7daefc1-464f-43a6-c501-c0be690cd113" }, "source": [ "tel2=dict(Ica=1234,Maca=4321,Dada=5567)\n", "tel2" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'Dada': 5567, 'Ica': 1234, 'Maca': 4321}" ] }, "metadata": { "tags": [] }, "execution_count": 116 } ] }, { "cell_type": "markdown", "metadata": { "id": "zJwIsTQWtY-K", "colab_type": "text" }, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": { "id": "JFBe4mFJtY-L", "colab_type": "text" }, "source": [ "## Bool változók és más változók kapcsolata\n", "Ahogy fentebb láttuk, egy objektumot egész számmá (**int()** függvény) vagy lebegőpontos számmá tudunk konvertálni ( **float()** függvény), illetve láttunk példát arra is hogy listává konvertáljunk valamit. Objektumokat Bool-változóvá is tudunk konvertálni. Erre szolgál a **bool()** függvény. Alább néhány példát nézzünk meg, hogy bizonyos dolgok hogy konvertálódnak a **bool()** függvénnyel.\n", "\n", "Egy szám vagy egy karakterlánc **True** értéket ad:" ] }, { "cell_type": "code", "metadata": { "id": "KgPDN2jCtY-L", "colab_type": "code", "colab": {}, "outputId": "1c2b3fd8-584f-44ff-e32a-b5e705a52d9c" }, "source": [ "bool(1.0)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 117 } ] }, { "cell_type": "code", "metadata": { "id": "rB1ZsBs-tY-N", "colab_type": "code", "colab": {}, "outputId": "68099d71-c10c-4d9c-96a4-7af5943d020e" }, "source": [ "bool('szoveg')" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 118 } ] }, { "cell_type": "markdown", "metadata": { "id": "ntI0DO5ptY-O", "colab_type": "text" }, "source": [ "Hasonlóan **True** értéked ad, ha egy nem üres listát vagy általában egy más létező objektumot vizsgálunk:" ] }, { "cell_type": "code", "metadata": { "id": "-0BNIDN-tY-O", "colab_type": "code", "colab": {}, "outputId": "1063c0ae-3d16-4057-db3e-bd6a2690a1e4" }, "source": [ "bool([1,'Bela'])" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 119 } ] }, { "cell_type": "markdown", "metadata": { "id": "sSIUiYhBtY-P", "colab_type": "text" }, "source": [ "**False** értéket ad a 0 az üres karakterlánc, illetve az üres lista:" ] }, { "cell_type": "code", "metadata": { "id": "jmLz4qsPtY-P", "colab_type": "code", "colab": {}, "outputId": "3d2ffcf4-848b-42b4-e395-2aeb5ccd6fe8" }, "source": [ "bool(0)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 120 } ] }, { "cell_type": "code", "metadata": { "id": "CFhymHEztY-Q", "colab_type": "code", "colab": {}, "outputId": "98a238cb-6d93-4c3d-9ec3-0042d173414c" }, "source": [ "bool(0.0)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 121 } ] }, { "cell_type": "code", "metadata": { "id": "fGBKxU28tY-R", "colab_type": "code", "colab": {}, "outputId": "966b2937-59c8-41a3-8bf4-bcabb015e8ad" }, "source": [ "bool('')" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 122 } ] }, { "cell_type": "code", "metadata": { "id": "NT_l-XgztY-V", "colab_type": "code", "colab": {}, "outputId": "475489a1-7b9d-40d6-ec4d-5ab8061986b5" }, "source": [ "bool([])" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 123 } ] }, { "cell_type": "markdown", "metadata": { "id": "upXqchwBtY-W", "colab_type": "text" }, "source": [ "Egy látszólag rosszul működő példa:" ] }, { "cell_type": "code", "metadata": { "id": "1YLBbDAvtY-X", "colab_type": "code", "colab": {}, "outputId": "272b9101-438f-4c36-b388-becf724c4905" }, "source": [ "'a' == ('a' or 'b') " ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 124 } ] }, { "cell_type": "code", "metadata": { "id": "qRWxu6RAtY-Z", "colab_type": "code", "colab": {}, "outputId": "1b9a759f-7828-496e-f1f8-fa653c0ede48" }, "source": [ "'b' == ('a' or 'b') " ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 125 } ] }, { "cell_type": "code", "metadata": { "id": "bu6oIoZJtY-a", "colab_type": "code", "colab": {}, "outputId": "775e83bc-502a-42dc-db15-6ae053c479db" }, "source": [ "'a' == ('a' and 'b') " ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 126 } ] }, { "cell_type": "code", "metadata": { "id": "dXqZn-t6tY-a", "colab_type": "code", "colab": {}, "outputId": "e68cf966-22d4-4de9-be12-3e5e5b83591f" }, "source": [ "'b' == ('a' and 'b') " ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 127 } ] }, { "cell_type": "markdown", "metadata": { "id": "SQy0yECRtY-c", "colab_type": "text" }, "source": [ "A második példa vajon miért **False**, ha az első **True** ? A harmadik miért **False**, ha a látszólag hasonló negyedik **True**?\n", "Ezen a ponton úgy tűnhet, hogy az **and** és **or** utasítások nem működnek megfelelően! Ha részletesen kiböngésszük, hogy mit is csinál a Python-értelmező, akkor kiderül, hogy pontosan azt teszi, amire megkértük. DE ez nem pontosan az, amire elsőnek gondolnánk! Az első két példa tehát nem azt ellenőrzi, hogy a **==** előtt álló karakter az szerepel-e a zárójelben. Mi is történik pontosan?\n", "Amikor a Python egy **or** kifejezéssel találkozik (a zárójeleken belül), sorba megy az **or** kifejezés által kapcsolatba hozott elemeken, és az első **True**-ként kiértékelhető objektum **értékét** adja vissza, tehát nem **True** vagy **False**-t hanem a változó értékét. Ez azért van így, mert ha egy **or** kifejezés egyik tagja igaz, akkor az egész kifejezés igaz. Az **and** ezzel szemben addig lépdel végig az alkotó kifejezéseken, amíg meggyőződött róla, hogy mindegyik kifejezés igaznak számít, és a legutolsó értéket adja vissza. A bool operációk ezen viselkedését *rövid zár*-nak hívják, és sok programozási nyelvben hasonlóan működik. \n", "Tehát ha arról akarunk meggyőződni, hogy egy kifejezés valamely kifejezéscsoport egyikével megegyezik, akkor az alábbiak szerint szükséges eljárni: " ] }, { "cell_type": "code", "metadata": { "id": "B_5m37YZtY-c", "colab_type": "code", "colab": {}, "outputId": "90475037-30d0-4ba5-b130-8aed2676b72e" }, "source": [ "(('a' == 'a') or ('a' == 'b'))" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 128 } ] }, { "cell_type": "code", "metadata": { "id": "44YT4Vk_tY-d", "colab_type": "code", "colab": {}, "outputId": "c9287bab-52c0-4132-c201-d08cdf4d08bd" }, "source": [ "(('b' == 'a') or ('b' == 'b'))" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 129 } ] }, { "cell_type": "markdown", "metadata": { "id": "jeR-5KVWtY-g", "colab_type": "text" }, "source": [ "Előfordul azonban sokszor, hogy az a csoport, amihez egy lehetséges elemet össze kell hasonlítani, igen népes. Például hogy mondjuk meg, hogy egy karakterlánc szerepel egy adott hosszú listában? Erre alkalmazható az **in** kulcsszó:" ] }, { "cell_type": "code", "metadata": { "collapsed": true, "id": "BuExwv9RtY-h", "colab_type": "code", "colab": {} }, "source": [ "Tage_der_Woche=['Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag','Sonntag']" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "GvfuMKlwtY-h", "colab_type": "code", "colab": {}, "outputId": "ff324c34-48c3-43cb-a308-af9e1e4a77c7" }, "source": [ "'hetfo' in Tage_der_Woche" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 131 } ] }, { "cell_type": "code", "metadata": { "id": "Rac5SqQ6tY-i", "colab_type": "code", "colab": {}, "outputId": "84220c00-0978-47b6-bb11-c13c958722fa" }, "source": [ "'Montag' in Tage_der_Woche" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 132 } ] } ] }