├── .gitignore ├── Assignments └── Python Assignment 1.pdf ├── Lecture-1 ├── .ipynb_checkpoints │ ├── Untitled-checkpoint.ipynb │ ├── Untitled1-checkpoint.ipynb │ ├── Untitled2-checkpoint.ipynb │ ├── Untitled3-checkpoint.ipynb │ ├── Untitled4-checkpoint.ipynb │ ├── Untitled5-checkpoint.ipynb │ ├── Untitled6-checkpoint.ipynb │ ├── Untitled7-checkpoint.ipynb │ └── Untitled8-checkpoint.ipynb ├── Untitled.ipynb ├── Untitled1.ipynb ├── Untitled2.ipynb ├── Untitled3.ipynb ├── Untitled4.ipynb ├── Untitled5.ipynb ├── Untitled6.ipynb ├── Untitled7.ipynb └── Untitled8.ipynb ├── Lecture-2 ├── .ipynb_checkpoints │ ├── Games-checkpoint.ipynb │ ├── Untitled-checkpoint.ipynb │ ├── Untitled1-checkpoint.ipynb │ ├── Untitled10-checkpoint.ipynb │ ├── Untitled2-checkpoint.ipynb │ ├── Untitled3-checkpoint.ipynb │ ├── Untitled4-checkpoint.ipynb │ ├── Untitled5-checkpoint.ipynb │ ├── Untitled6-checkpoint.ipynb │ ├── Untitled7-checkpoint.ipynb │ ├── Untitled8-checkpoint.ipynb │ └── Untitled9-checkpoint.ipynb ├── Games.ipynb ├── Untitled.ipynb ├── Untitled1.ipynb ├── Untitled10.ipynb ├── Untitled2.ipynb ├── Untitled3.ipynb ├── Untitled4.ipynb ├── Untitled5.ipynb ├── Untitled6.ipynb ├── Untitled7.ipynb ├── Untitled8.ipynb └── Untitled9.ipynb ├── Lecture-3 ├── .ipynb_checkpoints │ ├── Untitled-checkpoint.ipynb │ ├── Untitled1-checkpoint.ipynb │ ├── Untitled2-checkpoint.ipynb │ ├── Untitled3-checkpoint.ipynb │ ├── file-checkpoint.pdf │ ├── first-checkpoint.txt │ └── second-checkpoint.txt ├── Networking.ipynb ├── Untitled.ipynb ├── Untitled1.ipynb ├── Untitled2.ipynb ├── Untitled3.ipynb ├── chat │ ├── client.py │ └── server.py ├── downloadfile.py ├── file.pdf ├── first.txt ├── randomEpisode.py └── second.txt ├── Lecture-4 ├── .ipynb_checkpoints │ ├── Lecture-4-checkpoint.ipynb │ └── puppy-checkpoint.jpg ├── Lecture-4.ipynb ├── fb_pictures │ ├── 10_img.jpg │ ├── 4_img.jpg │ ├── 5_img.jpg │ ├── 6_img.jpg │ ├── 7_img.jpg │ ├── 8_img.jpg │ └── 9_img.jpg ├── pug.jpg └── puppy.jpg └── Lecture-5 ├── .ipynb_checkpoints ├── Untitled-checkpoint.ipynb ├── Untitled1-checkpoint.ipynb ├── Untitled2-checkpoint.ipynb ├── Untitled3-checkpoint.ipynb ├── Untitled4-checkpoint.ipynb ├── Untitled5-checkpoint.ipynb ├── Untitled6-checkpoint.ipynb ├── decorators-checkpoint.ipynb ├── exception_handling-checkpoint.ipynb ├── first-checkpoint.ipynb ├── first-checkpoint.txt ├── iterators-checkpoint.ipynb └── second-checkpoint.py ├── Untitled.ipynb ├── Untitled1.ipynb ├── Untitled2.ipynb ├── Untitled3.ipynb ├── Untitled4.ipynb ├── Untitled5.ipynb ├── Untitled6.ipynb ├── __main__ vs __name__ ├── first.py └── second.py ├── __pycache__ └── second.cpython-38.pyc ├── decorators.ipynb ├── exception_handling.ipynb ├── first.ipynb ├── first.txt ├── iterators.ipynb └── second.py /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea 3 | -------------------------------------------------------------------------------- /Assignments/Python Assignment 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeforcauseorg/Python-Webinars/19cab1b01af269f5eb19dd7126057f3c832cd477/Assignments/Python Assignment 1.pdf -------------------------------------------------------------------------------- /Lecture-1/.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# int" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "a = 10" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 5, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "data": { 28 | "text/plain": [ 29 | "10" 30 | ] 31 | }, 32 | "execution_count": 5, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "a" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 12, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "a = 40" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 7, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "40" 59 | ] 60 | }, 61 | "execution_count": 7, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "a" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 14, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "a = \"kunal\"" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 9, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "'kunal'" 88 | ] 89 | }, 90 | "execution_count": 9, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "a" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 10, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [ 105 | "# very important" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 15, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "data": { 115 | "text/plain": [ 116 | "str" 117 | ] 118 | }, 119 | "execution_count": 15, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "type(a)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 16, 131 | "metadata": {}, 132 | "outputs": [], 133 | "source": [ 134 | "a = 10" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 17, 140 | "metadata": {}, 141 | "outputs": [], 142 | "source": [ 143 | "b = 10" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 19, 149 | "metadata": {}, 150 | "outputs": [ 151 | { 152 | "data": { 153 | "text/plain": [ 154 | "True" 155 | ] 156 | }, 157 | "execution_count": 19, 158 | "metadata": {}, 159 | "output_type": "execute_result" 160 | } 161 | ], 162 | "source": [ 163 | "a == b" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 20, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "data": { 173 | "text/plain": [ 174 | "True" 175 | ] 176 | }, 177 | "execution_count": 20, 178 | "metadata": {}, 179 | "output_type": "execute_result" 180 | } 181 | ], 182 | "source": [ 183 | "a is b" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 21, 189 | "metadata": {}, 190 | "outputs": [], 191 | "source": [ 192 | "a = 4555\n", 193 | "b = 4555" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 22, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "data": { 203 | "text/plain": [ 204 | "False" 205 | ] 206 | }, 207 | "execution_count": 22, 208 | "metadata": {}, 209 | "output_type": "execute_result" 210 | } 211 | ], 212 | "source": [ 213 | "a is b" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": null, 219 | "metadata": {}, 220 | "outputs": [], 221 | "source": [] 222 | } 223 | ], 224 | "metadata": { 225 | "kernelspec": { 226 | "display_name": "Python 3", 227 | "language": "python", 228 | "name": "python3" 229 | }, 230 | "language_info": { 231 | "codemirror_mode": { 232 | "name": "ipython", 233 | "version": 3 234 | }, 235 | "file_extension": ".py", 236 | "mimetype": "text/x-python", 237 | "name": "python", 238 | "nbconvert_exporter": "python", 239 | "pygments_lexer": "ipython3", 240 | "version": "3.8.0" 241 | } 242 | }, 243 | "nbformat": 4, 244 | "nbformat_minor": 4 245 | } 246 | -------------------------------------------------------------------------------- /Lecture-1/.ipynb_checkpoints/Untitled1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "a = 10.3" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "float" 21 | ] 22 | }, 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "type(a)" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "# size" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 4, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "a = 23456789876543234567890987654321234567" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 5, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "int" 59 | ] 60 | }, 61 | "execution_count": 5, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "type(a)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 8, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "b = a ** 100" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 11, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "int" 88 | ] 89 | }, 90 | "execution_count": 11, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "type(b)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 13, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [ 105 | "c = b ** 100" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 14, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "data": { 115 | "text/plain": [ 116 | "int" 117 | ] 118 | }, 119 | "execution_count": 14, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "type(c)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": null, 131 | "metadata": {}, 132 | "outputs": [], 133 | "source": [] 134 | } 135 | ], 136 | "metadata": { 137 | "kernelspec": { 138 | "display_name": "Python 3", 139 | "language": "python", 140 | "name": "python3" 141 | }, 142 | "language_info": { 143 | "codemirror_mode": { 144 | "name": "ipython", 145 | "version": 3 146 | }, 147 | "file_extension": ".py", 148 | "mimetype": "text/x-python", 149 | "name": "python", 150 | "nbconvert_exporter": "python", 151 | "pygments_lexer": "ipython3", 152 | "version": "3.8.0" 153 | } 154 | }, 155 | "nbformat": 4, 156 | "nbformat_minor": 4 157 | } 158 | -------------------------------------------------------------------------------- /Lecture-1/.ipynb_checkpoints/Untitled2-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "30" 12 | ] 13 | }, 14 | "execution_count": 3, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "a = 10\n", 21 | "b = 20\n", 22 | "a + b" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 4, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "a = 3\n", 32 | "b = 2" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 6, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "1" 44 | ] 45 | }, 46 | "execution_count": 6, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "a // b" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 7, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "str = 'c'" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 9, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "data": { 71 | "text/plain": [ 72 | "str" 73 | ] 74 | }, 75 | "execution_count": 9, 76 | "metadata": {}, 77 | "output_type": "execute_result" 78 | } 79 | ], 80 | "source": [ 81 | "type(str)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 10, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "data": { 91 | "text/plain": [ 92 | "(True, False)" 93 | ] 94 | }, 95 | "execution_count": 10, 96 | "metadata": {}, 97 | "output_type": "execute_result" 98 | } 99 | ], 100 | "source": [ 101 | "True, False" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [] 110 | } 111 | ], 112 | "metadata": { 113 | "kernelspec": { 114 | "display_name": "Python 3", 115 | "language": "python", 116 | "name": "python3" 117 | }, 118 | "language_info": { 119 | "codemirror_mode": { 120 | "name": "ipython", 121 | "version": 3 122 | }, 123 | "file_extension": ".py", 124 | "mimetype": "text/x-python", 125 | "name": "python", 126 | "nbconvert_exporter": "python", 127 | "pygments_lexer": "ipython3", 128 | "version": "3.8.0" 129 | } 130 | }, 131 | "nbformat": 4, 132 | "nbformat_minor": 4 133 | } 134 | -------------------------------------------------------------------------------- /Lecture-1/.ipynb_checkpoints/Untitled3-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# data types" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "# lists" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "a = [2, 4, 6]" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 9, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "2" 39 | ] 40 | }, 41 | "execution_count": 9, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "a[-3] # from behind" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 10, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "b = [45, \"rahul\", 34.6]" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 11, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "45" 68 | ] 69 | }, 70 | "execution_count": 11, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "b[0]" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 12, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "'rahul'" 88 | ] 89 | }, 90 | "execution_count": 12, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "b[1]" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [] 112 | } 113 | ], 114 | "metadata": { 115 | "kernelspec": { 116 | "display_name": "Python 3", 117 | "language": "python", 118 | "name": "python3" 119 | }, 120 | "language_info": { 121 | "codemirror_mode": { 122 | "name": "ipython", 123 | "version": 3 124 | }, 125 | "file_extension": ".py", 126 | "mimetype": "text/x-python", 127 | "name": "python", 128 | "nbconvert_exporter": "python", 129 | "pygments_lexer": "ipython3", 130 | "version": "3.8.0" 131 | } 132 | }, 133 | "nbformat": 4, 134 | "nbformat_minor": 4 135 | } 136 | -------------------------------------------------------------------------------- /Lecture-1/.ipynb_checkpoints/Untitled4-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "apoorv = [23, 34, 76]\n", 10 | "baby = apoorv" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "data": { 20 | "text/plain": [ 21 | "[23, 34, 76]" 22 | ] 23 | }, 24 | "execution_count": 2, 25 | "metadata": {}, 26 | "output_type": "execute_result" 27 | } 28 | ], 29 | "source": [ 30 | "apoorv" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 3, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "[23, 34, 76]" 42 | ] 43 | }, 44 | "execution_count": 3, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "baby" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "baby[0] = 100" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 5, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "[100, 34, 76]" 71 | ] 72 | }, 73 | "execution_count": 5, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "apoorv" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 6, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "rahul = [45, 66]" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 7, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "baby = rahul" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 8, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "apoorv = None" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [] 115 | } 116 | ], 117 | "metadata": { 118 | "kernelspec": { 119 | "display_name": "Python 3", 120 | "language": "python", 121 | "name": "python3" 122 | }, 123 | "language_info": { 124 | "codemirror_mode": { 125 | "name": "ipython", 126 | "version": 3 127 | }, 128 | "file_extension": ".py", 129 | "mimetype": "text/x-python", 130 | "name": "python", 131 | "nbconvert_exporter": "python", 132 | "pygments_lexer": "ipython3", 133 | "version": "3.8.0" 134 | } 135 | }, 136 | "nbformat": 4, 137 | "nbformat_minor": 4 138 | } 139 | -------------------------------------------------------------------------------- /Lecture-1/.ipynb_checkpoints/Untitled5-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "a = (32, 44, 56)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "44" 21 | ] 22 | }, 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "a[1]" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "ename": "TypeError", 39 | "evalue": "'tuple' object does not support item assignment", 40 | "output_type": "error", 41 | "traceback": [ 42 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 43 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 44 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m67\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 45 | "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "a[0] = 67" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "a = (78, 89)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "b = ( [2,3], [5,6] ) " 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "b[1][0] = 90" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "b" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "b[0] = [6,5]" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "# sets" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "# same thing as in maths" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 2, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [ 122 | "a = {34, 34, 56}" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 3, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "{34, 56}" 134 | ] 135 | }, 136 | "execution_count": 3, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "a" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 4, 148 | "metadata": {}, 149 | "outputs": [], 150 | "source": [ 151 | "d = {\n", 152 | " \"mango\": \"king of fruits\",\n", 153 | " \"apple\": \"a sweet red fruit\"\n", 154 | "}" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 5, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "'a sweet red fruit'" 166 | ] 167 | }, 168 | "execution_count": 5, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "d['apple']" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 6, 180 | "metadata": {}, 181 | "outputs": [], 182 | "source": [ 183 | "d['apple'] = \"my fav fruit\"" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 7, 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "data": { 193 | "text/plain": [ 194 | "{'mango': 'king of fruits', 'apple': 'my fav fruit'}" 195 | ] 196 | }, 197 | "execution_count": 7, 198 | "metadata": {}, 199 | "output_type": "execute_result" 200 | } 201 | ], 202 | "source": [ 203 | "d" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 9, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [ 212 | "# slicing and comprehension" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 10, 218 | "metadata": {}, 219 | "outputs": [], 220 | "source": [ 221 | "# slicing" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 11, 227 | "metadata": {}, 228 | "outputs": [], 229 | "source": [ 230 | "a = [34, 56, 76, 54]" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 25, 236 | "metadata": {}, 237 | "outputs": [ 238 | { 239 | "data": { 240 | "text/plain": [ 241 | "[54, 76, 56, 34]" 242 | ] 243 | }, 244 | "execution_count": 25, 245 | "metadata": {}, 246 | "output_type": "execute_result" 247 | } 248 | ], 249 | "source": [ 250 | "a[-1::-1] # reverse" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": null, 256 | "metadata": {}, 257 | "outputs": [], 258 | "source": [] 259 | } 260 | ], 261 | "metadata": { 262 | "kernelspec": { 263 | "display_name": "Python 3", 264 | "language": "python", 265 | "name": "python3" 266 | }, 267 | "language_info": { 268 | "codemirror_mode": { 269 | "name": "ipython", 270 | "version": 3 271 | }, 272 | "file_extension": ".py", 273 | "mimetype": "text/x-python", 274 | "name": "python", 275 | "nbconvert_exporter": "python", 276 | "pygments_lexer": "ipython3", 277 | "version": "3.8.0" 278 | } 279 | }, 280 | "nbformat": 4, 281 | "nbformat_minor": 4 282 | } 283 | -------------------------------------------------------------------------------- /Lecture-1/.ipynb_checkpoints/Untitled8-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [] 16 | } 17 | ], 18 | "metadata": { 19 | "kernelspec": { 20 | "display_name": "Python 3", 21 | "language": "python", 22 | "name": "python3" 23 | }, 24 | "language_info": { 25 | "codemirror_mode": { 26 | "name": "ipython", 27 | "version": 3 28 | }, 29 | "file_extension": ".py", 30 | "mimetype": "text/x-python", 31 | "name": "python", 32 | "nbconvert_exporter": "python", 33 | "pygments_lexer": "ipython3", 34 | "version": "3.8.0" 35 | } 36 | }, 37 | "nbformat": 4, 38 | "nbformat_minor": 4 39 | } 40 | -------------------------------------------------------------------------------- /Lecture-1/Untitled.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# int" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "a = 10" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 5, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "data": { 28 | "text/plain": [ 29 | "10" 30 | ] 31 | }, 32 | "execution_count": 5, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "a" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 12, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "a = 40" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 7, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "40" 59 | ] 60 | }, 61 | "execution_count": 7, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "a" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 14, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "a = \"kunal\"" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 9, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "'kunal'" 88 | ] 89 | }, 90 | "execution_count": 9, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "a" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 10, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [ 105 | "# very important" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 15, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "data": { 115 | "text/plain": [ 116 | "str" 117 | ] 118 | }, 119 | "execution_count": 15, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "type(a)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 16, 131 | "metadata": {}, 132 | "outputs": [], 133 | "source": [ 134 | "a = 10" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 17, 140 | "metadata": {}, 141 | "outputs": [], 142 | "source": [ 143 | "b = 10" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 19, 149 | "metadata": {}, 150 | "outputs": [ 151 | { 152 | "data": { 153 | "text/plain": [ 154 | "True" 155 | ] 156 | }, 157 | "execution_count": 19, 158 | "metadata": {}, 159 | "output_type": "execute_result" 160 | } 161 | ], 162 | "source": [ 163 | "a == b" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 20, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "data": { 173 | "text/plain": [ 174 | "True" 175 | ] 176 | }, 177 | "execution_count": 20, 178 | "metadata": {}, 179 | "output_type": "execute_result" 180 | } 181 | ], 182 | "source": [ 183 | "a is b" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 21, 189 | "metadata": {}, 190 | "outputs": [], 191 | "source": [ 192 | "a = 4555\n", 193 | "b = 4555" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 22, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "data": { 203 | "text/plain": [ 204 | "False" 205 | ] 206 | }, 207 | "execution_count": 22, 208 | "metadata": {}, 209 | "output_type": "execute_result" 210 | } 211 | ], 212 | "source": [ 213 | "a is b" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": null, 219 | "metadata": {}, 220 | "outputs": [], 221 | "source": [] 222 | } 223 | ], 224 | "metadata": { 225 | "kernelspec": { 226 | "display_name": "Python 3", 227 | "language": "python", 228 | "name": "python3" 229 | }, 230 | "language_info": { 231 | "codemirror_mode": { 232 | "name": "ipython", 233 | "version": 3 234 | }, 235 | "file_extension": ".py", 236 | "mimetype": "text/x-python", 237 | "name": "python", 238 | "nbconvert_exporter": "python", 239 | "pygments_lexer": "ipython3", 240 | "version": "3.8.0" 241 | } 242 | }, 243 | "nbformat": 4, 244 | "nbformat_minor": 4 245 | } 246 | -------------------------------------------------------------------------------- /Lecture-1/Untitled1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "a = 10.3" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "float" 21 | ] 22 | }, 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "type(a)" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "# size" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 4, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "a = 23456789876543234567890987654321234567" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 5, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "int" 59 | ] 60 | }, 61 | "execution_count": 5, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "type(a)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 8, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "b = a ** 100" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 11, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "int" 88 | ] 89 | }, 90 | "execution_count": 11, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "type(b)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 13, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [ 105 | "c = b ** 100" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 14, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "data": { 115 | "text/plain": [ 116 | "int" 117 | ] 118 | }, 119 | "execution_count": 14, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "type(c)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": null, 131 | "metadata": {}, 132 | "outputs": [], 133 | "source": [] 134 | } 135 | ], 136 | "metadata": { 137 | "kernelspec": { 138 | "display_name": "Python 3", 139 | "language": "python", 140 | "name": "python3" 141 | }, 142 | "language_info": { 143 | "codemirror_mode": { 144 | "name": "ipython", 145 | "version": 3 146 | }, 147 | "file_extension": ".py", 148 | "mimetype": "text/x-python", 149 | "name": "python", 150 | "nbconvert_exporter": "python", 151 | "pygments_lexer": "ipython3", 152 | "version": "3.8.0" 153 | } 154 | }, 155 | "nbformat": 4, 156 | "nbformat_minor": 4 157 | } 158 | -------------------------------------------------------------------------------- /Lecture-1/Untitled2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "30" 12 | ] 13 | }, 14 | "execution_count": 3, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "a = 10\n", 21 | "b = 20\n", 22 | "a + b" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 4, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "a = 3\n", 32 | "b = 2" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 6, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "1" 44 | ] 45 | }, 46 | "execution_count": 6, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "a // b" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 7, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "str = 'c'" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 9, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "data": { 71 | "text/plain": [ 72 | "str" 73 | ] 74 | }, 75 | "execution_count": 9, 76 | "metadata": {}, 77 | "output_type": "execute_result" 78 | } 79 | ], 80 | "source": [ 81 | "type(str)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 10, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "data": { 91 | "text/plain": [ 92 | "(True, False)" 93 | ] 94 | }, 95 | "execution_count": 10, 96 | "metadata": {}, 97 | "output_type": "execute_result" 98 | } 99 | ], 100 | "source": [ 101 | "True, False" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [] 110 | } 111 | ], 112 | "metadata": { 113 | "kernelspec": { 114 | "display_name": "Python 3", 115 | "language": "python", 116 | "name": "python3" 117 | }, 118 | "language_info": { 119 | "codemirror_mode": { 120 | "name": "ipython", 121 | "version": 3 122 | }, 123 | "file_extension": ".py", 124 | "mimetype": "text/x-python", 125 | "name": "python", 126 | "nbconvert_exporter": "python", 127 | "pygments_lexer": "ipython3", 128 | "version": "3.8.0" 129 | } 130 | }, 131 | "nbformat": 4, 132 | "nbformat_minor": 4 133 | } 134 | -------------------------------------------------------------------------------- /Lecture-1/Untitled3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# data types" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "# lists" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "a = [2, 4, 6]" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 9, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "2" 39 | ] 40 | }, 41 | "execution_count": 9, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "a[-3] # from behind" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 10, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "b = [45, \"rahul\", 34.6]" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 11, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "45" 68 | ] 69 | }, 70 | "execution_count": 11, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "b[0]" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 12, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "'rahul'" 88 | ] 89 | }, 90 | "execution_count": 12, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "b[1]" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [] 112 | } 113 | ], 114 | "metadata": { 115 | "kernelspec": { 116 | "display_name": "Python 3", 117 | "language": "python", 118 | "name": "python3" 119 | }, 120 | "language_info": { 121 | "codemirror_mode": { 122 | "name": "ipython", 123 | "version": 3 124 | }, 125 | "file_extension": ".py", 126 | "mimetype": "text/x-python", 127 | "name": "python", 128 | "nbconvert_exporter": "python", 129 | "pygments_lexer": "ipython3", 130 | "version": "3.8.0" 131 | } 132 | }, 133 | "nbformat": 4, 134 | "nbformat_minor": 4 135 | } 136 | -------------------------------------------------------------------------------- /Lecture-1/Untitled4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "apoorv = [23, 34, 76]\n", 10 | "baby = apoorv" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "data": { 20 | "text/plain": [ 21 | "[23, 34, 76]" 22 | ] 23 | }, 24 | "execution_count": 2, 25 | "metadata": {}, 26 | "output_type": "execute_result" 27 | } 28 | ], 29 | "source": [ 30 | "apoorv" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 3, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "[23, 34, 76]" 42 | ] 43 | }, 44 | "execution_count": 3, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "baby" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "baby[0] = 100" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 5, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "[100, 34, 76]" 71 | ] 72 | }, 73 | "execution_count": 5, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "apoorv" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 6, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "rahul = [45, 66]" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 7, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "baby = rahul" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 8, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "apoorv = None" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [] 115 | } 116 | ], 117 | "metadata": { 118 | "kernelspec": { 119 | "display_name": "Python 3", 120 | "language": "python", 121 | "name": "python3" 122 | }, 123 | "language_info": { 124 | "codemirror_mode": { 125 | "name": "ipython", 126 | "version": 3 127 | }, 128 | "file_extension": ".py", 129 | "mimetype": "text/x-python", 130 | "name": "python", 131 | "nbconvert_exporter": "python", 132 | "pygments_lexer": "ipython3", 133 | "version": "3.8.0" 134 | } 135 | }, 136 | "nbformat": 4, 137 | "nbformat_minor": 4 138 | } 139 | -------------------------------------------------------------------------------- /Lecture-1/Untitled5.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "a = (32, 44, 56)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "44" 21 | ] 22 | }, 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "a[1]" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "ename": "TypeError", 39 | "evalue": "'tuple' object does not support item assignment", 40 | "output_type": "error", 41 | "traceback": [ 42 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 43 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 44 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m67\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 45 | "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "a[0] = 67" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "a = (78, 89)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "b = ( [2,3], [5,6] ) " 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "b[1][0] = 90" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "b" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "b[0] = [6,5]" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "# sets" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "# same thing as in maths" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 2, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [ 122 | "a = {34, 34, 56}" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 3, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "{34, 56}" 134 | ] 135 | }, 136 | "execution_count": 3, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "a" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 4, 148 | "metadata": {}, 149 | "outputs": [], 150 | "source": [ 151 | "d = {\n", 152 | " \"mango\": \"king of fruits\",\n", 153 | " \"apple\": \"a sweet red fruit\"\n", 154 | "}" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 5, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "'a sweet red fruit'" 166 | ] 167 | }, 168 | "execution_count": 5, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "d['apple']" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 6, 180 | "metadata": {}, 181 | "outputs": [], 182 | "source": [ 183 | "d['apple'] = \"my fav fruit\"" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 7, 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "data": { 193 | "text/plain": [ 194 | "{'mango': 'king of fruits', 'apple': 'my fav fruit'}" 195 | ] 196 | }, 197 | "execution_count": 7, 198 | "metadata": {}, 199 | "output_type": "execute_result" 200 | } 201 | ], 202 | "source": [ 203 | "d" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 9, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [ 212 | "# slicing and comprehension" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 10, 218 | "metadata": {}, 219 | "outputs": [], 220 | "source": [ 221 | "# slicing" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 11, 227 | "metadata": {}, 228 | "outputs": [], 229 | "source": [ 230 | "a = [34, 56, 76, 54]" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 25, 236 | "metadata": {}, 237 | "outputs": [ 238 | { 239 | "data": { 240 | "text/plain": [ 241 | "[54, 76, 56, 34]" 242 | ] 243 | }, 244 | "execution_count": 25, 245 | "metadata": {}, 246 | "output_type": "execute_result" 247 | } 248 | ], 249 | "source": [ 250 | "a[-1::-1] # reverse" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": null, 256 | "metadata": {}, 257 | "outputs": [], 258 | "source": [] 259 | } 260 | ], 261 | "metadata": { 262 | "kernelspec": { 263 | "display_name": "Python 3", 264 | "language": "python", 265 | "name": "python3" 266 | }, 267 | "language_info": { 268 | "codemirror_mode": { 269 | "name": "ipython", 270 | "version": 3 271 | }, 272 | "file_extension": ".py", 273 | "mimetype": "text/x-python", 274 | "name": "python", 275 | "nbconvert_exporter": "python", 276 | "pygments_lexer": "ipython3", 277 | "version": "3.8.0" 278 | } 279 | }, 280 | "nbformat": 4, 281 | "nbformat_minor": 4 282 | } 283 | -------------------------------------------------------------------------------- /Lecture-1/Untitled8.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [] 16 | } 17 | ], 18 | "metadata": { 19 | "kernelspec": { 20 | "display_name": "Python 3", 21 | "language": "python", 22 | "name": "python3" 23 | }, 24 | "language_info": { 25 | "codemirror_mode": { 26 | "name": "ipython", 27 | "version": 3 28 | }, 29 | "file_extension": ".py", 30 | "mimetype": "text/x-python", 31 | "name": "python", 32 | "nbconvert_exporter": "python", 33 | "pygments_lexer": "ipython3", 34 | "version": "3.8.0" 35 | } 36 | }, 37 | "nbformat": 4, 38 | "nbformat_minor": 4 39 | } 40 | -------------------------------------------------------------------------------- /Lecture-2/.ipynb_checkpoints/Games-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Guessing Game" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 26, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdin", 19 | "output_type": "stream", 20 | "text": [ 21 | "Enter your name: Kunal Kushwaha\n" 22 | ] 23 | }, 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "Hello Kunal Kushwaha! Welcome to the guessing game!\n", 29 | "_ _ c _ o o k " 30 | ] 31 | }, 32 | { 33 | "name": "stdin", 34 | "output_type": "stream", 35 | "text": [ 36 | "\n", 37 | "Guess a character: m\n" 38 | ] 39 | }, 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "m _ c _ o o k " 45 | ] 46 | }, 47 | { 48 | "name": "stdin", 49 | "output_type": "stream", 50 | "text": [ 51 | "\n", 52 | "Guess a character: f\n" 53 | ] 54 | }, 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "\n", 60 | "Wrong Answer!!\n", 61 | "You have 9 chances left!\n", 62 | "m _ c _ o o k " 63 | ] 64 | }, 65 | { 66 | "name": "stdin", 67 | "output_type": "stream", 68 | "text": [ 69 | "\n", 70 | "Guess a character: a\n" 71 | ] 72 | }, 73 | { 74 | "name": "stdout", 75 | "output_type": "stream", 76 | "text": [ 77 | "m a c _ o o k " 78 | ] 79 | }, 80 | { 81 | "name": "stdin", 82 | "output_type": "stream", 83 | "text": [ 84 | "\n", 85 | "Guess a character: b\n" 86 | ] 87 | }, 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "m a c b o o k \n", 93 | "You Won!\n", 94 | "Your score is 90\n" 95 | ] 96 | }, 97 | { 98 | "name": "stdin", 99 | "output_type": "stream", 100 | "text": [ 101 | "Do you want to play again? (Yes/No): Yes\n" 102 | ] 103 | }, 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "s u _ s e _ " 109 | ] 110 | }, 111 | { 112 | "name": "stdin", 113 | "output_type": "stream", 114 | "text": [ 115 | "\n", 116 | "Guess a character: i\n" 117 | ] 118 | }, 119 | { 120 | "name": "stdout", 121 | "output_type": "stream", 122 | "text": [ 123 | "\n", 124 | "Wrong Answer!!\n", 125 | "You have 9 chances left!\n", 126 | "s u _ s e _ " 127 | ] 128 | }, 129 | { 130 | "name": "stdin", 131 | "output_type": "stream", 132 | "text": [ 133 | "\n", 134 | "Guess a character: b\n" 135 | ] 136 | }, 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | "\n", 142 | "Wrong Answer!!\n", 143 | "You have 8 chances left!\n", 144 | "s u _ s e _ " 145 | ] 146 | }, 147 | { 148 | "name": "stdin", 149 | "output_type": "stream", 150 | "text": [ 151 | "\n", 152 | "Guess a character: n\n" 153 | ] 154 | }, 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "s u n s e _ " 160 | ] 161 | }, 162 | { 163 | "name": "stdin", 164 | "output_type": "stream", 165 | "text": [ 166 | "\n", 167 | "Guess a character: t\n" 168 | ] 169 | }, 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "s u n s e t \n", 175 | "You Won!\n", 176 | "Your score is 80\n" 177 | ] 178 | }, 179 | { 180 | "name": "stdin", 181 | "output_type": "stream", 182 | "text": [ 183 | "Do you want to play again? (Yes/No): No\n" 184 | ] 185 | }, 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "Thanks for playing!\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "import random\n", 196 | "\n", 197 | "name = input(\"Enter your name: \")\n", 198 | "print(f\"Hello {name}! Welcome to the guessing game!\")\n", 199 | "words = [\"guessing\",\"apple\",\"earphones\",\"macbook\",\"python\",\n", 200 | " \"television\", \"sunset\",\"opensource\"]\n", 201 | "# get a random word from this list\n", 202 | "index = random.randint(0, len(words))\n", 203 | "word = words[index]\n", 204 | "indexes = random.sample(range(0, len(word)), 3)\n", 205 | "guesses = \"\" #characters that the user has guessed so far\n", 206 | "for i in indexes:\n", 207 | " guesses += word[i]\n", 208 | "chances = 10\n", 209 | "play = \"Yes\"\n", 210 | "\n", 211 | "def playagain():\n", 212 | " # this will help you understad scoping\n", 213 | " global play\n", 214 | " play = input(\"Do you want to play again? (Yes/No): \")\n", 215 | " if play == \"Yes\":\n", 216 | " global chances, word, guesses\n", 217 | " chances = 10\n", 218 | " index = random.randint(0, len(words))\n", 219 | " word = words[index]\n", 220 | " indexes = random.sample(range(0, len(word)), 3)\n", 221 | " guesses = \"\" \n", 222 | " for i in indexes:\n", 223 | " guesses += word[i]\n", 224 | "\n", 225 | "# this play is global variable\n", 226 | "while play == \"Yes\":\n", 227 | " while chances > 0:\n", 228 | " won = True\n", 229 | " for ch in word:\n", 230 | " if ch in guesses: # the person has guessed\n", 231 | " print(ch, end=\" \")\n", 232 | " else:\n", 233 | " print(\"_\", end=\" \")\n", 234 | " won = False\n", 235 | "\n", 236 | " if won:\n", 237 | " print(\"\\nYou Won!\")\n", 238 | " print(f\"Your score is {chances * 10}\")\n", 239 | " playagain()\n", 240 | " break\n", 241 | "\n", 242 | " # take a guess from the user\n", 243 | " guess = input(\"\\nGuess a character: \")\n", 244 | " guesses += guess\n", 245 | "\n", 246 | " if guess not in word:\n", 247 | " chances -= 1\n", 248 | " print(\"\\nWrong Answer!!\")\n", 249 | " print(f\"You have {chances} chances left!\")\n", 250 | "\n", 251 | " if chances == 0:\n", 252 | " print(\"You Lose!!\")\n", 253 | " playagain()\n", 254 | " break\n", 255 | "\n", 256 | "print(\"Thanks for playing!\")" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": null, 262 | "metadata": {}, 263 | "outputs": [], 264 | "source": [] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": {}, 270 | "outputs": [], 271 | "source": [] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": null, 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [] 279 | } 280 | ], 281 | "metadata": { 282 | "kernelspec": { 283 | "display_name": "Python 3", 284 | "language": "python", 285 | "name": "python3" 286 | }, 287 | "language_info": { 288 | "codemirror_mode": { 289 | "name": "ipython", 290 | "version": 3 291 | }, 292 | "file_extension": ".py", 293 | "mimetype": "text/x-python", 294 | "name": "python", 295 | "nbconvert_exporter": "python", 296 | "pygments_lexer": "ipython3", 297 | "version": "3.8.0" 298 | } 299 | }, 300 | "nbformat": 4, 301 | "nbformat_minor": 4 302 | } 303 | -------------------------------------------------------------------------------- /Lecture-2/.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "kernelspec": { 13 | "display_name": "Python 3", 14 | "language": "python", 15 | "name": "python3" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 3 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython3", 27 | "version": "3.8.0" 28 | } 29 | }, 30 | "nbformat": 4, 31 | "nbformat_minor": 4 32 | } 33 | -------------------------------------------------------------------------------- /Lecture-2/.ipynb_checkpoints/Untitled1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture-2/.ipynb_checkpoints/Untitled10-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# closures" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 15, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "def outer_func():\n", 19 | " message = \"hi\"\n", 20 | " \n", 21 | " def inner_func():\n", 22 | " print(locals())\n", 23 | " print(message) # free variable\n", 24 | " \n", 25 | " return inner_func" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 16, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "my_func = outer_func() # my_func = inner_func" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 17, 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "name": "stdout", 44 | "output_type": "stream", 45 | "text": [ 46 | "{'message': 'hi'}\n", 47 | "hi\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "my_func()" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 24, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "def make_multi(n):\n", 62 | " def multiplier(x):\n", 63 | " print(locals())\n", 64 | " return x * n\n", 65 | " return multiplier" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 25, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "times2 = make_multi(2)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 26, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "times3 = make_multi(3)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 27, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "{'x': 9, 'n': 3}\n" 96 | ] 97 | }, 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "27" 102 | ] 103 | }, 104 | "execution_count": 27, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "times3(9)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": null, 116 | "metadata": {}, 117 | "outputs": [], 118 | "source": [] 119 | } 120 | ], 121 | "metadata": { 122 | "kernelspec": { 123 | "display_name": "Python 3", 124 | "language": "python", 125 | "name": "python3" 126 | }, 127 | "language_info": { 128 | "codemirror_mode": { 129 | "name": "ipython", 130 | "version": 3 131 | }, 132 | "file_extension": ".py", 133 | "mimetype": "text/x-python", 134 | "name": "python", 135 | "nbconvert_exporter": "python", 136 | "pygments_lexer": "ipython3", 137 | "version": "3.8.0" 138 | } 139 | }, 140 | "nbformat": 4, 141 | "nbformat_minor": 4 142 | } 143 | -------------------------------------------------------------------------------- /Lecture-2/.ipynb_checkpoints/Untitled2-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture-2/.ipynb_checkpoints/Untitled3-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Printing!" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 4, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "34-56\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "print(34, 56, sep=\"-\")" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 5, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "# string formating" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 6, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "a = 30\n", 45 | "b = 47" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 7, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "The two numbers are 30 and 47\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "print(\"The two numbers are\", a, \"and\", b)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 10, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "The two numbers are 30 and 47\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "print(\"The two numbers are {} and {}\".format(a, b))" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 11, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "# the best way" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 12, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "# f strings" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 13, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "The two numbers are 30 and 47\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "print(f\"The two numbers are {a} and {b}\")" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [] 123 | } 124 | ], 125 | "metadata": { 126 | "kernelspec": { 127 | "display_name": "Python 3", 128 | "language": "python", 129 | "name": "python3" 130 | }, 131 | "language_info": { 132 | "codemirror_mode": { 133 | "name": "ipython", 134 | "version": 3 135 | }, 136 | "file_extension": ".py", 137 | "mimetype": "text/x-python", 138 | "name": "python", 139 | "nbconvert_exporter": "python", 140 | "pygments_lexer": "ipython3", 141 | "version": "3.8.0" 142 | } 143 | }, 144 | "nbformat": 4, 145 | "nbformat_minor": 4 146 | } 147 | -------------------------------------------------------------------------------- /Lecture-2/.ipynb_checkpoints/Untitled4-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "kernelspec": { 13 | "display_name": "Python 3", 14 | "language": "python", 15 | "name": "python3" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 3 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython3", 27 | "version": "3.8.0" 28 | } 29 | }, 30 | "nbformat": 4, 31 | "nbformat_minor": 4 32 | } 33 | -------------------------------------------------------------------------------- /Lecture-2/.ipynb_checkpoints/Untitled5-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture-2/.ipynb_checkpoints/Untitled6-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture-2/.ipynb_checkpoints/Untitled7-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture-2/.ipynb_checkpoints/Untitled8-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 21, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "class Parent:\n", 10 | " f = 80\n", 11 | " def __init__(self, value):\n", 12 | " self.value = value" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 22, 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "class Child(Parent):\n", 22 | " def __init__(self, value, name):\n", 23 | " super().__init__(value)\n", 24 | " self.name = name" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 23, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "obj = Child(67, \"Kunal\")" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 25, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "data": { 43 | "text/plain": [ 44 | "(80, 67, 'Kunal')" 45 | ] 46 | }, 47 | "execution_count": 25, 48 | "metadata": {}, 49 | "output_type": "execute_result" 50 | } 51 | ], 52 | "source": [ 53 | "obj.f, obj.value, obj.name" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 15, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "obj2 = Parent(89)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 18, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "# I want to hide some variables\n", 72 | "# jugaad for private variables in python" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 35, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "class A:\n", 82 | " def __init__(self, value):\n", 83 | " self.__value = value\n", 84 | " # python changes __value to _A__value" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 36, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "obj = A(89)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 37, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "text/plain": [ 104 | "89" 105 | ] 106 | }, 107 | "execution_count": 37, 108 | "metadata": {}, 109 | "output_type": "execute_result" 110 | } 111 | ], 112 | "source": [ 113 | "obj._A__value # not really private" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 38, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [ 122 | "# totally depends on the person writing the code" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 39, 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [ 131 | "# not as good as C++/Java" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [] 140 | } 141 | ], 142 | "metadata": { 143 | "kernelspec": { 144 | "display_name": "Python 3", 145 | "language": "python", 146 | "name": "python3" 147 | }, 148 | "language_info": { 149 | "codemirror_mode": { 150 | "name": "ipython", 151 | "version": 3 152 | }, 153 | "file_extension": ".py", 154 | "mimetype": "text/x-python", 155 | "name": "python", 156 | "nbconvert_exporter": "python", 157 | "pygments_lexer": "ipython3", 158 | "version": "3.8.0" 159 | } 160 | }, 161 | "nbformat": 4, 162 | "nbformat_minor": 4 163 | } 164 | -------------------------------------------------------------------------------- /Lecture-2/.ipynb_checkpoints/Untitled9-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# scoping" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 5, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "def fun():\n", 19 | " a = 10\n", 20 | " print(a)" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 6, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "10\n" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "fun()" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 8, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "# print(a) # cannot access from outside" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 13, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "80\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "a = 20 # this is global\n", 64 | "def fun():\n", 65 | " a = 60 # this is local to function fun\n", 66 | " a = a + 20\n", 67 | " print(a)\n", 68 | "\n", 69 | "fun()" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 14, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "# you can access but cannot change directly" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 15, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [ 87 | "a = 10\n", 88 | "def fun():\n", 89 | " print(a)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 16, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "10\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "fun()" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 28, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [ 115 | "x = 50\n", 116 | "\n", 117 | "def a():\n", 118 | " x = 10 # this is local to a\n", 119 | " # this x = 10 , this is enclosed for b()\n", 120 | " def b():\n", 121 | " x = 40 # this is locl to b\n", 122 | " print(\"x in b() is\", x)\n", 123 | " print(\"locals for b are: \", locals())\n", 124 | " \n", 125 | " print(\"x in a() is\", x)\n", 126 | " print(\"locals for a are: \", locals())\n", 127 | " return b()" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 29, 133 | "metadata": {}, 134 | "outputs": [ 135 | { 136 | "name": "stdout", 137 | "output_type": "stream", 138 | "text": [ 139 | "x in a() is 50\n", 140 | "locals for a are: {'b': .b at 0x10fbd7550>}\n", 141 | "x in b() is 50\n", 142 | "locals for b are: {}\n" 143 | ] 144 | } 145 | ], 146 | "source": [ 147 | "a()" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 38, 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "256\n", 160 | "10\n", 161 | "45\n", 162 | "145\n", 163 | "x ki value in b after running c() 145\n" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "x = 256\n", 169 | "\n", 170 | "def a():\n", 171 | " x = 10\n", 172 | " \n", 173 | " def b():\n", 174 | " x = 45\n", 175 | " \n", 176 | " def c():\n", 177 | " nonlocal x # explicitly bata raha hu\n", 178 | " x += 100\n", 179 | " print(x)\n", 180 | " \n", 181 | " print(x) # 45\n", 182 | " c()\n", 183 | " print(\"x ki value in b after running c()\",x) # 145\n", 184 | " \n", 185 | " print(x)\n", 186 | " b()\n", 187 | "\n", 188 | "print(x)\n", 189 | "a()" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 39, 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "name": "stdout", 199 | "output_type": "stream", 200 | "text": [ 201 | "256\n", 202 | "10\n", 203 | "45\n", 204 | "356\n", 205 | "x ki value in b after running c() 45\n" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "x = 256\n", 211 | "\n", 212 | "def a():\n", 213 | " x = 10\n", 214 | " \n", 215 | " def b():\n", 216 | " x = 45\n", 217 | " \n", 218 | " def c():\n", 219 | " global x # explicitly bata raha hu\n", 220 | " x += 100\n", 221 | " print(x)\n", 222 | " \n", 223 | " print(x) # 45\n", 224 | " c()\n", 225 | " print(\"x ki value in b after running c()\",x) # 145\n", 226 | " \n", 227 | " print(x)\n", 228 | " b()\n", 229 | "\n", 230 | "print(x)\n", 231 | "a()" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 40, 237 | "metadata": {}, 238 | "outputs": [ 239 | { 240 | "data": { 241 | "text/plain": [ 242 | "356" 243 | ] 244 | }, 245 | "execution_count": 40, 246 | "metadata": {}, 247 | "output_type": "execute_result" 248 | } 249 | ], 250 | "source": [ 251 | "x" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": null, 257 | "metadata": {}, 258 | "outputs": [], 259 | "source": [] 260 | } 261 | ], 262 | "metadata": { 263 | "kernelspec": { 264 | "display_name": "Python 3", 265 | "language": "python", 266 | "name": "python3" 267 | }, 268 | "language_info": { 269 | "codemirror_mode": { 270 | "name": "ipython", 271 | "version": 3 272 | }, 273 | "file_extension": ".py", 274 | "mimetype": "text/x-python", 275 | "name": "python", 276 | "nbconvert_exporter": "python", 277 | "pygments_lexer": "ipython3", 278 | "version": "3.8.0" 279 | } 280 | }, 281 | "nbformat": 4, 282 | "nbformat_minor": 4 283 | } 284 | -------------------------------------------------------------------------------- /Lecture-2/Games.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Guessing Game" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 26, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdin", 19 | "output_type": "stream", 20 | "text": [ 21 | "Enter your name: Kunal Kushwaha\n" 22 | ] 23 | }, 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "Hello Kunal Kushwaha! Welcome to the guessing game!\n", 29 | "_ _ c _ o o k " 30 | ] 31 | }, 32 | { 33 | "name": "stdin", 34 | "output_type": "stream", 35 | "text": [ 36 | "\n", 37 | "Guess a character: m\n" 38 | ] 39 | }, 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "m _ c _ o o k " 45 | ] 46 | }, 47 | { 48 | "name": "stdin", 49 | "output_type": "stream", 50 | "text": [ 51 | "\n", 52 | "Guess a character: f\n" 53 | ] 54 | }, 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "\n", 60 | "Wrong Answer!!\n", 61 | "You have 9 chances left!\n", 62 | "m _ c _ o o k " 63 | ] 64 | }, 65 | { 66 | "name": "stdin", 67 | "output_type": "stream", 68 | "text": [ 69 | "\n", 70 | "Guess a character: a\n" 71 | ] 72 | }, 73 | { 74 | "name": "stdout", 75 | "output_type": "stream", 76 | "text": [ 77 | "m a c _ o o k " 78 | ] 79 | }, 80 | { 81 | "name": "stdin", 82 | "output_type": "stream", 83 | "text": [ 84 | "\n", 85 | "Guess a character: b\n" 86 | ] 87 | }, 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "m a c b o o k \n", 93 | "You Won!\n", 94 | "Your score is 90\n" 95 | ] 96 | }, 97 | { 98 | "name": "stdin", 99 | "output_type": "stream", 100 | "text": [ 101 | "Do you want to play again? (Yes/No): Yes\n" 102 | ] 103 | }, 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "s u _ s e _ " 109 | ] 110 | }, 111 | { 112 | "name": "stdin", 113 | "output_type": "stream", 114 | "text": [ 115 | "\n", 116 | "Guess a character: i\n" 117 | ] 118 | }, 119 | { 120 | "name": "stdout", 121 | "output_type": "stream", 122 | "text": [ 123 | "\n", 124 | "Wrong Answer!!\n", 125 | "You have 9 chances left!\n", 126 | "s u _ s e _ " 127 | ] 128 | }, 129 | { 130 | "name": "stdin", 131 | "output_type": "stream", 132 | "text": [ 133 | "\n", 134 | "Guess a character: b\n" 135 | ] 136 | }, 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | "\n", 142 | "Wrong Answer!!\n", 143 | "You have 8 chances left!\n", 144 | "s u _ s e _ " 145 | ] 146 | }, 147 | { 148 | "name": "stdin", 149 | "output_type": "stream", 150 | "text": [ 151 | "\n", 152 | "Guess a character: n\n" 153 | ] 154 | }, 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "s u n s e _ " 160 | ] 161 | }, 162 | { 163 | "name": "stdin", 164 | "output_type": "stream", 165 | "text": [ 166 | "\n", 167 | "Guess a character: t\n" 168 | ] 169 | }, 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "s u n s e t \n", 175 | "You Won!\n", 176 | "Your score is 80\n" 177 | ] 178 | }, 179 | { 180 | "name": "stdin", 181 | "output_type": "stream", 182 | "text": [ 183 | "Do you want to play again? (Yes/No): No\n" 184 | ] 185 | }, 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "Thanks for playing!\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "import random\n", 196 | "\n", 197 | "name = input(\"Enter your name: \")\n", 198 | "print(f\"Hello {name}! Welcome to the guessing game!\")\n", 199 | "words = [\"guessing\",\"apple\",\"earphones\",\"macbook\",\"python\",\n", 200 | " \"television\", \"sunset\",\"opensource\"]\n", 201 | "# get a random word from this list\n", 202 | "index = random.randint(0, len(words))\n", 203 | "word = words[index]\n", 204 | "indexes = random.sample(range(0, len(word)), 3)\n", 205 | "guesses = \"\" #characters that the user has guessed so far\n", 206 | "for i in indexes:\n", 207 | " guesses += word[i]\n", 208 | "chances = 10\n", 209 | "play = \"Yes\"\n", 210 | "\n", 211 | "def playagain():\n", 212 | " # this will help you understad scoping\n", 213 | " global play\n", 214 | " play = input(\"Do you want to play again? (Yes/No): \")\n", 215 | " if play == \"Yes\":\n", 216 | " global chances, word, guesses\n", 217 | " chances = 10\n", 218 | " index = random.randint(0, len(words))\n", 219 | " word = words[index]\n", 220 | " indexes = random.sample(range(0, len(word)), 3)\n", 221 | " guesses = \"\" \n", 222 | " for i in indexes:\n", 223 | " guesses += word[i]\n", 224 | "\n", 225 | "# this play is global variable\n", 226 | "while play == \"Yes\":\n", 227 | " while chances > 0:\n", 228 | " won = True\n", 229 | " for ch in word:\n", 230 | " if ch in guesses: # the person has guessed\n", 231 | " print(ch, end=\" \")\n", 232 | " else:\n", 233 | " print(\"_\", end=\" \")\n", 234 | " won = False\n", 235 | "\n", 236 | " if won:\n", 237 | " print(\"\\nYou Won!\")\n", 238 | " print(f\"Your score is {chances * 10}\")\n", 239 | " playagain()\n", 240 | " break\n", 241 | "\n", 242 | " # take a guess from the user\n", 243 | " guess = input(\"\\nGuess a character: \")\n", 244 | " guesses += guess\n", 245 | "\n", 246 | " if guess not in word:\n", 247 | " chances -= 1\n", 248 | " print(\"\\nWrong Answer!!\")\n", 249 | " print(f\"You have {chances} chances left!\")\n", 250 | "\n", 251 | " if chances == 0:\n", 252 | " print(\"You Lose!!\")\n", 253 | " playagain()\n", 254 | " break\n", 255 | "\n", 256 | "print(\"Thanks for playing!\")" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": null, 262 | "metadata": {}, 263 | "outputs": [], 264 | "source": [] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "metadata": {}, 270 | "outputs": [], 271 | "source": [] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": null, 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [] 279 | } 280 | ], 281 | "metadata": { 282 | "kernelspec": { 283 | "display_name": "Python 3", 284 | "language": "python", 285 | "name": "python3" 286 | }, 287 | "language_info": { 288 | "codemirror_mode": { 289 | "name": "ipython", 290 | "version": 3 291 | }, 292 | "file_extension": ".py", 293 | "mimetype": "text/x-python", 294 | "name": "python", 295 | "nbconvert_exporter": "python", 296 | "pygments_lexer": "ipython3", 297 | "version": "3.8.0" 298 | } 299 | }, 300 | "nbformat": 4, 301 | "nbformat_minor": 4 302 | } 303 | -------------------------------------------------------------------------------- /Lecture-2/Untitled.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# lists" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "a = [1,3,2,4]" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 4, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "a.insert(2, 34)" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 5, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "[1, 3, 34, 2, 4]" 39 | ] 40 | }, 41 | "execution_count": 5, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "a" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 7, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "a.remove(2)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 8, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "[1, 3, 34, 4]" 68 | ] 69 | }, 70 | "execution_count": 8, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "a" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 9, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "a.reverse()" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 10, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "text/plain": [ 96 | "[4, 34, 3, 1]" 97 | ] 98 | }, 99 | "execution_count": 10, 100 | "metadata": {}, 101 | "output_type": "execute_result" 102 | } 103 | ], 104 | "source": [ 105 | "a" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 11, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "data": { 115 | "text/plain": [ 116 | "\u001b[0;31mSignature:\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mextend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0miterable\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m/\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 117 | "\u001b[0;31mDocstring:\u001b[0m Extend list by appending elements from the iterable.\n", 118 | "\u001b[0;31mType:\u001b[0m builtin_function_or_method\n" 119 | ] 120 | }, 121 | "metadata": {}, 122 | "output_type": "display_data" 123 | } 124 | ], 125 | "source": [ 126 | "a.extend?" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": null, 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [] 135 | } 136 | ], 137 | "metadata": { 138 | "kernelspec": { 139 | "display_name": "Python 3", 140 | "language": "python", 141 | "name": "python3" 142 | }, 143 | "language_info": { 144 | "codemirror_mode": { 145 | "name": "ipython", 146 | "version": 3 147 | }, 148 | "file_extension": ".py", 149 | "mimetype": "text/x-python", 150 | "name": "python", 151 | "nbconvert_exporter": "python", 152 | "pygments_lexer": "ipython3", 153 | "version": "3.8.0" 154 | } 155 | }, 156 | "nbformat": 4, 157 | "nbformat_minor": 4 158 | } 159 | -------------------------------------------------------------------------------- /Lecture-2/Untitled1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# taking inputs" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdin", 19 | "output_type": "stream", 20 | "text": [ 21 | " 35\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "a = input()" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 6, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "name": "stdin", 36 | "output_type": "stream", 37 | "text": [ 38 | " 20\n" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "b = input()" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 7, 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "data": { 53 | "text/plain": [ 54 | "'3520'" 55 | ] 56 | }, 57 | "execution_count": 7, 58 | "metadata": {}, 59 | "output_type": "execute_result" 60 | } 61 | ], 62 | "source": [ 63 | "a + b" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 10, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "a = int(a)\n", 73 | "b = int(b)" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 11, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "data": { 83 | "text/plain": [ 84 | "55" 85 | ] 86 | }, 87 | "execution_count": 11, 88 | "metadata": {}, 89 | "output_type": "execute_result" 90 | } 91 | ], 92 | "source": [ 93 | "a + b" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 13, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "text/plain": [ 104 | "[2, 3, 6]" 105 | ] 106 | }, 107 | "execution_count": 13, 108 | "metadata": {}, 109 | "output_type": "execute_result" 110 | } 111 | ], 112 | "source": [ 113 | "list((2,3,6))" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 14, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [ 122 | "# shotcut" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 15, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "name": "stdin", 132 | "output_type": "stream", 133 | "text": [ 134 | " 20\n", 135 | " 40\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "a = int(input())\n", 141 | "b = int(input())" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 16, 147 | "metadata": {}, 148 | "outputs": [ 149 | { 150 | "data": { 151 | "text/plain": [ 152 | "60" 153 | ] 154 | }, 155 | "execution_count": 16, 156 | "metadata": {}, 157 | "output_type": "execute_result" 158 | } 159 | ], 160 | "source": [ 161 | "a + b" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 20, 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [ 170 | "# display" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 21, 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "name": "stdin", 180 | "output_type": "stream", 181 | "text": [ 182 | "Enter first number: 50\n", 183 | "Enter second number: 47\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "a = int(input(\"Enter first number: \"))\n", 189 | "b = int(input(\"Enter second number: \"))" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 22, 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "data": { 199 | "text/plain": [ 200 | "50" 201 | ] 202 | }, 203 | "execution_count": 22, 204 | "metadata": {}, 205 | "output_type": "execute_result" 206 | } 207 | ], 208 | "source": [ 209 | "a" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 23, 215 | "metadata": {}, 216 | "outputs": [ 217 | { 218 | "data": { 219 | "text/plain": [ 220 | "int" 221 | ] 222 | }, 223 | "execution_count": 23, 224 | "metadata": {}, 225 | "output_type": "execute_result" 226 | } 227 | ], 228 | "source": [ 229 | "type(a)" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 24, 235 | "metadata": {}, 236 | "outputs": [], 237 | "source": [] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": null, 242 | "metadata": {}, 243 | "outputs": [], 244 | "source": [] 245 | } 246 | ], 247 | "metadata": { 248 | "kernelspec": { 249 | "display_name": "Python 3", 250 | "language": "python", 251 | "name": "python3" 252 | }, 253 | "language_info": { 254 | "codemirror_mode": { 255 | "name": "ipython", 256 | "version": 3 257 | }, 258 | "file_extension": ".py", 259 | "mimetype": "text/x-python", 260 | "name": "python", 261 | "nbconvert_exporter": "python", 262 | "pygments_lexer": "ipython3", 263 | "version": "3.8.0" 264 | } 265 | }, 266 | "nbformat": 4, 267 | "nbformat_minor": 4 268 | } 269 | -------------------------------------------------------------------------------- /Lecture-2/Untitled10.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# closures" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 15, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "def outer_func():\n", 19 | " message = \"hi\"\n", 20 | " \n", 21 | " def inner_func():\n", 22 | " print(locals())\n", 23 | " print(message) # free variable\n", 24 | " \n", 25 | " return inner_func" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 16, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "my_func = outer_func() # my_func = inner_func" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 17, 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "name": "stdout", 44 | "output_type": "stream", 45 | "text": [ 46 | "{'message': 'hi'}\n", 47 | "hi\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "my_func()" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 24, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "def make_multi(n):\n", 62 | " def multiplier(x):\n", 63 | " print(locals())\n", 64 | " return x * n\n", 65 | " return multiplier" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 25, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "times2 = make_multi(2)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 26, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "times3 = make_multi(3)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 27, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "{'x': 9, 'n': 3}\n" 96 | ] 97 | }, 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "27" 102 | ] 103 | }, 104 | "execution_count": 27, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "times3(9)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": null, 116 | "metadata": {}, 117 | "outputs": [], 118 | "source": [] 119 | } 120 | ], 121 | "metadata": { 122 | "kernelspec": { 123 | "display_name": "Python 3", 124 | "language": "python", 125 | "name": "python3" 126 | }, 127 | "language_info": { 128 | "codemirror_mode": { 129 | "name": "ipython", 130 | "version": 3 131 | }, 132 | "file_extension": ".py", 133 | "mimetype": "text/x-python", 134 | "name": "python", 135 | "nbconvert_exporter": "python", 136 | "pygments_lexer": "ipython3", 137 | "version": "3.8.0" 138 | } 139 | }, 140 | "nbformat": 4, 141 | "nbformat_minor": 4 142 | } 143 | -------------------------------------------------------------------------------- /Lecture-2/Untitled3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Printing!" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 4, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "34-56\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "print(34, 56, sep=\"-\")" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 5, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "# string formating" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 6, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "a = 30\n", 45 | "b = 47" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 7, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "The two numbers are 30 and 47\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "print(\"The two numbers are\", a, \"and\", b)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 10, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "The two numbers are 30 and 47\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "print(\"The two numbers are {} and {}\".format(a, b))" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 11, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "# the best way" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 12, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "# f strings" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 13, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "The two numbers are 30 and 47\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "print(f\"The two numbers are {a} and {b}\")" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [] 123 | } 124 | ], 125 | "metadata": { 126 | "kernelspec": { 127 | "display_name": "Python 3", 128 | "language": "python", 129 | "name": "python3" 130 | }, 131 | "language_info": { 132 | "codemirror_mode": { 133 | "name": "ipython", 134 | "version": 3 135 | }, 136 | "file_extension": ".py", 137 | "mimetype": "text/x-python", 138 | "name": "python", 139 | "nbconvert_exporter": "python", 140 | "pygments_lexer": "ipython3", 141 | "version": "3.8.0" 142 | } 143 | }, 144 | "nbformat": 4, 145 | "nbformat_minor": 4 146 | } 147 | -------------------------------------------------------------------------------- /Lecture-2/Untitled4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "14" 12 | ] 13 | }, 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "sum([2,3,4,5])" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [] 29 | } 30 | ], 31 | "metadata": { 32 | "kernelspec": { 33 | "display_name": "Python 3", 34 | "language": "python", 35 | "name": "python3" 36 | }, 37 | "language_info": { 38 | "codemirror_mode": { 39 | "name": "ipython", 40 | "version": 3 41 | }, 42 | "file_extension": ".py", 43 | "mimetype": "text/x-python", 44 | "name": "python", 45 | "nbconvert_exporter": "python", 46 | "pygments_lexer": "ipython3", 47 | "version": "3.8.0" 48 | } 49 | }, 50 | "nbformat": 4, 51 | "nbformat_minor": 4 52 | } 53 | -------------------------------------------------------------------------------- /Lecture-2/Untitled5.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# simplest class" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 4, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "class Human:\n", 19 | " age = 0\n", 20 | " name = \"gumnaam\"" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 9, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "kunal = Human()\n", 30 | "# whenever you create an object, a constructor is called\n", 31 | "# constructor is a function" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 10, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "apoorv = Human()" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 11, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "(0, 0)" 52 | ] 53 | }, 54 | "execution_count": 11, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [ 60 | "kunal.age, apoorv.age" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 12, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "kunal.age = 20\n", 70 | "apoorv.age = 56" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 15, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "kunal.name = \"Kunal Kushwaha\"\n", 80 | "apoorv.name = \"Apoorv Goyal\"" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 17, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "data": { 90 | "text/plain": [ 91 | "'Apoorv Goyal'" 92 | ] 93 | }, 94 | "execution_count": 17, 95 | "metadata": {}, 96 | "output_type": "execute_result" 97 | } 98 | ], 99 | "source": [ 100 | "apoorv.name" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 18, 106 | "metadata": {}, 107 | "outputs": [ 108 | { 109 | "data": { 110 | "text/plain": [ 111 | "20" 112 | ] 113 | }, 114 | "execution_count": 18, 115 | "metadata": {}, 116 | "output_type": "execute_result" 117 | } 118 | ], 119 | "source": [ 120 | "kunal.age" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 19, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "data": { 130 | "text/plain": [ 131 | "0" 132 | ] 133 | }, 134 | "execution_count": 19, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "Human.age" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 20, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "'gumnaam'" 152 | ] 153 | }, 154 | "execution_count": 20, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "Human.name" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 21, 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [ 169 | "kunal.salary = 5000" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 22, 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "data": { 179 | "text/plain": [ 180 | "5000" 181 | ] 182 | }, 183 | "execution_count": 22, 184 | "metadata": {}, 185 | "output_type": "execute_result" 186 | } 187 | ], 188 | "source": [ 189 | "kunal.salary" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 24, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [ 198 | "Human.salary = 10000" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 25, 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "5000" 210 | ] 211 | }, 212 | "execution_count": 25, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "kunal.salary" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 26, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "data": { 228 | "text/plain": [ 229 | "10000" 230 | ] 231 | }, 232 | "execution_count": 26, 233 | "metadata": {}, 234 | "output_type": "execute_result" 235 | } 236 | ], 237 | "source": [ 238 | "apoorv.salary" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 27, 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [ 247 | "# solution?" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 28, 253 | "metadata": {}, 254 | "outputs": [], 255 | "source": [ 256 | "# i want to assign these values, when the object is created" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 49, 262 | "metadata": {}, 263 | "outputs": [], 264 | "source": [ 265 | "class Human:\n", 266 | " age = 10\n", 267 | " def fun(self):\n", 268 | " print(self.age)" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 55, 274 | "metadata": {}, 275 | "outputs": [], 276 | "source": [ 277 | "apoorv = Human()" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 56, 283 | "metadata": {}, 284 | "outputs": [], 285 | "source": [ 286 | "apoorv.age = 45" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": 59, 292 | "metadata": {}, 293 | "outputs": [], 294 | "source": [ 295 | "kunal = Human()\n", 296 | "kunal.age = 89" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 57, 302 | "metadata": {}, 303 | "outputs": [ 304 | { 305 | "name": "stdout", 306 | "output_type": "stream", 307 | "text": [ 308 | "45\n" 309 | ] 310 | } 311 | ], 312 | "source": [ 313 | "apoorv.fun()\n", 314 | "# This is same as:" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 54, 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "name": "stdout", 324 | "output_type": "stream", 325 | "text": [ 326 | "45\n" 327 | ] 328 | } 329 | ], 330 | "source": [ 331 | "Human.fun(apoorv)" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 60, 337 | "metadata": {}, 338 | "outputs": [ 339 | { 340 | "name": "stdout", 341 | "output_type": "stream", 342 | "text": [ 343 | "89\n" 344 | ] 345 | } 346 | ], 347 | "source": [ 348 | "kunal.fun()" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 77, 354 | "metadata": {}, 355 | "outputs": [], 356 | "source": [ 357 | "class Human:\n", 358 | " age = 10\n", 359 | " def fun(self):\n", 360 | " print(self.age)" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 78, 366 | "metadata": {}, 367 | "outputs": [], 368 | "source": [ 369 | "a = Human()" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": 79, 375 | "metadata": {}, 376 | "outputs": [], 377 | "source": [ 378 | "a.age = 45" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": 80, 384 | "metadata": {}, 385 | "outputs": [ 386 | { 387 | "name": "stdout", 388 | "output_type": "stream", 389 | "text": [ 390 | "45\n" 391 | ] 392 | } 393 | ], 394 | "source": [ 395 | "a.fun()" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": 81, 401 | "metadata": {}, 402 | "outputs": [ 403 | { 404 | "name": "stdout", 405 | "output_type": "stream", 406 | "text": [ 407 | "56\n" 408 | ] 409 | } 410 | ], 411 | "source": [ 412 | "b = Human()\n", 413 | "b.age = 56\n", 414 | "b.fun()" 415 | ] 416 | }, 417 | { 418 | "cell_type": "code", 419 | "execution_count": 87, 420 | "metadata": {}, 421 | "outputs": [], 422 | "source": [ 423 | "class Human:\n", 424 | " # overriding\n", 425 | " def __init__(self, name, age):\n", 426 | " self.age = age\n", 427 | " self.name = name" 428 | ] 429 | }, 430 | { 431 | "cell_type": "code", 432 | "execution_count": 88, 433 | "metadata": {}, 434 | "outputs": [], 435 | "source": [ 436 | "kunal = Human(\"Kunal Kushwaha\", 35)" 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "execution_count": 91, 442 | "metadata": {}, 443 | "outputs": [ 444 | { 445 | "data": { 446 | "text/plain": [ 447 | "('Kunal Kushwaha', 35)" 448 | ] 449 | }, 450 | "execution_count": 91, 451 | "metadata": {}, 452 | "output_type": "execute_result" 453 | } 454 | ], 455 | "source": [ 456 | "kunal.name, kunal.age" 457 | ] 458 | }, 459 | { 460 | "cell_type": "code", 461 | "execution_count": 92, 462 | "metadata": {}, 463 | "outputs": [], 464 | "source": [ 465 | "apoorv = Human(\"Apoorv Goyal\", 30)" 466 | ] 467 | }, 468 | { 469 | "cell_type": "code", 470 | "execution_count": 93, 471 | "metadata": {}, 472 | "outputs": [ 473 | { 474 | "data": { 475 | "text/plain": [ 476 | "('Apoorv Goyal', 30)" 477 | ] 478 | }, 479 | "execution_count": 93, 480 | "metadata": {}, 481 | "output_type": "execute_result" 482 | } 483 | ], 484 | "source": [ 485 | "apoorv.name, apoorv.age" 486 | ] 487 | }, 488 | { 489 | "cell_type": "code", 490 | "execution_count": null, 491 | "metadata": {}, 492 | "outputs": [], 493 | "source": [] 494 | }, 495 | { 496 | "cell_type": "code", 497 | "execution_count": null, 498 | "metadata": {}, 499 | "outputs": [], 500 | "source": [] 501 | }, 502 | { 503 | "cell_type": "code", 504 | "execution_count": null, 505 | "metadata": {}, 506 | "outputs": [], 507 | "source": [] 508 | } 509 | ], 510 | "metadata": { 511 | "kernelspec": { 512 | "display_name": "Python 3", 513 | "language": "python", 514 | "name": "python3" 515 | }, 516 | "language_info": { 517 | "codemirror_mode": { 518 | "name": "ipython", 519 | "version": 3 520 | }, 521 | "file_extension": ".py", 522 | "mimetype": "text/x-python", 523 | "name": "python", 524 | "nbconvert_exporter": "python", 525 | "pygments_lexer": "ipython3", 526 | "version": "3.8.0" 527 | } 528 | }, 529 | "nbformat": 4, 530 | "nbformat_minor": 4 531 | } 532 | -------------------------------------------------------------------------------- /Lecture-2/Untitled6.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "class Human:\n", 10 | " # static variables : common for all objects of that class\n", 11 | " population = 0\n", 12 | " \n", 13 | " def __init__(self, name, age):\n", 14 | " self.age = age\n", 15 | " self.name = name\n", 16 | " \n", 17 | " # when a new human comes, increase the population\n", 18 | " Human.population += 1" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 5, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "data": { 28 | "text/plain": [ 29 | "0" 30 | ] 31 | }, 32 | "execution_count": 5, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "Human.population" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 6, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "kunal = Human(\"Kunal Kushwaha\", 37)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 9, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "apoorv = Human(\"apoorv goyal\", 30)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 10, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "2" 68 | ] 69 | }, 70 | "execution_count": 10, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "kunal.population" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 11, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "2" 88 | ] 89 | }, 90 | "execution_count": 11, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "apoorv.population" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 28, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [ 105 | "kunal.population = kunal.population + 1\n", 106 | "# this is not changing orginal one in class\n", 107 | "# this is creating a new one\n", 108 | "# static variables = DO NOT ACCESS VIA OBJECTS" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 22, 114 | "metadata": {}, 115 | "outputs": [ 116 | { 117 | "data": { 118 | "text/plain": [ 119 | "(7, 2, 2)" 120 | ] 121 | }, 122 | "execution_count": 22, 123 | "metadata": {}, 124 | "output_type": "execute_result" 125 | } 126 | ], 127 | "source": [ 128 | "kunal.population, apoorv.population, Human.population" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 23, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "rahul = Human(\"Rahul\", 28)" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 26, 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "data": { 147 | "text/plain": [ 148 | "(7, 4, 4, 4)" 149 | ] 150 | }, 151 | "execution_count": 26, 152 | "metadata": {}, 153 | "output_type": "execute_result" 154 | } 155 | ], 156 | "source": [ 157 | "kunal.population, apoorv.population, rahul.population, Human.population" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 25, 163 | "metadata": {}, 164 | "outputs": [], 165 | "source": [ 166 | "Human.population += 1" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 43, 172 | "metadata": {}, 173 | "outputs": [], 174 | "source": [ 175 | "# static methods\n", 176 | "class Human:\n", 177 | " # static variables : common for all objects of that class\n", 178 | " population = 0\n", 179 | " \n", 180 | " def __init__(self, name, age):\n", 181 | " self.age = age\n", 182 | " self.name = name\n", 183 | " \n", 184 | " # when a new human comes, increase the population\n", 185 | " Human.population += 1\n", 186 | " \n", 187 | " @staticmethod # this is called a Decorator\n", 188 | " # used to modify behaviour of a function\n", 189 | " # wwe will see this in detail in future webinars\n", 190 | " def fun():\n", 191 | " print(Human.population)" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 44, 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "kunal = Human(\"Kunal\", 36)\n", 201 | "rahul = Human(\"rahul\", 45)" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 46, 207 | "metadata": {}, 208 | "outputs": [ 209 | { 210 | "name": "stdout", 211 | "output_type": "stream", 212 | "text": [ 213 | "2\n" 214 | ] 215 | } 216 | ], 217 | "source": [ 218 | "kunal.fun()" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": null, 224 | "metadata": {}, 225 | "outputs": [], 226 | "source": [] 227 | } 228 | ], 229 | "metadata": { 230 | "kernelspec": { 231 | "display_name": "Python 3", 232 | "language": "python", 233 | "name": "python3" 234 | }, 235 | "language_info": { 236 | "codemirror_mode": { 237 | "name": "ipython", 238 | "version": 3 239 | }, 240 | "file_extension": ".py", 241 | "mimetype": "text/x-python", 242 | "name": "python", 243 | "nbconvert_exporter": "python", 244 | "pygments_lexer": "ipython3", 245 | "version": "3.8.0" 246 | } 247 | }, 248 | "nbformat": 4, 249 | "nbformat_minor": 4 250 | } 251 | -------------------------------------------------------------------------------- /Lecture-2/Untitled7.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 21, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "class Human:\n", 10 | " population = 0\n", 11 | " \n", 12 | " def __init__(self, name, age):\n", 13 | " self.age = age\n", 14 | " self.name = name\n", 15 | " Human.population += 1\n", 16 | " \n", 17 | " @staticmethod \n", 18 | " def fun():\n", 19 | " print(Human.population)\n", 20 | " \n", 21 | " def getName(self):\n", 22 | " print(self.name)\n", 23 | " \n", 24 | " @classmethod\n", 25 | " def atomBomb(cls): # cls ki jagah, human will be passed\n", 26 | " cls.population = 0\n", 27 | " \n", 28 | " @classmethod # decorators use closures internally\n", 29 | " def fromString(cls, string, age):\n", 30 | " return cls(string, age)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 16, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "kunal = Human(\"Kunal\", 30)\n", 40 | "rahul = Human(\"Rahul\", 48)" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 12, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "Human.atomBomb()" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 17, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "apoorv = Human.fromString(\"Apoorv\", 56)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 18, 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "data": { 68 | "text/plain": [ 69 | "3" 70 | ] 71 | }, 72 | "execution_count": 18, 73 | "metadata": {}, 74 | "output_type": "execute_result" 75 | } 76 | ], 77 | "source": [ 78 | "apoorv.population" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 20, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "data": { 88 | "text/plain": [ 89 | "56" 90 | ] 91 | }, 92 | "execution_count": 20, 93 | "metadata": {}, 94 | "output_type": "execute_result" 95 | } 96 | ], 97 | "source": [ 98 | "apoorv.age" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [] 107 | } 108 | ], 109 | "metadata": { 110 | "kernelspec": { 111 | "display_name": "Python 3", 112 | "language": "python", 113 | "name": "python3" 114 | }, 115 | "language_info": { 116 | "codemirror_mode": { 117 | "name": "ipython", 118 | "version": 3 119 | }, 120 | "file_extension": ".py", 121 | "mimetype": "text/x-python", 122 | "name": "python", 123 | "nbconvert_exporter": "python", 124 | "pygments_lexer": "ipython3", 125 | "version": "3.8.0" 126 | } 127 | }, 128 | "nbformat": 4, 129 | "nbformat_minor": 4 130 | } 131 | -------------------------------------------------------------------------------- /Lecture-2/Untitled8.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 21, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "class Parent:\n", 10 | " f = 80\n", 11 | " def __init__(self, value):\n", 12 | " self.value = value" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 22, 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "class Child(Parent):\n", 22 | " def __init__(self, value, name):\n", 23 | " super().__init__(value)\n", 24 | " self.name = name" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 23, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "obj = Child(67, \"Kunal\")" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 25, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "data": { 43 | "text/plain": [ 44 | "(80, 67, 'Kunal')" 45 | ] 46 | }, 47 | "execution_count": 25, 48 | "metadata": {}, 49 | "output_type": "execute_result" 50 | } 51 | ], 52 | "source": [ 53 | "obj.f, obj.value, obj.name" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 15, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "obj2 = Parent(89)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 18, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "# I want to hide some variables\n", 72 | "# jugaad for private variables in python" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 35, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "class A:\n", 82 | " def __init__(self, value):\n", 83 | " self.__value = value\n", 84 | " # python changes __value to _A__value" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 36, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "obj = A(89)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 37, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "text/plain": [ 104 | "89" 105 | ] 106 | }, 107 | "execution_count": 37, 108 | "metadata": {}, 109 | "output_type": "execute_result" 110 | } 111 | ], 112 | "source": [ 113 | "obj._A__value # not really private" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 38, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [ 122 | "# totally depends on the person writing the code" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 39, 128 | "metadata": {}, 129 | "outputs": [], 130 | "source": [ 131 | "# not as good as C++/Java" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [] 140 | } 141 | ], 142 | "metadata": { 143 | "kernelspec": { 144 | "display_name": "Python 3", 145 | "language": "python", 146 | "name": "python3" 147 | }, 148 | "language_info": { 149 | "codemirror_mode": { 150 | "name": "ipython", 151 | "version": 3 152 | }, 153 | "file_extension": ".py", 154 | "mimetype": "text/x-python", 155 | "name": "python", 156 | "nbconvert_exporter": "python", 157 | "pygments_lexer": "ipython3", 158 | "version": "3.8.0" 159 | } 160 | }, 161 | "nbformat": 4, 162 | "nbformat_minor": 4 163 | } 164 | -------------------------------------------------------------------------------- /Lecture-2/Untitled9.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# scoping" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 5, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "def fun():\n", 19 | " a = 10\n", 20 | " print(a)" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 6, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "10\n" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "fun()" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 8, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "# print(a) # cannot access from outside" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 13, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "80\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "a = 20 # this is global\n", 64 | "def fun():\n", 65 | " a = 60 # this is local to function fun\n", 66 | " a = a + 20\n", 67 | " print(a)\n", 68 | "\n", 69 | "fun()" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 14, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "# you can access but cannot change directly" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 15, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [ 87 | "a = 10\n", 88 | "def fun():\n", 89 | " print(a)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 16, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "10\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "fun()" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 28, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [ 115 | "x = 50\n", 116 | "\n", 117 | "def a():\n", 118 | " x = 10 # this is local to a\n", 119 | " # this x = 10 , this is enclosed for b()\n", 120 | " def b():\n", 121 | " x = 40 # this is locl to b\n", 122 | " print(\"x in b() is\", x)\n", 123 | " print(\"locals for b are: \", locals())\n", 124 | " \n", 125 | " print(\"x in a() is\", x)\n", 126 | " print(\"locals for a are: \", locals())\n", 127 | " return b()" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 29, 133 | "metadata": {}, 134 | "outputs": [ 135 | { 136 | "name": "stdout", 137 | "output_type": "stream", 138 | "text": [ 139 | "x in a() is 50\n", 140 | "locals for a are: {'b': .b at 0x10fbd7550>}\n", 141 | "x in b() is 50\n", 142 | "locals for b are: {}\n" 143 | ] 144 | } 145 | ], 146 | "source": [ 147 | "a()" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 38, 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "256\n", 160 | "10\n", 161 | "45\n", 162 | "145\n", 163 | "x ki value in b after running c() 145\n" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "x = 256\n", 169 | "\n", 170 | "def a():\n", 171 | " x = 10\n", 172 | " \n", 173 | " def b():\n", 174 | " x = 45\n", 175 | " \n", 176 | " def c():\n", 177 | " nonlocal x # explicitly bata raha hu\n", 178 | " x += 100\n", 179 | " print(x)\n", 180 | " \n", 181 | " print(x) # 45\n", 182 | " c()\n", 183 | " print(\"x ki value in b after running c()\",x) # 145\n", 184 | " \n", 185 | " print(x)\n", 186 | " b()\n", 187 | "\n", 188 | "print(x)\n", 189 | "a()" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 39, 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "name": "stdout", 199 | "output_type": "stream", 200 | "text": [ 201 | "256\n", 202 | "10\n", 203 | "45\n", 204 | "356\n", 205 | "x ki value in b after running c() 45\n" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "x = 256\n", 211 | "\n", 212 | "def a():\n", 213 | " x = 10\n", 214 | " \n", 215 | " def b():\n", 216 | " x = 45\n", 217 | " \n", 218 | " def c():\n", 219 | " global x # explicitly bata raha hu\n", 220 | " x += 100\n", 221 | " print(x)\n", 222 | " \n", 223 | " print(x) # 45\n", 224 | " c()\n", 225 | " print(\"x ki value in b after running c()\",x) # 145\n", 226 | " \n", 227 | " print(x)\n", 228 | " b()\n", 229 | "\n", 230 | "print(x)\n", 231 | "a()" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 40, 237 | "metadata": {}, 238 | "outputs": [ 239 | { 240 | "data": { 241 | "text/plain": [ 242 | "356" 243 | ] 244 | }, 245 | "execution_count": 40, 246 | "metadata": {}, 247 | "output_type": "execute_result" 248 | } 249 | ], 250 | "source": [ 251 | "x" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": null, 257 | "metadata": {}, 258 | "outputs": [], 259 | "source": [] 260 | } 261 | ], 262 | "metadata": { 263 | "kernelspec": { 264 | "display_name": "Python 3", 265 | "language": "python", 266 | "name": "python3" 267 | }, 268 | "language_info": { 269 | "codemirror_mode": { 270 | "name": "ipython", 271 | "version": 3 272 | }, 273 | "file_extension": ".py", 274 | "mimetype": "text/x-python", 275 | "name": "python", 276 | "nbconvert_exporter": "python", 277 | "pygments_lexer": "ipython3", 278 | "version": "3.8.0" 279 | } 280 | }, 281 | "nbformat": 4, 282 | "nbformat_minor": 4 283 | } 284 | -------------------------------------------------------------------------------- /Lecture-3/.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# modules" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "import random" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 7, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "data": { 28 | "text/plain": [ 29 | "52" 30 | ] 31 | }, 32 | "execution_count": 7, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "random.randint(1, 100)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 8, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "a = [1,3,2,4,5]" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 13, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "4" 59 | ] 60 | }, 61 | "execution_count": 13, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "random.choice(a)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 14, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "import os" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 15, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/os.py'" 88 | ] 89 | }, 90 | "execution_count": 15, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "os.__file__" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 18, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "data": { 106 | "text/plain": [ 107 | "['Untitled.ipynb', '.ipynb_checkpoints']" 108 | ] 109 | }, 110 | "execution_count": 18, 111 | "metadata": {}, 112 | "output_type": "execute_result" 113 | } 114 | ], 115 | "source": [ 116 | "os.listdir() # ls " 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 19, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "data": { 126 | "text/plain": [ 127 | "'/Users/kunalkushwaha/Python-Webinars/Lecture-3'" 128 | ] 129 | }, 130 | "execution_count": 19, 131 | "metadata": {}, 132 | "output_type": "execute_result" 133 | } 134 | ], 135 | "source": [ 136 | "os.getcwd()" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 20, 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [ 145 | "os.mkdir('Demo')" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 22, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "os.rename('Untitled.ipynb', 'Networking.ipynb')" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": null, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [] 163 | } 164 | ], 165 | "metadata": { 166 | "kernelspec": { 167 | "display_name": "Python 3", 168 | "language": "python", 169 | "name": "python3" 170 | }, 171 | "language_info": { 172 | "codemirror_mode": { 173 | "name": "ipython", 174 | "version": 3 175 | }, 176 | "file_extension": ".py", 177 | "mimetype": "text/x-python", 178 | "name": "python", 179 | "nbconvert_exporter": "python", 180 | "pygments_lexer": "ipython3", 181 | "version": "3.8.0" 182 | } 183 | }, 184 | "nbformat": 4, 185 | "nbformat_minor": 4 186 | } 187 | -------------------------------------------------------------------------------- /Lecture-3/.ipynb_checkpoints/Untitled1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# file handling" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "# context managers" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "# reading a file in python" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 7, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "hello my name is kunal\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "with open(\"first.txt\", \"r\") as file:\n", 45 | " print(file.read())" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 8, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "# write in file" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 9, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "with open(\"second.txt\", \"w\") as file:\n", 64 | " file.write(\"Mazza aa gaya\")" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 12, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "# csv : MS Excel" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 13, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "# pandas" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [] 91 | } 92 | ], 93 | "metadata": { 94 | "kernelspec": { 95 | "display_name": "Python 3", 96 | "language": "python", 97 | "name": "python3" 98 | }, 99 | "language_info": { 100 | "codemirror_mode": { 101 | "name": "ipython", 102 | "version": 3 103 | }, 104 | "file_extension": ".py", 105 | "mimetype": "text/x-python", 106 | "name": "python", 107 | "nbconvert_exporter": "python", 108 | "pygments_lexer": "ipython3", 109 | "version": "3.8.0" 110 | } 111 | }, 112 | "nbformat": 4, 113 | "nbformat_minor": 4 114 | } 115 | -------------------------------------------------------------------------------- /Lecture-3/.ipynb_checkpoints/Untitled2-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 46, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "url = 'http://www.africau.edu/images/default/sample.pdf'" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 47, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "import requests\n", 19 | "from tqdm import tqdm\n", 20 | "import math\n", 21 | "import time" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 48, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "# at one time, i just want to download 256 bytes\n", 31 | "chunk_size = 256" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 49, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "data": { 41 | "text/plain": [ 42 | "941" 43 | ] 44 | }, 45 | "execution_count": 49, 46 | "metadata": {}, 47 | "output_type": "execute_result" 48 | } 49 | ], 50 | "source": [ 51 | "s = int(r.headers['Content-Length'])\n", 52 | "s" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 50, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "r = requests.get(url, stream=True) # will not get data at once, will get data one by one" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 51, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "n = math.ceil(s/chunk_size)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 52, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "# file handling" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 53, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "name": "stderr", 89 | "output_type": "stream", 90 | "text": [ 91 | "100%|██████████| 4/4 [00:02<00:00, 1.99it/s]\n" 92 | ] 93 | } 94 | ], 95 | "source": [ 96 | "with open(\"file.pdf\", \"wb\") as file:\n", 97 | " for chunk in tqdm(r.iter_content(chunk_size=chunk_size), total=n):\n", 98 | " time.sleep(0.5)\n", 99 | " file.write(chunk)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [] 115 | } 116 | ], 117 | "metadata": { 118 | "kernelspec": { 119 | "display_name": "Python 3", 120 | "language": "python", 121 | "name": "python3" 122 | }, 123 | "language_info": { 124 | "codemirror_mode": { 125 | "name": "ipython", 126 | "version": 3 127 | }, 128 | "file_extension": ".py", 129 | "mimetype": "text/x-python", 130 | "name": "python", 131 | "nbconvert_exporter": "python", 132 | "pygments_lexer": "ipython3", 133 | "version": "3.8.0" 134 | } 135 | }, 136 | "nbformat": 4, 137 | "nbformat_minor": 4 138 | } 139 | -------------------------------------------------------------------------------- /Lecture-3/.ipynb_checkpoints/Untitled3-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# animations" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "# animation on for loop" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 5, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "from tqdm import tqdm\n", 28 | "import time" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 6, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "a = [3,4,22, 78]" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 12, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stderr", 47 | "output_type": "stream", 48 | "text": [ 49 | " 25%|██▌ | 1/4 [00:01<00:03, 1.00s/it]" 50 | ] 51 | }, 52 | { 53 | "name": "stdout", 54 | "output_type": "stream", 55 | "text": [ 56 | "3\n" 57 | ] 58 | }, 59 | { 60 | "name": "stderr", 61 | "output_type": "stream", 62 | "text": [ 63 | " 50%|█████ | 2/4 [00:02<00:02, 1.00s/it]" 64 | ] 65 | }, 66 | { 67 | "name": "stdout", 68 | "output_type": "stream", 69 | "text": [ 70 | "4\n" 71 | ] 72 | }, 73 | { 74 | "name": "stderr", 75 | "output_type": "stream", 76 | "text": [ 77 | " 75%|███████▌ | 3/4 [00:03<00:01, 1.00s/it]" 78 | ] 79 | }, 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "22\n" 85 | ] 86 | }, 87 | { 88 | "name": "stderr", 89 | "output_type": "stream", 90 | "text": [ 91 | "100%|██████████| 4/4 [00:04<00:00, 1.00s/it]" 92 | ] 93 | }, 94 | { 95 | "name": "stdout", 96 | "output_type": "stream", 97 | "text": [ 98 | "78\n", 99 | "for loop finisdhed\n" 100 | ] 101 | }, 102 | { 103 | "name": "stderr", 104 | "output_type": "stream", 105 | "text": [ 106 | "\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "for i in tqdm(a):\n", 112 | " # wait for 1 sec so that you guys can see\n", 113 | " time.sleep(1)\n", 114 | " print(i)\n", 115 | "print(\"for loop finisdhed\")" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [] 124 | } 125 | ], 126 | "metadata": { 127 | "kernelspec": { 128 | "display_name": "Python 3", 129 | "language": "python", 130 | "name": "python3" 131 | }, 132 | "language_info": { 133 | "codemirror_mode": { 134 | "name": "ipython", 135 | "version": 3 136 | }, 137 | "file_extension": ".py", 138 | "mimetype": "text/x-python", 139 | "name": "python", 140 | "nbconvert_exporter": "python", 141 | "pygments_lexer": "ipython3", 142 | "version": "3.8.0" 143 | } 144 | }, 145 | "nbformat": 4, 146 | "nbformat_minor": 4 147 | } 148 | -------------------------------------------------------------------------------- /Lecture-3/.ipynb_checkpoints/file-checkpoint.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeforcauseorg/Python-Webinars/19cab1b01af269f5eb19dd7126057f3c832cd477/Lecture-3/.ipynb_checkpoints/file-checkpoint.pdf -------------------------------------------------------------------------------- /Lecture-3/.ipynb_checkpoints/first-checkpoint.txt: -------------------------------------------------------------------------------- 1 | hello my name is kunal -------------------------------------------------------------------------------- /Lecture-3/.ipynb_checkpoints/second-checkpoint.txt: -------------------------------------------------------------------------------- 1 | Mazza aa gaya -------------------------------------------------------------------------------- /Lecture-3/Untitled.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# modules" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "import random" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 7, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "data": { 28 | "text/plain": [ 29 | "52" 30 | ] 31 | }, 32 | "execution_count": 7, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "random.randint(1, 100)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 8, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "a = [1,3,2,4,5]" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 13, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "4" 59 | ] 60 | }, 61 | "execution_count": 13, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "random.choice(a)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 14, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "import os" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 15, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/os.py'" 88 | ] 89 | }, 90 | "execution_count": 15, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "os.__file__" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 18, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "data": { 106 | "text/plain": [ 107 | "['Untitled.ipynb', '.ipynb_checkpoints']" 108 | ] 109 | }, 110 | "execution_count": 18, 111 | "metadata": {}, 112 | "output_type": "execute_result" 113 | } 114 | ], 115 | "source": [ 116 | "os.listdir() # ls " 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 19, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "data": { 126 | "text/plain": [ 127 | "'/Users/kunalkushwaha/Python-Webinars/Lecture-3'" 128 | ] 129 | }, 130 | "execution_count": 19, 131 | "metadata": {}, 132 | "output_type": "execute_result" 133 | } 134 | ], 135 | "source": [ 136 | "os.getcwd()" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 20, 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [ 145 | "os.mkdir('Demo')" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 22, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "os.rename('Untitled.ipynb', 'Networking.ipynb')" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": null, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [] 163 | } 164 | ], 165 | "metadata": { 166 | "kernelspec": { 167 | "display_name": "Python 3", 168 | "language": "python", 169 | "name": "python3" 170 | }, 171 | "language_info": { 172 | "codemirror_mode": { 173 | "name": "ipython", 174 | "version": 3 175 | }, 176 | "file_extension": ".py", 177 | "mimetype": "text/x-python", 178 | "name": "python", 179 | "nbconvert_exporter": "python", 180 | "pygments_lexer": "ipython3", 181 | "version": "3.8.0" 182 | } 183 | }, 184 | "nbformat": 4, 185 | "nbformat_minor": 4 186 | } 187 | -------------------------------------------------------------------------------- /Lecture-3/Untitled1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# file handling" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "# context managers" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "# reading a file in python" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 7, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "hello my name is kunal\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "with open(\"first.txt\", \"r\") as file:\n", 45 | " print(file.read())" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 8, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "# write in file" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 9, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "with open(\"second.txt\", \"w\") as file:\n", 64 | " file.write(\"Mazza aa gaya\")" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 12, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "# csv : MS Excel" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 13, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "# pandas" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [] 91 | } 92 | ], 93 | "metadata": { 94 | "kernelspec": { 95 | "display_name": "Python 3", 96 | "language": "python", 97 | "name": "python3" 98 | }, 99 | "language_info": { 100 | "codemirror_mode": { 101 | "name": "ipython", 102 | "version": 3 103 | }, 104 | "file_extension": ".py", 105 | "mimetype": "text/x-python", 106 | "name": "python", 107 | "nbconvert_exporter": "python", 108 | "pygments_lexer": "ipython3", 109 | "version": "3.8.0" 110 | } 111 | }, 112 | "nbformat": 4, 113 | "nbformat_minor": 4 114 | } 115 | -------------------------------------------------------------------------------- /Lecture-3/Untitled2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 46, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "url = 'http://www.africau.edu/images/default/sample.pdf'" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 47, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "import requests\n", 19 | "from tqdm import tqdm\n", 20 | "import math\n", 21 | "import time" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 48, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "# at one time, i just want to download 256 bytes\n", 31 | "chunk_size = 256" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 49, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "data": { 41 | "text/plain": [ 42 | "941" 43 | ] 44 | }, 45 | "execution_count": 49, 46 | "metadata": {}, 47 | "output_type": "execute_result" 48 | } 49 | ], 50 | "source": [ 51 | "s = int(r.headers['Content-Length'])\n", 52 | "s" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 50, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "r = requests.get(url, stream=True) # will not get data at once, will get data one by one" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 51, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "n = math.ceil(s/chunk_size)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 52, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "# file handling" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 53, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "name": "stderr", 89 | "output_type": "stream", 90 | "text": [ 91 | "100%|██████████| 4/4 [00:02<00:00, 1.99it/s]\n" 92 | ] 93 | } 94 | ], 95 | "source": [ 96 | "with open(\"file.pdf\", \"wb\") as file:\n", 97 | " for chunk in tqdm(r.iter_content(chunk_size=chunk_size), total=n):\n", 98 | " time.sleep(0.5)\n", 99 | " file.write(chunk)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [] 115 | } 116 | ], 117 | "metadata": { 118 | "kernelspec": { 119 | "display_name": "Python 3", 120 | "language": "python", 121 | "name": "python3" 122 | }, 123 | "language_info": { 124 | "codemirror_mode": { 125 | "name": "ipython", 126 | "version": 3 127 | }, 128 | "file_extension": ".py", 129 | "mimetype": "text/x-python", 130 | "name": "python", 131 | "nbconvert_exporter": "python", 132 | "pygments_lexer": "ipython3", 133 | "version": "3.8.0" 134 | } 135 | }, 136 | "nbformat": 4, 137 | "nbformat_minor": 4 138 | } 139 | -------------------------------------------------------------------------------- /Lecture-3/Untitled3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# animations" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "# animation on for loop" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 5, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "from tqdm import tqdm\n", 28 | "import time" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 6, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "a = [3,4,22, 78]" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 12, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stderr", 47 | "output_type": "stream", 48 | "text": [ 49 | " 25%|██▌ | 1/4 [00:01<00:03, 1.00s/it]" 50 | ] 51 | }, 52 | { 53 | "name": "stdout", 54 | "output_type": "stream", 55 | "text": [ 56 | "3\n" 57 | ] 58 | }, 59 | { 60 | "name": "stderr", 61 | "output_type": "stream", 62 | "text": [ 63 | " 50%|█████ | 2/4 [00:02<00:02, 1.00s/it]" 64 | ] 65 | }, 66 | { 67 | "name": "stdout", 68 | "output_type": "stream", 69 | "text": [ 70 | "4\n" 71 | ] 72 | }, 73 | { 74 | "name": "stderr", 75 | "output_type": "stream", 76 | "text": [ 77 | " 75%|███████▌ | 3/4 [00:03<00:01, 1.00s/it]" 78 | ] 79 | }, 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "22\n" 85 | ] 86 | }, 87 | { 88 | "name": "stderr", 89 | "output_type": "stream", 90 | "text": [ 91 | "100%|██████████| 4/4 [00:04<00:00, 1.00s/it]" 92 | ] 93 | }, 94 | { 95 | "name": "stdout", 96 | "output_type": "stream", 97 | "text": [ 98 | "78\n", 99 | "for loop finisdhed\n" 100 | ] 101 | }, 102 | { 103 | "name": "stderr", 104 | "output_type": "stream", 105 | "text": [ 106 | "\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "for i in tqdm(a):\n", 112 | " # wait for 1 sec so that you guys can see\n", 113 | " time.sleep(1)\n", 114 | " print(i)\n", 115 | "print(\"for loop finisdhed\")" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [] 124 | } 125 | ], 126 | "metadata": { 127 | "kernelspec": { 128 | "display_name": "Python 3", 129 | "language": "python", 130 | "name": "python3" 131 | }, 132 | "language_info": { 133 | "codemirror_mode": { 134 | "name": "ipython", 135 | "version": 3 136 | }, 137 | "file_extension": ".py", 138 | "mimetype": "text/x-python", 139 | "name": "python", 140 | "nbconvert_exporter": "python", 141 | "pygments_lexer": "ipython3", 142 | "version": "3.8.0" 143 | } 144 | }, 145 | "nbformat": 4, 146 | "nbformat_minor": 4 147 | } 148 | -------------------------------------------------------------------------------- /Lecture-3/chat/client.py: -------------------------------------------------------------------------------- 1 | import socket 2 | port = 3000 3 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # create a socket object 4 | CHUNK = 65535 5 | # instead of explicitly binding the socket, i will let OS do it. 6 | # ephemaral ports 7 | # OS will bind this for us 8 | hostname = '127.0.0.1' 9 | while True: 10 | s.connect((hostname, port)) 11 | message = input("Type message: ") 12 | data = message.encode("ascii") 13 | s.send(data) 14 | # data received: 15 | data = s.recv(CHUNK) 16 | text = data.decode('ascii') 17 | print(f'Kunal: {text}') 18 | -------------------------------------------------------------------------------- /Lecture-3/chat/server.py: -------------------------------------------------------------------------------- 1 | import socket 2 | CHUNK = 65535 # receive at most these bytes of data at once 3 | port = 3000 4 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # create a socket object 5 | # socket.socket(family, type) 6 | # AF_NET : family of ipv4 ip address 7 | # SOCK_DGRAM: UDP, SOCK_STREAM: TCP 8 | 9 | # some ip address that the server will listen to when message comes 10 | hostname = '127.0.0.1' # ip add of local machine, same for everyone 11 | # aka home, there is no place like 127.0.0.1 12 | 13 | s.bind((hostname, port)) # bind the socket with ths host machine and on port 3000 14 | 15 | print(f"server is live on {s.getsockname()}")# 16 | 17 | # run this server ifinitely, till i stop manually 18 | while True: # infinite loop 19 | data, clientAdd = s.recvfrom(CHUNK) 20 | message = data.decode('ascii') # data by default travels in bytes 21 | # print(f"Apoorv at {clientAdd} Says: {message}") 22 | print(f"Apoorv: {message}") 23 | message_send = input("Reply: ") 24 | data = message_send.encode('ascii') 25 | # send data to the ip add that sent me the data 26 | s.sendto(data, clientAdd) 27 | -------------------------------------------------------------------------------- /Lecture-3/downloadfile.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeforcauseorg/Python-Webinars/19cab1b01af269f5eb19dd7126057f3c832cd477/Lecture-3/downloadfile.py -------------------------------------------------------------------------------- /Lecture-3/file.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeforcauseorg/Python-Webinars/19cab1b01af269f5eb19dd7126057f3c832cd477/Lecture-3/file.pdf -------------------------------------------------------------------------------- /Lecture-3/first.txt: -------------------------------------------------------------------------------- 1 | hello my name is kunal -------------------------------------------------------------------------------- /Lecture-3/randomEpisode.py: -------------------------------------------------------------------------------- 1 | import os 2 | import random 3 | 4 | p = '/Users/kunalkushwaha/Desktop/FRIENDS' 5 | os.chdir(p) 6 | 7 | # print(os.listdir(p)) 8 | folder_name = random.choice(os.listdir(p)) 9 | 10 | folder_path = str(os.path.realpath(folder_name)) 11 | os.chdir(folder_path) 12 | file_name = random.choice(os.listdir(folder_path)) 13 | 14 | print("Enjoy!") 15 | 16 | # play to file 17 | 18 | os.system("open " + file_name) # mac 19 | # os.system("start" + filename) # windows 20 | -------------------------------------------------------------------------------- /Lecture-3/second.txt: -------------------------------------------------------------------------------- 1 | Mazza aa gaya -------------------------------------------------------------------------------- /Lecture-4/.ipynb_checkpoints/puppy-checkpoint.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeforcauseorg/Python-Webinars/19cab1b01af269f5eb19dd7126057f3c832cd477/Lecture-4/.ipynb_checkpoints/puppy-checkpoint.jpg -------------------------------------------------------------------------------- /Lecture-4/fb_pictures/10_img.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeforcauseorg/Python-Webinars/19cab1b01af269f5eb19dd7126057f3c832cd477/Lecture-4/fb_pictures/10_img.jpg -------------------------------------------------------------------------------- /Lecture-4/fb_pictures/4_img.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeforcauseorg/Python-Webinars/19cab1b01af269f5eb19dd7126057f3c832cd477/Lecture-4/fb_pictures/4_img.jpg -------------------------------------------------------------------------------- /Lecture-4/fb_pictures/5_img.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeforcauseorg/Python-Webinars/19cab1b01af269f5eb19dd7126057f3c832cd477/Lecture-4/fb_pictures/5_img.jpg -------------------------------------------------------------------------------- /Lecture-4/fb_pictures/6_img.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeforcauseorg/Python-Webinars/19cab1b01af269f5eb19dd7126057f3c832cd477/Lecture-4/fb_pictures/6_img.jpg -------------------------------------------------------------------------------- /Lecture-4/fb_pictures/7_img.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeforcauseorg/Python-Webinars/19cab1b01af269f5eb19dd7126057f3c832cd477/Lecture-4/fb_pictures/7_img.jpg -------------------------------------------------------------------------------- /Lecture-4/fb_pictures/8_img.jpg: -------------------------------------------------------------------------------- 1 | {"error":{"message":"An access token is required to request this resource.","type":"OAuthException","code":104,"fbtrace_id":"ABfzO_xm1lsSlMFBM6uOW9y"}} -------------------------------------------------------------------------------- /Lecture-4/fb_pictures/9_img.jpg: -------------------------------------------------------------------------------- 1 | {"error":{"message":"An access token is required to request this resource.","type":"OAuthException","code":104,"fbtrace_id":"Aa-UI_7_r_6guL0ifUfuNT-"}} -------------------------------------------------------------------------------- /Lecture-4/pug.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeforcauseorg/Python-Webinars/19cab1b01af269f5eb19dd7126057f3c832cd477/Lecture-4/pug.jpg -------------------------------------------------------------------------------- /Lecture-4/puppy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeforcauseorg/Python-Webinars/19cab1b01af269f5eb19dd7126057f3c832cd477/Lecture-4/puppy.jpg -------------------------------------------------------------------------------- /Lecture-5/.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture-5/.ipynb_checkpoints/Untitled1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture-5/.ipynb_checkpoints/Untitled2-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture-5/.ipynb_checkpoints/Untitled3-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture-5/.ipynb_checkpoints/Untitled4-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# generators" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "# simpler way of creating iterators" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "# it returns an iterators over which we can iterate upon" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 4, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "# yield = works like the return statement" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 6, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "# why is efficient? \n", 46 | "# it does not hold all the values in memory\n", 47 | "# it is just going to store the state\n", 48 | "# you dont need to worry bout error handling" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 16, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "def square(nums):\n", 58 | " for i in nums:\n", 59 | " yield (i * i)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 17, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "obj = square([1,2,3,4,5])" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 22, 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "data": { 78 | "text/plain": [ 79 | "25" 80 | ] 81 | }, 82 | "execution_count": 22, 83 | "metadata": {}, 84 | "output_type": "execute_result" 85 | } 86 | ], 87 | "source": [ 88 | "next(obj)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 23, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "# create generators in one line!" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 33, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "obj = (num * num for num in [1,2,3,4,5,6]) # using generators" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 36, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [ 115 | "# using iterators\n", 116 | "class PT:\n", 117 | " def __init__(self, max=0):\n", 118 | " self.max = max\n", 119 | " \n", 120 | " def __iter__(self):\n", 121 | " self.n = 0\n", 122 | " return self # very important\n", 123 | " \n", 124 | " def __next__(self):\n", 125 | " if self.n <= self.max:\n", 126 | " result = 2 ** self.n\n", 127 | " self.n += 1\n", 128 | " return result\n", 129 | " else:\n", 130 | " raise StopIteration" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 39, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "obj = [num * num for num in [1,2,3,4,5,6]] # being treated like iterator" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 40, 145 | "metadata": {}, 146 | "outputs": [ 147 | { 148 | "data": { 149 | "text/plain": [ 150 | "[1, 4, 9, 16, 25, 36]" 151 | ] 152 | }, 153 | "execution_count": 40, 154 | "metadata": {}, 155 | "output_type": "execute_result" 156 | } 157 | ], 158 | "source": [ 159 | "obj" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [] 168 | } 169 | ], 170 | "metadata": { 171 | "kernelspec": { 172 | "display_name": "Python 3", 173 | "language": "python", 174 | "name": "python3" 175 | }, 176 | "language_info": { 177 | "codemirror_mode": { 178 | "name": "ipython", 179 | "version": 3 180 | }, 181 | "file_extension": ".py", 182 | "mimetype": "text/x-python", 183 | "name": "python", 184 | "nbconvert_exporter": "python", 185 | "pygments_lexer": "ipython3", 186 | "version": "3.8.0" 187 | } 188 | }, 189 | "nbformat": 4, 190 | "nbformat_minor": 4 191 | } 192 | -------------------------------------------------------------------------------- /Lecture-5/.ipynb_checkpoints/Untitled5-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture-5/.ipynb_checkpoints/Untitled6-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture-5/.ipynb_checkpoints/decorators-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lecture-5/.ipynb_checkpoints/exception_handling-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 6, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Sorry this is not allowed\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "try:\n", 18 | " a = d\n", 19 | "except Exception:\n", 20 | " print(\"Sorry this is not allowed\")" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 10, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "Sorry this file does not exist\n" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "try:\n", 38 | " f = open(\"test.txt\") # this will trhow an error because file does not exist\n", 39 | "except FileNotFoundError:\n", 40 | " print(\"Sorry this file does not exist\")" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": null, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 12, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "Sorry this file does not exist\n" 60 | ] 61 | } 62 | ], 63 | "source": [ 64 | "try:\n", 65 | " a = e # this will trhow an error because file does not exist\n", 66 | "except Exception:\n", 67 | " print(\"Sorry this file does not exist\")" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 13, 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "Sorry this file does not exist\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "try:\n", 85 | " f = open(\"first.txt\")\n", 86 | " a = e # this will trhow an error because file does not exist\n", 87 | "except Exception:\n", 88 | " print(\"Sorry this file does not exist\")" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 14, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "# multiple except values" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 25, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "will this be printed?\n", 110 | "[Errno 2] No such file or directory: 'file.txt'\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "try:\n", 116 | "# d = iii\n", 117 | " print(\"will this be printed?\")\n", 118 | " f = open(\"file.txt\") # this raises an error\n", 119 | " d = iii\n", 120 | " print(\"I did not run\")\n", 121 | "except FileNotFoundError as e:\n", 122 | " print(e)\n", 123 | "except Exception as e:\n", 124 | " print(e)" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 27, 130 | "metadata": {}, 131 | "outputs": [], 132 | "source": [ 133 | "# else block: it contains the code that will run if try block does not raise any exception" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 31, 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "name": "stdout", 143 | "output_type": "stream", 144 | "text": [ 145 | "heelo my name is Kunal.\n" 146 | ] 147 | } 148 | ], 149 | "source": [ 150 | "try:\n", 151 | " f = open(\"first.txt\")\n", 152 | "except Exception:\n", 153 | " print(\"Error aa gaya\")\n", 154 | "else: # this runs if try block is successful\n", 155 | " print(f.read())\n", 156 | " f.close() # you can use context managers" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 32, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "# finally block: runs the code no matter what happens" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 35, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "Error aa gaya\n", 178 | "this will run always\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "try:\n", 184 | " f = open(\"fir.txt\")\n", 185 | "except Exception:\n", 186 | " print(\"Error aa gaya\")\n", 187 | "else: # this runs if try block is successful\n", 188 | " print(f.read())\n", 189 | " f.close() # you can use context managers\n", 190 | "finally:\n", 191 | " # get rid of the resources\n", 192 | " print(\"this will run always\")" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [] 201 | } 202 | ], 203 | "metadata": { 204 | "kernelspec": { 205 | "display_name": "Python 3", 206 | "language": "python", 207 | "name": "python3" 208 | }, 209 | "language_info": { 210 | "codemirror_mode": { 211 | "name": "ipython", 212 | "version": 3 213 | }, 214 | "file_extension": ".py", 215 | "mimetype": "text/x-python", 216 | "name": "python", 217 | "nbconvert_exporter": "python", 218 | "pygments_lexer": "ipython3", 219 | "version": "3.8.0" 220 | } 221 | }, 222 | "nbformat": 4, 223 | "nbformat_minor": 4 224 | } 225 | -------------------------------------------------------------------------------- /Lecture-5/.ipynb_checkpoints/first-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# __init__ vs __call__" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "class A:\n", 19 | " def __init__(self):\n", 20 | " print(\"Constructor call ho gaya\")\n", 21 | " \n", 22 | " def __call__(self):\n", 23 | " print(\"call called\")" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 3, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Constructor call ho gaya\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "a = A()" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 4, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "call called\n" 53 | ] 54 | } 55 | ], 56 | "source": [ 57 | "a()" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 9, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [ 66 | "class Human(object):\n", 67 | " def __init__(self, name):\n", 68 | " self.name = name\n", 69 | " print(self.name)\n", 70 | " def __call__(self, name):\n", 71 | " self.name = name\n", 72 | " print(self.name)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 11, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "Kunal\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "kunal = Human(\"Kunal\")" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 12, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "Kunal 2.0\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "kunal(\"Kunal 2.0\")" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [] 115 | } 116 | ], 117 | "metadata": { 118 | "kernelspec": { 119 | "display_name": "Python 3", 120 | "language": "python", 121 | "name": "python3" 122 | }, 123 | "language_info": { 124 | "codemirror_mode": { 125 | "name": "ipython", 126 | "version": 3 127 | }, 128 | "file_extension": ".py", 129 | "mimetype": "text/x-python", 130 | "name": "python", 131 | "nbconvert_exporter": "python", 132 | "pygments_lexer": "ipython3", 133 | "version": "3.8.0" 134 | } 135 | }, 136 | "nbformat": 4, 137 | "nbformat_minor": 4 138 | } 139 | -------------------------------------------------------------------------------- /Lecture-5/.ipynb_checkpoints/first-checkpoint.txt: -------------------------------------------------------------------------------- 1 | heelo my name is Kunal. -------------------------------------------------------------------------------- /Lecture-5/.ipynb_checkpoints/iterators-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "kernelspec": { 13 | "display_name": "Python 3", 14 | "language": "python", 15 | "name": "python3" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 3 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython3", 27 | "version": "3.8.0" 28 | } 29 | }, 30 | "nbformat": 4, 31 | "nbformat_minor": 4 32 | } 33 | -------------------------------------------------------------------------------- /Lecture-5/.ipynb_checkpoints/second-checkpoint.py: -------------------------------------------------------------------------------- 1 | print("Hello World") -------------------------------------------------------------------------------- /Lecture-5/Untitled.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "r = \"aabfcdb\"" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "# dictionaris in python" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 6, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "from collections import Counter" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 3, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "d = {}" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 4, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "for ch in r:\n", 46 | " # check if the character is already in dictionary\n", 47 | " if ch in d:\n", 48 | " d[ch] = d[ch] + 1\n", 49 | " else:\n", 50 | " d[ch] = 1" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 5, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "{'a': 2, 'b': 2, 'f': 1, 'c': 1, 'd': 1}" 62 | ] 63 | }, 64 | "execution_count": 5, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "d" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 7, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "d2 = Counter(r)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 8, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/plain": [ 90 | "Counter({'a': 2, 'b': 2, 'f': 1, 'c': 1, 'd': 1})" 91 | ] 92 | }, 93 | "execution_count": 8, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "d2" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 9, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "r = \"aabc\"\n", 109 | "m = \"abedea\"" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 10, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "cr = Counter(r)\n", 119 | "cm = Counter(m)" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 11, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "data": { 129 | "text/plain": [ 130 | "(Counter({'a': 2, 'b': 1, 'c': 1}), Counter({'a': 2, 'b': 1, 'e': 2, 'd': 1}))" 131 | ] 132 | }, 133 | "execution_count": 11, 134 | "metadata": {}, 135 | "output_type": "execute_result" 136 | } 137 | ], 138 | "source": [ 139 | "cr, cm" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 15, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "def fun(r, m):\n", 149 | " cr = Counter(r)\n", 150 | " cm = Counter(m)\n", 151 | " for key in cr.keys(): # each unique character in ransome note\n", 152 | " if key not in cm.keys():\n", 153 | " return False\n", 154 | " if cr[key] < cm[key]:\n", 155 | " return False\n", 156 | " return True\n", 157 | " " 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 16, 163 | "metadata": {}, 164 | "outputs": [ 165 | { 166 | "data": { 167 | "text/plain": [ 168 | "True" 169 | ] 170 | }, 171 | "execution_count": 16, 172 | "metadata": {}, 173 | "output_type": "execute_result" 174 | } 175 | ], 176 | "source": [ 177 | "fun(\"aa\", \"aab\")" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "metadata": {}, 184 | "outputs": [], 185 | "source": [] 186 | } 187 | ], 188 | "metadata": { 189 | "kernelspec": { 190 | "display_name": "Python 3", 191 | "language": "python", 192 | "name": "python3" 193 | }, 194 | "language_info": { 195 | "codemirror_mode": { 196 | "name": "ipython", 197 | "version": 3 198 | }, 199 | "file_extension": ".py", 200 | "mimetype": "text/x-python", 201 | "name": "python", 202 | "nbconvert_exporter": "python", 203 | "pygments_lexer": "ipython3", 204 | "version": "3.8.0" 205 | } 206 | }, 207 | "nbformat": 4, 208 | "nbformat_minor": 4 209 | } 210 | -------------------------------------------------------------------------------- /Lecture-5/Untitled1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 9, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdin", 10 | "output_type": "stream", 11 | "text": [ 12 | " 3 6 5 4\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "x = list(map(int, input().split(\" \")))" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 10, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "data": { 27 | "text/plain": [ 28 | "[3, 6, 5, 4]" 29 | ] 30 | }, 31 | "execution_count": 10, 32 | "metadata": {}, 33 | "output_type": "execute_result" 34 | } 35 | ], 36 | "source": [ 37 | "x # list of ints" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 11, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "m = max(x)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 13, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "6" 58 | ] 59 | }, 60 | "execution_count": 13, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "m # a + b + c" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [] 75 | } 76 | ], 77 | "metadata": { 78 | "kernelspec": { 79 | "display_name": "Python 3", 80 | "language": "python", 81 | "name": "python3" 82 | }, 83 | "language_info": { 84 | "codemirror_mode": { 85 | "name": "ipython", 86 | "version": 3 87 | }, 88 | "file_extension": ".py", 89 | "mimetype": "text/x-python", 90 | "name": "python", 91 | "nbconvert_exporter": "python", 92 | "pygments_lexer": "ipython3", 93 | "version": "3.8.0" 94 | } 95 | }, 96 | "nbformat": 4, 97 | "nbformat_minor": 4 98 | } 99 | -------------------------------------------------------------------------------- /Lecture-5/Untitled2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def decorator(original):\n", 10 | " def wrapper(*args, **kwargs): # any number of arguments can be passed\n", 11 | " print(f\"This function executed before {original.__name__}\")\n", 12 | " return original(*args, **kwargs)\n", 13 | " return wrapper" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 11, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "# @decorator\n", 23 | "def greeting(name, age=90):\n", 24 | " print(f\"Hey {name}, you age is {age}\")" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 10, 30 | "metadata": {}, 31 | "outputs": [ 32 | { 33 | "name": "stdout", 34 | "output_type": "stream", 35 | "text": [ 36 | "This function executed before greeting\n", 37 | "Hey Kunal Kushwaha, you age is 56\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "greeting(\"Kunal Kushwaha\", 56)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 12, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "greeting = decorator(greeting)" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 14, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "This function executed before greeting\n", 64 | "Hey Kunal Kushwaha, you age is 56\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "greeting(\"Kunal Kushwaha\", 56)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [] 78 | } 79 | ], 80 | "metadata": { 81 | "kernelspec": { 82 | "display_name": "Python 3", 83 | "language": "python", 84 | "name": "python3" 85 | }, 86 | "language_info": { 87 | "codemirror_mode": { 88 | "name": "ipython", 89 | "version": 3 90 | }, 91 | "file_extension": ".py", 92 | "mimetype": "text/x-python", 93 | "name": "python", 94 | "nbconvert_exporter": "python", 95 | "pygments_lexer": "ipython3", 96 | "version": "3.8.0" 97 | } 98 | }, 99 | "nbformat": 4, 100 | "nbformat_minor": 4 101 | } 102 | -------------------------------------------------------------------------------- /Lecture-5/Untitled3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# classes as decorators" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 29, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "class Decorator(object):\n", 19 | " def __init__(self, original):\n", 20 | " self.original = original\n", 21 | " \n", 22 | " def __call__(self, *args, **kwargs): # this acts like a wrapper function\n", 23 | " print(f\"call executed before {self.original.__name__}\")\n", 24 | " return self.original(*args, **kwargs)\n", 25 | " " 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 30, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "@Decorator\n", 35 | "def display():\n", 36 | " print(\"Display function chal gaya\")" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 31, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "call executed before display\n", 49 | "Display function chal gaya\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "display()" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 7, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "@decorator\n", 64 | "def greeting(name, age=90):\n", 65 | " print(f\"Hello {name}, your age is {age}\")" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 9, 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "name": "stdout", 75 | "output_type": "stream", 76 | "text": [ 77 | "call executed before greeting\n", 78 | "Hello Kunal Kushwaha, your age is 42\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "greeting(\"Kunal Kushwaha\", 42)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 13, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [ 92 | "class Human:\n", 93 | " @staticmethod\n", 94 | " def fun():\n", 95 | " print(\"This method is common for all the objects\")" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 14, 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "kunal = Human()" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 16, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "name": "stdout", 114 | "output_type": "stream", 115 | "text": [ 116 | "This method is common for all the objects\n" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "Human.fun()" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 17, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "# decorators with arguments" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 19, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "# create a prefix decorator\n", 140 | "# use closures" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 26, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "def prefix(prefix):\n", 150 | " def decorator(original):\n", 151 | " def wrapper():\n", 152 | " print(prefix)\n", 153 | " return original()\n", 154 | " return wrapper\n", 155 | " return decorator" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 27, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "@prefix(\"Kunal Kushwaha\")\n", 165 | "def fun():\n", 166 | " print(\"Hello World\")" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 22, 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "name": "stdout", 176 | "output_type": "stream", 177 | "text": [ 178 | "Kunal Kushwaha\n", 179 | "Hello World\n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "fun()" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 23, 190 | "metadata": {}, 191 | "outputs": [], 192 | "source": [ 193 | "# this is same as" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 28, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "Kunal Kushwaha\n", 206 | "Hello World\n" 207 | ] 208 | } 209 | ], 210 | "source": [ 211 | "def fun():\n", 212 | " print(\"Hello World\")\n", 213 | " \n", 214 | "deco = prefix(\"Kunal Kushwaha\")\n", 215 | "fun = deco(fun)\n", 216 | "fun()" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": null, 222 | "metadata": {}, 223 | "outputs": [], 224 | "source": [] 225 | } 226 | ], 227 | "metadata": { 228 | "kernelspec": { 229 | "display_name": "Python 3", 230 | "language": "python", 231 | "name": "python3" 232 | }, 233 | "language_info": { 234 | "codemirror_mode": { 235 | "name": "ipython", 236 | "version": 3 237 | }, 238 | "file_extension": ".py", 239 | "mimetype": "text/x-python", 240 | "name": "python", 241 | "nbconvert_exporter": "python", 242 | "pygments_lexer": "ipython3", 243 | "version": "3.8.0" 244 | } 245 | }, 246 | "nbformat": 4, 247 | "nbformat_minor": 4 248 | } 249 | -------------------------------------------------------------------------------- /Lecture-5/Untitled4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# generators" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "# simpler way of creating iterators" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "# it returns an iterators over which we can iterate upon" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 41, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "# yield = works like the return statement" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 42, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "# why is efficient? \n", 46 | "# it does not hold all the values in memory\n", 47 | "# it is just going to store the state\n", 48 | "# you dont need to worry bout error handling" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 16, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "def square(nums):\n", 58 | " for i in nums:\n", 59 | " yield (i * i)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 17, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "obj = square([1,2,3,4,5])" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 22, 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "data": { 78 | "text/plain": [ 79 | "25" 80 | ] 81 | }, 82 | "execution_count": 22, 83 | "metadata": {}, 84 | "output_type": "execute_result" 85 | } 86 | ], 87 | "source": [ 88 | "next(obj)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 23, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "# create generators in one line!" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 33, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "obj = (num * num for num in [1,2,3,4,5,6]) # using generators" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 36, 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [ 115 | "# using iterators\n", 116 | "class PT:\n", 117 | " def __init__(self, max=0):\n", 118 | " self.max = max\n", 119 | " \n", 120 | " def __iter__(self):\n", 121 | " self.n = 0\n", 122 | " return self # very important\n", 123 | " \n", 124 | " def __next__(self):\n", 125 | " if self.n <= self.max:\n", 126 | " result = 2 ** self.n\n", 127 | " self.n += 1\n", 128 | " return result\n", 129 | " else:\n", 130 | " raise StopIteration" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 39, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "obj = [num * num for num in [1,2,3,4,5,6]] # being treated like iterator" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 40, 145 | "metadata": {}, 146 | "outputs": [ 147 | { 148 | "data": { 149 | "text/plain": [ 150 | "[1, 4, 9, 16, 25, 36]" 151 | ] 152 | }, 153 | "execution_count": 40, 154 | "metadata": {}, 155 | "output_type": "execute_result" 156 | } 157 | ], 158 | "source": [ 159 | "obj" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [] 168 | } 169 | ], 170 | "metadata": { 171 | "kernelspec": { 172 | "display_name": "Python 3", 173 | "language": "python", 174 | "name": "python3" 175 | }, 176 | "language_info": { 177 | "codemirror_mode": { 178 | "name": "ipython", 179 | "version": 3 180 | }, 181 | "file_extension": ".py", 182 | "mimetype": "text/x-python", 183 | "name": "python", 184 | "nbconvert_exporter": "python", 185 | "pygments_lexer": "ipython3", 186 | "version": "3.8.0" 187 | } 188 | }, 189 | "nbformat": 4, 190 | "nbformat_minor": 4 191 | } 192 | -------------------------------------------------------------------------------- /Lecture-5/Untitled5.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# del" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 46, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "class A:\n", 19 | " def __init__(self):\n", 20 | " print(\"Constructor call ho gaya\")\n", 21 | " \n", 22 | " def __del__(self):\n", 23 | " print(\"De-reference ho gaya\")\n", 24 | " \n", 25 | " def __repr__(self):\n", 26 | " return \"some object\"" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 47, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "name": "stdout", 36 | "output_type": "stream", 37 | "text": [ 38 | "Constructor call ho gaya\n" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "obj = A()" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 48, 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "name": "stdout", 53 | "output_type": "stream", 54 | "text": [ 55 | "De-reference ho gaya\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "del(obj)" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 49, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "Constructor call ho gaya\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "obj2 = A()" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 50, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "some object" 89 | ] 90 | }, 91 | "execution_count": 50, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "obj2" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 51, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "del(obj2)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 54, 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "data": { 116 | "text/plain": [ 117 | "some object" 118 | ] 119 | }, 120 | "execution_count": 54, 121 | "metadata": {}, 122 | "output_type": "execute_result" 123 | } 124 | ], 125 | "source": [ 126 | "_" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 53, 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "ename": "NameError", 136 | "evalue": "name 'obj2' is not defined", 137 | "output_type": "error", 138 | "traceback": [ 139 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 140 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 141 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mobj2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 142 | "\u001b[0;31mNameError\u001b[0m: name 'obj2' is not defined" 143 | ] 144 | } 145 | ], 146 | "source": [ 147 | "obj2" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 59, 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "Constructor call ho gaya\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "a = A()" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 60, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [ 173 | "b = a" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 62, 179 | "metadata": {}, 180 | "outputs": [ 181 | { 182 | "name": "stdout", 183 | "output_type": "stream", 184 | "text": [ 185 | "De-reference ho gaya\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "del(b)" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": null, 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": null, 203 | "metadata": {}, 204 | "outputs": [], 205 | "source": [] 206 | } 207 | ], 208 | "metadata": { 209 | "kernelspec": { 210 | "display_name": "Python 3", 211 | "language": "python", 212 | "name": "python3" 213 | }, 214 | "language_info": { 215 | "codemirror_mode": { 216 | "name": "ipython", 217 | "version": 3 218 | }, 219 | "file_extension": ".py", 220 | "mimetype": "text/x-python", 221 | "name": "python", 222 | "nbconvert_exporter": "python", 223 | "pygments_lexer": "ipython3", 224 | "version": "3.8.0" 225 | } 226 | }, 227 | "nbformat": 4, 228 | "nbformat_minor": 4 229 | } 230 | -------------------------------------------------------------------------------- /Lecture-5/Untitled6.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": null, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [] 16 | } 17 | ], 18 | "metadata": { 19 | "kernelspec": { 20 | "display_name": "Python 3", 21 | "language": "python", 22 | "name": "python3" 23 | }, 24 | "language_info": { 25 | "codemirror_mode": { 26 | "name": "ipython", 27 | "version": 3 28 | }, 29 | "file_extension": ".py", 30 | "mimetype": "text/x-python", 31 | "name": "python", 32 | "nbconvert_exporter": "python", 33 | "pygments_lexer": "ipython3", 34 | "version": "3.8.0" 35 | } 36 | }, 37 | "nbformat": 4, 38 | "nbformat_minor": 4 39 | } 40 | -------------------------------------------------------------------------------- /Lecture-5/__main__ vs __name__/first.py: -------------------------------------------------------------------------------- 1 | def fun(): 2 | print("Hello World") 3 | 4 | if __name__ == '__main__': 5 | fun() 6 | -------------------------------------------------------------------------------- /Lecture-5/__main__ vs __name__/second.py: -------------------------------------------------------------------------------- 1 | import first 2 | -------------------------------------------------------------------------------- /Lecture-5/__pycache__/second.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeforcauseorg/Python-Webinars/19cab1b01af269f5eb19dd7126057f3c832cd477/Lecture-5/__pycache__/second.cpython-38.pyc -------------------------------------------------------------------------------- /Lecture-5/decorators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 19, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def decorator(original):\n", 10 | " def wrapper():\n", 11 | " print(\"Functionality added\")\n", 12 | " return original() # closures\n", 13 | " return wrapper" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 20, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "def fun():\n", 23 | " print(\"Hello World\")\n", 24 | "\n", 25 | "fun = decorator(fun) # fun = wrapper\n", 26 | "fun()" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 26, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "# same as: preferred way\n", 36 | "@decorator \n", 37 | "def fun2():\n", 38 | " print(\"Hello World\")" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 25, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "Functionality added\n", 51 | "Hello World\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "fun2()" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [] 65 | } 66 | ], 67 | "metadata": { 68 | "kernelspec": { 69 | "display_name": "Python 3", 70 | "language": "python", 71 | "name": "python3" 72 | }, 73 | "language_info": { 74 | "codemirror_mode": { 75 | "name": "ipython", 76 | "version": 3 77 | }, 78 | "file_extension": ".py", 79 | "mimetype": "text/x-python", 80 | "name": "python", 81 | "nbconvert_exporter": "python", 82 | "pygments_lexer": "ipython3", 83 | "version": "3.8.0" 84 | } 85 | }, 86 | "nbformat": 4, 87 | "nbformat_minor": 4 88 | } 89 | -------------------------------------------------------------------------------- /Lecture-5/exception_handling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 6, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Sorry this is not allowed\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "try:\n", 18 | " a = d\n", 19 | "except Exception:\n", 20 | " print(\"Sorry this is not allowed\")" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 10, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "Sorry this file does not exist\n" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "try:\n", 38 | " f = open(\"test.txt\") # this will trhow an error because file does not exist\n", 39 | "except FileNotFoundError:\n", 40 | " print(\"Sorry this file does not exist\")" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": null, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 12, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "Sorry this file does not exist\n" 60 | ] 61 | } 62 | ], 63 | "source": [ 64 | "try:\n", 65 | " a = e # this will trhow an error because file does not exist\n", 66 | "except Exception:\n", 67 | " print(\"Sorry this file does not exist\")" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 13, 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "Sorry this file does not exist\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "try:\n", 85 | " f = open(\"first.txt\")\n", 86 | " a = e # this will trhow an error because file does not exist\n", 87 | "except Exception:\n", 88 | " print(\"Sorry this file does not exist\")" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 14, 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "# multiple except values" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 25, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "will this be printed?\n", 110 | "[Errno 2] No such file or directory: 'file.txt'\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "try:\n", 116 | "# d = iii\n", 117 | " print(\"will this be printed?\")\n", 118 | " f = open(\"file.txt\") # this raises an error\n", 119 | " d = iii\n", 120 | " print(\"I did not run\")\n", 121 | "except FileNotFoundError as e:\n", 122 | " print(e)\n", 123 | "except Exception as e:\n", 124 | " print(e)" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 27, 130 | "metadata": {}, 131 | "outputs": [], 132 | "source": [ 133 | "# else block: it contains the code that will run if try block does not raise any exception" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 31, 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "name": "stdout", 143 | "output_type": "stream", 144 | "text": [ 145 | "heelo my name is Kunal.\n" 146 | ] 147 | } 148 | ], 149 | "source": [ 150 | "try:\n", 151 | " f = open(\"first.txt\")\n", 152 | "except Exception:\n", 153 | " print(\"Error aa gaya\")\n", 154 | "else: # this runs if try block is successful\n", 155 | " print(f.read())\n", 156 | " f.close() # you can use context managers" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 32, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "# finally block: runs the code no matter what happens" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 35, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "Error aa gaya\n", 178 | "this will run always\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "try:\n", 184 | " f = open(\"fir.txt\")\n", 185 | "except Exception:\n", 186 | " print(\"Error aa gaya\")\n", 187 | "else: # this runs if try block is successful\n", 188 | " print(f.read())\n", 189 | " f.close() # you can use context managers\n", 190 | "finally:\n", 191 | " # get rid of the resources\n", 192 | " print(\"this will run always\")" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [] 201 | } 202 | ], 203 | "metadata": { 204 | "kernelspec": { 205 | "display_name": "Python 3", 206 | "language": "python", 207 | "name": "python3" 208 | }, 209 | "language_info": { 210 | "codemirror_mode": { 211 | "name": "ipython", 212 | "version": 3 213 | }, 214 | "file_extension": ".py", 215 | "mimetype": "text/x-python", 216 | "name": "python", 217 | "nbconvert_exporter": "python", 218 | "pygments_lexer": "ipython3", 219 | "version": "3.8.0" 220 | } 221 | }, 222 | "nbformat": 4, 223 | "nbformat_minor": 4 224 | } 225 | -------------------------------------------------------------------------------- /Lecture-5/first.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# __init__ vs __call__" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "class A:\n", 19 | " def __init__(self):\n", 20 | " print(\"Constructor call ho gaya\")\n", 21 | " \n", 22 | " def __call__(self):\n", 23 | " print(\"call called\")" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 3, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Constructor call ho gaya\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "a = A()" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 4, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "call called\n" 53 | ] 54 | } 55 | ], 56 | "source": [ 57 | "a()" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 17, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [ 66 | "class Human(object):\n", 67 | " def __init__(self, name):\n", 68 | " self.name = name\n", 69 | " print(self.name)\n", 70 | " \n", 71 | " def __call__(self, name):\n", 72 | " self.name = name\n", 73 | " print(self.name)\n" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 11, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "Kunal\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "kunal = Human(\"Kunal\")" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 16, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "Rahul\n" 103 | ] 104 | } 105 | ], 106 | "source": [ 107 | "kunal(\"Rahul\") # works like a function, using __call__" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 15, 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "'Rahul'" 119 | ] 120 | }, 121 | "execution_count": 15, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "kunal.name" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 18, 133 | "metadata": {}, 134 | "outputs": [ 135 | { 136 | "data": { 137 | "text/plain": [ 138 | "10" 139 | ] 140 | }, 141 | "execution_count": 18, 142 | "metadata": {}, 143 | "output_type": "execute_result" 144 | } 145 | ], 146 | "source": [ 147 | "10" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 19, 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "data": { 157 | "text/plain": [ 158 | "10" 159 | ] 160 | }, 161 | "execution_count": 19, 162 | "metadata": {}, 163 | "output_type": "execute_result" 164 | } 165 | ], 166 | "source": [ 167 | "_" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 22, 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [ 176 | "import second" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": null, 182 | "metadata": {}, 183 | "outputs": [], 184 | "source": [] 185 | } 186 | ], 187 | "metadata": { 188 | "kernelspec": { 189 | "display_name": "Python 3", 190 | "language": "python", 191 | "name": "python3" 192 | }, 193 | "language_info": { 194 | "codemirror_mode": { 195 | "name": "ipython", 196 | "version": 3 197 | }, 198 | "file_extension": ".py", 199 | "mimetype": "text/x-python", 200 | "name": "python", 201 | "nbconvert_exporter": "python", 202 | "pygments_lexer": "ipython3", 203 | "version": "3.8.0" 204 | } 205 | }, 206 | "nbformat": 4, 207 | "nbformat_minor": 4 208 | } 209 | -------------------------------------------------------------------------------- /Lecture-5/first.txt: -------------------------------------------------------------------------------- 1 | heelo my name is Kunal. -------------------------------------------------------------------------------- /Lecture-5/iterators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 18, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "a = [3,2,4,5]" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 6, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "# dir(a)" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 7, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "# iterable: an object from which i can get an iterator\n", 28 | "# iterator: an object that can be iterated upon" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 19, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "it = iter(a)\n", 38 | "# this is same as\n", 39 | "it = a.__iter__()" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 20, 45 | "metadata": {}, 46 | "outputs": [ 47 | { 48 | "data": { 49 | "text/plain": [ 50 | "list_iterator" 51 | ] 52 | }, 53 | "execution_count": 20, 54 | "metadata": {}, 55 | "output_type": "execute_result" 56 | } 57 | ], 58 | "source": [ 59 | "type(it)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 21, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "3\n", 72 | "2\n", 73 | "4\n", 74 | "5\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "for i in it:\n", 80 | " print(i)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 26, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "b = [23, 54, 76]" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 27, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "23\n", 102 | "54\n", 103 | "76\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "# b = iter(b)\n", 109 | "for num in b:\n", 110 | " print(num)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 46, 116 | "metadata": {}, 117 | "outputs": [], 118 | "source": [ 119 | "class PT:\n", 120 | " def __init__(self, max=0):\n", 121 | " self.max = max\n", 122 | " \n", 123 | " def __iter__(self):\n", 124 | " self.n = 0\n", 125 | " return self # very important\n", 126 | " \n", 127 | " def __next__(self):\n", 128 | " if self.n <= self.max:\n", 129 | " result = 2 ** self.n\n", 130 | " self.n += 1\n", 131 | " return result\n", 132 | " else:\n", 133 | " raise StopIteration" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 47, 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [ 142 | "a = PT(4)" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 48, 148 | "metadata": {}, 149 | "outputs": [], 150 | "source": [ 151 | "a = iter(a)" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 49, 157 | "metadata": {}, 158 | "outputs": [ 159 | { 160 | "data": { 161 | "text/plain": [ 162 | "1" 163 | ] 164 | }, 165 | "execution_count": 49, 166 | "metadata": {}, 167 | "output_type": "execute_result" 168 | } 169 | ], 170 | "source": [ 171 | "next(a)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [] 180 | } 181 | ], 182 | "metadata": { 183 | "kernelspec": { 184 | "display_name": "Python 3", 185 | "language": "python", 186 | "name": "python3" 187 | }, 188 | "language_info": { 189 | "codemirror_mode": { 190 | "name": "ipython", 191 | "version": 3 192 | }, 193 | "file_extension": ".py", 194 | "mimetype": "text/x-python", 195 | "name": "python", 196 | "nbconvert_exporter": "python", 197 | "pygments_lexer": "ipython3", 198 | "version": "3.8.0" 199 | } 200 | }, 201 | "nbformat": 4, 202 | "nbformat_minor": 4 203 | } 204 | -------------------------------------------------------------------------------- /Lecture-5/second.py: -------------------------------------------------------------------------------- 1 | print("Hello World") --------------------------------------------------------------------------------