Files
2024-09-21 11:20:44 +02:00

1028 lines
20 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A Jupyter Notebook környezet\n",
"\n",
"- A [Jupyter Notebook](https://jupyter-notebook.readthedocs.io/en/stable/) egy böngésző alapú, interaktív munkakörnyezet.\n",
"- Elsődlegesen a Python nyelvhez fejlesztették ki, de más programozási nyelvekkel is használható.\n",
"- Egy notebook cellákból áll, a cellák lehetnek szöveges (Markdown) vagy kód (Code) típusúak.\n",
"- A kódcellákat le lehet futtatni, akár többször is egymás után. A futtatás eredménye megjelenik az adott kódcella utáni kimenetben.\n",
"- A notebook használata kétféle üzemmódban történik:\n",
" + Parancsmódban tudjuk elvégezni a cellaszintű műveleteket (pl. új cella beszúrása, cella törlése, cellák mozgatása, lépegetés a cellák között, stb). Néhány billentyűparancs:\n",
" - ```b```: Új kódcella beszúrása az aktuális cella után.\n",
" - ```m```: Az aktuális cella típusának átállítása szövegesre.\n",
" - ```dd```: Az aktuális cella törlése.\n",
" - ```Enter```: Átlépés szerkesztőmódba (az aktuális cella tartalmának szerkesztése).\n",
" + Szerkesztőmódban tudjuk szerkeszteni a cellák tartalmát. Néhány billentyűparancs:\n",
" - ```Ctrl+Enter```: Az aktuális cella futtatása.\n",
" - ```Esc```: Visszalépés parancsmódba.\n",
"- A billentyűparancsokról a Help / Keyboard Shortcuts menü ad részletesebb leírást."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Egyszerű adattípusok"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### [Egész szám](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A számok között a szokásos módon végezhetünk műveleteket.\n",
"1 + 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Megjegyzések:\n",
"- A szóközök nem számítanak, a fenti írásmód a PEP 8 kódolási stílust követi.\n",
"- A Jupyter a futtatás után megjeleníti a cella utolsó kifejezését."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Ha szeretnénk felülbírálni a precedenciát, használjunk zárójelezést!\n",
"(2 + 3) * 4"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A Python képes tetszőleges hosszúságú egész számokkal dolgozni, nincsen túlcsordulási hiba.\n",
"111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 + 1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Hozzunk létre egy i nevű változót, és tegyük bele a 11 értéket!\n",
"i = 11"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Megjegyzések:\n",
"- Az = az értékadás műveleti jele.\n",
"- i felveszi a megadott értéket, de magának az értékadásnak nincs eredménye. Emiatt a cella kimenete üres."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"22"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A változóra a továbbiakban is lehet hivatkozni.\n",
"i * 2"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# A változó értéke természetesen változtatható.\n",
"i = 42"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"i"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Az értékadást lehet kombinálni a többi művelettel.\n",
"i += 1 # ekvivalens az i = i + 1 értékadással"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"43"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"i"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.3333333333333335"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Lebegőpontos osztás.\n",
"7 / 3"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Egészosztás (levágja a törtrészt). Sok hibalehetőséget megelőz, hogy külön műveleti jele van.\n",
"7 // 3"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Maradékképzés.\n",
"7 % 3"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1024"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Van hatványozás is, ** a műveleti jele.\n",
"2**10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### [Lebegőpontos szám](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex)\n",
"\n",
"- A [lebegőpontos számábrázolás](https://hu.wikipedia.org/wiki/Lebeg%C5%91pontos_sz%C3%A1m%C3%A1br%C3%A1zol%C3%A1s) lehetővé teszi a valós számokkal történő, közelítő számolást.\n",
"- A Python lebegőpontos típusa az IEEE-754 szabvány dupla pontosságú (64 bites double) típusát valósítja meg."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.44"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Lebegőpontos állandókat a tizedespont használatával tudunk megadni.\n",
"2.34 + 1.1"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.4142135623730951"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Gyök kettő (közelítő) kiszámítása.\n",
"2**0.5"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"# Hozzunk létre egy f nevű, lebegőpontos típusú változót!\n",
"f = 2.6"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"float"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A type függvénnyel tudjuk lekérdezni f típusát.\n",
"type(f)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# ...vagy bármely más érték típusát.\n",
"type(2 * 3 - 10)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# Tegyünk most f-be egy int típusú értéket!\n",
"# Python-ban ez minden probléma nélkül megtehető.\n",
"f = 20"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(f)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### [Komplex szám](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex)\n",
"- A Python támogatja a komplex számokkal való számolást, külső könyvtárak használata nélkül."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(-0.2+0.4j)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Osztás algebrai alakban.\n",
"(1 + 2j) / (3 - 4j)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3.6709073923227765e-14+1j)"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A képzetes egység hatványozása.\n",
"1j**2021"
]
},
{
"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",
"- Pythonban 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": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'alma'"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A sztringállandót ' jelekkel határoljuk.\n",
"'alma'"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'körte'"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# ...de lehet használni \" jeleket is.\n",
"\"körte\""
]
},
{
"cell_type": "code",
"execution_count": 26,
"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": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A type függvény most is működik.\n",
"type('alma')"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'I ♥ ♬'"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Unicode szimbólumokat tartalmazó sztring.\n",
"'I ♥ ♬'"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A sztring hossza (Unicode szimbólumok száma):\n",
"len('I ♥ ♬')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### [Logikai érték](https://docs.python.org/3/library/stdtypes.html#boolean-values)\n",
"- A logikai igaz értéket a *True*, a hamisat a *False* jelöli. A nagy kezdőbetű fontos, a Python különbözőnek tekinti a kis- és nagybetűket."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"# Hozzunk létre logikai típusú változót!\n",
"b = True"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"bool"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(b)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n"
]
}
],
"source": [
"# Logikai ÉS művelet.\n",
"print(True and True)\n",
"print(True and False)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"True\n"
]
}
],
"source": [
"# Logikai VAGY művelet.\n",
"print(False or False)\n",
"print(False or True)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Logikai tagadás.\n",
"not True"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Az összehasonlító műveletek eredménye logikai érték.\n",
"2 < 3"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5 >= 11"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Pythonban az egyenlőségvizsgálat műveleti jele ==.\n",
"2 == 3"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"4 == 4"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10 != 20 # != jelentése: nem egyenlő"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### [None](https://docs.python.org/3/library/stdtypes.html#the-null-object)\n",
"- A szó jelentése *semmi* vagy *egyik sem*. A Pythonban a None értéknek helykitöltő szerepe van. Ezzel jelölhetjük pl. a hiányzó vagy érvénytelen eredményt vagy az alapértelmezett beállítást. "
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"NoneType"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A None érték típusa.\n",
"type(None)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"# Ha a cella utolsó kifejezése None értékű, akkor nincs kimenet.\n",
"1 + 1\n",
"None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Konverzió\n",
"\n",
"Az adattípusok neve függvényként használható az adott adattípusra való konvertálásra, amennyiben a konverziónak van értelme."
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int('12') # str => int"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int(2.7) # float => int"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int(True) # bool => int"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.7"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"float('2.7') # str => float"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(0) # int => bool"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "invalid literal for int() with base 10: 'sör'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[47], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124msör\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
"\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'sör'"
]
}
],
"source": [
"int('sör')"
]
}
],
"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
}