Files
PythonProgramozas_GKNB_MSTM032/05_gyak_mego.ipynb
2024-09-21 11:20:44 +02:00

762 lines
18 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Egy hallgatói adatnyilvántartás\n",
"A megoldásokat több lépésben készítjük el, az adott lépésekre fókuszálva."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. lépés \n",
"Egy hallgató adatai: név, Neptun kód, érdemjegyek. Minden hallgató ugyanazokat a tárgyakat tanulja (pl. 3 db tárgyat) és egy tárgyból mindenkinek pontosan egy db jegye van."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('Gipsz Jakab', 'ABC123', 1, 2, 3)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Ha az adatok nem módusulnak, akkor a tuple is megfelelő \n",
"('Gipsz Jakab', 'ABC123', 1, 2, 3)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Gipsz Jakab', 'ABC123', 1, 2, 3]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Ha az adatok módusulhatnak, akkor a lista a megfelelő \n",
"['Gipsz Jakab', 'ABC123', 1, 2, 3]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ABC123'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Egy változóban is tárolhatók az adatok, ha később hivatkozni szeretnénk rá\n",
"h = ('Gipsz Jakab', 'ABC123', 1, 2, 3)\n",
"# A hallgató Neptun kódja (feltételezve a fenti adatsorrendet)\n",
"h[1]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Gipsz Jakab', 'XYZ000', 1, 2, 3]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Lista esetén módosíthatók is az adatok\n",
"h = ['Gipsz Jakab', 'ABC123', 1, 2, 3]\n",
"# A Neptun kód módosítása\n",
"h[1] = 'XYZ000'\n",
"h"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. lépés \n",
"Legyen több hallgató a nyilvántartásban!"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gipsz Jakab\n",
"5\n"
]
}
],
"source": [
"# A hallgatókat (most két hallgatót) egy tuple-ben adjuk meg\n",
"\n",
"# Egy hallgató adatai tuple-ben\n",
"h = (('Gipsz Jakab', 'ABC123', 1, 2, 3),\n",
" ('Wincs Eszter', 'XYZ000', 2, 4, 5))\n",
"\n",
"# Az első hallgató neve\n",
"print(h[0][0])\n",
"\n",
"# Az utolsó hallgató 3. jegye\n",
"print(h[len(h) - 1][4])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' 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)",
"\u001b[1;32m<ipython-input-6-0a64637030d2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Az adatok nem módosíthatók\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mh\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
],
"source": [
"# Az adatok nem módosíthatók\n",
"h[1][4] = 2"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['Gipsz Jakab', 'ABC123', 1, 2, 3], ['Wincs Eszter', 'XYZ000', 2, 4, 5]]\n",
"[['Móricka', 'XXX999', 1, 1, 1], ['Wincs Eszter', 'XYZ000', 2, 4, 5]]\n",
"[['Móricka', 'XXX999', 1, 1, 1], ['Wincs Eszter', 'XYZ000', 2, 4, 3]]\n"
]
}
],
"source": [
"# A hallgatókat (most két hallgatót) egy listában adjuk meg\n",
"\n",
"# Egy hallgató adatai listában\n",
"h = [['Gipsz Jakab', 'ABC123', 1, 2, 3],\n",
" ['Wincs Eszter', 'XYZ000', 2, 4, 5]]\n",
"\n",
"print(h)\n",
"\n",
"# Az adatok módosíthatók\n",
"h[0] = ['Móricka', 'XXX999', 1, 1, 1]\n",
"print(h)\n",
"h[1][4] = 3 \n",
"print(h)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A jegyek elkülönítve is kezelhetők (például egy-egy listában)\n",
"h1 = [['Gipsz Jakab', 'ABC123', [1, 2, 3]],\n",
" ['Wincs Eszter', 'XYZ000', [2, 4, 5]]]\n",
"# Az első hallgató első jegye\n",
"h1[0][2][0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. lépés \n",
"A tárgyaknak legyenek nevei!"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('Matematika', 'Fizika', 'Programozás')"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Egy külön változóba vesszük fel a tárgyak neveit (ahelyett, hogy minden hallgatóhoz felvennénk őket)\n",
"t = ('Matematika', 'Fizika', 'Programozás')\n",
"t"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['Móricka', 'XXX999', 1, 1, 1], ['Wincs Eszter', 'XYZ000', 2, 4, 3]]\n",
"Móricka 1 Matematika\n"
]
}
],
"source": [
"# A jegyek és a tárgyak sorrendje megegyező\n",
"print(h)\n",
"# Az első hallgató neve, első jegye és hozzá tartozó tárgy neve\n",
"print(h[0][0], h[0][2], t[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. lépés \n",
"A Neptun kód legyen egyedi!"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['Gipsz Jakab', 'ABC123', 1, 2, 3],\n",
" ['Móricka', 'XXX999', 1, 1, 1],\n",
" ['Wincs Eszter', 'ABC123', 2, 4, 5]]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A lista nem 'védi' az egyediséget (azaz megenged egyforma Neptun kódokat)\n",
"h = [['Gipsz Jakab', 'ABC123', 1, 2, 3],\n",
" ['Móricka', 'XXX999', 1, 1, 1],\n",
" ['Wincs Eszter', 'ABC123', 2, 4, 5]]\n",
"h"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'ABC123': ['Wincs Eszter', 2, 4, 5], 'XXX999': ['Móricka', 1, 1, 1]}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A szótár viszont csak egyedi kulcsokat enged meg\n",
"# A Neptun kód a kulcs, az értékek pedig az egyes hallgatók adatai egy-egy listában\n",
"# Azonos kulcs esetén az utoljára felvett adat marad csak meg a szótárban\n",
"h = {'ABC123':['Gipsz Jakab', 1, 2, 3],\n",
" 'XXX999':['Móricka', 1, 1, 1],\n",
" 'ABC123':['Wincs Eszter', 2, 4, 5]}\n",
"h"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "0",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-13-919b098136b6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# A hallgatók elérése a szótárban már nem történhet index segítségével\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mh\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mKeyError\u001b[0m: 0"
]
}
],
"source": [
"# A hallgatók elérése a szótárban már nem történhet index segítségével\n",
"h[0]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Wincs Eszter', 2, 4, 5]"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A kulccsal kell indexelni\n",
"h['ABC123']"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Wincs Eszter'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Egy adat elérése is csak a 'hallgatón keresztül' történhet\n",
"# Az ABC123 Neptun kódú hallgató neve\n",
"h['ABC123'][0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5. lépés \n",
"A hallgatók tanulhassanak különböző tárgyakat!"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['Gipsz Jakab', 'ABC123', {'Matematika': 1, 'Fizika': 2, 'Programozás': 3}],\n",
" ['Móricka', 'XXX999', {'Mechanika': 1}],\n",
" ['Wincs Eszter', 'AAA123', {'Fizika': 2, 'Testnevelés': 5}]]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A tantárgyaknak a hallgatókhoz kell tartozni, így szótár a megfelelő adatstruktúra\n",
"h = [['Gipsz Jakab', 'ABC123', {'Matematika':1, 'Fizika':2, 'Programozás':3}],\n",
" ['Móricka', 'XXX999', {'Mechanika':1}],\n",
" ['Wincs Eszter', 'AAA123', {'Fizika':2, 'Testnevelés':5}]]\n",
"h"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Az első hallgató hány tárgyat tanul\n",
"len(h[0][2])"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Tanul-e fizikát az első hallgató?\n",
"'Fizika' in h[0][2]"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Hányast kapott belőle?\n",
"h[0][2]['Fizika']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 6. lépés \n",
"A hallgatók egy tárgyból több jegyet is kaphassanak!"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['Gipsz Jakab',\n",
" 'ABC123',\n",
" {'Matematika': [1, 2], 'Fizika': [2, 3], 'Programozás': [3, 4, 1]}],\n",
" ['Móricka', 'XXX999', {'Mechanika': [1, 1, 1]}],\n",
" ['Wincs Eszter', 'AAA123', {'Fizika': [2], 'Testnevelés': [5, 5, 5]}]]"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A tantárgyakhoz az 1 db érdemjegy helyett többet rendelünk (tuple vagy lista, itt most listát használunk)\n",
"h = [['Gipsz Jakab', 'ABC123', {'Matematika':[1, 2], 'Fizika':[2, 3], 'Programozás':[3, 4, 1]}],\n",
" ['Móricka', 'XXX999', {'Mechanika':[1, 1, 1]}],\n",
" ['Wincs Eszter', 'AAA123', {'Fizika':[2], 'Testnevelés':[5, 5, 5]}]]\n",
"h"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 3]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Az első hallgató jegyei fizikából\n",
"h[0][2]['Fizika']"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Gipsz Jakab',\n",
" 'ABC123',\n",
" {'Matematika': [1, 2], 'Fizika': [2, 3, 5], 'Programozás': [3, 4, 1]}]"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Az első hallgató kapott egy új jegyet (egy 5-t) fizikából\n",
"h[0][2]['Fizika'].append(5)\n",
"h[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 7. lépés \n",
"A hallgatóknak lehessenek kedvenc tárgyai!"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['Gipsz Jakab',\n",
" 'ABC123',\n",
" {'Matematika': 1, 'Fizika': 2, 'Programozás': 3},\n",
" {'Fizika'}],\n",
" ['Móricka', 'XXX999', {'Mechanika': 1}, {}],\n",
" ['Wincs Eszter',\n",
" 'AAA123',\n",
" {'Fizika': 2, 'Testnevelés': 5},\n",
" {'Fizika', 'Testnevelés'}]]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A halmaz a megfelelő adatstruktúra a kedvenc tárgyaknak, felvesszük a hallgatókhoz (most utolsó adatként)\n",
"h = [['Gipsz Jakab', 'ABC123', {'Matematika':1, 'Fizika':2, 'Programozás':3}, {'Fizika'}],\n",
" ['Móricka', 'XXX999', {'Mechanika':1}, {}],\n",
" ['Wincs Eszter', 'AAA123', {'Fizika':2, 'Testnevelés':5}, {'Testnevelés', 'Fizika'}]]\n",
"h"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Van-e kedvenc tárgya az első hallgatónak?\n",
"len(h[0][3]) > 0\n"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Az utolsó hallgató kedveli-e a Fizikát?\n",
"'Fizika' in h[len(h) - 1][3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 8. lépés \n",
"A hallgatók egyes adatait ne indexekkel, hanem nevekkel (Név, Neptun kód, Jegyek, Kedvencek) lehessen hivatkozni!"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'Név': 'Gipsz Jakab',\n",
" 'Neptun kód': 'ABC123',\n",
" 'Jegyek': {'Fizika': 5},\n",
" 'Kedvencek': {'Fizika'}},\n",
" {'Név': 'Móricka',\n",
" 'Neptun kód': 'XXX999',\n",
" 'Jegyek': {'Mechanika': 1},\n",
" 'Kedvencek': {}},\n",
" {'Név': 'Wincs Eszter',\n",
" 'Neptun kód': 'AAA123',\n",
" 'Jegyek': {'Fizika': 2, 'Testnevelés': 5},\n",
" 'Kedvencek': {'Fizika', 'Testnevelés'}}]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Ehhez az egyes hallgatók adatait szótárba kell tennünk\n",
"h = [{'Név':'Gipsz Jakab', 'Neptun kód':'ABC123', 'Jegyek':{'Fizika':5}, 'Kedvencek':{'Fizika'}},\n",
" {'Név':'Móricka', 'Neptun kód':'XXX999', 'Jegyek':{'Mechanika':1}, 'Kedvencek':{}},\n",
" {'Név':'Wincs Eszter', 'Neptun kód':'AAA123', 'Jegyek':{'Fizika':2, 'Testnevelés':5}, 'Kedvencek':{'Testnevelés', 'Fizika'}}]\n",
"h"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gipsz Jakab {'Fizika': 5}\n"
]
}
],
"source": [
"# Az első hallgató neve és érdemjegyei\n",
"print(h[0]['Név'], h[0]['Jegyek'])"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Fizika', 'Testnevelés'}"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# A harmadik hallgató kedvenc tárgyai\n",
"h[2]['Kedvencek']"
]
}
],
"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
}