{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "

Day 10 Dictionary and Sets in Python

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Yesterday Recap\n", "- List\n", " - Accessing\n", " - Methods\n", "- Tuple\n", " - Accessing\n", " - Methods\n", " \n", "### Todays Objectives\n", "\n", "- Dictionary\n", " - Accessing the elements\n", " - Dictionary Methods\n", "- Sets\n", " - Set Methods" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary\n", "\n", "It is used to store non-homogenous group of data in the form of Key:value\n", "\n", "#### Properties\n", "\n", "- It is used to store data as K:V in `{}`\n", "- It is iterable\n", "- Ordered\n", " - Python 3.6 is unOrdered\n", " - Python 3.6+ in ordered\n", "- Key should be unique and it is immutable" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "d1 = {}\n", "d2 = dict()\n", "\n", "\n", "print(type(d1), type(d2))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'RollNo': [1, 2, 3, 4, 5, 6]}\n" ] } ], "source": [ "d1 = {'RollNo': [1,2,3,4,5,6]}\n", "\n", "\n", "print(d1)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'1234567890': ['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210'], '9876543210': ['Python', 'Earth', '01/01/1994', '0123456789']}\n" ] } ], "source": [ "d1 = {'1234567890': ['APSSDC', 'Vijayawada', '01/01/2000', '9876543210'],\n", " '9876543210': ['Python', 'Earth', '01/01/1994', '0123456789'],\n", " '1234567890': ['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210']}\n", "\n", "print(d1)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unhashable type: 'list'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0md2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;34m'Python'\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'" ] } ], "source": [ "d2 = {[1,2,3]: 'Python'}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "d2 = {(1,2,3): 'Python'}" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{(1, 2, 3): 'Python'}\n" ] } ], "source": [ "print(d2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing the pairs from the dict" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'1234567890': ['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210'], '9876543210': ['Python', 'Earth', '01/01/1994', '0123456789']}\n" ] } ], "source": [ "print(d1)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210']\n" ] } ], "source": [ "print(d1['1234567890'])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "'123456789'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0md1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'123456789'\u001b[0m\u001b[1;33m]\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: '123456789'" ] } ], "source": [ "print(d1['123456789'])" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'1234567890': ['Apssdc', 'Tadepalli', '01/01/2014', '1234567890'], '9876543210': ['Python', 'Earth', '01/01/1994', '0123456789'], '123456789': ['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210']}\n" ] } ], "source": [ "d1['1234567890'] = ['Apssdc', 'Tadepalli', '01/01/2014', '1234567890']\n", "d1['123456789'] = ['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210']\n", "\n", "\n", "print(d1)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Apssdc', 'Tadepalli', '01/01/2014', '1234567890']\n" ] } ], "source": [ "print(d1['1234567890'])" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "01/01/2014\n" ] } ], "source": [ "print(d1['1234567890'][2])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'1234567890': ['Apssdc', 'Tadepalli', '07/07/2014', '1234567890'], '9876543210': ['Python', 'Earth', '01/01/1994', '0123456789'], '123456789': ['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210']}\n" ] } ], "source": [ "d1['1234567890'][2] = '07/07/2014'\n", "\n", "print(d1)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'1234567890': ['Apssdc', 'Tadepalli', '07/07/2014', '95135741236'], '9876543210': ['Python', 'Earth', '01/01/1994', '0123456789'], '123456789': ['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210']}\n" ] } ], "source": [ "d1['1234567890'][3] = '95135741236'\n", "\n", "print(d1)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'1234567890': ['01/01/2014', '1234567890', 'Apssdc', 'Tadepalli'], '9876543210': ['Python', 'Earth', '01/01/1994', '0123456789'], '123456789': ['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210']}\n" ] } ], "source": [ "d1['1234567890'] = sorted(d1['1234567890'])\n", "\n", "print(d1)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "d1 = {123:456, 132:344, 123:456}" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{123: 456, 132: 344}\n" ] } ], "source": [ "print(d1)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "456\n" ] } ], "source": [ "print(d1[123])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary Methods" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'1234567890': ['Apssdc', 'Tadepalli', '01/01/2014', '1234567890'], '9876543210': ['Python', 'Earth', '01/01/1994', '0123456789'], '123456789': ['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210']}\n" ] } ], "source": [ "print(d1)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_keys(['1234567890', '9876543210', '123456789'])\n" ] } ], "source": [ "print(d1.keys())" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_values([['Apssdc', 'Tadepalli', '01/01/2014', '1234567890'], ['Python', 'Earth', '01/01/1994', '0123456789'], ['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210']])\n" ] } ], "source": [ "print(d1.values())" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dict_items([('1234567890', ['Apssdc', 'Tadepalli', '01/01/2014', '1234567890']), ('9876543210', ['Python', 'Earth', '01/01/1994', '0123456789']), ('123456789', ['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210'])])\n" ] } ], "source": [ "print(d1.items())" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Apssdc', 'Tadepalli', '01/01/2014', '1234567890']" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d1.get('1234567890')" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(d1.get('123'))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Key not Avaliable\n" ] } ], "source": [ "print(d1.get('123', 'Key not Avaliable'))" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Apssdc', 'Tadepalli', '01/01/2014', '1234567890']\n" ] } ], "source": [ "print(d1.get('1234567890', 'Key not Avaliable'))" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "m21 = {'2_1': [25, 55, 65, 77, 80, 60]}\n", "m31 = {'2_1': [35, 55, 65, 77, 80, 60], '3_1': [1,2,4,5,6]}\n", "m32 = {'2_1': [50, 55, 65, 77, 80, 60], '3_2': [6,5,4,3,2,1]}" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'2_1': [25, 55, 65, 77, 80, 60]}\n" ] } ], "source": [ "print(m21)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'2_1': [35, 55, 65, 77, 80, 60], '3_1': [1, 2, 4, 5, 6]}\n" ] } ], "source": [ "m21.update(m31)\n", "print(m21)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'2_1': [50, 55, 65, 77, 80, 60], '3_1': [1, 2, 4, 5, 6], '3_2': [6, 5, 4, 3, 2, 1]}\n" ] } ], "source": [ "m21.update(m32)\n", "\n", "print(m21)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'RollNO': [50, 55, 65, 77, 80, 60], '3_1': [1, 2, 4, 5, 6], '3_2': [6, 5, 4, 3, 2, 1]}\n" ] } ], "source": [ "copy = Mark2_1.copy()\n", "\n", "print(copy)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'2_1': [35, 55, 65, 77, 80, 60], '3_1': [1, 2, 4, 5, 6]}\n" ] } ], "source": [ "m21 = m31.copy()\n", "\n", "print(m21)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{}\n" ] } ], "source": [ "m21.clear()\n", "\n", "print(m21)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 4, 5, 6]\n", "{'2_1': [35, 55, 65, 77, 80, 60]}\n" ] } ], "source": [ "print(m31.pop('3_1'))\n", "\n", "print(m31)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "'3_1'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm31\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'3_1'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm31\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mKeyError\u001b[0m: '3_1'" ] } ], "source": [ "print(m31.pop('3_1'))\n", "\n", "print(m31)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'2_1': [50, 55, 65, 77, 80, 60], '3_2': [6, 5, 4, 3, 2, 1]}\n" ] } ], "source": [ "print(m32)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('3_2', [6, 5, 4, 3, 2, 1])\n", "{'2_1': [50, 55, 65, 77, 80, 60]}\n" ] } ], "source": [ "print(m32.popitem())\n", "\n", "print(m32)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "'popitem(): dictionary is empty'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm21\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpopitem\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm21\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mKeyError\u001b[0m: 'popitem(): dictionary is empty'" ] } ], "source": [ "print(m21.popitem())\n", "\n", "print(m21)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3\n" ] } ], "source": [ "li = [1,2,3]\n", "\n", "\n", "a, b, c = li\n", "\n", "print(a,b,c)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "too many values to unpack (expected 2)", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mValueError\u001b[0m: too many values to unpack (expected 2)" ] } ], "source": [ "a,b = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "not enough values to unpack (expected 4, got 3)", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0md\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mValueError\u001b[0m: not enough values to unpack (expected 4, got 3)" ] } ], "source": [ "a,b,c,d = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 1 2 3\n" ] } ], "source": [ "a,b,c,d = (1,1,2,3)\n", "\n", "print(a,b,c,d)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 1, 2, 3]\n" ] } ], "source": [ "li2 = [a,b,c,d]\n", "\n", "print(li2)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "print(len(d1))" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9876543210\n" ] } ], "source": [ "print(max(d1))" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "123456789\n" ] } ], "source": [ "print(min(d1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iterating" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1234567890\n", "9876543210\n", "123456789\n" ] } ], "source": [ "for pair in d1:\n", " print(pair)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Apssdc', 'Tadepalli', '01/01/2014', '1234567890']\n", "['Python', 'Earth', '01/01/1994', '0123456789']\n", "['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210']\n" ] } ], "source": [ "for val in d1.values():\n", " print(val)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Apssdc', 'Tadepalli', '01/01/2014', '1234567890']\n", "['Python', 'Earth', '01/01/1994', '0123456789']\n", "['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210']\n" ] } ], "source": [ "for pair in d1:\n", " print(d1[pair])" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Apssdc', 'Tadepalli', '01/01/2014', '1234567890']\n", "['Python', 'Earth', '01/01/1994', '0123456789']\n", "['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210']\n" ] } ], "source": [ "for pair in d1:\n", " print(d1.get(pair))" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('1234567890', ['Apssdc', 'Tadepalli', '01/01/2014', '1234567890'])\n", "('9876543210', ['Python', 'Earth', '01/01/1994', '0123456789'])\n", "('123456789', ['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210'])\n" ] } ], "source": [ "for pair in d1.items():\n", " print(pair)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Apssdc', 'Tadepalli', '01/01/2014', '1234567890']\n", "['Python', 'Earth', '01/01/1994', '0123456789']\n", "['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210']\n" ] } ], "source": [ "for pair in d1.items():\n", " print(pair[1])" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1234567890\n", "['Apssdc', 'Tadepalli', '01/01/2014', '1234567890']\n", "**********\n", "9876543210\n", "['Python', 'Earth', '01/01/1994', '0123456789']\n", "**********\n", "123456789\n", "['APSSDC', 'Visakhapatnam', '01/01/2000', '9876543210']\n", "**********\n" ] } ], "source": [ "for key, value in d1.items():\n", " print(key)\n", " print(value)\n", " print('*' * 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "{1:1, 2:4, 3:9, .......... 100: 10000}" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225, 16: 256, 17: 289, 18: 324, 19: 361, 20: 400, 21: 441, 22: 484, 23: 529, 24: 576, 25: 625, 26: 676, 27: 729, 28: 784, 29: 841, 30: 900, 31: 961, 32: 1024, 33: 1089, 34: 1156, 35: 1225, 36: 1296, 37: 1369, 38: 1444, 39: 1521, 40: 1600, 41: 1681, 42: 1764, 43: 1849, 44: 1936, 45: 2025, 46: 2116, 47: 2209, 48: 2304, 49: 2401, 50: 2500, 51: 2601, 52: 2704, 53: 2809, 54: 2916, 55: 3025, 56: 3136, 57: 3249, 58: 3364, 59: 3481, 60: 3600, 61: 3721, 62: 3844, 63: 3969, 64: 4096, 65: 4225, 66: 4356, 67: 4489, 68: 4624, 69: 4761, 70: 4900, 71: 5041, 72: 5184, 73: 5329, 74: 5476, 75: 5625, 76: 5776, 77: 5929, 78: 6084, 79: 6241, 80: 6400, 81: 6561, 82: 6724, 83: 6889, 84: 7056, 85: 7225, 86: 7396, 87: 7569, 88: 7744, 89: 7921, 90: 8100, 91: 8281, 92: 8464, 93: 8649, 94: 8836, 95: 9025, 96: 9216, 97: 9409, 98: 9604, 99: 9801, 100: 10000}\n" ] } ], "source": [ "sq = {}\n", "\n", "\n", "for i in range(1, 101):\n", " sq[i] = i ** 2\n", "\n", " \n", "print(sq)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4--16--36--64--100--144--196--256--324--400--484--576--676--784--900--1024--1156--1296--1444--1600--1764--1936--2116--2304--2500--2704--2916--3136--3364--3600--3844--4096--4356--4624--4900--5184--5476--5776--6084--6400--6724--7056--7396--7744--8100--8464--8836--9216--9604--10000--" ] } ], "source": [ "for key in sq:\n", " if key % 2 == 0:\n", " print(sq[key], end = '--')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Task\n", "\n", "\n", "- Character frequency inside the string\n", "- word frequency inside the string" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [], "source": [ "s = \"\"\"Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Wikipedia\n", "Developer: Python Software Foundation\n", "Stable release: 3.9.5 / 3 May 2021; 19 days ago\n", "Preview release: 3.10.0b1 / 3 May 2021; 19 days ago\n", "Typing discipline: Duck, dynamic, strong typing; gradual (since 3.5, but ignored in CPython)\n", "First appeared: February 1991; 30 years ago\n", "Paradigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "char, char_count\n", "\n", "\n", "- [['p', 5], ['a', 10]]\n", "- {'p':5, 'a':10}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Contact Application\n", "\n", "\n", "\n", "{'Name': ['Mobile1','mobile1', 'email', 'DOB', 'website']}\n", "\n", "\n", "Take the input from the user\n", "- 1. Create a contact -> Key:Value\n", "- 2. add the contact to existing contact -> Key -> Name\n", "- 3. edit the contact -> Key -> value\n", "- 4. delete the contact -> Name\n", "- 5. view the contact -> Name" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Press the options below\n", "1. Create a contact\n", "2. add the contact to existing contact\n", "3. edit the contact\n", "4. delete the contact2\n" ] } ], "source": [ "option = input(\"\"\"Press the options below\n", "1. Create a contact\n", "2. add the contact to existing contact\n", "3. edit the contact\n", "4. delete the contact\"\"\")" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [], "source": [ "contact = {}" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter Contact NamePython\n", "Enter detail with space sep9876543210 python@gmail.com 01-01-1997\n" ] } ], "source": [ "if option == '1':\n", " key = input(\"Enter Contact Name\")\n", " value = input(\"Enter detail with space sep\").split()\n", " contact[key] = value" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Python': ['9876543210', 'python@gmail.com', '01-01-1997']}\n" ] } ], "source": [ "print(contact)" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter the name to add the existingPython\n", "Enter the data to update1234567890\n" ] } ], "source": [ "if option == '2':\n", " name = input('Enter the name to add the existing')\n", " if name in contact:\n", " data = input(\"Enter the data to update\").split()\n", " contact[name].extend(data)\n", " else:\n", " print(name, \"contact is not avaliable\")" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Python': ['9876543210', 'python@gmail.com', '01-01-1997', '1234567890']}\n" ] } ], "source": [ "print(contact)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python\n" ] } ], "source": [ "name = input()\n", "\n", "if name in contact.keys():\n", " print(contact.pop(name))\n", "else:\n", " print('name is not avaliable')" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{}\n" ] } ], "source": [ "print(contact)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Day10 Outcomes\n", "\n", "1. Dictionary\n", "2. Dictoinary Methods" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 4 }