|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 1. Strings"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Strings are ordered text based data which are represented by enclosing the same in single/double/triple quotes."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "10\n"
- ]
- }
- ],
- "source": [
- "String0 = 'Taj Mahal is beautiful'\n",
- "String1 = \"Taj Mahal is beautiful\"\n",
- "String2 = '''Taj Mahal\n",
- "is\n",
- "beautiful'''"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Taj Mahal is beautiful <class 'str'>\n",
- "Taj Mahal is beautiful <class 'str'>\n",
- "Taj Mahal\n",
- "is\n",
- "beautiful <class 'str'>\n"
- ]
- }
- ],
- "source": [
- "print(String0, type(String0))\n",
- "print(String1, type(String1))\n",
- "print(String2, type(String2))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "String Indexing and Slicing are similar to Lists which was explained in detail earlier."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "M\n",
- "Mahal is beautiful\n"
- ]
- }
- ],
- "source": [
- "print(String0[4])\n",
- "print(String0[4:])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 1.1 Built-in Functions"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**find( )** function returns the index value of the given data that is to found in the string. If it is not found it returns **-1**. Remember to not confuse the returned -1 for reverse indexing value."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "7\n",
- "-1\n"
- ]
- }
- ],
- "source": [
- "print(String0.find('al'))\n",
- "print(String0.find('am'))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "The index value returned is the index of the first element in the input data."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "a\n"
- ]
- }
- ],
- "source": [
- "print(String0[7])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "One can also input **find( )** function between which index values it has to search."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2\n",
- "2\n"
- ]
- }
- ],
- "source": [
- "print(String0.find('j',1))\n",
- "print(String0.find('j',1,3))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**capitalize( )** is used to capitalize the first element in the string."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Observe the first letter in this sentence. can you change this sentence\n"
- ]
- }
- ],
- "source": [
- "String3 = 'observe the first letter in this sentence. can you change this sentence'\n",
- "print(String3.capitalize())"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**center( )** is used to center align the string by specifying the field width."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "' Taj Mahal is beautiful '"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "String0.center(70)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "One can also fill the left out spaces with any other character."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'------------------------Taj Mahal is beautiful------------------------'"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "String0.center(70,'-')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**zfill( )** is used for zero padding by specifying the field width."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'00000000Taj Mahal is beautiful'"
- ]
- },
- "execution_count": 12,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "String0.zfill(30)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**expandtabs( )** allows you to change the spacing of the tab character. '\\t' which is by default set to 8 spaces."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "h\te\tl\tl\to\n",
- "h e l l o\n",
- "h e l l o\n"
- ]
- }
- ],
- "source": [
- "s = 'h\\te\\tl\\tl\\to'\n",
- "print(s)\n",
- "print(s.expandtabs(1))\n",
- "print(s.expandtabs())"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "collapsed": true
- },
- "source": [
- "**index( )** works the same way as **find( )** function the only difference is find returns '-1' when the input element is not found in the string but **index( )** function throws a ValueError"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0\n",
- "4\n"
- ]
- },
- {
- "ename": "ValueError",
- "evalue": "substring not found",
- "output_type": "error",
- "traceback": [
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
- "\u001b[0;32m<ipython-input-14-a7d6b97b4839>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Taj'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[0;31mValueError\u001b[0m: substring not found"
- ]
- }
- ],
- "source": [
- "print(String0.index('Taj'))\n",
- "print(String0.index('Mahal',0))\n",
- "print(String0.index('Mahal',10,20))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**endswith( )** function is used to check if the given string ends with the particular char which is given as input."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "False\n"
- ]
- }
- ],
- "source": [
- "print(String0.endswith('y'))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "The start and stop index values can also be specified."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n",
- "True\n"
- ]
- }
- ],
- "source": [
- "print(String0.endswith('l',0))\n",
- "print(String0.endswith('M',0,5))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**count( )** function counts the number of char in the given string. The start and the stop index can also be specified or left blank. (These are Implicit arguments which will be dealt in functions)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "4\n",
- "2\n"
- ]
- }
- ],
- "source": [
- "print(String0.count('a',0))\n",
- "print(String0.count('a',5,10))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**join( )** function is used add a char in between the elements of the input string."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'*a_a-'"
- ]
- },
- "execution_count": 16,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'a'.join('*_-')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'1\\n2'"
- ]
- },
- "execution_count": 6,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'\\n'.join(['1', '2'])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "'*_-' is the input string and char 'a' is added in between each element"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**join( )** function can also be used to convert a list into a string."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['T', 'a', 'j', ' ', 'M', 'a', 'h', 'a', 'l', ' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n",
- "Taj Mahal is beautiful\n"
- ]
- }
- ],
- "source": [
- "a = list(String0)\n",
- "print(a)\n",
- "b = ''.join(a)\n",
- "print(b)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Before converting it into a string **join( )** function can be used to insert any char in between the list elements."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- " /i/s/ /b/e/a/u/t/i/f/u/l\n"
- ]
- }
- ],
- "source": [
- "c = '/'.join(a)[18:]\n",
- "print(c)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**split( )** function is used to convert a string back to a list. Think of it as the opposite of the **join()** function."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[' ', 'i', 's', ' ', 'b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n"
- ]
- }
- ],
- "source": [
- "d = c.split('/')\n",
- "print(d)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "In **split( )** function one can also specify the number of times you want to split the string or the number of elements the new returned list should conatin. The number of elements is always one more than the specified number this is because it is split the number of times specified."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[' ', 'i', 's', ' /b/e/a/u/t/i/f/u/l']\n",
- "4\n"
- ]
- }
- ],
- "source": [
- "e = c.split('/',3)\n",
- "print(e)\n",
- "print(len(e))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**lower( )** converts any capital letter to small letter."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Taj Mahal is beautiful\n",
- "taj mahal is beautiful\n"
- ]
- }
- ],
- "source": [
- "print(String0)\n",
- "print(String0.lower())"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**upper( )** converts any small letter to capital letter."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'TAJ MAHAL IS BEAUTIFUL'"
- ]
- },
- "execution_count": 12,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "String0.upper()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**replace( )** function replaces the element with another element."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'Bengaluru is beautiful'"
- ]
- },
- "execution_count": 22,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "String0.replace('Taj Mahal','Bengaluru')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**strip( )** function is used to delete elements from the right end and the left end which is not required."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [],
- "source": [
- "f = ' hello '"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "If no char is specified then it will delete all the spaces that is present in the right and left hand side of the data."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'hello'"
- ]
- },
- "execution_count": 17,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "f.strip()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**strip( )** function, when a char is specified then it deletes that char if it is present in the two ends of the specified string."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [],
- "source": [
- "f = ' ***----hello---******* '"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "' ***----hello---******* '"
- ]
- },
- "execution_count": 21,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "f.strip('*')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "The asterisk had to be deleted but is not. This is because there is a space in both the right and left hand side. So in strip function. The characters need to be inputted in the specific order in which they are present."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "----hello---\n",
- "hello\n"
- ]
- }
- ],
- "source": [
- "print(f.strip(' *'))\n",
- "print(f.strip(' *-'))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**lstrip( )** and **rstrip( )** function have the same functionality as strip function but the only difference is **lstrip( )** deletes only towards the left side and **rstrip( )** towards the right."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "----hello---******* \n",
- " ***----hello---\n"
- ]
- }
- ],
- "source": [
- "print(f.lstrip(' *'))\n",
- "print(f.rstrip(' *'))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## 2. Dictionaries"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Dictionaries are more used like a database because here you can index a particular sequence with your user defined string."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "To define a dictionary, equate a variable to { } or dict()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'dict'> <class 'dict'>\n"
- ]
- }
- ],
- "source": [
- "d0 = {}\n",
- "d1 = dict()\n",
- "print(type(d0), type(d1))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Dictionary works somewhat like a list but with an added capability of assigning it's own index style."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'OneTwo': 12, 'One': 1}\n"
- ]
- }
- ],
- "source": [
- "d0['One'] = 1\n",
- "d0['OneTwo'] = 12 \n",
- "print(d0)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'key2': [1, 2, 4], 'key1': 1, 3: (1, 4, 6)}\n"
- ]
- }
- ],
- "source": [
- "d1 = {\"key1\":1, \"key2\":[1,2,4], 3:(1, 4, 6)}\n",
- "print(d1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "That is how a dictionary looks like. Now you are able to access '1' by the index value set at 'One'"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1\n"
- ]
- }
- ],
- "source": [
- "print(d0['One'])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Two lists which are related can be merged to form a dictionary."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "names = ['One', 'Two', 'Three', 'Four', 'Five']\n",
- "numbers = [1, 2, 3, 4, 5]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**zip( )** function is used to combine two lists"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'One': 1, 'Four': 4, 'Three': 3, 'Five': 5, 'Two': 2}\n"
- ]
- }
- ],
- "source": [
- "d2 = zip(names,numbers)\n",
- "print(dict(d2))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "The two lists are combined to form a single list and each elements are clubbed with their respective elements from the other list inside a tuple. Tuples because that is what is assigned and the value should not change.\n",
- "\n",
- "Further, To convert the above into a dictionary. **dict( )** function is used."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'One': 1, 'Four': 4, 'Three': 3, 'Five': 5, 'Two': 2}\n"
- ]
- }
- ],
- "source": [
- "d2 = zip(names,numbers)\n",
- "\n",
- "a1 = dict(d2)\n",
- "print(a1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 2.1 Built-in Functions"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**clear( )** function is used to erase the entire database that was created."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{}\n"
- ]
- }
- ],
- "source": [
- "a1.clear()\n",
- "print(a1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Dictionary can also be built using loops."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'One': 1, 'Four': 4, 'Three': 3, 'Five': 5, 'Two': 2}\n"
- ]
- }
- ],
- "source": [
- "a1 = {names[i]:numbers[i] for i in range(len(names))}\n",
- "print(a1)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'One': 1, 'Four': 4, 'Three': 3, 'Five': 5, 'Two': 2}\n"
- ]
- }
- ],
- "source": [
- "for i in range(len(names)):\n",
- " a1[names[i]] = numbers[i]\n",
- "print(a1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**values( )** function returns a list with all the assigned values in the dictionary."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "dict_values([1, 4, 3, 5, 2])"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "a1.values()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**keys( )** function returns all the index or the keys to which contains the values that it was assigned to."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "dict_keys(['One', 'Four', 'Three', 'Five', 'Two'])"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "a1.keys()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**items( )** is returns a list containing both the list but each element in the dictionary is inside a tuple. This is same as the result that was obtained when zip function was used."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ One] 1\n",
- "[ Four] 4\n",
- "[ Three] 3\n",
- "[ Five] 5\n",
- "[ Two] 2\n"
- ]
- }
- ],
- "source": [
- "a1.items()\n",
- "\n",
- "for (k,v) in a1.items():\n",
- " print(\"[%6s] %d\" % (k, v))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**pop( )** function is used to get the remove that particular element and this removed element can be assigned to a new variable. But remember only the value is stored and not the key. Because the is just a index value."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'One': 1, 'Three': 3, 'Five': 5, 'Two': 2}\n",
- "4\n"
- ]
- }
- ],
- "source": [
- "a2 = a1.pop('Four')\n",
- "print(a1)\n",
- "print(a2)"
- ]
- }
- ],
- "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.6.8"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
- }
|