Ace-xvi/Generative_AI
020
1{2 "cells": [3 {4 "cell_type": "markdown",5 "id": "de008568",6 "metadata": {},7 "source": [8 "# Agenda\n",9 "- Recap\n",10 "- for loop\n",11 "- while loop\n",12 "- single line if else statement\n",13 "- list comprehensions\n",14 "- dictionary comprehensions\n",15 "- Functions"16 ]17 },18 {19 "cell_type": "markdown",20 "id": "5512c46c",21 "metadata": {},22 "source": [23 "### If else boolean interpretation "24 ]25 },26 {27 "cell_type": "code",28 "execution_count": 1,29 "id": "3eeee664",30 "metadata": {},31 "outputs": [],32 "source": [33 "st = 'python is easy to understand'"34 ]35 },36 {37 "cell_type": "code",38 "execution_count": 2,39 "id": "f4b718c1",40 "metadata": {},41 "outputs": [42 {43 "data": {44 "text/plain": [45 "False"46 ]47 },48 "execution_count": 2,49 "metadata": {},50 "output_type": "execute_result"51 }52 ],53 "source": [54 "st.startswith('z')"55 ]56 },57 {58 "cell_type": "code",59 "execution_count": 3,60 "id": "f4eefdfc",61 "metadata": {},62 "outputs": [63 {64 "data": {65 "text/plain": [66 "0"67 ]68 },69 "execution_count": 3,70 "metadata": {},71 "output_type": "execute_result"72 }73 ],74 "source": [75 "st.index('p')"76 ]77 },78 {79 "cell_type": "code",80 "execution_count": 7,81 "id": "501e9ee8",82 "metadata": {},83 "outputs": [84 {85 "data": {86 "text/plain": [87 "5"88 ]89 },90 "execution_count": 7,91 "metadata": {},92 "output_type": "execute_result"93 }94 ],95 "source": [96 "len('Hello')"97 ]98 },99 {100 "cell_type": "code",101 "execution_count": 20,102 "id": "48aeabc9",103 "metadata": {},104 "outputs": [],105 "source": [106 "### Problem statement: Remove the empty string from a list"107 ]108 },109 {110 "cell_type": "code",111 "execution_count": 30,112 "id": "e62462d1",113 "metadata": {},114 "outputs": [],115 "source": [116 "li = ['Python','','Java','','C','C++','Scala','']"117 ]118 },119 {120 "cell_type": "code",121 "execution_count": 29,122 "id": "e66c99c9",123 "metadata": {},124 "outputs": [125 {126 "name": "stdout",127 "output_type": "stream",128 "text": [129 "0\n",130 "6\n"131 ]132 }133 ],134 "source": [135 "print(len(''))\n",136 "print(len('Python'))"137 ]138 },139 {140 "cell_type": "code",141 "execution_count": 17,142 "id": "8e7b7a92",143 "metadata": {},144 "outputs": [],145 "source": [146 "# Using jhgj_989 to demonstrate any variable name can be used to carry elements of iterator\n",147 "li2 = []\n",148 "for jhgj_989 in li:\n",149 " if len(jhgj_989):\n",150 " li2.append(jhgj_989)"151 ]152 },153 {154 "cell_type": "code",155 "execution_count": 19,156 "id": "6ca638f4",157 "metadata": {},158 "outputs": [159 {160 "name": "stdout",161 "output_type": "stream",162 "text": [163 "['Python', 'Java', 'C', 'C++', 'Scala']\n"164 ]165 }166 ],167 "source": [168 "print(li2)"169 ]170 },171 {172 "cell_type": "code",173 "execution_count": 12,174 "id": "76981022",175 "metadata": {},176 "outputs": [],177 "source": [178 "li3=[]\n",179 "for i in li:\n",180 " if i:\n",181 " li3.append(i)"182 ]183 },184 {185 "cell_type": "code",186 "execution_count": null,187 "id": "70b2037e",188 "metadata": {},189 "outputs": [],190 "source": [191 "### Problem statement : Remove Java"192 ]193 },194 {195 "cell_type": "code",196 "execution_count": 21,197 "id": "a6a1e70b",198 "metadata": {},199 "outputs": [],200 "source": [201 "li2.remove('Java')"202 ]203 },204 {205 "cell_type": "code",206 "execution_count": null,207 "id": "2b8b9372",208 "metadata": {},209 "outputs": [],210 "source": [211 "### Problem statement: Remove empty string and Java"212 ]213 },214 {215 "cell_type": "code",216 "execution_count": 27,217 "id": "cf2ce65b",218 "metadata": {},219 "outputs": [],220 "source": [221 "li = ['Python','','Java','','C','C++','Scala','']\n",222 "li4= []\n",223 "for i in li:\n",224 " if i and i!='Java':\n",225 " li4.append(i)"226 ]227 },228 {229 "cell_type": "code",230 "execution_count": 28,231 "id": "b8a35cc4",232 "metadata": {},233 "outputs": [234 {235 "data": {236 "text/plain": [237 "['Python', 'C', 'C++', 'Scala']"238 ]239 },240 "execution_count": 28,241 "metadata": {},242 "output_type": "execute_result"243 }244 ],245 "source": [246 "li4"247 ]248 },249 {250 "cell_type": "markdown",251 "id": "9b655f75",252 "metadata": {},253 "source": [254 "### if else boolean interpretation\n",255 "- Interpreted as True\n",256 " - Boolean True\n",257 " - Any non empty datatype\n",258 " - Any non zero integer or float or complex\n",259 "- Interpreted as False\n",260 " - Boolean False\n",261 " - Any empty datatype\n",262 " - zero written as integer or float or complex"263 ]264 },265 {266 "cell_type": "code",267 "execution_count": 31,268 "id": "7501c17c",269 "metadata": {},270 "outputs": [],271 "source": [272 "### For loop recap"273 ]274 },275 {276 "cell_type": "code",277 "execution_count": 42,278 "id": "127abdec",279 "metadata": {},280 "outputs": [],281 "source": [282 "li = [1,2,44,'ty',43,'a','','']"283 ]284 },285 {286 "cell_type": "code",287 "execution_count": 43,288 "id": "7bcc4fde",289 "metadata": {},290 "outputs": [291 {292 "name": "stdout",293 "output_type": "stream",294 "text": [295 "1\n",296 "2\n",297 "44\n",298 "ty\n",299 "43\n",300 "a\n",301 "\n"302 ]303 }304 ],305 "source": [306 "for ytutu_55 in li:\n",307 " print(ytutu_55)"308 ]309 },310 {311 "cell_type": "code",312 "execution_count": 44,313 "id": "3fffaac6",314 "metadata": {},315 "outputs": [],316 "source": [317 "# Simply append every element to new list\n",318 "li2 = []\n",319 "for i in li:\n",320 " li2.append(i)"321 ]322 },323 {324 "cell_type": "code",325 "execution_count": 45,326 "id": "d295b8c5",327 "metadata": {},328 "outputs": [329 {330 "data": {331 "text/plain": [332 "[1, 2, 44, 'ty', 43, 'a', '']"333 ]334 },335 "execution_count": 45,336 "metadata": {},337 "output_type": "execute_result"338 }339 ],340 "source": [341 "li"342 ]343 },344 {345 "cell_type": "code",346 "execution_count": 46,347 "id": "ad555338",348 "metadata": {},349 "outputs": [350 {351 "data": {352 "text/plain": [353 "[1, 2, 44, 'ty', 43, 'a', '']"354 ]355 },356 "execution_count": 46,357 "metadata": {},358 "output_type": "execute_result"359 }360 ],361 "source": [362 "li2"363 ]364 },365 {366 "cell_type": "code",367 "execution_count": 58,368 "id": "5b40e78f",369 "metadata": {},370 "outputs": [371 {372 "name": "stdout",373 "output_type": "stream",374 "text": [375 "Python\n",376 "Java\n",377 "Java\n",378 "\n",379 "C\n",380 "C++\n",381 "Scala\n",382 "\n"383 ]384 }385 ],386 "source": [387 "li = ['Python','Java','Java','','C','C++','Scala','']\n",388 "li3 = []\n",389 "for xyz in li:\n",390 " print(xyz)\n",391 " if len(xyz)==0:\n",392 " pass\n",393 " else:\n",394 " li3.append(xyz)"395 ]396 },397 {398 "cell_type": "code",399 "execution_count": 59,400 "id": "2168fa91",401 "metadata": {},402 "outputs": [403 {404 "data": {405 "text/plain": [406 "['Python', 'Java', 'Java', 'C', 'C++', 'Scala']"407 ]408 },409 "execution_count": 59,410 "metadata": {},411 "output_type": "execute_result"412 }413 ],414 "source": [415 "li3"416 ]417 },418 {419 "cell_type": "code",420 "execution_count": 57,421 "id": "cdc74e0f",422 "metadata": {},423 "outputs": [424 {425 "ename": "IndentationError",426 "evalue": "unexpected indent (3660117040.py, line 5)",427 "output_type": "error",428 "traceback": [429 "\u001b[1;36m Input \u001b[1;32mIn [57]\u001b[1;36m\u001b[0m\n\u001b[1;33m print(i)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unexpected indent\n"430 ]431 }432 ],433 "source": [434 "li = [1,2,3,5,6,88,56]\n",435 "for i in li:\n",436 " print(i)\n",437 "print(i)\n",438 " print(i)"439 ]440 },441 {442 "cell_type": "markdown",443 "id": "337e7aba",444 "metadata": {},445 "source": [446 "### if else boolean interpretation\n",447 "- Interpreted as True\n",448 " - Boolean True\n",449 " - Any non empty datatype\n",450 " - Any non zero integer or float or complex\n",451 "- Interpreted as False\n",452 " - Boolean False\n",453 " - Any empty datatype\n",454 " - zero written as integer or float or complex"455 ]456 },457 {458 "cell_type": "code",459 "execution_count": 84,460 "id": "6f41bf05",461 "metadata": {},462 "outputs": [463 {464 "name": "stdout",465 "output_type": "stream",466 "text": [467 "['Python', 'Java', 'Java', 'C', 'C++', 'Scala']\n"468 ]469 }470 ],471 "source": [472 "### More eloborate way of writing code for better understanding for beginners\n",473 "li = ['Python','Java','Java','','C','C++','Scala','']\n",474 "li3 = []\n",475 "for xyz in li:\n",476 " if len(xyz)==0:\n",477 " pass\n",478 " else:\n",479 " li3.append(xyz)\n",480 "print(li3)"481 ]482 },483 {484 "cell_type": "code",485 "execution_count": 85,486 "id": "8fc84024",487 "metadata": {},488 "outputs": [489 {490 "name": "stdout",491 "output_type": "stream",492 "text": [493 "['Python', 'Java', 'Java', 'C', 'C++', 'Scala']\n"494 ]495 }496 ],497 "source": [498 "li = ['Python','Java','Java','','C','C++','Scala','']\n",499 "li3 = []\n",500 "for xyz in li:\n",501 " if len(xyz)!=0: # if takes Boolean to evaluate condition\n",502 " li3.append(xyz)\n",503 "print(li3)"504 ]505 },506 {507 "cell_type": "code",508 "execution_count": 86,509 "id": "6b4b6e38",510 "metadata": {},511 "outputs": [512 {513 "name": "stdout",514 "output_type": "stream",515 "text": [516 "['Python', 'Java', 'Java', 'C', 'C++', 'Scala']\n"517 ]518 }519 ],520 "source": [521 "li = ['Python','Java','Java','','C','C++','Scala','']\n",522 "li3 = []\n",523 "for xyz in li:\n",524 " if len(xyz): # if takes integer to evaluate condition - 0 - interpreted as False, 1 - interpreted as True\n",525 " li3.append(xyz)\n",526 "print(li3)"527 ]528 },529 {530 "cell_type": "code",531 "execution_count": 89,532 "id": "cce93eef",533 "metadata": {},534 "outputs": [535 {536 "name": "stdout",537 "output_type": "stream",538 "text": [539 "['Python', 'Java', 'Java', 'C', 'C++', 'Scala']\n"540 ]541 }542 ],543 "source": [544 "li = ['Python','Java','Java','','C','C++','Scala','']\n",545 "li3 = []\n",546 "for xyz in li:\n",547 " if xyz: # if takes weather the variable is empty or not to evaluate condition\n",548 " li3.append(xyz)\n",549 "print(li3)"550 ]551 },552 {553 "cell_type": "code",554 "execution_count": null,555 "id": "d34c7173",556 "metadata": {},557 "outputs": [],558 "source": [559 "#### What is the out put of the below code ?"560 ]561 },562 {563 "cell_type": "code",564 "execution_count": 91,565 "id": "0542c840",566 "metadata": {},567 "outputs": [],568 "source": [569 "li = ['',[5],'Java',0,85,'C',' ',True,False,{1,2},'0',0.00,{},(),{'Hi':'Hello'}]"570 ]571 },572 {573 "cell_type": "code",574 "execution_count": 92,575 "id": "b50ac3b9",576 "metadata": {},577 "outputs": [578 {579 "name": "stdout",580 "output_type": "stream",581 "text": [582 "[5]\n",583 "Java\n",584 "85\n",585 "C\n",586 " \n",587 "True\n",588 "{1, 2}\n",589 "0\n",590 "{'Hi': 'Hello'}\n"591 ]592 }593 ],594 "source": [595 "for i in li:\n",596 " if i:\n",597 " print(i)"598 ]599 },600 {601 "cell_type": "code",602 "execution_count": 93,603 "id": "442de460",604 "metadata": {},605 "outputs": [606 {607 "name": "stdout",608 "output_type": "stream",609 "text": [610 "<class 'str'>\n",611 "<class 'list'>\n",612 "[5]\n",613 "<class 'str'>\n",614 "Java\n",615 "<class 'int'>\n",616 "<class 'int'>\n",617 "85\n",618 "<class 'str'>\n",619 "C\n",620 "<class 'str'>\n",621 " \n",622 "<class 'bool'>\n",623 "True\n",624 "<class 'bool'>\n",625 "<class 'set'>\n",626 "{1, 2}\n",627 "<class 'str'>\n",628 "0\n",629 "<class 'float'>\n",630 "<class 'dict'>\n",631 "<class 'tuple'>\n",632 "<class 'dict'>\n",633 "{'Hi': 'Hello'}\n"634 ]635 }636 ],637 "source": [638 "for i in li:\n",639 " print(type(i))\n",640 " if i:\n",641 " print(i)"642 ]643 },644 {645 "cell_type": "markdown",646 "id": "a880a314",647 "metadata": {},648 "source": [649 "### Problem Statement: Write a program to extract elements divisable by both 3 and 4 for the below list list\n",650 "li = [15,8,7,9,15,14,21,12]"651 ]652 },653 {654 "cell_type": "code",655 "execution_count": 97,656 "id": "9e38d45f",657 "metadata": {},658 "outputs": [659 {660 "name": "stdout",661 "output_type": "stream",662 "text": [663 "12\n",664 "24\n"665 ]666 }667 ],668 "source": [669 "# Right solution \n",670 "li = [15,8,7,9,15,14,21,12,24]\n",671 "for i in li:\n",672 " if i%3 ==0 and i%4==0:\n",673 " print(i)"674 ]675 },676 {677 "cell_type": "code",678 "execution_count": 99,679 "id": "e0ae3e55",680 "metadata": {},681 "outputs": [682 {683 "name": "stdout",684 "output_type": "stream",685 "text": [686 "5.0\n",687 "2.6666666666666665\n",688 "2.3333333333333335\n",689 "3.0\n",690 "5.0\n",691 "4.666666666666667\n",692 "7.0\n",693 "4.0\n",694 "8.0\n"695 ]696 }697 ],698 "source": [699 "# Commonly done mistake\n",700 "for i in li:\n",701 " print(i/3)\n",702 " if i/3 and i/4 :\n",703 " print(i)"704 ]705 },706 {707 "cell_type": "code",708 "execution_count": 104,709 "id": "746a8321",710 "metadata": {},711 "outputs": [712 {713 "name": "stdout",714 "output_type": "stream",715 "text": [716 "12\n",717 "24\n"718 ]719 }720 ],721 "source": [722 "# Commonly done mistake\n",723 "for i in li:\n",724 " if not (i%3 + i%4) :\n",725 " print(i)"726 ]727 },728 {729 "cell_type": "code",730 "execution_count": 110,731 "id": "9ec83205",732 "metadata": {},733 "outputs": [734 {735 "data": {736 "text/plain": [737 "False"738 ]739 },740 "execution_count": 110,741 "metadata": {},742 "output_type": "execute_result"743 }744 ],745 "source": [746 "not 8%3 and 4"747 ]748 },749 {750 "cell_type": "code",751 "execution_count": 112,752 "id": "d557cee6",753 "metadata": {},754 "outputs": [755 {756 "name": "stdout",757 "output_type": "stream",758 "text": [759 "8\n",760 "7\n",761 "14\n"762 ]763 }764 ],765 "source": [766 "li = [15,8,7,9,15,14,21,12,24]\n",767 "for xyz in li:\n",768 " if xyz%3 and (4) :\n",769 " print(xyz)"770 ]771 },772 {773 "cell_type": "code",774 "execution_count": 114,775 "id": "83f9a686",776 "metadata": {},777 "outputs": [778 {779 "data": {780 "text/plain": [781 "2"782 ]783 },784 "execution_count": 114,785 "metadata": {},786 "output_type": "execute_result"787 }788 ],789 "source": [790 " 8%3 "791 ]792 },793 {794 "cell_type": "code",795 "execution_count": 115,796 "id": "85a65fba",797 "metadata": {},798 "outputs": [799 {800 "data": {801 "text/plain": [802 "4"803 ]804 },805 "execution_count": 115,806 "metadata": {},807 "output_type": "execute_result"808 }809 ],810 "source": [811 "2 and 4"812 ]813 },814 {815 "cell_type": "code",816 "execution_count": 143,817 "id": "5af977ef",818 "metadata": {},819 "outputs": [820 {821 "name": "stdout",822 "output_type": "stream",823 "text": [824 "p\n",825 "y\n",826 "t\n",827 "h\n",828 "o\n",829 "n\n",830 "\n",831 "6\n"832 ]833 }834 ],835 "source": [836 "#### \n",837 "ppppp = 0\n",838 "for i in 'python':\n",839 " ppppp = ppppp+1\n",840 " print(i)\n",841 "print()\n",842 "print(ppppp)"843 ]844 },845 {846 "cell_type": "code",847 "execution_count": 144,848 "id": "0c56ef99",849 "metadata": {},850 "outputs": [851 {852 "name": "stdout",853 "output_type": "stream",854 "text": [855 "python\n",856 "\n",857 "1\n"858 ]859 }860 ],861 "source": [862 "#### \n",863 "ppp = 0\n",864 "for i in ['python']:\n",865 " ppp = ppp+1\n",866 " print(i)\n",867 "print()\n",868 "print(ppp)"869 ]870 },871 {872 "cell_type": "code",873 "execution_count": 161,874 "id": "e9e9fbd4",875 "metadata": {},876 "outputs": [877 {878 "data": {879 "text/plain": [880 "'Trivindrum'"881 ]882 },883 "execution_count": 161,884 "metadata": {},885 "output_type": "execute_result"886 }887 ],888 "source": [889 "#### \n",890 "states_and_capitals['Kerala']"891 ]892 },893 {894 "cell_type": "code",895 "execution_count": 155,896 "id": "65e50621",897 "metadata": {},898 "outputs": [899 {900 "name": "stdout",901 "output_type": "stream",902 "text": [903 "Karnataka\n",904 "Kerala\n",905 "Tamilnadu\n",906 "Jharkand\n"907 ]908 }909 ],910 "source": [911 "# If dictionary is looped, only keys are carried in the loop variable given\n",912 "states_and_capitals = {'Karnataka':'Banglore','Kerala':'Trivindrum','Tamilnadu':'Chennai','Jharkand':'Ranchi'}\n",913 "for zzz in states_and_capitals:\n",914 " print(zzz)"915 ]916 },917 {918 "cell_type": "code",919 "execution_count": 156,920 "id": "f52c8e0f",921 "metadata": {},922 "outputs": [923 {924 "name": "stdout",925 "output_type": "stream",926 "text": [927 "Banglore\n",928 "Trivindrum\n",929 "Chennai\n",930 "Ranchi\n"931 ]932 }933 ],934 "source": [935 "states_and_capitals = {'Karnataka':'Banglore','Kerala':'Trivindrum','Tamilnadu':'Chennai','Jharkand':'Ranchi'}\n",936 "for zzz in states_and_capitals:\n",937 " print(states_and_capitals[zzz])"938 ]939 },940 {941 "cell_type": "code",942 "execution_count": 158,943 "id": "c0474835",944 "metadata": {},945 "outputs": [946 {947 "name": "stdout",948 "output_type": "stream",949 "text": [950 "Karnataka ---- Banglore\n",951 "Kerala ---- Trivindrum\n",952 "Tamilnadu ---- Chennai\n",953 "Jharkand ---- Ranchi\n"954 ]955 }956 ],957 "source": [958 "states_and_capitals = {'Karnataka':'Banglore','Kerala':'Trivindrum','Tamilnadu':'Chennai','Jharkand':'Ranchi'}\n",959 "for zzz in states_and_capitals:\n",960 " st = zzz + ' ---- '+ states_and_capitals[zzz]\n",961 " print(st)"962 ]963 },964 {965 "cell_type": "markdown",966 "id": "bac57bc9",967 "metadata": {},968 "source": [969 "### Problem statement : print values of keys starting with alphabet K"970 ]971 },972 {973 "cell_type": "code",974 "execution_count": 164,975 "id": "f9126cbb",976 "metadata": {},977 "outputs": [978 {979 "name": "stdout",980 "output_type": "stream",981 "text": [982 "Banglore\n",983 "Trivindrum\n"984 ]985 }986 ],987 "source": [988 "for e in states_and_capitals:\n",989 " if e.startswith('K'):\n",990 " print(states_and_capitals[e])"991 ]992 },993 {994 "cell_type": "markdown",995 "id": "fa5dd054",996 "metadata": {},997 "source": [998 "### Problem statement : print keys whose values starting with alphabet T "999 ]1000 },1001 {1002 "cell_type": "code",1003 "execution_count": 165,1004 "id": "9ddbab62",1005 "metadata": {},1006 "outputs": [1007 {1008 "name": "stdout",1009 "output_type": "stream",1010 "text": [1011 "Kerala\n"1012 ]1013 }1014 ],1015 "source": [1016 "for e in states_and_capitals:\n",1017 " if states_and_capitals[e].startswith('T'):\n",1018 " print(e)"1019 ]1020 },1021 {1022 "cell_type": "markdown",1023 "id": "c77eafe5",1024 "metadata": {},1025 "source": [1026 "### Single line if else or ternary operator"1027 ]1028 },1029 {1030 "cell_type": "code",1031 "execution_count": 179,1032 "id": "d74cc8d4",1033 "metadata": {},1034 "outputs": [],1035 "source": [1036 "# Syntax\n",1037 "## return_when_true if condition else return_when_false"1038 ]1039 },1040 {1041 "cell_type": "code",1042 "execution_count": 185,1043 "id": "a770cadc",1044 "metadata": {},1045 "outputs": [1046 {1047 "data": {1048 "text/plain": [1049 "'Even'"1050 ]1051 },1052 "execution_count": 185,1053 "metadata": {},1054 "output_type": "execute_result"1055 }1056 ],1057 "source": [1058 "a = 22\n",1059 "res = 'Even' if a%2==0 else 'Odd'\n",1060 "res"1061 ]1062 },1063 {1064 "cell_type": "code",1065 "execution_count": 195,1066 "id": "141ad712",1067 "metadata": {},1068 "outputs": [1069 {1070 "name": "stdout",1071 "output_type": "stream",1072 "text": [1073 "['Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd']\n"1074 ]1075 }1076 ],1077 "source": [1078 "even_or_odd = []\n",1079 "for i in range(10):\n",1080 " res ='Even' if i%2==0 else 'Odd'\n",1081 " even_or_odd.append(res)\n",1082 "print(even_or_odd)"1083 ]1084 },1085 {1086 "cell_type": "code",1087 "execution_count": 196,1088 "id": "921b8998",1089 "metadata": {},1090 "outputs": [1091 {1092 "name": "stdout",1093 "output_type": "stream",1094 "text": [1095 "[(0, 'Even'), (1, 'Odd'), (2, 'Even'), (3, 'Odd'), (4, 'Even'), (5, 'Odd'), (6, 'Even'), (7, 'Odd'), (8, 'Even'), (9, 'Odd')]\n"1096 ]1097 }1098 ],1099 "source": [1100 "even_or_odd = []\n",1101 "for i in range(10):\n",1102 " res ='Even' if i%2==0 else 'Odd'\n",1103 " even_or_odd.append((i,res))\n",1104 "print(even_or_odd)"1105 ]1106 },1107 {1108 "cell_type": "code",1109 "execution_count": 197,1110 "id": "fd62032a",1111 "metadata": {},1112 "outputs": [1113 {1114 "name": "stdout",1115 "output_type": "stream",1116 "text": [1117 "{0: 'Even', 1: 'Odd', 2: 'Even', 3: 'Odd', 4: 'Even', 5: 'Odd', 6: 'Even', 7: 'Odd', 8: 'Even', 9: 'Odd'}\n"1118 ]1119 }1120 ],1121 "source": [1122 "even_odd_di = {}\n",1123 "for i in range(10):\n",1124 " res ='Even' if i%2==0 else 'Odd'\n",1125 " even_odd_di.update({i:res})\n",1126 "print(even_odd_di)"1127 ]1128 },1129 {1130 "cell_type": "markdown",1131 "id": "3e0f43ad",1132 "metadata": {},1133 "source": [1134 "### List comprehensions"1135 ]1136 },1137 {1138 "cell_type": "code",1139 "execution_count": 202,1140 "id": "7fa843cd",1141 "metadata": {},1142 "outputs": [1143 {1144 "data": {1145 "text/plain": [1146 "[2, 4, 6, 8]"1147 ]1148 },1149 "execution_count": 202,1150 "metadata": {},1151 "output_type": "execute_result"1152 }1153 ],1154 "source": [1155 "li1 =[1,2,3,4,5,6,7,8]\n",1156 "li2 = []\n",1157 "for i in li1:\n",1158 " if i%2==0:\n",1159 " li2.append(i)\n",1160 "li2"1161 ]1162 },1163 {1164 "cell_type": "code",1165 "execution_count": 203,1166 "id": "9d9c1e8b",1167 "metadata": {},1168 "outputs": [],1169 "source": [1170 "### Equivalent list comprehension for above code\n",1171 "li1 =[1,2,3,4,5,6,7,8]\n",1172 "li2 = [i for i in li1 if i%2==0]"1173 ]1174 },1175 {1176 "cell_type": "code",1177 "execution_count": 201,1178 "id": "77a6078f",1179 "metadata": {},1180 "outputs": [1181 {1182 "data": {1183 "text/plain": [1184 "[2, 4, 6, 8]"1185 ]1186 },1187 "execution_count": 201,1188 "metadata": {},1189 "output_type": "execute_result"1190 }1191 ],1192 "source": [1193 "li2"1194 ]1195 },1196 {1197 "cell_type": "code",1198 "execution_count": null,1199 "id": "4810d2f6",1200 "metadata": {},