├── .idea ├── .gitignore ├── Python.iml ├── inspectionProfiles │ └── profiles_settings.xml ├── misc.xml ├── modules.xml └── vcs.xml ├── Bootcamp ├── 1.0-Python Object and Data Structure Basics.ipynb ├── 1.1-Objects and Data Structures summup.ipynb ├── 1.2 Built-Ins on DS.ipynb ├── 10.1 Regular Expressions.ipynb ├── 11.1 Web Scrapping 1.ipynb ├── 11.2 Web Scrapping 2.ipynb ├── 11.3 Web Scrapping 3-image.ipynb ├── 11.4 Web Scrapping 4.ipynb ├── 12.1 Sending email from Python script.ipynb ├── 12.2 Email part 2.ipynb ├── 13.1 Images in python.ipynb ├── 14.1. Working with CSV Files.ipynb ├── 2.0-Python control flow Statements.ipynb ├── 2.1-Python control flow Statements.ipynb ├── 2.2-control flow Statements sumup.ipynb ├── 2.3-Guessing Game Challenge - Solution.ipynb ├── 3.0-Methods and Functions.ipynb ├── 3.1-Carnival guessing game- Three cup Monte.ipynb ├── 3.3 args and kwargs.ipynb ├── 3.4 Lambda exp- Map and Filter.ipynb ├── 3.5 Function Practice Exercises.ipynb ├── 3.6 Functions and Methods Homework.ipynb ├── 4.0 Object Oriented Programming.ipynb ├── 4.1 OOP - Inheritance and Polymorphism.ipynb ├── 4.2 Object Oriented Programming Homework.ipynb ├── 4.3 OOP Challenge.ipynb ├── 5.0 Exception handling- Try and Except.ipynb ├── 5.1. Errors and Exceptions Homework.ipynb ├── 5.2 Unit Testing.ipynb ├── 6.0 Python Decorator.ipynb ├── 7.0 Python Generators.ipynb ├── 7.1 Iterators and Generators Homework.ipynb ├── 8.1 Collection Module.ipynb ├── 8.2 Timing your code- TIME, TIMEIT module.ipynb ├── 9.1 Files in python.ipynb └── Extras.ipynb ├── Code Workspace ├── Anagram Check.ipynb ├── Array Pair Sum .ipynb ├── Balanced Parenthesis Check.ipynb ├── Balanced_array.ipynb ├── Count prime numbers.py ├── Factorial-recursive.py ├── Fibonnaci Sequence.py ├── Find the Missing Element .ipynb ├── Lucky number.ipynb ├── README.md ├── String Compression .ipynb └── Unique Characters in String.ipynb ├── PhotoGrid_1599194924746.jpg ├── PhotoGrid_1599195048543.jpg ├── PhotoGrid_1599195115056.jpg ├── README.md ├── Resources ├── Python notes.docx ├── Python resource links file.txt ├── PythonNotesForProfessionals.pdf ├── beginners_python_cheat_sheet_pcc_all.pdf └── winds of python.pdf ├── Screenshot_4.png └── demo.gif /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/Python.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Bootcamp/1.2 Built-Ins on DS.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Numbers" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "data": { 17 | "text/plain": [ 18 | "'0xc'" 19 | ] 20 | }, 21 | "execution_count": 1, 22 | "metadata": {}, 23 | "output_type": "execute_result" 24 | } 25 | ], 26 | "source": [ 27 | "#Using the function hex() you can convert numbers into a hexadecimal format:\n", 28 | "hex(12)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 2, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "data": { 38 | "text/plain": [ 39 | "'0x200'" 40 | ] 41 | }, 42 | "execution_count": 2, 43 | "metadata": {}, 44 | "output_type": "execute_result" 45 | } 46 | ], 47 | "source": [ 48 | "hex(512)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 3, 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "data": { 58 | "text/plain": [ 59 | "'0b1100'" 60 | ] 61 | }, 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "output_type": "execute_result" 65 | } 66 | ], 67 | "source": [ 68 | "#Using the function bin() you can convert numbers into their binary format.\n", 69 | "bin(12)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 8, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "data": { 79 | "text/plain": [ 80 | "'0b1000000000'" 81 | ] 82 | }, 83 | "execution_count": 8, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "bin(512)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "## Advanced Strings" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [] 105 | } 106 | ], 107 | "metadata": { 108 | "kernelspec": { 109 | "display_name": "Python 3", 110 | "language": "python", 111 | "name": "python3" 112 | }, 113 | "language_info": { 114 | "codemirror_mode": { 115 | "name": "ipython", 116 | "version": 3 117 | }, 118 | "file_extension": ".py", 119 | "mimetype": "text/x-python", 120 | "name": "python", 121 | "nbconvert_exporter": "python", 122 | "pygments_lexer": "ipython3", 123 | "version": "3.7.6" 124 | } 125 | }, 126 | "nbformat": 4, 127 | "nbformat_minor": 4 128 | } 129 | -------------------------------------------------------------------------------- /Bootcamp/10.1 Regular Expressions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Regular Expressions(RegEx)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 3, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "True\n" 20 | ] 21 | } 22 | ], 23 | "source": [ 24 | "#genral case:\n", 25 | "text= 'my name is bob'\n", 26 | "\n", 27 | "print('bob' in text)" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 6, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "#with re module\n", 37 | "import re\n", 38 | "\n", 39 | "text=\"hello, your agent's num is 444-5544-5050. call his num soon!\"\n", 40 | "pattern = 'your'" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "### search( ) -- case when pattern is found -- we are storing the result in varibale k1" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 9, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "\n" 60 | ] 61 | } 62 | ], 63 | "source": [ 64 | "#search will return first occourance of pattern\n", 65 | "k1 = re.search(pattern,text)\n", 66 | "print(k1) #returns span too" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 10, 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "None\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "#case when pattern is not found\n", 84 | "pattern='cat'\n", 85 | "k2 = re.search(pattern,text) #returns None\n", 86 | "print(k2)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "### span( ) start( ) end( )" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 13, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "(7, 11)\n", 106 | "7\n", 107 | "11\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "print(k1.span())\n", 113 | "print(k1.start())\n", 114 | "print(k1.end())" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "### findall( ) --> to find all occourance of patterns" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 15, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "pattern = 'num'\n", 139 | "k2 = re.search(pattern,text)\n", 140 | "print(k2)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 17, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "name": "stdout", 150 | "output_type": "stream", 151 | "text": [ 152 | "['num', 'num']\n" 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "k2 = re.findall(pattern, text)\n", 158 | "print(k2)" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 19, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "name": "stdout", 168 | "output_type": "stream", 169 | "text": [ 170 | "\n", 171 | "2\n" 172 | ] 173 | } 174 | ], 175 | "source": [ 176 | "print(type(k2))\n", 177 | "print(len(k2))" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 20, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "\n", 190 | "\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "#using for loop\n", 196 | "for i in re.finditer(pattern,text):\n", 197 | " print(i)" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 21, 203 | "metadata": {}, 204 | "outputs": [ 205 | { 206 | "name": "stdout", 207 | "output_type": "stream", 208 | "text": [ 209 | "(20, 23)\n", 210 | "(51, 54)\n" 211 | ] 212 | } 213 | ], 214 | "source": [ 215 | "for i in re.finditer(pattern,text):\n", 216 | " print(i.span())" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 22, 222 | "metadata": {}, 223 | "outputs": [ 224 | { 225 | "name": "stdout", 226 | "output_type": "stream", 227 | "text": [ 228 | "num\n", 229 | "num\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "for i in re.finditer(pattern,text): #return pattern that matched\n", 235 | " print(i.group())" 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "metadata": {}, 241 | "source": [ 242 | "## Regex example" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "### chachacter identifier" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 27, 255 | "metadata": {}, 256 | "outputs": [ 257 | { 258 | "name": "stdout", 259 | "output_type": "stream", 260 | "text": [ 261 | "\n" 262 | ] 263 | } 264 | ], 265 | "source": [ 266 | "text=\"hello, your agent's num is 444-545-5050, call his num soon!\"\n", 267 | "k3 = re.search(r'\\d\\d\\d-\\d\\d\\d-\\d\\d\\d\\d',text)\n", 268 | "print(k3)" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 28, 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "data": { 278 | "text/plain": [ 279 | "'444-545-5050'" 280 | ] 281 | }, 282 | "execution_count": 28, 283 | "metadata": {}, 284 | "output_type": "execute_result" 285 | } 286 | ], 287 | "source": [ 288 | "k3.group()" 289 | ] 290 | }, 291 | { 292 | "cell_type": "markdown", 293 | "metadata": {}, 294 | "source": [ 295 | "### Quantifier" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": 29, 301 | "metadata": {}, 302 | "outputs": [ 303 | { 304 | "name": "stdout", 305 | "output_type": "stream", 306 | "text": [ 307 | "\n" 308 | ] 309 | } 310 | ], 311 | "source": [ 312 | "k4 = re.search(r'\\d{3}-\\d{3}-\\d{4}',text)\n", 313 | "print(k4)" 314 | ] 315 | }, 316 | { 317 | "cell_type": "markdown", 318 | "metadata": {}, 319 | "source": [ 320 | "### Compile\n", 321 | "\n", 322 | "here we creating 3 groups. we can fetch each group indivisually. And grouping starts at 1 unlike python starts at 0" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 33, 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "name": "stdout", 332 | "output_type": "stream", 333 | "text": [ 334 | "\n" 335 | ] 336 | } 337 | ], 338 | "source": [ 339 | "result = re.compile(r'(\\d{3})-(\\d{3})-(\\d{4})')\n", 340 | "k4 = re.search(result,text)\n", 341 | "print(k4)" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": 37, 347 | "metadata": {}, 348 | "outputs": [ 349 | { 350 | "name": "stdout", 351 | "output_type": "stream", 352 | "text": [ 353 | "444-545-5050\n", 354 | "444\n", 355 | "545\n" 356 | ] 357 | } 358 | ], 359 | "source": [ 360 | "print(k4.group())\n", 361 | "print(k4.group(1))\n", 362 | "print(k4.group(2))" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": null, 368 | "metadata": {}, 369 | "outputs": [], 370 | "source": [] 371 | } 372 | ], 373 | "metadata": { 374 | "kernelspec": { 375 | "display_name": "Python 3", 376 | "language": "python", 377 | "name": "python3" 378 | }, 379 | "language_info": { 380 | "codemirror_mode": { 381 | "name": "ipython", 382 | "version": 3 383 | }, 384 | "file_extension": ".py", 385 | "mimetype": "text/x-python", 386 | "name": "python", 387 | "nbconvert_exporter": "python", 388 | "pygments_lexer": "ipython3", 389 | "version": "3.7.6" 390 | } 391 | }, 392 | "nbformat": 4, 393 | "nbformat_minor": 4 394 | } 395 | -------------------------------------------------------------------------------- /Bootcamp/11.1 Web Scrapping 1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Web Scrapping" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "import requests" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "result = requests.get('http://www.example.com')" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 3, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "data": { 35 | "text/plain": [ 36 | "b'\\n\\n\\n Example Domain\\n\\n \\n \\n \\n \\n\\n\\n\\n
\\n

Example Domain

\\n

This domain is for use in illustrative examples in documents. You may use this\\n domain in literature without prior coordination or asking for permission.

\\n

More information...

\\n
\\n\\n\\n'" 37 | ] 38 | }, 39 | "execution_count": 3, 40 | "metadata": {}, 41 | "output_type": "execute_result" 42 | } 43 | ], 44 | "source": [ 45 | "result.content" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 3, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "data": { 55 | "text/plain": [ 56 | "'\\n\\n\\n Example Domain\\n\\n \\n \\n \\n \\n\\n\\n\\n
\\n

Example Domain

\\n

This domain is for use in illustrative examples in documents. You may use this\\n domain in literature without prior coordination or asking for permission.

\\n

More information...

\\n
\\n\\n\\n'" 57 | ] 58 | }, 59 | "execution_count": 3, 60 | "metadata": {}, 61 | "output_type": "execute_result" 62 | } 63 | ], 64 | "source": [ 65 | "result.text" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 4, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "import bs4" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 5, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "soup = bs4.BeautifulSoup(result.text,'lxml')" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 6, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "\n", 95 | "\n", 96 | "\n", 97 | "Example Domain\n", 98 | "\n", 99 | "\n", 100 | "\n", 101 | "\n", 128 | "\n", 129 | "\n", 130 | "
\n", 131 | "

Example Domain

\n", 132 | "

This domain is for use in illustrative examples in documents. You may use this\n", 133 | " domain in literature without prior coordination or asking for permission.

\n", 134 | "

More information...

\n", 135 | "
\n", 136 | "\n", 137 | "" 138 | ] 139 | }, 140 | "execution_count": 6, 141 | "metadata": {}, 142 | "output_type": "execute_result" 143 | } 144 | ], 145 | "source": [ 146 | "soup" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 7, 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "data": { 156 | "text/plain": [ 157 | "[Example Domain]" 158 | ] 159 | }, 160 | "execution_count": 7, 161 | "metadata": {}, 162 | "output_type": "execute_result" 163 | } 164 | ], 165 | "source": [ 166 | "soup.select('title')" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 8, 172 | "metadata": {}, 173 | "outputs": [], 174 | "source": [ 175 | "#by default it is a list because there can be multiple tags" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 9, 181 | "metadata": {}, 182 | "outputs": [ 183 | { 184 | "data": { 185 | "text/plain": [ 186 | "[

This domain is for use in illustrative examples in documents. You may use this\n", 187 | " domain in literature without prior coordination or asking for permission.

,\n", 188 | "

