{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### A [math](https://docs.python.org/3/library/math.html) modul\n", "- Alapvető matematikai függvényeket tartalmaz.\n", "- Jótanács: Egy NumPy (lásd később) alapú kódban ne a math modul függvényeit használjuk, hanem a NumPy beépített függvényeit!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# A math modul importálása.\n", "import math" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.718281828459045" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Exponenciális függvény.\n", "math.exp(1)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.302585092994046" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Természetes alapú logaritmus.\n", "math.log(10)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Adott alapú (most 2-es) logaritmus.\n", "math.log(8, 2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Trigonometrikus függvények és inverzeik (a szöget radiánban adjuk meg).\n", "math.sin(0)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.cos(0)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.asin(0)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.acos(1)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.141592653589793" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Fok => radián átváltás.\n", "math.radians(180)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "179.99469134034814" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Radián => fok átváltás.\n", "math.degrees(3.1415)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9999999999999999" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.tan(math.radians(45))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.141592653589793" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# pi\n", "math.pi" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.718281828459045" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# e\n", "math.e" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [Sztring](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str)\n", "\n", "- A sztring adattípus szöveges értékek tárolására szolgál.\n", "- Python-ban a sztring nem más mint [Unicode](https://hu.wikipedia.org/wiki/Unicode) szimbólumok (másnéven Unicode karakterek) nem módosítható sorozata." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'alma'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# A sztringállandót ' jelekkel határoljuk.\n", "'alma'" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'körte'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ...de lehet használni \" jeleket is.\n", "\"körte\"" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "alma\n" ] } ], "source": [ "# Az előző cellák kimenetében a ' nem a sztring része, csak az adattípust jelzi.\n", "# Írjuk ki a sztring tartalmát, határoló jelek nélkül!\n", "print('alma')" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# A type függvény most is működik.\n", "type('alma')" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'I ♥ ♬'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# A sztringben természetesen használhatunk Unicode szimbólumokat.\n", "'I ♥ ♬'" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "foo\"bar\n", "foo'bar\n" ] } ], "source": [ "# A kétféle határoló értelme:\n", "print('foo\"bar')\n", "print(\"foo'bar\")" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "foo'bar\n", "foo\"bar\n" ] } ], "source": [ "# ...egyébként le kéne védeni az ' ill. \" karaktert.\n", "print('foo\\'bar')\n", "print(\"foo\\\"bar\")" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "# Hozzunk létre egy s nevű sztringváltozót!\n", "s = 'sör'" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'s'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# s karaktereinek kinyerése. Az indexelés 0-tól indul!\n", "s[0]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ö'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[1]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'r'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[2]" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# A kinyert karaktert egy 1 hosszú sztring formájában kapjuk vissza.\n", "type(s[0])" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "string index out of range", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[26], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Túlindexelés esetén hibaüzenetet kapunk.\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m s[\u001b[38;5;241m3\u001b[39m]\n", "\u001b[1;31mIndexError\u001b[0m: string index out of range" ] } ], "source": [ "# Túlindexelés esetén hibaüzenetet kapunk.\n", "s[3]" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'str' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[27], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# A sztring karaktereit nem lehet módosítani!\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;66;03m# (Majd később meglátjuk, hogy miért.)\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m s[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mb\u001b[39m\u001b[38;5;124m'\u001b[39m\n", "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "source": [ "# A sztring karaktereit nem lehet módosítani!\n", "# (Majd később meglátjuk, hogy miért.)\n", "s[0] = 'b'" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "# Természetesen s-nek adhatunk új értéket.\n", "s = 'xör'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Megjegyzés: Az értékadás megtörténik, de magának az értékadó kifejezésnek nincs eredménye. Emiatt a cellának nincsen kimenete." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "xör\n" ] } ], "source": [ "# Írjuk ki s tartalmát!\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# A sztring hossza (Unicode szimbólumok száma):\n", "len('Béla♥')" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'sörbor'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sztringek összefűzése.\n", "'sör' + 'bor'" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Tartalmazásvizsgálat.\n", "'ka' in 'abrakadabra'" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'xyz' in 'abrakadabra'" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'B\\xc3\\xa9la\\xe2\\x99\\xa5'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sztringből a kódolás műveletével képezhetünk bájtsorozatot.\n", "'Béla♥'.encode('utf-8')" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bytes" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Az eredmény típusa.\n", "type('Béla♥'.encode('utf-8'))" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# A bájtok száma nagyobb lehet, mint a Unicode szimbólumok száma!\n", "len('Béla♥'.encode('utf-8'))" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n", "2\n" ] } ], "source": [ "# Hány bájton tárolódnak a magyar ábécé ékezetes kisbetűi UTF-8 kódolás esetén?\n", "print(len('á'.encode('utf-8')))\n", "print(len('é'.encode('utf-8')))\n", "print(len('í'.encode('utf-8')))\n", "print(len('ó'.encode('utf-8')))\n", "print(len('ö'.encode('utf-8')))\n", "print(len('ő'.encode('utf-8')))\n", "print(len('ú'.encode('utf-8')))\n", "print(len('ü'.encode('utf-8')))\n", "print(len('ű'.encode('utf-8')))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "🤔🤔🤔 A fenti kód tele van ismétléssel. Gyakorlaton feladat lesz ezt rövidebben megoldani!" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "3\n" ] } ], "source": [ "# Hány bájton tárolódik a ♥ és a ♬ szimbólum?\n", "print(len('♥'.encode('utf-8')))\n", "print(len('♬'.encode('utf-8')))" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Béla♥'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Bájtsorozatból a dekódolás műveletével képezhetünk sztringet.\n", "b'B\\xc3\\xa9la\\xe2\\x99\\xa5'.decode('utf-8')" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Üres sztring létrehozása.\n", "''" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len('')" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'valami'" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Fehér karakterek (szóköz, tabulátor, sortörés) eltávolítása a sztring elejéről és végéről.\n", "' valami\\t\\n'.strip()" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'valami'" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Megadott karakterek eltávolítása a sztring elejéről és végéről.\n", "'++++valami---++----'.strip('+-')" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'álmos'" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Kisbetűssé alakítás.\n", "'Álmos'.lower()" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'KUTYA13'" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Nagybetűssé alakítás.\n", "'kutya13'.upper()" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hahaha'" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sztring ismétlése.\n", "'ha' * 3" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "l\n", "m\n", "a\n" ] } ], "source": [ "# Iterálás egy sztring karakterein.\n", "s = 'alma'\n", "for c in s:\n", " print(c)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "l\n", "m\n", "a\n" ] } ], "source": [ "# Iterálás egy sztring karakterein index segítségével.\n", "s = 'alma'\n", "for i in range(len(s)):\n", " print(s[i])" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "# A random modul importálása\n", "import random" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "# A random modul randint függvényének használata\n", "# Egy véletlen 5-ös lottószám generálása\n", "print(random.randint(1, 90))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.3" } }, "nbformat": 4, "nbformat_minor": 2 }