{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dictionaries in Python\n",
    "\n",
    "Trainer: Md. Jalal Uddin, Founder and director of Research Society\n",
    "    \n",
    "email: dmjalal90@gmail.com, 20205103002@nuist.edu.cn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dictionaries (store data): \n",
    "    \n",
    "    # curly brackets\n",
    "    # have keys and values (pairs)\n",
    "    # unordered (item does not have a defined order)  \n",
    "    # changeable \n",
    "    # No duplicate members\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# string, int, boolean, and list data types"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Jalal', 'wife': 'Ripa Jalal', 'kids': 'Jara Jalal', 'year of birth': 1990, 'good man': True, 'favourite colors': ['white', 'green', 'red']}\n"
     ]
    }
   ],
   "source": [
    "mydict = {\"name\": \"Jalal\",\"wife\": \"Ripa Jalal\",\"kids\":\"Jara Jalal\",\"year of birth\": 1990,\"good man\":True,\"favourite colors\": [\"white\", \"green\", \"red\"]}\n",
    "print(mydict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Jalal', 'wife': 'Ripa Jalal', 'kids': 'Jara Jalal', 'year of birth': 1990, 'good man': True, 'favourite colors': ['white', 'green', 'red']}\n"
     ]
    }
   ],
   "source": [
    "mydict = {\n",
    "  \"name\": \"Jalal\",\n",
    "  \"wife\": \"Ripa Jalal\",\n",
    "  \"kids\":\"Jara Jalal\",\n",
    "  \"year of birth\": 1990,\n",
    "  \"good man\":True,\n",
    "  \"favourite colors\": [\"white\", \"green\", \"red\"]  \n",
    "}\n",
    "print(mydict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# create a dictionary by using dict function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Jalal', 'wife': 'Ripa Jalal', 'kids': 'Jara Jalal', 'year_of_birth': '1990', 'good_man': True, 'favourite_colors': ['white', 'green', 'red']}\n"
     ]
    }
   ],
   "source": [
    "dict_with_funt = dict(name = \"Jalal\", wife = \"Ripa Jalal\", kids = \"Jara Jalal\", year_of_birth = \"1990\", good_man = True, favourite_colors = [\"white\", \"green\", \"red\"])\n",
    "print(dict_with_funt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# nested Dictionaries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Research Society': {'course name': 'Python', 'participants level': 'all', 'year': 2020}, 'IMSA': {'course name': 'Matlab', 'participants level': 'all', 'year': 2020}, 'CUSS and RS': {'course name': 'Statistics', 'participants level': 'all', 'year': 2020}}\n"
     ]
    }
   ],
   "source": [
    "training = {\n",
    "  \"Research Society\" : {\n",
    "    \"course name\" : \"Python\",\n",
    "    \"participants level\":\"all\",\n",
    "    \"year\" : 2020\n",
    "  },\n",
    "    \n",
    "  \"IMSA\" : {\n",
    "    \"course name\" : \"Matlab\",\n",
    "    \"participants level\":\"all\",\n",
    "    \"year\" : 2020\n",
    "  },\n",
    "    \n",
    "  \"CUSS and RS\" : {\n",
    "    \"course name\" : \"Statistics\",\n",
    "    \"participants level\":\"all\",\n",
    "    \"year\" : 2020\n",
    "  }\n",
    "}\n",
    "\n",
    "print(training)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# join dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Research Society': {'course name': 'Python', 'participants level': 'all', 'year': 2020}, 'IMSA': {'course name': 'Matlab', 'participants level': 'all', 'year': 2020}, 'CUSS and RS': {'course name': 'Statistics', 'participants level': 'all', 'year': 2020}}\n"
     ]
    }
   ],
   "source": [
    "Research_Society = {\n",
    "    \"course name\" : \"Python\",\n",
    "    \"participants level\":\"all\",\n",
    "    \"year\" : 2020\n",
    "}\n",
    " \n",
    "IMSA = {    \n",
    "    \"course name\" : \"Matlab\",\n",
    "    \"participants level\":\"all\",\n",
    "    \"year\" : 2020\n",
    "}\n",
    " \n",
    "CUSS_RS = {     \n",
    "    \"course name\" : \"Statistics\",\n",
    "    \"participants level\":\"all\",\n",
    "    \"year\" : 2020\n",
    "}\n",
    "\n",
    "training = {\n",
    "  \"Research Society\" : Research_Society,\n",
    "  \"IMSA\" : IMSA,\n",
    "  \"CUSS and RS\" : CUSS_RS\n",
    "}\n",
    "\n",
    "print(training)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# duplicates not allowed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'course name': 'Python', 'participants level': 'all', 'year': 2020}\n"
     ]
    }
   ],
   "source": [
    "RS = {\n",
    "    \"course name\" : \"Python\",\n",
    "    \"participants level\":\"all\",\n",
    "    \"year\" : 2019,\n",
    "    \"year\" : 2020\n",
    "}\n",
    "\n",
    "print(Research_Society)   # it is not allowed to have two items with the same key\n",
    "                          # overwrite existing values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# print the data type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(type(dict_with_funt))  # dictionaries are defined as objects with the data type 'dict'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# print the \"name\" from the dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jalal\n"
     ]
    }
   ],
   "source": [
    "print(dict_with_funt[\"name\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# print the number of items in the dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "print(len(dict_with_funt))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Dictionary Methods"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Jalal', 'wife': 'Ripa Jalal', 'kids': 'Jara Jalal', 'year of birth': 1990, 'good man': True, 'favourite colors': ['white', 'green', 'red']}\n"
     ]
    }
   ],
   "source": [
    "new_dict = mydict.copy()    # make a copy of a dictionary\n",
    "print(new_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Jalal', 'wife': 'Ripa Jalal', 'kids': 'Jara Jalal', 'year of birth': 1990, 'good man': True, 'favourite colors': ['white', 'green', 'red']}\n"
     ]
    }
   ],
   "source": [
    "new_dict1 = dict(new_dict)  # make a copy of a dictionary\n",
    "print(new_dict1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Jara Jalal', 'wife': 'Ripa Jalal', 'kids': 'Jara Jalal', 'year of birth': 1990, 'good man': True, 'favourite colors': ['white', 'green', 'red']}\n"
     ]
    }
   ],
   "source": [
    "new_dict.update({\"name\": \"Jara Jalal\"})    # update value\n",
    "print(new_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Jara Jalal', 'wife': 'Ripa Jalal', 'kids': 'Jara Jalal', 'year of birth': 1990, 'good man': True, 'favourite colors': ['white', 'green', 'red'], 'trainers': ['RS', 'IMSA', 'CUSS']}\n"
     ]
    }
   ],
   "source": [
    "new_dict.update({\"trainers\": [\"RS\", \"IMSA\", \"CUSS\"]})    # inserts the specified items to the dictionary\n",
    "print(new_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jara Jalal\n"
     ]
    }
   ],
   "source": [
    "get_value = new_dict.get(\"name\")   # get the value of the \"name\" item\n",
    "print(get_value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('name', 'Jara Jalal'), ('wife', 'Ripa Jalal'), ('kids', 'Jara Jalal'), ('year of birth', 1990), ('good man', True), ('favourite colors', ['white', 'green', 'red']), ('trainers', ['RS', 'IMSA', 'CUSS'])])\n"
     ]
    }
   ],
   "source": [
    "items = new_dict.items()   # return the dictionary's key-value pairs\n",
    "print(items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['name', 'wife', 'kids', 'year of birth', 'good man', 'favourite colors', 'trainers'])\n"
     ]
    }
   ],
   "source": [
    "keys = new_dict.keys()   # return the keys\n",
    "print(keys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values(['Jara Jalal', 'Ripa Jalal', 'Jara Jalal', 1990, True, ['white', 'green', 'red'], ['RS', 'IMSA', 'CUSS']])\n"
     ]
    }
   ],
   "source": [
    "values = new_dict.values()   # return the values\n",
    "print(values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'wife': 'Ripa Jalal', 'kids': 'Jara Jalal', 'year of birth': 1990, 'good man': True, 'favourite colors': ['white', 'green', 'red'], 'trainers': ['RS', 'IMSA', 'CUSS']}\n"
     ]
    }
   ],
   "source": [
    "new_dict.pop(\"name\")   # removes the specified item from the dictionary\n",
    "print(new_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'wife': 'Ripa Jalal', 'kids': 'Jara Jalal', 'year of birth': 1990, 'good man': True, 'favourite colors': ['white', 'green', 'red']}\n"
     ]
    }
   ],
   "source": [
    "new_dict.popitem()   # remove the last item from the dictionary\n",
    "print(new_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "new_dict.clear()   # removes all the elements from a dictionary\n",
    "print(new_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