More information...

]" 189 | ] 190 | }, 191 | "execution_count": 9, 192 | "metadata": {}, 193 | "output_type": "execute_result" 194 | } 195 | ], 196 | "source": [ 197 | "soup.select('p')" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 10, 203 | "metadata": {}, 204 | "outputs": [ 205 | { 206 | "data": { 207 | "text/plain": [ 208 | "[

Example Domain

]" 209 | ] 210 | }, 211 | "execution_count": 10, 212 | "metadata": {}, 213 | "output_type": "execute_result" 214 | } 215 | ], 216 | "source": [ 217 | "soup.select('h1')" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 11, 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "data": { 227 | "text/plain": [ 228 | "Example Domain" 229 | ] 230 | }, 231 | "execution_count": 11, 232 | "metadata": {}, 233 | "output_type": "execute_result" 234 | } 235 | ], 236 | "source": [ 237 | "#we can grap 1st item of the list\n", 238 | "soup.select('title')[0]" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 12, 244 | "metadata": {}, 245 | "outputs": [ 246 | { 247 | "data": { 248 | "text/plain": [ 249 | "'Example Domain'" 250 | ] 251 | }, 252 | "execution_count": 12, 253 | "metadata": {}, 254 | "output_type": "execute_result" 255 | } 256 | ], 257 | "source": [ 258 | "#getText method return the content between the tag\n", 259 | "soup.select('title')[0].getText()" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 13, 265 | "metadata": {}, 266 | "outputs": [], 267 | "source": [ 268 | "para = soup.select('p')" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 14, 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "data": { 278 | "text/plain": [ 279 | "'This domain is for use in illustrative examples in documents. You may use this\\n domain in literature without prior coordination or asking for permission.'" 280 | ] 281 | }, 282 | "execution_count": 14, 283 | "metadata": {}, 284 | "output_type": "execute_result" 285 | } 286 | ], 287 | "source": [ 288 | "para[0].getText()" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": null, 294 | "metadata": {}, 295 | "outputs": [], 296 | "source": [] 297 | } 298 | ], 299 | "metadata": { 300 | "kernelspec": { 301 | "display_name": "Python 3", 302 | "language": "python", 303 | "name": "python3" 304 | }, 305 | "language_info": { 306 | "codemirror_mode": { 307 | "name": "ipython", 308 | "version": 3 309 | }, 310 | "file_extension": ".py", 311 | "mimetype": "text/x-python", 312 | "name": "python", 313 | "nbconvert_exporter": "python", 314 | "pygments_lexer": "ipython3", 315 | "version": "3.7.6" 316 | } 317 | }, 318 | "nbformat": 4, 319 | "nbformat_minor": 4 320 | } 321 | -------------------------------------------------------------------------------- /Bootcamp/11.2 Web Scrapping 2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import requests" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 3, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "result = requests.get('https://en.wikipedia.org/wiki/Grace_Hopper')" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 4, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "import bs4" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 5, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "soup = bs4.BeautifulSoup(result.text,'lxml')" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 6, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "#soup" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 7, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "k=soup.select('.toctext') #.toctext is the class name attached with table of contents" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 8, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "'Early life and education'" 66 | ] 67 | }, 68 | "execution_count": 8, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "k[0].text" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 9, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "Early life and education\n", 87 | "Career\n", 88 | "World War II\n", 89 | "UNIVAC\n", 90 | "COBOL\n", 91 | "Standards\n", 92 | "Retirement\n", 93 | "Post-retirement\n", 94 | "Anecdotes\n", 95 | "Death\n", 96 | "Dates of rank\n", 97 | "Awards and honors\n", 98 | "Military awards\n", 99 | "Other awards\n", 100 | "Legacy\n", 101 | "Places\n", 102 | "Programs\n", 103 | "In popular culture\n", 104 | "Grace Hopper Celebration of Women in Computing\n", 105 | "Notes\n", 106 | "Obituary notices\n", 107 | "See also\n", 108 | "References\n", 109 | "Further reading\n", 110 | "External links\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "for i in soup.select('.toctext'):\n", 116 | " print(i.text)" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [] 125 | } 126 | ], 127 | "metadata": { 128 | "kernelspec": { 129 | "display_name": "Python 3", 130 | "language": "python", 131 | "name": "python3" 132 | }, 133 | "language_info": { 134 | "codemirror_mode": { 135 | "name": "ipython", 136 | "version": 3 137 | }, 138 | "file_extension": ".py", 139 | "mimetype": "text/x-python", 140 | "name": "python", 141 | "nbconvert_exporter": "python", 142 | "pygments_lexer": "ipython3", 143 | "version": "3.7.6" 144 | } 145 | }, 146 | "nbformat": 4, 147 | "nbformat_minor": 4 148 | } 149 | -------------------------------------------------------------------------------- /Bootcamp/11.3 Web Scrapping 3-image.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import requests" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 3, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "result = requests.get('https://en.wikipedia.org/wiki/Deep_Blue_(chess_computer)')" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 4, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "import bs4\n", 28 | "#result.text" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 5, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "soup = bs4.BeautifulSoup(result.text,'lxml')" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 6, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "#soup" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 7, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "[\"Chess,\n", 58 | " \"\",\n", 59 | " \"\",\n", 60 | " \"Nuvola,\n", 61 | " \"IBM,\n", 62 | " \"Edit,\n", 63 | " \"\",\n", 64 | " \"Wikimedia,\n", 65 | " \"Powered]" 66 | ] 67 | }, 68 | "execution_count": 7, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "soup.select('img')" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 8, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "data": { 84 | "text/plain": [ 85 | "[\"\",\n", 86 | " \"\"]" 87 | ] 88 | }, 89 | "execution_count": 8, 90 | "metadata": {}, 91 | "output_type": "execute_result" 92 | } 93 | ], 94 | "source": [ 95 | "soup.select('.thumbimage')\n", 96 | "#.thumbimage is css class for main images inside the body." 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": {}, 102 | "source": [ 103 | "## get the image class to access image from website" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 10, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "\"\"" 115 | ] 116 | }, 117 | "execution_count": 10, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "computer = soup.select('.thumbimage')[0]\n", 124 | "computer \n", 125 | "#we can now treat computer a dictionary like object. like computer[class], computer[width], computer[src]" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 12, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "'//upload.wikimedia.org/wikipedia/commons/thumb/b/be/Deep_Blue.jpg/220px-Deep_Blue.jpg'" 137 | ] 138 | }, 139 | "execution_count": 12, 140 | "metadata": {}, 141 | "output_type": "execute_result" 142 | } 143 | ], 144 | "source": [ 145 | "computer['src']" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 13, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "#now we can see image in juyptr notebook because markdown cells have img tag which supports images\n", 155 | "\n", 156 | "#run this cell in markdown\n", 157 | "" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "## running image command" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "### save this image on computer" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 18, 184 | "metadata": {}, 185 | "outputs": [], 186 | "source": [ 187 | "image_link = requests.get('https://upload.wikimedia.org/wikipedia/commons/thumb/b/be/Deep_Blue.jpg/220px-Deep_Blue.jpg')" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 21, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [ 196 | "#image_link.content" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 23, 202 | "metadata": {}, 203 | "outputs": [ 204 | { 205 | "data": { 206 | "text/plain": [ 207 | "16806" 208 | ] 209 | }, 210 | "execution_count": 23, 211 | "metadata": {}, 212 | "output_type": "execute_result" 213 | } 214 | ], 215 | "source": [ 216 | "f = open('hello.jpg', 'wb')\n", 217 | "f.write(image_link.content)" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 24, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [ 226 | "f.close()" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 25, 232 | "metadata": {}, 233 | "outputs": [], 234 | "source": [ 235 | "#opened file -- gave a name -- write binary mode -- write to file -- and close file" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": null, 241 | "metadata": {}, 242 | "outputs": [], 243 | "source": [] 244 | } 245 | ], 246 | "metadata": { 247 | "kernelspec": { 248 | "display_name": "Python 3", 249 | "language": "python", 250 | "name": "python3" 251 | }, 252 | "language_info": { 253 | "codemirror_mode": { 254 | "name": "ipython", 255 | "version": 3 256 | }, 257 | "file_extension": ".py", 258 | "mimetype": "text/x-python", 259 | "name": "python", 260 | "nbconvert_exporter": "python", 261 | "pygments_lexer": "ipython3", 262 | "version": "3.7.6" 263 | } 264 | }, 265 | "nbformat": 4, 266 | "nbformat_minor": 4 267 | } 268 | -------------------------------------------------------------------------------- /Bootcamp/11.4 Web Scrapping 4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "kernelspec": { 13 | "display_name": "Python 3", 14 | "language": "python", 15 | "name": "python3" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 3 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython3", 27 | "version": "3.7.6" 28 | } 29 | }, 30 | "nbformat": 4, 31 | "nbformat_minor": 4 32 | } 33 | -------------------------------------------------------------------------------- /Bootcamp/12.1 Sending email from Python script.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 9, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import smtplib" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 10, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "#create an obj -- set email and give port num\n", 19 | "smtp_obj = smtplib.SMTP('smtp.gmail.com',587)" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 11, 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "data": { 29 | "text/plain": [ 30 | "(250,\n", 31 | " b'smtp.gmail.com at your service, [122.168.197.139]\\nSIZE 35882577\\n8BITMIME\\nSTARTTLS\\nENHANCEDSTATUSCODES\\nPIPELINING\\nCHUNKING\\nSMTPUTF8')" 32 | ] 33 | }, 34 | "execution_count": 11, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "smtp_obj.ehlo()" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 12, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "(220, b'2.0.0 Ready to start TLS')" 52 | ] 53 | }, 54 | "execution_count": 12, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [ 60 | "smtp_obj.starttls()" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 6, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "enter password········\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "#time to setup email n password\n", 78 | "#if u need to hide password take user input - if someone is viewing ur screen then use getpass library\n", 79 | "\n", 80 | "import getpass\n", 81 | "password = getpass.getpass('enter password')" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 7, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "name": "stdout", 91 | "output_type": "stream", 92 | "text": [ 93 | "python\n" 94 | ] 95 | } 96 | ], 97 | "source": [ 98 | "print(password)\n", 99 | "krykjckxmvfwvyjq" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 13, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "enter email: ········\n", 112 | "enter password: ········\n" 113 | ] 114 | }, 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "(235, b'2.7.0 Accepted')" 119 | ] 120 | }, 121 | "execution_count": 13, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "email = getpass.getpass('enter email: ')\n", 128 | "password = getpass.getpass('enter password: ')\n", 129 | "smtp_obj.login(email,password)" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 15, 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | "Enter the subject line: test python \n", 142 | "Type out the message you want to send: this is a test mail to test send email from python\n" 143 | ] 144 | }, 145 | { 146 | "data": { 147 | "text/plain": [ 148 | "{}" 149 | ] 150 | }, 151 | "execution_count": 15, 152 | "metadata": {}, 153 | "output_type": "execute_result" 154 | } 155 | ], 156 | "source": [ 157 | "from_address = email\n", 158 | "to_address = email\n", 159 | "subject = input(\"Enter the subject line: \")\n", 160 | "message = input(\"Type out the message you want to send: \")\n", 161 | "\n", 162 | "msg = \"Subject: \" + subject + '\\n' + message\n", 163 | "\n", 164 | "smtp_obj.sendmail(from_address,to_address,msg)" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 17, 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "data": { 174 | "text/plain": [ 175 | "(221, b'2.0.0 closing connection u15sm20433114pje.42 - gsmtp')" 176 | ] 177 | }, 178 | "execution_count": 17, 179 | "metadata": {}, 180 | "output_type": "execute_result" 181 | } 182 | ], 183 | "source": [ 184 | "smtp_obj.quit()" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": null, 190 | "metadata": {}, 191 | "outputs": [], 192 | "source": [] 193 | } 194 | ], 195 | "metadata": { 196 | "kernelspec": { 197 | "display_name": "Python 3", 198 | "language": "python", 199 | "name": "python3" 200 | }, 201 | "language_info": { 202 | "codemirror_mode": { 203 | "name": "ipython", 204 | "version": 3 205 | }, 206 | "file_extension": ".py", 207 | "mimetype": "text/x-python", 208 | "name": "python", 209 | "nbconvert_exporter": "python", 210 | "pygments_lexer": "ipython3", 211 | "version": "3.7.6" 212 | } 213 | }, 214 | "nbformat": 4, 215 | "nbformat_minor": 4 216 | } 217 | -------------------------------------------------------------------------------- /Bootcamp/12.2 Email part 2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import imaplib" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "M = imaplib.IMAP4_SSL('imap.gmail.com')" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "import getpass" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 4, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "enter password: ········\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "email = 'ayushi7rawat@gmail.com'\n", 45 | "password = getpass.getpass('enter password: ')\n" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 6, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "data": { 55 | "text/plain": [ 56 | "('OK', [b'ayushi7rawat@gmail.com authenticated (Success)'])" 57 | ] 58 | }, 59 | "execution_count": 6, 60 | "metadata": {}, 61 | "output_type": "execute_result" 62 | } 63 | ], 64 | "source": [ 65 | "M.login(email,password)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 7, 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "('OK',\n", 77 | " [b'(\\\\HasNoChildren) \"/\" \"INBOX\"',\n", 78 | " b'(\\\\HasNoChildren) \"/\" \"Notes\"',\n", 79 | " b'(\\\\HasChildren \\\\Noselect) \"/\" \"[Gmail]\"',\n", 80 | " b'(\\\\All \\\\HasNoChildren) \"/\" \"[Gmail]/All Mail\"',\n", 81 | " b'(\\\\Drafts \\\\HasNoChildren) \"/\" \"[Gmail]/Drafts\"',\n", 82 | " b'(\\\\HasNoChildren \\\\Important) \"/\" \"[Gmail]/Important\"',\n", 83 | " b'(\\\\HasNoChildren \\\\Sent) \"/\" \"[Gmail]/Sent Mail\"',\n", 84 | " b'(\\\\HasNoChildren \\\\Junk) \"/\" \"[Gmail]/Spam\"',\n", 85 | " b'(\\\\Flagged \\\\HasNoChildren) \"/\" \"[Gmail]/Starred\"',\n", 86 | " b'(\\\\HasNoChildren \\\\Trash) \"/\" \"[Gmail]/Trash\"'])" 87 | ] 88 | }, 89 | "execution_count": 7, 90 | "metadata": {}, 91 | "output_type": "execute_result" 92 | } 93 | ], 94 | "source": [ 95 | "M.list()" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 8, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "('OK', [b'58'])" 107 | ] 108 | }, 109 | "execution_count": 8, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "M.select('inbox')" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 9, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [ 124 | "#typ, data = M.search(None,SINCE 18-Aug-2020)\n", 125 | "typ, data = M.search(None,'SUBJECT \"test python\"')" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 10, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "'OK'" 137 | ] 138 | }, 139 | "execution_count": 10, 140 | "metadata": {}, 141 | "output_type": "execute_result" 142 | } 143 | ], 144 | "source": [ 145 | "typ" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 11, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "data": { 155 | "text/plain": [ 156 | "[b'57']" 157 | ] 158 | }, 159 | "execution_count": 11, 160 | "metadata": {}, 161 | "output_type": "execute_result" 162 | } 163 | ], 164 | "source": [ 165 | "data" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 12, 171 | "metadata": {}, 172 | "outputs": [], 173 | "source": [ 174 | "email_id = data[0]" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 13, 180 | "metadata": {}, 181 | "outputs": [], 182 | "source": [ 183 | "result, email_data = M.fetch(email_id, '(RFC822)')" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 14, 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "data": { 193 | "text/plain": [ 194 | "[(b'57 (RFC822 {576}',\n", 195 | " b'Bcc: ayushi7rawat@gmail.com\\r\\nReturn-Path: \\r\\nReceived: from AyushiRawat.domain.name ([122.168.197.139])\\r\\n by smtp.gmail.com with ESMTPSA id u15sm20433114pje.42.2020.08.17.20.24.39\\r\\n for \\r\\n (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\\r\\n Mon, 17 Aug 2020 20:24:40 -0700 (PDT)\\r\\nMessage-ID: <5f3b49f8.1c69fb81.4744c.df66@mx.google.com>\\r\\nDate: Mon, 17 Aug 2020 20:24:40 -0700 (PDT)\\r\\nFrom: ayushi7rawat@gmail.com\\r\\nSubject: test python \\r\\n\\r\\nthis is a test mail to test send email from python\\r\\n'),\n", 196 | " b')']" 197 | ] 198 | }, 199 | "execution_count": 14, 200 | "metadata": {}, 201 | "output_type": "execute_result" 202 | } 203 | ], 204 | "source": [ 205 | "email_data" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 16, 211 | "metadata": {}, 212 | "outputs": [], 213 | "source": [ 214 | "raw_email = email_data[0][1]" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 17, 220 | "metadata": {}, 221 | "outputs": [], 222 | "source": [ 223 | "raw_email_string = raw_email.decode('utf-8')" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 18, 229 | "metadata": {}, 230 | "outputs": [], 231 | "source": [ 232 | "import email" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 19, 238 | "metadata": {}, 239 | "outputs": [], 240 | "source": [ 241 | "email_message = email.message_from_string(raw_email_string)" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 20, 247 | "metadata": {}, 248 | "outputs": [ 249 | { 250 | "name": "stdout", 251 | "output_type": "stream", 252 | "text": [ 253 | "b'this is a test mail to test send email from python\\r\\n'\n" 254 | ] 255 | } 256 | ], 257 | "source": [ 258 | "for part in email_message.walk():\n", 259 | " if part.get_content_type() == \"text/plain\":\n", 260 | " body = part.get_payload(decode=True)\n", 261 | " print(body)" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": null, 267 | "metadata": {}, 268 | "outputs": [], 269 | "source": [] 270 | } 271 | ], 272 | "metadata": { 273 | "kernelspec": { 274 | "display_name": "Python 3", 275 | "language": "python", 276 | "name": "python3" 277 | }, 278 | "language_info": { 279 | "codemirror_mode": { 280 | "name": "ipython", 281 | "version": 3 282 | }, 283 | "file_extension": ".py", 284 | "mimetype": "text/x-python", 285 | "name": "python", 286 | "nbconvert_exporter": "python", 287 | "pygments_lexer": "ipython3", 288 | "version": "3.7.6" 289 | } 290 | }, 291 | "nbformat": 4, 292 | "nbformat_minor": 4 293 | } 294 | -------------------------------------------------------------------------------- /Bootcamp/13.1 Images in python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "#we will use pillow library here\n", 10 | "from PIL import Image" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 7, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "i = Image.open('example.jpg')" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 14, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "#i.show()\n", 29 | "#i" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 9, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "PIL.JpegImagePlugin.JpegImageFile" 41 | ] 42 | }, 43 | "execution_count": 9, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "type(i)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 11, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "(1993, 1257)" 61 | ] 62 | }, 63 | "execution_count": 11, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "source": [ 69 | "i.size" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 12, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "data": { 79 | "text/plain": [ 80 | "'example.jpg'" 81 | ] 82 | }, 83 | "execution_count": 12, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "i.filename" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 13, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "data": { 99 | "text/plain": [ 100 | "'JPEG (ISO 10918)'" 101 | ] 102 | }, 103 | "execution_count": 13, 104 | "metadata": {}, 105 | "output_type": "execute_result" 106 | } 107 | ], 108 | "source": [ 109 | "i.format_description" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [] 118 | } 119 | ], 120 | "metadata": { 121 | "kernelspec": { 122 | "display_name": "Python 3", 123 | "language": "python", 124 | "name": "python3" 125 | }, 126 | "language_info": { 127 | "codemirror_mode": { 128 | "name": "ipython", 129 | "version": 3 130 | }, 131 | "file_extension": ".py", 132 | "mimetype": "text/x-python", 133 | "name": "python", 134 | "nbconvert_exporter": "python", 135 | "pygments_lexer": "ipython3", 136 | "version": "3.7.6" 137 | } 138 | }, 139 | "nbformat": 4, 140 | "nbformat_minor": 4 141 | } 142 | -------------------------------------------------------------------------------- /Bootcamp/2.1-Python control flow Statements.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "At index 0 the letter is a\n", 13 | "At index 1 the letter is b\n", 14 | "At index 2 the letter is c\n", 15 | "At index 3 the letter is d\n", 16 | "At index 4 the letter is e\n" 17 | ] 18 | } 19 | ], 20 | "source": [ 21 | "index_count = 0\n", 22 | "\n", 23 | "for letter in 'abcde':\n", 24 | " print(f\"At index {index_count} the letter is {letter}\")\n", 25 | " index_count += 1" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 1, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "At index 0 the letter is a\n", 38 | "At index 1 the letter is b\n", 39 | "At index 2 the letter is c\n", 40 | "At index 3 the letter is d\n", 41 | "At index 4 the letter is e\n" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "\n", 47 | "for index_count,letter in enumerate('abcde'):\n", 48 | " print(f\"At index {index_count} the letter is {letter}\")" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 8, 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "data": { 58 | "text/plain": [ 59 | "[(1, 'a'), (2, 'b')]" 60 | ] 61 | }, 62 | "execution_count": 8, 63 | "metadata": {}, 64 | "output_type": "execute_result" 65 | } 66 | ], 67 | "source": [ 68 | "mylist1 = [1,2]\n", 69 | "mylist2 = ['a','b','c']\n", 70 | "list(zip(mylist1,mylist2))" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 9, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [ 79 | "from random import shuffle" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 21, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "l1=[1,2,5,3,2,4,2,3]\n", 89 | "shuffle(l1)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 22, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "data": { 99 | "text/plain": [ 100 | "[2, 3, 5, 2, 4, 3, 1, 2]" 101 | ] 102 | }, 103 | "execution_count": 22, 104 | "metadata": {}, 105 | "output_type": "execute_result" 106 | } 107 | ], 108 | "source": [ 109 | "l1" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 23, 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "data": { 119 | "text/plain": [ 120 | "[1, 2, 2, 2, 3, 3, 4, 5]" 121 | ] 122 | }, 123 | "execution_count": 23, 124 | "metadata": {}, 125 | "output_type": "execute_result" 126 | } 127 | ], 128 | "source": [ 129 | "sorted(l1)" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 24, 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "data": { 139 | "text/plain": [ 140 | "{1, 2, 3, 4, 5}" 141 | ] 142 | }, 143 | "execution_count": 24, 144 | "metadata": {}, 145 | "output_type": "execute_result" 146 | } 147 | ], 148 | "source": [ 149 | "set(l1)" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 25, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [ 158 | "from random import randint" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 28, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "data": { 168 | "text/plain": [ 169 | "32" 170 | ] 171 | }, 172 | "execution_count": 28, 173 | "metadata": {}, 174 | "output_type": "execute_result" 175 | } 176 | ], 177 | "source": [ 178 | "randint(0,100)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 29, 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "Enter Something into this box: hi\n" 191 | ] 192 | }, 193 | { 194 | "data": { 195 | "text/plain": [ 196 | "'hi'" 197 | ] 198 | }, 199 | "execution_count": 29, 200 | "metadata": {}, 201 | "output_type": "execute_result" 202 | } 203 | ], 204 | "source": [ 205 | "input('Enter Something into this box: ')" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 30, 211 | "metadata": {}, 212 | "outputs": [ 213 | { 214 | "name": "stdout", 215 | "output_type": "stream", 216 | "text": [ 217 | "0\n", 218 | "1\n", 219 | "4\n", 220 | "9\n", 221 | "16\n", 222 | "25\n", 223 | "36\n", 224 | "49\n", 225 | "64\n", 226 | "81\n", 227 | "100\n" 228 | ] 229 | } 230 | ], 231 | "source": [ 232 | "for i in range(0,11):\n", 233 | " print(i**2)" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 11, 239 | "metadata": {}, 240 | "outputs": [], 241 | "source": [ 242 | "l1=[i**2 for i in range(0,11) if i%3==0]" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": 12, 248 | "metadata": {}, 249 | "outputs": [ 250 | { 251 | "data": { 252 | "text/plain": [ 253 | "[0, 9, 36, 81]" 254 | ] 255 | }, 256 | "execution_count": 12, 257 | "metadata": {}, 258 | "output_type": "execute_result" 259 | } 260 | ], 261 | "source": [ 262 | "l1" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": 42, 268 | "metadata": {}, 269 | "outputs": [ 270 | { 271 | "name": "stdout", 272 | "output_type": "stream", 273 | "text": [ 274 | "10\n", 275 | "12\n", 276 | "14\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "for i in range(0,6):\n", 282 | " for j in range(10,11):\n", 283 | " if i%2==0:\n", 284 | " print(i+j)" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 8, 290 | "metadata": {}, 291 | "outputs": [ 292 | { 293 | "name": "stdout", 294 | "output_type": "stream", 295 | "text": [ 296 | "1234\n" 297 | ] 298 | } 299 | ], 300 | "source": [ 301 | "print(*range(1,5),sep=\"\")" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "metadata": {}, 308 | "outputs": [], 309 | "source": [] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": null, 314 | "metadata": {}, 315 | "outputs": [], 316 | "source": [] 317 | } 318 | ], 319 | "metadata": { 320 | "kernelspec": { 321 | "display_name": "Python 3", 322 | "language": "python", 323 | "name": "python3" 324 | }, 325 | "language_info": { 326 | "codemirror_mode": { 327 | "name": "ipython", 328 | "version": 3 329 | }, 330 | "file_extension": ".py", 331 | "mimetype": "text/x-python", 332 | "name": "python", 333 | "nbconvert_exporter": "python", 334 | "pygments_lexer": "ipython3", 335 | "version": "3.7.6" 336 | } 337 | }, 338 | "nbformat": 4, 339 | "nbformat_minor": 4 340 | } 341 | -------------------------------------------------------------------------------- /Bootcamp/2.2-control flow Statements sumup.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# Statements Assessment Test\n", 10 | "Let's test your knowledge!" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "_____\n", 18 | "**Use for, .split(), and if to create a Statement that will print out words that start with 's':**" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 40, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "st = 'Print only the words that start with s in this sentence'" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 42, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "start\n", 40 | "s\n", 41 | "sentence\n" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "#Code here\n", 47 | "for i in st.split():\n", 48 | " if i[0]== 's':\n", 49 | " print(i)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "______\n", 57 | "**Use range() to print all the even numbers from 0 to 10.**" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 43, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "0\n", 70 | "2\n", 71 | "4\n", 72 | "6\n", 73 | "8\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "#Code Here\n", 79 | "for i in range(0,10,2):\n", 80 | " print(i)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "___\n", 88 | "**Use a List Comprehension to create a list of all numbers between 1 and 50 that are divisible by 3.**" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 7, 94 | "metadata": {}, 95 | "outputs": [ 96 | { 97 | "data": { 98 | "text/plain": [ 99 | "[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]" 100 | ] 101 | }, 102 | "execution_count": 7, 103 | "metadata": {}, 104 | "output_type": "execute_result" 105 | } 106 | ], 107 | "source": [ 108 | "#Code in this cell\n", 109 | "l1=[i for i in range(0,50) if i%3==0]\n", 110 | "l1" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "_____\n", 118 | "**Go through the string below and if the length of a word is even print \"even!\"**" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 12, 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "st = 'Print every word in this sentence that has an even number of letters'" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 20, 133 | "metadata": {}, 134 | "outputs": [ 135 | { 136 | "name": "stdout", 137 | "output_type": "stream", 138 | "text": [ 139 | "word\n", 140 | "in\n", 141 | "this\n", 142 | "sentence\n", 143 | "that\n", 144 | "an\n", 145 | "even\n", 146 | "number\n", 147 | "of\n" 148 | ] 149 | } 150 | ], 151 | "source": [ 152 | "#Code in this cell\n", 153 | "l1=st.split()\n", 154 | "\n", 155 | "for i in l1:\n", 156 | " if len(i)%2==0:\n", 157 | " print(i)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "____\n", 165 | "**Write a program that prints the integers from 1 to 100. But for multiples of three print \"Fizz\" instead of the number, and for the multiples of five print \"Buzz\". For numbers which are multiples of both three and five print \"FizzBuzz\".**" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 33, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "1\n", 178 | "2\n", 179 | "Fizz\n", 180 | "4\n", 181 | "Buzz\n", 182 | "Fizz\n", 183 | "7\n", 184 | "8\n", 185 | "Fizz\n", 186 | "Buzz\n", 187 | "11\n", 188 | "Fizz\n", 189 | "13\n", 190 | "14\n", 191 | "FizzBuzz\n", 192 | "16\n", 193 | "17\n", 194 | "Fizz\n", 195 | "19\n", 196 | "Buzz\n", 197 | "Fizz\n", 198 | "22\n", 199 | "23\n", 200 | "Fizz\n", 201 | "Buzz\n", 202 | "26\n", 203 | "Fizz\n", 204 | "28\n", 205 | "29\n", 206 | "FizzBuzz\n", 207 | "31\n", 208 | "32\n", 209 | "Fizz\n", 210 | "34\n", 211 | "Buzz\n", 212 | "Fizz\n", 213 | "37\n", 214 | "38\n", 215 | "Fizz\n", 216 | "Buzz\n", 217 | "41\n", 218 | "Fizz\n", 219 | "43\n", 220 | "44\n", 221 | "FizzBuzz\n", 222 | "46\n", 223 | "47\n", 224 | "Fizz\n", 225 | "49\n", 226 | "Buzz\n", 227 | "Fizz\n", 228 | "52\n", 229 | "53\n", 230 | "Fizz\n", 231 | "Buzz\n", 232 | "56\n", 233 | "Fizz\n", 234 | "58\n", 235 | "59\n", 236 | "FizzBuzz\n", 237 | "61\n", 238 | "62\n", 239 | "Fizz\n", 240 | "64\n", 241 | "Buzz\n", 242 | "Fizz\n", 243 | "67\n", 244 | "68\n", 245 | "Fizz\n", 246 | "Buzz\n", 247 | "71\n", 248 | "Fizz\n", 249 | "73\n", 250 | "74\n", 251 | "FizzBuzz\n", 252 | "76\n", 253 | "77\n", 254 | "Fizz\n", 255 | "79\n", 256 | "Buzz\n", 257 | "Fizz\n", 258 | "82\n", 259 | "83\n", 260 | "Fizz\n", 261 | "Buzz\n", 262 | "86\n", 263 | "Fizz\n", 264 | "88\n", 265 | "89\n", 266 | "FizzBuzz\n", 267 | "91\n", 268 | "92\n", 269 | "Fizz\n", 270 | "94\n", 271 | "Buzz\n", 272 | "Fizz\n", 273 | "97\n", 274 | "98\n", 275 | "Fizz\n" 276 | ] 277 | } 278 | ], 279 | "source": [ 280 | "#Code in this cell\n", 281 | "\n", 282 | "for i in range(1,100):\n", 283 | " if i % 3 == 0:\n", 284 | " if i % 15 == 0:\n", 285 | " print('FizzBuzz')\n", 286 | " else:\n", 287 | " print('Fizz')\n", 288 | " elif i % 5 == 0:\n", 289 | " if i % 15 == 0:\n", 290 | " print('FizzBuzz')\n", 291 | " else:\n", 292 | " print('Buzz')\n", 293 | " else:\n", 294 | " print(i)" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "____\n", 302 | "**Use List Comprehension to create a list of the first letters of every word in the string below:**" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 34, 308 | "metadata": {}, 309 | "outputs": [], 310 | "source": [ 311 | "st = 'Create a list of the first letters of every word in this string'" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 39, 317 | "metadata": {}, 318 | "outputs": [ 319 | { 320 | "data": { 321 | "text/plain": [ 322 | "['C', 'a', 'l', 'o', 't', 'f', 'l', 'o', 'e', 'w', 'i', 't', 's']" 323 | ] 324 | }, 325 | "execution_count": 39, 326 | "metadata": {}, 327 | "output_type": "execute_result" 328 | } 329 | ], 330 | "source": [ 331 | "#Code in this cell\n", 332 | "l5=st.split()\n", 333 | "l5\n", 334 | "\n", 335 | "l6=[i[0] for i in l5]\n", 336 | "l6" 337 | ] 338 | }, 339 | { 340 | "cell_type": "markdown", 341 | "metadata": {}, 342 | "source": [ 343 | "### Great Job!" 344 | ] 345 | } 346 | ], 347 | "metadata": { 348 | "kernelspec": { 349 | "display_name": "Python 3", 350 | "language": "python", 351 | "name": "python3" 352 | }, 353 | "language_info": { 354 | "codemirror_mode": { 355 | "name": "ipython", 356 | "version": 3 357 | }, 358 | "file_extension": ".py", 359 | "mimetype": "text/x-python", 360 | "name": "python", 361 | "nbconvert_exporter": "python", 362 | "pygments_lexer": "ipython3", 363 | "version": "3.7.6" 364 | } 365 | }, 366 | "nbformat": 4, 367 | "nbformat_minor": 1 368 | } 369 | -------------------------------------------------------------------------------- /Bootcamp/2.3-Guessing Game Challenge - Solution.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Guessing Game Challenge\n", 8 | "\n", 9 | "Let's use `while` loops to create a guessing game.\n", 10 | "\n", 11 | "The Challenge:\n", 12 | "\n", 13 | "Write a program that picks a random integer from 1 to 100, and has players guess the number. The rules are:\n", 14 | "\n", 15 | "1. If a player's guess is less than 1 or greater than 100, say \"OUT OF BOUNDS\"\n", 16 | "2. On a player's first turn, if their guess is\n", 17 | " * within 10 of the number, return \"WARM!\"\n", 18 | " * further than 10 away from the number, return \"COLD!\"\n", 19 | "3. On all subsequent turns, if a guess is \n", 20 | " * closer to the number than the previous guess return \"WARMER!\"\n", 21 | " * farther from the number than the previous guess, return \"COLDER!\"\n", 22 | "4. When the player's guess equals the number, tell them they've guessed correctly *and* how many guesses it took!\n", 23 | "\n", 24 | "You can try this from scratch, or follow the steps outlined below. A separate Solution notebook has been provided. Good luck!\n" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "#### First, pick a random integer from 1 to 100 using the random module and assign it to a variable\n", 32 | "\n", 33 | "Note: `random.randint(a,b)` returns a random integer in range `[a, b]`, including both end points." 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "data": { 43 | "text/plain": [ 44 | "56" 45 | ] 46 | }, 47 | "execution_count": 2, 48 | "metadata": {}, 49 | "output_type": "execute_result" 50 | } 51 | ], 52 | "source": [ 53 | "from random import randint\n", 54 | "num=randint(0,101)\n", 55 | "num" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "#### Next, print an introduction to the game and explain the rules" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "you have to guess the correct number" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "RULES ARE:" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "1.number must be between 1-100\n" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "2.when if your guess is\n", 91 | "within 10 of the number, hint- \"WARM!\"\n", 92 | "further than 10 away from the number, hint- \"COLD!\"" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "3. On all subsequent turns, if your guess is\n", 100 | "closer to the number than the previous guess hint- \"WARMER!\"\n", 101 | "farther from the number than the previous guess, hint-\"COLDER!\"" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "#### Create a list to store guesses\n", 109 | "\n", 110 | "Hint: zero is a good placeholder value. It's useful because it evaluates to \"False\"" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 3, 116 | "metadata": {}, 117 | "outputs": [], 118 | "source": [ 119 | "guess_list=[]\n" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "#### Write a `while` loop that asks for a valid guess. Test it a few times to make sure it works." 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 4, 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "name": "stdout", 136 | "output_type": "stream", 137 | "text": [ 138 | "enter a number between 1-10077\n", 139 | "valid entry\n" 140 | ] 141 | } 142 | ], 143 | "source": [ 144 | "while True:\n", 145 | " a=''\n", 146 | " a = input('enter a number between 1-100')\n", 147 | " if int(a)>=1 and int(a)<=100:\n", 148 | " guess_list.append(int(a))\n", 149 | " print('valid entry')\n", 150 | " break\n", 151 | " else:\n", 152 | " print('num is out of bounds,please enter again')\n", 153 | " pass" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "#### Write a `while` loop that compares the player's guess to our number. If the player guesses correctly, break from the loop. Otherwise, tell the player if they're warmer or colder, and continue asking for guesses.\n", 161 | "\n", 162 | "Some hints:\n", 163 | "* it may help to sketch out all possible combinations on paper first!\n", 164 | "* you can use the `abs()` function to find the positive difference between two numbers\n", 165 | "* if you append all new guesses to the list, then the previous guess is given as `guesses[-2]`" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": null, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "89\n", 178 | "enter a number between 1-100 \n", 179 | "150\n", 180 | "num is out of bounds,please enter again \n", 181 | "\n", 182 | "enter a number between 1-100 \n", 183 | "-60\n", 184 | "num is out of bounds,please enter again \n", 185 | "\n", 186 | "enter a number between 1-100 \n", 187 | "70\n", 188 | "enter a number between 1-100 \n", 189 | "89\n" 190 | ] 191 | } 192 | ], 193 | "source": [ 194 | "from random import randint\n", 195 | "num=randint(0,101)\n", 196 | "print(num)\n", 197 | "\n", 198 | "guess_list=[]\n", 199 | " \n", 200 | "while True:\n", 201 | " \n", 202 | " a = input('enter a number between 1-100 \\n')\n", 203 | " if int(a)>=1 and int(a)<=100:\n", 204 | " guess_list.append(int(a))\n", 205 | "\n", 206 | " else:\n", 207 | " print('num is out of bounds,please enter again \\n')\n", 208 | " continue \n", 209 | "\n", 210 | " if guess_list[-1]==num:\n", 211 | " print('you guessed it!')\n", 212 | " break\n", 213 | " elif abs(guess_list[0]-num) <=10 and len(guess_list) == 1:\n", 214 | " print('WARM')\n", 215 | " elif len(guess_list) == 1:\n", 216 | " print('COLD')\n", 217 | " else:\n", 218 | " if abs(num-guess_list[-2]) > abs(num-guess_list[-1]):\n", 219 | " print(\"Warmer\")\n", 220 | " else:\n", 221 | " print(\"Colder\")\n", 222 | "\n", 223 | "\n", 224 | "print(\"Game over!\")\n" 225 | ] 226 | }, 227 | { 228 | "cell_type": "markdown", 229 | "metadata": {}, 230 | "source": [ 231 | "That's it! You've just programmed your first game!\n", 232 | "\n", 233 | "In the next section we'll learn how to turn some of these repetitive actions into *functions* that can be called whenever we need them." 234 | ] 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": {}, 239 | "source": [ 240 | "### Good Job!" 241 | ] 242 | } 243 | ], 244 | "metadata": { 245 | "kernelspec": { 246 | "display_name": "Python 3", 247 | "language": "python", 248 | "name": "python3" 249 | }, 250 | "language_info": { 251 | "codemirror_mode": { 252 | "name": "ipython", 253 | "version": 3 254 | }, 255 | "file_extension": ".py", 256 | "mimetype": "text/x-python", 257 | "name": "python", 258 | "nbconvert_exporter": "python", 259 | "pygments_lexer": "ipython3", 260 | "version": "3.7.6" 261 | } 262 | }, 263 | "nbformat": 4, 264 | "nbformat_minor": 2 265 | } 266 | -------------------------------------------------------------------------------- /Bootcamp/3.1-Carnival guessing game- Three cup Monte.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "example=[1,2,3,4,5,5,6,7]" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "from random import shuffle" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "data": { 28 | "text/plain": [ 29 | "[1, 6, 5, 5, 2, 4, 3, 7]" 30 | ] 31 | }, 32 | "execution_count": 3, 33 | "metadata": {}, 34 | "output_type": "execute_result" 35 | } 36 | ], 37 | "source": [ 38 | "shuffle(example)\n", 39 | "example" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 4, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "#function -1\n", 49 | "def shuffle_list(l1):\n", 50 | " return l1" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 5, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "[1, 6, 5, 5, 2, 4, 3, 7]" 62 | ] 63 | }, 64 | "execution_count": 5, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "result=shuffle_list(example)\n", 71 | "result" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 6, 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "['', 'o', '']" 83 | ] 84 | }, 85 | "execution_count": 6, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "my_list=['','o','']\n", 92 | "result=shuffle_list(my_list)\n", 93 | "result" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 7, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "#function-2\n", 103 | "def player_guess():\n", 104 | " guess=''\n", 105 | " \n", 106 | " while guess not in ['0','1','2']:\n", 107 | " guess=input('choose: 0, 1 or 2')\n", 108 | " \n", 109 | " return int(guess)" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 8, 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "name": "stdout", 119 | "output_type": "stream", 120 | "text": [ 121 | "choose: 0, 1 or 23\n", 122 | "choose: 0, 1 or 21\n" 123 | ] 124 | }, 125 | { 126 | "data": { 127 | "text/plain": [ 128 | "1" 129 | ] 130 | }, 131 | "execution_count": 8, 132 | "metadata": {}, 133 | "output_type": "execute_result" 134 | } 135 | ], 136 | "source": [ 137 | "player_guess()" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 9, 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "choose: 0, 1 or 22\n" 150 | ] 151 | }, 152 | { 153 | "data": { 154 | "text/plain": [ 155 | "2" 156 | ] 157 | }, 158 | "execution_count": 9, 159 | "metadata": {}, 160 | "output_type": "execute_result" 161 | } 162 | ], 163 | "source": [ 164 | "choice=player_guess()\n", 165 | "choice" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 10, 171 | "metadata": {}, 172 | "outputs": [], 173 | "source": [ 174 | "#function-3\n", 175 | "def check_guess(my_list,guess):\n", 176 | " if my_list[guess]=='o':\n", 177 | " pritnt('you won!')\n", 178 | " else:\n", 179 | " print('you lose')\n", 180 | " print(my_list)" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 11, 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "name": "stdout", 190 | "output_type": "stream", 191 | "text": [ 192 | "you lose\n", 193 | "['', 'o', '']\n" 194 | ] 195 | } 196 | ], 197 | "source": [ 198 | "check_guess(result,choice)" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 12, 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [ 207 | "#final composition" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 13, 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "name": "stdout", 217 | "output_type": "stream", 218 | "text": [ 219 | "choose: 0, 1 or 22\n", 220 | "you lose\n", 221 | "['', 'o', '']\n" 222 | ] 223 | } 224 | ], 225 | "source": [ 226 | "#INITIAL LIST\n", 227 | "my_list=['','o','']\n", 228 | "\n", 229 | "#SHUFFLE LIST\n", 230 | "mixedup_list=shuffle_list(my_list)\n", 231 | "\n", 232 | "#USER GUESS\n", 233 | "guess=player_guess()\n", 234 | "\n", 235 | "#CHECK GUESS\n", 236 | "check_guess(mixedup_list,guess)" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": null, 242 | "metadata": {}, 243 | "outputs": [], 244 | "source": [] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": null, 249 | "metadata": {}, 250 | "outputs": [], 251 | "source": [] 252 | } 253 | ], 254 | "metadata": { 255 | "kernelspec": { 256 | "display_name": "Python 3", 257 | "language": "python", 258 | "name": "python3" 259 | }, 260 | "language_info": { 261 | "codemirror_mode": { 262 | "name": "ipython", 263 | "version": 3 264 | }, 265 | "file_extension": ".py", 266 | "mimetype": "text/x-python", 267 | "name": "python", 268 | "nbconvert_exporter": "python", 269 | "pygments_lexer": "ipython3", 270 | "version": "3.7.6" 271 | } 272 | }, 273 | "nbformat": 4, 274 | "nbformat_minor": 4 275 | } 276 | -------------------------------------------------------------------------------- /Bootcamp/3.3 args and kwargs.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 15, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def myfunc(a,b,c=0,d=0,e=0):\n", 10 | " #returns 50% of the sum\n", 11 | " return sum((a,b,c,d,e)) * 0.5" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 16, 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "data": { 21 | "text/plain": [ 22 | "150.0" 23 | ] 24 | }, 25 | "execution_count": 16, 26 | "metadata": {}, 27 | "output_type": "execute_result" 28 | } 29 | ], 30 | "source": [ 31 | "myfunc(40,60,50,50,100)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 19, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "def myfunc(*args):\n", 41 | " #returns 50% of the sum\n", 42 | " return sum(args) * 0.5" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 23, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "150.0" 54 | ] 55 | }, 56 | "execution_count": 23, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "myfunc(40,60,50,50,100)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 22, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "data": { 72 | "text/plain": [ 73 | "50.0" 74 | ] 75 | }, 76 | "execution_count": 22, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "myfunc(40,60)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 24, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "def myfunc(*args):\n", 92 | " for i in args:\n", 93 | " print(i)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 26, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "40\n", 106 | "6\n", 107 | "500\n", 108 | "5\n", 109 | "100\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "myfunc(40,6,500,5,100)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 39, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "def myfunc(**kwargs):\n", 124 | " print(kwargs)\n", 125 | " if 'fruit' in kwargs:\n", 126 | " print('fruit is {}'.format(kwargs['fruit']))\n", 127 | " else:\n", 128 | " print('no fruit')" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 40, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "name": "stdout", 138 | "output_type": "stream", 139 | "text": [ 140 | "{'fruit': 'mango', 'veggi': 'tamato', 'flower': 'daisy'}\n", 141 | "fruit is mango\n" 142 | ] 143 | } 144 | ], 145 | "source": [ 146 | "myfunc(fruit='mango',veggi='tamato',flower='daisy')" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": null, 159 | "metadata": {}, 160 | "outputs": [], 161 | "source": [] 162 | } 163 | ], 164 | "metadata": { 165 | "kernelspec": { 166 | "display_name": "Python 3", 167 | "language": "python", 168 | "name": "python3" 169 | }, 170 | "language_info": { 171 | "codemirror_mode": { 172 | "name": "ipython", 173 | "version": 3 174 | }, 175 | "file_extension": ".py", 176 | "mimetype": "text/x-python", 177 | "name": "python", 178 | "nbconvert_exporter": "python", 179 | "pygments_lexer": "ipython3", 180 | "version": "3.7.6" 181 | } 182 | }, 183 | "nbformat": 4, 184 | "nbformat_minor": 4 185 | } 186 | -------------------------------------------------------------------------------- /Bootcamp/3.4 Lambda exp- Map and Filter.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def square(num):\n", 10 | " return num**2" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "my_num=[1,2,3,4,5]" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 3, 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "1\n", 32 | "4\n", 33 | "9\n", 34 | "16\n", 35 | "25\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "for i in my_num:\n", 41 | " print(square(i))\n" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 4, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "1\n", 54 | "4\n", 55 | "9\n", 56 | "16\n", 57 | "25\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "for i in map(square,my_num):\n", 63 | " print(i)" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 5, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "data": { 73 | "text/plain": [ 74 | "[1, 4, 9, 16, 25]" 75 | ] 76 | }, 77 | "execution_count": 5, 78 | "metadata": {}, 79 | "output_type": "execute_result" 80 | } 81 | ], 82 | "source": [ 83 | "list(map(square,my_num))" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 9, 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [ 92 | "def splicer(str1):\n", 93 | " if len(str1)%2 == 0:\n", 94 | " return 'EVEN'\n", 95 | " else:\n", 96 | " return str1[0]" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 10, 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [ 105 | "str1=['Andy','Eve','Billy','Candy','Sally']" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 13, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "data": { 115 | "text/plain": [ 116 | "['EVEN', 'E', 'B', 'C', 'S']" 117 | ] 118 | }, 119 | "execution_count": 13, 120 | "metadata": {}, 121 | "output_type": "execute_result" 122 | } 123 | ], 124 | "source": [ 125 | "list(map(splicer,str1))" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 15, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "('EVEN', 'E', 'B', 'C', 'S')" 137 | ] 138 | }, 139 | "execution_count": 15, 140 | "metadata": {}, 141 | "output_type": "execute_result" 142 | } 143 | ], 144 | "source": [ 145 | "tuple(map(splicer,str1))" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 21, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "data": { 155 | "text/plain": [ 156 | "{'B', 'C', 'E', 'EVEN', 'S'}" 157 | ] 158 | }, 159 | "execution_count": 21, 160 | "metadata": {}, 161 | "output_type": "execute_result" 162 | } 163 | ], 164 | "source": [ 165 | "set(map(splicer,str1))\n", 166 | "#maps the functn and argument\n", 167 | "#run for every value of argumnet.no filter.display result for both true and false" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 17, 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [ 176 | "def check_even(num):\n", 177 | " return num%2==0" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 18, 183 | "metadata": {}, 184 | "outputs": [], 185 | "source": [ 186 | "mynum=[1,2,3,4,5]" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": 19, 192 | "metadata": {}, 193 | "outputs": [ 194 | { 195 | "data": { 196 | "text/plain": [ 197 | "[2, 4]" 198 | ] 199 | }, 200 | "execution_count": 19, 201 | "metadata": {}, 202 | "output_type": "execute_result" 203 | } 204 | ], 205 | "source": [ 206 | "list(filter(check_even,mynum))\n", 207 | "#filtered out and displayed only values which evaluate to be true" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 22, 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "name": "stdout", 217 | "output_type": "stream", 218 | "text": [ 219 | "2\n", 220 | "4\n" 221 | ] 222 | } 223 | ], 224 | "source": [ 225 | "for i in filter(check_even,mynum):\n", 226 | " print(i)" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 23, 232 | "metadata": {}, 233 | "outputs": [], 234 | "source": [ 235 | "def square(num):\n", 236 | " result=num**2\n", 237 | " return result" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 24, 243 | "metadata": {}, 244 | "outputs": [ 245 | { 246 | "data": { 247 | "text/plain": [ 248 | "9" 249 | ] 250 | }, 251 | "execution_count": 24, 252 | "metadata": {}, 253 | "output_type": "execute_result" 254 | } 255 | ], 256 | "source": [ 257 | "square(3)" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 34, 263 | "metadata": {}, 264 | "outputs": [], 265 | "source": [ 266 | "square1= lambda num : num**2\n", 267 | "#one liner" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 33, 273 | "metadata": {}, 274 | "outputs": [ 275 | { 276 | "data": { 277 | "text/plain": [ 278 | "9" 279 | ] 280 | }, 281 | "execution_count": 33, 282 | "metadata": {}, 283 | "output_type": "execute_result" 284 | } 285 | ], 286 | "source": [ 287 | "square1(3)" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 37, 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "data": { 297 | "text/plain": [ 298 | "[1, 4, 9, 16, 25]" 299 | ] 300 | }, 301 | "execution_count": 37, 302 | "metadata": {}, 303 | "output_type": "execute_result" 304 | } 305 | ], 306 | "source": [ 307 | "list(map(lambda num : num**2,mynum))\n", 308 | "#one liner-wrote fn,made it into lambda,passed into map,stored in list" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 38, 314 | "metadata": {}, 315 | "outputs": [], 316 | "source": [ 317 | "#nested statements and scope" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 39, 323 | "metadata": {}, 324 | "outputs": [], 325 | "source": [ 326 | "n=25" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "execution_count": 40, 332 | "metadata": {}, 333 | "outputs": [], 334 | "source": [ 335 | "def printer(n):\n", 336 | " x=30\n", 337 | " return x" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 45, 343 | "metadata": {}, 344 | "outputs": [ 345 | { 346 | "data": { 347 | "text/plain": [ 348 | "30" 349 | ] 350 | }, 351 | "execution_count": 45, 352 | "metadata": {}, 353 | "output_type": "execute_result" 354 | } 355 | ], 356 | "source": [ 357 | "n=printer(n)\n", 358 | "x" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": 46, 364 | "metadata": {}, 365 | "outputs": [ 366 | { 367 | "data": { 368 | "text/plain": [ 369 | "30" 370 | ] 371 | }, 372 | "execution_count": 46, 373 | "metadata": {}, 374 | "output_type": "execute_result" 375 | } 376 | ], 377 | "source": [ 378 | "n" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": null, 384 | "metadata": {}, 385 | "outputs": [], 386 | "source": [] 387 | } 388 | ], 389 | "metadata": { 390 | "kernelspec": { 391 | "display_name": "Python 3", 392 | "language": "python", 393 | "name": "python3" 394 | }, 395 | "language_info": { 396 | "codemirror_mode": { 397 | "name": "ipython", 398 | "version": 3 399 | }, 400 | "file_extension": ".py", 401 | "mimetype": "text/x-python", 402 | "name": "python", 403 | "nbconvert_exporter": "python", 404 | "pygments_lexer": "ipython3", 405 | "version": "3.7.6" 406 | } 407 | }, 408 | "nbformat": 4, 409 | "nbformat_minor": 4 410 | } 411 | -------------------------------------------------------------------------------- /Bootcamp/3.6 Functions and Methods Homework.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Functions and Methods Homework \n", 8 | "\n", 9 | "Complete the following questions:\n", 10 | "____\n", 11 | "**Write a function that computes the volume of a sphere given its radius.**\n", 12 | "

