|
24 | 24 | "## Dictionary:\n",
|
25 | 25 | "Dictionaries are unordered collection of data.\n",
|
26 | 26 | "They are key-value mapping. \n",
|
27 | | - "Represented by Curly braces '{ }' \n", |
28 | | - "`dictionary : {1:10, 2:20, 3:30, 4:'Hello', 5:-2.0}`\n", |
| 27 | + "Represented by Curly braces `{}` \n", |
| 28 | + "\n", |
| 29 | + "```python\n", |
| 30 | + "dictionary : {1: 10, 2: 20, 3: 30, 4: 'Hello', 5: -2.0}\n", |
| 31 | + "```\n", |
29 | 32 | "\n",
|
30 | 33 | "Defined by - key : value. \n",
|
31 | 34 | "Each key-value map is separated by comma (,)\n",
|
|
45 | 48 | "2. dict()\n",
|
46 | 49 | "\n",
|
47 | 50 | "### 1. '{}'\n",
|
| 51 | + "```python\n", |
48 | 52 | "dict1 = {1:10, 2:20, 3:30}\n",
|
| 53 | + "```\n", |
49 | 54 | "### 2. dict()\n",
|
50 | | - "Syntax: `dict(iterable = None)` \n", |
| 55 | + "Syntax: \n", |
| 56 | + "```python\n", |
| 57 | + "dict(iterable = None)\n", |
| 58 | + "``` \n", |
51 | 59 | "iterable: any data structures listed above given that elements are present as key value mapping. "
|
52 | 60 | ]
|
53 | 61 | },
|
|
130 | 138 | "### 1. len:\n",
|
131 | 139 | "Returns the length of the given iterable.\n",
|
132 | 140 | "\n",
|
133 | | - "Syntax: `len(iterable)`" |
| 141 | + "Syntax: \n", |
| 142 | + "```python\n", |
| 143 | + "len(iterable)\n", |
| 144 | + "```" |
134 | 145 | ]
|
135 | 146 | },
|
136 | 147 | {
|
|
161 | 172 | "### 2. str:\n",
|
162 | 173 | "Returns the string format of the given dictionary.\n",
|
163 | 174 | "\n",
|
164 | | - "Syntax: `str(dict)`" |
| 175 | + "Syntax: \n", |
| 176 | + "```python\n", |
| 177 | + "str(dict)\n", |
| 178 | + "```" |
165 | 179 | ]
|
166 | 180 | },
|
167 | 181 | {
|
|
192 | 206 | "### 3. clear:\n",
|
193 | 207 | "Deletes the items in the dictionary.\n",
|
194 | 208 | "\n",
|
195 | | - "Syntax: `dict.clear()`" |
| 209 | + "Syntax: \n", |
| 210 | + "```python\n", |
| 211 | + "dict.clear()\n", |
| 212 | + "```" |
196 | 213 | ]
|
197 | 214 | },
|
198 | 215 | {
|
|
224 | 241 | "### 4. copy:\n",
|
225 | 242 | "Returns the shallow copy of the given dictionary.\n",
|
226 | 243 | "\n",
|
227 | | - "Syntax: `dict.copy()`" |
| 244 | + "Syntax: \n", |
| 245 | + "```python\n", |
| 246 | + "dict.copy()\n", |
| 247 | + "```" |
228 | 248 | ]
|
229 | 249 | },
|
230 | 250 | {
|
|
256 | 276 | "### 5. fromkeys:\n",
|
257 | 277 | "Returns a new dictionary form the given list of keys. By default the values are set to None.\n",
|
258 | 278 | "\n",
|
259 | | - "Syntax: `dict.fromkeys(sep, val = None)`" |
| 279 | + "Syntax: \n", |
| 280 | + "```python\n", |
| 281 | + "dict.fromkeys(sep, val = None)\n", |
| 282 | + "```" |
260 | 283 | ]
|
261 | 284 | },
|
262 | 285 | {
|
|
310 | 333 | "### 6. get:\n",
|
311 | 334 | "Returns the value of the given key if present else returns the default value given (None by Default).\n",
|
312 | 335 | "\n",
|
313 | | - "Syntax: `dict.get(key, default = None)`" |
| 336 | + "Syntax: \n", |
| 337 | + "```python\n", |
| 338 | + "dict.get(key, default = None)\n", |
| 339 | + "```" |
314 | 340 | ]
|
315 | 341 | },
|
316 | 342 | {
|
|
380 | 406 | "### 7. items:\n",
|
381 | 407 | "Returns the list of key-value pairs in the dictionary.\n",
|
382 | 408 | "\n",
|
383 | | - "Syntax: `dict.items()`" |
| 409 | + "Syntax: \n", |
| 410 | + "```python\n", |
| 411 | + "dict.items()\n", |
| 412 | + "```" |
384 | 413 | ]
|
385 | 414 | },
|
386 | 415 | {
|
|
411 | 440 | "### 8. keys:\n",
|
412 | 441 | "Returns the list of keys present in the dictionary.\n",
|
413 | 442 | "\n",
|
414 | | - "Syntax: `dict.keys()`" |
| 443 | + "Syntax: \n", |
| 444 | + "```python\n", |
| 445 | + "dict.keys()\n", |
| 446 | + "```" |
415 | 447 | ]
|
416 | 448 | },
|
417 | 449 | {
|
|
442 | 474 | "### 9. values:\n",
|
443 | 475 | "Returns the list of values present on the dictionary.\n",
|
444 | 476 | "\n",
|
445 | | - "Syntax: `dict.values()`" |
| 477 | + "Syntax: \n", |
| 478 | + "```python\n", |
| 479 | + "dict.values()\n", |
| 480 | + "```" |
446 | 481 | ]
|
447 | 482 | },
|
448 | 483 | {
|
|
473 | 508 | "### 10. update:\n",
|
474 | 509 | "Adds the key-value pairs in second dictionary to the first.\n",
|
475 | 510 | "\n",
|
476 | | - "Syntax: `dict.update(dict)`" |
| 511 | + "Syntax: \n", |
| 512 | + "```python\n", |
| 513 | + "dict.update(dict)\n", |
| 514 | + "```" |
477 | 515 | ]
|
478 | 516 | },
|
479 | 517 | {
|
|
506 | 544 | "### 11. pop:\n",
|
507 | 545 | "Removes and returns the value of given key. If the key is absent, Error is raised if default is not given.\n",
|
508 | 546 | "\n",
|
509 | | - "Syntax: `dict.pop(key, [default])`" |
| 547 | + "Syntax: \n", |
| 548 | + "```python\n", |
| 549 | + "dict.pop(key, [default])\n", |
| 550 | + "```" |
510 | 551 | ]
|
511 | 552 | },
|
512 | 553 | {
|
|
597 | 638 | "### 12. popitem:\n",
|
598 | 639 | "Removes and returns the last key-value pair of the dictionary.\n",
|
599 | 640 | "\n",
|
600 | | - "Syntax: `dict.popitem()`" |
| 641 | + "Syntax: \n", |
| 642 | + "```python\n", |
| 643 | + "dict.popitem()\n", |
| 644 | + "```" |
601 | 645 | ]
|
602 | 646 | },
|
603 | 647 | {
|
|
636 | 680 | "### 13. setdefault:\n",
|
637 | 681 | "Sets the default value to key if the key is absent.\n",
|
638 | 682 | "\n",
|
639 | | - "Syntax: `dict.setdefault(key, value)`" |
| 683 | + "Syntax: \n", |
| 684 | + "```python\n", |
| 685 | + "dict.setdefault(key, value)\n", |
| 686 | + "```" |
640 | 687 | ]
|
641 | 688 | },
|
642 | 689 | {
|
|
668 | 715 | "## Accessing elements:\n",
|
669 | 716 | "The values in the dictionary are accessed by key.\n",
|
670 | 717 | "\n",
|
671 | | - "Syntax: `dictionary[key]`" |
| 718 | + "Syntax: \n", |
| 719 | + "```python\n", |
| 720 | + "dictionary[key]\n", |
| 721 | + "```" |
672 | 722 | ]
|
673 | 723 | },
|
674 | 724 | {
|
|
742 | 792 | "## Adding or Modifying items:\n",
|
743 | 793 | "Items can be added or modified by using keys.\n",
|
744 | 794 | "\n",
|
745 | | - "Syntax: `dictaionary[key] = value` " |
| 795 | + "If the key already exists, the value is replcaed by the new value. If not, new key-value pair is created\n", |
| 796 | + "\n", |
| 797 | + "Syntax: \n", |
| 798 | + "```python\n", |
| 799 | + "dictaionary[key] = value\n", |
| 800 | + "``` " |
746 | 801 | ]
|
747 | 802 | },
|
748 | 803 | {
|
|
775 | 830 | "## Delete Elements:\n",
|
776 | 831 | "Elements can be deleted by using the *del* keyword.\n",
|
777 | 832 | "\n",
|
778 | | - "Syntax: `del dict[key]`" |
| 833 | + "Syntax: \n", |
| 834 | + "```python\n", |
| 835 | + "del dict[key]\n", |
| 836 | + "```" |
779 | 837 | ]
|
780 | 838 | },
|
781 | 839 | {
|
|
901 | 959 | "## Dictionaries and list Comprehension:\n",
|
902 | 960 | "Dictionaries can be defined by comprehensions.\n",
|
903 | 961 | "\n",
|
904 | | - "Syntax: `dict = {key: value (loops)}`" |
| 962 | + "Syntax: \n", |
| 963 | + "```python\n", |
| 964 | + "dict = {key: value (loops)}\n", |
| 965 | + "```" |
905 | 966 | ]
|
906 | 967 | },
|
907 | 968 | {
|
|
973 | 1034 | "name": "python",
|
974 | 1035 | "nbconvert_exporter": "python",
|
975 | 1036 | "pygments_lexer": "ipython3",
|
976 | | - "version": "3.8.6" |
| 1037 | + "version": "3.9.6" |
977 | 1038 | }
|
978 | 1039 | },
|
979 | 1040 | "nbformat": 4,
|
|
0 commit comments