{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd96ea03",
   "metadata": {},
   "outputs": [],
   "source": [
    "phonebook = {} \n",
    "phonebook.update({'Dimitra': 3222}) \n",
    "print(phonebook) \n",
    "phonebook.update({'Dimitra': 4444}) \n",
    "print(phonebook) \n",
    "del phonebook['Dimitra'] \n",
    "print(phonebook) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b95abe3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Συνολικό παράδειγμα ελέγχου & αντικατάστασης\n",
    "phonebook = {} \n",
    "phonebook.update({'Dimitra': '3222'}) \n",
    "\n",
    "name = input('Give name:') \n",
    "phone = input('Give phone:') \n",
    "\n",
    "update_record = True \n",
    "if name in phonebook: \n",
    "    print(f'The record {name} exists in the phonebook') \n",
    "    ans = input('Override? Y/N') \n",
    "    update_record = (ans == 'Y') \n",
    "\n",
    "if (update_record): \n",
    "    phonebook.update({name: phone}) \n",
    "\n",
    "print(phonebook) \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b24072ca",
   "metadata": {},
   "source": [
    "Παράδειγμα 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7734b939",
   "metadata": {},
   "outputs": [],
   "source": [
    "phonebook = {'Philip': 2823, 'Nikos': 3128, 'Dimitra': 3222}\n",
    "phonebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2736bf10",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Εύρεση με βάση το κλειδί (key)\n",
    "phonebook['Dimitra']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c35f7142",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Προσθήκη ζεύγους key:value\n",
    "phonebook['Secretary'] = 1134\n",
    "print(phonebook)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f45d13ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Μπορώ να προσθέσω τιμή με ίδιο key; --> Κάνει update την τιμή!\n",
    "phonebook['Secretary'] = 1128"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a621cce",
   "metadata": {},
   "outputs": [],
   "source": [
    "phonebook"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41870a6d",
   "metadata": {},
   "source": [
    "Βασικές μέθοδοι και συναρτήσεις"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d93fa5e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(phonebook)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "780907d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Αφαίρεση τιμής\n",
    "del(phonebook['Dimitra'])\n",
    "print(phonebook)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "136a070f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# in\n",
    "'Nikos' in phonebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f996b9de",
   "metadata": {},
   "outputs": [],
   "source": [
    "phonebook.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1124b143",
   "metadata": {},
   "outputs": [],
   "source": [
    "phonebook.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48276012",
   "metadata": {},
   "outputs": [],
   "source": [
    "phonebook.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbce3afb",
   "metadata": {},
   "outputs": [],
   "source": [
    "phonebook.update({'Dimitra': 3222})\n",
    "print(phonebook)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e00cfa7b",
   "metadata": {},
   "source": [
    "Συγχώνευση-επέκταση λεξικών"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cc06997",
   "metadata": {},
   "outputs": [],
   "source": [
    "mails = {'Dimitra': 'dvarsou@.ntua.com', 'Nikos': 'nmand@central.ntua.gr', 'Philip': 'fdoganis@chemeng.ntua.gr', 'Secretary': 'secretariat@chemeng.ntua.gr'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00a8cb44",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_keys = phonebook.keys() | mails.keys()          # union of both key sets\n",
    "contact_data = {k: [phonebook.get(k), mails.get(k)] for k in all_keys}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76472c18",
   "metadata": {},
   "outputs": [],
   "source": [
    "contact_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8804624",
   "metadata": {},
   "outputs": [],
   "source": [
    "contact_data['Nikos'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dda822eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "contact_data_dict = {key: {'tel': phonebook[key], 'mail': mails[key]} for key in phonebook.keys()}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e086f116",
   "metadata": {},
   "outputs": [],
   "source": [
    "contact_data_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39563d37",
   "metadata": {},
   "outputs": [],
   "source": [
    "contact_data_dict['Nikos']['tel']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90b0c344",
   "metadata": {},
   "source": [
    "Προσπέλαση στοιχείων λεξικού"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21ca22df",
   "metadata": {},
   "outputs": [],
   "source": [
    "for name, tel in phonebook.items():\n",
    "    print(f'{name} number is {tel}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e273a771",
   "metadata": {},
   "outputs": [],
   "source": [
    "for name, contact in contact_data_dict.items():\n",
    "    print(name, \"has\", contact[\"tel\"], \"and\", contact[\"mail\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "495a15b0",
   "metadata": {},
   "source": [
    "enumerate function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef1dccee",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i, (name, contact) in enumerate(contact_data_dict.items(), start=1):\n",
    "    print(f\"{i}. {name}: {contact}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d8f9e45",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Αν παραλείψω το start=1 απλά θα ξεκινήσει η αρίθμηση από το 0\n",
    "for i, (name, contact) in enumerate(phonebook.items()):\n",
    "    print(f\"{i}. {name}: {contact}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38e0871f",
   "metadata": {},
   "source": [
    "Άσκηση"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "190b0b08",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Αρχικοποίηση - κενό λεξικό\n",
    "MW_dict = {}\n",
    "MW_dict = {'H2': 2, 'H2O': 18, 'C': 12, 'O2': 32}\n",
    "\n",
    "print(MW_dict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5661937",
   "metadata": {},
   "outputs": [],
   "source": [
    "MW_dict.update({'N2': 28})\n",
    "# MW_dict['N2'] = 28 # εναλλακτικά\n",
    "\n",
    "print(MW_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b23c72a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_element_key = input('Give the key of the new element: ')\n",
    "\n",
    "update_record = True\n",
    "\n",
    "if new_element_key in MW_dict:\n",
    "    print(f'Key {new_element_key} exists in the dictionary') \n",
    "    ans = input('Override? Y/N') \n",
    "    update_record = (ans == 'Y') \n",
    "    \n",
    "if (update_record): \n",
    "    new_element_mw = input('Give the MW of the new element: ')\n",
    "    new_element_mw = float(new_element_mw)\n",
    "    MW_dict.update({new_element_key: new_element_mw}) \n",
    "    \n",
    "print(MW_dict) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "150826f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "search_element_key = input('Search for an element based on key: ')\n",
    "\n",
    "if search_element_key in MW_dict:\n",
    "    print(f'{search_element_key} has MW = {MW_dict[search_element_key]}') \n",
    "else:\n",
    "    print(f'{search_element_key} not found') "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