The volume of a sphere is given as $$\\frac{4}{3} πr^3$$

" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 3, 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "def vol(rad):\n", 22 | " return ((4*3.14*(rad**3))/3)" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 4, 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "data": { 32 | "text/plain": [ 33 | "33.49333333333333" 34 | ] 35 | }, 36 | "execution_count": 4, 37 | "metadata": {}, 38 | "output_type": "execute_result" 39 | } 40 | ], 41 | "source": [ 42 | "# Check\n", 43 | "vol(2)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "___\n", 51 | "**Write a function that checks whether a number is in a given range (inclusive of high and low)**" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 5, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "def ran_check(num,low,high):\n", 61 | " if num in range(low,high+1):\n", 62 | " print(f'{num} is in the range between {low} and {high}')\n", 63 | " else:\n", 64 | " print(f'{num} is not in the range between {low} and {high}')" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 6, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "name": "stdout", 74 | "output_type": "stream", 75 | "text": [ 76 | "5 is in the range between 2 and 7\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "# Check\n", 82 | "ran_check(5,2,7)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "If you only wanted to return a boolean:" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 7, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "def ran_bool(num,low,high):\n", 99 | " return num in range(low,high+1)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 25, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "data": { 109 | "text/plain": [ 110 | "True" 111 | ] 112 | }, 113 | "execution_count": 25, 114 | "metadata": {}, 115 | "output_type": "execute_result" 116 | } 117 | ], 118 | "source": [ 119 | "ran_bool(3,1,10)\n" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "metadata": {}, 125 | "source": [ 126 | "____\n", 127 | "**Write a Python function that accepts a string and calculates the number of upper case letters and lower case letters.**\n", 128 | "\n", 129 | " Sample String : 'Hello Mr. Rogers, how are you this fine Tuesday?'\n", 130 | " Expected Output : \n", 131 | " No. of Upper case characters : 4\n", 132 | " No. of Lower case Characters : 33\n", 133 | "\n", 134 | "HINT: Two string methods that might prove useful: **.isupper()** and **.islower()**\n", 135 | "\n", 136 | "If you feel ambitious, explore the Collections module to solve this problem!" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 23, 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [ 145 | "def up_low(s):\n", 146 | " count1 = 0\n", 147 | " count2 = 0\n", 148 | " for i in s:\n", 149 | " if i.isupper():\n", 150 | " count1 =count1+1\n", 151 | " elif i.islower():\n", 152 | " count2 =count2+1\n", 153 | " else:\n", 154 | " pass\n", 155 | " print(s)\n", 156 | " print(f'No. of Upper case characters : {count1}')\n", 157 | " print(f'No. of Lower case characters : {count2}')\n", 158 | " " 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 24, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "name": "stdout", 168 | "output_type": "stream", 169 | "text": [ 170 | "Hello Mr. Rogers, how are you this fine Tuesday?\n", 171 | "No. of Upper case characters : 4\n", 172 | "No. of Lower case characters : 33\n" 173 | ] 174 | } 175 | ], 176 | "source": [ 177 | "s = 'Hello Mr. Rogers, how are you this fine Tuesday?'\n", 178 | "up_low(s)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "____\n", 186 | "**Write a Python function that takes a list and returns a new list with unique elements of the first list.**\n", 187 | "\n", 188 | " Sample List : [1,1,1,1,2,2,3,3,3,3,4,5]\n", 189 | " Unique List : [1, 2, 3, 4, 5]" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 30, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [ 198 | "def unique_list(lst):\n", 199 | " l2=set(lst)\n", 200 | " return list(l2)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 31, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "[1, 2, 3, 4, 5]" 212 | ] 213 | }, 214 | "execution_count": 31, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "unique_list([1,1,1,1,2,2,3,3,3,3,4,5])" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "____\n", 228 | "**Write a Python function to multiply all the numbers in a list.**\n", 229 | "\n", 230 | " Sample List : [1, 2, 3, -4]\n", 231 | " Expected Output : -24" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 34, 237 | "metadata": {}, 238 | "outputs": [], 239 | "source": [ 240 | "def multiply(numbers): \n", 241 | " mul=1\n", 242 | " for i in numbers:\n", 243 | " mul *=i\n", 244 | " return mul" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": 35, 250 | "metadata": {}, 251 | "outputs": [ 252 | { 253 | "data": { 254 | "text/plain": [ 255 | "-24" 256 | ] 257 | }, 258 | "execution_count": 35, 259 | "metadata": {}, 260 | "output_type": "execute_result" 261 | } 262 | ], 263 | "source": [ 264 | "multiply([1,2,3,-4])" 265 | ] 266 | }, 267 | { 268 | "cell_type": "markdown", 269 | "metadata": {}, 270 | "source": [ 271 | "____\n", 272 | "**Write a Python function that checks whether a word or phrase is palindrome or not.**\n", 273 | "\n", 274 | "Note: A palindrome is word, phrase, or sequence that reads the same backward as forward, e.g., madam,kayak,racecar, or a phrase \"nurses run\". Hint: You may want to check out the .replace() method in a string to help out with dealing with spaces. Also google search how to reverse a string in Python, there are some clever ways to do it with slicing notation." 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 54, 280 | "metadata": {}, 281 | "outputs": [], 282 | "source": [ 283 | "def palindrome(s):\n", 284 | " return s[::-1]==s" 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 55, 290 | "metadata": {}, 291 | "outputs": [ 292 | { 293 | "name": "stdout", 294 | "output_type": "stream", 295 | "text": [ 296 | "True\n" 297 | ] 298 | } 299 | ], 300 | "source": [ 301 | "print(palindrome('helleh'))" 302 | ] 303 | }, 304 | { 305 | "cell_type": "markdown", 306 | "metadata": {}, 307 | "source": [ 308 | "____\n", 309 | "#### Hard:\n", 310 | "\n", 311 | "**Write a Python function to check whether a string is pangram or not. (Assume the string passed in does not have any punctuation)**\n", 312 | "\n", 313 | " Note : Pangrams are words or sentences containing every letter of the alphabet at least once.\n", 314 | " For example : \"The quick brown fox jumps over the lazy dog\"\n", 315 | "\n", 316 | "Hint: You may want to use .replace() method to get rid of spaces.\n", 317 | "\n", 318 | "Hint: Look at the [string module](https://stackoverflow.com/questions/16060899/alphabet-range-in-python)\n", 319 | "\n", 320 | "Hint: In case you want to use [set comparisons](https://medium.com/better-programming/a-visual-guide-to-set-comparisons-in-python-6ab7edb9ec41)" 321 | ] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": 78, 326 | "metadata": {}, 327 | "outputs": [], 328 | "source": [ 329 | "import string\n", 330 | "\n", 331 | "def ispangram(str1, alphabet=string.ascii_lowercase):\n", 332 | " #replace -remove spaces\n", 333 | " str1=str1.replace(' ','') \n", 334 | "\n", 335 | " #convert all to lower\n", 336 | " str1=str1.lower()\n", 337 | "\n", 338 | " #set-to get unique values\n", 339 | " str1=set(str1)\n", 340 | "\n", 341 | " #convert to list- to use sort fn\n", 342 | " l1=list(str1)\n", 343 | "\n", 344 | " #sort\n", 345 | " l1.sort()\n", 346 | " \n", 347 | " #cast all alphabets to another list\n", 348 | " l2=list(alphabet)\n", 349 | "\n", 350 | " #compare\n", 351 | " return l1==l2\n", 352 | " " 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": 79, 358 | "metadata": {}, 359 | "outputs": [ 360 | { 361 | "data": { 362 | "text/plain": [ 363 | "True" 364 | ] 365 | }, 366 | "execution_count": 79, 367 | "metadata": {}, 368 | "output_type": "execute_result" 369 | } 370 | ], 371 | "source": [ 372 | "ispangram(\"The quick brown fox jumps over the lazy dog\")" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 80, 378 | "metadata": {}, 379 | "outputs": [ 380 | { 381 | "name": "stdout", 382 | "output_type": "stream", 383 | "text": [ 384 | "abcdefghijklmnopqrstuvwxyz\n" 385 | ] 386 | } 387 | ], 388 | "source": [ 389 | "import string\n", 390 | "\n", 391 | "print(string.ascii_lowercase)" 392 | ] 393 | }, 394 | { 395 | "cell_type": "markdown", 396 | "metadata": { 397 | "collapsed": true 398 | }, 399 | "source": [ 400 | "#### Great Job!" 401 | ] 402 | } 403 | ], 404 | "metadata": { 405 | "kernelspec": { 406 | "display_name": "Python 3", 407 | "language": "python", 408 | "name": "python3" 409 | }, 410 | "language_info": { 411 | "codemirror_mode": { 412 | "name": "ipython", 413 | "version": 3 414 | }, 415 | "file_extension": ".py", 416 | "mimetype": "text/x-python", 417 | "name": "python", 418 | "nbconvert_exporter": "python", 419 | "pygments_lexer": "ipython3", 420 | "version": "3.7.6" 421 | } 422 | }, 423 | "nbformat": 4, 424 | "nbformat_minor": 1 425 | } 426 | -------------------------------------------------------------------------------- /Bootcamp/4.0 Object Oriented Programming.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "class Sample():\n", 10 | " pass" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "s1=Sample()" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 3, 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "data": { 29 | "text/plain": [ 30 | "__main__.Sample" 31 | ] 32 | }, 33 | "execution_count": 3, 34 | "metadata": {}, 35 | "output_type": "execute_result" 36 | } 37 | ], 38 | "source": [ 39 | "type(s1)" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 32, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "class Dog():\n", 49 | " #class level attribute-same for all instance\n", 50 | " species = 'mammal'\n", 51 | " \n", 52 | " #constructor body\n", 53 | " def __init__(self,name,spots,age):\n", 54 | " self.name=name\n", 55 | " self.spots=spots\n", 56 | " self.age=age\n", 57 | " \n", 58 | " #fn inside class called as method\n", 59 | " def bark(self,number):\n", 60 | " print(f'Woof! my name is {self.name} and the no is {number}')" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 33, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "my_dog1=Dog(age='4',name='max',spots=False) #order does not matter if name is given\n", 70 | "\n", 71 | "my_dog2=Dog('baax',False,3) #matters when name not given\n", 72 | "\n", 73 | "#do datatype check" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 42, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "3 max False mammal\n", 86 | "baax\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "print(f'{my_dog2.age} {my_dog1.name} {my_dog2.spots} {my_dog1.species}')\n", 92 | "print(my_dog2.name)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 35, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "name": "stdout", 102 | "output_type": "stream", 103 | "text": [ 104 | "Woof! my name is max and the no is 7\n" 105 | ] 106 | } 107 | ], 108 | "source": [ 109 | "my_dog1.bark(7)" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 54, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "class Circle():\n", 119 | " pi=3.14\n", 120 | " \n", 121 | " def __init__(self,radius=1):\n", 122 | " self.radius=radius\n", 123 | " self.area=radius*radius*self.pi\n", 124 | " \n", 125 | " def get_circumference(self):\n", 126 | " return 2*self.pi*self.radius" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 55, 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [ 135 | "my_circle=Circle(100)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 56, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "data": { 145 | "text/plain": [ 146 | "628.0" 147 | ] 148 | }, 149 | "execution_count": 56, 150 | "metadata": {}, 151 | "output_type": "execute_result" 152 | } 153 | ], 154 | "source": [ 155 | "my_circle.get_circumference()" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 57, 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "data": { 165 | "text/plain": [ 166 | "31400.0" 167 | ] 168 | }, 169 | "execution_count": 57, 170 | "metadata": {}, 171 | "output_type": "execute_result" 172 | } 173 | ], 174 | "source": [ 175 | "my_circle.area" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 35, 181 | "metadata": {}, 182 | "outputs": [], 183 | "source": [ 184 | "class Student():\n", 185 | " #class level attribute -same for all instances\n", 186 | " city='Ujjain'\n", 187 | " \n", 188 | " #constructor body\n", 189 | " def __init__(self,name='None',subject1='None',subject2='None',hostler=False):\n", 190 | " self.name=name\n", 191 | " self.subject1=subject1\n", 192 | " self.subject2=subject2\n", 193 | " self.hostler=hostler\n", 194 | " \n", 195 | " #fn inside class called as method\n", 196 | " def calculate_grade(self,score1=50,score2=50):\n", 197 | " sum=score1+score2\n", 198 | " return (f' {self.name} has score {sum/2}')\n", 199 | " " 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 36, 205 | "metadata": {}, 206 | "outputs": [], 207 | "source": [ 208 | "manu=Student('manu','PCM','PE',False) #order does not matter if name is given\n", 209 | "tanu=Student(subject2='IP',name='tanu',subject1='PCM',hostler=True)#matters when name not given\n", 210 | "#no datatype match" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 37, 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "name": "stdout", 220 | "output_type": "stream", 221 | "text": [ 222 | "Ujjain\n", 223 | "False\n" 224 | ] 225 | } 226 | ], 227 | "source": [ 228 | "print(tanu.city)\n", 229 | "print(manu.hostler)" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 38, 235 | "metadata": {}, 236 | "outputs": [ 237 | { 238 | "name": "stdout", 239 | "output_type": "stream", 240 | "text": [ 241 | " manu has score 98.0\n", 242 | " tanu has score 90.0\n" 243 | ] 244 | } 245 | ], 246 | "source": [ 247 | "print(manu.calculate_grade(96,100))\n", 248 | "print(tanu.calculate_grade(86,94))" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "metadata": {}, 255 | "outputs": [], 256 | "source": [] 257 | } 258 | ], 259 | "metadata": { 260 | "kernelspec": { 261 | "display_name": "Python 3", 262 | "language": "python", 263 | "name": "python3" 264 | }, 265 | "language_info": { 266 | "codemirror_mode": { 267 | "name": "ipython", 268 | "version": 3 269 | }, 270 | "file_extension": ".py", 271 | "mimetype": "text/x-python", 272 | "name": "python", 273 | "nbconvert_exporter": "python", 274 | "pygments_lexer": "ipython3", 275 | "version": "3.7.6" 276 | } 277 | }, 278 | "nbformat": 4, 279 | "nbformat_minor": 4 280 | } 281 | -------------------------------------------------------------------------------- /Bootcamp/4.1 OOP - Inheritance and Polymorphism.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Inheritance" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 11, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "class Animal():\n", 17 | " #base class\n", 18 | " def __init__(self):\n", 19 | " print('Animal created!')\n", 20 | " \n", 21 | " def who_am_i(self):\n", 22 | " print('I am animal')\n", 23 | " \n", 24 | " def eat(self):\n", 25 | " print('I am eating')" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 33, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "Animal created!\n", 38 | "I am eating\n" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "my_animal=Animal()\n", 44 | "my_animal.eat()" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 39, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "class Dog(Animal):\n", 54 | " #derieved class\n", 55 | " def __init__(self):\n", 56 | " Animal.__init__(self)\n", 57 | " print('I am dog')\n", 58 | " \n", 59 | " def who_am_i(self):\n", 60 | " print('I am an dog')\n", 61 | " \n", 62 | " def bark(self):\n", 63 | " print('Woof!')" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 40, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | "Animal created!\n", 76 | "I am dog\n", 77 | "I am eating\n" 78 | ] 79 | } 80 | ], 81 | "source": [ 82 | "mydog=Dog()\n", 83 | "mydog.eat()" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 41, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "I am an dog\n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "mydog.who_am_i()" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 42, 106 | "metadata": {}, 107 | "outputs": [ 108 | { 109 | "name": "stdout", 110 | "output_type": "stream", 111 | "text": [ 112 | "I am eating\n" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "mydog.eat()" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 43, 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "name": "stdout", 127 | "output_type": "stream", 128 | "text": [ 129 | "Woof!\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "mydog.bark()" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "## Special Methods" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 123, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "#str method --> used when you simply print the object without any dot operator\n", 151 | "class Book():\n", 152 | " def __init__(self,titile,author,pages):\n", 153 | " self.titile=titile\n", 154 | " self.author=author\n", 155 | " self.pages=pages\n", 156 | " \n", 157 | " def __str__(self):\n", 158 | " #represents actual string representation displayed when u print the object\n", 159 | " return f\"{self.titile} is wriiten by {self.author} \"\n", 160 | " \n", 161 | " def __len__(self):\n", 162 | " #this will be called when you use length fn on book object\n", 163 | " return self.pages\n", 164 | " def __del__(self):\n", 165 | " print(\"A book is destroyed\") " 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 124, 171 | "metadata": {}, 172 | "outputs": [], 173 | "source": [ 174 | "b1=Book('Python rocks','yashvant',200)" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 125, 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "data": { 184 | "text/plain": [ 185 | "'yashvant'" 186 | ] 187 | }, 188 | "execution_count": 125, 189 | "metadata": {}, 190 | "output_type": "execute_result" 191 | } 192 | ], 193 | "source": [ 194 | "b1.author" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 126, 200 | "metadata": {}, 201 | "outputs": [ 202 | { 203 | "name": "stdout", 204 | "output_type": "stream", 205 | "text": [ 206 | "Python rocks is wriiten by yashvant \n" 207 | ] 208 | } 209 | ], 210 | "source": [ 211 | "print(b1)" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 127, 217 | "metadata": {}, 218 | "outputs": [ 219 | { 220 | "name": "stdout", 221 | "output_type": "stream", 222 | "text": [ 223 | "200\n" 224 | ] 225 | } 226 | ], 227 | "source": [ 228 | "print(len(b1))" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 128, 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "name": "stdout", 238 | "output_type": "stream", 239 | "text": [ 240 | "A book is destroyed\n" 241 | ] 242 | } 243 | ], 244 | "source": [ 245 | "del b1" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": null, 251 | "metadata": {}, 252 | "outputs": [], 253 | "source": [] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [] 261 | } 262 | ], 263 | "metadata": { 264 | "kernelspec": { 265 | "display_name": "Python 3", 266 | "language": "python", 267 | "name": "python3" 268 | }, 269 | "language_info": { 270 | "codemirror_mode": { 271 | "name": "ipython", 272 | "version": 3 273 | }, 274 | "file_extension": ".py", 275 | "mimetype": "text/x-python", 276 | "name": "python", 277 | "nbconvert_exporter": "python", 278 | "pygments_lexer": "ipython3", 279 | "version": "3.7.6" 280 | } 281 | }, 282 | "nbformat": 4, 283 | "nbformat_minor": 4 284 | } 285 | -------------------------------------------------------------------------------- /Bootcamp/4.2 Object Oriented Programming Homework.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Object Oriented Programming\n", 8 | "## Homework Assignment\n", 9 | "\n", 10 | "#### Problem 1\n", 11 | "Fill in the Line class methods to accept coordinates as a pair of tuples and return the slope and distance of the line." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 50, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "class Line:\n", 21 | " \n", 22 | " def __init__(self,coor1,coor2):\n", 23 | " self.coor1=coor1\n", 24 | " self.coor2=coor2\n", 25 | " \n", 26 | " def distance(self):\n", 27 | " return ((((self.coor2[0]) - (self.coor1[0]))**2) + (((self.coor2[1]) - (self.coor1[1]))**2))**0.5\n", 28 | " \n", 29 | " def slope(self):\n", 30 | " nominator= (self.coor2[1]) - (self.coor1[1])\n", 31 | " denominator= (self.coor2[0]) - (self.coor1[0])\n", 32 | " return nominator/denominator" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 51, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [ 41 | "# EXAMPLE OUTPUT\n", 42 | "\n", 43 | "coordinate1 = (3,2)\n", 44 | "coordinate2 = (8,10)\n", 45 | "\n", 46 | "li = Line(coordinate1,coordinate2)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 52, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "9.433981132056603" 58 | ] 59 | }, 60 | "execution_count": 52, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "li.distance()" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 53, 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "data": { 76 | "text/plain": [ 77 | "1.6" 78 | ] 79 | }, 80 | "execution_count": 53, 81 | "metadata": {}, 82 | "output_type": "execute_result" 83 | } 84 | ], 85 | "source": [ 86 | "li.slope()" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "________\n", 94 | "#### Problem 2" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "Fill in the class " 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 54, 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [ 110 | "class Cylinder:\n", 111 | " \n", 112 | " def __init__(self,height=1,radius=1):\n", 113 | " self.height=height\n", 114 | " self.radius=radius\n", 115 | " \n", 116 | " def volume(self):\n", 117 | " return 3.14*(self.radius**2)*self.height\n", 118 | " \n", 119 | " def surface_area(self):\n", 120 | " return (3.14*(self.radius**2)*2)+ (2*3.14*self.radius*self.height)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 55, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "# EXAMPLE OUTPUT\n", 130 | "c = Cylinder(2,3)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 56, 136 | "metadata": {}, 137 | "outputs": [ 138 | { 139 | "data": { 140 | "text/plain": [ 141 | "56.52" 142 | ] 143 | }, 144 | "execution_count": 56, 145 | "metadata": {}, 146 | "output_type": "execute_result" 147 | } 148 | ], 149 | "source": [ 150 | "c.volume()" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 57, 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "data": { 160 | "text/plain": [ 161 | "94.2" 162 | ] 163 | }, 164 | "execution_count": 57, 165 | "metadata": {}, 166 | "output_type": "execute_result" 167 | } 168 | ], 169 | "source": [ 170 | "c.surface_area()" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": null, 176 | "metadata": {}, 177 | "outputs": [], 178 | "source": [] 179 | } 180 | ], 181 | "metadata": { 182 | "kernelspec": { 183 | "display_name": "Python 3", 184 | "language": "python", 185 | "name": "python3" 186 | }, 187 | "language_info": { 188 | "codemirror_mode": { 189 | "name": "ipython", 190 | "version": 3 191 | }, 192 | "file_extension": ".py", 193 | "mimetype": "text/x-python", 194 | "name": "python", 195 | "nbconvert_exporter": "python", 196 | "pygments_lexer": "ipython3", 197 | "version": "3.7.6" 198 | } 199 | }, 200 | "nbformat": 4, 201 | "nbformat_minor": 1 202 | } 203 | -------------------------------------------------------------------------------- /Bootcamp/4.3 OOP Challenge.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Object Oriented Programming Challenge\n", 8 | "\n", 9 | "For this challenge, create a bank account class that has two attributes:\n", 10 | "\n", 11 | "* owner\n", 12 | "* balance\n", 13 | "\n", 14 | "and two methods:\n", 15 | "\n", 16 | "* deposit\n", 17 | "* withdraw\n", 18 | "\n", 19 | "As an added requirement, withdrawals may not exceed the available balance.\n", 20 | "\n", 21 | "Instantiate your class, make several deposits and withdrawals, and test to make sure the account can't be overdrawn." 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 14, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "class Account:\n", 31 | " def __init__(self,owner,balance):\n", 32 | " self.owner=owner\n", 33 | " self.balance=balance\n", 34 | " \n", 35 | " def deposit(self,amount):\n", 36 | " self.balance=self.balance+amount\n", 37 | " print('Deposit Accepted')\n", 38 | " \n", 39 | " def withdraw(self,amount):\n", 40 | " if self.balance-amount>=0:\n", 41 | " self.balance=self.balance-amount\n", 42 | " print('Withdrawal Accepted')\n", 43 | " else:\n", 44 | " print('Funds Unavailable!')\n", 45 | " \n", 46 | " def __str__(self):\n", 47 | " return f'Account owner: {self.owner}\\nAccount balance: ${self.balance}'" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 15, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "# 1. Instantiate the class\n", 57 | "acct1 = Account('Jose',100)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 16, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "Account owner: Jose\n", 70 | "Account balance: $100\n" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "# 2. Print the object\n", 76 | "print(acct1)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 17, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "'Jose'" 88 | ] 89 | }, 90 | "execution_count": 17, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "# 3. Show the account owner attribute\n", 97 | "acct1.owner" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 18, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "data": { 107 | "text/plain": [ 108 | "100" 109 | ] 110 | }, 111 | "execution_count": 18, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "# 4. Show the account balance attribute\n", 118 | "acct1.balance" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 19, 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "Deposit Accepted\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "# 5. Make a series of deposits and withdrawals\n", 136 | "acct1.deposit(50)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 20, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "name": "stdout", 146 | "output_type": "stream", 147 | "text": [ 148 | "Withdrawal Accepted\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "acct1.withdraw(75)" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 21, 159 | "metadata": {}, 160 | "outputs": [ 161 | { 162 | "name": "stdout", 163 | "output_type": "stream", 164 | "text": [ 165 | "Funds Unavailable!\n" 166 | ] 167 | } 168 | ], 169 | "source": [ 170 | "# 6. Make a withdrawal that exceeds the available balance\n", 171 | "acct1.withdraw(500)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "## Good job!" 179 | ] 180 | } 181 | ], 182 | "metadata": { 183 | "kernelspec": { 184 | "display_name": "Python 3", 185 | "language": "python", 186 | "name": "python3" 187 | }, 188 | "language_info": { 189 | "codemirror_mode": { 190 | "name": "ipython", 191 | "version": 3 192 | }, 193 | "file_extension": ".py", 194 | "mimetype": "text/x-python", 195 | "name": "python", 196 | "nbconvert_exporter": "python", 197 | "pygments_lexer": "ipython3", 198 | "version": "3.7.6" 199 | } 200 | }, 201 | "nbformat": 4, 202 | "nbformat_minor": 2 203 | } 204 | -------------------------------------------------------------------------------- /Bootcamp/5.0 Exception handling- Try and Except.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Try and Except" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 8, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "n1=10\n", 17 | "n2=20\n", 18 | "\n", 19 | "def add_num(n1,n2):\n", 20 | " sum=n1+n2\n", 21 | " return sum" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 9, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/plain": [ 32 | "30" 33 | ] 34 | }, 35 | "execution_count": 9, 36 | "metadata": {}, 37 | "output_type": "execute_result" 38 | } 39 | ], 40 | "source": [ 41 | "add_num(n1,n2)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 10, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "enter a num20\n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "n3=input('enter a num')" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 11, 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "ename": "TypeError", 68 | "evalue": "unsupported operand type(s) for +: 'int' and 'str'", 69 | "output_type": "error", 70 | "traceback": [ 71 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 72 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 73 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0madd_num\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mn3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 74 | "\u001b[1;32m\u001b[0m in \u001b[0;36madd_num\u001b[1;34m(n1, n2)\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;32mdef\u001b[0m \u001b[0madd_num\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mn2\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[0msum\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mn1\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mn2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 6\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 75 | "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" 76 | ] 77 | } 78 | ], 79 | "source": [ 80 | "add_num(n1,n3)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 12, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "def add_num(n1,n2):\n", 90 | " try:\n", 91 | " sum=n1+n2\n", 92 | " return sum\n", 93 | " except:\n", 94 | " print('make sure u are adding correctly')" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 13, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "name": "stdout", 104 | "output_type": "stream", 105 | "text": [ 106 | "make sure u are adding correctly\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "add_num(n1,n3)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "### Else and Finally" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 14, 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "#finally will always execute\n", 128 | "\n", 129 | "def add_num(n1,n2):\n", 130 | " try:\n", 131 | " sum=n1+n2\n", 132 | " return sum\n", 133 | " except:\n", 134 | " print('make sure u are adding correctly')\n", 135 | " finally:\n", 136 | " print('i will always run')" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 16, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "name": "stdout", 146 | "output_type": "stream", 147 | "text": [ 148 | "i will always run\n" 149 | ] 150 | }, 151 | { 152 | "data": { 153 | "text/plain": [ 154 | "30" 155 | ] 156 | }, 157 | "execution_count": 16, 158 | "metadata": {}, 159 | "output_type": "execute_result" 160 | } 161 | ], 162 | "source": [ 163 | "add_num(n1,n2)" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 17, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "name": "stdout", 173 | "output_type": "stream", 174 | "text": [ 175 | "make sure u are adding correctly\n", 176 | "i will always run\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "add_num(n1,n3)" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 18, 187 | "metadata": {}, 188 | "outputs": [], 189 | "source": [ 190 | "#else will excute when except does not run\n", 191 | "\n", 192 | "def add_num(n1,n2):\n", 193 | " try:\n", 194 | " sum=n1+n2\n", 195 | " except:\n", 196 | " print('make sure u are adding correctly')\n", 197 | " else:\n", 198 | " return sum\n", 199 | " finally:\n", 200 | " print('i will always run')" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 19, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "name": "stdout", 210 | "output_type": "stream", 211 | "text": [ 212 | "i will always run\n" 213 | ] 214 | }, 215 | { 216 | "data": { 217 | "text/plain": [ 218 | "30" 219 | ] 220 | }, 221 | "execution_count": 19, 222 | "metadata": {}, 223 | "output_type": "execute_result" 224 | } 225 | ], 226 | "source": [ 227 | "add_num(n1,n2)" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 20, 233 | "metadata": {}, 234 | "outputs": [ 235 | { 236 | "name": "stdout", 237 | "output_type": "stream", 238 | "text": [ 239 | "make sure u are adding correctly\n", 240 | "i will always run\n" 241 | ] 242 | } 243 | ], 244 | "source": [ 245 | "add_num(n1,n3)" 246 | ] 247 | }, 248 | { 249 | "cell_type": "markdown", 250 | "metadata": {}, 251 | "source": [ 252 | "### Type of Exceptions:" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 21, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [ 261 | "def add_num(n1,n2):\n", 262 | " try:\n", 263 | " sum=n1+n2\n", 264 | " except TypeError:\n", 265 | " print('make sure u are adding correctly')\n", 266 | " except:\n", 267 | " print('you encountered some other error')\n", 268 | " else:\n", 269 | " return sum\n", 270 | " finally:\n", 271 | " print('i will always run')" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": 22, 277 | "metadata": {}, 278 | "outputs": [], 279 | "source": [ 280 | "#adding while loop to it.\n", 281 | "\n", 282 | "def add_num(n1,n2):\n", 283 | " while True:\n", 284 | " try:\n", 285 | " sum=n1+n2\n", 286 | " except TypeError:\n", 287 | " print('make sure u are adding correctly')\n", 288 | " except:\n", 289 | " print('you encountered some other error')\n", 290 | " continue\n", 291 | " else:\n", 292 | " return sum\n", 293 | " finally:\n", 294 | " print('i will always run')\n", 295 | " " 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": 23, 301 | "metadata": {}, 302 | "outputs": [ 303 | { 304 | "name": "stdout", 305 | "output_type": "stream", 306 | "text": [ 307 | "i will always run\n" 308 | ] 309 | }, 310 | { 311 | "data": { 312 | "text/plain": [ 313 | "30" 314 | ] 315 | }, 316 | "execution_count": 23, 317 | "metadata": {}, 318 | "output_type": "execute_result" 319 | } 320 | ], 321 | "source": [ 322 | "add_num(n1,n2)" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": null, 328 | "metadata": {}, 329 | "outputs": [], 330 | "source": [] 331 | }, 332 | { 333 | "cell_type": "code", 334 | "execution_count": null, 335 | "metadata": {}, 336 | "outputs": [], 337 | "source": [] 338 | } 339 | ], 340 | "metadata": { 341 | "kernelspec": { 342 | "display_name": "Python 3", 343 | "language": "python", 344 | "name": "python3" 345 | }, 346 | "language_info": { 347 | "codemirror_mode": { 348 | "name": "ipython", 349 | "version": 3 350 | }, 351 | "file_extension": ".py", 352 | "mimetype": "text/x-python", 353 | "name": "python", 354 | "nbconvert_exporter": "python", 355 | "pygments_lexer": "ipython3", 356 | "version": "3.7.6" 357 | } 358 | }, 359 | "nbformat": 4, 360 | "nbformat_minor": 4 361 | } 362 | -------------------------------------------------------------------------------- /Bootcamp/5.1. Errors and Exceptions Homework.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Errors and Exceptions Homework" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Problem 1\n", 15 | "Handle the exception thrown by the code below by using try and except blocks." 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 2, 21 | "metadata": {}, 22 | "outputs": [ 23 | { 24 | "name": "stdout", 25 | "output_type": "stream", 26 | "text": [ 27 | "exception raised\n" 28 | ] 29 | } 30 | ], 31 | "source": [ 32 | "try:\n", 33 | " for i in ['a','b','c']:\n", 34 | " print(i**2)\n", 35 | "except:\n", 36 | " print('exception raised')" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": {}, 42 | "source": [ 43 | "### Problem 2\n", 44 | "Handle the exception thrown by the code below by using try and except blocks. Then use a finally block to print 'All Done.'" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 3, 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "name": "stdout", 54 | "output_type": "stream", 55 | "text": [ 56 | "cannot devide by zero\n", 57 | "All Done!\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "try:\n", 63 | " x = 5\n", 64 | " y = 0\n", 65 | "\n", 66 | " z = x/y\n", 67 | "except ZeroDivisionError:\n", 68 | " print('cannot devide by zero')\n", 69 | "finally:\n", 70 | " print('All Done!')\n", 71 | " " 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "### Problem 3\n", 79 | "Write a function that asks for an integer and prints the square of it. Use a while loop with a try, except, else block to account for incorrect inputs." 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 3, 85 | "metadata": {}, 86 | "outputs": [], 87 | "source": [ 88 | "def ask():\n", 89 | " while True:\n", 90 | " try:\n", 91 | " a=int(input('Input an integer: '))\n", 92 | " except:\n", 93 | " print('An error occurred! Please try again!') \n", 94 | " continue\n", 95 | " else:\n", 96 | " print(f'Thank you, your number squared is: {a*a}') \n", 97 | " break" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 4, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "Input an integer: w\n", 110 | "An error occurred! Please try again!\n", 111 | "Input an integer: 2\n", 112 | "Thank you, your number squared is: 4\n" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "ask()" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "# Great Job!" 125 | ] 126 | } 127 | ], 128 | "metadata": { 129 | "kernelspec": { 130 | "display_name": "Python 3", 131 | "language": "python", 132 | "name": "python3" 133 | }, 134 | "language_info": { 135 | "codemirror_mode": { 136 | "name": "ipython", 137 | "version": 3 138 | }, 139 | "file_extension": ".py", 140 | "mimetype": "text/x-python", 141 | "name": "python", 142 | "nbconvert_exporter": "python", 143 | "pygments_lexer": "ipython3", 144 | "version": "3.7.6" 145 | } 146 | }, 147 | "nbformat": 4, 148 | "nbformat_minor": 1 149 | } 150 | -------------------------------------------------------------------------------- /Bootcamp/5.2 Unit Testing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /Bootcamp/6.0 Python Decorator.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 11, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def hello():\n", 10 | " return 'hello!'" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 12, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "data": { 20 | "text/plain": [ 21 | "" 22 | ] 23 | }, 24 | "execution_count": 12, 25 | "metadata": {}, 26 | "output_type": "execute_result" 27 | } 28 | ], 29 | "source": [ 30 | "hello" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 13, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "'hello!'" 42 | ] 43 | }, 44 | "execution_count": 13, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "hello()" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 14, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "greet=hello\n", 60 | "#is greet pointing to hello or made a copy of hello" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 15, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/plain": [ 71 | "'hello!'" 72 | ] 73 | }, 74 | "execution_count": 15, 75 | "metadata": {}, 76 | "output_type": "execute_result" 77 | } 78 | ], 79 | "source": [ 80 | "greet()" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 16, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "del hello" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 17, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "ename": "NameError", 99 | "evalue": "name 'hello' is not defined", 100 | "output_type": "error", 101 | "traceback": [ 102 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 103 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 104 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhello\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", 105 | "\u001b[1;31mNameError\u001b[0m: name 'hello' is not defined" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "hello()" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 18, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "data": { 120 | "text/plain": [ 121 | "'hello!'" 122 | ] 123 | }, 124 | "execution_count": 18, 125 | "metadata": {}, 126 | "output_type": "execute_result" 127 | } 128 | ], 129 | "source": [ 130 | "greet()\n", 131 | "#made a copy of hello" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 50, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "# if we create a fn1 inside a fn2,the scope is limited to inside the fn2.\n", 141 | "# but we can return fn1 from fn2.\n", 142 | "\n", 143 | "def hello1():\n", 144 | " \n", 145 | " def greet1():\n", 146 | " return 'i m greet() inside hello fn'\n", 147 | " \n", 148 | " return greet1\n", 149 | "#return fn from a fn" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 51, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [ 158 | "hi1=hello1()" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 52, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "data": { 168 | "text/plain": [ 169 | ".greet1()>" 170 | ] 171 | }, 172 | "execution_count": 52, 173 | "metadata": {}, 174 | "output_type": "execute_result" 175 | } 176 | ], 177 | "source": [ 178 | "hi1" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 53, 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "data": { 188 | "text/plain": [ 189 | "'i m greet() inside hello fn'" 190 | ] 191 | }, 192 | "execution_count": 53, 193 | "metadata": {}, 194 | "output_type": "execute_result" 195 | } 196 | ], 197 | "source": [ 198 | "hi1()" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 54, 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [ 207 | "#pass in a fn as param to a fn" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 56, 213 | "metadata": {}, 214 | "outputs": [], 215 | "source": [ 216 | "def a_func(some_func):\n", 217 | " \n", 218 | " print('some code')\n", 219 | " print(some_func())" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 57, 225 | "metadata": {}, 226 | "outputs": [], 227 | "source": [ 228 | "def good():\n", 229 | " return 'hi'" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 58, 235 | "metadata": {}, 236 | "outputs": [ 237 | { 238 | "name": "stdout", 239 | "output_type": "stream", 240 | "text": [ 241 | "some code\n", 242 | "hi\n" 243 | ] 244 | } 245 | ], 246 | "source": [ 247 | "a_func(good)" 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "metadata": {}, 253 | "source": [ 254 | "### Decorator:" 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": 59, 260 | "metadata": {}, 261 | "outputs": [], 262 | "source": [ 263 | "def new_decorator(orignal_func):\n", 264 | " \n", 265 | " def wrap_func():\n", 266 | " print('some code before orignal fn')\n", 267 | " orignal_func()\n", 268 | " print('some code before orignal fn')\n", 269 | " \n", 270 | " return wrap_func" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 66, 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [ 279 | "def func_needs_decoration():\n", 280 | " print('I want to be decorated')" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 67, 286 | "metadata": {}, 287 | "outputs": [], 288 | "source": [ 289 | "decorated_func=new_decorator(func_needs_decoration)" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 68, 295 | "metadata": {}, 296 | "outputs": [ 297 | { 298 | "name": "stdout", 299 | "output_type": "stream", 300 | "text": [ 301 | "some code before orignal fn\n", 302 | "I want to be decorated\n", 303 | "some code before orignal fn\n" 304 | ] 305 | } 306 | ], 307 | "source": [ 308 | "decorated_func()" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": null, 314 | "metadata": {}, 315 | "outputs": [], 316 | "source": [] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": 69, 321 | "metadata": {}, 322 | "outputs": [], 323 | "source": [ 324 | "@new_decorator\n", 325 | "def func_needs_decoration():\n", 326 | " print('I want to be decorated')" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "execution_count": 70, 332 | "metadata": {}, 333 | "outputs": [ 334 | { 335 | "name": "stdout", 336 | "output_type": "stream", 337 | "text": [ 338 | "some code before orignal fn\n", 339 | "I want to be decorated\n", 340 | "some code before orignal fn\n" 341 | ] 342 | } 343 | ], 344 | "source": [ 345 | "func_needs_decoration()" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": null, 351 | "metadata": {}, 352 | "outputs": [], 353 | "source": [] 354 | } 355 | ], 356 | "metadata": { 357 | "kernelspec": { 358 | "display_name": "Python 3", 359 | "language": "python", 360 | "name": "python3" 361 | }, 362 | "language_info": { 363 | "codemirror_mode": { 364 | "name": "ipython", 365 | "version": 3 366 | }, 367 | "file_extension": ".py", 368 | "mimetype": "text/x-python", 369 | "name": "python", 370 | "nbconvert_exporter": "python", 371 | "pygments_lexer": "ipython3", 372 | "version": "3.7.6" 373 | } 374 | }, 375 | "nbformat": 4, 376 | "nbformat_minor": 4 377 | } 378 | -------------------------------------------------------------------------------- /Bootcamp/7.0 Python Generators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 32, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def create_cubes(num): #without generator:\n", 10 | " cubes=[]\n", 11 | " for i in range(1,num):\n", 12 | " cubes.append(i**3)\n", 13 | " return cubes " 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 33, 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "data": { 23 | "text/plain": [ 24 | "[1, 8, 27, 64]" 25 | ] 26 | }, 27 | "execution_count": 33, 28 | "metadata": {}, 29 | "output_type": "execute_result" 30 | } 31 | ], 32 | "source": [ 33 | "create_cubes(5)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 34, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "1\n", 46 | "8\n", 47 | "27\n", 48 | "64\n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "for i in create_cubes(5):\n", 54 | " print(i)\n", 55 | "#if we want to print cubes one by one,we dont need to store cubes into a list" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 30, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "def create_cubes(num): #with generator:\n", 65 | " for i in range(1,num): #saves memory\n", 66 | " yield(i**3) " 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 35, 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "1\n", 79 | "8\n", 80 | "27\n", 81 | "64\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "for i in create_cubes(5): #gives same result\n", 87 | " print(i)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "### fibanacci sequence:" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 2, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [ 103 | "def fib(n):\n", 104 | " a=1\n", 105 | " b=1\n", 106 | " result=[]\n", 107 | " for i in range(n):\n", 108 | " result.append(a)\n", 109 | " a,b=b,a+b\n", 110 | " return result" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 3, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "data": { 120 | "text/plain": [ 121 | "[1, 1, 2, 3, 5]" 122 | ] 123 | }, 124 | "execution_count": 3, 125 | "metadata": {}, 126 | "output_type": "execute_result" 127 | } 128 | ], 129 | "source": [ 130 | "fib(5)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 6, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "def fib(n):\n", 140 | " a=1\n", 141 | " b=1\n", 142 | " for i in range(n):\n", 143 | " yield(a)\n", 144 | " a,b=b,a+b" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 9, 150 | "metadata": {}, 151 | "outputs": [ 152 | { 153 | "name": "stdout", 154 | "output_type": "stream", 155 | "text": [ 156 | "1\n", 157 | "1\n", 158 | "2\n", 159 | "3\n", 160 | "5\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "for i in fib(5):\n", 166 | " print(i)" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "metadata": {}, 173 | "outputs": [], 174 | "source": [] 175 | } 176 | ], 177 | "metadata": { 178 | "kernelspec": { 179 | "display_name": "Python 3", 180 | "language": "python", 181 | "name": "python3" 182 | }, 183 | "language_info": { 184 | "codemirror_mode": { 185 | "name": "ipython", 186 | "version": 3 187 | }, 188 | "file_extension": ".py", 189 | "mimetype": "text/x-python", 190 | "name": "python", 191 | "nbconvert_exporter": "python", 192 | "pygments_lexer": "ipython3", 193 | "version": "3.7.6" 194 | } 195 | }, 196 | "nbformat": 4, 197 | "nbformat_minor": 4 198 | } 199 | -------------------------------------------------------------------------------- /Bootcamp/7.1 Iterators and Generators Homework.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Iterators and Generators Homework \n", 8 | "\n", 9 | "### Problem 1\n", 10 | "\n", 11 | "Create a generator that generates the squares of numbers up to some number N." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 3, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "def gensquares(N):\n", 21 | " for i in range(N):\n", 22 | " yield i**2" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 4, 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "name": "stdout", 32 | "output_type": "stream", 33 | "text": [ 34 | "0\n", 35 | "1\n", 36 | "4\n", 37 | "9\n", 38 | "16\n", 39 | "25\n", 40 | "36\n", 41 | "49\n", 42 | "64\n", 43 | "81\n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "for x in gensquares(10):\n", 49 | " print(x)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "### Problem 2\n", 57 | "\n", 58 | "Create a generator that yields \"n\" random numbers between a low and high number (that are inputs).
Note: Use the random library. For example:" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 5, 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "data": { 68 | "text/plain": [ 69 | "7" 70 | ] 71 | }, 72 | "execution_count": 5, 73 | "metadata": {}, 74 | "output_type": "execute_result" 75 | } 76 | ], 77 | "source": [ 78 | "import random\n", 79 | "\n", 80 | "random.randint(1,10)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 11, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "def rand_num(low,high,n):\n", 90 | " for i in range(n):\n", 91 | " yield (random.randint(low,high))" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 12, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "6\n", 104 | "10\n", 105 | "5\n", 106 | "5\n", 107 | "4\n", 108 | "8\n", 109 | "2\n", 110 | "7\n", 111 | "4\n", 112 | "2\n", 113 | "1\n", 114 | "10\n" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "for num in rand_num(1,10,12):\n", 120 | " print(num)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": {}, 126 | "source": [ 127 | "### Problem 3\n", 128 | "\n", 129 | "Use the iter() function to convert the string below into an iterator:\n" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 15, 135 | "metadata": {}, 136 | "outputs": [ 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | "h\n" 142 | ] 143 | } 144 | ], 145 | "source": [ 146 | "s = 'hello'\n", 147 | "s=iter(s)\n", 148 | "print(next(s))\n", 149 | "#code here" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "### Problem 4\n", 157 | "Explain a use case for a generator using a yield statement where you would not want to use a normal function with a return statement.





\n", 158 | "\n" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "### Extra Credit!\n", 166 | "Can you explain what *gencomp* is in the code below? (Note: We never covered this in lecture! You will have to do some Googling/Stack Overflowing!)" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 16, 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "name": "stdout", 176 | "output_type": "stream", 177 | "text": [ 178 | "4\n", 179 | "5\n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "my_list = [1,2,3,4,5]\n", 185 | "\n", 186 | "gencomp = (item for item in my_list if item > 3)\n", 187 | "\n", 188 | "for item in gencomp:\n", 189 | " print(item)" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "Hint: Google *generator comprehension*!\n", 197 | "\n", 198 | "# Great Job!" 199 | ] 200 | } 201 | ], 202 | "metadata": { 203 | "kernelspec": { 204 | "display_name": "Python 3", 205 | "language": "python", 206 | "name": "python3" 207 | }, 208 | "language_info": { 209 | "codemirror_mode": { 210 | "name": "ipython", 211 | "version": 3 212 | }, 213 | "file_extension": ".py", 214 | "mimetype": "text/x-python", 215 | "name": "python", 216 | "nbconvert_exporter": "python", 217 | "pygments_lexer": "ipython3", 218 | "version": "3.7.6" 219 | } 220 | }, 221 | "nbformat": 4, 222 | "nbformat_minor": 1 223 | } 224 | -------------------------------------------------------------------------------- /Bootcamp/8.1 Collection Module.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Collection module" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "from collections import Counter" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "l1 = [1,1,1,1,2,2,2,3,3,3,3,3,3,3,3,3]" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 5, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "data": { 35 | "text/plain": [ 36 | "Counter({1: 4, 2: 3, 3: 9})" 37 | ] 38 | }, 39 | "execution_count": 5, 40 | "metadata": {}, 41 | "output_type": "execute_result" 42 | } 43 | ], 44 | "source": [ 45 | "Counter(l1)" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [] 54 | } 55 | ], 56 | "metadata": { 57 | "kernelspec": { 58 | "display_name": "Python 3", 59 | "language": "python", 60 | "name": "python3" 61 | }, 62 | "language_info": { 63 | "codemirror_mode": { 64 | "name": "ipython", 65 | "version": 3 66 | }, 67 | "file_extension": ".py", 68 | "mimetype": "text/x-python", 69 | "name": "python", 70 | "nbconvert_exporter": "python", 71 | "pygments_lexer": "ipython3", 72 | "version": "3.7.6" 73 | } 74 | }, 75 | "nbformat": 4, 76 | "nbformat_minor": 4 77 | } 78 | -------------------------------------------------------------------------------- /Bootcamp/8.2 Timing your code- TIME, TIMEIT module.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Timing your code\n", 8 | "### Example Function or Script" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "metadata": {}, 15 | "outputs": [], 16 | "source": [ 17 | "def func_one(n):\n", 18 | " '''\n", 19 | " Given a number n, returns a list of string integers\n", 20 | " ['0','1','2',...'n]\n", 21 | " '''\n", 22 | " return [str(num) for num in range(n)]" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "data": { 32 | "text/plain": [ 33 | "['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']" 34 | ] 35 | }, 36 | "execution_count": 2, 37 | "metadata": {}, 38 | "output_type": "execute_result" 39 | } 40 | ], 41 | "source": [ 42 | "func_one(10)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 3, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "def func_two(n):\n", 52 | " '''\n", 53 | " Given a number n, returns a list of string integers\n", 54 | " ['0','1','2',...'n]\n", 55 | " '''\n", 56 | " return list(map(str,range(n)))" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 4, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']" 68 | ] 69 | }, 70 | "execution_count": 4, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "func_two(10)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "### Method - 1\n", 84 | "## Timing Start and Stop\n", 85 | "We can try using the time module to simply calculate the elapsed time for the code." 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 5, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "import time" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 6, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [ 103 | "# STEP 1: Get start time\n", 104 | "start_time = time.time()\n", 105 | "# Step 2: Run your code you want to time\n", 106 | "result = func_one(1000000)\n", 107 | "# Step 3: Calculate total time elapsed\n", 108 | "end_time = time.time() - start_time" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 7, 114 | "metadata": {}, 115 | "outputs": [ 116 | { 117 | "data": { 118 | "text/plain": [ 119 | "1.335386037826538" 120 | ] 121 | }, 122 | "execution_count": 7, 123 | "metadata": {}, 124 | "output_type": "execute_result" 125 | } 126 | ], 127 | "source": [ 128 | "end_time" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 8, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "# STEP 1: Get start time\n", 138 | "start_time = time.time()\n", 139 | "# Step 2: Run your code you want to time\n", 140 | "result = func_two(1000000)\n", 141 | "# Step 3: Calculate total time elapsed\n", 142 | "end_time = time.time() - start_time" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 9, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "data": { 152 | "text/plain": [ 153 | "0.9863467216491699" 154 | ] 155 | }, 156 | "execution_count": 9, 157 | "metadata": {}, 158 | "output_type": "execute_result" 159 | } 160 | ], 161 | "source": [ 162 | "end_time" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "### Method - 2\n", 170 | "## Timeit Module\n", 171 | "The timeit module takes in two strings, a statement (stmt) and a setup. It then runs the setup code and runs the stmt code some n number of times and reports back average length of time it took." 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 10, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "import timeit" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 11, 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [ 189 | "setup = '''\n", 190 | "def func_one(n):\n", 191 | " return [str(num) for num in range(n)]\n", 192 | "'''\n", 193 | "\n", 194 | "stmt = 'func_one(100)'" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 12, 200 | "metadata": {}, 201 | "outputs": [ 202 | { 203 | "data": { 204 | "text/plain": [ 205 | "10.700997204000032" 206 | ] 207 | }, 208 | "execution_count": 12, 209 | "metadata": {}, 210 | "output_type": "execute_result" 211 | } 212 | ], 213 | "source": [ 214 | "timeit.timeit(stmt,setup,number=100000)" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 13, 220 | "metadata": {}, 221 | "outputs": [], 222 | "source": [ 223 | "setup2 = '''\n", 224 | "def func_two(n):\n", 225 | " return list(map(str,range(n)))\n", 226 | "'''\n", 227 | "\n", 228 | "stmt2 = 'func_two(100)'" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 14, 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "data": { 238 | "text/plain": [ 239 | "7.292531953999969" 240 | ] 241 | }, 242 | "execution_count": 14, 243 | "metadata": {}, 244 | "output_type": "execute_result" 245 | } 246 | ], 247 | "source": [ 248 | "timeit.timeit(stmt2,setup2,number=100000)" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": {}, 254 | "source": [ 255 | "### Method - 3\n", 256 | "## Timing you code with Jupyter \"magic\" method\n", 257 | "NOTE: This method is ONLY available in Jupyter and the magic command needs to be at the top of the cell with nothing above it (not even commented code)" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 15, 263 | "metadata": {}, 264 | "outputs": [ 265 | { 266 | "name": "stdout", 267 | "output_type": "stream", 268 | "text": [ 269 | "113 µs ± 19 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" 270 | ] 271 | } 272 | ], 273 | "source": [ 274 | "%%timeit\n", 275 | "func_one(100)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 16, 281 | "metadata": {}, 282 | "outputs": [ 283 | { 284 | "name": "stdout", 285 | "output_type": "stream", 286 | "text": [ 287 | "70.7 µs ± 2.93 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" 288 | ] 289 | } 290 | ], 291 | "source": [ 292 | "%%timeit\n", 293 | "func_two(100)" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": null, 299 | "metadata": {}, 300 | "outputs": [], 301 | "source": [] 302 | } 303 | ], 304 | "metadata": { 305 | "kernelspec": { 306 | "display_name": "Python 3", 307 | "language": "python", 308 | "name": "python3" 309 | }, 310 | "language_info": { 311 | "codemirror_mode": { 312 | "name": "ipython", 313 | "version": 3 314 | }, 315 | "file_extension": ".py", 316 | "mimetype": "text/x-python", 317 | "name": "python", 318 | "nbconvert_exporter": "python", 319 | "pygments_lexer": "ipython3", 320 | "version": "3.7.6" 321 | } 322 | }, 323 | "nbformat": 4, 324 | "nbformat_minor": 4 325 | } 326 | -------------------------------------------------------------------------------- /Bootcamp/9.1 Files in python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "'C:\\\\Users\\\\Ayushirawat\\\\Desktop\\\\Python-prog-code\\\\Code Workspace'" 12 | ] 13 | }, 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "pwd" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 2, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "import os" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "### create file" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 3, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "Writing file1.txt\n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "%%writefile file1.txt\n", 54 | "this is 1st line\n", 55 | "this is 2nd line\n", 56 | "this is 3rd line\n", 57 | "#created a file at the present location -- works only in juyptr notebook" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "### read from a file" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 6, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "file1 = open('file1.txt')\n", 74 | "#no error msg because file is present at present working directory" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 7, 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "#if not present you will get FileNotFound Error" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 8, 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "'this is 1st line\\nthis is 2nd line\\nthis is 3rd line\\n'" 95 | ] 96 | }, 97 | "execution_count": 8, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "file1.read()" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 9, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "''" 115 | ] 116 | }, 117 | "execution_count": 9, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "#now see, when i read again empty string will display- because i have read the file so now the cursor is at the end of the file,\n", 124 | "#so in order to read again you need to reset the cursor.\n", 125 | "file1.read()" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 10, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "0" 137 | ] 138 | }, 139 | "execution_count": 10, 140 | "metadata": {}, 141 | "output_type": "execute_result" 142 | } 143 | ], 144 | "source": [ 145 | "file1.seek(0)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 11, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "data": { 155 | "text/plain": [ 156 | "'this is 1st line\\nthis is 2nd line\\nthis is 3rd line\\n'" 157 | ] 158 | }, 159 | "execution_count": 11, 160 | "metadata": {}, 161 | "output_type": "execute_result" 162 | } 163 | ], 164 | "source": [ 165 | "file1.read()" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 15, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "data": { 175 | "text/plain": [ 176 | "['this is 1st line\\n', 'this is 2nd line\\n', 'this is 3rd line\\n']" 177 | ] 178 | }, 179 | "execution_count": 15, 180 | "metadata": {}, 181 | "output_type": "execute_result" 182 | } 183 | ], 184 | "source": [ 185 | "file1.seek(0)\n", 186 | "file1.readlines() #a list is returned instead of a gaint string\n", 187 | "#if u dont want to use seek then close and open the file again." 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": {}, 193 | "source": [ 194 | "### Looping through a file" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 17, 200 | "metadata": {}, 201 | "outputs": [ 202 | { 203 | "name": "stdout", 204 | "output_type": "stream", 205 | "text": [ 206 | "this is 1st line\n", 207 | "\n", 208 | "this is 2nd line\n", 209 | "\n", 210 | "this is 3rd line\n", 211 | "\n" 212 | ] 213 | } 214 | ], 215 | "source": [ 216 | "for i in open('file1.txt'):\n", 217 | " print(i)" 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "### Alternative of closing file every time: use with open-->" 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": null, 230 | "metadata": {}, 231 | "outputs": [], 232 | "source": [ 233 | "#creating new file and writing to it\n", 234 | "with open('file2.txt' mode = )" 235 | ] 236 | } 237 | ], 238 | "metadata": { 239 | "kernelspec": { 240 | "display_name": "Python 3", 241 | "language": "python", 242 | "name": "python3" 243 | }, 244 | "language_info": { 245 | "codemirror_mode": { 246 | "name": "ipython", 247 | "version": 3 248 | }, 249 | "file_extension": ".py", 250 | "mimetype": "text/x-python", 251 | "name": "python", 252 | "nbconvert_exporter": "python", 253 | "pygments_lexer": "ipython3", 254 | "version": "3.7.6" 255 | } 256 | }, 257 | "nbformat": 4, 258 | "nbformat_minor": 4 259 | } 260 | -------------------------------------------------------------------------------- /Bootcamp/Extras.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 | "42\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "x = '40'\n", 18 | "y = int(x) + 2\n", 19 | "print(y)" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 5, 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "uct\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "x = 'From marquard@uct.ac.za'\n", 37 | "print(x[14:17])" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 8, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "42\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "print(len('banana')*7)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 10, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "HELLO BOB\n" 67 | ] 68 | } 69 | ], 70 | "source": [ 71 | "greet = 'Hello Bob'\n", 72 | "print(greet.upper())" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 11, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "ename": "NameError", 82 | "evalue": "name 'upper' is not defined", 83 | "output_type": "error", 84 | "traceback": [ 85 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 86 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 87 | "\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[0mupper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgreet\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", 88 | "\u001b[1;31mNameError\u001b[0m: name 'upper' is not defined" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "print(upper(greet))" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 13, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | ".ma\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "data = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'\n", 111 | "pos = data.find('.')\n", 112 | "print(data[pos:pos+3])" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 1, 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "name": "stdout", 122 | "output_type": "stream", 123 | "text": [ 124 | "The Zen of Python, by Tim Peters\n", 125 | "\n", 126 | "Beautiful is better than ugly.\n", 127 | "Explicit is better than implicit.\n", 128 | "Simple is better than complex.\n", 129 | "Complex is better than complicated.\n", 130 | "Flat is better than nested.\n", 131 | "Sparse is better than dense.\n", 132 | "Readability counts.\n", 133 | "Special cases aren't special enough to break the rules.\n", 134 | "Although practicality beats purity.\n", 135 | "Errors should never pass silently.\n", 136 | "Unless explicitly silenced.\n", 137 | "In the face of ambiguity, refuse the temptation to guess.\n", 138 | "There should be one-- and preferably only one --obvious way to do it.\n", 139 | "Although that way may not be obvious at first unless you're Dutch.\n", 140 | "Now is better than never.\n", 141 | "Although never is often better than *right* now.\n", 142 | "If the implementation is hard to explain, it's a bad idea.\n", 143 | "If the implementation is easy to explain, it may be a good idea.\n", 144 | "Namespaces are one honking great idea -- let's do more of those!\n" 145 | ] 146 | } 147 | ], 148 | "source": [ 149 | "import this" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": null, 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [] 158 | } 159 | ], 160 | "metadata": { 161 | "kernelspec": { 162 | "display_name": "Python 3", 163 | "language": "python", 164 | "name": "python3" 165 | }, 166 | "language_info": { 167 | "codemirror_mode": { 168 | "name": "ipython", 169 | "version": 3 170 | }, 171 | "file_extension": ".py", 172 | "mimetype": "text/x-python", 173 | "name": "python", 174 | "nbconvert_exporter": "python", 175 | "pygments_lexer": "ipython3", 176 | "version": "3.7.6" 177 | } 178 | }, 179 | "nbformat": 4, 180 | "nbformat_minor": 4 181 | } 182 | -------------------------------------------------------------------------------- /Code Workspace/Anagram Check.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Anagram Check\n", 8 | "\n", 9 | "## Problem\n", 10 | "\n", 11 | "Given two strings, check to see if they are anagrams. An anagram is when the two strings can be written using the exact same letters (so you can just rearrange the letters to get a different phrase or word). \n", 12 | "\n", 13 | "For example:\n", 14 | "\n", 15 | " \"public relations\" is an anagram of \"crap built on lies.\"\n", 16 | " \n", 17 | " \"clint eastwood\" is an anagram of \"old west action\"\n", 18 | " \n", 19 | "**Note: Ignore spaces and capitalization. So \"d go\" is an anagram of \"God\" and \"dog\" and \"o d g\".**\n", 20 | "\n", 21 | "## Solution\n", 22 | "\n", 23 | "Fill out your solution below:" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 4, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "def anagram(s1,s2):\n", 33 | " #method-1\n", 34 | " \n", 35 | " a1=s1.replace(' ','').lower()\n", 36 | " a2=s2.replace(' ','').lower()\n", 37 | "\n", 38 | " return sorted(a1)==sorted(a2)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 5, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "def anagram2(s1,s2):\n", 48 | " #method-2\n", 49 | " # Remove spaces and lowercase letters\n", 50 | " s1 = s1.replace(' ','').lower()\n", 51 | " s2 = s2.replace(' ','').lower()\n", 52 | " \n", 53 | " # Edge Case to check if same number of letters\n", 54 | " if len(s1) != len(s2):\n", 55 | " return False\n", 56 | " \n", 57 | " # Create counting dictionary (Note could use DefaultDict from Collections module)\n", 58 | " count = {}\n", 59 | " \n", 60 | " # Fill dictionary for first string (add counts)\n", 61 | " for letter in s1:\n", 62 | " if letter in count:\n", 63 | " count[letter] += 1\n", 64 | " else:\n", 65 | " count[letter] = 1\n", 66 | " \n", 67 | " # Fill dictionary for second string (subtract counts)\n", 68 | " for letter in s2:\n", 69 | " if letter in count:\n", 70 | " count[letter] -= 1\n", 71 | " else:\n", 72 | " count[letter] = 1\n", 73 | " \n", 74 | " # Check that all counts are 0\n", 75 | " for k in count:\n", 76 | " if count[k] != 0:\n", 77 | " return False\n", 78 | "\n", 79 | " # Otherwise they're anagrams\n", 80 | " return True" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 6, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "data": { 90 | "text/plain": [ 91 | "True" 92 | ] 93 | }, 94 | "execution_count": 6, 95 | "metadata": {}, 96 | "output_type": "execute_result" 97 | } 98 | ], 99 | "source": [ 100 | "anagram('dog','god')" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 7, 106 | "metadata": {}, 107 | "outputs": [ 108 | { 109 | "data": { 110 | "text/plain": [ 111 | "True" 112 | ] 113 | }, 114 | "execution_count": 7, 115 | "metadata": {}, 116 | "output_type": "execute_result" 117 | } 118 | ], 119 | "source": [ 120 | "anagram('clint eastwood','old west action')" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 8, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "data": { 130 | "text/plain": [ 131 | "False" 132 | ] 133 | }, 134 | "execution_count": 8, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "anagram('aa','bb')" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "# Test Your Solution\n", 148 | "Run the cell below to test your solution" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 9, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "ALL TEST CASES PASSED\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "\"\"\"\n", 166 | "RUN THIS CELL TO TEST YOUR SOLUTION\n", 167 | "\"\"\"\n", 168 | "from nose.tools import assert_equal\n", 169 | "\n", 170 | "class AnagramTest(object):\n", 171 | " \n", 172 | " def test(self,sol):\n", 173 | " assert_equal(sol('go go go','gggooo'),True)\n", 174 | " assert_equal(sol('abc','cba'),True)\n", 175 | " assert_equal(sol('hi man','hi man'),True)\n", 176 | " assert_equal(sol('aabbcc','aabbc'),False)\n", 177 | " assert_equal(sol('123','1 2'),False)\n", 178 | " print (\"ALL TEST CASES PASSED\")\n", 179 | "\n", 180 | "# Run Tests\n", 181 | "t = AnagramTest()\n", 182 | "t.test(anagram)" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 10, 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "name": "stdout", 192 | "output_type": "stream", 193 | "text": [ 194 | "ALL TEST CASES PASSED\n" 195 | ] 196 | } 197 | ], 198 | "source": [ 199 | "t.test(anagram)" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 11, 205 | "metadata": {}, 206 | "outputs": [ 207 | { 208 | "name": "stdout", 209 | "output_type": "stream", 210 | "text": [ 211 | "ALL TEST CASES PASSED\n" 212 | ] 213 | } 214 | ], 215 | "source": [ 216 | "t.test(anagram2)" 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "metadata": {}, 222 | "source": [ 223 | "# Good Job!" 224 | ] 225 | } 226 | ], 227 | "metadata": { 228 | "kernelspec": { 229 | "display_name": "Python 3", 230 | "language": "python", 231 | "name": "python3" 232 | }, 233 | "language_info": { 234 | "codemirror_mode": { 235 | "name": "ipython", 236 | "version": 3 237 | }, 238 | "file_extension": ".py", 239 | "mimetype": "text/x-python", 240 | "name": "python", 241 | "nbconvert_exporter": "python", 242 | "pygments_lexer": "ipython3", 243 | "version": "3.7.6" 244 | } 245 | }, 246 | "nbformat": 4, 247 | "nbformat_minor": 1 248 | } 249 | -------------------------------------------------------------------------------- /Code Workspace/Array Pair Sum .ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Array Pair Sum\n", 8 | "\n", 9 | "### Given an integer array, output all the ** *unique* ** pairs that sum up to a specific value **k**.\n", 10 | "\n", 11 | "input: pair_sum([1,3,2,2],4)\n", 12 | "\n", 13 | "output **2** \n" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 15, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "def pair_sum(arr,k):\n", 23 | " count=0\n", 24 | " for i in range(len(arr)):\n", 25 | " for j in range(i,len(arr)):\n", 26 | " if i==j:\n", 27 | " continue\n", 28 | " if (arr[i]+arr[j]==k):\n", 29 | " count+=1\n", 30 | " return count" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 17, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "2" 42 | ] 43 | }, 44 | "execution_count": 17, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "pair_sum([1,2,3,1],3)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "# Test Your Solution" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 18, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "ALL TEST CASES PASSED\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "\"\"\"\n", 75 | "RUN THIS CELL TO TEST YOUR SOLUTION\n", 76 | "\"\"\"\n", 77 | "from nose.tools import assert_equal\n", 78 | "\n", 79 | "class TestPair(object):\n", 80 | " \n", 81 | " def test(self,sol):\n", 82 | " assert_equal(sol([1,9,2,8,3,7,4,6,5,5,13,14,11,13,-1],10),6)\n", 83 | " assert_equal(sol([1,2,3,1],3),2)\n", 84 | " assert_equal(sol([1,3,2,2],4),2)\n", 85 | " print('ALL TEST CASES PASSED')\n", 86 | " \n", 87 | "#Run tests\n", 88 | "t = TestPair()\n", 89 | "t.test(pair_sum)\n", 90 | " " 91 | ] 92 | } 93 | ], 94 | "metadata": { 95 | "kernelspec": { 96 | "display_name": "Python 3", 97 | "language": "python", 98 | "name": "python3" 99 | }, 100 | "language_info": { 101 | "codemirror_mode": { 102 | "name": "ipython", 103 | "version": 3 104 | }, 105 | "file_extension": ".py", 106 | "mimetype": "text/x-python", 107 | "name": "python", 108 | "nbconvert_exporter": "python", 109 | "pygments_lexer": "ipython3", 110 | "version": "3.7.6" 111 | } 112 | }, 113 | "nbformat": 4, 114 | "nbformat_minor": 1 115 | } 116 | -------------------------------------------------------------------------------- /Code Workspace/Balanced Parenthesis Check.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 37, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def validateBracets(brackets):\n", 10 | " mystack = [] #we will use stack\n", 11 | " for i in brackets:\n", 12 | " if i in [\"[\",\"{\",\"(\"]: \n", 13 | " mystack.append(i)\n", 14 | " continue\n", 15 | "\n", 16 | " counterBracket = \"\"\n", 17 | " if i == ')': \n", 18 | " counterBracket = '('\n", 19 | " if i == ']':\n", 20 | " counterBracket = '['\n", 21 | " if i == '}':\n", 22 | " counterBracket = '{'\n", 23 | "\n", 24 | " if mystack[-1] == counterBracket:\n", 25 | " mystack.pop()\n", 26 | " continue\n", 27 | " else:\n", 28 | " return False\n", 29 | "\n", 30 | " if len(mystack) == 0:\n", 31 | " return True\n", 32 | " return False" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 38, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "False\n", 45 | "True\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "print(validateBracets('()(){]}'))\n", 51 | "print(validateBracets('[](){([[[]]])}'))" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [] 60 | } 61 | ], 62 | "metadata": { 63 | "kernelspec": { 64 | "display_name": "Python 3", 65 | "language": "python", 66 | "name": "python3" 67 | }, 68 | "language_info": { 69 | "codemirror_mode": { 70 | "name": "ipython", 71 | "version": 3 72 | }, 73 | "file_extension": ".py", 74 | "mimetype": "text/x-python", 75 | "name": "python", 76 | "nbconvert_exporter": "python", 77 | "pygments_lexer": "ipython3", 78 | "version": "3.7.6" 79 | } 80 | }, 81 | "nbformat": 4, 82 | "nbformat_minor": 4 83 | } 84 | -------------------------------------------------------------------------------- /Code Workspace/Balanced_array.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 14, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "#if array can split into two parts such that sum of both sub part is equal, then array is balanced! " 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 15, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "def balanced_array(arr):\n", 19 | " s=0\n", 20 | " if sum(arr)%2==0:\n", 21 | " sums=sum(arr)\n", 22 | " for i in range(len(arr)):\n", 23 | " s+=arr[i]\n", 24 | " if s==sums/2:\n", 25 | " return True\n", 26 | " return False\n", 27 | " else:\n", 28 | " return False" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 16, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "data": { 38 | "text/plain": [ 39 | "True" 40 | ] 41 | }, 42 | "execution_count": 16, 43 | "metadata": {}, 44 | "output_type": "execute_result" 45 | } 46 | ], 47 | "source": [ 48 | "arr=[1,1,1,2,1]\n", 49 | "\n", 50 | "balanced_array(arr)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [] 59 | } 60 | ], 61 | "metadata": { 62 | "kernelspec": { 63 | "display_name": "Python 3", 64 | "language": "python", 65 | "name": "python3" 66 | }, 67 | "language_info": { 68 | "codemirror_mode": { 69 | "name": "ipython", 70 | "version": 3 71 | }, 72 | "file_extension": ".py", 73 | "mimetype": "text/x-python", 74 | "name": "python", 75 | "nbconvert_exporter": "python", 76 | "pygments_lexer": "ipython3", 77 | "version": "3.7.6" 78 | } 79 | }, 80 | "nbformat": 4, 81 | "nbformat_minor": 4 82 | } 83 | -------------------------------------------------------------------------------- /Code Workspace/Count prime numbers.py: -------------------------------------------------------------------------------- 1 | def primeList(number): 2 | #storage 3 | primes = [2] 4 | #driver 5 | for num in range(3,number+1,2): 6 | #engine 7 | flag = True 8 | for i in range(2,num//2 + 1): 9 | if num%i ==0: 10 | flag = False 11 | break 12 | if flag: 13 | primes.append(num) 14 | return primes 15 | 16 | print(primeList(25)) -------------------------------------------------------------------------------- /Code Workspace/Factorial-recursive.py: -------------------------------------------------------------------------------- 1 | def fact(n): 2 | #base case 3 | if n==0: 4 | return 1 5 | return n*fact((n-1)) 6 | 7 | fact(5) -->120 -------------------------------------------------------------------------------- /Code Workspace/Fibonnaci Sequence.py: -------------------------------------------------------------------------------- 1 | def fib_rec(n): 2 | # Base Case 3 | if n == 0 or n == 1: 4 | return n 5 | # Recursion 6 | else: 7 | return fib_rec(n-2) + fib_rec(n-1) 8 | 9 | print(fib_rec(6)) 10 | -------------------------------------------------------------------------------- /Code Workspace/Find the Missing Element .ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Find the Missing Element\n", 8 | "\n", 9 | "Consider an array of non-negative integers. A second array is formed by shuffling the elements of the first array and deleting a random element. Given these two arrays, find which element is missing in the second array. \n", 10 | "\n", 11 | "Input:\n", 12 | " \n", 13 | " finder([1,2,3,4,5,6,7],[3,7,2,1,4,6])\n", 14 | "\n", 15 | "Output:\n", 16 | "\n", 17 | " 5 is the missing number" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 8, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "def finder(arr1,arr2):\n", 27 | " arr1.sort()\n", 28 | " arr2.sort()\n", 29 | " for i in range(len(arr1)):\n", 30 | " if arr1[i]!=arr2[i]:\n", 31 | " return arr1[i]\n", 32 | " return -1" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 9, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "5" 44 | ] 45 | }, 46 | "execution_count": 9, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "arr1 = [1,2,3,4,5,6,7]\n", 53 | "arr2 = [3,7,2,1,4,6]\n", 54 | "finder(arr1,arr2)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 10, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "5" 66 | ] 67 | }, 68 | "execution_count": 10, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "arr1 = [5,5,7,7]\n", 75 | "arr2 = [5,7,7]\n", 76 | "\n", 77 | "finder(arr1,arr2)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "_____" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "# Test Your Solution" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 11, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "ALL TEST CASES PASSED\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "\"\"\"\n", 109 | "RUN THIS CELL TO TEST YOUR SOLUTION\n", 110 | "\"\"\"\n", 111 | "from nose.tools import assert_equal\n", 112 | "\n", 113 | "class TestFinder(object):\n", 114 | " \n", 115 | " def test(self,sol):\n", 116 | " assert_equal(sol([5,5,7,7],[5,7,7]),5)\n", 117 | " assert_equal(sol([1,2,3,4,5,6,7],[3,7,2,1,4,6]),5)\n", 118 | " assert_equal(sol([9,8,7,6,5,4,3,2,1],[9,8,7,5,4,3,2,1]),6)\n", 119 | " print ('ALL TEST CASES PASSED')\n", 120 | "\n", 121 | "# Run test\n", 122 | "t = TestFinder()\n", 123 | "t.test(finder)" 124 | ] 125 | } 126 | ], 127 | "metadata": { 128 | "kernelspec": { 129 | "display_name": "Python 3", 130 | "language": "python", 131 | "name": "python3" 132 | }, 133 | "language_info": { 134 | "codemirror_mode": { 135 | "name": "ipython", 136 | "version": 3 137 | }, 138 | "file_extension": ".py", 139 | "mimetype": "text/x-python", 140 | "name": "python", 141 | "nbconvert_exporter": "python", 142 | "pygments_lexer": "ipython3", 143 | "version": "3.7.6" 144 | } 145 | }, 146 | "nbformat": 4, 147 | "nbformat_minor": 1 148 | } 149 | -------------------------------------------------------------------------------- /Code Workspace/Lucky number.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### A number will be lucky when if the occourance of a number is equal to the number itself in the list, return the lucky numbers,else return -1" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 44, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "def lucky_number(arr):\n", 17 | " luckynum=[]\n", 18 | " for i in range(len(arr)):\n", 19 | " if arr.count(i)==arr[i-1]:\n", 20 | " luckynum.append(arr[i-1])\n", 21 | " #print(len(a))\n", 22 | " if len(luckynum)>0:\n", 23 | " return luckynum\n", 24 | " else:\n", 25 | " return -1" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 45, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "data": { 35 | "text/plain": [ 36 | "-1" 37 | ] 38 | }, 39 | "execution_count": 45, 40 | "metadata": {}, 41 | "output_type": "execute_result" 42 | } 43 | ], 44 | "source": [ 45 | "arr=[2,3]\n", 46 | "lucky_number(arr)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 46, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | "[2, 3]" 58 | ] 59 | }, 60 | "execution_count": 46, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "arr2=[2,2,3,4,3,5,3]\n", 67 | "lucky_number(arr2)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [] 76 | } 77 | ], 78 | "metadata": { 79 | "kernelspec": { 80 | "display_name": "Python 3", 81 | "language": "python", 82 | "name": "python3" 83 | }, 84 | "language_info": { 85 | "codemirror_mode": { 86 | "name": "ipython", 87 | "version": 3 88 | }, 89 | "file_extension": ".py", 90 | "mimetype": "text/x-python", 91 | "name": "python", 92 | "nbconvert_exporter": "python", 93 | "pygments_lexer": "ipython3", 94 | "version": "3.7.6" 95 | } 96 | }, 97 | "nbformat": 4, 98 | "nbformat_minor": 4 99 | } 100 | -------------------------------------------------------------------------------- /Code Workspace/README.md: -------------------------------------------------------------------------------- 1 | ## Fundamental Python programs 2 | Fundamental programs in Python goes here. 3 | -------------------------------------------------------------------------------- /Code Workspace/String Compression .ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# String Compression\n", 8 | "\n", 9 | "## Problem\n", 10 | "\n", 11 | "Given a string in the form 'AAAABBBBCCCCCDDEEEE' compress it to become 'A4B4C5D2E4'. For this problem, you can falsely \"compress\" strings of single or double letters. For instance, it is okay for 'AAB' to return 'A2B1' even though this technically takes more space. \n", 12 | "\n", 13 | "The function should also be case sensitive, so that a string 'AAAaaa' returns 'A3a3'." 14 | ] 15 | }, 16 | { 17 | "cell_type": "markdown", 18 | "metadata": {}, 19 | "source": [ 20 | "## Solution\n", 21 | "Fill out your solution below:" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 7, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "def compress(s):\n", 31 | " d1=dict()\n", 32 | " s1=''\n", 33 | " if len(s)==0:\n", 34 | " return s\n", 35 | " if len(s)==1:\n", 36 | " return s+'1'\n", 37 | " \n", 38 | " for i in range(len(s)):\n", 39 | " if s[i] not in d1:\n", 40 | " d1[s[i]]=1\n", 41 | " else:\n", 42 | " d1[s[i]]+=1\n", 43 | " \n", 44 | " for i,j in d1.items():\n", 45 | " s1 += i+str(j)\n", 46 | " \n", 47 | " return s1" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 8, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "'A5B4C4'" 59 | ] 60 | }, 61 | "execution_count": 8, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "compress('AAAAABBBBCCCC')" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "# Test Your Solution" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 10, 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "name": "stdout", 84 | "output_type": "stream", 85 | "text": [ 86 | "ALL TEST CASES PASSED\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "\"\"\"\n", 92 | "RUN THIS CELL TO TEST YOUR SOLUTION\n", 93 | "\"\"\"\n", 94 | "from nose.tools import assert_equal\n", 95 | "\n", 96 | "class TestCompress(object):\n", 97 | "\n", 98 | " def test(self, sol):\n", 99 | " assert_equal(sol(''), '')\n", 100 | " assert_equal(sol('AABBCC'), 'A2B2C2')\n", 101 | " assert_equal(sol('AAABCCDDDDD'), 'A3B1C2D5')\n", 102 | " print('ALL TEST CASES PASSED')\n", 103 | "\n", 104 | "# Run Tests\n", 105 | "t = TestCompress()\n", 106 | "t.test(compress)" 107 | ] 108 | } 109 | ], 110 | "metadata": { 111 | "kernelspec": { 112 | "display_name": "Python 3", 113 | "language": "python", 114 | "name": "python3" 115 | }, 116 | "language_info": { 117 | "codemirror_mode": { 118 | "name": "ipython", 119 | "version": 3 120 | }, 121 | "file_extension": ".py", 122 | "mimetype": "text/x-python", 123 | "name": "python", 124 | "nbconvert_exporter": "python", 125 | "pygments_lexer": "ipython3", 126 | "version": "3.7.6" 127 | } 128 | }, 129 | "nbformat": 4, 130 | "nbformat_minor": 1 131 | } 132 | -------------------------------------------------------------------------------- /Code Workspace/Unique Characters in String.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Unique Characters in String\n", 8 | "\n", 9 | "if it is compreised of all unique characters. For example,'abcde' has all unique characters should return True. The string 'aabcde' contains duplicate characters and should return false." 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 7, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "def uni_char(s):\n", 19 | " if len(s)<=1:\n", 20 | " return True\n", 21 | " \n", 22 | " for i in range(len(s)):\n", 23 | " if s[i] in s[i+1:]:\n", 24 | " return False\n", 25 | " return True" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "# Test Your Solution" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 8, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "ALL TEST CASES PASSED\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "\"\"\"\n", 50 | "RUN THIS CELL TO TEST YOUR CODE>\n", 51 | "\"\"\"\n", 52 | "from nose.tools import assert_equal\n", 53 | "\n", 54 | "\n", 55 | "class TestUnique(object):\n", 56 | "\n", 57 | " def test(self, sol):\n", 58 | " assert_equal(sol(''), True)\n", 59 | " assert_equal(sol('goo'), False)\n", 60 | " assert_equal(sol('abcdefg'), True)\n", 61 | " print('ALL TEST CASES PASSED')\n", 62 | " \n", 63 | "# Run Tests\n", 64 | "t = TestUnique()\n", 65 | "t.test(uni_char)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": null, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [] 74 | } 75 | ], 76 | "metadata": { 77 | "kernelspec": { 78 | "display_name": "Python 3", 79 | "language": "python", 80 | "name": "python3" 81 | }, 82 | "language_info": { 83 | "codemirror_mode": { 84 | "name": "ipython", 85 | "version": 3 86 | }, 87 | "file_extension": ".py", 88 | "mimetype": "text/x-python", 89 | "name": "python", 90 | "nbconvert_exporter": "python", 91 | "pygments_lexer": "ipython3", 92 | "version": "3.7.6" 93 | } 94 | }, 95 | "nbformat": 4, 96 | "nbformat_minor": 1 97 | } 98 | -------------------------------------------------------------------------------- /PhotoGrid_1599194924746.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ayushi7rawat/Python-Bootcamp/7ab8f59eceb4c4c513b06eea5de017e2e4ff3abc/PhotoGrid_1599194924746.jpg -------------------------------------------------------------------------------- /PhotoGrid_1599195048543.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ayushi7rawat/Python-Bootcamp/7ab8f59eceb4c4c513b06eea5de017e2e4ff3abc/PhotoGrid_1599195048543.jpg -------------------------------------------------------------------------------- /PhotoGrid_1599195115056.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ayushi7rawat/Python-Bootcamp/7ab8f59eceb4c4c513b06eea5de017e2e4ff3abc/PhotoGrid_1599195115056.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Bootcamp 2 | learning to code in python 3 | -------------------------------------------------------------------------------- /Resources/Python notes.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ayushi7rawat/Python-Bootcamp/7ab8f59eceb4c4c513b06eea5de017e2e4ff3abc/Resources/Python notes.docx -------------------------------------------------------------------------------- /Resources/Python resource links file.txt: -------------------------------------------------------------------------------- 1 | PYTHON RESOURCES: 2 | 3 | 1.PHYTHON DOCUMENT: https://docs.python.org/3/ 4 | 5 | 2.Course Files for Python3 Course on Udemy:- https://github.com/Pierian-Data/Complete-Python-3-Bootcamp 6 | 7 | 3.CodingBat code practice:- http://codingbat.com/python 8 | 9 | 4.Geeks for geeks:- https://www.geeksforgeeks.org/python-3-basics/ 10 | 11 | 5.Beginner Python exercises:- http://www.practicepython.org/ 12 | 13 | 6.codechef:- https://www.codechef.com/problems/easy?sort_by=SuccessfulSubmission&sorting_order=desc 14 | 15 | 7.Leetcode:- https://leetcode.com/problemset/all/ 16 | 17 | 8.Mathematical(Harder) Practice:- https://projecteuler.net/themes/20200426/logo_default.png 18 | 19 | 9. List of Practice Problems:- http://www.codeabbey.com/index/task_list 20 | 21 | 10. A SubReddit Devoted to Daily Practice Problems: https://www.reddit.com/r/dailyprogrammer/ 22 | 23 | 11. A very tricky website with very few hints problems (Not for beginners):- http://www.pythonchallenge.com/ 24 | 25 | 12. https://solution.programmingoneonone.com/p/hackerrank-python-challenges-solutions.html 26 | 27 | 13. https://www.thepoorcoder.com/author/hackerrankdude/ 28 | 29 | 14. LEARN MACHINE LEARNING SKILLS WITH AI-Korbit :-https://korbit.ai/machinelearning?utm_source=20200217_8_kr&utm_medium=c1_fb&utm_campaign=soc 30 | 31 | 15. https://cppsecrets.com/home-page/index.php?articlecategory=3 32 | 33 | 16. https://dabeaz-course.github.io/practical-python/Notes/Contents.html -------------------------------------------------------------------------------- /Resources/PythonNotesForProfessionals.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ayushi7rawat/Python-Bootcamp/7ab8f59eceb4c4c513b06eea5de017e2e4ff3abc/Resources/PythonNotesForProfessionals.pdf -------------------------------------------------------------------------------- /Resources/beginners_python_cheat_sheet_pcc_all.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ayushi7rawat/Python-Bootcamp/7ab8f59eceb4c4c513b06eea5de017e2e4ff3abc/Resources/beginners_python_cheat_sheet_pcc_all.pdf -------------------------------------------------------------------------------- /Resources/winds of python.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ayushi7rawat/Python-Bootcamp/7ab8f59eceb4c4c513b06eea5de017e2e4ff3abc/Resources/winds of python.pdf -------------------------------------------------------------------------------- /Screenshot_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ayushi7rawat/Python-Bootcamp/7ab8f59eceb4c4c513b06eea5de017e2e4ff3abc/Screenshot_4.png -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ayushi7rawat/Python-Bootcamp/7ab8f59eceb4c4c513b06eea5de017e2e4ff3abc/demo.gif --------------------------------------------------------------------------------