├── How to Check Python modules version .ipynb ├── Python Exceptions.ipynb ├── Python Keywords.ipynb ├── Python OOP.ipynb ├── Python_Built-in_Functions.ipynb ├── Python_Functions_arguments_recursion.ipynb ├── Python_Math.ipynb ├── Python_file_operations_&_Directory.ipynb ├── Python_module_doc.ipynb ├── Python_statements_indentation.ipynb ├── Python_variables_datatypes_operators.ipynb ├── README.md └── for_loop.ipynb /How to Check Python modules version .ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import sys\n", 10 | "\n", 11 | "import numpy\n", 12 | "\n", 13 | "import pandas\n", 14 | "\n", 15 | "import matplotlib\n", 16 | "\n", 17 | "import seaborn\n", 18 | "\n", 19 | "import scipy\n", 20 | "\n", 21 | "import sklearn\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "Python : 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] \n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "print('Python : {} '.format(sys.version))" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 3, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "Numpy : 1.16.5\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "print ('Numpy : {}'.format(numpy.__version__))" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 4, 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "name": "stdout", 65 | "output_type": "stream", 66 | "text": [ 67 | "Pandas : 0.25.1\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "print ('Pandas : {}'.format(pandas.__version__))" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 5, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "Matplotlib : 3.1.1\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "print ('Matplotlib : {}'.format(matplotlib.__version__))" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 6, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "Seaborn : 0.9.0\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "print ('Seaborn : {}'.format(seaborn.__version__))" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 7, 112 | "metadata": {}, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "Scipy : 1.4.1\n" 119 | ] 120 | } 121 | ], 122 | "source": [ 123 | "print ('Scipy : {}'.format(scipy.__version__))" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 3, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "Sklearn : 0.22.2.post1\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "print ('Sklearn : {}'.format(sklearn.__version__))" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 9, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "'0.25.1'" 152 | ] 153 | }, 154 | "execution_count": 9, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "import pandas as pd\n", 161 | "pd.__version__" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 13, 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "data": { 171 | "text/plain": [ 172 | "'1.16.5'" 173 | ] 174 | }, 175 | "execution_count": 13, 176 | "metadata": {}, 177 | "output_type": "execute_result" 178 | } 179 | ], 180 | "source": [ 181 | "import numpy as np\n", 182 | "np.__version__" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 14, 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "data": { 192 | "text/plain": [ 193 | "'0.9.0'" 194 | ] 195 | }, 196 | "execution_count": 14, 197 | "metadata": {}, 198 | "output_type": "execute_result" 199 | } 200 | ], 201 | "source": [ 202 | "import seaborn as sns\n", 203 | "sns.__version__" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [] 212 | } 213 | ], 214 | "metadata": { 215 | "kernelspec": { 216 | "display_name": "Python 3", 217 | "language": "python", 218 | "name": "python3" 219 | }, 220 | "language_info": { 221 | "codemirror_mode": { 222 | "name": "ipython", 223 | "version": 3 224 | }, 225 | "file_extension": ".py", 226 | "mimetype": "text/x-python", 227 | "name": "python", 228 | "nbconvert_exporter": "python", 229 | "pygments_lexer": "ipython3", 230 | "version": "3.7.6" 231 | } 232 | }, 233 | "nbformat": 4, 234 | "nbformat_minor": 2 235 | } 236 | -------------------------------------------------------------------------------- /Python Exceptions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "https://www.programiz.com/python-programming/exceptions" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "**Python Errors and Built-in Exceptions**" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "We make certain mistakes while writing a program that lead to errors when we try to run it. A python program terminates as soon as it encounters an unhandled error. \n", 22 | "\n", 23 | "These errors can be broadly classified into two classes:\n", 24 | "\n", 25 | "1. Syntax errors\n", 26 | "\n", 27 | "2. Logical errors (Exceptions)" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "**Python Syntax Errors**" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "Error caused by not following the proper structure (syntax) of the language is called syntax error or parsing error." 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 1, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "ename": "SyntaxError", 51 | "evalue": "invalid syntax (, line 1)", 52 | "output_type": "error", 53 | "traceback": [ 54 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m if a < 3\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "if a < 3\n", 60 | "File \"\", line 1\n", 61 | " if a < 3" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "As shown in the example, an arrow indicates where the parser ran into the syntax error.\n", 69 | "\n", 70 | "We can notice here that a colon : is missing in the if statement." 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "metadata": {}, 76 | "source": [ 77 | "**Python Logical Errors (Exceptions)**" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "Errors that occur at runtime (after passing the syntax test) are called exceptions or logical errors.\n", 85 | "\n", 86 | "For instance, they occur when we try to open a file(for reading) that does not exist (FileNotFoundError), try to divide a number by zero (ZeroDivisionError), or try to import a module that does not exist (ImportError)." 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "Whenever these types of runtime errors occur, Python creates an exception object. \n", 94 | "\n", 95 | "If not handled properly, it prints a traceback to that error along with some details about why that error occurred." 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 2, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "ename": "ZeroDivisionError", 105 | "evalue": "division by zero", 106 | "output_type": "error", 107 | "traceback": [ 108 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 109 | "\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 110 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;36m1\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 111 | "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "1/0" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 3, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "ename": "FileNotFoundError", 126 | "evalue": "[Errno 2] No such file or directory: 'imaginary.txt'", 127 | "output_type": "error", 128 | "traceback": [ 129 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 130 | "\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", 131 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"imaginary.txt\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 132 | "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'imaginary.txt'" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "open(\"imaginary.txt\")" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "metadata": {}, 143 | "source": [ 144 | "**Python Built-in Exceptions**" 145 | ] 146 | }, 147 | { 148 | "cell_type": "markdown", 149 | "metadata": {}, 150 | "source": [ 151 | "Illegal operations can raise exceptions. \n", 152 | "\n", 153 | "There are plenty of built-in exceptions in Python that are raised when corresponding errors occur. \n", 154 | "\n", 155 | "We can view all the built-in exceptions using the built-in local() function as follows:" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 4, 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "name": "stdout", 165 | "output_type": "stream", 166 | "text": [ 167 | "['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__IPYTHON__', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'display', 'divmod', 'enumerate', 'eval', 'exec', 'filter', 'float', 'format', 'frozenset', 'get_ipython', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "print(dir(locals()['__builtins__']))" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 5, 178 | "metadata": {}, 179 | "outputs": [ 180 | { 181 | "data": { 182 | "text/plain": [ 183 | "154" 184 | ] 185 | }, 186 | "execution_count": 5, 187 | "metadata": {}, 188 | "output_type": "execute_result" 189 | } 190 | ], 191 | "source": [ 192 | "len(dir(locals()['__builtins__']))" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 6, 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "data": { 202 | "text/plain": [ 203 | "{'__name__': '__main__',\n", 204 | " '__doc__': 'Automatically created module for IPython interactive environment',\n", 205 | " '__package__': None,\n", 206 | " '__loader__': None,\n", 207 | " '__spec__': None,\n", 208 | " '__builtin__': ,\n", 209 | " '__builtins__': ,\n", 210 | " '_ih': ['',\n", 211 | " 'if a < 3\\nFile \"\", line 1\\n if a < 3',\n", 212 | " '1/0',\n", 213 | " 'open(\"imaginary.txt\")',\n", 214 | " \"print(dir(locals()['__builtins__']))\",\n", 215 | " \"len(dir(locals()['__builtins__']))\",\n", 216 | " 'locals()'],\n", 217 | " '_oh': {5: 154},\n", 218 | " '_dh': ['C:\\\\Users\\\\siddhi Golatkar\\\\Desktop\\\\Python'],\n", 219 | " 'In': ['',\n", 220 | " 'if a < 3\\nFile \"\", line 1\\n if a < 3',\n", 221 | " '1/0',\n", 222 | " 'open(\"imaginary.txt\")',\n", 223 | " \"print(dir(locals()['__builtins__']))\",\n", 224 | " \"len(dir(locals()['__builtins__']))\",\n", 225 | " 'locals()'],\n", 226 | " 'Out': {5: 154},\n", 227 | " 'get_ipython': >,\n", 228 | " 'exit': ,\n", 229 | " 'quit': ,\n", 230 | " '_': 154,\n", 231 | " '__': '',\n", 232 | " '___': '',\n", 233 | " '_i': \"len(dir(locals()['__builtins__']))\",\n", 234 | " '_ii': \"print(dir(locals()['__builtins__']))\",\n", 235 | " '_iii': 'open(\"imaginary.txt\")',\n", 236 | " '_i1': 'if a < 3\\nFile \"\", line 1\\n if a < 3',\n", 237 | " '_i2': '1/0',\n", 238 | " '_i3': 'open(\"imaginary.txt\")',\n", 239 | " '_i4': \"print(dir(locals()['__builtins__']))\",\n", 240 | " '_i5': \"len(dir(locals()['__builtins__']))\",\n", 241 | " '_5': 154,\n", 242 | " '_i6': 'locals()'}" 243 | ] 244 | }, 245 | "execution_count": 6, 246 | "metadata": {}, 247 | "output_type": "execute_result" 248 | } 249 | ], 250 | "source": [ 251 | "locals()" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 7, 257 | "metadata": {}, 258 | "outputs": [ 259 | { 260 | "data": { 261 | "text/plain": [ 262 | "['__class__',\n", 263 | " '__contains__',\n", 264 | " '__delattr__',\n", 265 | " '__delitem__',\n", 266 | " '__dir__',\n", 267 | " '__doc__',\n", 268 | " '__eq__',\n", 269 | " '__format__',\n", 270 | " '__ge__',\n", 271 | " '__getattribute__',\n", 272 | " '__getitem__',\n", 273 | " '__gt__',\n", 274 | " '__hash__',\n", 275 | " '__init__',\n", 276 | " '__init_subclass__',\n", 277 | " '__iter__',\n", 278 | " '__le__',\n", 279 | " '__len__',\n", 280 | " '__lt__',\n", 281 | " '__ne__',\n", 282 | " '__new__',\n", 283 | " '__reduce__',\n", 284 | " '__reduce_ex__',\n", 285 | " '__repr__',\n", 286 | " '__setattr__',\n", 287 | " '__setitem__',\n", 288 | " '__sizeof__',\n", 289 | " '__str__',\n", 290 | " '__subclasshook__',\n", 291 | " 'clear',\n", 292 | " 'copy',\n", 293 | " 'fromkeys',\n", 294 | " 'get',\n", 295 | " 'items',\n", 296 | " 'keys',\n", 297 | " 'pop',\n", 298 | " 'popitem',\n", 299 | " 'setdefault',\n", 300 | " 'update',\n", 301 | " 'values']" 302 | ] 303 | }, 304 | "execution_count": 7, 305 | "metadata": {}, 306 | "output_type": "execute_result" 307 | } 308 | ], 309 | "source": [ 310 | "dir(locals())" 311 | ] 312 | }, 313 | { 314 | "cell_type": "markdown", 315 | "metadata": {}, 316 | "source": [ 317 | "Some of the common built-in exceptions in Python programming along with the error that cause them are listed below:" 318 | ] 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "metadata": {}, 323 | "source": [ 324 | "**AssertionError**\t Raised when an assert statement fails.\n", 325 | "\n", 326 | "**AttributeError**\tRaised when attribute assignment or reference fails.\n", 327 | "\n", 328 | "**EOFError**\tRaised when the input() function hits end-of-file condition.\n", 329 | "\n", 330 | "**FloatingPointError**\tRaised when a floating point operation fails.\n", 331 | "\n", 332 | "**GeneratorExit**\tRaise when a generator's close() method is called.\n", 333 | "\n", 334 | "**ImportError**\tRaised when the imported module is not found.\n", 335 | "\n", 336 | "**IndexError**\tRaised when the index of a sequence is out of range.\n", 337 | "\n", 338 | "**KeyError**\tRaised when a key is not found in a dictionary.\n", 339 | "\n", 340 | "**KeyboardInterrupt**\tRaised when the user hits the interrupt key (Ctrl+C or Delete).\n", 341 | "\n", 342 | "**MemoryError**\tRaised when an operation runs out of memory.\n", 343 | "\n", 344 | "**NameError**\tRaised when a variable is not found in local or global scope.\n", 345 | "\n", 346 | "**NotImplementedError**\tRaised by abstract methods.\n", 347 | "\n", 348 | "**OSError**\tRaised when system operation causes system related error.\n", 349 | "\n", 350 | "**OverflowError**\tRaised when the result of an arithmetic operation is too large to be represented.\n", 351 | "\n", 352 | "**ReferenceError**\tRaised when a weak reference proxy is used to access a garbage collected referent.\n", 353 | "\n", 354 | "**RuntimeError**\tRaised when an error does not fall under any other category.\n", 355 | "\n", 356 | "**StopIteration**\tRaised by next() function to indicate that there is no further item to be returned by iterator.\n", 357 | "\n", 358 | "**SyntaxError**\tRaised by parser when syntax error is encountered.\n", 359 | "\n", 360 | "**IndentationError**\tRaised when there is incorrect indentation.\n", 361 | "\n", 362 | "**TabError**\tRaised when indentation consists of inconsistent tabs and spaces.\n", 363 | "\n", 364 | "**SystemError**\tRaised when interpreter detects internal error.\n", 365 | "\n", 366 | "**SystemExit**\tRaised by sys.exit() function.\n", 367 | "\n", 368 | "**TypeError**\tRaised when a function or operation is applied to an object of incorrect type.\n", 369 | "\n", 370 | "**UnboundLocalError**\tRaised when a reference is made to a local variable in a function or method, but no value has been bound to that variable.\n", 371 | "\n", 372 | "**UnicodeError**\tRaised when a Unicode-related encoding or decoding error occurs.\n", 373 | "\n", 374 | "**UnicodeEncodeError**\tRaised when a Unicode-related error occurs during encoding.\n", 375 | "\n", 376 | "**UnicodeDecodeError**\tRaised when a Unicode-related error occurs during decoding.\n", 377 | "\n", 378 | "**UnicodeTranslateError**\tRaised when a Unicode-related error occurs during translating.\n", 379 | "\n", 380 | "**ValueError**\tRaised when a function gets an argument of correct type but improper value.\n", 381 | "\n", 382 | "**ZeroDivisionError**\tRaised when the second operand of division or modulo operation is zero." 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 11, 388 | "metadata": {}, 389 | "outputs": [ 390 | { 391 | "name": "stdout", 392 | "output_type": "stream", 393 | "text": [ 394 | "The entry is a\n", 395 | "Oops! occurred.\n", 396 | "Next entry.\n", 397 | "\n", 398 | "The entry is 0\n", 399 | "Oops! occurred.\n", 400 | "Next entry.\n", 401 | "\n", 402 | "The entry is 2\n", 403 | "The reciprocal of 2 is 0.5\n" 404 | ] 405 | } 406 | ], 407 | "source": [ 408 | "# import module sys to get the type of exception\n", 409 | "\n", 410 | "import sys\n", 411 | "\n", 412 | "randomList = ['a', 0, 2]\n", 413 | "\n", 414 | "for entry in randomList:\n", 415 | " try:\n", 416 | " print(\"The entry is\", entry)\n", 417 | " r = 1/int(entry)\n", 418 | " break\n", 419 | " except:\n", 420 | " print(\"Oops!\", sys.exc_info()[0], \"occurred.\")\n", 421 | " print(\"Next entry.\")\n", 422 | " print()\n", 423 | "print(\"The reciprocal of\", entry, \"is\", r)" 424 | ] 425 | }, 426 | { 427 | "cell_type": "code", 428 | "execution_count": 13, 429 | "metadata": {}, 430 | "outputs": [ 431 | { 432 | "data": { 433 | "text/plain": [ 434 | "\"This module provides access to some objects used or maintained by the\\ninterpreter and to functions that interact strongly with the interpreter.\\n\\nDynamic objects:\\n\\nargv -- command line arguments; argv[0] is the script pathname if known\\npath -- module search path; path[0] is the script directory, else ''\\nmodules -- dictionary of loaded modules\\n\\ndisplayhook -- called to show results in an interactive session\\nexcepthook -- called to handle any uncaught exception other than SystemExit\\n To customize printing in an interactive session or to install a custom\\n top-level exception handler, assign other functions to replace these.\\n\\nstdin -- standard input file object; used by input()\\nstdout -- standard output file object; used by print()\\nstderr -- standard error object; used for error messages\\n By assigning other file objects (or objects that behave like files)\\n to these, it is possible to redirect all of the interpreter's I/O.\\n\\nlast_type -- type of last uncaught exception\\nlast_value -- value of last uncaught exception\\nlast_traceback -- traceback of last uncaught exception\\n These three are only available in an interactive session after a\\n traceback has been printed.\\n\\nStatic objects:\\n\\nbuiltin_module_names -- tuple of module names built into this interpreter\\ncopyright -- copyright notice pertaining to this interpreter\\nexec_prefix -- prefix used to find the machine-specific Python library\\nexecutable -- absolute path of the executable binary of the Python interpreter\\nfloat_info -- a named tuple with information about the float implementation.\\nfloat_repr_style -- string indicating the style of repr() output for floats\\nhash_info -- a named tuple with information about the hash algorithm.\\nhexversion -- version information encoded as a single integer\\nimplementation -- Python implementation information.\\nint_info -- a named tuple with information about the int implementation.\\nmaxsize -- the largest supported length of containers.\\nmaxunicode -- the value of the largest Unicode code point\\nplatform -- platform identifier\\nprefix -- prefix used to find the Python library\\nthread_info -- a named tuple with information about the thread implementation.\\nversion -- the version of this interpreter as a string\\nversion_info -- version information as a named tuple\\ndllhandle -- [Windows only] integer handle of the Python DLL\\nwinver -- [Windows only] version number of the Python DLL\\n_enablelegacywindowsfsencoding -- [Windows only] \\n__stdin__ -- the original stdin; don't touch!\\n__stdout__ -- the original stdout; don't touch!\\n__stderr__ -- the original stderr; don't touch!\\n__displayhook__ -- the original displayhook; don't touch!\\n__excepthook__ -- the original excepthook; don't touch!\\n\\nFunctions:\\n\\ndisplayhook() -- print an object to the screen, and save it in builtins._\\nexcepthook() -- print an exception and its traceback to sys.stderr\\nexc_info() -- return thread-safe information about the current exception\\nexit() -- exit the interpreter by raising SystemExit\\ngetdlopenflags() -- returns flags to be used for dlopen() calls\\ngetprofile() -- get the global profiling function\\ngetrefcount() -- return the reference count for an object (plus one :-)\\ngetrecursionlimit() -- return the max recursion depth for the interpreter\\ngetsizeof() -- return the size of an object in bytes\\ngettrace() -- get the global debug tracing function\\nsetcheckinterval() -- control how often the interpreter checks for events\\nsetdlopenflags() -- set the flags to be used for dlopen() calls\\nsetprofile() -- set the global profiling function\\nsetrecursionlimit() -- set the max recursion depth for the interpreter\\nsettrace() -- set the global debug tracing function\\n\"" 435 | ] 436 | }, 437 | "execution_count": 13, 438 | "metadata": {}, 439 | "output_type": "execute_result" 440 | } 441 | ], 442 | "source": [ 443 | "sys.__doc__" 444 | ] 445 | }, 446 | { 447 | "cell_type": "markdown", 448 | "metadata": {}, 449 | "source": [ 450 | "Since every exception in Python inherits from the base Exception class, we can also perform the above task in the following way:" 451 | ] 452 | }, 453 | { 454 | "cell_type": "code", 455 | "execution_count": 14, 456 | "metadata": {}, 457 | "outputs": [ 458 | { 459 | "name": "stdout", 460 | "output_type": "stream", 461 | "text": [ 462 | "The entry is a\n", 463 | "Oops! occurred.\n", 464 | "Next entry.\n", 465 | "\n", 466 | "The entry is 0\n", 467 | "Oops! occurred.\n", 468 | "Next entry.\n", 469 | "\n", 470 | "The entry is 2\n", 471 | "The reciprocal of 2 is 0.5\n" 472 | ] 473 | } 474 | ], 475 | "source": [ 476 | "# import module sys to get the type of exception\n", 477 | "\n", 478 | "import sys\n", 479 | "\n", 480 | "randomList = ['a', 0, 2]\n", 481 | "\n", 482 | "for entry in randomList:\n", 483 | " try:\n", 484 | " print(\"The entry is\", entry)\n", 485 | " r = 1/int(entry)\n", 486 | " break\n", 487 | " except Exception as e:\n", 488 | " print(\"Oops!\", e.__class__, \"occurred.\")\n", 489 | " print(\"Next entry.\")\n", 490 | " print()\n", 491 | "print(\"The reciprocal of\", entry, \"is\", r)\n" 492 | ] 493 | }, 494 | { 495 | "cell_type": "markdown", 496 | "metadata": {}, 497 | "source": [ 498 | "This program has the same output as the above program." 499 | ] 500 | }, 501 | { 502 | "cell_type": "code", 503 | "execution_count": 15, 504 | "metadata": {}, 505 | "outputs": [ 506 | { 507 | "ename": "KeyboardInterrupt", 508 | "evalue": "", 509 | "output_type": "error", 510 | "traceback": [ 511 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 512 | "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 513 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 514 | "\u001b[1;31mKeyboardInterrupt\u001b[0m: " 515 | ] 516 | } 517 | ], 518 | "source": [ 519 | "raise KeyboardInterrupt" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 16, 525 | "metadata": {}, 526 | "outputs": [ 527 | { 528 | "ename": "MemoryError", 529 | "evalue": "This is an argument", 530 | "output_type": "error", 531 | "traceback": [ 532 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 533 | "\u001b[1;31mMemoryError\u001b[0m Traceback (most recent call last)", 534 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mMemoryError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"This is an argument\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 535 | "\u001b[1;31mMemoryError\u001b[0m: This is an argument" 536 | ] 537 | } 538 | ], 539 | "source": [ 540 | "raise MemoryError(\"This is an argument\")" 541 | ] 542 | }, 543 | { 544 | "cell_type": "code", 545 | "execution_count": 17, 546 | "metadata": {}, 547 | "outputs": [ 548 | { 549 | "name": "stdout", 550 | "output_type": "stream", 551 | "text": [ 552 | "Enter a positive integer: -2\n", 553 | "That is not a positive number!\n" 554 | ] 555 | } 556 | ], 557 | "source": [ 558 | " try:\n", 559 | "... a = int(input(\"Enter a positive integer: \"))\n", 560 | "... if a <= 0:\n", 561 | "... raise ValueError(\"That is not a positive number!\")\n", 562 | "... except ValueError as ve:\n", 563 | "... print(ve)\n", 564 | "... \n" 565 | ] 566 | }, 567 | { 568 | "cell_type": "code", 569 | "execution_count": 18, 570 | "metadata": {}, 571 | "outputs": [ 572 | { 573 | "name": "stdout", 574 | "output_type": "stream", 575 | "text": [ 576 | "Enter a number: 1\n", 577 | "Not an even number!\n" 578 | ] 579 | } 580 | ], 581 | "source": [ 582 | "# program to print the reciprocal of even numbers\n", 583 | "\n", 584 | "try:\n", 585 | " num = int(input(\"Enter a number: \"))\n", 586 | " assert num % 2 == 0\n", 587 | "except:\n", 588 | " print(\"Not an even number!\")\n", 589 | "else:\n", 590 | " reciprocal = 1/num\n", 591 | " print(reciprocal)" 592 | ] 593 | }, 594 | { 595 | "cell_type": "code", 596 | "execution_count": 19, 597 | "metadata": {}, 598 | "outputs": [ 599 | { 600 | "name": "stdout", 601 | "output_type": "stream", 602 | "text": [ 603 | "Enter a number: 4\n", 604 | "0.25\n" 605 | ] 606 | } 607 | ], 608 | "source": [ 609 | "# program to print the reciprocal of even numbers\n", 610 | "\n", 611 | "try:\n", 612 | " num = int(input(\"Enter a number: \"))\n", 613 | " assert num % 2 == 0\n", 614 | "except:\n", 615 | " print(\"Not an even number!\")\n", 616 | "else:\n", 617 | " reciprocal = 1/num\n", 618 | " print(reciprocal)" 619 | ] 620 | }, 621 | { 622 | "cell_type": "markdown", 623 | "metadata": {}, 624 | "source": [ 625 | "**Creating Custom Exceptions**" 626 | ] 627 | }, 628 | { 629 | "cell_type": "markdown", 630 | "metadata": {}, 631 | "source": [ 632 | "In Python, users can define custom exceptions by creating a new class. \n", 633 | "\n", 634 | "This exception class has to be derived, either directly or indirectly, from the built-in Exception class. \n", 635 | "\n", 636 | "Most of the built-in exceptions are also derived from this class." 637 | ] 638 | }, 639 | { 640 | "cell_type": "code", 641 | "execution_count": 20, 642 | "metadata": {}, 643 | "outputs": [], 644 | "source": [ 645 | "class CustomError(Exception):\n", 646 | " pass" 647 | ] 648 | }, 649 | { 650 | "cell_type": "code", 651 | "execution_count": 23, 652 | "metadata": {}, 653 | "outputs": [ 654 | { 655 | "ename": "CustomError", 656 | "evalue": "", 657 | "output_type": "error", 658 | "traceback": [ 659 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 660 | "\u001b[1;31mCustomError\u001b[0m Traceback (most recent call last)", 661 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mCustomError\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 662 | "\u001b[1;31mCustomError\u001b[0m: " 663 | ] 664 | } 665 | ], 666 | "source": [ 667 | "raise CustomError" 668 | ] 669 | }, 670 | { 671 | "cell_type": "code", 672 | "execution_count": 24, 673 | "metadata": {}, 674 | "outputs": [ 675 | { 676 | "ename": "CustomError", 677 | "evalue": "An error occurred", 678 | "output_type": "error", 679 | "traceback": [ 680 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 681 | "\u001b[1;31mCustomError\u001b[0m Traceback (most recent call last)", 682 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mCustomError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"An error occurred\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 683 | "\u001b[1;31mCustomError\u001b[0m: An error occurred" 684 | ] 685 | } 686 | ], 687 | "source": [ 688 | "raise CustomError(\"An error occurred\")" 689 | ] 690 | }, 691 | { 692 | "cell_type": "markdown", 693 | "metadata": {}, 694 | "source": [ 695 | "Here, we have created a user-defined exception called CustomError which inherits from the Exception class.\n", 696 | "\n", 697 | "This new exception, like other exceptions, can be raised using the raise statement with an optional error message." 698 | ] 699 | }, 700 | { 701 | "cell_type": "markdown", 702 | "metadata": {}, 703 | "source": [ 704 | "When we are developing a large Python program, it is a good practice to place all the user-defined exceptions that our program raises in a separate file. \n", 705 | "\n", 706 | "Many standard modules do this. \n", 707 | "\n", 708 | "They define their exceptions separately as exceptions.py or errors.py (generally but not always)." 709 | ] 710 | }, 711 | { 712 | "cell_type": "markdown", 713 | "metadata": {}, 714 | "source": [ 715 | "**User-Defined Exception in Python**" 716 | ] 717 | }, 718 | { 719 | "cell_type": "markdown", 720 | "metadata": {}, 721 | "source": [ 722 | "user-defined exceptions can be used in a program to raise and catch errors." 723 | ] 724 | }, 725 | { 726 | "cell_type": "markdown", 727 | "metadata": {}, 728 | "source": [ 729 | "This program will ask the user to enter a number until they guess a stored number correctly. \n", 730 | "\n", 731 | "To help them figure it out, a hint is provided whether their guess is greater than or less than the stored number." 732 | ] 733 | }, 734 | { 735 | "cell_type": "code", 736 | "execution_count": 25, 737 | "metadata": {}, 738 | "outputs": [ 739 | { 740 | "name": "stdout", 741 | "output_type": "stream", 742 | "text": [ 743 | "Enter a number: 5\n", 744 | "This value is too small, try again!\n", 745 | "\n", 746 | "Enter a number: 7\n", 747 | "This value is too small, try again!\n", 748 | "\n", 749 | "Enter a number: 0\n", 750 | "This value is too small, try again!\n", 751 | "\n", 752 | "Enter a number: 15\n", 753 | "This value is too large, try again!\n", 754 | "\n", 755 | "Enter a number: 12\n", 756 | "This value is too large, try again!\n", 757 | "\n", 758 | "Enter a number: 10\n", 759 | "Congratulations! You guessed it correctly.\n" 760 | ] 761 | } 762 | ], 763 | "source": [ 764 | "# define Python user-defined exceptions\n", 765 | "\n", 766 | "class Error(Exception):\n", 767 | " \"\"\"Base class for other exceptions\"\"\"\n", 768 | " pass\n", 769 | "\n", 770 | "\n", 771 | "class ValueTooSmallError(Error):\n", 772 | " \"\"\"Raised when the input value is too small\"\"\"\n", 773 | " pass\n", 774 | "\n", 775 | "\n", 776 | "class ValueTooLargeError(Error):\n", 777 | " \"\"\"Raised when the input value is too large\"\"\"\n", 778 | " pass\n", 779 | "\n", 780 | "\n", 781 | "# you need to guess this number\n", 782 | "number = 10\n", 783 | "\n", 784 | "# user guesses a number until he/she gets it right\n", 785 | "while True:\n", 786 | " try:\n", 787 | " i_num = int(input(\"Enter a number: \"))\n", 788 | " if i_num < number:\n", 789 | " raise ValueTooSmallError\n", 790 | " elif i_num > number:\n", 791 | " raise ValueTooLargeError\n", 792 | " break\n", 793 | " except ValueTooSmallError:\n", 794 | " print(\"This value is too small, try again!\")\n", 795 | " print()\n", 796 | " except ValueTooLargeError:\n", 797 | " print(\"This value is too large, try again!\")\n", 798 | " print()\n", 799 | "\n", 800 | "print(\"Congratulations! You guessed it correctly.\")" 801 | ] 802 | }, 803 | { 804 | "cell_type": "markdown", 805 | "metadata": {}, 806 | "source": [ 807 | "**Customizing Exception Classes**" 808 | ] 809 | }, 810 | { 811 | "cell_type": "markdown", 812 | "metadata": {}, 813 | "source": [ 814 | "To learn about customizing the Exception classes, you need to have the basic knowledge of Object-Oriented programming." 815 | ] 816 | }, 817 | { 818 | "cell_type": "code", 819 | "execution_count": 29, 820 | "metadata": {}, 821 | "outputs": [ 822 | { 823 | "name": "stdout", 824 | "output_type": "stream", 825 | "text": [ 826 | "Enter salary amount: 500\n" 827 | ] 828 | } 829 | ], 830 | "source": [ 831 | "class SalaryNotInRangeError(Exception):\n", 832 | " \"\"\"Exception raised for errors in the input salary.\n", 833 | "\n", 834 | " Attributes:\n", 835 | " salary -- input salary which caused the error\n", 836 | " message -- explanation of the error\n", 837 | " \"\"\"\n", 838 | "\n", 839 | " def __init__(self, salary, message=\"Salary is not in (5000, 15000) range\"):\n", 840 | " self.salary = salary\n", 841 | " self.message = message\n", 842 | " super().__init__(self.message)\n", 843 | "\n", 844 | "\n", 845 | "salary = int(input(\"Enter salary amount: \"))\n" 846 | ] 847 | }, 848 | { 849 | "cell_type": "code", 850 | "execution_count": 27, 851 | "metadata": {}, 852 | "outputs": [ 853 | { 854 | "name": "stdout", 855 | "output_type": "stream", 856 | "text": [ 857 | "Enter salary amount: 500\n" 858 | ] 859 | }, 860 | { 861 | "ename": "SalaryNotInRangeError", 862 | "evalue": "Salary is not in (5000, 15000) range", 863 | "output_type": "error", 864 | "traceback": [ 865 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 866 | "\u001b[1;31mSalaryNotInRangeError\u001b[0m Traceback (most recent call last)", 867 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[0msalary\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Enter salary amount: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;36m5000\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0msalary\u001b[0m \u001b[1;33m<\u001b[0m \u001b[1;36m15000\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mSalaryNotInRangeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msalary\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 868 | "\u001b[1;31mSalaryNotInRangeError\u001b[0m: Salary is not in (5000, 15000) range" 869 | ] 870 | } 871 | ], 872 | "source": [ 873 | "class SalaryNotInRangeError(Exception):\n", 874 | " \"\"\"Exception raised for errors in the input salary.\n", 875 | "\n", 876 | " Attributes:\n", 877 | " salary -- input salary which caused the error\n", 878 | " message -- explanation of the error\n", 879 | " \"\"\"\n", 880 | "\n", 881 | " def __init__(self, salary, message=\"Salary is not in (5000, 15000) range\"):\n", 882 | " self.salary = salary\n", 883 | " self.message = message\n", 884 | " super().__init__(self.message)\n", 885 | "\n", 886 | "\n", 887 | "salary = int(input(\"Enter salary amount: \"))\n", 888 | "if not 5000 < salary < 15000:\n", 889 | " raise SalaryNotInRangeError(salary)" 890 | ] 891 | }, 892 | { 893 | "cell_type": "code", 894 | "execution_count": 28, 895 | "metadata": {}, 896 | "outputs": [ 897 | { 898 | "name": "stdout", 899 | "output_type": "stream", 900 | "text": [ 901 | "Enter salary amount: 5000\n" 902 | ] 903 | }, 904 | { 905 | "ename": "SalaryNotInRangeError", 906 | "evalue": "Salary is not in (5000, 15000) range", 907 | "output_type": "error", 908 | "traceback": [ 909 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 910 | "\u001b[1;31mSalaryNotInRangeError\u001b[0m Traceback (most recent call last)", 911 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[0msalary\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Enter salary amount: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;36m5000\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0msalary\u001b[0m \u001b[1;33m<\u001b[0m \u001b[1;36m15000\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mSalaryNotInRangeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msalary\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 912 | "\u001b[1;31mSalaryNotInRangeError\u001b[0m: Salary is not in (5000, 15000) range" 913 | ] 914 | } 915 | ], 916 | "source": [ 917 | "class SalaryNotInRangeError(Exception):\n", 918 | " \"\"\"Exception raised for errors in the input salary.\n", 919 | "\n", 920 | " Attributes:\n", 921 | " salary -- input salary which caused the error\n", 922 | " message -- explanation of the error\n", 923 | " \"\"\"\n", 924 | "\n", 925 | " def __init__(self, salary, message=\"Salary is not in (5000, 15000) range\"):\n", 926 | " self.salary = salary\n", 927 | " self.message = message\n", 928 | " super().__init__(self.message)\n", 929 | "\n", 930 | "\n", 931 | "salary = int(input(\"Enter salary amount: \"))\n", 932 | "if not 5000 < salary < 15000:\n", 933 | " raise SalaryNotInRangeError(salary)" 934 | ] 935 | }, 936 | { 937 | "cell_type": "code", 938 | "execution_count": 30, 939 | "metadata": {}, 940 | "outputs": [ 941 | { 942 | "name": "stdout", 943 | "output_type": "stream", 944 | "text": [ 945 | "Enter salary amount: 15000\n" 946 | ] 947 | }, 948 | { 949 | "ename": "SalaryNotInRangeError", 950 | "evalue": "Salary is not in (5000, 15000) range", 951 | "output_type": "error", 952 | "traceback": [ 953 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 954 | "\u001b[1;31mSalaryNotInRangeError\u001b[0m Traceback (most recent call last)", 955 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[0msalary\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Enter salary amount: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;36m5000\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0msalary\u001b[0m \u001b[1;33m<\u001b[0m \u001b[1;36m15000\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mSalaryNotInRangeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msalary\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 956 | "\u001b[1;31mSalaryNotInRangeError\u001b[0m: Salary is not in (5000, 15000) range" 957 | ] 958 | } 959 | ], 960 | "source": [ 961 | "class SalaryNotInRangeError(Exception):\n", 962 | " \"\"\"Exception raised for errors in the input salary.\n", 963 | "\n", 964 | " Attributes:\n", 965 | " salary -- input salary which caused the error\n", 966 | " message -- explanation of the error\n", 967 | " \"\"\"\n", 968 | "\n", 969 | " def __init__(self, salary, message=\"Salary is not in (5000, 15000) range\"):\n", 970 | " self.salary = salary\n", 971 | " self.message = message\n", 972 | " super().__init__(self.message)\n", 973 | "\n", 974 | "\n", 975 | "salary = int(input(\"Enter salary amount: \"))\n", 976 | "if not 5000 < salary < 15000:\n", 977 | " raise SalaryNotInRangeError(salary)" 978 | ] 979 | }, 980 | { 981 | "cell_type": "code", 982 | "execution_count": 31, 983 | "metadata": {}, 984 | "outputs": [ 985 | { 986 | "name": "stdout", 987 | "output_type": "stream", 988 | "text": [ 989 | "Enter salary amount: 10000\n" 990 | ] 991 | } 992 | ], 993 | "source": [ 994 | "class SalaryNotInRangeError(Exception):\n", 995 | " \"\"\"Exception raised for errors in the input salary.\n", 996 | "\n", 997 | " Attributes:\n", 998 | " salary -- input salary which caused the error\n", 999 | " message -- explanation of the error\n", 1000 | " \"\"\"\n", 1001 | "\n", 1002 | " def __init__(self, salary, message=\"Salary is not in (5000, 15000) range\"):\n", 1003 | " self.salary = salary\n", 1004 | " self.message = message\n", 1005 | " super().__init__(self.message)\n", 1006 | "\n", 1007 | "\n", 1008 | "salary = int(input(\"Enter salary amount: \"))\n", 1009 | "if not 5000 < salary < 15000:\n", 1010 | " raise SalaryNotInRangeError(salary)" 1011 | ] 1012 | }, 1013 | { 1014 | "cell_type": "code", 1015 | "execution_count": 32, 1016 | "metadata": {}, 1017 | "outputs": [ 1018 | { 1019 | "name": "stdout", 1020 | "output_type": "stream", 1021 | "text": [ 1022 | "Enter salary amount: 200\n" 1023 | ] 1024 | }, 1025 | { 1026 | "ename": "SalaryNotInRangeError", 1027 | "evalue": "Salary is not in (5000, 15000) range", 1028 | "output_type": "error", 1029 | "traceback": [ 1030 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1031 | "\u001b[1;31mSalaryNotInRangeError\u001b[0m Traceback (most recent call last)", 1032 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0msalary\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Enter salary amount: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;36m5000\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0msalary\u001b[0m \u001b[1;33m<\u001b[0m \u001b[1;36m15000\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mSalaryNotInRangeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msalary\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 1033 | "\u001b[1;31mSalaryNotInRangeError\u001b[0m: Salary is not in (5000, 15000) range" 1034 | ] 1035 | } 1036 | ], 1037 | "source": [ 1038 | "salary = int(input(\"Enter salary amount: \"))\n", 1039 | "if not 5000 < salary < 15000:\n", 1040 | " raise SalaryNotInRangeError(salary)" 1041 | ] 1042 | }, 1043 | { 1044 | "cell_type": "code", 1045 | "execution_count": 33, 1046 | "metadata": {}, 1047 | "outputs": [ 1048 | { 1049 | "name": "stdout", 1050 | "output_type": "stream", 1051 | "text": [ 1052 | "Enter salary amount: 7000\n" 1053 | ] 1054 | } 1055 | ], 1056 | "source": [ 1057 | "salary = int(input(\"Enter salary amount: \"))\n", 1058 | "if not 5000 < salary < 15000:\n", 1059 | " raise SalaryNotInRangeError(salary)" 1060 | ] 1061 | }, 1062 | { 1063 | "cell_type": "code", 1064 | "execution_count": 35, 1065 | "metadata": {}, 1066 | "outputs": [], 1067 | "source": [ 1068 | "class SalaryNotInRangeError(Exception):\n", 1069 | " \"\"\"Exception raised for errors in the input salary.\n", 1070 | "\n", 1071 | " Attributes:\n", 1072 | " salary -- input salary which caused the error\n", 1073 | " message -- explanation of the error\n", 1074 | " \"\"\"" 1075 | ] 1076 | }, 1077 | { 1078 | "cell_type": "code", 1079 | "execution_count": 34, 1080 | "metadata": {}, 1081 | "outputs": [], 1082 | "source": [ 1083 | "def __init__(self, salary, message=\"Salary is not in (5000, 15000) range\"):\n", 1084 | " self.salary = salary\n", 1085 | " self.message = message\n", 1086 | " super().__init__(self.message)" 1087 | ] 1088 | }, 1089 | { 1090 | "cell_type": "markdown", 1091 | "metadata": {}, 1092 | "source": [ 1093 | "We can also customize the __str__ method itself by overriding it." 1094 | ] 1095 | }, 1096 | { 1097 | "cell_type": "code", 1098 | "execution_count": 36, 1099 | "metadata": {}, 1100 | "outputs": [ 1101 | { 1102 | "name": "stdout", 1103 | "output_type": "stream", 1104 | "text": [ 1105 | "Enter salary amount: 800\n" 1106 | ] 1107 | }, 1108 | { 1109 | "ename": "SalaryNotInRangeError", 1110 | "evalue": "800 -> Salary is not in (5000, 15000) range", 1111 | "output_type": "error", 1112 | "traceback": [ 1113 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1114 | "\u001b[1;31mSalaryNotInRangeError\u001b[0m Traceback (most recent call last)", 1115 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[0msalary\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Enter salary amount: \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;36m5000\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0msalary\u001b[0m \u001b[1;33m<\u001b[0m \u001b[1;36m15000\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mSalaryNotInRangeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msalary\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 1116 | "\u001b[1;31mSalaryNotInRangeError\u001b[0m: 800 -> Salary is not in (5000, 15000) range" 1117 | ] 1118 | } 1119 | ], 1120 | "source": [ 1121 | "class SalaryNotInRangeError(Exception):\n", 1122 | " \"\"\"Exception raised for errors in the input salary.\n", 1123 | "\n", 1124 | " Attributes:\n", 1125 | " salary -- input salary which caused the error\n", 1126 | " message -- explanation of the error\n", 1127 | " \"\"\"\n", 1128 | "\n", 1129 | " def __init__(self, salary, message=\"Salary is not in (5000, 15000) range\"):\n", 1130 | " self.salary = salary\n", 1131 | " self.message = message\n", 1132 | " super().__init__(self.message)\n", 1133 | "\n", 1134 | " def __str__(self):\n", 1135 | " return f'{self.salary} -> {self.message}'\n", 1136 | "\n", 1137 | "\n", 1138 | "salary = int(input(\"Enter salary amount: \"))\n", 1139 | "if not 5000 < salary < 15000:\n", 1140 | " raise SalaryNotInRangeError(salary)" 1141 | ] 1142 | }, 1143 | { 1144 | "cell_type": "code", 1145 | "execution_count": 37, 1146 | "metadata": {}, 1147 | "outputs": [], 1148 | "source": [ 1149 | " def __str__(self):\n", 1150 | " return f'{self.salary} -> {self.message}'" 1151 | ] 1152 | }, 1153 | { 1154 | "cell_type": "code", 1155 | "execution_count": null, 1156 | "metadata": {}, 1157 | "outputs": [], 1158 | "source": [] 1159 | } 1160 | ], 1161 | "metadata": { 1162 | "kernelspec": { 1163 | "display_name": "Python 3", 1164 | "language": "python", 1165 | "name": "python3" 1166 | }, 1167 | "language_info": { 1168 | "codemirror_mode": { 1169 | "name": "ipython", 1170 | "version": 3 1171 | }, 1172 | "file_extension": ".py", 1173 | "mimetype": "text/x-python", 1174 | "name": "python", 1175 | "nbconvert_exporter": "python", 1176 | "pygments_lexer": "ipython3", 1177 | "version": "3.7.6" 1178 | } 1179 | }, 1180 | "nbformat": 4, 1181 | "nbformat_minor": 4 1182 | } 1183 | -------------------------------------------------------------------------------- /Python OOP.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "https://www.programiz.com/python-programming/object-oriented-programming" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Object Oriented Programming" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "One of the popular approaches to solve a programming problem is by creating objects. \n", 22 | "\n", 23 | "This is known as Object-Oriented Programming (OOP).\n", 24 | "\n", 25 | "An object has two characteristics:\n", 26 | "\n", 27 | "- attributes\n", 28 | "- behavior\n", 29 | "\n", 30 | "Let's take an example:\n", 31 | "\n", 32 | "A parrot is can be an object,as it has the following properties:\n", 33 | "\n", 34 | "- name, age, color as attributes\n", 35 | "\n", 36 | "- singing, dancing as behavior\n", 37 | "\n", 38 | "The concept of OOP in Python focuses on creating reusable code. This concept is also known as DRY (Don't Repeat Yourself)." 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "**Example 1: Creating Class and Object in Python**" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 1, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "Blu is a bird\n", 58 | "Woo is also a bird\n", 59 | "Blu is 10 years old\n", 60 | "Woo is 15 years old\n" 61 | ] 62 | } 63 | ], 64 | "source": [ 65 | "class Parrot:\n", 66 | "\n", 67 | " # class attribute\n", 68 | " species = \"bird\"\n", 69 | "\n", 70 | " # instance attribute\n", 71 | " def __init__(self, name, age):\n", 72 | " self.name = name\n", 73 | " self.age = age\n", 74 | "\n", 75 | "# instantiate the Parrot class\n", 76 | "blu = Parrot(\"Blu\", 10)\n", 77 | "woo = Parrot(\"Woo\", 15)\n", 78 | "\n", 79 | "# access the class attributes\n", 80 | "print(\"Blu is a {}\".format(blu.__class__.species))\n", 81 | "print(\"Woo is also a {}\".format(woo.__class__.species))\n", 82 | "\n", 83 | "# access the instance attributes\n", 84 | "print(\"{} is {} years old\".format( blu.name, blu.age))\n", 85 | "print(\"{} is {} years old\".format( woo.name, woo.age))" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "**Example 2 : Creating Methods in Python**" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 2, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "name": "stdout", 102 | "output_type": "stream", 103 | "text": [ 104 | "Blu sings 'Happy'\n", 105 | "Blu is now dancing\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "class Parrot:\n", 111 | " \n", 112 | " # instance attributes\n", 113 | " def __init__(self, name, age):\n", 114 | " self.name = name\n", 115 | " self.age = age\n", 116 | " \n", 117 | " # instance method\n", 118 | " def sing(self, song):\n", 119 | " return \"{} sings {}\".format(self.name, song)\n", 120 | "\n", 121 | " def dance(self):\n", 122 | " return \"{} is now dancing\".format(self.name)\n", 123 | "\n", 124 | "# instantiate the object\n", 125 | "blu = Parrot(\"Blu\", 10)\n", 126 | "\n", 127 | "# call our instance methods\n", 128 | "print(blu.sing(\"'Happy'\"))\n", 129 | "print(blu.dance())" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "**Example 3: Use of Inheritance in Python**" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 3, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "name": "stdout", 146 | "output_type": "stream", 147 | "text": [ 148 | "Bird is ready\n", 149 | "Penguin is ready\n", 150 | "Penguin\n", 151 | "Swim faster\n", 152 | "Run faster\n" 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "# parent class\n", 158 | "class Bird:\n", 159 | " \n", 160 | " def __init__(self):\n", 161 | " print(\"Bird is ready\")\n", 162 | "\n", 163 | " def whoisThis(self):\n", 164 | " print(\"Bird\")\n", 165 | "\n", 166 | " def swim(self):\n", 167 | " print(\"Swim faster\")\n", 168 | " \n", 169 | "# child class\n", 170 | "class Penguin(Bird):\n", 171 | "\n", 172 | " def __init__(self):\n", 173 | " # call super() function\n", 174 | " super().__init__()\n", 175 | " print(\"Penguin is ready\")\n", 176 | "\n", 177 | " def whoisThis(self):\n", 178 | " print(\"Penguin\")\n", 179 | "\n", 180 | " def run(self):\n", 181 | " print(\"Run faster\")\n", 182 | " \n", 183 | "peggy = Penguin()\n", 184 | "peggy.whoisThis()\n", 185 | "peggy.swim()\n", 186 | "peggy.run()" 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "**Example 4: Data Encapsulation in Python**" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 4, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "Selling Price: 900\n", 206 | "Selling Price: 900\n", 207 | "Selling Price: 1000\n" 208 | ] 209 | } 210 | ], 211 | "source": [ 212 | "class Computer:\n", 213 | "\n", 214 | " def __init__(self):\n", 215 | " self.__maxprice = 900\n", 216 | "\n", 217 | " def sell(self):\n", 218 | " print(\"Selling Price: {}\".format(self.__maxprice))\n", 219 | "\n", 220 | " def setMaxPrice(self, price):\n", 221 | " self.__maxprice = price\n", 222 | " \n", 223 | "c = Computer()\n", 224 | "c.sell()\n", 225 | "\n", 226 | "# change the price\n", 227 | "c.__maxprice = 1000\n", 228 | "c.sell()\n", 229 | "\n", 230 | "# using setter function\n", 231 | "c.setMaxPrice(1000)\n", 232 | "c.sell()" 233 | ] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": {}, 238 | "source": [ 239 | "**Polymorphism**\n", 240 | "\n", 241 | "Polymorphism is an ability (in OOP) to use a common interface for multiple forms (data types).\n", 242 | "\n", 243 | "Suppose, we need to color a shape, there are multiple shape options (rectangle, square, circle). \n", 244 | "\n", 245 | "However we could use the same method to color any shape. This concept is called Polymorphism." 246 | ] 247 | }, 248 | { 249 | "cell_type": "markdown", 250 | "metadata": {}, 251 | "source": [ 252 | "**Example 5: Using Polymorphism in Python**" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 5, 258 | "metadata": {}, 259 | "outputs": [ 260 | { 261 | "name": "stdout", 262 | "output_type": "stream", 263 | "text": [ 264 | "Parrot can fly\n", 265 | "Penguin can't fly\n" 266 | ] 267 | } 268 | ], 269 | "source": [ 270 | "class Parrot:\n", 271 | "\n", 272 | " def fly(self):\n", 273 | " print(\"Parrot can fly\")\n", 274 | " \n", 275 | " def swim(self):\n", 276 | " print(\"Parrot can't swim\")\n", 277 | "\n", 278 | "class Penguin:\n", 279 | "\n", 280 | " def fly(self):\n", 281 | " print(\"Penguin can't fly\")\n", 282 | " \n", 283 | " def swim(self):\n", 284 | " print(\"Penguin can swim\")\n", 285 | "\n", 286 | "# common interface\n", 287 | "def flying_test(bird):\n", 288 | " bird.fly()\n", 289 | "\n", 290 | "#instantiate objects\n", 291 | "blu = Parrot()\n", 292 | "peggy = Penguin()\n", 293 | "\n", 294 | "# passing the object\n", 295 | "flying_test(blu)\n", 296 | "flying_test(peggy)" 297 | ] 298 | }, 299 | { 300 | "cell_type": "markdown", 301 | "metadata": {}, 302 | "source": [ 303 | "## Python Objects and Classes" 304 | ] 305 | }, 306 | { 307 | "cell_type": "markdown", 308 | "metadata": {}, 309 | "source": [ 310 | "An object is simply a collection of data (variables) and methods (functions) that act on those data. \n", 311 | "\n", 312 | "Similarly, a class is a blueprint for that object." 313 | ] 314 | }, 315 | { 316 | "cell_type": "markdown", 317 | "metadata": {}, 318 | "source": [ 319 | "We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object." 320 | ] 321 | }, 322 | { 323 | "cell_type": "markdown", 324 | "metadata": {}, 325 | "source": [ 326 | "As many houses can be made from a house's blueprint, we can create many objects from a class.\n", 327 | "\n", 328 | "An object is also called an instance of a class and the process of creating this object is called instantiation." 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 6, 334 | "metadata": {}, 335 | "outputs": [], 336 | "source": [ 337 | "class MyNewClass:\n", 338 | " '''This is a docstring. I have created a new class'''\n", 339 | " pass" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": 11, 345 | "metadata": {}, 346 | "outputs": [], 347 | "source": [ 348 | "class Person:\n", 349 | " \"This is a person class\"\n", 350 | " age = 10\n", 351 | "\n", 352 | " def greet(self):\n", 353 | " print('Hello')\n", 354 | " \n", 355 | "harry = Person()" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": 8, 361 | "metadata": {}, 362 | "outputs": [ 363 | { 364 | "name": "stdout", 365 | "output_type": "stream", 366 | "text": [ 367 | "10\n" 368 | ] 369 | } 370 | ], 371 | "source": [ 372 | "print(Person.age)" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 9, 378 | "metadata": {}, 379 | "outputs": [ 380 | { 381 | "name": "stdout", 382 | "output_type": "stream", 383 | "text": [ 384 | "\n" 385 | ] 386 | } 387 | ], 388 | "source": [ 389 | "print(Person.greet)" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": 10, 395 | "metadata": {}, 396 | "outputs": [ 397 | { 398 | "name": "stdout", 399 | "output_type": "stream", 400 | "text": [ 401 | "This is a person class\n" 402 | ] 403 | } 404 | ], 405 | "source": [ 406 | "print(Person.__doc__)" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 12, 412 | "metadata": {}, 413 | "outputs": [ 414 | { 415 | "name": "stdout", 416 | "output_type": "stream", 417 | "text": [ 418 | "Hello\n" 419 | ] 420 | } 421 | ], 422 | "source": [ 423 | "harry.greet()" 424 | ] 425 | }, 426 | { 427 | "cell_type": "code", 428 | "execution_count": 13, 429 | "metadata": {}, 430 | "outputs": [ 431 | { 432 | "name": "stdout", 433 | "output_type": "stream", 434 | "text": [ 435 | "2+3j\n" 436 | ] 437 | } 438 | ], 439 | "source": [ 440 | "class ComplexNumber:\n", 441 | " def __init__(self, r=0, i=0):\n", 442 | " self.real = r\n", 443 | " self.imag = i\n", 444 | "\n", 445 | " def get_data(self):\n", 446 | " print(f'{self.real}+{self.imag}j')\n", 447 | "\n", 448 | "\n", 449 | "# Create a new ComplexNumber object\n", 450 | "num1 = ComplexNumber(2, 3)\n", 451 | "\n", 452 | "# Call get_data() method\n", 453 | "# Output: 2+3j\n", 454 | "num1.get_data()" 455 | ] 456 | }, 457 | { 458 | "cell_type": "code", 459 | "execution_count": 14, 460 | "metadata": {}, 461 | "outputs": [], 462 | "source": [ 463 | "# Create another ComplexNumber object\n", 464 | "# and create a new attribute 'attr'\n", 465 | "num2 = ComplexNumber(5)\n", 466 | "num2.attr = 10" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 15, 472 | "metadata": {}, 473 | "outputs": [ 474 | { 475 | "name": "stdout", 476 | "output_type": "stream", 477 | "text": [ 478 | "(5, 0, 10)\n" 479 | ] 480 | } 481 | ], 482 | "source": [ 483 | "print((num2.real, num2.imag, num2.attr))" 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "execution_count": 16, 489 | "metadata": {}, 490 | "outputs": [ 491 | { 492 | "ename": "AttributeError", 493 | "evalue": "'ComplexNumber' object has no attribute 'attr'", 494 | "output_type": "error", 495 | "traceback": [ 496 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 497 | "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", 498 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# but c1 object doesn't have attribute 'attr'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;31m# AttributeError: 'ComplexNumber' object has no attribute 'attr'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mattr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 499 | "\u001b[1;31mAttributeError\u001b[0m: 'ComplexNumber' object has no attribute 'attr'" 500 | ] 501 | } 502 | ], 503 | "source": [ 504 | "# but c1 object doesn't have attribute 'attr'\n", 505 | "# AttributeError: 'ComplexNumber' object has no attribute 'attr'\n", 506 | "print(num1.attr)" 507 | ] 508 | }, 509 | { 510 | "cell_type": "markdown", 511 | "metadata": {}, 512 | "source": [ 513 | "**Deleting Attributes and Objects**" 514 | ] 515 | }, 516 | { 517 | "cell_type": "code", 518 | "execution_count": 17, 519 | "metadata": {}, 520 | "outputs": [ 521 | { 522 | "ename": "AttributeError", 523 | "evalue": "'ComplexNumber' object has no attribute 'imag'", 524 | "output_type": "error", 525 | "traceback": [ 526 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 527 | "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", 528 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mnum1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mComplexNumber\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mnum1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimag\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mnum1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_data\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 529 | "\u001b[1;32m\u001b[0m in \u001b[0;36mget_data\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mget_data\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mf'{self.real}+{self.imag}j'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", 530 | "\u001b[1;31mAttributeError\u001b[0m: 'ComplexNumber' object has no attribute 'imag'" 531 | ] 532 | } 533 | ], 534 | "source": [ 535 | "num1 = ComplexNumber(2,3)\n", 536 | "del num1.imag\n", 537 | "num1.get_data()" 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "execution_count": 18, 543 | "metadata": {}, 544 | "outputs": [ 545 | { 546 | "ename": "AttributeError", 547 | "evalue": "'ComplexNumber' object has no attribute 'get_data'", 548 | "output_type": "error", 549 | "traceback": [ 550 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 551 | "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", 552 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mComplexNumber\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_data\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mnum1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_data\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 553 | "\u001b[1;31mAttributeError\u001b[0m: 'ComplexNumber' object has no attribute 'get_data'" 554 | ] 555 | } 556 | ], 557 | "source": [ 558 | "del ComplexNumber.get_data\n", 559 | "\n", 560 | "num1.get_data()" 561 | ] 562 | }, 563 | { 564 | "cell_type": "code", 565 | "execution_count": 19, 566 | "metadata": {}, 567 | "outputs": [], 568 | "source": [ 569 | "c1 = ComplexNumber(1,3)\n", 570 | "\n", 571 | "del c1" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": 20, 577 | "metadata": {}, 578 | "outputs": [ 579 | { 580 | "ename": "NameError", 581 | "evalue": "name 'c1' is not defined", 582 | "output_type": "error", 583 | "traceback": [ 584 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 585 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 586 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mc1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 587 | "\u001b[1;31mNameError\u001b[0m: name 'c1' is not defined" 588 | ] 589 | } 590 | ], 591 | "source": [ 592 | "c1" 593 | ] 594 | }, 595 | { 596 | "cell_type": "markdown", 597 | "metadata": {}, 598 | "source": [ 599 | "## Python Inheritance" 600 | ] 601 | }, 602 | { 603 | "cell_type": "markdown", 604 | "metadata": {}, 605 | "source": [ 606 | "Inheritance enables us to define a class that takes all the functionality from a parent class and allows us to add more." 607 | ] 608 | }, 609 | { 610 | "cell_type": "markdown", 611 | "metadata": {}, 612 | "source": [ 613 | "Inheritance is a powerful feature in object oriented programming.\n", 614 | "\n", 615 | "It refers to defining a new class with little or no modification to an existing class. \n", 616 | "\n", 617 | "The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class." 618 | ] 619 | }, 620 | { 621 | "cell_type": "code", 622 | "execution_count": 21, 623 | "metadata": {}, 624 | "outputs": [], 625 | "source": [ 626 | "class Polygon:\n", 627 | " def __init__(self, no_of_sides):\n", 628 | " self.n = no_of_sides\n", 629 | " self.sides = [0 for i in range(no_of_sides)]\n", 630 | "\n", 631 | " def inputSides(self):\n", 632 | " self.sides = [float(input(\"Enter side \"+str(i+1)+\" : \")) for i in range(self.n)]\n", 633 | "\n", 634 | " def dispSides(self):\n", 635 | " for i in range(self.n):\n", 636 | " print(\"Side\",i+1,\"is\",self.sides[i])" 637 | ] 638 | }, 639 | { 640 | "cell_type": "code", 641 | "execution_count": 22, 642 | "metadata": {}, 643 | "outputs": [], 644 | "source": [ 645 | "class Triangle(Polygon):\n", 646 | " def __init__(self):\n", 647 | " Polygon.__init__(self,3)\n", 648 | "\n", 649 | " def findArea(self):\n", 650 | " a, b, c = self.sides\n", 651 | " # calculate the semi-perimeter\n", 652 | " s = (a + b + c) / 2\n", 653 | " area = (s*(s-a)*(s-b)*(s-c)) ** 0.5\n", 654 | " print('The area of the triangle is %0.2f' %area)" 655 | ] 656 | }, 657 | { 658 | "cell_type": "code", 659 | "execution_count": 23, 660 | "metadata": {}, 661 | "outputs": [], 662 | "source": [ 663 | "t = Triangle()" 664 | ] 665 | }, 666 | { 667 | "cell_type": "code", 668 | "execution_count": 25, 669 | "metadata": {}, 670 | "outputs": [ 671 | { 672 | "name": "stdout", 673 | "output_type": "stream", 674 | "text": [ 675 | "Enter side 1 : 3\n", 676 | "Enter side 2 : 5\n", 677 | "Enter side 3 : 4\n" 678 | ] 679 | } 680 | ], 681 | "source": [ 682 | "t.inputSides()" 683 | ] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": 26, 688 | "metadata": {}, 689 | "outputs": [ 690 | { 691 | "name": "stdout", 692 | "output_type": "stream", 693 | "text": [ 694 | "Side 1 is 3.0\n", 695 | "Side 2 is 5.0\n", 696 | "Side 3 is 4.0\n" 697 | ] 698 | } 699 | ], 700 | "source": [ 701 | "t.dispSides()" 702 | ] 703 | }, 704 | { 705 | "cell_type": "code", 706 | "execution_count": 27, 707 | "metadata": {}, 708 | "outputs": [ 709 | { 710 | "name": "stdout", 711 | "output_type": "stream", 712 | "text": [ 713 | "The area of the triangle is 6.00\n" 714 | ] 715 | } 716 | ], 717 | "source": [ 718 | "t.findArea()" 719 | ] 720 | }, 721 | { 722 | "cell_type": "code", 723 | "execution_count": 28, 724 | "metadata": {}, 725 | "outputs": [ 726 | { 727 | "data": { 728 | "text/plain": [ 729 | "True" 730 | ] 731 | }, 732 | "execution_count": 28, 733 | "metadata": {}, 734 | "output_type": "execute_result" 735 | } 736 | ], 737 | "source": [ 738 | "isinstance(t,Triangle)" 739 | ] 740 | }, 741 | { 742 | "cell_type": "code", 743 | "execution_count": 29, 744 | "metadata": {}, 745 | "outputs": [ 746 | { 747 | "data": { 748 | "text/plain": [ 749 | "True" 750 | ] 751 | }, 752 | "execution_count": 29, 753 | "metadata": {}, 754 | "output_type": "execute_result" 755 | } 756 | ], 757 | "source": [ 758 | "isinstance(t,Polygon)" 759 | ] 760 | }, 761 | { 762 | "cell_type": "code", 763 | "execution_count": 30, 764 | "metadata": {}, 765 | "outputs": [ 766 | { 767 | "data": { 768 | "text/plain": [ 769 | "False" 770 | ] 771 | }, 772 | "execution_count": 30, 773 | "metadata": {}, 774 | "output_type": "execute_result" 775 | } 776 | ], 777 | "source": [ 778 | "isinstance(t,int)" 779 | ] 780 | }, 781 | { 782 | "cell_type": "code", 783 | "execution_count": 31, 784 | "metadata": {}, 785 | "outputs": [ 786 | { 787 | "data": { 788 | "text/plain": [ 789 | "True" 790 | ] 791 | }, 792 | "execution_count": 31, 793 | "metadata": {}, 794 | "output_type": "execute_result" 795 | } 796 | ], 797 | "source": [ 798 | "isinstance(t,object)" 799 | ] 800 | }, 801 | { 802 | "cell_type": "markdown", 803 | "metadata": {}, 804 | "source": [ 805 | "## Python Multiple Inheritance" 806 | ] 807 | }, 808 | { 809 | "cell_type": "markdown", 810 | "metadata": {}, 811 | "source": [ 812 | "A class can be derived from more than one base class in Python, similar to C++. This is called multiple inheritance." 813 | ] 814 | }, 815 | { 816 | "cell_type": "code", 817 | "execution_count": 32, 818 | "metadata": {}, 819 | "outputs": [], 820 | "source": [ 821 | "class Base1:\n", 822 | " pass\n", 823 | "\n", 824 | "class Base2:\n", 825 | " pass\n", 826 | "\n", 827 | "class MultiDerived(Base1, Base2):\n", 828 | " pass" 829 | ] 830 | }, 831 | { 832 | "cell_type": "code", 833 | "execution_count": 33, 834 | "metadata": {}, 835 | "outputs": [], 836 | "source": [ 837 | "class Base:\n", 838 | " pass\n", 839 | "\n", 840 | "class Derived1(Base):\n", 841 | " pass\n", 842 | "\n", 843 | "class Derived2(Derived1):\n", 844 | " pass" 845 | ] 846 | }, 847 | { 848 | "cell_type": "code", 849 | "execution_count": 34, 850 | "metadata": {}, 851 | "outputs": [ 852 | { 853 | "name": "stdout", 854 | "output_type": "stream", 855 | "text": [ 856 | "True\n" 857 | ] 858 | } 859 | ], 860 | "source": [ 861 | "print(issubclass(list,object))" 862 | ] 863 | }, 864 | { 865 | "cell_type": "code", 866 | "execution_count": 35, 867 | "metadata": {}, 868 | "outputs": [ 869 | { 870 | "name": "stdout", 871 | "output_type": "stream", 872 | "text": [ 873 | "True\n" 874 | ] 875 | } 876 | ], 877 | "source": [ 878 | "print(isinstance(5.5,object))" 879 | ] 880 | }, 881 | { 882 | "cell_type": "code", 883 | "execution_count": 36, 884 | "metadata": {}, 885 | "outputs": [ 886 | { 887 | "name": "stdout", 888 | "output_type": "stream", 889 | "text": [ 890 | "True\n" 891 | ] 892 | } 893 | ], 894 | "source": [ 895 | "print(isinstance(\"Hello\",object))" 896 | ] 897 | }, 898 | { 899 | "cell_type": "markdown", 900 | "metadata": {}, 901 | "source": [ 902 | "## Python Operator Overloading" 903 | ] 904 | }, 905 | { 906 | "cell_type": "markdown", 907 | "metadata": {}, 908 | "source": [ 909 | "Python operators work for built-in classes. \n", 910 | "\n", 911 | "But the same operator behaves differently with different types. \n", 912 | "\n", 913 | "For example, the + operator will perform arithmetic addition on two numbers, merge two lists, or concatenate two strings." 914 | ] 915 | }, 916 | { 917 | "cell_type": "code", 918 | "execution_count": 37, 919 | "metadata": {}, 920 | "outputs": [], 921 | "source": [ 922 | "class Point:\n", 923 | " def __init__(self, x=0, y=0):\n", 924 | " self.x = x\n", 925 | " self.y = y\n", 926 | "\n", 927 | "\n", 928 | "p1 = Point(1, 2)\n", 929 | "p2 = Point(2, 3)" 930 | ] 931 | }, 932 | { 933 | "cell_type": "code", 934 | "execution_count": 38, 935 | "metadata": {}, 936 | "outputs": [ 937 | { 938 | "ename": "TypeError", 939 | "evalue": "unsupported operand type(s) for +: 'Point' and 'Point'", 940 | "output_type": "error", 941 | "traceback": [ 942 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 943 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 944 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mp1\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mp2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 945 | "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'Point' and 'Point'" 946 | ] 947 | } 948 | ], 949 | "source": [ 950 | "print(p1+p2)" 951 | ] 952 | }, 953 | { 954 | "cell_type": "code", 955 | "execution_count": 40, 956 | "metadata": {}, 957 | "outputs": [ 958 | { 959 | "name": "stdout", 960 | "output_type": "stream", 961 | "text": [ 962 | "<__main__.Point object at 0x0000025035006BC8>\n" 963 | ] 964 | } 965 | ], 966 | "source": [ 967 | "print(p1)" 968 | ] 969 | }, 970 | { 971 | "cell_type": "code", 972 | "execution_count": 41, 973 | "metadata": {}, 974 | "outputs": [ 975 | { 976 | "name": "stdout", 977 | "output_type": "stream", 978 | "text": [ 979 | "(2, 3)\n" 980 | ] 981 | } 982 | ], 983 | "source": [ 984 | "class Point:\n", 985 | " def __init__(self, x=0, y=0):\n", 986 | " self.x = x\n", 987 | " self.y = y\n", 988 | "\n", 989 | " def __str__(self):\n", 990 | " return \"({0}, {1})\".format(self.x, self.y)\n", 991 | "\n", 992 | "\n", 993 | "p1 = Point(2, 3)\n", 994 | "print(p1)" 995 | ] 996 | }, 997 | { 998 | "cell_type": "code", 999 | "execution_count": 42, 1000 | "metadata": {}, 1001 | "outputs": [ 1002 | { 1003 | "data": { 1004 | "text/plain": [ 1005 | "'(2, 3)'" 1006 | ] 1007 | }, 1008 | "execution_count": 42, 1009 | "metadata": {}, 1010 | "output_type": "execute_result" 1011 | } 1012 | ], 1013 | "source": [ 1014 | "str(p1)" 1015 | ] 1016 | }, 1017 | { 1018 | "cell_type": "code", 1019 | "execution_count": 43, 1020 | "metadata": {}, 1021 | "outputs": [ 1022 | { 1023 | "data": { 1024 | "text/plain": [ 1025 | "'(2, 3)'" 1026 | ] 1027 | }, 1028 | "execution_count": 43, 1029 | "metadata": {}, 1030 | "output_type": "execute_result" 1031 | } 1032 | ], 1033 | "source": [ 1034 | "format(p1)" 1035 | ] 1036 | }, 1037 | { 1038 | "cell_type": "code", 1039 | "execution_count": 44, 1040 | "metadata": {}, 1041 | "outputs": [], 1042 | "source": [ 1043 | "class Point:\n", 1044 | " def __init__(self, x=0, y=0):\n", 1045 | " self.x = x\n", 1046 | " self.y = y\n", 1047 | "\n", 1048 | " def __str__(self):\n", 1049 | " return \"({0},{1})\".format(self.x, self.y)\n", 1050 | "\n", 1051 | " def __add__(self, other):\n", 1052 | " x = self.x + other.x\n", 1053 | " y = self.y + other.y\n", 1054 | " return Point(x, y)" 1055 | ] 1056 | }, 1057 | { 1058 | "cell_type": "markdown", 1059 | "metadata": {}, 1060 | "source": [ 1061 | "**Addition**" 1062 | ] 1063 | }, 1064 | { 1065 | "cell_type": "code", 1066 | "execution_count": 46, 1067 | "metadata": {}, 1068 | "outputs": [ 1069 | { 1070 | "name": "stdout", 1071 | "output_type": "stream", 1072 | "text": [ 1073 | "(3,5)\n" 1074 | ] 1075 | } 1076 | ], 1077 | "source": [ 1078 | "class Point:\n", 1079 | " def __init__(self, x=0, y=0):\n", 1080 | " self.x = x\n", 1081 | " self.y = y\n", 1082 | "\n", 1083 | " def __str__(self):\n", 1084 | " return \"({0},{1})\".format(self.x, self.y)\n", 1085 | "\n", 1086 | " def __add__(self, other):\n", 1087 | " x = self.x + other.x\n", 1088 | " y = self.y + other.y\n", 1089 | " return Point(x, y)\n", 1090 | "\n", 1091 | "\n", 1092 | "p1 = Point(1, 2)\n", 1093 | "p2 = Point(2, 3)\n", 1094 | "\n", 1095 | "print(p1+p2)\n" 1096 | ] 1097 | }, 1098 | { 1099 | "cell_type": "markdown", 1100 | "metadata": {}, 1101 | "source": [ 1102 | "**Subtraction**" 1103 | ] 1104 | }, 1105 | { 1106 | "cell_type": "code", 1107 | "execution_count": 49, 1108 | "metadata": {}, 1109 | "outputs": [ 1110 | { 1111 | "name": "stdout", 1112 | "output_type": "stream", 1113 | "text": [ 1114 | "(-1,-1)\n" 1115 | ] 1116 | } 1117 | ], 1118 | "source": [ 1119 | "class Point:\n", 1120 | " def __init__(self, x=0, y=0):\n", 1121 | " self.x = x\n", 1122 | " self.y = y\n", 1123 | "\n", 1124 | " def __str__(self):\n", 1125 | " return \"({0},{1})\".format(self.x, self.y)\n", 1126 | "\n", 1127 | " def __sub__(self, other):\n", 1128 | " x = self.x - other.x\n", 1129 | " y = self.y - other.y\n", 1130 | " return Point(x, y)\n", 1131 | "\n", 1132 | "\n", 1133 | "p1 = Point(1, 2)\n", 1134 | "p2 = Point(2, 3)\n", 1135 | "\n", 1136 | "print(p1-p2)" 1137 | ] 1138 | }, 1139 | { 1140 | "cell_type": "markdown", 1141 | "metadata": {}, 1142 | "source": [ 1143 | "**Multiplication**" 1144 | ] 1145 | }, 1146 | { 1147 | "cell_type": "code", 1148 | "execution_count": 50, 1149 | "metadata": {}, 1150 | "outputs": [ 1151 | { 1152 | "name": "stdout", 1153 | "output_type": "stream", 1154 | "text": [ 1155 | "(2,6)\n" 1156 | ] 1157 | } 1158 | ], 1159 | "source": [ 1160 | "class Point:\n", 1161 | " def __init__(self, x=0, y=0):\n", 1162 | " self.x = x\n", 1163 | " self.y = y\n", 1164 | "\n", 1165 | " def __str__(self):\n", 1166 | " return \"({0},{1})\".format(self.x, self.y)\n", 1167 | "\n", 1168 | " def __mul__(self, other):\n", 1169 | " x = self.x * other.x\n", 1170 | " y = self.y * other.y\n", 1171 | " return Point(x, y)\n", 1172 | "\n", 1173 | "\n", 1174 | "p1 = Point(1, 2)\n", 1175 | "p2 = Point(2, 3)\n", 1176 | "\n", 1177 | "print(p1*p2)" 1178 | ] 1179 | }, 1180 | { 1181 | "cell_type": "markdown", 1182 | "metadata": {}, 1183 | "source": [ 1184 | "**Power**" 1185 | ] 1186 | }, 1187 | { 1188 | "cell_type": "code", 1189 | "execution_count": 51, 1190 | "metadata": {}, 1191 | "outputs": [ 1192 | { 1193 | "name": "stdout", 1194 | "output_type": "stream", 1195 | "text": [ 1196 | "(1,8)\n" 1197 | ] 1198 | } 1199 | ], 1200 | "source": [ 1201 | "class Point:\n", 1202 | " def __init__(self, x=0, y=0):\n", 1203 | " self.x = x\n", 1204 | " self.y = y\n", 1205 | "\n", 1206 | " def __str__(self):\n", 1207 | " return \"({0},{1})\".format(self.x, self.y)\n", 1208 | "\n", 1209 | " def __pow__(self, other):\n", 1210 | " x = self.x ** other.x\n", 1211 | " y = self.y ** other.y\n", 1212 | " return Point(x, y)\n", 1213 | "\n", 1214 | "\n", 1215 | "p1 = Point(1, 2)\n", 1216 | "p2 = Point(2, 3)\n", 1217 | "\n", 1218 | "print(p1**p2)" 1219 | ] 1220 | }, 1221 | { 1222 | "cell_type": "code", 1223 | "execution_count": 52, 1224 | "metadata": {}, 1225 | "outputs": [], 1226 | "source": [ 1227 | "# overloading the less than operator\n", 1228 | "class Point:\n", 1229 | " def __init__(self, x=0, y=0):\n", 1230 | " self.x = x\n", 1231 | " self.y = y\n", 1232 | "\n", 1233 | " def __str__(self):\n", 1234 | " return \"({0},{1})\".format(self.x, self.y)\n", 1235 | "\n", 1236 | " def __lt__(self, other):\n", 1237 | " self_mag = (self.x ** 2) + (self.y ** 2)\n", 1238 | " other_mag = (other.x ** 2) + (other.y ** 2)\n", 1239 | " return self_mag < other_mag" 1240 | ] 1241 | }, 1242 | { 1243 | "cell_type": "code", 1244 | "execution_count": 53, 1245 | "metadata": {}, 1246 | "outputs": [], 1247 | "source": [ 1248 | "p1 = Point(1,1)\n", 1249 | "p2 = Point(-2,-3)\n", 1250 | "p3 = Point(1,-1)" 1251 | ] 1252 | }, 1253 | { 1254 | "cell_type": "code", 1255 | "execution_count": 54, 1256 | "metadata": {}, 1257 | "outputs": [ 1258 | { 1259 | "name": "stdout", 1260 | "output_type": "stream", 1261 | "text": [ 1262 | "True\n", 1263 | "False\n", 1264 | "False\n" 1265 | ] 1266 | } 1267 | ], 1268 | "source": [ 1269 | "# use less than\n", 1270 | "print(p1\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrmdir\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Practise'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 1371 | "\u001b[1;31mOSError\u001b[0m: [WinError 145] The directory is not empty: 'Practise'" 1372 | ] 1373 | } 1374 | ], 1375 | "source": [ 1376 | "os.rmdir('Practise')" 1377 | ] 1378 | }, 1379 | { 1380 | "cell_type": "code", 1381 | "execution_count": 99, 1382 | "metadata": {}, 1383 | "outputs": [], 1384 | "source": [ 1385 | "import shutil" 1386 | ] 1387 | }, 1388 | { 1389 | "cell_type": "code", 1390 | "execution_count": 100, 1391 | "metadata": {}, 1392 | "outputs": [], 1393 | "source": [ 1394 | "shutil.rmtree('Practise')" 1395 | ] 1396 | }, 1397 | { 1398 | "cell_type": "code", 1399 | "execution_count": 101, 1400 | "metadata": {}, 1401 | "outputs": [ 1402 | { 1403 | "data": { 1404 | "text/plain": [ 1405 | "['.ipynb_checkpoints',\n", 1406 | " 'bootcamp python Test.ipynb',\n", 1407 | " 'example.txt',\n", 1408 | " 'How to Check Python modules version .ipynb',\n", 1409 | " 'Python Keywords.ipynb',\n", 1410 | " 'Python_file_operations.ipynb',\n", 1411 | " 'Python_Functions_arguments_recursion.ipynb',\n", 1412 | " 'Python_Math.ipynb',\n", 1413 | " 'Python_module_doc.ipynb',\n", 1414 | " 'Python_statements_indentation.ipynb',\n", 1415 | " 'Python_variables_datatypes_operators.ipynb',\n", 1416 | " 'readme.txt']" 1417 | ] 1418 | }, 1419 | "execution_count": 101, 1420 | "metadata": {}, 1421 | "output_type": "execute_result" 1422 | } 1423 | ], 1424 | "source": [ 1425 | "os.listdir()" 1426 | ] 1427 | }, 1428 | { 1429 | "cell_type": "code", 1430 | "execution_count": null, 1431 | "metadata": {}, 1432 | "outputs": [], 1433 | "source": [] 1434 | }, 1435 | { 1436 | "cell_type": "code", 1437 | "execution_count": null, 1438 | "metadata": {}, 1439 | "outputs": [], 1440 | "source": [] 1441 | } 1442 | ], 1443 | "metadata": { 1444 | "kernelspec": { 1445 | "display_name": "Python 3", 1446 | "language": "python", 1447 | "name": "python3" 1448 | }, 1449 | "language_info": { 1450 | "codemirror_mode": { 1451 | "name": "ipython", 1452 | "version": 3 1453 | }, 1454 | "file_extension": ".py", 1455 | "mimetype": "text/x-python", 1456 | "name": "python", 1457 | "nbconvert_exporter": "python", 1458 | "pygments_lexer": "ipython3", 1459 | "version": "3.7.6" 1460 | } 1461 | }, 1462 | "nbformat": 4, 1463 | "nbformat_minor": 4 1464 | } 1465 | -------------------------------------------------------------------------------- /Python_module_doc.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "\n", 13 | "pandas - a powerful data analysis and manipulation library for Python\n", 14 | "=====================================================================\n", 15 | "\n", 16 | "**pandas** is a Python package providing fast, flexible, and expressive data\n", 17 | "structures designed to make working with \"relational\" or \"labeled\" data both\n", 18 | "easy and intuitive. It aims to be the fundamental high-level building block for\n", 19 | "doing practical, **real world** data analysis in Python. Additionally, it has\n", 20 | "the broader goal of becoming **the most powerful and flexible open source data\n", 21 | "analysis / manipulation tool available in any language**. It is already well on\n", 22 | "its way toward this goal.\n", 23 | "\n", 24 | "Main Features\n", 25 | "-------------\n", 26 | "Here are just a few of the things that pandas does well:\n", 27 | "\n", 28 | " - Easy handling of missing data in floating point as well as non-floating\n", 29 | " point data.\n", 30 | " - Size mutability: columns can be inserted and deleted from DataFrame and\n", 31 | " higher dimensional objects\n", 32 | " - Automatic and explicit data alignment: objects can be explicitly aligned\n", 33 | " to a set of labels, or the user can simply ignore the labels and let\n", 34 | " `Series`, `DataFrame`, etc. automatically align the data for you in\n", 35 | " computations.\n", 36 | " - Powerful, flexible group by functionality to perform split-apply-combine\n", 37 | " operations on data sets, for both aggregating and transforming data.\n", 38 | " - Make it easy to convert ragged, differently-indexed data in other Python\n", 39 | " and NumPy data structures into DataFrame objects.\n", 40 | " - Intelligent label-based slicing, fancy indexing, and subsetting of large\n", 41 | " data sets.\n", 42 | " - Intuitive merging and joining data sets.\n", 43 | " - Flexible reshaping and pivoting of data sets.\n", 44 | " - Hierarchical labeling of axes (possible to have multiple labels per tick).\n", 45 | " - Robust IO tools for loading data from flat files (CSV and delimited),\n", 46 | " Excel files, databases, and saving/loading data from the ultrafast HDF5\n", 47 | " format.\n", 48 | " - Time series-specific functionality: date range generation and frequency\n", 49 | " conversion, moving window statistics, date shifting and lagging.\n", 50 | "\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "import pandas\n", 56 | "print(pandas.__doc__)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 3, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "\n", 69 | "NumPy\n", 70 | "=====\n", 71 | "\n", 72 | "Provides\n", 73 | " 1. An array object of arbitrary homogeneous items\n", 74 | " 2. Fast mathematical operations over arrays\n", 75 | " 3. Linear Algebra, Fourier Transforms, Random Number Generation\n", 76 | "\n", 77 | "How to use the documentation\n", 78 | "----------------------------\n", 79 | "Documentation is available in two forms: docstrings provided\n", 80 | "with the code, and a loose standing reference guide, available from\n", 81 | "`the NumPy homepage `_.\n", 82 | "\n", 83 | "We recommend exploring the docstrings using\n", 84 | "`IPython `_, an advanced Python shell with\n", 85 | "TAB-completion and introspection capabilities. See below for further\n", 86 | "instructions.\n", 87 | "\n", 88 | "The docstring examples assume that `numpy` has been imported as `np`::\n", 89 | "\n", 90 | " >>> import numpy as np\n", 91 | "\n", 92 | "Code snippets are indicated by three greater-than signs::\n", 93 | "\n", 94 | " >>> x = 42\n", 95 | " >>> x = x + 1\n", 96 | "\n", 97 | "Use the built-in ``help`` function to view a function's docstring::\n", 98 | "\n", 99 | " >>> help(np.sort)\n", 100 | " ... # doctest: +SKIP\n", 101 | "\n", 102 | "For some objects, ``np.info(obj)`` may provide additional help. This is\n", 103 | "particularly true if you see the line \"Help on ufunc object:\" at the top\n", 104 | "of the help() page. Ufuncs are implemented in C, not Python, for speed.\n", 105 | "The native Python help() does not know how to view their help, but our\n", 106 | "np.info() function does.\n", 107 | "\n", 108 | "To search for documents containing a keyword, do::\n", 109 | "\n", 110 | " >>> np.lookfor('keyword')\n", 111 | " ... # doctest: +SKIP\n", 112 | "\n", 113 | "General-purpose documents like a glossary and help on the basic concepts\n", 114 | "of numpy are available under the ``doc`` sub-module::\n", 115 | "\n", 116 | " >>> from numpy import doc\n", 117 | " >>> help(doc)\n", 118 | " ... # doctest: +SKIP\n", 119 | "\n", 120 | "Available subpackages\n", 121 | "---------------------\n", 122 | "doc\n", 123 | " Topical documentation on broadcasting, indexing, etc.\n", 124 | "lib\n", 125 | " Basic functions used by several sub-packages.\n", 126 | "random\n", 127 | " Core Random Tools\n", 128 | "linalg\n", 129 | " Core Linear Algebra Tools\n", 130 | "fft\n", 131 | " Core FFT routines\n", 132 | "polynomial\n", 133 | " Polynomial tools\n", 134 | "testing\n", 135 | " NumPy testing tools\n", 136 | "f2py\n", 137 | " Fortran to Python Interface Generator.\n", 138 | "distutils\n", 139 | " Enhancements to distutils with support for\n", 140 | " Fortran compilers support and more.\n", 141 | "\n", 142 | "Utilities\n", 143 | "---------\n", 144 | "test\n", 145 | " Run numpy unittests\n", 146 | "show_config\n", 147 | " Show numpy build configuration\n", 148 | "dual\n", 149 | " Overwrite certain functions with high-performance Scipy tools\n", 150 | "matlib\n", 151 | " Make everything matrices.\n", 152 | "__version__\n", 153 | " NumPy version string\n", 154 | "\n", 155 | "Viewing documentation using IPython\n", 156 | "-----------------------------------\n", 157 | "Start IPython with the NumPy profile (``ipython -p numpy``), which will\n", 158 | "import `numpy` under the alias `np`. Then, use the ``cpaste`` command to\n", 159 | "paste examples into the shell. To see which functions are available in\n", 160 | "`numpy`, type ``np.`` (where ```` refers to the TAB key), or use\n", 161 | "``np.*cos*?`` (where ```` refers to the ENTER key) to narrow\n", 162 | "down the list. To view the docstring for a function, use\n", 163 | "``np.cos?`` (to view the docstring) and ``np.cos??`` (to view\n", 164 | "the source code).\n", 165 | "\n", 166 | "Copies vs. in-place operation\n", 167 | "-----------------------------\n", 168 | "Most of the functions in `numpy` return a copy of the array argument\n", 169 | "(e.g., `np.sort`). In-place versions of these functions are often\n", 170 | "available as array methods, i.e. ``x = np.array([1,2,3]); x.sort()``.\n", 171 | "Exceptions to this rule are documented.\n", 172 | "\n", 173 | "\n" 174 | ] 175 | } 176 | ], 177 | "source": [ 178 | "import numpy\n", 179 | "print(numpy.__doc__)" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 5, 185 | "metadata": {}, 186 | "outputs": [ 187 | { 188 | "name": "stdout", 189 | "output_type": "stream", 190 | "text": [ 191 | "\n", 192 | "This is an object-oriented plotting library.\n", 193 | "\n", 194 | "A procedural interface is provided by the companion pyplot module,\n", 195 | "which may be imported directly, e.g.::\n", 196 | "\n", 197 | " import matplotlib.pyplot as plt\n", 198 | "\n", 199 | "or using ipython::\n", 200 | "\n", 201 | " ipython\n", 202 | "\n", 203 | "at your terminal, followed by::\n", 204 | "\n", 205 | " In [1]: %matplotlib\n", 206 | " In [2]: import matplotlib.pyplot as plt\n", 207 | "\n", 208 | "at the ipython shell prompt.\n", 209 | "\n", 210 | "For the most part, direct use of the object-oriented library is\n", 211 | "encouraged when programming; pyplot is primarily for working\n", 212 | "interactively. The\n", 213 | "exceptions are the pyplot commands :func:`~matplotlib.pyplot.figure`,\n", 214 | ":func:`~matplotlib.pyplot.subplot`,\n", 215 | ":func:`~matplotlib.pyplot.subplots`, and\n", 216 | ":func:`~pyplot.savefig`, which can greatly simplify scripting.\n", 217 | "\n", 218 | "Modules include:\n", 219 | "\n", 220 | " :mod:`matplotlib.axes`\n", 221 | " defines the :class:`~matplotlib.axes.Axes` class. Most pyplot\n", 222 | " commands are wrappers for :class:`~matplotlib.axes.Axes`\n", 223 | " methods. The axes module is the highest level of OO access to\n", 224 | " the library.\n", 225 | "\n", 226 | " :mod:`matplotlib.figure`\n", 227 | " defines the :class:`~matplotlib.figure.Figure` class.\n", 228 | "\n", 229 | " :mod:`matplotlib.artist`\n", 230 | " defines the :class:`~matplotlib.artist.Artist` base class for\n", 231 | " all classes that draw things.\n", 232 | "\n", 233 | " :mod:`matplotlib.lines`\n", 234 | " defines the :class:`~matplotlib.lines.Line2D` class for\n", 235 | " drawing lines and markers\n", 236 | "\n", 237 | " :mod:`matplotlib.patches`\n", 238 | " defines classes for drawing polygons\n", 239 | "\n", 240 | " :mod:`matplotlib.text`\n", 241 | " defines the :class:`~matplotlib.text.Text`,\n", 242 | " :class:`~matplotlib.text.TextWithDash`, and\n", 243 | " :class:`~matplotlib.text.Annotate` classes\n", 244 | "\n", 245 | " :mod:`matplotlib.image`\n", 246 | " defines the :class:`~matplotlib.image.AxesImage` and\n", 247 | " :class:`~matplotlib.image.FigureImage` classes\n", 248 | "\n", 249 | " :mod:`matplotlib.collections`\n", 250 | " classes for efficient drawing of groups of lines or polygons\n", 251 | "\n", 252 | " :mod:`matplotlib.colors`\n", 253 | " classes for interpreting color specifications and for making\n", 254 | " colormaps\n", 255 | "\n", 256 | " :mod:`matplotlib.cm`\n", 257 | " colormaps and the :class:`~matplotlib.image.ScalarMappable`\n", 258 | " mixin class for providing color mapping functionality to other\n", 259 | " classes\n", 260 | "\n", 261 | " :mod:`matplotlib.ticker`\n", 262 | " classes for calculating tick mark locations and for formatting\n", 263 | " tick labels\n", 264 | "\n", 265 | " :mod:`matplotlib.backends`\n", 266 | " a subpackage with modules for various gui libraries and output\n", 267 | " formats\n", 268 | "\n", 269 | "The base matplotlib namespace includes:\n", 270 | "\n", 271 | " :data:`~matplotlib.rcParams`\n", 272 | " a global dictionary of default configuration settings. It is\n", 273 | " initialized by code which may be overridden by a matplotlibrc\n", 274 | " file.\n", 275 | "\n", 276 | " :func:`~matplotlib.rc`\n", 277 | " a function for setting groups of rcParams values\n", 278 | "\n", 279 | " :func:`~matplotlib.use`\n", 280 | " a function for setting the matplotlib backend. If used, this\n", 281 | " function must be called immediately after importing matplotlib\n", 282 | " for the first time. In particular, it must be called\n", 283 | " **before** importing pyplot (if pyplot is imported).\n", 284 | "\n", 285 | "matplotlib was initially written by John D. Hunter (1968-2012) and is now\n", 286 | "developed and maintained by a host of others.\n", 287 | "\n", 288 | "Occasionally the internal documentation (python docstrings) will refer\n", 289 | "to MATLAB®, a registered trademark of The MathWorks, Inc.\n", 290 | "\n", 291 | "\n" 292 | ] 293 | } 294 | ], 295 | "source": [ 296 | "import matplotlib\n", 297 | "print(matplotlib.__doc__)" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 6, 303 | "metadata": {}, 304 | "outputs": [ 305 | { 306 | "name": "stdout", 307 | "output_type": "stream", 308 | "text": [ 309 | "None\n" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "import seaborn\n", 315 | "print(seaborn.__doc__)" 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": 9, 321 | "metadata": {}, 322 | "outputs": [ 323 | { 324 | "name": "stdout", 325 | "output_type": "stream", 326 | "text": [ 327 | "This module provides access to the mathematical functions\n", 328 | "defined by the C standard.\n" 329 | ] 330 | } 331 | ], 332 | "source": [ 333 | "import math\n", 334 | "print(math.__doc__)" 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": 10, 340 | "metadata": {}, 341 | "outputs": [ 342 | { 343 | "name": "stdout", 344 | "output_type": "stream", 345 | "text": [ 346 | "None\n" 347 | ] 348 | } 349 | ], 350 | "source": [ 351 | "import keras\n", 352 | "print(keras.__doc__)" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": 13, 358 | "metadata": {}, 359 | "outputs": [ 360 | { 361 | "name": "stdout", 362 | "output_type": "stream", 363 | "text": [ 364 | "\n", 365 | "Top-level module of TensorFlow. By convention, we refer to this module as \n", 366 | "`tf` instead of `tensorflow`, following the common practice of importing \n", 367 | "TensorFlow via the command `import tensorflow as tf`.\n", 368 | "\n", 369 | "The primary function of this module is to import all of the public TensorFlow \n", 370 | "interfaces into a single place. The interfaces themselves are located in \n", 371 | "sub-modules, as described below.\n", 372 | "\n", 373 | "Note that the file `__init__.py` in the TensorFlow source code tree is actually \n", 374 | "only a placeholder to enable test cases to run. The TensorFlow build replaces \n", 375 | "this file with a file generated from [`api_template.__init__.py`](https://www.github.com/tensorflow/tensorflow/blob/master/tensorflow/api_template.__init__.py)\n", 376 | "\n" 377 | ] 378 | } 379 | ], 380 | "source": [ 381 | "import tensorflow\n", 382 | "print(tensorflow.__doc__)" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 21, 388 | "metadata": {}, 389 | "outputs": [ 390 | { 391 | "name": "stdout", 392 | "output_type": "stream", 393 | "text": [ 394 | "XGBoost: eXtreme Gradient Boosting library.\n", 395 | "\n", 396 | "Contributors: https://github.com/dmlc/xgboost/blob/master/CONTRIBUTORS.md\n", 397 | "\n" 398 | ] 399 | } 400 | ], 401 | "source": [ 402 | "import xgboost\n", 403 | "print(xgboost.__doc__)" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 22, 409 | "metadata": {}, 410 | "outputs": [ 411 | { 412 | "name": "stdout", 413 | "output_type": "stream", 414 | "text": [ 415 | "Support for regular expressions (RE).\n", 416 | "\n", 417 | "This module provides regular expression matching operations similar to\n", 418 | "those found in Perl. It supports both 8-bit and Unicode strings; both\n", 419 | "the pattern and the strings being processed can contain null bytes and\n", 420 | "characters outside the US ASCII range.\n", 421 | "\n", 422 | "Regular expressions can contain both special and ordinary characters.\n", 423 | "Most ordinary characters, like \"A\", \"a\", or \"0\", are the simplest\n", 424 | "regular expressions; they simply match themselves. You can\n", 425 | "concatenate ordinary characters, so last matches the string 'last'.\n", 426 | "\n", 427 | "The special characters are:\n", 428 | " \".\" Matches any character except a newline.\n", 429 | " \"^\" Matches the start of the string.\n", 430 | " \"$\" Matches the end of the string or just before the newline at\n", 431 | " the end of the string.\n", 432 | " \"*\" Matches 0 or more (greedy) repetitions of the preceding RE.\n", 433 | " Greedy means that it will match as many repetitions as possible.\n", 434 | " \"+\" Matches 1 or more (greedy) repetitions of the preceding RE.\n", 435 | " \"?\" Matches 0 or 1 (greedy) of the preceding RE.\n", 436 | " *?,+?,?? Non-greedy versions of the previous three special characters.\n", 437 | " {m,n} Matches from m to n repetitions of the preceding RE.\n", 438 | " {m,n}? Non-greedy version of the above.\n", 439 | " \"\\\\\" Either escapes special characters or signals a special sequence.\n", 440 | " [] Indicates a set of characters.\n", 441 | " A \"^\" as the first character indicates a complementing set.\n", 442 | " \"|\" A|B, creates an RE that will match either A or B.\n", 443 | " (...) Matches the RE inside the parentheses.\n", 444 | " The contents can be retrieved or matched later in the string.\n", 445 | " (?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).\n", 446 | " (?:...) Non-grouping version of regular parentheses.\n", 447 | " (?P...) The substring matched by the group is accessible by name.\n", 448 | " (?P=name) Matches the text matched earlier by the group named name.\n", 449 | " (?#...) A comment; ignored.\n", 450 | " (?=...) Matches if ... matches next, but doesn't consume the string.\n", 451 | " (?!...) Matches if ... doesn't match next.\n", 452 | " (?<=...) Matches if preceded by ... (must be fixed length).\n", 453 | " (? string\n", 535 | " load(file) -> object\n", 536 | " loads(string) -> object\n", 537 | "\n", 538 | "Misc variables:\n", 539 | "\n", 540 | " __version__\n", 541 | " format_version\n", 542 | " compatible_formats\n", 543 | "\n", 544 | "\n" 545 | ] 546 | } 547 | ], 548 | "source": [ 549 | "import pickle\n", 550 | "print(pickle.__doc__)" 551 | ] 552 | }, 553 | { 554 | "cell_type": "markdown", 555 | "metadata": {}, 556 | "source": [ 557 | "**Docstrings for Python Functions**\n", 558 | "\n", 559 | "The docstring for a function or method should summarize its behavior and document its arguments and return values.\n", 560 | "\n", 561 | "It should also list all the exceptions that can be raised and other optional arguments." 562 | ] 563 | }, 564 | { 565 | "cell_type": "code", 566 | "execution_count": 33, 567 | "metadata": {}, 568 | "outputs": [ 569 | { 570 | "name": "stdout", 571 | "output_type": "stream", 572 | "text": [ 573 | "\n", 574 | " Returns the sum of two decimal numbers in binary digits.\n", 575 | "\n", 576 | " Parameters:\n", 577 | " a (int): A decimal integer\n", 578 | " b (int): Another decimal integer\n", 579 | "\n", 580 | " Returns:\n", 581 | " binary_sum (str): Binary string of the sum of a and b\n", 582 | " \n" 583 | ] 584 | } 585 | ], 586 | "source": [ 587 | "def add_binary(a, b):\n", 588 | " '''\n", 589 | " Returns the sum of two decimal numbers in binary digits.\n", 590 | "\n", 591 | " Parameters:\n", 592 | " a (int): A decimal integer\n", 593 | " b (int): Another decimal integer\n", 594 | "\n", 595 | " Returns:\n", 596 | " binary_sum (str): Binary string of the sum of a and b\n", 597 | " '''\n", 598 | " binary_sum = bin(a+b)[2:]\n", 599 | " return binary_sum\n", 600 | "\n", 601 | "\n", 602 | "print(add_binary.__doc__)" 603 | ] 604 | }, 605 | { 606 | "cell_type": "code", 607 | "execution_count": 34, 608 | "metadata": {}, 609 | "outputs": [ 610 | { 611 | "data": { 612 | "text/plain": [ 613 | "'11'" 614 | ] 615 | }, 616 | "execution_count": 34, 617 | "metadata": {}, 618 | "output_type": "execute_result" 619 | } 620 | ], 621 | "source": [ 622 | "add_binary(1,2)" 623 | ] 624 | }, 625 | { 626 | "cell_type": "code", 627 | "execution_count": 35, 628 | "metadata": {}, 629 | "outputs": [ 630 | { 631 | "data": { 632 | "text/plain": [ 633 | "'0b11'" 634 | ] 635 | }, 636 | "execution_count": 35, 637 | "metadata": {}, 638 | "output_type": "execute_result" 639 | } 640 | ], 641 | "source": [ 642 | "bin(3)" 643 | ] 644 | }, 645 | { 646 | "cell_type": "code", 647 | "execution_count": 45, 648 | "metadata": {}, 649 | "outputs": [], 650 | "source": [ 651 | "def add_binary(a, b):\n", 652 | " '''\n", 653 | " Returns the sum of two decimal numbers in binary digits.\n", 654 | "\n", 655 | " Parameters:\n", 656 | " a (int): A decimal integer\n", 657 | " b (int): Another decimal integer\n", 658 | "\n", 659 | " Returns:\n", 660 | " binary_sum (str): Binary string of the sum of a and b\n", 661 | " '''\n", 662 | " binary_sum = bin(a+b)\n", 663 | " return binary_sum" 664 | ] 665 | }, 666 | { 667 | "cell_type": "code", 668 | "execution_count": 46, 669 | "metadata": {}, 670 | "outputs": [ 671 | { 672 | "data": { 673 | "text/plain": [ 674 | "'0b11'" 675 | ] 676 | }, 677 | "execution_count": 46, 678 | "metadata": {}, 679 | "output_type": "execute_result" 680 | } 681 | ], 682 | "source": [ 683 | "add_binary(1,2)" 684 | ] 685 | }, 686 | { 687 | "cell_type": "code", 688 | "execution_count": 38, 689 | "metadata": {}, 690 | "outputs": [ 691 | { 692 | "data": { 693 | "text/plain": [ 694 | "" 695 | ] 696 | }, 697 | "execution_count": 38, 698 | "metadata": {}, 699 | "output_type": "execute_result" 700 | } 701 | ], 702 | "source": [ 703 | "bin" 704 | ] 705 | }, 706 | { 707 | "cell_type": "code", 708 | "execution_count": 47, 709 | "metadata": {}, 710 | "outputs": [], 711 | "source": [ 712 | "class Person:\n", 713 | " \"\"\"\n", 714 | " A class to represent a person.\n", 715 | "\n", 716 | " ...\n", 717 | "\n", 718 | " Attributes\n", 719 | " ----------\n", 720 | " name : str\n", 721 | " first name of the person\n", 722 | " surname : str\n", 723 | " family name of the person\n", 724 | " age : int\n", 725 | " age of the person\n", 726 | "\n", 727 | " Methods\n", 728 | " -------\n", 729 | " info(additional=\"\"):\n", 730 | " Prints the person's name and age.\n", 731 | " \"\"\"\n", 732 | "\n", 733 | " def __init__(self, name, surname, age):\n", 734 | " \"\"\"\n", 735 | " Constructs all the necessary attributes for the person object.\n", 736 | "\n", 737 | " Parameters\n", 738 | " ----------\n", 739 | " name : str\n", 740 | " first name of the person\n", 741 | " surname : str\n", 742 | " family name of the person\n", 743 | " age : int\n", 744 | " age of the person\n", 745 | " \"\"\"\n", 746 | "\n", 747 | " self.name = name\n", 748 | " self.surname = surname\n", 749 | " self.age = age\n", 750 | "\n", 751 | " def info(self, additional=\"\"):\n", 752 | " \"\"\"\n", 753 | " Prints the person's name and age.\n", 754 | "\n", 755 | " If the argument 'additional' is passed, then it is appended after the main info.\n", 756 | "\n", 757 | " Parameters\n", 758 | " ----------\n", 759 | " additional : str, optional\n", 760 | " More info to be displayed (default is None)\n", 761 | "\n", 762 | " Returns\n", 763 | " -------\n", 764 | " None\n", 765 | " \"\"\"\n", 766 | "\n", 767 | " print(f'My name is {self.name} {self.surname}. I am {self.age} years old.' + additional)" 768 | ] 769 | }, 770 | { 771 | "cell_type": "code", 772 | "execution_count": 48, 773 | "metadata": {}, 774 | "outputs": [ 775 | { 776 | "name": "stdout", 777 | "output_type": "stream", 778 | "text": [ 779 | "\n", 780 | " A class to represent a person.\n", 781 | "\n", 782 | " ...\n", 783 | "\n", 784 | " Attributes\n", 785 | " ----------\n", 786 | " name : str\n", 787 | " first name of the person\n", 788 | " surname : str\n", 789 | " family name of the person\n", 790 | " age : int\n", 791 | " age of the person\n", 792 | "\n", 793 | " Methods\n", 794 | " -------\n", 795 | " info(additional=\"\"):\n", 796 | " Prints the person's name and age.\n", 797 | " \n" 798 | ] 799 | } 800 | ], 801 | "source": [ 802 | "print(Person.__doc__)" 803 | ] 804 | }, 805 | { 806 | "cell_type": "code", 807 | "execution_count": 51, 808 | "metadata": {}, 809 | "outputs": [ 810 | { 811 | "name": "stdout", 812 | "output_type": "stream", 813 | "text": [ 814 | "Help on class Person in module __main__:\n", 815 | "\n", 816 | "class Person(builtins.object)\n", 817 | " | Person(name, surname, age)\n", 818 | " | \n", 819 | " | A class to represent a person.\n", 820 | " | \n", 821 | " | ...\n", 822 | " | \n", 823 | " | Attributes\n", 824 | " | ----------\n", 825 | " | name : str\n", 826 | " | first name of the person\n", 827 | " | surname : str\n", 828 | " | family name of the person\n", 829 | " | age : int\n", 830 | " | age of the person\n", 831 | " | \n", 832 | " | Methods\n", 833 | " | -------\n", 834 | " | info(additional=\"\"):\n", 835 | " | Prints the person's name and age.\n", 836 | " | \n", 837 | " | Methods defined here:\n", 838 | " | \n", 839 | " | __init__(self, name, surname, age)\n", 840 | " | Constructs all the necessary attributes for the person object.\n", 841 | " | \n", 842 | " | Parameters\n", 843 | " | ----------\n", 844 | " | name : str\n", 845 | " | first name of the person\n", 846 | " | surname : str\n", 847 | " | family name of the person\n", 848 | " | age : int\n", 849 | " | age of the person\n", 850 | " | \n", 851 | " | info(self, additional='')\n", 852 | " | Prints the person's name and age.\n", 853 | " | \n", 854 | " | If the argument 'additional' is passed, then it is appended after the main info.\n", 855 | " | \n", 856 | " | Parameters\n", 857 | " | ----------\n", 858 | " | additional : str, optional\n", 859 | " | More info to be displayed (default is None)\n", 860 | " | \n", 861 | " | Returns\n", 862 | " | -------\n", 863 | " | None\n", 864 | " | \n", 865 | " | ----------------------------------------------------------------------\n", 866 | " | Data descriptors defined here:\n", 867 | " | \n", 868 | " | __dict__\n", 869 | " | dictionary for instance variables (if defined)\n", 870 | " | \n", 871 | " | __weakref__\n", 872 | " | list of weak references to the object (if defined)\n", 873 | "\n" 874 | ] 875 | } 876 | ], 877 | "source": [ 878 | "help(Person)" 879 | ] 880 | }, 881 | { 882 | "cell_type": "markdown", 883 | "metadata": {}, 884 | "source": [ 885 | "Here, we can see that the help() function retrieves the docstrings of the Person class along with the methods associated with that class." 886 | ] 887 | }, 888 | { 889 | "cell_type": "code", 890 | "execution_count": null, 891 | "metadata": {}, 892 | "outputs": [], 893 | "source": [] 894 | } 895 | ], 896 | "metadata": { 897 | "kernelspec": { 898 | "display_name": "Python 3", 899 | "language": "python", 900 | "name": "python3" 901 | }, 902 | "language_info": { 903 | "codemirror_mode": { 904 | "name": "ipython", 905 | "version": 3 906 | }, 907 | "file_extension": ".py", 908 | "mimetype": "text/x-python", 909 | "name": "python", 910 | "nbconvert_exporter": "python", 911 | "pygments_lexer": "ipython3", 912 | "version": "3.7.6" 913 | } 914 | }, 915 | "nbformat": 4, 916 | "nbformat_minor": 4 917 | } 918 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Basics 2 | source : https://www.programiz.com/python-programming 3 | -------------------------------------------------------------------------------- /for_loop.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "https://youtu.be/RvvXo2-9bnE?si=nXJxcBpgSHx6HaIq " 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "150\n" 20 | ] 21 | } 22 | ], 23 | "source": [ 24 | "num = [10,20,30,40,50]\n", 25 | "\n", 26 | "result = 0 \n", 27 | "\n", 28 | "for i in num:\n", 29 | " result += i\n", 30 | " \n", 31 | "print(result)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 2, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "data": { 41 | "text/plain": [ 42 | "150" 43 | ] 44 | }, 45 | "execution_count": 2, 46 | "metadata": {}, 47 | "output_type": "execute_result" 48 | } 49 | ], 50 | "source": [ 51 | "sum(num)" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 3, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "from functools import reduce" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 5, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "150\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "num = [10,20,30,40,50]\n", 78 | "\n", 79 | "result_ = reduce(lambda x,y:x+y , num)\n", 80 | "print(result_)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 8, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "0 10\n", 93 | "1 20\n", 94 | "2 30\n", 95 | "3 40\n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "num = [10,20,30,40]\n", 101 | "\n", 102 | "for index in range(len(num)):\n", 103 | " print(index, num[index])" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 9, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "0 10\n", 116 | "1 20\n", 117 | "2 30\n", 118 | "3 40\n" 119 | ] 120 | } 121 | ], 122 | "source": [ 123 | "for index, value in enumerate(num):\n", 124 | " print(index, value)" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 10, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "1 10\n", 137 | "2 20\n", 138 | "3 30\n", 139 | "4 40\n" 140 | ] 141 | } 142 | ], 143 | "source": [ 144 | "for index, value in enumerate(num, start = 1):\n", 145 | " print(index, value)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 12, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "10 a\n", 158 | "20 b\n", 159 | "30 c\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "a = [10,20,30]\n", 165 | "b = ['a', 'b', 'c']\n", 166 | "\n", 167 | "for index in range(len(a)):\n", 168 | " print(a[index], b[index])" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 13, 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "name": "stdout", 178 | "output_type": "stream", 179 | "text": [ 180 | "10 a\n", 181 | "20 b\n", 182 | "30 c\n" 183 | ] 184 | }, 185 | { 186 | "ename": "IndexError", 187 | "evalue": "list index out of range", 188 | "output_type": "error", 189 | "traceback": [ 190 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 191 | "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", 192 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mindex\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 193 | "\u001b[1;31mIndexError\u001b[0m: list index out of range" 194 | ] 195 | } 196 | ], 197 | "source": [ 198 | "a = [10, 20,30,40]\n", 199 | "b = [\"a\", \"b\", \"c\"]\n", 200 | "\n", 201 | "for index in range(len(a)):\n", 202 | " print(a[index], b[index])" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 16, 208 | "metadata": {}, 209 | "outputs": [ 210 | { 211 | "name": "stdout", 212 | "output_type": "stream", 213 | "text": [ 214 | "10 a\n", 215 | "20 b\n", 216 | "30 c\n" 217 | ] 218 | } 219 | ], 220 | "source": [ 221 | "a = [10, 20, 30, 40]\n", 222 | "b = [\"a\", \"b\", \"c\"]\n", 223 | "\n", 224 | "for val1, val2 in zip(a,b):\n", 225 | " print(val1,val2)" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 25, 231 | "metadata": {}, 232 | "outputs": [ 233 | { 234 | "data": { 235 | "text/plain": [ 236 | "'3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]'" 237 | ] 238 | }, 239 | "execution_count": 25, 240 | "metadata": {}, 241 | "output_type": "execute_result" 242 | } 243 | ], 244 | "source": [ 245 | "import sys\n", 246 | "sys.version" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 27, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "data": { 256 | "text/plain": [ 257 | "{1: 's', 2: 'i', 3: 'd', 4: 'd', 5: 'h', 6: 'i'}" 258 | ] 259 | }, 260 | "execution_count": 27, 261 | "metadata": {}, 262 | "output_type": "execute_result" 263 | } 264 | ], 265 | "source": [ 266 | "a = 'siddhi'\n", 267 | "\n", 268 | "dict(zip(range(1,7), a))" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 30, 274 | "metadata": {}, 275 | "outputs": [], 276 | "source": [ 277 | "def palindrome():\n", 278 | " s = input(\"enter a number - \")\n", 279 | " \n", 280 | " reverse = s[::-1]\n", 281 | " \n", 282 | " if s == reverse:\n", 283 | " return \"a palindrome\"\n", 284 | " return \"not a palindrome\"" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 29, 290 | "metadata": {}, 291 | "outputs": [ 292 | { 293 | "name": "stdout", 294 | "output_type": "stream", 295 | "text": [ 296 | "enter a number121\n" 297 | ] 298 | }, 299 | { 300 | "data": { 301 | "text/plain": [ 302 | "'a palindrome'" 303 | ] 304 | }, 305 | "execution_count": 29, 306 | "metadata": {}, 307 | "output_type": "execute_result" 308 | } 309 | ], 310 | "source": [ 311 | "palindrome()" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 31, 317 | "metadata": {}, 318 | "outputs": [ 319 | { 320 | "name": "stdout", 321 | "output_type": "stream", 322 | "text": [ 323 | "enter a number - madam\n" 324 | ] 325 | }, 326 | { 327 | "data": { 328 | "text/plain": [ 329 | "'a palindrome'" 330 | ] 331 | }, 332 | "execution_count": 31, 333 | "metadata": {}, 334 | "output_type": "execute_result" 335 | } 336 | ], 337 | "source": [ 338 | "palindrome()" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 32, 344 | "metadata": {}, 345 | "outputs": [ 346 | { 347 | "name": "stdout", 348 | "output_type": "stream", 349 | "text": [ 350 | "enter a number - 1234\n" 351 | ] 352 | }, 353 | { 354 | "data": { 355 | "text/plain": [ 356 | "'not a palindrome'" 357 | ] 358 | }, 359 | "execution_count": 32, 360 | "metadata": {}, 361 | "output_type": "execute_result" 362 | } 363 | ], 364 | "source": [ 365 | "palindrome()" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 33, 371 | "metadata": {}, 372 | "outputs": [ 373 | { 374 | "name": "stdout", 375 | "output_type": "stream", 376 | "text": [ 377 | "10\n" 378 | ] 379 | } 380 | ], 381 | "source": [ 382 | "events = [(\"learn\", 5), (\"learn\", 5), (\"relaxed\", 20)]\n", 383 | "\n", 384 | "min_studied = 0\n", 385 | "\n", 386 | "for event in events:\n", 387 | " if event[0] == \"learn\":\n", 388 | " min_studied += event[1]\n", 389 | "print(min_studied)\n", 390 | " " 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": 39, 396 | "metadata": {}, 397 | "outputs": [ 398 | { 399 | "data": { 400 | "text/plain": [ 401 | "10" 402 | ] 403 | }, 404 | "execution_count": 39, 405 | "metadata": {}, 406 | "output_type": "execute_result" 407 | } 408 | ], 409 | "source": [ 410 | "study_time = (event[1] for event in events if event[0]==\"learn\")\n", 411 | "sum(study_time)" 412 | ] 413 | }, 414 | { 415 | "cell_type": "code", 416 | "execution_count": 34, 417 | "metadata": {}, 418 | "outputs": [], 419 | "source": [ 420 | "import pandas as pd" 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": 35, 426 | "metadata": {}, 427 | "outputs": [], 428 | "source": [ 429 | "df = pd.DataFrame(events, columns= [\"event\", \"time\"])" 430 | ] 431 | }, 432 | { 433 | "cell_type": "code", 434 | "execution_count": 36, 435 | "metadata": {}, 436 | "outputs": [ 437 | { 438 | "data": { 439 | "text/html": [ 440 | "
\n", 441 | "\n", 454 | "\n", 455 | " \n", 456 | " \n", 457 | " \n", 458 | " \n", 459 | " \n", 460 | " \n", 461 | " \n", 462 | " \n", 463 | " \n", 464 | " \n", 465 | " \n", 466 | " \n", 467 | " \n", 468 | " \n", 469 | " \n", 470 | " \n", 471 | " \n", 472 | " \n", 473 | " \n", 474 | " \n", 475 | " \n", 476 | " \n", 477 | " \n", 478 | " \n", 479 | "
eventtime
0learn5
1learn5
2relaxed20
\n", 480 | "
" 481 | ], 482 | "text/plain": [ 483 | " event time\n", 484 | "0 learn 5\n", 485 | "1 learn 5\n", 486 | "2 relaxed 20" 487 | ] 488 | }, 489 | "execution_count": 36, 490 | "metadata": {}, 491 | "output_type": "execute_result" 492 | } 493 | ], 494 | "source": [ 495 | "df" 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": 38, 501 | "metadata": {}, 502 | "outputs": [ 503 | { 504 | "data": { 505 | "text/plain": [ 506 | "event learnlearn\n", 507 | "time 10\n", 508 | "dtype: object" 509 | ] 510 | }, 511 | "execution_count": 38, 512 | "metadata": {}, 513 | "output_type": "execute_result" 514 | } 515 | ], 516 | "source": [ 517 | "df[df['event'] == \"learn\"].sum()" 518 | ] 519 | }, 520 | { 521 | "cell_type": "code", 522 | "execution_count": 40, 523 | "metadata": {}, 524 | "outputs": [], 525 | "source": [ 526 | "# use itertools" 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "execution_count": 46, 532 | "metadata": {}, 533 | "outputs": [], 534 | "source": [ 535 | "lines = [\"line1\", \"line2\", \"line3\", \"line4\", \"line5\", \"line6\", \"line7\", \"line8\", \"line9\", \"line10\"]" 536 | ] 537 | }, 538 | { 539 | "cell_type": "code", 540 | "execution_count": 50, 541 | "metadata": {}, 542 | "outputs": [ 543 | { 544 | "name": "stdout", 545 | "output_type": "stream", 546 | "text": [ 547 | "line1\n", 548 | "line2\n", 549 | "line3\n", 550 | "line4\n" 551 | ] 552 | } 553 | ], 554 | "source": [ 555 | "for i, line in enumerate(lines):\n", 556 | " if i>= 4:\n", 557 | " break\n", 558 | " print(line)" 559 | ] 560 | }, 561 | { 562 | "cell_type": "code", 563 | "execution_count": 51, 564 | "metadata": {}, 565 | "outputs": [ 566 | { 567 | "data": { 568 | "text/plain": [ 569 | "['line1', 'line3', 'line5', 'line7', 'line9']" 570 | ] 571 | }, 572 | "execution_count": 51, 573 | "metadata": {}, 574 | "output_type": "execute_result" 575 | } 576 | ], 577 | "source": [ 578 | "lines[::2]" 579 | ] 580 | }, 581 | { 582 | "cell_type": "code", 583 | "execution_count": 52, 584 | "metadata": {}, 585 | "outputs": [ 586 | { 587 | "data": { 588 | "text/plain": [ 589 | "['line10',\n", 590 | " 'line9',\n", 591 | " 'line8',\n", 592 | " 'line7',\n", 593 | " 'line6',\n", 594 | " 'line5',\n", 595 | " 'line4',\n", 596 | " 'line3',\n", 597 | " 'line2',\n", 598 | " 'line1']" 599 | ] 600 | }, 601 | "execution_count": 52, 602 | "metadata": {}, 603 | "output_type": "execute_result" 604 | } 605 | ], 606 | "source": [ 607 | "lines[::-1]" 608 | ] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "execution_count": 53, 613 | "metadata": {}, 614 | "outputs": [ 615 | { 616 | "data": { 617 | "text/plain": [ 618 | "['line1', 'line2', 'line3', 'line4', 'line5']" 619 | ] 620 | }, 621 | "execution_count": 53, 622 | "metadata": {}, 623 | "output_type": "execute_result" 624 | } 625 | ], 626 | "source": [ 627 | "lines[:5]" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": 54, 633 | "metadata": {}, 634 | "outputs": [ 635 | { 636 | "name": "stdout", 637 | "output_type": "stream", 638 | "text": [ 639 | "line1\n", 640 | "line2\n", 641 | "line3\n", 642 | "line4\n", 643 | "line5\n" 644 | ] 645 | } 646 | ], 647 | "source": [ 648 | "from itertools import islice\n", 649 | "\n", 650 | "first_5_lines = islice(lines, 5)\n", 651 | "\n", 652 | "for line in first_5_lines:\n", 653 | " print(line)" 654 | ] 655 | }, 656 | { 657 | "cell_type": "code", 658 | "execution_count": 55, 659 | "metadata": {}, 660 | "outputs": [ 661 | { 662 | "name": "stdout", 663 | "output_type": "stream", 664 | "text": [ 665 | "line2\n", 666 | "line3\n", 667 | "line4\n", 668 | "line5\n" 669 | ] 670 | } 671 | ], 672 | "source": [ 673 | "from itertools import islice\n", 674 | "\n", 675 | "first_5_lines = islice(lines,1, 5)\n", 676 | "\n", 677 | "for line in first_5_lines:\n", 678 | " print(line)" 679 | ] 680 | }, 681 | { 682 | "cell_type": "code", 683 | "execution_count": 56, 684 | "metadata": {}, 685 | "outputs": [ 686 | { 687 | "name": "stdout", 688 | "output_type": "stream", 689 | "text": [ 690 | "line2\n", 691 | "line4\n" 692 | ] 693 | } 694 | ], 695 | "source": [ 696 | "from itertools import islice\n", 697 | "\n", 698 | "first_5_lines = islice(lines,1, 5,2)\n", 699 | "\n", 700 | "for line in first_5_lines:\n", 701 | " print(line)" 702 | ] 703 | }, 704 | { 705 | "cell_type": "code", 706 | "execution_count": 60, 707 | "metadata": {}, 708 | "outputs": [ 709 | { 710 | "name": "stdout", 711 | "output_type": "stream", 712 | "text": [ 713 | "A B\n", 714 | "B C\n", 715 | "C D\n", 716 | "D E\n", 717 | "E F\n", 718 | "F G\n" 719 | ] 720 | } 721 | ], 722 | "source": [ 723 | "data = 'ABCDEFG'\n", 724 | "\n", 725 | "for i in range(len(data) - 1):\n", 726 | " print(data[i], data[i+1])\n", 727 | " " 728 | ] 729 | }, 730 | { 731 | "cell_type": "code", 732 | "execution_count": 62, 733 | "metadata": {}, 734 | "outputs": [ 735 | { 736 | "name": "stdout", 737 | "output_type": "stream", 738 | "text": [ 739 | "1\n", 740 | "2\n", 741 | "4\n" 742 | ] 743 | } 744 | ], 745 | "source": [ 746 | "for item in [1,2,4,-1,4,1]:\n", 747 | " if item >=0:\n", 748 | " print(item)\n", 749 | " else:\n", 750 | " break" 751 | ] 752 | }, 753 | { 754 | "cell_type": "code", 755 | "execution_count": 64, 756 | "metadata": {}, 757 | "outputs": [ 758 | { 759 | "name": "stdout", 760 | "output_type": "stream", 761 | "text": [ 762 | "1\n", 763 | "2\n", 764 | "4\n", 765 | "4\n", 766 | "1\n" 767 | ] 768 | } 769 | ], 770 | "source": [ 771 | "for item in [1,2,4,-1,4,1]:\n", 772 | " if item >=0:\n", 773 | " print(item)" 774 | ] 775 | }, 776 | { 777 | "cell_type": "code", 778 | "execution_count": 65, 779 | "metadata": {}, 780 | "outputs": [], 781 | "source": [ 782 | "from itertools import takewhile" 783 | ] 784 | }, 785 | { 786 | "cell_type": "code", 787 | "execution_count": 66, 788 | "metadata": {}, 789 | "outputs": [ 790 | { 791 | "name": "stdout", 792 | "output_type": "stream", 793 | "text": [ 794 | "1\n", 795 | "2\n", 796 | "4\n" 797 | ] 798 | } 799 | ], 800 | "source": [ 801 | "items = takewhile(lambda x: x>=0, [1,2,4,-1,4,1])\n", 802 | "\n", 803 | "for item in items:\n", 804 | " print(item)" 805 | ] 806 | }, 807 | { 808 | "cell_type": "code", 809 | "execution_count": 67, 810 | "metadata": {}, 811 | "outputs": [ 812 | { 813 | "name": "stdout", 814 | "output_type": "stream", 815 | "text": [ 816 | "32\n" 817 | ] 818 | } 819 | ], 820 | "source": [ 821 | "import numpy as np\n", 822 | "\n", 823 | "a = [1,2,3]\n", 824 | "b = [4,5,6]\n", 825 | "\n", 826 | "result = 0\n", 827 | "\n", 828 | "for val1, val2 in zip(a,b):\n", 829 | " result += val1 * val2\n", 830 | "print(result)" 831 | ] 832 | }, 833 | { 834 | "cell_type": "code", 835 | "execution_count": 73, 836 | "metadata": {}, 837 | "outputs": [ 838 | { 839 | "name": "stdout", 840 | "output_type": "stream", 841 | "text": [ 842 | "4\n", 843 | "10\n", 844 | "18\n" 845 | ] 846 | } 847 | ], 848 | "source": [ 849 | "import numpy as np\n", 850 | "\n", 851 | "a = [1,2,3]\n", 852 | "b = [4,5,6]\n", 853 | "\n", 854 | "result = 0\n", 855 | "\n", 856 | "for val1, val2 in zip(a,b):\n", 857 | " print(val1 * val2)\n" 858 | ] 859 | }, 860 | { 861 | "cell_type": "code", 862 | "execution_count": 77, 863 | "metadata": {}, 864 | "outputs": [ 865 | { 866 | "data": { 867 | "text/plain": [ 868 | "32" 869 | ] 870 | }, 871 | "execution_count": 77, 872 | "metadata": {}, 873 | "output_type": "execute_result" 874 | } 875 | ], 876 | "source": [ 877 | "np.dot(a,b)" 878 | ] 879 | }, 880 | { 881 | "cell_type": "code", 882 | "execution_count": 78, 883 | "metadata": {}, 884 | "outputs": [], 885 | "source": [ 886 | "n = 100_000_000\n", 887 | "def sum_python():\n", 888 | " return sum(range(n))" 889 | ] 890 | }, 891 | { 892 | "cell_type": "code", 893 | "execution_count": 79, 894 | "metadata": {}, 895 | "outputs": [], 896 | "source": [ 897 | "def sum_numpy():\n", 898 | " return np.sum(np.arange(n))" 899 | ] 900 | }, 901 | { 902 | "cell_type": "code", 903 | "execution_count": 80, 904 | "metadata": {}, 905 | "outputs": [ 906 | { 907 | "data": { 908 | "text/plain": [ 909 | "4999999950000000" 910 | ] 911 | }, 912 | "execution_count": 80, 913 | "metadata": {}, 914 | "output_type": "execute_result" 915 | } 916 | ], 917 | "source": [ 918 | "sum_python()" 919 | ] 920 | }, 921 | { 922 | "cell_type": "code", 923 | "execution_count": 81, 924 | "metadata": {}, 925 | "outputs": [ 926 | { 927 | "data": { 928 | "text/plain": [ 929 | "887459712" 930 | ] 931 | }, 932 | "execution_count": 81, 933 | "metadata": {}, 934 | "output_type": "execute_result" 935 | } 936 | ], 937 | "source": [ 938 | "sum_numpy()" 939 | ] 940 | }, 941 | { 942 | "cell_type": "code", 943 | "execution_count": 82, 944 | "metadata": {}, 945 | "outputs": [], 946 | "source": [ 947 | "import timeit" 948 | ] 949 | }, 950 | { 951 | "cell_type": "code", 952 | "execution_count": 83, 953 | "metadata": {}, 954 | "outputs": [ 955 | { 956 | "name": "stdout", 957 | "output_type": "stream", 958 | "text": [ 959 | "sum python: 4.958669400000872\n", 960 | "sum numpy: 0.2702554000006785\n" 961 | ] 962 | } 963 | ], 964 | "source": [ 965 | "print(\"sum python: \", timeit.timeit(sum_python, number= 1))\n", 966 | "print(\"sum numpy: \", timeit.timeit(sum_numpy, number= 1))" 967 | ] 968 | }, 969 | { 970 | "cell_type": "code", 971 | "execution_count": 6, 972 | "metadata": {}, 973 | "outputs": [], 974 | "source": [ 975 | "a = [1,2,3]" 976 | ] 977 | }, 978 | { 979 | "cell_type": "code", 980 | "execution_count": 9, 981 | "metadata": {}, 982 | "outputs": [], 983 | "source": [ 984 | "b = list(map(lambda x:x * 5, a))" 985 | ] 986 | }, 987 | { 988 | "cell_type": "code", 989 | "execution_count": 10, 990 | "metadata": {}, 991 | "outputs": [ 992 | { 993 | "data": { 994 | "text/plain": [ 995 | "[5, 10, 15]" 996 | ] 997 | }, 998 | "execution_count": 10, 999 | "metadata": {}, 1000 | "output_type": "execute_result" 1001 | } 1002 | ], 1003 | "source": [ 1004 | "b" 1005 | ] 1006 | }, 1007 | { 1008 | "cell_type": "code", 1009 | "execution_count": 11, 1010 | "metadata": {}, 1011 | "outputs": [], 1012 | "source": [ 1013 | "string1 = \"siddhi\"\n", 1014 | "string2 = \"vinit\"" 1015 | ] 1016 | }, 1017 | { 1018 | "cell_type": "code", 1019 | "execution_count": 12, 1020 | "metadata": {}, 1021 | "outputs": [ 1022 | { 1023 | "data": { 1024 | "text/plain": [ 1025 | "{'i'}" 1026 | ] 1027 | }, 1028 | "execution_count": 12, 1029 | "metadata": {}, 1030 | "output_type": "execute_result" 1031 | } 1032 | ], 1033 | "source": [ 1034 | "set(string1).intersection(set(string2))" 1035 | ] 1036 | }, 1037 | { 1038 | "cell_type": "code", 1039 | "execution_count": 13, 1040 | "metadata": {}, 1041 | "outputs": [], 1042 | "source": [ 1043 | "list_1 = [1,2,3]\n", 1044 | "list_2 = [4,5,6]" 1045 | ] 1046 | }, 1047 | { 1048 | "cell_type": "code", 1049 | "execution_count": 19, 1050 | "metadata": {}, 1051 | "outputs": [ 1052 | { 1053 | "data": { 1054 | "text/plain": [ 1055 | "[1, 2, 3, [4, 5, 6], [4, 5, 6], 4, 5, 6]" 1056 | ] 1057 | }, 1058 | "execution_count": 19, 1059 | "metadata": {}, 1060 | "output_type": "execute_result" 1061 | } 1062 | ], 1063 | "source": [ 1064 | "list_1 + list_2" 1065 | ] 1066 | }, 1067 | { 1068 | "cell_type": "code", 1069 | "execution_count": 20, 1070 | "metadata": {}, 1071 | "outputs": [], 1072 | "source": [ 1073 | "list_1 = [1,2,3]\n", 1074 | "list_2 = [4,5,6]" 1075 | ] 1076 | }, 1077 | { 1078 | "cell_type": "code", 1079 | "execution_count": 22, 1080 | "metadata": {}, 1081 | "outputs": [], 1082 | "source": [ 1083 | "list_1.extend(list_2)" 1084 | ] 1085 | }, 1086 | { 1087 | "cell_type": "code", 1088 | "execution_count": 23, 1089 | "metadata": {}, 1090 | "outputs": [ 1091 | { 1092 | "data": { 1093 | "text/plain": [ 1094 | "[1, 2, 3, 4, 5, 6]" 1095 | ] 1096 | }, 1097 | "execution_count": 23, 1098 | "metadata": {}, 1099 | "output_type": "execute_result" 1100 | } 1101 | ], 1102 | "source": [ 1103 | "list_1" 1104 | ] 1105 | }, 1106 | { 1107 | "cell_type": "code", 1108 | "execution_count": 26, 1109 | "metadata": {}, 1110 | "outputs": [], 1111 | "source": [ 1112 | "list_1 = [1,2,3]\n", 1113 | "list_2 = [4,5,6]" 1114 | ] 1115 | }, 1116 | { 1117 | "cell_type": "code", 1118 | "execution_count": 27, 1119 | "metadata": {}, 1120 | "outputs": [], 1121 | "source": [ 1122 | "for i in list_2:\n", 1123 | " list_1.append(i)" 1124 | ] 1125 | }, 1126 | { 1127 | "cell_type": "code", 1128 | "execution_count": 28, 1129 | "metadata": {}, 1130 | "outputs": [ 1131 | { 1132 | "data": { 1133 | "text/plain": [ 1134 | "[1, 2, 3, 4, 5, 6]" 1135 | ] 1136 | }, 1137 | "execution_count": 28, 1138 | "metadata": {}, 1139 | "output_type": "execute_result" 1140 | } 1141 | ], 1142 | "source": [ 1143 | "list_1" 1144 | ] 1145 | }, 1146 | { 1147 | "cell_type": "code", 1148 | "execution_count": 48, 1149 | "metadata": {}, 1150 | "outputs": [], 1151 | "source": [ 1152 | "def secondlargestnumber(num):\n", 1153 | " num = list(set(num))\n", 1154 | " num = sorted(num)\n", 1155 | " sln = num[-2]\n", 1156 | " return sln" 1157 | ] 1158 | }, 1159 | { 1160 | "cell_type": "code", 1161 | "execution_count": 44, 1162 | "metadata": {}, 1163 | "outputs": [], 1164 | "source": [ 1165 | "array = [10,2,30,4,30,5,6]" 1166 | ] 1167 | }, 1168 | { 1169 | "cell_type": "code", 1170 | "execution_count": 49, 1171 | "metadata": {}, 1172 | "outputs": [ 1173 | { 1174 | "data": { 1175 | "text/plain": [ 1176 | "10" 1177 | ] 1178 | }, 1179 | "execution_count": 49, 1180 | "metadata": {}, 1181 | "output_type": "execute_result" 1182 | } 1183 | ], 1184 | "source": [ 1185 | "secondlargestnumber(array)" 1186 | ] 1187 | }, 1188 | { 1189 | "cell_type": "code", 1190 | "execution_count": 63, 1191 | "metadata": {}, 1192 | "outputs": [], 1193 | "source": [ 1194 | "import pandas as pd" 1195 | ] 1196 | }, 1197 | { 1198 | "cell_type": "code", 1199 | "execution_count": 83, 1200 | "metadata": {}, 1201 | "outputs": [], 1202 | "source": [ 1203 | "b = pd.Series(array)" 1204 | ] 1205 | }, 1206 | { 1207 | "cell_type": "code", 1208 | "execution_count": 84, 1209 | "metadata": {}, 1210 | "outputs": [ 1211 | { 1212 | "data": { 1213 | "text/plain": [ 1214 | "0 10\n", 1215 | "1 2\n", 1216 | "2 30\n", 1217 | "3 4\n", 1218 | "4 30\n", 1219 | "5 5\n", 1220 | "6 6\n", 1221 | "dtype: int64" 1222 | ] 1223 | }, 1224 | "execution_count": 84, 1225 | "metadata": {}, 1226 | "output_type": "execute_result" 1227 | } 1228 | ], 1229 | "source": [ 1230 | "b" 1231 | ] 1232 | }, 1233 | { 1234 | "cell_type": "code", 1235 | "execution_count": 85, 1236 | "metadata": {}, 1237 | "outputs": [ 1238 | { 1239 | "data": { 1240 | "text/plain": [ 1241 | "2 30\n", 1242 | "dtype: int64" 1243 | ] 1244 | }, 1245 | "execution_count": 85, 1246 | "metadata": {}, 1247 | "output_type": "execute_result" 1248 | } 1249 | ], 1250 | "source": [ 1251 | "b.nlargest(1)" 1252 | ] 1253 | }, 1254 | { 1255 | "cell_type": "code", 1256 | "execution_count": 71, 1257 | "metadata": {}, 1258 | "outputs": [ 1259 | { 1260 | "data": { 1261 | "text/plain": [ 1262 | "2 30\n", 1263 | "4 30\n", 1264 | "dtype: int64" 1265 | ] 1266 | }, 1267 | "execution_count": 71, 1268 | "metadata": {}, 1269 | "output_type": "execute_result" 1270 | } 1271 | ], 1272 | "source": [ 1273 | "a.nlargest(2)" 1274 | ] 1275 | }, 1276 | { 1277 | "cell_type": "code", 1278 | "execution_count": 77, 1279 | "metadata": {}, 1280 | "outputs": [], 1281 | "source": [ 1282 | "a.drop_duplicates(inplace = True)" 1283 | ] 1284 | }, 1285 | { 1286 | "cell_type": "code", 1287 | "execution_count": 78, 1288 | "metadata": {}, 1289 | "outputs": [ 1290 | { 1291 | "data": { 1292 | "text/plain": [ 1293 | "2 30\n", 1294 | "0 10\n", 1295 | "6 6\n", 1296 | "dtype: int64" 1297 | ] 1298 | }, 1299 | "execution_count": 78, 1300 | "metadata": {}, 1301 | "output_type": "execute_result" 1302 | } 1303 | ], 1304 | "source": [ 1305 | "a.nlargest(3)" 1306 | ] 1307 | }, 1308 | { 1309 | "cell_type": "code", 1310 | "execution_count": 86, 1311 | "metadata": {}, 1312 | "outputs": [ 1313 | { 1314 | "data": { 1315 | "text/plain": [ 1316 | "10" 1317 | ] 1318 | }, 1319 | "execution_count": 86, 1320 | "metadata": {}, 1321 | "output_type": "execute_result" 1322 | } 1323 | ], 1324 | "source": [ 1325 | "secondlargestnumber(b)" 1326 | ] 1327 | }, 1328 | { 1329 | "cell_type": "code", 1330 | "execution_count": null, 1331 | "metadata": {}, 1332 | "outputs": [], 1333 | "source": [] 1334 | }, 1335 | { 1336 | "cell_type": "code", 1337 | "execution_count": null, 1338 | "metadata": {}, 1339 | "outputs": [], 1340 | "source": [] 1341 | }, 1342 | { 1343 | "cell_type": "code", 1344 | "execution_count": null, 1345 | "metadata": {}, 1346 | "outputs": [], 1347 | "source": [] 1348 | }, 1349 | { 1350 | "cell_type": "code", 1351 | "execution_count": null, 1352 | "metadata": {}, 1353 | "outputs": [], 1354 | "source": [] 1355 | }, 1356 | { 1357 | "cell_type": "code", 1358 | "execution_count": null, 1359 | "metadata": {}, 1360 | "outputs": [], 1361 | "source": [] 1362 | } 1363 | ], 1364 | "metadata": { 1365 | "kernelspec": { 1366 | "display_name": "Python 3", 1367 | "language": "python", 1368 | "name": "python3" 1369 | }, 1370 | "language_info": { 1371 | "codemirror_mode": { 1372 | "name": "ipython", 1373 | "version": 3 1374 | }, 1375 | "file_extension": ".py", 1376 | "mimetype": "text/x-python", 1377 | "name": "python", 1378 | "nbconvert_exporter": "python", 1379 | "pygments_lexer": "ipython3", 1380 | "version": "3.7.6" 1381 | } 1382 | }, 1383 | "nbformat": 4, 1384 | "nbformat_minor": 4 1385 | } 1386 | --------------------------------------------------------------------------------