├── 1_Introduction.ipynb
├── 2_1_Strings.ipynb
├── 2_2_Lists.ipynb
├── 2_3_Tuple.ipynb
├── 2_4_Dictionary.ipynb
├── 2_5_Sets.ipynb
├── 2_6ConditionalBlocks.ipynb
├── 2_7_Loops.ipynb
├── 2_8_List&Dict_Comprehension.ipynb
├── 3_1_DefiningCallingFunctions.ipynb
├── 3_2_RecursiveFunctions.ipynb
├── 3_3_LambdaFunctions.ipynb
├── 3_4_MapFilter.ipynb
├── 3_5_ReduceZipEnumerate.ipynb
├── 4_1_Files.ipynb
├── 4_2_Seek_Tell_Pickle.ipynb
├── 4_3_OSModule.ipynb
├── 4_4_Exceptions.ipynb
├── 5_1_Classes&Objects.ipynb
├── 6_1_RegularExpressions.ipynb
├── 7_1_Numpy.ipynb
├── 7_2_Pandas.ipynb
├── 7_3_VisualizationMatplotlib.ipynb
├── README.md
├── SignalProcessingandCommunication.ipynb
├── Task_DateModule_Regular Expressions.ipynb
├── Task_Diff495.ipynb
├── Task_LogicGates.ipynb
├── Task_Matplotlib_Pandas_Numpy.ipynb
├── Task_NumberGame.ipynb
├── Task_NumpyPandas.ipynb
├── Task_Practice_Problems.ipynb
├── Task_QuizandSubjective.ipynb
└── VacuumCleaner.ipynb
/2_1_Strings.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Unit2.1_Strings.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyNXB8nJRPpUioJJl88HkTFc",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "gf-uFLyFuNl0"
34 | },
35 | "source": [
36 | "#Unit 2 Python Data Structure and Flow Control\n",
37 | "**Syllabus**
\n",
38 | "String, List, Dictionary, Tuple, Sets
\n",
39 | "Slicing
\n",
40 | "Properties, operations and methods used for these data structures
\n",
41 | "Conditional block using if-else and elif, simple for loops, for loop using range, while loop, loop manipulation using continue, break, pass
\n",
42 | "List and dictionary comprehension"
43 | ]
44 | },
45 | {
46 | "cell_type": "markdown",
47 | "metadata": {
48 | "id": "eeclFw0Pv-hR"
49 | },
50 | "source": [
51 | "#Data Structure\n",
52 | "\n",
53 | "The way or a structure or a form in which the data is stored so that they can be accessed easily for further processing, maniupulation, analysis.\n"
54 | ]
55 | },
56 | {
57 | "cell_type": "markdown",
58 | "metadata": {
59 | "id": "hCN6fnLyxX8w"
60 | },
61 | "source": [
62 | "###String\n",
63 | "Strings are array of bytes representing unicode characters"
64 | ]
65 | },
66 | {
67 | "cell_type": "markdown",
68 | "metadata": {
69 | "id": "YMz9Gp3HL2X_"
70 | },
71 | "source": [
72 | "**DIY** Find out about unicode characters"
73 | ]
74 | },
75 | {
76 | "cell_type": "markdown",
77 | "metadata": {
78 | "id": "PPYAZcNXQP2B"
79 | },
80 | "source": [
81 | "####String Operators"
82 | ]
83 | },
84 | {
85 | "cell_type": "code",
86 | "metadata": {
87 | "id": "VNdgHGRruHM_"
88 | },
89 | "source": [
90 | "# + operator for concatenation\n",
91 | "first_name= input(\"Enter your first name: \") #input from user\n",
92 | "last_name= \"Munshi\"\n",
93 | "name= first_name +\" \" +last_name #string concatenation\n",
94 | "print(name)"
95 | ],
96 | "execution_count": null,
97 | "outputs": []
98 | },
99 | {
100 | "cell_type": "code",
101 | "metadata": {
102 | "id": "5s8PNjzlI3m-"
103 | },
104 | "source": [
105 | "type(name) #type of variable name"
106 | ],
107 | "execution_count": null,
108 | "outputs": []
109 | },
110 | {
111 | "cell_type": "code",
112 | "metadata": {
113 | "id": "SH5-VMEnQZFn"
114 | },
115 | "source": [
116 | "# * operator for repetition\n",
117 | "word= \"good\"\n",
118 | "n=2\n",
119 | "2*word"
120 | ],
121 | "execution_count": null,
122 | "outputs": []
123 | },
124 | {
125 | "cell_type": "code",
126 | "metadata": {
127 | "colab": {
128 | "base_uri": "https://localhost:8080/"
129 | },
130 | "id": "R7vhUq7cPC-S",
131 | "outputId": "45740f6b-7025-43e5-d1d7-cd60a6bc0b9f"
132 | },
133 | "source": [
134 | "# in operator to check if certain word is present in the string\n",
135 | "info= \"Good morning Mumbai. Welcome to this program\"\n",
136 | "if \"Mumbai\" in info:\n",
137 | " print(\"yes\")\n",
138 | "\n",
139 | "#Alternative way \n",
140 | "print(\"Mumbai\" in info)"
141 | ],
142 | "execution_count": null,
143 | "outputs": [
144 | {
145 | "output_type": "stream",
146 | "text": [
147 | "yes\n",
148 | "True\n"
149 | ],
150 | "name": "stdout"
151 | }
152 | ]
153 | },
154 | {
155 | "cell_type": "code",
156 | "metadata": {
157 | "id": "q29NZ-gEKBzG"
158 | },
159 | "source": [
160 | "#mulit line string\n",
161 | "announce= \"\"\"Hello and welcome to this program\n",
162 | "my name is Ami Munshi\"\"\" # can use single quotes also\n",
163 | "print(announce)"
164 | ],
165 | "execution_count": null,
166 | "outputs": []
167 | },
168 | {
169 | "cell_type": "markdown",
170 | "metadata": {
171 | "id": "lyvUmvgCSyeh"
172 | },
173 | "source": [
174 | "####Some basic in built function in string"
175 | ]
176 | },
177 | {
178 | "cell_type": "code",
179 | "metadata": {
180 | "colab": {
181 | "base_uri": "https://localhost:8080/",
182 | "height": 35
183 | },
184 | "id": "Iftaqry9TByR",
185 | "outputId": "c58bf0c0-9309-4231-f95c-46e5f3cb97fa"
186 | },
187 | "source": [
188 | "ord(\"a\")\n",
189 | "chr(65)\n",
190 | "len(\"fighter\")\n",
191 | "str(78)\n"
192 | ],
193 | "execution_count": null,
194 | "outputs": [
195 | {
196 | "output_type": "execute_result",
197 | "data": {
198 | "application/vnd.google.colaboratory.intrinsic+json": {
199 | "type": "string"
200 | },
201 | "text/plain": [
202 | "'78'"
203 | ]
204 | },
205 | "metadata": {
206 | "tags": []
207 | },
208 | "execution_count": 42
209 | }
210 | ]
211 | },
212 | {
213 | "cell_type": "markdown",
214 | "metadata": {
215 | "id": "j8_J32P4Tk95"
216 | },
217 | "source": [
218 | "####String Indexing and slicing"
219 | ]
220 | },
221 | {
222 | "cell_type": "code",
223 | "metadata": {
224 | "colab": {
225 | "base_uri": "https://localhost:8080/"
226 | },
227 | "id": "FWe9EQR7LvNK",
228 | "outputId": "3e711188-3264-47b8-f578-c630e8e19e87"
229 | },
230 | "source": [
231 | "#accessing string element\n",
232 | "city= \"Mumbai\"\n",
233 | "print(city[2])\n",
234 | "print (city[-1])"
235 | ],
236 | "execution_count": null,
237 | "outputs": [
238 | {
239 | "output_type": "stream",
240 | "text": [
241 | "m\n",
242 | "i\n"
243 | ],
244 | "name": "stdout"
245 | }
246 | ]
247 | },
248 | {
249 | "cell_type": "code",
250 | "metadata": {
251 | "colab": {
252 | "base_uri": "https://localhost:8080/"
253 | },
254 | "id": "eh8bW6B1Mfds",
255 | "outputId": "a8ca8101-5ff1-4321-fa3c-d17238d72c4d"
256 | },
257 | "source": [
258 | "#slicing examples\n",
259 | "data= 'Programming'\n",
260 | "#print(len(data)) #get length of the string\n",
261 | "#print(data[0:11]) #print all the characters\n",
262 | "#print(data[:])#print all the characters\n",
263 | "#print(data[::-1]) #print reverse\n",
264 | "#print(data[3:7])\n",
265 | "#print(data[0:-4])\n"
266 | ],
267 | "execution_count": null,
268 | "outputs": [
269 | {
270 | "output_type": "stream",
271 | "text": [
272 | "11\n",
273 | "Programming\n",
274 | "Programming\n",
275 | "gnimmargorP\n",
276 | "gram\n",
277 | "Program\n"
278 | ],
279 | "name": "stdout"
280 | }
281 | ]
282 | },
283 | {
284 | "cell_type": "code",
285 | "metadata": {
286 | "id": "Y9cX0yHGM33r"
287 | },
288 | "source": [
289 | "#print all the characters in the string one by one using for and in\n",
290 | "country=\"India\"\n",
291 | "for letter in country:\n",
292 | " print(letter)"
293 | ],
294 | "execution_count": null,
295 | "outputs": []
296 | },
297 | {
298 | "cell_type": "code",
299 | "metadata": {
300 | "colab": {
301 | "base_uri": "https://localhost:8080/"
302 | },
303 | "id": "PVc9IeZgOZdL",
304 | "outputId": "3d6c8684-1aba-495f-d7f4-143c43115126"
305 | },
306 | "source": [
307 | "#count number of a letters in a string\n",
308 | "river= \"mississippi\"\n",
309 | "river.count(\"s\")"
310 | ],
311 | "execution_count": null,
312 | "outputs": [
313 | {
314 | "output_type": "execute_result",
315 | "data": {
316 | "text/plain": [
317 | "4"
318 | ]
319 | },
320 | "metadata": {
321 | "tags": []
322 | },
323 | "execution_count": 25
324 | }
325 | ]
326 | },
327 | {
328 | "cell_type": "markdown",
329 | "metadata": {
330 | "id": "fAHxiv5cUfP3"
331 | },
332 | "source": [
333 | "####Inserting values in to a string"
334 | ]
335 | },
336 | {
337 | "cell_type": "code",
338 | "metadata": {
339 | "id": "ZRIuLBzaPy9B"
340 | },
341 | "source": [
342 | "#Method 1\n",
343 | "x= 6\n",
344 | "y=7\n",
345 | "z= x+y\n",
346 | "print(\"when \", x, \" is added to \", y, \" we get \", z )\n"
347 | ],
348 | "execution_count": null,
349 | "outputs": []
350 | },
351 | {
352 | "cell_type": "code",
353 | "metadata": {
354 | "colab": {
355 | "base_uri": "https://localhost:8080/"
356 | },
357 | "id": "bez8YjFQU9Vl",
358 | "outputId": "d0f0735b-d167-4961-c7da-a5a42c4ecba3"
359 | },
360 | "source": [
361 | "#Method 2- using f-string\n",
362 | "\n",
363 | "m= 6\n",
364 | "n=7\n",
365 | "o= m + n\n",
366 | "print(f'sum of {m} and {n} is {o}')"
367 | ],
368 | "execution_count": null,
369 | "outputs": [
370 | {
371 | "output_type": "stream",
372 | "text": [
373 | "sum of 6 and 7 is 13\n"
374 | ],
375 | "name": "stdout"
376 | }
377 | ]
378 | },
379 | {
380 | "cell_type": "code",
381 | "metadata": {
382 | "colab": {
383 | "base_uri": "https://localhost:8080/"
384 | },
385 | "id": "4ZSn1PjtVuBN",
386 | "outputId": "d1ec9dc5-4262-4f56-e195-5fc7f1ddcc9f"
387 | },
388 | "source": [
389 | "# Method 3 using format\n",
390 | "student= \"Raj\"\n",
391 | "marks= \"87\"\n",
392 | "subject= \"Math\"\n",
393 | "\n",
394 | "print(\"{} got {} marks in {}\".format(student, marks, subject))\n"
395 | ],
396 | "execution_count": null,
397 | "outputs": [
398 | {
399 | "output_type": "stream",
400 | "text": [
401 | "Raj got 87 marks in Math\n"
402 | ],
403 | "name": "stdout"
404 | }
405 | ]
406 | },
407 | {
408 | "cell_type": "markdown",
409 | "metadata": {
410 | "id": "Kkc028kMY64j"
411 | },
412 | "source": [
413 | "###Modifying strings"
414 | ]
415 | },
416 | {
417 | "cell_type": "code",
418 | "metadata": {
419 | "id": "pAb9aUEQXQEI"
420 | },
421 | "source": [
422 | "#String is immutable\n",
423 | "\n",
424 | "colour= \"Orange\"\n",
425 | "colour[2]=\"x\" #Replacing the letter at index 2 is not possible\n",
426 | "print(colour)"
427 | ],
428 | "execution_count": null,
429 | "outputs": []
430 | },
431 | {
432 | "cell_type": "code",
433 | "metadata": {
434 | "colab": {
435 | "base_uri": "https://localhost:8080/"
436 | },
437 | "id": "N_NSS6akZXaE",
438 | "outputId": "cbf40948-4428-4bda-84ad-4229c98736da"
439 | },
440 | "source": [
441 | "Method 1 to modify string\n",
442 | "str1= colour[0:2] +\"x\" +colour[3:]\n",
443 | "print(str1)"
444 | ],
445 | "execution_count": null,
446 | "outputs": [
447 | {
448 | "output_type": "stream",
449 | "text": [
450 | "Orxnge\n"
451 | ],
452 | "name": "stdout"
453 | }
454 | ]
455 | },
456 | {
457 | "cell_type": "code",
458 | "metadata": {
459 | "colab": {
460 | "base_uri": "https://localhost:8080/"
461 | },
462 | "id": "6FaCHf9NZuae",
463 | "outputId": "0920905d-b89a-4165-9af8-30d99eedf103"
464 | },
465 | "source": [
466 | "#Method 2- Use in built method\n",
467 | "\n",
468 | "planet= \"Saturn\"\n",
469 | "planet= planet.replace(\"t\",\"X\")\n",
470 | "print(planet)"
471 | ],
472 | "execution_count": null,
473 | "outputs": [
474 | {
475 | "output_type": "stream",
476 | "text": [
477 | "SaXurn\n"
478 | ],
479 | "name": "stdout"
480 | }
481 | ]
482 | },
483 | {
484 | "cell_type": "markdown",
485 | "metadata": {
486 | "id": "ti6ZBf7xaWxe"
487 | },
488 | "source": [
489 | "####Some useful methods in strings- **EXPLORE**\n",
490 | "\n",
491 | "capitalize()\tConverts the first character to upper case
\n",
492 | "upper() Converts all the characters in upper case
\n",
493 | "lower() Converst all the characters in lower case
\n",
494 | "casefold()\tConverts string into lower case
\n",
495 | "center()\tReturns a centered string
\n",
496 | "count()\tReturns the number of times a specified value occurs in a string
\n",
497 | "encode()\tReturns an encoded version of the string
\n",
498 | "endswith()\tReturns true if the string ends with the specified value
\n",
499 | "expandtabs()\tSets the tab size of the string
\n",
500 | "find()\tSearches the string for a specified value and returns the position of where it was found
\n",
501 | "format()\tFormats specified values in a string
\n",
502 | "format_map()\tFormats specified values in a string
\n",
503 | "index()\tSearches the string for a specified value and returns the position of where it was found
\n",
504 | "isalnum()\tReturns True if all characters in the string are alphanumeric
\n",
505 | "isalpha()\tReturns True if all characters in the string are in the alphabet
\n",
506 | "isdecimal()\tReturns True if all characters in the string are decimals
\n",
507 | "isdigit()\tReturns True if all characters in the string are digits
\n",
508 | "isidentifier()\tReturns True if the string is an identifier
\n",
509 | "islower()\tReturns True if all characters in the string are lower case
\n",
510 | "isnumeric()\tReturns True if all characters in the string are numeric
\n",
511 | "isprintable()\tReturns True if all characters in the string are printable
\n",
512 | "isspace()\tReturns True if all characters in the string are whitespaces
"
513 | ]
514 | },
515 | {
516 | "cell_type": "code",
517 | "metadata": {
518 | "id": "pJ278go7aO9Q"
519 | },
520 | "source": [
521 | "#Examples\n",
522 | "name= \"jiya\"\n",
523 | "print(name.upper())\n",
524 | "print(\"Good Morning\".isspace())\n",
525 | "print(\" \".isspace())\n",
526 | "print(\"123\".isdigit())"
527 | ],
528 | "execution_count": null,
529 | "outputs": []
530 | },
531 | {
532 | "cell_type": "markdown",
533 | "metadata": {
534 | "id": "OZeuTZ9NccjP"
535 | },
536 | "source": [
537 | "####Use of escape character \\- **EXPLORE**\n",
538 | "\n",
539 | "\n",
540 | "\\'\tSingle Quote\t
\n",
541 | "\\\\\tBackslash\t
\n",
542 | "\\n\tNew Line\t
\n",
543 | "\\r\tCarriage Return
\t\n",
544 | "\\t\tTab\t
\n",
545 | "\\b\tBackspace
\t\n",
546 | "\\f\tForm Feed\t
\n",
547 | "\\ooo\tOctal value\t
\n",
548 | "\\xhh\tHex value
"
549 | ]
550 | },
551 | {
552 | "cell_type": "code",
553 | "metadata": {
554 | "id": "CZ3NA-2_bTT1"
555 | },
556 | "source": [
557 | "#Some examples\n",
558 | "print(\"My am a \\\"Champion\\\" in this game\") # use of double quotes within a text\n"
559 | ],
560 | "execution_count": null,
561 | "outputs": []
562 | },
563 | {
564 | "cell_type": "code",
565 | "metadata": {
566 | "id": "mEN_G7Ooc80e"
567 | },
568 | "source": [
569 | "print(\"You go to school \\ndo your homework in the evening\") #use of new line character"
570 | ],
571 | "execution_count": null,
572 | "outputs": []
573 | },
574 | {
575 | "cell_type": "markdown",
576 | "metadata": {
577 | "id": "YlO2q194es3t"
578 | },
579 | "source": [
580 | "####String to List and Back to string"
581 | ]
582 | },
583 | {
584 | "cell_type": "code",
585 | "metadata": {
586 | "colab": {
587 | "base_uri": "https://localhost:8080/"
588 | },
589 | "id": "BPcN_8KTdK_H",
590 | "outputId": "e2db0310-45c8-481c-e68f-43ce0aeea084"
591 | },
592 | "source": [
593 | "sentence= \"We all like Python programming\"\n",
594 | "print(sentence)\n",
595 | "new= sentence.split(\" \")\n",
596 | "print(new)\n",
597 | "sentence_again= \" \".join(new)\n",
598 | "print(sentence_again)"
599 | ],
600 | "execution_count": null,
601 | "outputs": [
602 | {
603 | "output_type": "stream",
604 | "text": [
605 | "We all like Python programming\n",
606 | "['We', 'all', 'like', 'Python', 'programming']\n",
607 | "We all like Python programming\n"
608 | ],
609 | "name": "stdout"
610 | }
611 | ]
612 | },
613 | {
614 | "cell_type": "code",
615 | "metadata": {
616 | "id": "I2dw1nDBe_Zw"
617 | },
618 | "source": [
619 | ""
620 | ],
621 | "execution_count": null,
622 | "outputs": []
623 | }
624 | ]
625 | }
626 |
--------------------------------------------------------------------------------
/2_2_Lists.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Unit2.2_Lists.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyPLwCCU6Cw19NRo+jaBz5BK",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "qoMHi9O2f5bs"
34 | },
35 | "source": [
36 | "### List\n",
37 | "It is collection of various elements. Used to store multiple items. Lists are **mutable**"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "metadata": {
43 | "id": "Dx0qKDXDf1Vw"
44 | },
45 | "source": [
46 | "#Example\n",
47 | "vegetables= [\"Beans\", \"Pepper\", \"Cabbage\"]\n",
48 | "student_info= [\"Jeet\", 89]"
49 | ],
50 | "execution_count": null,
51 | "outputs": []
52 | },
53 | {
54 | "cell_type": "markdown",
55 | "metadata": {
56 | "id": "nDp-5x5Lj9rg"
57 | },
58 | "source": [
59 | "####Lists are ordered\n",
60 | "Every element has a fixed index in a list"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "metadata": {
66 | "id": "MxWXwJYGkEIG"
67 | },
68 | "source": [
69 | "#Example\n",
70 | "class1= [\"Jeet\", \"Jay\", \"Joe\"]\n",
71 | "class2= [\"Joe\", \"Jeet\", \"Jay\"]\n",
72 | "class1==class2 #Although elements of both the lists are same, order of the elements are different. Hence comparison gives false output"
73 | ],
74 | "execution_count": null,
75 | "outputs": []
76 | },
77 | {
78 | "cell_type": "markdown",
79 | "metadata": {
80 | "id": "uKTxfuAWlbNc"
81 | },
82 | "source": [
83 | "####Can add, remove, replace items from a list\n"
84 | ]
85 | },
86 | {
87 | "cell_type": "code",
88 | "metadata": {
89 | "colab": {
90 | "base_uri": "https://localhost:8080/"
91 | },
92 | "id": "k21FV_NXjodm",
93 | "outputId": "ee45a096-7b9d-4676-fa82-edeaf7d2de0a"
94 | },
95 | "source": [
96 | "colours= [\"Red\", \"Green\"]\n",
97 | "colours.append(\"Blue\") #adds the argument as a single element\n",
98 | "print(colours)"
99 | ],
100 | "execution_count": null,
101 | "outputs": [
102 | {
103 | "output_type": "stream",
104 | "text": [
105 | "['Red', 'Green', 'Blue']\n"
106 | ],
107 | "name": "stdout"
108 | }
109 | ]
110 | },
111 | {
112 | "cell_type": "code",
113 | "metadata": {
114 | "colab": {
115 | "base_uri": "https://localhost:8080/"
116 | },
117 | "id": "mGQAoAZSlslJ",
118 | "outputId": "34b1c528-49a0-4d1f-cb6d-e16dc88c8caf"
119 | },
120 | "source": [
121 | "colours.extend(\"white\")\n",
122 | "print(colours) # Adds the argument by iterarating over it"
123 | ],
124 | "execution_count": null,
125 | "outputs": [
126 | {
127 | "output_type": "stream",
128 | "text": [
129 | "['Red', 'Green', 'Blue', 'w', 'h', 'i', 't', 'e']\n"
130 | ],
131 | "name": "stdout"
132 | }
133 | ]
134 | },
135 | {
136 | "cell_type": "code",
137 | "metadata": {
138 | "colab": {
139 | "base_uri": "https://localhost:8080/"
140 | },
141 | "id": "3FAWeemFl9LP",
142 | "outputId": "4b9c3e40-fe20-4255-e0cc-5a170d290390"
143 | },
144 | "source": [
145 | "#Element in the list replaced by another element\n",
146 | "colours[1]= \"purple\"\n",
147 | "print(colours)"
148 | ],
149 | "execution_count": null,
150 | "outputs": [
151 | {
152 | "output_type": "stream",
153 | "text": [
154 | "['Red', 'purple', 'Blue', 'w', 'h', 'i', 't', 'e']\n"
155 | ],
156 | "name": "stdout"
157 | }
158 | ]
159 | },
160 | {
161 | "cell_type": "code",
162 | "metadata": {
163 | "colab": {
164 | "base_uri": "https://localhost:8080/"
165 | },
166 | "id": "q0l4Kedwmdp2",
167 | "outputId": "32120d10-668c-40c0-8536-8a5647a73f2e"
168 | },
169 | "source": [
170 | "colours.pop(1) #removesthe element at index 1\n",
171 | "print(colours)"
172 | ],
173 | "execution_count": null,
174 | "outputs": [
175 | {
176 | "output_type": "stream",
177 | "text": [
178 | "['Red', 'Blue', 'w', 'h', 'i', 't', 'e']\n"
179 | ],
180 | "name": "stdout"
181 | }
182 | ]
183 | },
184 | {
185 | "cell_type": "markdown",
186 | "metadata": {
187 | "id": "7NCdI-crnkuP"
188 | },
189 | "source": [
190 | "####List Indexing and Slicing"
191 | ]
192 | },
193 | {
194 | "cell_type": "code",
195 | "metadata": {
196 | "colab": {
197 | "base_uri": "https://localhost:8080/"
198 | },
199 | "id": "5pMHbVlym3aO",
200 | "outputId": "16a7ffc9-f2c0-4e16-d5f2-5b669daa23f0"
201 | },
202 | "source": [
203 | "stationery=[\"pencil\", \"eraser\", \"scale\", \"papers\", \"gluestick\", \"sharpner\"]\n",
204 | "print(stationery[0:3])\n",
205 | "print(stationery[2:4])\n",
206 | "\n",
207 | "#Apply slicing similar to string"
208 | ],
209 | "execution_count": null,
210 | "outputs": [
211 | {
212 | "output_type": "stream",
213 | "text": [
214 | "['pencil', 'eraser', 'scale']\n",
215 | "['scale', 'papers']\n"
216 | ],
217 | "name": "stdout"
218 | }
219 | ]
220 | },
221 | {
222 | "cell_type": "markdown",
223 | "metadata": {
224 | "id": "5RbP7jhDpQOT"
225 | },
226 | "source": [
227 | "###Concatenation and replication"
228 | ]
229 | },
230 | {
231 | "cell_type": "code",
232 | "metadata": {
233 | "colab": {
234 | "base_uri": "https://localhost:8080/"
235 | },
236 | "id": "WmhWGkvvpbh3",
237 | "outputId": "4b7e8b55-4570-4c1e-abdf-478a8bcb5e9b"
238 | },
239 | "source": [
240 | "vowels= [\"a\", \"e\", \"i\"]\n",
241 | "vowels=vowels+[\"o\", \"u\"] #concatenation\n",
242 | "print(vowels)"
243 | ],
244 | "execution_count": null,
245 | "outputs": [
246 | {
247 | "output_type": "stream",
248 | "text": [
249 | "['a', 'e', 'i', 'o', 'u']\n"
250 | ],
251 | "name": "stdout"
252 | }
253 | ]
254 | },
255 | {
256 | "cell_type": "code",
257 | "metadata": {
258 | "colab": {
259 | "base_uri": "https://localhost:8080/"
260 | },
261 | "id": "7R3JnuzGpqt8",
262 | "outputId": "0bf48b55-03a8-44b8-bef5-b0a69c31be22"
263 | },
264 | "source": [
265 | "vowels= 2*vowels\n",
266 | "print(vowels)"
267 | ],
268 | "execution_count": null,
269 | "outputs": [
270 | {
271 | "output_type": "stream",
272 | "text": [
273 | "['a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u']\n"
274 | ],
275 | "name": "stdout"
276 | }
277 | ]
278 | },
279 | {
280 | "cell_type": "markdown",
281 | "metadata": {
282 | "id": "aQygyGOKqQBc"
283 | },
284 | "source": [
285 | "len(), min(), max(), sorted functions"
286 | ]
287 | },
288 | {
289 | "cell_type": "code",
290 | "metadata": {
291 | "id": "VKlx6ltXoM70"
292 | },
293 | "source": [
294 | "countries= [\"India\", \"Canada\", \"New Zealand\", \"Australia\", \"United Kingdom\"]\n",
295 | "print(len(countries))\n",
296 | "print(max(countries))\n",
297 | "print(min(countries))\n",
298 | "print(sorted(countries))\n",
299 | "print(countries)"
300 | ],
301 | "execution_count": null,
302 | "outputs": []
303 | },
304 | {
305 | "cell_type": "code",
306 | "metadata": {
307 | "id": "oaaE2hH0rP_c"
308 | },
309 | "source": [
310 | "countries.sort()\n",
311 | "print(countries)\n"
312 | ],
313 | "execution_count": null,
314 | "outputs": []
315 | },
316 | {
317 | "cell_type": "markdown",
318 | "metadata": {
319 | "id": "YE7mPNsWrzH5"
320 | },
321 | "source": [
322 | "####Nested Lists"
323 | ]
324 | },
325 | {
326 | "cell_type": "code",
327 | "metadata": {
328 | "colab": {
329 | "base_uri": "https://localhost:8080/"
330 | },
331 | "id": "TcA1ELyJoon-",
332 | "outputId": "66c52bbc-5fb9-4f8f-8ef4-1a9f130c96ad"
333 | },
334 | "source": [
335 | "my_list= [\"shoes\", [\"potato\", \"onions\"], [[\"books\", \"papers\"], \"pencils\", \"gluestick\"], \"bread\"]\n",
336 | "print(len(my_list))"
337 | ],
338 | "execution_count": null,
339 | "outputs": [
340 | {
341 | "output_type": "stream",
342 | "text": [
343 | "4\n"
344 | ],
345 | "name": "stdout"
346 | }
347 | ]
348 | },
349 | {
350 | "cell_type": "code",
351 | "metadata": {
352 | "colab": {
353 | "base_uri": "https://localhost:8080/"
354 | },
355 | "id": "uwa-z2iL5pSq",
356 | "outputId": "3b5df7c2-859b-41d7-e1bb-d95bc700ae90"
357 | },
358 | "source": [
359 | "print(my_list[0], my_list[3])"
360 | ],
361 | "execution_count": null,
362 | "outputs": [
363 | {
364 | "output_type": "stream",
365 | "text": [
366 | "shoes bread\n"
367 | ],
368 | "name": "stdout"
369 | }
370 | ]
371 | },
372 | {
373 | "cell_type": "code",
374 | "metadata": {
375 | "colab": {
376 | "base_uri": "https://localhost:8080/"
377 | },
378 | "id": "zoFsCZ9450ZO",
379 | "outputId": "7103399b-0655-43d9-dfe9-7c558ce897cb"
380 | },
381 | "source": [
382 | "print(my_list[1])"
383 | ],
384 | "execution_count": null,
385 | "outputs": [
386 | {
387 | "output_type": "stream",
388 | "text": [
389 | "['potato', 'onions']\n"
390 | ],
391 | "name": "stdout"
392 | }
393 | ]
394 | },
395 | {
396 | "cell_type": "code",
397 | "metadata": {
398 | "colab": {
399 | "base_uri": "https://localhost:8080/"
400 | },
401 | "id": "ErhZiEK56H1b",
402 | "outputId": "cbd85768-a1ed-49d1-b250-5ba4c3fe63dc"
403 | },
404 | "source": [
405 | "print(my_list[1][1])"
406 | ],
407 | "execution_count": null,
408 | "outputs": [
409 | {
410 | "output_type": "stream",
411 | "text": [
412 | "onions\n"
413 | ],
414 | "name": "stdout"
415 | }
416 | ]
417 | },
418 | {
419 | "cell_type": "code",
420 | "metadata": {
421 | "colab": {
422 | "base_uri": "https://localhost:8080/"
423 | },
424 | "id": "aoMjLQa76AL2",
425 | "outputId": "e90f34f5-d79c-45f7-edc2-f38f87991b9a"
426 | },
427 | "source": [
428 | "print(my_list[2])"
429 | ],
430 | "execution_count": null,
431 | "outputs": [
432 | {
433 | "output_type": "stream",
434 | "text": [
435 | "[['books', 'papers'], 'pencils', 'gluestick']\n"
436 | ],
437 | "name": "stdout"
438 | }
439 | ]
440 | },
441 | {
442 | "cell_type": "code",
443 | "metadata": {
444 | "colab": {
445 | "base_uri": "https://localhost:8080/"
446 | },
447 | "id": "rNziub516Cx6",
448 | "outputId": "cc910e54-bc80-4a4a-fbaf-cdc2eb82e139"
449 | },
450 | "source": [
451 | "print(my_list[2][0])"
452 | ],
453 | "execution_count": null,
454 | "outputs": [
455 | {
456 | "output_type": "stream",
457 | "text": [
458 | "['books', 'papers']\n"
459 | ],
460 | "name": "stdout"
461 | }
462 | ]
463 | },
464 | {
465 | "cell_type": "code",
466 | "metadata": {
467 | "colab": {
468 | "base_uri": "https://localhost:8080/"
469 | },
470 | "id": "YLUC5i_w6UQZ",
471 | "outputId": "f4fe161a-9d40-4924-c09a-f5656004328a"
472 | },
473 | "source": [
474 | "print(my_list[2][0][1])"
475 | ],
476 | "execution_count": null,
477 | "outputs": [
478 | {
479 | "output_type": "stream",
480 | "text": [
481 | "papers\n"
482 | ],
483 | "name": "stdout"
484 | }
485 | ]
486 | },
487 | {
488 | "cell_type": "markdown",
489 | "metadata": {
490 | "id": "wQ5lVe7bGQMg"
491 | },
492 | "source": [
493 | "###Copying a list"
494 | ]
495 | },
496 | {
497 | "cell_type": "code",
498 | "metadata": {
499 | "id": "UKJYTmuTFfmx"
500 | },
501 | "source": [
502 | ""
503 | ],
504 | "execution_count": null,
505 | "outputs": []
506 | }
507 | ]
508 | }
509 |
--------------------------------------------------------------------------------
/2_3_Tuple.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Unit2.3_Tuple.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyP3otsUg8AnmxwbIzk6f+sq",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "FN1yikAp82TF"
34 | },
35 | "source": [
36 | "#Tuples\n",
37 | "\n",
38 | "Similar to list. Main difference is that Tuplesare immutable"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "metadata": {
44 | "colab": {
45 | "base_uri": "https://localhost:8080/"
46 | },
47 | "id": "4jKeXAv174VH",
48 | "outputId": "792ab95f-3559-4842-fdb5-7116845a6151"
49 | },
50 | "source": [
51 | "my_tuple= (\"banana\", \"apple\", \"grapes\")#Packing Tuple\n",
52 | "print(my_tuple)\n",
53 | "print(type(my_tuple))"
54 | ],
55 | "execution_count": null,
56 | "outputs": [
57 | {
58 | "output_type": "stream",
59 | "text": [
60 | "('banana', 'apple', 'grapes')\n",
61 | "\n"
62 | ],
63 | "name": "stdout"
64 | }
65 | ]
66 | },
67 | {
68 | "cell_type": "code",
69 | "metadata": {
70 | "colab": {
71 | "base_uri": "https://localhost:8080/",
72 | "height": 35
73 | },
74 | "id": "ec9kd5sr9Nqw",
75 | "outputId": "6b492ea0-1610-43b2-80af-89b96e90155a"
76 | },
77 | "source": [
78 | "my_tuple[1]"
79 | ],
80 | "execution_count": null,
81 | "outputs": [
82 | {
83 | "output_type": "execute_result",
84 | "data": {
85 | "application/vnd.google.colaboratory.intrinsic+json": {
86 | "type": "string"
87 | },
88 | "text/plain": [
89 | "'apple'"
90 | ]
91 | },
92 | "metadata": {
93 | "tags": []
94 | },
95 | "execution_count": 2
96 | }
97 | ]
98 | },
99 | {
100 | "cell_type": "markdown",
101 | "metadata": {
102 | "id": "wgC5vi3C_2S0"
103 | },
104 | "source": [
105 | "####Tuple methods count(),index()"
106 | ]
107 | },
108 | {
109 | "cell_type": "code",
110 | "metadata": {
111 | "colab": {
112 | "base_uri": "https://localhost:8080/"
113 | },
114 | "id": "V_7njpBH9P4s",
115 | "outputId": "dd163571-c3c8-40ab-83fb-9ef1ae0048e7"
116 | },
117 | "source": [
118 | "my_tuple.count(\"banana\")"
119 | ],
120 | "execution_count": null,
121 | "outputs": [
122 | {
123 | "output_type": "execute_result",
124 | "data": {
125 | "text/plain": [
126 | "1"
127 | ]
128 | },
129 | "metadata": {
130 | "tags": []
131 | },
132 | "execution_count": 4
133 | }
134 | ]
135 | },
136 | {
137 | "cell_type": "code",
138 | "metadata": {
139 | "colab": {
140 | "base_uri": "https://localhost:8080/"
141 | },
142 | "id": "eNJZThXI9ZI0",
143 | "outputId": "7ba16f4e-f4cb-4b17-bd73-9341ac311f69"
144 | },
145 | "source": [
146 | "my_tuple.index(\"grapes\")"
147 | ],
148 | "execution_count": null,
149 | "outputs": [
150 | {
151 | "output_type": "execute_result",
152 | "data": {
153 | "text/plain": [
154 | "2"
155 | ]
156 | },
157 | "metadata": {
158 | "tags": []
159 | },
160 | "execution_count": 6
161 | }
162 | ]
163 | },
164 | {
165 | "cell_type": "code",
166 | "metadata": {
167 | "id": "AMvT_PAC9icE"
168 | },
169 | "source": [
170 | "new= (\"food\", (\"shirts\", \"Jeans\"), ((\"sprite\", \"coke\"), \"mangola\")) #Nested Tuple"
171 | ],
172 | "execution_count": null,
173 | "outputs": []
174 | },
175 | {
176 | "cell_type": "code",
177 | "metadata": {
178 | "colab": {
179 | "base_uri": "https://localhost:8080/"
180 | },
181 | "id": "ZYvfSO0797-b",
182 | "outputId": "d7ae645c-70de-46ee-9aad-6a351d935af6"
183 | },
184 | "source": [
185 | "print(new[1])"
186 | ],
187 | "execution_count": null,
188 | "outputs": [
189 | {
190 | "output_type": "stream",
191 | "text": [
192 | "('shirts', 'Jeans')\n"
193 | ],
194 | "name": "stdout"
195 | }
196 | ]
197 | },
198 | {
199 | "cell_type": "code",
200 | "metadata": {
201 | "colab": {
202 | "base_uri": "https://localhost:8080/"
203 | },
204 | "id": "ra-wWvCS9--d",
205 | "outputId": "0cf77b53-587d-47e2-d32d-60167091bdaa"
206 | },
207 | "source": [
208 | "print(new[1][1])"
209 | ],
210 | "execution_count": null,
211 | "outputs": [
212 | {
213 | "output_type": "stream",
214 | "text": [
215 | "Jeans\n"
216 | ],
217 | "name": "stdout"
218 | }
219 | ]
220 | },
221 | {
222 | "cell_type": "code",
223 | "metadata": {
224 | "colab": {
225 | "base_uri": "https://localhost:8080/"
226 | },
227 | "id": "WmPPvDIj-CM7",
228 | "outputId": "ccf3b157-3576-44d0-8f90-b4b90544407f"
229 | },
230 | "source": [
231 | "print(new[2][0][1])"
232 | ],
233 | "execution_count": null,
234 | "outputs": [
235 | {
236 | "output_type": "stream",
237 | "text": [
238 | "coke\n"
239 | ],
240 | "name": "stdout"
241 | }
242 | ]
243 | },
244 | {
245 | "cell_type": "markdown",
246 | "metadata": {
247 | "id": "MrXwjJxE_BHf"
248 | },
249 | "source": [
250 | "####Unpacking Tuple"
251 | ]
252 | },
253 | {
254 | "cell_type": "code",
255 | "metadata": {
256 | "id": "qAjW2yvk-G-7",
257 | "colab": {
258 | "base_uri": "https://localhost:8080/"
259 | },
260 | "outputId": "f99cece3-b61e-4422-9a44-d80e72033ba7"
261 | },
262 | "source": [
263 | "(a,b,c)= my_tuple\n",
264 | "print(my_tuple)\n",
265 | "print(b)"
266 | ],
267 | "execution_count": null,
268 | "outputs": [
269 | {
270 | "output_type": "stream",
271 | "text": [
272 | "('banana', 'apple', 'grapes')\n",
273 | "apple\n"
274 | ],
275 | "name": "stdout"
276 | }
277 | ]
278 | },
279 | {
280 | "cell_type": "markdown",
281 | "metadata": {
282 | "id": "7WsULLwi_S80"
283 | },
284 | "source": [
285 | "####Join two tuples"
286 | ]
287 | },
288 | {
289 | "cell_type": "code",
290 | "metadata": {
291 | "colab": {
292 | "base_uri": "https://localhost:8080/"
293 | },
294 | "id": "z1eBPj5O_Hmd",
295 | "outputId": "86892109-8525-4bd9-8a3f-236e8a2fd102"
296 | },
297 | "source": [
298 | "my_tuple + (\"mangoes\", \"banana\")"
299 | ],
300 | "execution_count": null,
301 | "outputs": [
302 | {
303 | "output_type": "execute_result",
304 | "data": {
305 | "text/plain": [
306 | "('banana', 'apple', 'grapes', 'mangoes', 'banana')"
307 | ]
308 | },
309 | "metadata": {
310 | "tags": []
311 | },
312 | "execution_count": 14
313 | }
314 | ]
315 | },
316 | {
317 | "cell_type": "markdown",
318 | "metadata": {
319 | "id": "mFkMfsm-_lSl"
320 | },
321 | "source": [
322 | "####Multiply Tuples\n",
323 | "\n"
324 | ]
325 | },
326 | {
327 | "cell_type": "code",
328 | "metadata": {
329 | "colab": {
330 | "base_uri": "https://localhost:8080/"
331 | },
332 | "id": "Is0Nrwza_fDd",
333 | "outputId": "0d45e0f6-9d67-4db8-c8f5-a854c91cb3dc"
334 | },
335 | "source": [
336 | "my_tuple*2\n"
337 | ],
338 | "execution_count": null,
339 | "outputs": [
340 | {
341 | "output_type": "execute_result",
342 | "data": {
343 | "text/plain": [
344 | "('banana', 'apple', 'grapes', 'banana', 'apple', 'grapes')"
345 | ]
346 | },
347 | "metadata": {
348 | "tags": []
349 | },
350 | "execution_count": 15
351 | }
352 | ]
353 | },
354 | {
355 | "cell_type": "markdown",
356 | "metadata": {
357 | "id": "6C2y7XzCPZUh"
358 | },
359 | "source": [
360 | "Tuple to list"
361 | ]
362 | },
363 | {
364 | "cell_type": "code",
365 | "metadata": {
366 | "id": "NZ-ULC5Q_uEs"
367 | },
368 | "source": [
369 | "#DIY\n",
370 | "#Convert tuple to list\n",
371 | "\n",
372 | "mix_tuple= (1,\"vegetables\", [\"yellow\", \"banana\"])"
373 | ],
374 | "execution_count": null,
375 | "outputs": []
376 | },
377 | {
378 | "cell_type": "code",
379 | "metadata": {
380 | "id": "Pd3CidUcPtrO"
381 | },
382 | "source": [
383 | "#DIY\n",
384 | "#from the given tuple, print yellow using indexing"
385 | ],
386 | "execution_count": null,
387 | "outputs": []
388 | },
389 | {
390 | "cell_type": "code",
391 | "metadata": {
392 | "id": "pFqS3s5ZP1Xs",
393 | "colab": {
394 | "base_uri": "https://localhost:8080/"
395 | },
396 | "outputId": "11004c4a-52bb-4194-e99d-8174c9c07aa0"
397 | },
398 | "source": [
399 | "#Student names are saved in a tuple. Find the length of each student name.\n",
400 | "names= (\"Jiya\", \"Jeet\", \"Mahek\", \"Aarya\", \"Ahaan\")\n",
401 | "#iterating over tuple \n",
402 | "for name in names:\n",
403 | " n= len(name) # find length of each element in the tuple\n",
404 | " print(name,n) #print the name and its length"
405 | ],
406 | "execution_count": null,
407 | "outputs": [
408 | {
409 | "output_type": "stream",
410 | "text": [
411 | "Jiya 4\n",
412 | "Jeet 4\n",
413 | "Mahek 5\n",
414 | "Aarya 5\n",
415 | "Ahaan 5\n"
416 | ],
417 | "name": "stdout"
418 | }
419 | ]
420 | },
421 | {
422 | "cell_type": "code",
423 | "metadata": {
424 | "id": "bqn81Uc7QKh3"
425 | },
426 | "source": [
427 | ""
428 | ],
429 | "execution_count": null,
430 | "outputs": []
431 | }
432 | ]
433 | }
434 |
--------------------------------------------------------------------------------
/2_4_Dictionary.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Unit2.4_Dictionary.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyPVtgwLBK55o1ZeXlP2YxqF",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "Ku1Fte3HA4SW"
34 | },
35 | "source": [
36 | "\n",
37 | "#Dictionary\n",
38 | "It is mutable. We can change, add and delete the key-value pair.Order does not matter. However their is a unique key associated with each value. Key can be any of Python's immutable data types (Boolean, String, Integer, Float and others)\n",
39 | "\n",
40 | "\n",
41 | " "
42 | ]
43 | },
44 | {
45 | "cell_type": "code",
46 | "metadata": {
47 | "id": "k_RVEZ1n-Yau"
48 | },
49 | "source": [
50 | "# Create dictionary Method 1\n",
51 | "dict_1= {\"Rohan\": 5, \"Mahesh\":7} #creating dictionary\n",
52 | "dict_2= {\"Mahesh\":7,\"Rohan\": 5}\n",
53 | "dict_1==dict_2"
54 | ],
55 | "execution_count": null,
56 | "outputs": []
57 | },
58 | {
59 | "cell_type": "code",
60 | "metadata": {
61 | "id": "UMIVHECGBPHO"
62 | },
63 | "source": [
64 | "!python --version"
65 | ],
66 | "execution_count": null,
67 | "outputs": []
68 | },
69 | {
70 | "cell_type": "code",
71 | "metadata": {
72 | "id": "Gy-57c76BeO9"
73 | },
74 | "source": [
75 | "dict_1[\"Mahesh\"] # access value of dict_1 at key \"Mahesh\""
76 | ],
77 | "execution_count": null,
78 | "outputs": []
79 | },
80 | {
81 | "cell_type": "code",
82 | "metadata": {
83 | "id": "SdhctL3QCI93"
84 | },
85 | "source": [
86 | "# Create dictionary Method 2 using dict()\n",
87 | "\n",
88 | "my_class= dict({\"Geeta\": 29, \"Reena\": 30, \"Priya\":14})\n",
89 | "print(my_class)"
90 | ],
91 | "execution_count": null,
92 | "outputs": []
93 | },
94 | {
95 | "cell_type": "code",
96 | "metadata": {
97 | "id": "43QWY7ePoD-k"
98 | },
99 | "source": [
100 | "my_fruit = dict([(1,'apple'), (2,'mango')])\n",
101 | "print(my_fruit)"
102 | ],
103 | "execution_count": null,
104 | "outputs": []
105 | },
106 | {
107 | "cell_type": "code",
108 | "metadata": {
109 | "id": "2p6GD_VasvH6",
110 | "colab": {
111 | "base_uri": "https://localhost:8080/"
112 | },
113 | "outputId": "be51474f-e519-41c6-d385-8bcfa0d0aeed"
114 | },
115 | "source": [
116 | "#One more example of dictionary\n",
117 | "\n",
118 | "dict_new= {1: (\"Red\", \"Blue\"), (2,3): \"Green\", \"My_list\":[1,2,3,4]} #Keys and values can be of any data type\n",
119 | "print(dict_new)"
120 | ],
121 | "execution_count": null,
122 | "outputs": [
123 | {
124 | "output_type": "stream",
125 | "text": [
126 | "{1: ('Red', 'Blue'), (2, 3): 'Green', 'My_list': [1, 2, 3, 4]}\n"
127 | ],
128 | "name": "stdout"
129 | }
130 | ]
131 | },
132 | {
133 | "cell_type": "markdown",
134 | "metadata": {
135 | "id": "g79zsS68qj6U"
136 | },
137 | "source": [
138 | "####Accessing Dictionary"
139 | ]
140 | },
141 | {
142 | "cell_type": "code",
143 | "metadata": {
144 | "id": "9mBDmNTUp-7W"
145 | },
146 | "source": [
147 | "dict1= {\"India\": 30, \"Russia\":35, \"Britain\": 25} #create a dictionary\n",
148 | "print(dict1)"
149 | ],
150 | "execution_count": null,
151 | "outputs": []
152 | },
153 | {
154 | "cell_type": "code",
155 | "metadata": {
156 | "colab": {
157 | "base_uri": "https://localhost:8080/"
158 | },
159 | "id": "z_ux3uL4qtHT",
160 | "outputId": "08aa2c11-7372-465f-87d0-f3b79cc30bae"
161 | },
162 | "source": [
163 | "print(dict1.keys()) #Print keys of dictionary"
164 | ],
165 | "execution_count": null,
166 | "outputs": [
167 | {
168 | "output_type": "stream",
169 | "text": [
170 | "dict_keys(['India', 'Russia', 'Britain'])\n"
171 | ],
172 | "name": "stdout"
173 | }
174 | ]
175 | },
176 | {
177 | "cell_type": "code",
178 | "metadata": {
179 | "id": "HVH3axdIq2FW"
180 | },
181 | "source": [
182 | "for key in dict1.keys():\n",
183 | " print(key) #Print individual keys"
184 | ],
185 | "execution_count": null,
186 | "outputs": []
187 | },
188 | {
189 | "cell_type": "code",
190 | "metadata": {
191 | "colab": {
192 | "base_uri": "https://localhost:8080/"
193 | },
194 | "id": "boayp_QlrEj-",
195 | "outputId": "9f6ffaa6-caa4-4dc8-828a-6953a2d6e55b"
196 | },
197 | "source": [
198 | "print(dict1.items()) #print key-value pair"
199 | ],
200 | "execution_count": null,
201 | "outputs": [
202 | {
203 | "output_type": "stream",
204 | "text": [
205 | "dict_items([('India', 30), ('Russia', 35), ('Britain', 25)])\n"
206 | ],
207 | "name": "stdout"
208 | }
209 | ]
210 | },
211 | {
212 | "cell_type": "code",
213 | "metadata": {
214 | "id": "ryqtJbL0rKem"
215 | },
216 | "source": [
217 | "for item in dict1.items():\n",
218 | " print(item) #print individual key-value pair"
219 | ],
220 | "execution_count": null,
221 | "outputs": []
222 | },
223 | {
224 | "cell_type": "code",
225 | "metadata": {
226 | "id": "oDljP2v7rQO9"
227 | },
228 | "source": [
229 | "for item in dict1.items():\n",
230 | " print(item[1]) #Print only values"
231 | ],
232 | "execution_count": null,
233 | "outputs": []
234 | },
235 | {
236 | "cell_type": "code",
237 | "metadata": {
238 | "id": "ITrf_9Lssepn"
239 | },
240 | "source": [
241 | "#Access vlaue for a particular key\n",
242 | "dict1[\"Russia\"]"
243 | ],
244 | "execution_count": null,
245 | "outputs": []
246 | },
247 | {
248 | "cell_type": "code",
249 | "metadata": {
250 | "id": "sLxVlzYwsnSx"
251 | },
252 | "source": [
253 | "print(dict_new)"
254 | ],
255 | "execution_count": null,
256 | "outputs": []
257 | },
258 | {
259 | "cell_type": "code",
260 | "metadata": {
261 | "id": "gSPxO5Xsto_o"
262 | },
263 | "source": [
264 | "#Accessing Tuple value in a dictionary\n",
265 | "\n",
266 | "dict_new[1][1]"
267 | ],
268 | "execution_count": null,
269 | "outputs": []
270 | },
271 | {
272 | "cell_type": "code",
273 | "metadata": {
274 | "id": "aAg-zcqStzjH"
275 | },
276 | "source": [
277 | "dict_new[(2,3)]"
278 | ],
279 | "execution_count": null,
280 | "outputs": []
281 | },
282 | {
283 | "cell_type": "code",
284 | "metadata": {
285 | "id": "qrRnQz7ut4vQ"
286 | },
287 | "source": [
288 | "dict_new[\"My_list\"][1:] #Accessing list values in dcitionary using slicing"
289 | ],
290 | "execution_count": null,
291 | "outputs": []
292 | },
293 | {
294 | "cell_type": "markdown",
295 | "metadata": {
296 | "id": "IHdZSpBWuNT-"
297 | },
298 | "source": [
299 | "####Change, Delete, Add Dictionary values"
300 | ]
301 | },
302 | {
303 | "cell_type": "code",
304 | "metadata": {
305 | "id": "vm2t8ibTt_l4"
306 | },
307 | "source": [
308 | "my_veg= {1: \"Onions\", 2: \"Peas\", 3: \"carrots\"} #Create a dictionary\n",
309 | "print(my_veg)"
310 | ],
311 | "execution_count": null,
312 | "outputs": []
313 | },
314 | {
315 | "cell_type": "code",
316 | "metadata": {
317 | "id": "e9PEe291uzTu"
318 | },
319 | "source": [
320 | "#change value \n",
321 | "\n",
322 | "my_veg[2]= \"Cabbage\"\n",
323 | "print(my_veg)"
324 | ],
325 | "execution_count": null,
326 | "outputs": []
327 | },
328 | {
329 | "cell_type": "code",
330 | "metadata": {
331 | "id": "gOAzZN92u_sw"
332 | },
333 | "source": [
334 | "#Use update() function to change the value at a particular key\n",
335 | "\n",
336 | "my_veg.update({3: \"Pepper\"})\n",
337 | "print(my_veg)"
338 | ],
339 | "execution_count": null,
340 | "outputs": []
341 | },
342 | {
343 | "cell_type": "code",
344 | "metadata": {
345 | "id": "RoHKl6dzvNPy"
346 | },
347 | "source": [
348 | "#add a new key-value pair\n",
349 | "my_veg.update({4: \"Cauliflower\"})\n",
350 | "print(my_veg)"
351 | ],
352 | "execution_count": null,
353 | "outputs": []
354 | },
355 | {
356 | "cell_type": "code",
357 | "metadata": {
358 | "id": "VbvysIkLz6X4"
359 | },
360 | "source": [
361 | "#Alternative way to add key-value to the dictionary\n",
362 | "my_veg[5]= \"Beans\"\n",
363 | "print(my_veg)"
364 | ],
365 | "execution_count": null,
366 | "outputs": []
367 | },
368 | {
369 | "cell_type": "code",
370 | "metadata": {
371 | "id": "L3g728GYzvlg"
372 | },
373 | "source": [
374 | "#Removing/Deleting items using pop\n",
375 | "\n",
376 | "my_veg.pop(2)\n",
377 | "print(my_veg)"
378 | ],
379 | "execution_count": null,
380 | "outputs": []
381 | },
382 | {
383 | "cell_type": "code",
384 | "metadata": {
385 | "id": "AJBm1eIB1TlC"
386 | },
387 | "source": [
388 | "#Removing some key-value pair in LIFO order\n",
389 | "\n",
390 | "my_veg.popitem()"
391 | ],
392 | "execution_count": null,
393 | "outputs": []
394 | },
395 | {
396 | "cell_type": "code",
397 | "metadata": {
398 | "id": "mmAwREBn1PCa"
399 | },
400 | "source": [
401 | "print(my_veg)"
402 | ],
403 | "execution_count": null,
404 | "outputs": []
405 | },
406 | {
407 | "cell_type": "code",
408 | "metadata": {
409 | "id": "S71w1NUT1lua"
410 | },
411 | "source": [
412 | "#clear all items\n",
413 | "my_veg.clear() #sll items are cleared. But empty dictionary still there\n",
414 | "print(my_veg)"
415 | ],
416 | "execution_count": null,
417 | "outputs": []
418 | },
419 | {
420 | "cell_type": "code",
421 | "metadata": {
422 | "id": "c3UVHold2EcD"
423 | },
424 | "source": [
425 | "#delete all items\n",
426 | "print(dict1)"
427 | ],
428 | "execution_count": null,
429 | "outputs": []
430 | },
431 | {
432 | "cell_type": "code",
433 | "metadata": {
434 | "id": "7sAyQdRU2YJk"
435 | },
436 | "source": [
437 | "del dict1 #Dictionary does not exist after deleting\n",
438 | "print(dict1)"
439 | ],
440 | "execution_count": null,
441 | "outputs": []
442 | },
443 | {
444 | "cell_type": "markdown",
445 | "metadata": {
446 | "id": "OOV_pAzd9eRO"
447 | },
448 | "source": [
449 | "####Dictionary methods -**EXPLORE**\n",
450 | "\n",
451 | "\n",
452 | "dict.clear()\tRemoves all the key-value pairs from the dictionary.
\n",
453 | "dict.copy()\tReturns a shallow copy of the dictionary.
\n",
454 | "dict.fromkeys()\tCreates a new dictionary from the given iterable (string, list, set, tuple) as keys and with the specified value.
\n",
455 | "dict.get()\tReturns the value of the specified key.
\n",
456 | "dict.items()\tReturns a dictionary view object that provides a dynamic view of dictionary elements as a list of key-value pairs. This view object changes when the dictionary changes.
\n",
457 | "dict.keys()\tReturns a dictionary view object that contains the list of keys of the dictionary.
\n",
458 | "dict.pop()\tRemoves the key and return its value. If a key does not exist in the dictionary, then returns the default value if specified, else throws a KeyError.
\n",
459 | "dict.popitem()\tRemoves and return a tuple of (key, value) pair from the dictionary. Pairs are returned in Last In First Out (LIFO) order.
\n",
460 | "dict.setdefault()\tReturns the value of the specified key in the dictionary. If the key not found, then it adds the key with the specified defaultvalue. If the defaultvalue is not specified then it set None value.
\n",
461 | "dict.update()\tUpdates the dictionary with the key-value pairs from another dictionary or another iterable such as tuple having key-value pairs.
\n",
462 | "dict.values()\tReturns the dictionary view object that provides a dynamic view of all the values in the dictionary. This view object changes when the dictionary changes.
"
463 | ]
464 | },
465 | {
466 | "cell_type": "markdown",
467 | "metadata": {
468 | "id": "ELfSDxx4Pd0-"
469 | },
470 | "source": [
471 | "####Nested Dictionary"
472 | ]
473 | },
474 | {
475 | "cell_type": "code",
476 | "metadata": {
477 | "id": "-Neg1e0x2pll"
478 | },
479 | "source": [
480 | "student = {1: {'name': 'Jay', 'subject': 'Math', 'marks': 77},\n",
481 | " 2: {'name': 'Jash', 'subject': 'Math', 'marks': 87}}"
482 | ],
483 | "execution_count": null,
484 | "outputs": []
485 | },
486 | {
487 | "cell_type": "code",
488 | "metadata": {
489 | "colab": {
490 | "base_uri": "https://localhost:8080/"
491 | },
492 | "id": "d6QPBAlFQAyg",
493 | "outputId": "6c2d0607-b477-4420-d54e-7648463e2954"
494 | },
495 | "source": [
496 | "print(student)"
497 | ],
498 | "execution_count": null,
499 | "outputs": [
500 | {
501 | "output_type": "stream",
502 | "text": [
503 | "{1: {'name': 'Jay', 'subject': 'Math', 'marks': 77}, 2: {'name': 'Jash', 'subject': 'Math', 'marks': 87}}\n"
504 | ],
505 | "name": "stdout"
506 | }
507 | ]
508 | },
509 | {
510 | "cell_type": "code",
511 | "metadata": {
512 | "colab": {
513 | "base_uri": "https://localhost:8080/"
514 | },
515 | "id": "YNd2aj-cP6PR",
516 | "outputId": "aef0f9ef-317c-47e5-a694-3afebe09d4a5"
517 | },
518 | "source": [
519 | "student[1][\"marks\"]"
520 | ],
521 | "execution_count": null,
522 | "outputs": [
523 | {
524 | "output_type": "execute_result",
525 | "data": {
526 | "text/plain": [
527 | "77"
528 | ]
529 | },
530 | "metadata": {
531 | "tags": []
532 | },
533 | "execution_count": 6
534 | }
535 | ]
536 | },
537 | {
538 | "cell_type": "markdown",
539 | "metadata": {
540 | "id": "VaxkWXZgQfXq"
541 | },
542 | "source": [
543 | "####DIY: Add a dictionary in the nested dictionary, Delete Dictionary from Nested Dictionary, change contents of the nested dictionary"
544 | ]
545 | },
546 | {
547 | "cell_type": "markdown",
548 | "metadata": {
549 | "id": "MHxgwlHCYyBa"
550 | },
551 | "source": [
552 | "Concatenate dictionaries"
553 | ]
554 | },
555 | {
556 | "cell_type": "code",
557 | "metadata": {
558 | "id": "G_ptinH5ZE7b"
559 | },
560 | "source": [
561 | "dict1= {1:10, 2:20}\n",
562 | "dict2= {3:30,4:40}\n",
563 | "dict3={}\n",
564 | "for d in (dict1,dict2):dict3.update(d)"
565 | ],
566 | "execution_count": null,
567 | "outputs": []
568 | },
569 | {
570 | "cell_type": "code",
571 | "metadata": {
572 | "colab": {
573 | "base_uri": "https://localhost:8080/"
574 | },
575 | "id": "imY2kH4HZLXp",
576 | "outputId": "961a4fac-f781-4129-b1c8-75c40f147e5c"
577 | },
578 | "source": [
579 | "print(dict3)"
580 | ],
581 | "execution_count": null,
582 | "outputs": [
583 | {
584 | "output_type": "stream",
585 | "text": [
586 | "{1: 10, 2: 20, 3: 30, 4: 40}\n"
587 | ],
588 | "name": "stdout"
589 | }
590 | ]
591 | },
592 | {
593 | "cell_type": "code",
594 | "metadata": {
595 | "id": "GfeakeIPZfGI"
596 | },
597 | "source": [
598 | ""
599 | ],
600 | "execution_count": null,
601 | "outputs": []
602 | }
603 | ]
604 | }
605 |
--------------------------------------------------------------------------------
/2_5_Sets.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Unit2.5Sets.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyPcCzNvtZNlVZrPcHBQSQ/R",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "vF8vyWpUnl4K"
34 | },
35 | "source": [
36 | "Sets\n",
37 | "\n",
38 | "Used to store multiple items in a single variable. It is unordered,unstructured and does not allow duplicate values. "
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "metadata": {
44 | "colab": {
45 | "base_uri": "https://localhost:8080/"
46 | },
47 | "id": "Bm8CXl87nUN-",
48 | "outputId": "303a1878-f176-41a8-fc2a-cbeaf39bf87a"
49 | },
50 | "source": [
51 | "fruits= {\"apple\", \"banana\", \"mango\", \"banana\", \"grapes\"} #create set\n",
52 | "print(fruits)"
53 | ],
54 | "execution_count": null,
55 | "outputs": [
56 | {
57 | "output_type": "stream",
58 | "text": [
59 | "{'banana', 'mango', 'grapes', 'apple'}\n"
60 | ],
61 | "name": "stdout"
62 | }
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "metadata": {
68 | "colab": {
69 | "base_uri": "https://localhost:8080/"
70 | },
71 | "id": "--Lds34ooiGk",
72 | "outputId": "389298db-30a3-4b1d-c7e7-1b339b876033"
73 | },
74 | "source": [
75 | "print(fruits)"
76 | ],
77 | "execution_count": null,
78 | "outputs": [
79 | {
80 | "output_type": "stream",
81 | "text": [
82 | "{'banana', 'mango', 'grapes', 'apple'}\n"
83 | ],
84 | "name": "stdout"
85 | }
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "metadata": {
91 | "colab": {
92 | "base_uri": "https://localhost:8080/"
93 | },
94 | "id": "M6qQkcoNom-7",
95 | "outputId": "d3f0d585-e7b6-4869-bba6-d06386e7ba66"
96 | },
97 | "source": [
98 | "#different data types allowed in a set\n",
99 | "\n",
100 | "my_set= {\"car\", 20, True, 3.14, 20 }\n",
101 | "print(my_set)"
102 | ],
103 | "execution_count": null,
104 | "outputs": [
105 | {
106 | "output_type": "stream",
107 | "text": [
108 | "{'car', 3.14, 20, True}\n"
109 | ],
110 | "name": "stdout"
111 | }
112 | ]
113 | },
114 | {
115 | "cell_type": "code",
116 | "metadata": {
117 | "colab": {
118 | "base_uri": "https://localhost:8080/"
119 | },
120 | "id": "7kaBpL7Rovfh",
121 | "outputId": "65f47de6-8bb3-47ec-ed29-86179b6827c0"
122 | },
123 | "source": [
124 | "type(my_set) #get the type of the data structure"
125 | ],
126 | "execution_count": null,
127 | "outputs": [
128 | {
129 | "output_type": "execute_result",
130 | "data": {
131 | "text/plain": [
132 | "set"
133 | ]
134 | },
135 | "metadata": {
136 | "tags": []
137 | },
138 | "execution_count": 8
139 | }
140 | ]
141 | },
142 | {
143 | "cell_type": "code",
144 | "metadata": {
145 | "colab": {
146 | "base_uri": "https://localhost:8080/"
147 | },
148 | "id": "TOT6vUFBpRvh",
149 | "outputId": "b4d96ec8-9e44-4e04-e15a-ad3f4050467e"
150 | },
151 | "source": [
152 | "len(my_set) #Get the length of the set. It it counts nly number of unique elements"
153 | ],
154 | "execution_count": null,
155 | "outputs": [
156 | {
157 | "output_type": "execute_result",
158 | "data": {
159 | "text/plain": [
160 | "4"
161 | ]
162 | },
163 | "metadata": {
164 | "tags": []
165 | },
166 | "execution_count": 9
167 | }
168 | ]
169 | },
170 | {
171 | "cell_type": "code",
172 | "metadata": {
173 | "colab": {
174 | "base_uri": "https://localhost:8080/"
175 | },
176 | "id": "VR-tDU7YpX9p",
177 | "outputId": "5566af8f-5817-48c9-dfe2-6657c4ac96ac"
178 | },
179 | "source": [
180 | "my_cars= set(input(\"Enter car companies separated bya comma: \").split(\",\")) #input set from the user\n",
181 | "print(my_cars)"
182 | ],
183 | "execution_count": null,
184 | "outputs": [
185 | {
186 | "output_type": "stream",
187 | "text": [
188 | "Enter car companies separated bya comma: Tata,Honda,Hyundai\n",
189 | "{'Hyundai', 'Honda', 'Tata'}\n"
190 | ],
191 | "name": "stdout"
192 | }
193 | ]
194 | },
195 | {
196 | "cell_type": "markdown",
197 | "metadata": {
198 | "id": "2tCtQ_7jqKpp"
199 | },
200 | "source": [
201 | "####Access Set item"
202 | ]
203 | },
204 | {
205 | "cell_type": "code",
206 | "metadata": {
207 | "colab": {
208 | "base_uri": "https://localhost:8080/"
209 | },
210 | "id": "TGfW2TLYpnb5",
211 | "outputId": "4c3b180e-9e1d-4140-8c6c-ad9c12fb3811"
212 | },
213 | "source": [
214 | "for car in my_cars:\n",
215 | " print(car)"
216 | ],
217 | "execution_count": null,
218 | "outputs": [
219 | {
220 | "output_type": "stream",
221 | "text": [
222 | "Hyundai\n",
223 | "Honda\n",
224 | "Tata\n"
225 | ],
226 | "name": "stdout"
227 | }
228 | ]
229 | },
230 | {
231 | "cell_type": "code",
232 | "metadata": {
233 | "colab": {
234 | "base_uri": "https://localhost:8080/"
235 | },
236 | "id": "4lxvsabopsux",
237 | "outputId": "d9f67b53-e822-4cbc-c096-55cc4dab832f"
238 | },
239 | "source": [
240 | "# check is some element is present in the set\n",
241 | "print(\"Tata\" in my_cars)"
242 | ],
243 | "execution_count": null,
244 | "outputs": [
245 | {
246 | "output_type": "stream",
247 | "text": [
248 | "True\n"
249 | ],
250 | "name": "stdout"
251 | }
252 | ]
253 | },
254 | {
255 | "cell_type": "markdown",
256 | "metadata": {
257 | "id": "g7ahxVNMq3Zh"
258 | },
259 | "source": [
260 | "Add and remove values in the set"
261 | ]
262 | },
263 | {
264 | "cell_type": "code",
265 | "metadata": {
266 | "colab": {
267 | "base_uri": "https://localhost:8080/"
268 | },
269 | "id": "uxK6XywiqoBh",
270 | "outputId": "7b017b13-be69-4040-b613-b2382650af3a"
271 | },
272 | "source": [
273 | "my_cars.add(\"Mercedes\") #use add method\n",
274 | "print(my_cars)"
275 | ],
276 | "execution_count": null,
277 | "outputs": [
278 | {
279 | "output_type": "stream",
280 | "text": [
281 | "{'Hyundai', 'Honda', 'Mercedes', 'Tata'}\n"
282 | ],
283 | "name": "stdout"
284 | }
285 | ]
286 | },
287 | {
288 | "cell_type": "code",
289 | "metadata": {
290 | "colab": {
291 | "base_uri": "https://localhost:8080/"
292 | },
293 | "id": "nhAwjvsirDgp",
294 | "outputId": "48c6ee20-70fa-4cbf-fed7-c24a0ab13f0b"
295 | },
296 | "source": [
297 | "my_cars.remove(\"Honda\")\n",
298 | "print(my_cars)"
299 | ],
300 | "execution_count": null,
301 | "outputs": [
302 | {
303 | "output_type": "stream",
304 | "text": [
305 | "{'Hyundai', 'Mercedes', 'Tata'}\n"
306 | ],
307 | "name": "stdout"
308 | }
309 | ]
310 | },
311 | {
312 | "cell_type": "code",
313 | "metadata": {
314 | "id": "YPzTrqj4Fqyp"
315 | },
316 | "source": [
317 | "# DIY Union of two sets\n",
318 | "even={2,4,6,8}\n",
319 | "odd= {1,3,5,7}\n"
320 | ],
321 | "execution_count": null,
322 | "outputs": []
323 | },
324 | {
325 | "cell_type": "code",
326 | "metadata": {
327 | "id": "8Uz0iT0NFwBZ"
328 | },
329 | "source": [
330 | ""
331 | ],
332 | "execution_count": null,
333 | "outputs": []
334 | },
335 | {
336 | "cell_type": "code",
337 | "metadata": {
338 | "id": "br1BoXq-F0BB"
339 | },
340 | "source": [
341 | "# DIY Set intersection\n",
342 | "a= {1,2,3}\n",
343 | "b={1,2,3,4,5,6}\n"
344 | ],
345 | "execution_count": null,
346 | "outputs": []
347 | },
348 | {
349 | "cell_type": "code",
350 | "metadata": {
351 | "id": "duWAwGbYGOCJ"
352 | },
353 | "source": [
354 | "#DIY Exclusivity ,Symmetric Difference\n",
355 | "d= {1,2,3,9,10,11}\n",
356 | "c= {1,2,3,4,5,6,7}\n"
357 | ],
358 | "execution_count": null,
359 | "outputs": []
360 | },
361 | {
362 | "cell_type": "code",
363 | "metadata": {
364 | "id": "6jtINIO8GSw_"
365 | },
366 | "source": [
367 | "#Set difference\n",
368 | "a= {1,2,3}\n",
369 | "c= {1,2,3,4,5,6,7}\n"
370 | ],
371 | "execution_count": null,
372 | "outputs": []
373 | },
374 | {
375 | "cell_type": "code",
376 | "metadata": {
377 | "id": "bsV2bkl0GaDf"
378 | },
379 | "source": [
380 | "#Check if a set is subset of another\n",
381 | "a= {1,2,3,4}\n",
382 | "b= {1,2,3,4,5}\n"
383 | ],
384 | "execution_count": null,
385 | "outputs": []
386 | },
387 | {
388 | "cell_type": "code",
389 | "metadata": {
390 | "id": "41q0HDWkFZ09"
391 | },
392 | "source": [
393 | "# Converting string to set\n",
394 | "string1= \"good morning India\"\n",
395 | "set1= set(string1)\n",
396 | "print(set1)\n",
397 | "print(set1[4]) # Indexing not possible in set"
398 | ],
399 | "execution_count": null,
400 | "outputs": []
401 | },
402 | {
403 | "cell_type": "markdown",
404 | "metadata": {
405 | "id": "OHs9amyJsjLk"
406 | },
407 | "source": [
408 | "####Set Methods- **EXPLORE**\n",
409 | "\n",
410 | "add()\tAdds an element to the set
\n",
411 | "clear()\tRemoves all the elements from the set
\n",
412 | "copy()\tReturns a copy of the set
\n",
413 | "difference()\tReturns a set containing the difference between two or more sets
\n",
414 | "difference_update()\tRemoves the items in this set that are also included in another, specified set
\n",
415 | "discard()\tRemove the specified item
\n",
416 | "intersection()\tReturns a set, that is the intersection of two other sets
\n",
417 | "intersection_update()\tRemoves the items in this set that are not present in other, specified set(s)
\n",
418 | "isdisjoint()\tReturns whether two sets have a intersection or not
\n",
419 | "issubset()\tReturns whether another set contains this set or not
\n",
420 | "issuperset()\tReturns whether this set contains another set or not
\n",
421 | "pop()\tRemoves an element from the set
\n",
422 | "remove()\tRemoves the specified element
\n",
423 | "symmetric_difference()\tReturns a set with the symmetric differences of two sets
\n",
424 | "symmetric_difference_update()\tinserts the symmetric differences from this set and another
\n",
425 | "union()\tReturn a set containing the union of sets
\n",
426 | "update()\tUpdate the set with the union of this set and others
"
427 | ]
428 | },
429 | {
430 | "cell_type": "code",
431 | "metadata": {
432 | "id": "r5uUSLa7sZcW"
433 | },
434 | "source": [
435 | ""
436 | ],
437 | "execution_count": null,
438 | "outputs": []
439 | }
440 | ]
441 | }
442 |
--------------------------------------------------------------------------------
/2_6ConditionalBlocks.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Unit2.6ConditionalBlocks.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyM7wMAOhvlQiitAZEQG9siV",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "UJS9esdyfgpt"
34 | },
35 | "source": [
36 | "###Conditional Blocks. \n",
37 | "\n",
38 | "It involves decision making. If given condition is True, a decision is made to run one block of program. If the decision is false, some other block of code is executed\n"
39 | ]
40 | },
41 | {
42 | "cell_type": "markdown",
43 | "metadata": {
44 | "id": "OFag7E2MgEo8"
45 | },
46 | "source": [
47 | "If,Else, Elif"
48 | ]
49 | },
50 | {
51 | "cell_type": "markdown",
52 | "metadata": {
53 | "id": "yXdJ_S_-gPtn"
54 | },
55 | "source": [
56 | "Simple If statement"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "metadata": {
62 | "colab": {
63 | "base_uri": "https://localhost:8080/"
64 | },
65 | "id": "e6e6JFKygHRo",
66 | "outputId": "56d78e91-1c39-41c4-fa59-8f606702ac53"
67 | },
68 | "source": [
69 | "marks= input(\"Enter your marks between 1 to 100: \")\n",
70 | "marks=int(marks)\n",
71 | "if 50<= marks<= 70:\n",
72 | " print(\"Your marks are satisfactory\")"
73 | ],
74 | "execution_count": null,
75 | "outputs": [
76 | {
77 | "output_type": "stream",
78 | "text": [
79 | "Enter your marks between 1 to 100: 40\n"
80 | ],
81 | "name": "stdout"
82 | }
83 | ]
84 | },
85 | {
86 | "cell_type": "markdown",
87 | "metadata": {
88 | "id": "duV3G9-Fg3dI"
89 | },
90 | "source": [
91 | "If-Else"
92 | ]
93 | },
94 | {
95 | "cell_type": "code",
96 | "metadata": {
97 | "colab": {
98 | "base_uri": "https://localhost:8080/"
99 | },
100 | "id": "DFn8o-yJgzRF",
101 | "outputId": "2857212a-f99c-4ff4-bbe9-f192295f1e0c"
102 | },
103 | "source": [
104 | "marks= input(\"Enter your marks between 1 to 100: \")\n",
105 | "marks=int(marks)\n",
106 | "if marks<=50:\n",
107 | " print(\"Your marks are poor\")\n",
108 | "else:\n",
109 | " print(\"Your marks are good\")\n"
110 | ],
111 | "execution_count": null,
112 | "outputs": [
113 | {
114 | "output_type": "stream",
115 | "text": [
116 | "Enter your marks between 1 to 100: 70\n",
117 | "Your marks are good\n"
118 | ],
119 | "name": "stdout"
120 | }
121 | ]
122 | },
123 | {
124 | "cell_type": "markdown",
125 | "metadata": {
126 | "id": "c70aFMCLhjp2"
127 | },
128 | "source": [
129 | "If, Else,Elif"
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "metadata": {
135 | "colab": {
136 | "base_uri": "https://localhost:8080/"
137 | },
138 | "id": "Ht1sdGrYhmJP",
139 | "outputId": "c64656e6-f940-40f0-831d-0a7e6d49542e"
140 | },
141 | "source": [
142 | "marks= input(\"Enter your marks between 1 to 100: \")\n",
143 | "marks=int(marks)\n",
144 | "if marks<=50:\n",
145 | " print(\"Your marks are poor!\")\n",
146 | "elif 50
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "p_b6FPGG20v7"
34 | },
35 | "source": [
36 | "\n",
37 | "\n",
38 | "Loops
\n",
39 | "\n",
40 | "There are two basic loops in python-
\n",
41 | "For Loop
\n",
42 | "While Loop\n",
43 | ""
44 | ]
45 | },
46 | {
47 | "cell_type": "markdown",
48 | "metadata": {
49 | "id": "6_VaXsXl3HEr"
50 | },
51 | "source": [
52 | "####While Loops:\n",
53 | "Set of statements within the block are executed till the statement is true"
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "metadata": {
59 | "colab": {
60 | "base_uri": "https://localhost:8080/"
61 | },
62 | "id": "8p-61x-g2m21",
63 | "outputId": "deff2779-f6cd-4ef5-db0f-88feaddc290c"
64 | },
65 | "source": [
66 | "#print yes 5 times\n",
67 | "i=0\n",
68 | "while i<5:\n",
69 | " print(\"yes\")\n",
70 | " i+=1"
71 | ],
72 | "execution_count": null,
73 | "outputs": [
74 | {
75 | "output_type": "stream",
76 | "text": [
77 | "yes\n",
78 | "yes\n",
79 | "yes\n",
80 | "yes\n",
81 | "yes\n"
82 | ],
83 | "name": "stdout"
84 | }
85 | ]
86 | },
87 | {
88 | "cell_type": "code",
89 | "metadata": {
90 | "colab": {
91 | "base_uri": "https://localhost:8080/"
92 | },
93 | "id": "zLA31A_G3lqo",
94 | "outputId": "f4eb3b0f-0746-4b26-9262-3e3bb8638916"
95 | },
96 | "source": [
97 | "#print value of i\n",
98 | "i=7\n",
99 | "while i>1:\n",
100 | " print(i)\n",
101 | " i-=1 #i=i-1\n"
102 | ],
103 | "execution_count": null,
104 | "outputs": [
105 | {
106 | "output_type": "stream",
107 | "text": [
108 | "7\n",
109 | "6\n",
110 | "5\n",
111 | "4\n",
112 | "3\n",
113 | "2\n"
114 | ],
115 | "name": "stdout"
116 | }
117 | ]
118 | },
119 | {
120 | "cell_type": "markdown",
121 | "metadata": {
122 | "id": "PCdwVhJRLRR9"
123 | },
124 | "source": [
125 | "####Break Statement\n",
126 | "Exit the loop even if the while statement is True"
127 | ]
128 | },
129 | {
130 | "cell_type": "code",
131 | "metadata": {
132 | "colab": {
133 | "base_uri": "https://localhost:8080/"
134 | },
135 | "id": "3ezo5bOlKgrr",
136 | "outputId": "95a50c9e-2782-4f7d-95d4-9181e8e7fa5a"
137 | },
138 | "source": [
139 | "i = 1\n",
140 | "while i <7:\n",
141 | " print(i)\n",
142 | " if i == 4:\n",
143 | " break\n",
144 | " i += 1"
145 | ],
146 | "execution_count": null,
147 | "outputs": [
148 | {
149 | "output_type": "stream",
150 | "text": [
151 | "1\n",
152 | "2\n",
153 | "3\n",
154 | "4\n"
155 | ],
156 | "name": "stdout"
157 | }
158 | ]
159 | },
160 | {
161 | "cell_type": "code",
162 | "metadata": {
163 | "colab": {
164 | "base_uri": "https://localhost:8080/"
165 | },
166 | "id": "ScqVlbywL2wt",
167 | "outputId": "31f40afa-73ba-4463-8e2b-cacb48b32c92"
168 | },
169 | "source": [
170 | "i= 1\n",
171 | "while i<7:\n",
172 | " print(i)\n",
173 | " s=input(\"Continue? Enter Y/N: \")\n",
174 | " if s==\"N\":\n",
175 | " break\n",
176 | " i+=1 "
177 | ],
178 | "execution_count": null,
179 | "outputs": [
180 | {
181 | "output_type": "stream",
182 | "text": [
183 | "1\n",
184 | "Continue? Enter Y/N: y\n",
185 | "2\n",
186 | "Continue? Enter Y/N: n\n",
187 | "3\n",
188 | "Continue? Enter Y/N: N\n"
189 | ],
190 | "name": "stdout"
191 | }
192 | ]
193 | },
194 | {
195 | "cell_type": "markdown",
196 | "metadata": {
197 | "id": "hyAsM8ATMdPN"
198 | },
199 | "source": [
200 | "####Continue\n",
201 | "With the continue statement we can stop the current iteration, and continue with the next"
202 | ]
203 | },
204 | {
205 | "cell_type": "code",
206 | "metadata": {
207 | "colab": {
208 | "base_uri": "https://localhost:8080/"
209 | },
210 | "id": "uZQW2YAgMLmb",
211 | "outputId": "b5a887e3-3173-4f21-a2ed-f50f8ce64ef4"
212 | },
213 | "source": [
214 | "i = 0\n",
215 | "while i <7:\n",
216 | " i += 1\n",
217 | " if i == 4:\n",
218 | " continue\n",
219 | " print(i)\n",
220 | " "
221 | ],
222 | "execution_count": null,
223 | "outputs": [
224 | {
225 | "output_type": "stream",
226 | "text": [
227 | "1\n",
228 | "2\n",
229 | "3\n",
230 | "5\n",
231 | "6\n",
232 | "7\n"
233 | ],
234 | "name": "stdout"
235 | }
236 | ]
237 | },
238 | {
239 | "cell_type": "markdown",
240 | "metadata": {
241 | "id": "56faDMPCNI4V"
242 | },
243 | "source": [
244 | "####While-Else"
245 | ]
246 | },
247 | {
248 | "cell_type": "code",
249 | "metadata": {
250 | "colab": {
251 | "base_uri": "https://localhost:8080/"
252 | },
253 | "id": "tuWibbcAM2c1",
254 | "outputId": "c648dd9e-b280-450e-84e9-6a173a97a381"
255 | },
256 | "source": [
257 | "i=7\n",
258 | "while i>1:\n",
259 | " print(i)\n",
260 | " i-=1\n",
261 | "else:\n",
262 | " print(\"Count down over\")\n"
263 | ],
264 | "execution_count": null,
265 | "outputs": [
266 | {
267 | "output_type": "stream",
268 | "text": [
269 | "7\n",
270 | "6\n",
271 | "5\n",
272 | "4\n",
273 | "3\n",
274 | "2\n",
275 | "Count down over\n"
276 | ],
277 | "name": "stdout"
278 | }
279 | ]
280 | },
281 | {
282 | "cell_type": "markdown",
283 | "metadata": {
284 | "id": "ZfNHVZTyNaVQ"
285 | },
286 | "source": [
287 | "###For Loops"
288 | ]
289 | },
290 | {
291 | "cell_type": "code",
292 | "metadata": {
293 | "colab": {
294 | "base_uri": "https://localhost:8080/"
295 | },
296 | "id": "vURGRHqhNZ9q",
297 | "outputId": "1a8a0785-40c0-4434-ed0b-06146d398eb4"
298 | },
299 | "source": [
300 | "#print numbers\n",
301 | "numbers=[4,2,3,7,9,6,5]\n",
302 | "for i in numbers:\n",
303 | " print(i)\n"
304 | ],
305 | "execution_count": null,
306 | "outputs": [
307 | {
308 | "output_type": "stream",
309 | "text": [
310 | "4\n",
311 | "2\n",
312 | "3\n",
313 | "7\n",
314 | "9\n",
315 | "6\n",
316 | "5\n"
317 | ],
318 | "name": "stdout"
319 | }
320 | ]
321 | },
322 | {
323 | "cell_type": "markdown",
324 | "metadata": {
325 | "id": "FZLriS5QP_7_"
326 | },
327 | "source": [
328 | "###Break statement"
329 | ]
330 | },
331 | {
332 | "cell_type": "code",
333 | "metadata": {
334 | "colab": {
335 | "base_uri": "https://localhost:8080/"
336 | },
337 | "id": "gu82dSVRNTaE",
338 | "outputId": "9ad8e195-20e3-4908-f597-893693ec8d76"
339 | },
340 | "source": [
341 | "#if a certain number comes, break the loop and exit\n",
342 | "numbers=[4,2,3,7,9,6,5]\n",
343 | "for i in numbers:\n",
344 | " if i==7:\n",
345 | " break\n",
346 | " print(i)"
347 | ],
348 | "execution_count": null,
349 | "outputs": [
350 | {
351 | "output_type": "stream",
352 | "text": [
353 | "4\n",
354 | "2\n",
355 | "3\n"
356 | ],
357 | "name": "stdout"
358 | }
359 | ]
360 | },
361 | {
362 | "cell_type": "markdown",
363 | "metadata": {
364 | "id": "3WFbHhEdnniL"
365 | },
366 | "source": [
367 | "For Loops
\n",
368 | "A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).\n",
369 | "\n",
370 | "This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.\n",
371 | "\n",
372 | "With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc"
373 | ]
374 | },
375 | {
376 | "cell_type": "code",
377 | "metadata": {
378 | "id": "66FWT2HVQTTx",
379 | "colab": {
380 | "base_uri": "https://localhost:8080/"
381 | },
382 | "outputId": "b0720165-83b9-4bb5-8535-52f07d767abb"
383 | },
384 | "source": [
385 | "#Print elements in the list using for loop\n",
386 | "fruits= [\"banana\", \"apple\", \"mango\"]\n",
387 | "for fruit in fruits:\n",
388 | " print(fruit)"
389 | ],
390 | "execution_count": null,
391 | "outputs": [
392 | {
393 | "output_type": "stream",
394 | "text": [
395 | "banana\n",
396 | "apple\n",
397 | "mango\n"
398 | ],
399 | "name": "stdout"
400 | }
401 | ]
402 | },
403 | {
404 | "cell_type": "code",
405 | "metadata": {
406 | "colab": {
407 | "base_uri": "https://localhost:8080/"
408 | },
409 | "id": "wYJp42_uoKIh",
410 | "outputId": "6cc53bbf-e7f4-4e03-e4ae-4917422a1ae0"
411 | },
412 | "source": [
413 | "# sum all the numbers in the given list\n",
414 | "\n",
415 | "nums= [10,20,30,40,50]\n",
416 | "\n",
417 | "count=0\n",
418 | "for num in nums:\n",
419 | " count+=num\n",
420 | " print(count)\n",
421 | "print(count)"
422 | ],
423 | "execution_count": null,
424 | "outputs": [
425 | {
426 | "output_type": "stream",
427 | "text": [
428 | "10\n",
429 | "30\n",
430 | "60\n",
431 | "100\n",
432 | "150\n",
433 | "150\n"
434 | ],
435 | "name": "stdout"
436 | }
437 | ]
438 | },
439 | {
440 | "cell_type": "code",
441 | "metadata": {
442 | "id": "Xsn2Qwgpof3b"
443 | },
444 | "source": [
445 | "#Display odd numbers between 1 to 100 using for loop"
446 | ],
447 | "execution_count": null,
448 | "outputs": []
449 | },
450 | {
451 | "cell_type": "code",
452 | "metadata": {
453 | "colab": {
454 | "base_uri": "https://localhost:8080/"
455 | },
456 | "id": "GzRRf37JPM1w",
457 | "outputId": "1094253e-9f8f-43f9-82d8-2952afa95ab1"
458 | },
459 | "source": [
460 | "#searching for element in the list\n",
461 | "numbers=[4,2,3,7,9,6,5]\n",
462 | "\n",
463 | "search = int(input(\"Enter the number: \"))\n",
464 | "for element in numbers:\n",
465 | " if search==element:\n",
466 | " print(\"Element found\")\n",
467 | " break\n",
468 | " \n",
469 | "else: \n",
470 | " print(\"Element not found\")"
471 | ],
472 | "execution_count": null,
473 | "outputs": [
474 | {
475 | "output_type": "stream",
476 | "text": [
477 | "Enter the number: 10\n",
478 | "Element not found\n"
479 | ],
480 | "name": "stdout"
481 | }
482 | ]
483 | },
484 | {
485 | "cell_type": "markdown",
486 | "metadata": {
487 | "id": "dVm5LCcA6Qik"
488 | },
489 | "source": [
490 | "Nested For loop"
491 | ]
492 | },
493 | {
494 | "cell_type": "code",
495 | "metadata": {
496 | "colab": {
497 | "base_uri": "https://localhost:8080/"
498 | },
499 | "id": "S1tFgjVx6YbG",
500 | "outputId": "cbfb8425-50d4-4f47-95ca-683586c2b9d5"
501 | },
502 | "source": [
503 | "for i in range(3):\n",
504 | " for j in range(4):\n",
505 | " print(\"i= \", i, \" j= \", j)"
506 | ],
507 | "execution_count": null,
508 | "outputs": [
509 | {
510 | "output_type": "stream",
511 | "text": [
512 | "i= 0 j= 0\n",
513 | "i= 0 j= 1\n",
514 | "i= 0 j= 2\n",
515 | "i= 0 j= 3\n",
516 | "i= 1 j= 0\n",
517 | "i= 1 j= 1\n",
518 | "i= 1 j= 2\n",
519 | "i= 1 j= 3\n",
520 | "i= 2 j= 0\n",
521 | "i= 2 j= 1\n",
522 | "i= 2 j= 2\n",
523 | "i= 2 j= 3\n"
524 | ],
525 | "name": "stdout"
526 | }
527 | ]
528 | },
529 | {
530 | "cell_type": "code",
531 | "metadata": {
532 | "id": "RxPLkmeQ6Rxr"
533 | },
534 | "source": [
535 | "# DIY: Display stars in right angled triangular form using nested for loops"
536 | ],
537 | "execution_count": null,
538 | "outputs": []
539 | },
540 | {
541 | "cell_type": "markdown",
542 | "metadata": {
543 | "id": "s47CjFVt8SMY"
544 | },
545 | "source": [
546 | "Use of pass statement\n",
547 | "\n",
548 | "Pass statement does not do anything. It is used with \"if\" or inside a loopto represent no operation.\n",
549 | "\n",
550 | "We use pass statement when we need a statement syntactically but do not want to do any operation.\n",
551 | "\n"
552 | ]
553 | },
554 | {
555 | "cell_type": "code",
556 | "metadata": {
557 | "id": "dWQsmuAS8oYw",
558 | "colab": {
559 | "base_uri": "https://localhost:8080/"
560 | },
561 | "outputId": "c68c6305-d349-432d-d41c-92a74de0bf81"
562 | },
563 | "source": [
564 | "#Write a program to print only negative numbers\n",
565 | "\n",
566 | "nums= [1,4,-3,2,-5,6,-8]\n",
567 | "for i in nums:\n",
568 | " if i>0:\n",
569 | " pass\n",
570 | " else:\n",
571 | " print(i)"
572 | ],
573 | "execution_count": null,
574 | "outputs": [
575 | {
576 | "output_type": "stream",
577 | "text": [
578 | "-3\n",
579 | "-5\n",
580 | "-8\n"
581 | ],
582 | "name": "stdout"
583 | }
584 | ]
585 | },
586 | {
587 | "cell_type": "code",
588 | "metadata": {
589 | "colab": {
590 | "base_uri": "https://localhost:8080/"
591 | },
592 | "id": "qEjW5ifzrxOy",
593 | "outputId": "0705015b-01fc-40f7-cf9b-1852299a4043"
594 | },
595 | "source": [
596 | "# Pass and continue\n",
597 | "#Display numbers from 1 to 5\n",
598 | "x=0\n",
599 | "while x<10:\n",
600 | " x+=1\n",
601 | " if x>5:\n",
602 | " continue\n",
603 | " print('x=',x)\n",
604 | "print(\"Out of loop\")"
605 | ],
606 | "execution_count": null,
607 | "outputs": [
608 | {
609 | "output_type": "stream",
610 | "text": [
611 | "x= 1\n",
612 | "x= 2\n",
613 | "x= 3\n",
614 | "x= 4\n",
615 | "x= 5\n",
616 | "Out of loop\n"
617 | ],
618 | "name": "stdout"
619 | }
620 | ]
621 | },
622 | {
623 | "cell_type": "code",
624 | "metadata": {
625 | "colab": {
626 | "base_uri": "https://localhost:8080/"
627 | },
628 | "id": "IRrzKFC2sVhA",
629 | "outputId": "84776060-b078-4c06-e315-37293a216eb4"
630 | },
631 | "source": [
632 | "x=0\n",
633 | "while x<10:\n",
634 | " x+=1\n",
635 | " if x>5:\n",
636 | " pass\n",
637 | " print('x=',x)\n",
638 | "print(\"Out of loop\")"
639 | ],
640 | "execution_count": null,
641 | "outputs": [
642 | {
643 | "output_type": "stream",
644 | "text": [
645 | "x= 1\n",
646 | "x= 2\n",
647 | "x= 3\n",
648 | "x= 4\n",
649 | "x= 5\n",
650 | "x= 6\n",
651 | "x= 7\n",
652 | "x= 8\n",
653 | "x= 9\n",
654 | "x= 10\n",
655 | "Out of loop\n"
656 | ],
657 | "name": "stdout"
658 | }
659 | ]
660 | },
661 | {
662 | "cell_type": "markdown",
663 | "metadata": {
664 | "id": "eukUVEUBIVqJ"
665 | },
666 | "source": [
667 | "Assert Statement
\n",
668 | "\n",
669 | "Used to check if a particular statement is fulfilled"
670 | ]
671 | },
672 | {
673 | "cell_type": "code",
674 | "metadata": {
675 | "id": "HlDswYaAIgJW"
676 | },
677 | "source": [
678 | "#program to assert if the number entered by the user is greater than zero\n",
679 | "\n",
680 | "x= int(input(\"Enter a number >0 : \"))\n",
681 | "assert x>0, \"Wrong input\"\n"
682 | ],
683 | "execution_count": null,
684 | "outputs": []
685 | },
686 | {
687 | "cell_type": "markdown",
688 | "metadata": {
689 | "id": "0nquP1n5JEJN"
690 | },
691 | "source": [
692 | "try except
\n",
693 | "\n",
694 | "The assertion error shown above when the negative input is given is called an exception. An exception is an error that occurs during runtime. To avoid such type of exceptions, we can handle them using \"try\" and \"except\"
\n",
695 | "After *try* we write *assert*.After *except* we write the exception name to be handled."
696 | ]
697 | },
698 | {
699 | "cell_type": "code",
700 | "metadata": {
701 | "id": "kMkJEWlo87PO"
702 | },
703 | "source": [
704 | "x= int(input(\"Enter the number greater than 0: \"))\n",
705 | "try: \n",
706 | " assert (x>0) #exception may occur here\n",
707 | " print(\"You entered \",x)\n",
708 | "except AssertionError:\n",
709 | " print(\"Wrong input entered\") #this is excuted if the exception occurs"
710 | ],
711 | "execution_count": null,
712 | "outputs": []
713 | },
714 | {
715 | "cell_type": "code",
716 | "metadata": {
717 | "id": "xvbfPkgLySNU"
718 | },
719 | "source": [
720 | "#DIY find more examples for try and except"
721 | ],
722 | "execution_count": null,
723 | "outputs": []
724 | },
725 | {
726 | "cell_type": "code",
727 | "metadata": {
728 | "id": "amacpHyIypAS"
729 | },
730 | "source": [
731 | ""
732 | ],
733 | "execution_count": null,
734 | "outputs": []
735 | },
736 | {
737 | "cell_type": "code",
738 | "metadata": {
739 | "id": "CN50jo8WzCaK"
740 | },
741 | "source": [
742 | ""
743 | ],
744 | "execution_count": null,
745 | "outputs": []
746 | }
747 | ]
748 | }
749 |
--------------------------------------------------------------------------------
/2_8_List&Dict_Comprehension.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Unit2.8_List&Dict_Comprehension.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyMnhdL1GkpYxFrLJ38v6EPf",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "KJu_hu5rHbVq"
34 | },
35 | "source": [
36 | "###List Comprehension\n",
37 | "\n",
38 | "Syntax becomes shorter and elegant with list comprehension"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "metadata": {
44 | "id": "T7AkwKPAGtED",
45 | "outputId": "ba2bc7b3-c90d-400f-c421-95b880e97502",
46 | "colab": {
47 | "base_uri": "https://localhost:8080/"
48 | }
49 | },
50 | "source": [
51 | "#Store the values of two times table in a list and print that list\n",
52 | "twoTimes= []\n",
53 | "for i in range(1,13):\n",
54 | " twoTimes.append(2*i)\n",
55 | "\n",
56 | "print(twoTimes)"
57 | ],
58 | "execution_count": 1,
59 | "outputs": [
60 | {
61 | "output_type": "stream",
62 | "text": [
63 | "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]\n"
64 | ],
65 | "name": "stdout"
66 | }
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "metadata": {
72 | "id": "cANU9mHmIYrL"
73 | },
74 | "source": [
75 | "#Using list comprehension for pring two times table\n",
76 | "tableTwo= [2*i for i in range(0,13)]\n",
77 | "print(tableTwo)"
78 | ],
79 | "execution_count": null,
80 | "outputs": []
81 | },
82 | {
83 | "cell_type": "code",
84 | "metadata": {
85 | "id": "fqLUqW_JKTwf"
86 | },
87 | "source": [
88 | "#storing each letter of the given sentence in a list\n",
89 | "str1= \"Good morning Mumbai\"\n",
90 | "letterList=[]\n",
91 | "for letter in str1:\n",
92 | " letterList.append(letter)\n",
93 | "print(letterList)"
94 | ],
95 | "execution_count": null,
96 | "outputs": []
97 | },
98 | {
99 | "cell_type": "code",
100 | "metadata": {
101 | "id": "ZVDZPpEUK5wr"
102 | },
103 | "source": [
104 | "#using list comprehension for storing each letter of the given sentence in a list\n",
105 | "str1= \"Good morning Mumbai\"\n",
106 | "listLetter= [char for char in str1]\n",
107 | "print(listLetter)"
108 | ],
109 | "execution_count": null,
110 | "outputs": []
111 | },
112 | {
113 | "cell_type": "markdown",
114 | "metadata": {
115 | "id": "rsdrmu8ELdLl"
116 | },
117 | "source": [
118 | "Using condition in list comprehension"
119 | ]
120 | },
121 | {
122 | "cell_type": "code",
123 | "metadata": {
124 | "id": "ceS_lz6ULhRJ"
125 | },
126 | "source": [
127 | "#print odd numbers\n",
128 | "odd= [i for i in range(20) if i%2!=0]\n",
129 | "print(odd)"
130 | ],
131 | "execution_count": null,
132 | "outputs": []
133 | },
134 | {
135 | "cell_type": "code",
136 | "metadata": {
137 | "id": "vHi6aS6yL3TU"
138 | },
139 | "source": [
140 | "#Print odd or even\n",
141 | "\n",
142 | "num= [\"odd\" if i%2!=0 else \"even\" for i in range(21)]\n",
143 | "print(num)"
144 | ],
145 | "execution_count": null,
146 | "outputs": []
147 | },
148 | {
149 | "cell_type": "code",
150 | "metadata": {
151 | "id": "re_YDWGiIMdW"
152 | },
153 | "source": [
154 | "#DIY Create a list with squares of integers from 1 to 10"
155 | ],
156 | "execution_count": null,
157 | "outputs": []
158 | },
159 | {
160 | "cell_type": "code",
161 | "metadata": {
162 | "id": "xkR56uAZIbQG"
163 | },
164 | "source": [
165 | "#DIY Create list of integers with even squares"
166 | ],
167 | "execution_count": null,
168 | "outputs": []
169 | },
170 | {
171 | "cell_type": "code",
172 | "metadata": {
173 | "id": "we5tQSU9IpLG"
174 | },
175 | "source": [
176 | "#Add each element of list X to each element of list Y without using list comprehension\n",
177 | "\n",
178 | "X-"
179 | ],
180 | "execution_count": null,
181 | "outputs": []
182 | },
183 | {
184 | "cell_type": "markdown",
185 | "metadata": {
186 | "id": "IzOYBcLOOvzK"
187 | },
188 | "source": [
189 | "Dictionary Comprehension"
190 | ]
191 | },
192 | {
193 | "cell_type": "code",
194 | "metadata": {
195 | "colab": {
196 | "base_uri": "https://localhost:8080/"
197 | },
198 | "id": "_rN4MTU5LQ-T",
199 | "outputId": "2da98453-7284-423b-f006-59e440472391"
200 | },
201 | "source": [
202 | "#Two times table\n",
203 | "twoTimes={}\n",
204 | "for i in range (1,13):\n",
205 | " twoTimes[i]=2*i\n",
206 | "print(twoTimes)"
207 | ],
208 | "execution_count": null,
209 | "outputs": [
210 | {
211 | "output_type": "stream",
212 | "text": [
213 | "{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24}\n"
214 | ],
215 | "name": "stdout"
216 | }
217 | ]
218 | },
219 | {
220 | "cell_type": "code",
221 | "metadata": {
222 | "colab": {
223 | "base_uri": "https://localhost:8080/"
224 | },
225 | "id": "1ZqzenY8QplN",
226 | "outputId": "d0e92639-fc41-4e3a-9452-b1685571e350"
227 | },
228 | "source": [
229 | "#Two times table using dictionary comprehension\n",
230 | "\n",
231 | "tableTwo= {i:2*i for i in range(1,13) }\n",
232 | "print(tableTwo)"
233 | ],
234 | "execution_count": null,
235 | "outputs": [
236 | {
237 | "output_type": "stream",
238 | "text": [
239 | "{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22, 12: 24}\n"
240 | ],
241 | "name": "stdout"
242 | }
243 | ]
244 | },
245 | {
246 | "cell_type": "code",
247 | "metadata": {
248 | "colab": {
249 | "base_uri": "https://localhost:8080/"
250 | },
251 | "id": "aSuuHeLMRP-w",
252 | "outputId": "dc729efc-1d32-475d-cd8f-b41c8ba043d3"
253 | },
254 | "source": [
255 | "# Count number of letters in the words\n",
256 | "\n",
257 | "words= input().split(',')\n",
258 | "wordCount= {fruit:len(fruit) for fruit in words}\n",
259 | "print(wordCount)\n"
260 | ],
261 | "execution_count": null,
262 | "outputs": [
263 | {
264 | "output_type": "stream",
265 | "text": [
266 | "banana,apple,mango,grapes,orange\n",
267 | "{'banana': 6, 'apple': 5, 'mango': 5, 'grapes': 6, 'orange': 6}\n"
268 | ],
269 | "name": "stdout"
270 | }
271 | ]
272 | },
273 | {
274 | "cell_type": "code",
275 | "metadata": {
276 | "colab": {
277 | "base_uri": "https://localhost:8080/"
278 | },
279 | "id": "5vt5AJxFShy_",
280 | "outputId": "c18190ad-c0f8-4991-b59a-347d744762d0"
281 | },
282 | "source": [
283 | "#dictionary comprehension with two variables\n",
284 | "students= [\"Ram\", \"Rahul\", \"Seema\", \"Priya\"]\n",
285 | "marks= [25, 27, 24, 26]\n",
286 | "result={s:m for s,m in zip(students,marks)}\n",
287 | "print(result)"
288 | ],
289 | "execution_count": null,
290 | "outputs": [
291 | {
292 | "output_type": "stream",
293 | "text": [
294 | "{'Ram': 25, 'Rahul': 27, 'Seema': 24, 'Priya': 26}\n"
295 | ],
296 | "name": "stdout"
297 | }
298 | ]
299 | },
300 | {
301 | "cell_type": "code",
302 | "metadata": {
303 | "id": "aW-H6VcLSoQ5"
304 | },
305 | "source": [
306 | ""
307 | ],
308 | "execution_count": null,
309 | "outputs": []
310 | }
311 | ]
312 | }
313 |
--------------------------------------------------------------------------------
/3_2_RecursiveFunctions.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Unit3.2_RecursiveFunctions.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyO+zBda5FhV24WNoMo7PF4q",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "4Yw31G3RzS2S"
34 | },
35 | "source": [
36 | "Recursive function
\n",
37 | "\n",
38 | "A function that calls itself is called as recursive function\n"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "metadata": {
44 | "id": "dMGtJgMF0YUa"
45 | },
46 | "source": [
47 | "#Write a program to calculate factorial of a number using recursive function\n",
48 | "\n",
49 | "def fact(n):\n",
50 | " if n==0:\n",
51 | " result=1\n",
52 | " else:\n",
53 | " result= n*fact(n-1)\n",
54 | " return result\n"
55 | ],
56 | "execution_count": null,
57 | "outputs": []
58 | },
59 | {
60 | "cell_type": "code",
61 | "metadata": {
62 | "colab": {
63 | "base_uri": "https://localhost:8080/"
64 | },
65 | "id": "e81MljWO09-d",
66 | "outputId": "2d7d7423-29e3-4a67-b43b-8b12ecf9e2c0"
67 | },
68 | "source": [
69 | "print(fact(6))"
70 | ],
71 | "execution_count": null,
72 | "outputs": [
73 | {
74 | "output_type": "stream",
75 | "text": [
76 | "720\n"
77 | ],
78 | "name": "stdout"
79 | }
80 | ]
81 | },
82 | {
83 | "cell_type": "code",
84 | "metadata": {
85 | "colab": {
86 | "base_uri": "https://localhost:8080/"
87 | },
88 | "id": "QnRZciNu1HNs",
89 | "outputId": "8e655950-752e-4111-bbc2-a07bc0ab08c9"
90 | },
91 | "source": [
92 | "#Print factorial of first 1 to 5 numbers\n",
93 | "\n",
94 | "for i in range(1,6):\n",
95 | " print(\"Factorial of {} is {}\".format(i,fact(i)))"
96 | ],
97 | "execution_count": null,
98 | "outputs": [
99 | {
100 | "output_type": "stream",
101 | "text": [
102 | "Factorial of 1 is 0\n",
103 | "Factorial of 2 is 2\n",
104 | "Factorial of 3 is 6\n",
105 | "Factorial of 4 is 12\n",
106 | "Factorial of 5 is 20\n"
107 | ],
108 | "name": "stdout"
109 | }
110 | ]
111 | },
112 | {
113 | "cell_type": "code",
114 | "metadata": {
115 | "id": "J07qUc1A3lqW"
116 | },
117 | "source": [
118 | "#Obtain sum of range of numbers using recursion\n",
119 | "\n",
120 | "def cumm(n):\n",
121 | " \n",
122 | " if n==1:\n",
123 | " result=1\n",
124 | " else:\n",
125 | " result=n+cumm(n-1)\n",
126 | " return result\n",
127 | "\n"
128 | ],
129 | "execution_count": null,
130 | "outputs": []
131 | },
132 | {
133 | "cell_type": "code",
134 | "metadata": {
135 | "colab": {
136 | "base_uri": "https://localhost:8080/"
137 | },
138 | "id": "ad6aeAzxAHAJ",
139 | "outputId": "5aa08f8b-8efc-4dd1-a356-4146ecabd7df"
140 | },
141 | "source": [
142 | "print(cumm(4))"
143 | ],
144 | "execution_count": null,
145 | "outputs": [
146 | {
147 | "output_type": "stream",
148 | "text": [
149 | "10\n"
150 | ],
151 | "name": "stdout"
152 | }
153 | ]
154 | },
155 | {
156 | "cell_type": "code",
157 | "metadata": {
158 | "id": "JKMu_UVwA7pR"
159 | },
160 | "source": [
161 | "#compute fibonnaci series using recursion"
162 | ],
163 | "execution_count": null,
164 | "outputs": []
165 | },
166 | {
167 | "cell_type": "code",
168 | "metadata": {
169 | "id": "dLfq0a8o5jZA"
170 | },
171 | "source": [
172 | ""
173 | ],
174 | "execution_count": null,
175 | "outputs": []
176 | }
177 | ]
178 | }
179 |
--------------------------------------------------------------------------------
/3_3_LambdaFunctions.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": []
7 | },
8 | "kernelspec": {
9 | "name": "python3",
10 | "display_name": "Python 3"
11 | },
12 | "language_info": {
13 | "name": "python"
14 | }
15 | },
16 | "cells": [
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {
20 | "id": "GW6lGKW8FbgI"
21 | },
22 | "source": [
23 | "Lambda Functions
\n",
24 | "\n",
25 | "A lambda function is a small anonymous function.\n",
26 | "\n",
27 | "A lambda function can take any number of arguments, but can only have one expression.\n",
28 | "\n",
29 | "They do not use def of return key words. These are implicit in lambda\n",
30 | "\n"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "metadata": {
36 | "id": "JVo2aO2kGCDR"
37 | },
38 | "source": [
39 | "#Function to find cube of a number\n",
40 | "def cube(x):\n",
41 | " return x**3"
42 | ],
43 | "execution_count": null,
44 | "outputs": []
45 | },
46 | {
47 | "cell_type": "code",
48 | "metadata": {
49 | "colab": {
50 | "base_uri": "https://localhost:8080/"
51 | },
52 | "id": "kNRCkY3PGLAf",
53 | "outputId": "2478b142-ba88-4445-9e55-2099862e9d6e"
54 | },
55 | "source": [
56 | "x= int(input(\"Enter the number: \"))\n",
57 | "print(\"Cube of the number {} is {}\".format(x,cube(x)))"
58 | ],
59 | "execution_count": null,
60 | "outputs": [
61 | {
62 | "output_type": "stream",
63 | "text": [
64 | "Enter the number: 6\n",
65 | "Cube of the number 6 is 216\n"
66 | ],
67 | "name": "stdout"
68 | }
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "metadata": {
74 | "id": "5cR198_bFThi"
75 | },
76 | "source": [
77 | "#lambda function to find cube of a number\n",
78 | "\n",
79 | "cube_new= lambda y: y**3"
80 | ],
81 | "execution_count": null,
82 | "outputs": []
83 | },
84 | {
85 | "cell_type": "code",
86 | "metadata": {
87 | "id": "JQaFlnD2KfmH",
88 | "colab": {
89 | "base_uri": "https://localhost:8080/"
90 | },
91 | "outputId": "e2474cca-a61d-4b41-861d-0dbd2ed345f6"
92 | },
93 | "source": [
94 | "print(cube_new(6))"
95 | ],
96 | "execution_count": null,
97 | "outputs": [
98 | {
99 | "output_type": "stream",
100 | "text": [
101 | "216\n"
102 | ],
103 | "name": "stdout"
104 | }
105 | ]
106 | },
107 | {
108 | "cell_type": "code",
109 | "metadata": {
110 | "id": "FAkgVKBcF4tM"
111 | },
112 | "source": [
113 | "# Function with two values passed in\n",
114 | "\n",
115 | "def add_num(x,y):\n",
116 | " return x+y"
117 | ],
118 | "execution_count": null,
119 | "outputs": []
120 | },
121 | {
122 | "cell_type": "code",
123 | "metadata": {
124 | "colab": {
125 | "base_uri": "https://localhost:8080/"
126 | },
127 | "id": "qMeA9mgCMEEc",
128 | "outputId": "80ef2a98-f6fd-4ecf-c7f6-1f394475ddaa"
129 | },
130 | "source": [
131 | "a,b= input(\"Enter two integers separated by comma \").split(\",\") #Take the input form the user\n",
132 | "print(add_num(int(a),int(b)))"
133 | ],
134 | "execution_count": null,
135 | "outputs": [
136 | {
137 | "output_type": "stream",
138 | "text": [
139 | "Enter two integers separated by comma 6, -8\n",
140 | "-2\n"
141 | ],
142 | "name": "stdout"
143 | }
144 | ]
145 | },
146 | {
147 | "cell_type": "code",
148 | "metadata": {
149 | "id": "4APk3hEDMadK"
150 | },
151 | "source": [
152 | "#Lambda function with two variables\n",
153 | "\n",
154 | "add_new= lambda x,y: x+y\n"
155 | ],
156 | "execution_count": null,
157 | "outputs": []
158 | },
159 | {
160 | "cell_type": "code",
161 | "metadata": {
162 | "colab": {
163 | "base_uri": "https://localhost:8080/"
164 | },
165 | "id": "XJEJdOVsMuej",
166 | "outputId": "2cc95af4-8f1a-495b-9c5b-aa82f0929cc7"
167 | },
168 | "source": [
169 | "print(add_new(6,-8))"
170 | ],
171 | "execution_count": null,
172 | "outputs": [
173 | {
174 | "output_type": "stream",
175 | "text": [
176 | "-2\n"
177 | ],
178 | "name": "stdout"
179 | }
180 | ]
181 | },
182 | {
183 | "cell_type": "code",
184 | "metadata": {
185 | "id": "o9I7CNVFMw9C"
186 | },
187 | "source": [
188 | "#Function to find max of two numbers\n",
189 | "\n",
190 | "def max_num(x,y):\n",
191 | " if x>y:\n",
192 | " return x\n",
193 | " else:\n",
194 | " return y\n"
195 | ],
196 | "execution_count": null,
197 | "outputs": []
198 | },
199 | {
200 | "cell_type": "code",
201 | "metadata": {
202 | "colab": {
203 | "base_uri": "https://localhost:8080/"
204 | },
205 | "id": "qpDleOdjNLt3",
206 | "outputId": "2ccd553e-60ec-47d8-ce8f-10fc3ac4546e"
207 | },
208 | "source": [
209 | "a,b= input(\"Enter two integers separated by comma \").split(\",\")\n",
210 | "print(max_num(int(a), int(b)))"
211 | ],
212 | "execution_count": null,
213 | "outputs": [
214 | {
215 | "output_type": "stream",
216 | "text": [
217 | "Enter two integers separated by comma 6, -9\n",
218 | "6\n"
219 | ],
220 | "name": "stdout"
221 | }
222 | ]
223 | },
224 | {
225 | "cell_type": "code",
226 | "metadata": {
227 | "id": "iXFHjjPTNbBk"
228 | },
229 | "source": [
230 | "#Print max of two numbers using lambda function\n",
231 | "\n",
232 | "max_new= lambda x,y: x if x>y else y\n"
233 | ],
234 | "execution_count": null,
235 | "outputs": []
236 | },
237 | {
238 | "cell_type": "code",
239 | "metadata": {
240 | "id": "M0xRzQ9BNsxz",
241 | "colab": {
242 | "base_uri": "https://localhost:8080/"
243 | },
244 | "outputId": "052f2552-23b1-4439-ef79-1375fc506b2a"
245 | },
246 | "source": [
247 | "max_new(6,-9)"
248 | ],
249 | "execution_count": null,
250 | "outputs": [
251 | {
252 | "output_type": "execute_result",
253 | "data": {
254 | "text/plain": [
255 | "6"
256 | ]
257 | },
258 | "metadata": {
259 | "tags": []
260 | },
261 | "execution_count": 37
262 | }
263 | ]
264 | },
265 | {
266 | "cell_type": "code",
267 | "metadata": {
268 | "id": "Xl9wfFMS8rJN"
269 | },
270 | "source": [],
271 | "execution_count": null,
272 | "outputs": []
273 | }
274 | ]
275 | }
--------------------------------------------------------------------------------
/3_4_MapFilter.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": []
7 | },
8 | "kernelspec": {
9 | "name": "python3",
10 | "display_name": "Python 3"
11 | },
12 | "language_info": {
13 | "name": "python"
14 | }
15 | },
16 | "cells": [
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {
20 | "id": "jYF9-ewmdPBq"
21 | },
22 | "source": [
23 | "map()\n",
24 | "\n",
25 | "It is Python's built-in function to used to apply the given function to all the iterables. The returned value from map() (map object) can then be passed to functions like list() (to create a list), set() (to create a set) and so on."
26 | ]
27 | },
28 | {
29 | "cell_type": "code",
30 | "metadata": {
31 | "id": "mjnXemWmOQVT"
32 | },
33 | "source": [
34 | "#Function to obtain square of numbers\n",
35 | "\n",
36 | "def squares(n):\n",
37 | " return n**2"
38 | ],
39 | "execution_count": null,
40 | "outputs": []
41 | },
42 | {
43 | "cell_type": "code",
44 | "source": [
45 | "num=[1,2,3,4]\n",
46 | "y=map(squares,num)\n",
47 | "print(list(y))"
48 | ],
49 | "metadata": {
50 | "colab": {
51 | "base_uri": "https://localhost:8080/"
52 | },
53 | "id": "lPlyG3HbSh_L",
54 | "outputId": "df2478fc-4a79-4faa-9701-75c9b1548ee6"
55 | },
56 | "execution_count": null,
57 | "outputs": [
58 | {
59 | "output_type": "stream",
60 | "name": "stdout",
61 | "text": [
62 | "[1, 4, 9, 16]\n"
63 | ]
64 | }
65 | ]
66 | },
67 | {
68 | "cell_type": "code",
69 | "metadata": {
70 | "id": "Rm_w8uC8eosw"
71 | },
72 | "source": [
73 | "#Use map to obtain square of nos in the given tuple\n",
74 | "num= (1,2,3,4)\n",
75 | "\n",
76 | "z= list(map(squares, num))\n"
77 | ],
78 | "execution_count": null,
79 | "outputs": []
80 | },
81 | {
82 | "cell_type": "code",
83 | "metadata": {
84 | "colab": {
85 | "base_uri": "https://localhost:8080/"
86 | },
87 | "id": "29GjLmEAUY5l",
88 | "outputId": "4a22a42a-9313-495a-db20-94cda245d6e3"
89 | },
90 | "source": [
91 | "print(z)"
92 | ],
93 | "execution_count": null,
94 | "outputs": [
95 | {
96 | "output_type": "stream",
97 | "text": [
98 | "[1, 4, 9, 16]\n"
99 | ],
100 | "name": "stdout"
101 | }
102 | ]
103 | },
104 | {
105 | "cell_type": "code",
106 | "metadata": {
107 | "id": "8xGvJs3geyjg"
108 | },
109 | "source": [
110 | "#Function to find length of each word\n",
111 | "\n",
112 | "def word_len(n):\n",
113 | " return len(n)\n",
114 | "\n"
115 | ],
116 | "execution_count": null,
117 | "outputs": []
118 | },
119 | {
120 | "cell_type": "code",
121 | "metadata": {
122 | "colab": {
123 | "base_uri": "https://localhost:8080/"
124 | },
125 | "id": "1x8usUuXVSFe",
126 | "outputId": "2b4617d9-dd10-4d13-ee03-7d9a4142de75"
127 | },
128 | "source": [
129 | "print(word_len(\"Mumbai\"))"
130 | ],
131 | "execution_count": null,
132 | "outputs": [
133 | {
134 | "output_type": "stream",
135 | "text": [
136 | "6\n"
137 | ],
138 | "name": "stdout"
139 | }
140 | ]
141 | },
142 | {
143 | "cell_type": "code",
144 | "metadata": {
145 | "id": "OsVdJaqZgfdp",
146 | "colab": {
147 | "base_uri": "https://localhost:8080/"
148 | },
149 | "outputId": "a18738c9-c70f-40d0-c65b-6b64660bc51a"
150 | },
151 | "source": [
152 | "#Using map , calculate length of each word in the given tuple/list\n",
153 | "\n",
154 | "names= [\"Jeet\", \"Jiya\", \"Ahaan\", \"Aarya\", \"Vanishka\"]\n",
155 | "\n",
156 | "length_string= list(map(word_len,names))\n",
157 | "\n",
158 | "print(length_string)\n"
159 | ],
160 | "execution_count": null,
161 | "outputs": [
162 | {
163 | "output_type": "stream",
164 | "name": "stdout",
165 | "text": [
166 | "[4, 4, 5, 5, 8]\n"
167 | ]
168 | }
169 | ]
170 | },
171 | {
172 | "cell_type": "code",
173 | "metadata": {
174 | "id": "mPI2OymJhEXt"
175 | },
176 | "source": [
177 | "#Function to concatenate two strings\n",
178 | "\n",
179 | "def myfunc(a, b):\n",
180 | " return a + b\n",
181 | "\n"
182 | ],
183 | "execution_count": null,
184 | "outputs": []
185 | },
186 | {
187 | "cell_type": "code",
188 | "metadata": {
189 | "colab": {
190 | "base_uri": "https://localhost:8080/"
191 | },
192 | "id": "xUQmKFbbV71f",
193 | "outputId": "172d6795-3212-402e-f2f4-1d7871d77923"
194 | },
195 | "source": [
196 | "greet= [\"Hello\", \"Good morning\", \"Thank you\"]\n",
197 | "names= names= [\" Jeet\", \" Jiya\", \" Ahaan\"]\n",
198 | "\n",
199 | "greeting= list(map(myfunc, greet, names))\n",
200 | "print(greeting)"
201 | ],
202 | "execution_count": null,
203 | "outputs": [
204 | {
205 | "output_type": "stream",
206 | "text": [
207 | "['Hello Jeet', 'Good morning Jiya', 'Thank you Ahaan']\n"
208 | ],
209 | "name": "stdout"
210 | }
211 | ]
212 | },
213 | {
214 | "cell_type": "code",
215 | "metadata": {
216 | "id": "z-s6yf1neVhT"
217 | },
218 | "source": [
219 | "#Function to multiple by two\n",
220 | "def mul(i):\n",
221 | " return 2*i\n",
222 | ""
223 | ],
224 | "execution_count": null,
225 | "outputs": []
226 | },
227 | {
228 | "cell_type": "code",
229 | "metadata": {
230 | "id": "ixH-fUj-fR7p",
231 | "colab": {
232 | "base_uri": "https://localhost:8080/"
233 | },
234 | "outputId": "d43cd4cd-9fda-4584-abd3-75a3a207b64e"
235 | },
236 | "source": [
237 | "#two times table using map()\n",
238 | "\n",
239 | "table= list(map(mul, list(range(1,13))))\n",
240 | "print(table)"
241 | ],
242 | "execution_count": null,
243 | "outputs": [
244 | {
245 | "output_type": "stream",
246 | "text": [
247 | "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]\n"
248 | ],
249 | "name": "stdout"
250 | }
251 | ]
252 | },
253 | {
254 | "cell_type": "markdown",
255 | "metadata": {
256 | "id": "d7puZvkGgQHa"
257 | },
258 | "source": [
259 | "Using lambda function with map()"
260 | ]
261 | },
262 | {
263 | "cell_type": "code",
264 | "metadata": {
265 | "id": "hyuHdKmmg2N2"
266 | },
267 | "source": [
268 | "#Two times table using lambda function with map()\n",
269 | "\n",
270 | "num=list(range(1,13))\n",
271 | "\n",
272 | "table_2= list(map(lambda i: 2*i, num))\n",
273 | "\n"
274 | ],
275 | "execution_count": null,
276 | "outputs": []
277 | },
278 | {
279 | "cell_type": "code",
280 | "metadata": {
281 | "colab": {
282 | "base_uri": "https://localhost:8080/"
283 | },
284 | "id": "bcCiV53tZZi3",
285 | "outputId": "f43229fa-f543-4b61-f387-ee542de32be8"
286 | },
287 | "source": [
288 | "print(table_2)"
289 | ],
290 | "execution_count": null,
291 | "outputs": [
292 | {
293 | "output_type": "stream",
294 | "text": [
295 | "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]\n"
296 | ],
297 | "name": "stdout"
298 | }
299 | ]
300 | },
301 | {
302 | "cell_type": "code",
303 | "metadata": {
304 | "id": "itpVFxpMg_ZW",
305 | "colab": {
306 | "base_uri": "https://localhost:8080/"
307 | },
308 | "outputId": "3d53b966-b951-47f3-8b11-e59db0461271"
309 | },
310 | "source": [
311 | "#Convert all the letters in the strings in the given list to upper characters\n",
312 | "\n",
313 | "fruits= [\"banana\", \"mango\", \"apple\", \"grapes\"]\n",
314 | "\n",
315 | "upper_case= list(map(lambda s:s.upper(), fruits))\n",
316 | "\n",
317 | "print(upper_case)\n"
318 | ],
319 | "execution_count": null,
320 | "outputs": [
321 | {
322 | "output_type": "stream",
323 | "text": [
324 | "['BANANA', 'MANGO', 'APPLE', 'GRAPES']\n"
325 | ],
326 | "name": "stdout"
327 | }
328 | ]
329 | },
330 | {
331 | "cell_type": "code",
332 | "metadata": {
333 | "id": "lQv6IXeyanPX"
334 | },
335 | "source": [
336 | "upper_case(\"school\")"
337 | ],
338 | "execution_count": null,
339 | "outputs": []
340 | },
341 | {
342 | "cell_type": "code",
343 | "metadata": {
344 | "id": "ruDdqYYZkmax"
345 | },
346 | "source": [
347 | "#Add a string in the dictionary value\n",
348 | "\n",
349 | "wages= {\"A\": \"2500\", \"B\": \"3000\", \"C\": \"3700\"}\n",
350 | "\n",
351 | "new_wages= dict(map(lambda d: (d[0], \"Rs\"+d[1]), wages.items()))\n",
352 | "\n"
353 | ],
354 | "execution_count": null,
355 | "outputs": []
356 | },
357 | {
358 | "cell_type": "code",
359 | "metadata": {
360 | "id": "LDJWMCuVcn14",
361 | "colab": {
362 | "base_uri": "https://localhost:8080/"
363 | },
364 | "outputId": "e2aeee40-a35c-4f3a-c115-23b5f6ba4e58"
365 | },
366 | "source": [
367 | "print(new_wages)"
368 | ],
369 | "execution_count": null,
370 | "outputs": [
371 | {
372 | "output_type": "stream",
373 | "text": [
374 | "{'A': 'Rs2500', 'B': 'Rs3000', 'C': 'Rs3700'}\n"
375 | ],
376 | "name": "stdout"
377 | }
378 | ]
379 | },
380 | {
381 | "cell_type": "code",
382 | "metadata": {
383 | "colab": {
384 | "base_uri": "https://localhost:8080/"
385 | },
386 | "id": "Ab4RqeqDbgO5",
387 | "outputId": "5a0a3515-7b62-450a-c245-97108bf8e5bd"
388 | },
389 | "source": [
390 | "for d in wages.items():\n",
391 | " print(d)\n",
392 | " print(d[0])\n",
393 | " print(d[1])\n"
394 | ],
395 | "execution_count": null,
396 | "outputs": [
397 | {
398 | "output_type": "stream",
399 | "text": [
400 | "('A', '2500')\n",
401 | "A\n",
402 | "2500\n",
403 | "('B', '3000')\n",
404 | "B\n",
405 | "3000\n",
406 | "('C', '3700')\n",
407 | "C\n",
408 | "3700\n"
409 | ],
410 | "name": "stdout"
411 | }
412 | ]
413 | },
414 | {
415 | "cell_type": "code",
416 | "metadata": {
417 | "colab": {
418 | "base_uri": "https://localhost:8080/"
419 | },
420 | "id": "JCRdBmUvmHp5",
421 | "outputId": "3f9d32fb-3728-45e7-893b-953e55974152"
422 | },
423 | "source": [
424 | "#update dictionary from two lists using map and lambda function\n",
425 | "\n",
426 | "students=[\"Jeet\", \"Jiya\", \"Rohan\"]\n",
427 | "marks= [15,14,13]\n",
428 | "\n",
429 | "dict1= dict(map(lambda key,value:(key,value), students,marks))\n",
430 | "print(dict1)"
431 | ],
432 | "execution_count": null,
433 | "outputs": [
434 | {
435 | "output_type": "stream",
436 | "text": [
437 | "{'Jeet': 15, 'Jiya': 14, 'Rohan': 13}\n"
438 | ],
439 | "name": "stdout"
440 | }
441 | ]
442 | },
443 | {
444 | "cell_type": "code",
445 | "metadata": {
446 | "id": "r72zIYKcnJqX",
447 | "colab": {
448 | "base_uri": "https://localhost:8080/"
449 | },
450 | "outputId": "35908d5a-ecbc-4f7b-81ba-e97402f3f0cf"
451 | },
452 | "source": [
453 | "#Write a lamda function using map that returns product of the elements in two lists\n",
454 | "lst1= [1,2,3,4,5]\n",
455 | "lst2= [5,4,3,2,1]\n",
456 | "\n",
457 | "lst3= list(map(lambda x,y:x*y,lst1,lst2))\n",
458 | "print(lst3)"
459 | ],
460 | "execution_count": null,
461 | "outputs": [
462 | {
463 | "output_type": "stream",
464 | "text": [
465 | "[5, 8, 9, 8, 5]\n"
466 | ],
467 | "name": "stdout"
468 | }
469 | ]
470 | },
471 | {
472 | "cell_type": "markdown",
473 | "metadata": {
474 | "id": "-MoVQqmYN66C"
475 | },
476 | "source": [
477 | "Filter()\n",
478 | "It filters the array and returns a new array with filtered value"
479 | ]
480 | },
481 | {
482 | "cell_type": "code",
483 | "metadata": {
484 | "id": "RoNxv-wJNdTT"
485 | },
486 | "source": [
487 | "#Program to display all the even nos\n",
488 | "\n",
489 | "even = lambda x : True if x%2==0 else False"
490 | ],
491 | "execution_count": null,
492 | "outputs": []
493 | },
494 | {
495 | "cell_type": "code",
496 | "metadata": {
497 | "colab": {
498 | "base_uri": "https://localhost:8080/"
499 | },
500 | "id": "If8DybpKURHU",
501 | "outputId": "a1d5d211-962b-4637-bdcd-e9aafd9eaa18"
502 | },
503 | "source": [
504 | "even(7)"
505 | ],
506 | "execution_count": null,
507 | "outputs": [
508 | {
509 | "output_type": "execute_result",
510 | "data": {
511 | "text/plain": [
512 | "False"
513 | ]
514 | },
515 | "metadata": {},
516 | "execution_count": 2
517 | }
518 | ]
519 | },
520 | {
521 | "cell_type": "code",
522 | "metadata": {
523 | "id": "x80JT4YBUT2j"
524 | },
525 | "source": [
526 | "even1= lambda x: x%2==0"
527 | ],
528 | "execution_count": null,
529 | "outputs": []
530 | },
531 | {
532 | "cell_type": "code",
533 | "metadata": {
534 | "colab": {
535 | "base_uri": "https://localhost:8080/"
536 | },
537 | "id": "yczSX7UYUbw0",
538 | "outputId": "715216f0-7279-44f0-95eb-846128b3c1cf"
539 | },
540 | "source": [
541 | "even1(7)"
542 | ],
543 | "execution_count": null,
544 | "outputs": [
545 | {
546 | "output_type": "execute_result",
547 | "data": {
548 | "text/plain": [
549 | "False"
550 | ]
551 | },
552 | "metadata": {},
553 | "execution_count": 4
554 | }
555 | ]
556 | },
557 | {
558 | "cell_type": "code",
559 | "metadata": {
560 | "colab": {
561 | "base_uri": "https://localhost:8080/"
562 | },
563 | "id": "I7pS4FvgUtSl",
564 | "outputId": "d5121495-7c65-4584-9f25-75c16c77a9d8"
565 | },
566 | "source": [
567 | "#Program to display all the even nos in the list using filter\n",
568 | "nums= [4,7,8,2,3,5,7,9,11]\n",
569 | "even_nos= list(filter(even, nums))\n",
570 | "print(even_nos)"
571 | ],
572 | "execution_count": null,
573 | "outputs": [
574 | {
575 | "output_type": "stream",
576 | "text": [
577 | "[4, 8, 2]\n"
578 | ],
579 | "name": "stdout"
580 | }
581 | ]
582 | },
583 | {
584 | "cell_type": "code",
585 | "metadata": {
586 | "id": "fOaWdXkXXO1w"
587 | },
588 | "source": [
589 | "#Program to display all the nos which are square in the given list using filter()\n",
590 | "\n",
591 | "import math\n",
592 | "\n",
593 | "root= lambda x: True if math.sqrt(x)%1==0 else False # if the number is divisible by 1, remainder is zero\n"
594 | ],
595 | "execution_count": null,
596 | "outputs": []
597 | },
598 | {
599 | "cell_type": "code",
600 | "metadata": {
601 | "id": "EhxDycT2Hqpc",
602 | "colab": {
603 | "base_uri": "https://localhost:8080/"
604 | },
605 | "outputId": "7768cc44-fd97-4414-b866-d1b243ea204f"
606 | },
607 | "source": [
608 | "num= list(range(1,101))\n",
609 | "print(num)"
610 | ],
611 | "execution_count": null,
612 | "outputs": [
613 | {
614 | "output_type": "stream",
615 | "text": [
616 | "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]\n"
617 | ],
618 | "name": "stdout"
619 | }
620 | ]
621 | },
622 | {
623 | "cell_type": "code",
624 | "metadata": {
625 | "colab": {
626 | "base_uri": "https://localhost:8080/"
627 | },
628 | "id": "fBGF2hnZIi94",
629 | "outputId": "1edd68d6-061d-4c9e-a3d5-8eafb3fffecd"
630 | },
631 | "source": [
632 | "#Using filter print the values which are squares\n",
633 | "\n",
634 | "squares=list(filter(root, num))\n",
635 | "print(squares)"
636 | ],
637 | "execution_count": null,
638 | "outputs": [
639 | {
640 | "output_type": "stream",
641 | "text": [
642 | "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n"
643 | ],
644 | "name": "stdout"
645 | }
646 | ]
647 | },
648 | {
649 | "cell_type": "code",
650 | "metadata": {
651 | "id": "A2r2ezeTI5in"
652 | },
653 | "source": [],
654 | "execution_count": null,
655 | "outputs": []
656 | }
657 | ]
658 | }
--------------------------------------------------------------------------------
/3_5_ReduceZipEnumerate.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": []
7 | },
8 | "kernelspec": {
9 | "name": "python3",
10 | "display_name": "Python 3"
11 | },
12 | "language_info": {
13 | "name": "python"
14 | }
15 | },
16 | "cells": [
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {
20 | "id": "TDG3oBy3K53c"
21 | },
22 | "source": [
23 | "reduce()\n",
24 | "This function implements a mathematical technique called folding or reduction.
\n",
25 | "It is useful when you need to apply a function to an iterable and reduce it to a single cumulative value\n",
26 | "\n",
27 | "Note reduce is a function in functools package. So import it from functool"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "metadata": {
33 | "id": "AohrLxDfKetm",
34 | "colab": {
35 | "base_uri": "https://localhost:8080/"
36 | },
37 | "outputId": "e407ca60-5dac-40a4-d63c-78ed933dd051"
38 | },
39 | "source": [
40 | "#Multiply all the numbers in the list to obtain the final product\n",
41 | "from functools import reduce\n",
42 | "\n",
43 | "lst1= list(range(1,5))\n",
44 | "reduce(lambda x,y:x*y, lst1)\n"
45 | ],
46 | "execution_count": null,
47 | "outputs": [
48 | {
49 | "output_type": "execute_result",
50 | "data": {
51 | "text/plain": [
52 | "24"
53 | ]
54 | },
55 | "metadata": {},
56 | "execution_count": 10
57 | }
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "metadata": {
63 | "colab": {
64 | "base_uri": "https://localhost:8080/"
65 | },
66 | "id": "h97cm7l9lpZt",
67 | "outputId": "3492ce4a-8b3c-4afc-a057-b39090480ebf"
68 | },
69 | "source": [
70 | "#Sum all the nos in the list\n",
71 | "from functools import reduce\n",
72 | "lst2= [int(a) for a in input().split()] #using list comprehension to take the list of nos from the user\n",
73 | "sumNos= lambda a,b: a+b #Function to add two nos\n",
74 | "#list3= list(map(sumNos,lst2))\n",
75 | "print(lst2)\n",
76 | "#print(list3)\n",
77 | "sumList= reduce(sumNos, lst2) #Using reduce() to obtain sum of all the nos in the list"
78 | ],
79 | "execution_count": null,
80 | "outputs": [
81 | {
82 | "output_type": "stream",
83 | "name": "stdout",
84 | "text": [
85 | "1 4 6 7\n",
86 | "[1, 4, 6, 7]\n"
87 | ]
88 | }
89 | ]
90 | },
91 | {
92 | "cell_type": "code",
93 | "metadata": {
94 | "colab": {
95 | "base_uri": "https://localhost:8080/"
96 | },
97 | "id": "a91e1Of6aK_9",
98 | "outputId": "04d1d415-ba1b-4756-9a68-c75bf59b40a7"
99 | },
100 | "source": [
101 | "print(sumList)"
102 | ],
103 | "execution_count": null,
104 | "outputs": [
105 | {
106 | "output_type": "stream",
107 | "name": "stdout",
108 | "text": [
109 | "18\n"
110 | ]
111 | }
112 | ]
113 | },
114 | {
115 | "cell_type": "code",
116 | "metadata": {
117 | "colab": {
118 | "base_uri": "https://localhost:8080/"
119 | },
120 | "id": "_svFz1g2aRJk",
121 | "outputId": "e0643aed-0ee9-48dc-e5e0-c6a20c4495d6"
122 | },
123 | "source": [
124 | "print(sum(lst2)) #Verifying using sum()"
125 | ],
126 | "execution_count": null,
127 | "outputs": [
128 | {
129 | "output_type": "stream",
130 | "text": [
131 | "31\n"
132 | ],
133 | "name": "stdout"
134 | }
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "source": [
140 | "# using reduce to compute maximum element from list\n",
141 | "from functools import reduce\n",
142 | "list1=[1,3,6,2,7]\n",
143 | "print(\"The maximum element of the list is : \", end=\"\")\n",
144 | "print(reduce(lambda a, b: a if a > b else b, list1))"
145 | ],
146 | "metadata": {
147 | "colab": {
148 | "base_uri": "https://localhost:8080/"
149 | },
150 | "id": "Ru9ICmC3Wr8P",
151 | "outputId": "ac9dd14d-e239-4e89-993e-70a72ab10db1"
152 | },
153 | "execution_count": null,
154 | "outputs": [
155 | {
156 | "output_type": "stream",
157 | "name": "stdout",
158 | "text": [
159 | "The maximum element of the list is : 7\n"
160 | ]
161 | }
162 | ]
163 | },
164 | {
165 | "cell_type": "code",
166 | "metadata": {
167 | "id": "gx8IR3KfmlBU"
168 | },
169 | "source": [
170 | "#Find minimum number in the list using reduce\n",
171 | "\n"
172 | ],
173 | "execution_count": null,
174 | "outputs": []
175 | },
176 | {
177 | "cell_type": "code",
178 | "metadata": {
179 | "id": "f9hflaN6mynD"
180 | },
181 | "source": [],
182 | "execution_count": null,
183 | "outputs": []
184 | },
185 | {
186 | "cell_type": "code",
187 | "metadata": {
188 | "id": "OIxbt_lznpaW"
189 | },
190 | "source": [
191 | "#WAP to find perimeter of the given quadrilateral when the lengths of each side is given"
192 | ],
193 | "execution_count": null,
194 | "outputs": []
195 | },
196 | {
197 | "cell_type": "markdown",
198 | "metadata": {
199 | "id": "JwvvWb08nkza"
200 | },
201 | "source": [
202 | "zip()\n",
203 | "Python’s zip() function is defined as zip(*iterables). The function takes in iterables as arguments and returns an iterator. This iterator generates a series of tuples containing elements from each iterable. zip() can accept any type of iterable, such as files, lists, tuples, dictionaries, sets, and so on"
204 | ]
205 | },
206 | {
207 | "cell_type": "code",
208 | "metadata": {
209 | "id": "wOqv3ilgm0Zg",
210 | "colab": {
211 | "base_uri": "https://localhost:8080/"
212 | },
213 | "outputId": "46d476e4-ac67-4237-ffab-3ef4b812753a"
214 | },
215 | "source": [
216 | "#Put student_name, marks and standard in a tuple\n",
217 | "\n",
218 | "name= [\"A\", \"B\", \"C\"]\n",
219 | "marks= [24, 23, 26]\n",
220 | "std= [5,6,3]\n",
221 | "\n",
222 | "student= list(zip(name, marks, std))\n",
223 | "print(student)"
224 | ],
225 | "execution_count": null,
226 | "outputs": [
227 | {
228 | "output_type": "stream",
229 | "text": [
230 | "[('A', 24, 5), ('B', 23, 6), ('C', 26, 3)]\n"
231 | ],
232 | "name": "stdout"
233 | }
234 | ]
235 | },
236 | {
237 | "cell_type": "markdown",
238 | "metadata": {
239 | "id": "KFfXlWz5NHPr"
240 | },
241 | "source": [
242 | "enumerate()\n",
243 | "The enumerate() method adds a counter to an iterable and returns it (the enumerate object).\n",
244 | "The firstindex value will start from 0. You can also specify the startindex by using the optional parameter startIndex in enumerate."
245 | ]
246 | },
247 | {
248 | "cell_type": "code",
249 | "metadata": {
250 | "colab": {
251 | "base_uri": "https://localhost:8080/"
252 | },
253 | "id": "NVKTnpW3M6Xa",
254 | "outputId": "20f4f0be-ec37-4925-96df-b86d4996309d"
255 | },
256 | "source": [
257 | "#Enumerate example\n",
258 | "languages = [\"Hindi\", \"English\", \"Sanskrit\"]\n",
259 | "\n",
260 | "enum_language= enumerate(languages)\n",
261 | "\n",
262 | "print(list(enum_language))"
263 | ],
264 | "execution_count": null,
265 | "outputs": [
266 | {
267 | "output_type": "stream",
268 | "text": [
269 | "[(0, 'Hindi'), (1, 'English'), (2, 'Sanskrit')]\n"
270 | ],
271 | "name": "stdout"
272 | }
273 | ]
274 | },
275 | {
276 | "cell_type": "code",
277 | "metadata": {
278 | "id": "q8eIyXLCNus0"
279 | },
280 | "source": [
281 | "#Use enumerate\n",
282 | "x = ('apple', 'banana', 'cherry')\n"
283 | ],
284 | "execution_count": null,
285 | "outputs": []
286 | },
287 | {
288 | "cell_type": "code",
289 | "source": [
290 | "# enumerate function in loops\n",
291 | "l1 = [\"high\", \"medium\", \"low\"]\n",
292 | "\n",
293 | "# printing the tuples in object directly\n",
294 | "for ele in enumerate(l1,100):\n",
295 | " print (ele)"
296 | ],
297 | "metadata": {
298 | "colab": {
299 | "base_uri": "https://localhost:8080/"
300 | },
301 | "id": "yDhi1P2-YO68",
302 | "outputId": "8bff2325-ebf2-4219-f4e0-a7b9edd1188e"
303 | },
304 | "execution_count": null,
305 | "outputs": [
306 | {
307 | "output_type": "stream",
308 | "name": "stdout",
309 | "text": [
310 | "(100, 'high')\n",
311 | "(101, 'medium')\n",
312 | "(102, 'low')\n"
313 | ]
314 | }
315 | ]
316 | },
317 | {
318 | "cell_type": "code",
319 | "source": [],
320 | "metadata": {
321 | "id": "Vk38Tn7bYdHw"
322 | },
323 | "execution_count": null,
324 | "outputs": []
325 | }
326 | ]
327 | }
--------------------------------------------------------------------------------
/4_1_Files.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Unit4.1_Files.ipynb",
7 | "provenance": [],
8 | "collapsed_sections": [],
9 | "authorship_tag": "ABX9TyPDQPyZQJgx1C53bXk6rrUv",
10 | "include_colab_link": true
11 | },
12 | "kernelspec": {
13 | "name": "python3",
14 | "display_name": "Python 3"
15 | },
16 | "language_info": {
17 | "name": "python"
18 | }
19 | },
20 | "cells": [
21 | {
22 | "cell_type": "markdown",
23 | "metadata": {
24 | "id": "view-in-github",
25 | "colab_type": "text"
26 | },
27 | "source": [
28 | "
"
29 | ]
30 | },
31 | {
32 | "cell_type": "markdown",
33 | "metadata": {
34 | "id": "zFGig3c616EU"
35 | },
36 | "source": [
37 | "Files\n",
38 | "\n",
39 | "To store data, we need files. Place on our computer harddisk where we store our data is called as a file.\n",
40 | "File handling is an important task and has many applications.\n",
41 | "Python has several functions for creating, reading,writing updating, and deleting files."
42 | ]
43 | },
44 | {
45 | "cell_type": "markdown",
46 | "metadata": {
47 | "id": "cioOpVJE40Jt"
48 | },
49 | "source": [
50 | "Website references\n",
51 | "\n",
52 | "https://www.w3schools.com/python/python_file_handling.asp
\n",
53 | "https://www.javatpoint.com/python-files-io\n"
54 | ]
55 | },
56 | {
57 | "cell_type": "markdown",
58 | "metadata": {
59 | "id": "yGp-f7O-2xJC"
60 | },
61 | "source": [
62 | "Opening a File using open()\n",
63 | "\n",
64 | "Python has a built-in open() function to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly.\n",
65 | "\n",
66 | "The open() function takes two parameters; filename, and mode.\n",
67 | "\n",
68 | "There are four different methods (modes) for opening a file-\n",
69 | "\n",
70 | "\"r\" - Read - Default value. Opens a file for reading, error if the file does not exist\n",
71 | "\n",
72 | "\"a\" - Append - Opens a file for appending, creates the file if it does not exist\n",
73 | "\n",
74 | "\"w\" - Write - Opens a file for writing, creates the file if it does not exist\n",
75 | "\n",
76 | "\"x\" - Create - Creates the specified file, returns an error if the file exists\n",
77 | "\n",
78 | "In addition you can specify if the file should be handled as binary or text mode\n",
79 | "\n",
80 | "\"t\" - Text - Default value. Text mode\n",
81 | "\n",
82 | "\"b\" - Binary - Binary mode (e.g. images)"
83 | ]
84 | },
85 | {
86 | "cell_type": "code",
87 | "metadata": {
88 | "id": "ErI37OGs1dCs",
89 | "colab": {
90 | "base_uri": "https://localhost:8080/"
91 | },
92 | "outputId": "96c4ec0f-bdcf-4459-9b8f-6fd3fd17b968"
93 | },
94 | "source": [
95 | "#Open a file using \"with\"\n",
96 | "with open(\"/Hello.txt\", \"r\") as file1:\n",
97 | " lines= file1.read()\n",
98 | " #print(lines)\n",
99 | "print(lines)\n"
100 | ],
101 | "execution_count": null,
102 | "outputs": [
103 | {
104 | "output_type": "stream",
105 | "text": [
106 | "Hello world\n",
107 | "Hello Python\n",
108 | "Good morning\n",
109 | "How are you\n"
110 | ],
111 | "name": "stdout"
112 | }
113 | ]
114 | },
115 | {
116 | "cell_type": "code",
117 | "metadata": {
118 | "colab": {
119 | "base_uri": "https://localhost:8080/"
120 | },
121 | "id": "ujR5F9U04T0x",
122 | "outputId": "c8445271-ffe1-4d2d-889e-b82d61ed788a"
123 | },
124 | "source": [
125 | "#Using readlines() method\n",
126 | "with open(\"/Hello.txt\", \"r\") as file2:\n",
127 | " lines= file2.readlines()\n",
128 | " print(lines)\n",
129 | "#print(lines)"
130 | ],
131 | "execution_count": null,
132 | "outputs": [
133 | {
134 | "output_type": "stream",
135 | "text": [
136 | "['Hello world\\n', 'Hello Python\\n', 'Good morning\\n', 'How are you']\n"
137 | ],
138 | "name": "stdout"
139 | }
140 | ]
141 | },
142 | {
143 | "cell_type": "code",
144 | "metadata": {
145 | "colab": {
146 | "base_uri": "https://localhost:8080/"
147 | },
148 | "id": "FTn183-F6jvz",
149 | "outputId": "c126839a-3cdf-4bc7-9a9f-906e4a2f9d7e"
150 | },
151 | "source": [
152 | "#Printing line by line\n",
153 | "with open(\"/Hello.txt\", \"r\") as file3:\n",
154 | " for line in file3.readlines():\n",
155 | " \n",
156 | " print(line)\n"
157 | ],
158 | "execution_count": null,
159 | "outputs": [
160 | {
161 | "output_type": "stream",
162 | "text": [
163 | "Hello world\n",
164 | "\n",
165 | "Hello Python\n",
166 | "\n",
167 | "Good morning\n",
168 | "\n",
169 | "How are you\n"
170 | ],
171 | "name": "stdout"
172 | }
173 | ]
174 | },
175 | {
176 | "cell_type": "code",
177 | "metadata": {
178 | "id": "9so4EhMh4LYO"
179 | },
180 | "source": [
181 | "#Reading line by line\n",
182 | "with open(\"/Hello.txt\", \"r\") as file4:\n",
183 | " for line in file4:\n",
184 | " print(line)\n",
185 | " "
186 | ],
187 | "execution_count": null,
188 | "outputs": []
189 | },
190 | {
191 | "cell_type": "code",
192 | "metadata": {
193 | "colab": {
194 | "base_uri": "https://localhost:8080/"
195 | },
196 | "id": "uqNiKm4y7r-d",
197 | "outputId": "b179a675-5b1b-41d1-af33-5711c629c1de"
198 | },
199 | "source": [
200 | "#Reading characters by one from a file\n",
201 | "with open(\"/Hello.txt\", \"r\") as file5:\n",
202 | " for char in file5.readline():\n",
203 | " print(char)"
204 | ],
205 | "execution_count": null,
206 | "outputs": [
207 | {
208 | "output_type": "stream",
209 | "text": [
210 | "H\n",
211 | "e\n",
212 | "l\n",
213 | "l\n",
214 | "o\n",
215 | " \n",
216 | "w\n",
217 | "o\n",
218 | "r\n",
219 | "l\n",
220 | "d\n",
221 | "\n",
222 | "\n"
223 | ],
224 | "name": "stdout"
225 | }
226 | ]
227 | },
228 | {
229 | "cell_type": "code",
230 | "metadata": {
231 | "id": "U4IOwsi4z_dA"
232 | },
233 | "source": [
234 | ""
235 | ],
236 | "execution_count": null,
237 | "outputs": []
238 | },
239 | {
240 | "cell_type": "code",
241 | "metadata": {
242 | "colab": {
243 | "base_uri": "https://localhost:8080/"
244 | },
245 | "id": "QEJELdNS7-Iz",
246 | "outputId": "a6c202c5-df08-43b0-c041-bffdb373d9c7"
247 | },
248 | "source": [
249 | "# Writing into a file\n",
250 | "\n",
251 | "file6= open(\"test.txt\", \"w\")\n",
252 | "file6.write(\"I am learning Python\")\n",
253 | "file6.write(\"I find it interesting\")"
254 | ],
255 | "execution_count": null,
256 | "outputs": [
257 | {
258 | "output_type": "execute_result",
259 | "data": {
260 | "text/plain": [
261 | "21"
262 | ]
263 | },
264 | "metadata": {},
265 | "execution_count": 31
266 | }
267 | ]
268 | },
269 | {
270 | "cell_type": "code",
271 | "metadata": {
272 | "colab": {
273 | "base_uri": "https://localhost:8080/"
274 | },
275 | "id": "5xKXxT7h9DMj",
276 | "outputId": "97a7119f-c428-4d58-a3d8-be3735ec42bb"
277 | },
278 | "source": [
279 | "#Write multiple statements from the user in the file\n",
280 | "with open(\"Test2.txt\",\"w\")as file7:\n",
281 | " iter=4\n",
282 | " for i in range(4):\n",
283 | " txt= input(\"Enter the details\") +\"\\n\"\n",
284 | " file7.write(txt)\n"
285 | ],
286 | "execution_count": null,
287 | "outputs": [
288 | {
289 | "output_type": "stream",
290 | "text": [
291 | "Enter the detailsAmi\n",
292 | "Enter the detailsMunshi\n",
293 | "Enter the detailsNMIMS\n",
294 | "Enter the detailsUniversity\n"
295 | ],
296 | "name": "stdout"
297 | }
298 | ]
299 | },
300 | {
301 | "cell_type": "code",
302 | "metadata": {
303 | "colab": {
304 | "base_uri": "https://localhost:8080/"
305 | },
306 | "id": "4zKcb8qu0EwE",
307 | "outputId": "35684de1-6366-4fe3-ecdd-bf5a9eab7a0b"
308 | },
309 | "source": [
310 | "#Take multiple lines from the user in the file\n",
311 | "new= open(\"new_file\",\"w\")\n",
312 | "char=\"\"\n",
313 | "\n",
314 | "while char != \"#\":\n",
315 | " char= input(\"Enter the text: \")\n",
316 | " if (char!= \"#\"):\n",
317 | " new.write(char +\"\\n\")\n"
318 | ],
319 | "execution_count": null,
320 | "outputs": [
321 | {
322 | "output_type": "stream",
323 | "text": [
324 | "Enter the text: Ami\n",
325 | "Enter the text: Munshi\n",
326 | "Enter the text: #\n"
327 | ],
328 | "name": "stdout"
329 | }
330 | ]
331 | },
332 | {
333 | "cell_type": "code",
334 | "metadata": {
335 | "id": "gsGektOB9IRI"
336 | },
337 | "source": [
338 | "# Append some text to the existing file\n",
339 | "with open(\"Test2.txt\",\"a\") as file8:\n",
340 | " file8.write(\"Mumbai\")\n"
341 | ],
342 | "execution_count": null,
343 | "outputs": []
344 | },
345 | {
346 | "cell_type": "code",
347 | "metadata": {
348 | "id": "gWu8O6j_YkV1"
349 | },
350 | "source": [
351 | "#Delete a file\n",
352 | "import os\n",
353 | "os.remove(\"Test2.txt\")"
354 | ],
355 | "execution_count": null,
356 | "outputs": []
357 | },
358 | {
359 | "cell_type": "code",
360 | "metadata": {
361 | "colab": {
362 | "base_uri": "https://localhost:8080/"
363 | },
364 | "id": "zajjOgdDZWlq",
365 | "outputId": "cde871e9-5353-4ea7-ab7d-805e0ed5ba35"
366 | },
367 | "source": [
368 | "#Check if the file exists\n",
369 | "import os\n",
370 | "if os.path.exists(\"test2.txt\"):\n",
371 | " print(\"Yes\")\n",
372 | "else:\n",
373 | " print(\"The file does not exist\")"
374 | ],
375 | "execution_count": null,
376 | "outputs": [
377 | {
378 | "output_type": "stream",
379 | "text": [
380 | "The file does not exist\n"
381 | ],
382 | "name": "stdout"
383 | }
384 | ]
385 | },
386 | {
387 | "cell_type": "code",
388 | "metadata": {
389 | "id": "H7y89TMxQRzw"
390 | },
391 | "source": [
392 | "#copy an image into another file\n",
393 | "#Open files in binary mode\n",
394 | "I1= open(\"Test.tif\",\"rb\")\n",
395 | "I2= open(\"new.tif\",\"wb\")\n",
396 | "\n",
397 | "#Read bytes from I1 and write to I2\n",
398 | "pixels= I1.read()\n",
399 | "I2.write(pixels)\n",
400 | "\n",
401 | "I1.close()\n",
402 | "I2.close()\n",
403 | "\n"
404 | ],
405 | "execution_count": null,
406 | "outputs": []
407 | },
408 | {
409 | "cell_type": "markdown",
410 | "metadata": {
411 | "id": "aGi6a4ivJH6i"
412 | },
413 | "source": [
414 | "seek() and tell()
\n",
415 | "The functions that we have learnt till now are used to\n",
416 | "access the data sequentially from a file. But if we want\n",
417 | "to access data in a random fashion, then Python gives\n",
418 | "us seek() and tell() functions to do so\n",
419 | "\n",
420 | "The tell() method\n",
421 | "This function returns an integer that specifies the\n",
422 | "current position of the file object in the file. The position\n",
423 | "so specified is the byte position from the beginning of\n",
424 | "the file till the current position of the file object. The\n",
425 | "syntax of using tell() is:\n",
426 | "file_object.tell()\n",
427 | "\n",
428 | "The seek() method\n",
429 | "This method is used to position the file object at a\n",
430 | "particular position in a file. The syntax of seek() is:\n",
431 | "file_object.seek(offset, fromwhere)\n",
432 | "In the above syntax, offset is the number of bytes by\n",
433 | "which the file object is to be moved. fromwhere\n",
434 | "indicates the starting position of the file object. That is,\n",
435 | "with reference to which position, the offset has to be\n",
436 | "counted. It can have any of the following values:\n",
437 | "0 - beginning of the file\n",
438 | "1 - current position of the file\n",
439 | "2 - end of file\n",
440 | "By default, the value of reference_point is 0, i.e.\n",
441 | "the offset is counted from the beginning of the file. For\n",
442 | "example, the statement fileObject.seek(5,0) will\n",
443 | "position the file object at 5th byte position from the\n",
444 | "beginning of the file.
\n",
445 | "\n",
446 | "\n",
447 | "To know the position of the pointer, we can use tell() method\n",
448 | "\n",
449 | "To move the the pointer to a paticular location, we can use seek() method"
450 | ]
451 | },
452 | {
453 | "cell_type": "code",
454 | "metadata": {
455 | "colab": {
456 | "base_uri": "https://localhost:8080/"
457 | },
458 | "id": "0XpGHMlaKyb1",
459 | "outputId": "9a4cd637-a39d-4ee7-e3e4-c56a340950d6"
460 | },
461 | "source": [
462 | "f= open(\"/content/Hello.txt\", \"r+b\")\n",
463 | "f.seek(14,0) # here offset is 10, and from where is 0. So from the beginning the file pointer will move to the 11th byte(10+1) from the beginning. \n"
464 | ],
465 | "execution_count": null,
466 | "outputs": [
467 | {
468 | "output_type": "execute_result",
469 | "data": {
470 | "text/plain": [
471 | "14"
472 | ]
473 | },
474 | "metadata": {},
475 | "execution_count": 10
476 | }
477 | ]
478 | },
479 | {
480 | "cell_type": "code",
481 | "metadata": {
482 | "colab": {
483 | "base_uri": "https://localhost:8080/"
484 | },
485 | "id": "BsjzdUKRL4Q1",
486 | "outputId": "3ad41fc5-cea9-4c13-efb1-3f3193326151"
487 | },
488 | "source": [
489 | "#print(f.readline())"
490 | ],
491 | "execution_count": null,
492 | "outputs": [
493 | {
494 | "output_type": "stream",
495 | "name": "stdout",
496 | "text": [
497 | "b'ello Python\\r\\n'\n"
498 | ]
499 | }
500 | ]
501 | },
502 | {
503 | "cell_type": "code",
504 | "metadata": {
505 | "id": "5JP5JT6yQyrU",
506 | "colab": {
507 | "base_uri": "https://localhost:8080/"
508 | },
509 | "outputId": "88c745d5-e17a-4490-b0fa-324b795845a2"
510 | },
511 | "source": [
512 | "print(f.tell())"
513 | ],
514 | "execution_count": null,
515 | "outputs": [
516 | {
517 | "output_type": "stream",
518 | "name": "stdout",
519 | "text": [
520 | "14\n"
521 | ]
522 | }
523 | ]
524 | },
525 | {
526 | "cell_type": "markdown",
527 | "metadata": {
528 | "id": "mtrNeI0-IwUx"
529 | },
530 | "source": [
531 | "Pickle in Python"
532 | ]
533 | },
534 | {
535 | "cell_type": "markdown",
536 | "metadata": {
537 | "id": "eL8w9YV7NXZz"
538 | },
539 | "source": [
540 | "Pickling is the serializing and de-serializing of python objects to a byte stream.Pickling is used to store python objects. This means things like lists, dictionaries, class objects, and more\n",
541 | "\n",
542 | "Pickling is a process of converting a class object into byte stream so that it can be stored into a file"
543 | ]
544 | },
545 | {
546 | "cell_type": "code",
547 | "metadata": {
548 | "id": "Sb4gyUJdOBJ5"
549 | },
550 | "source": [
551 | "#Example 1\n",
552 | "#create a pickle file (serialization)\n",
553 | "import pickle\n",
554 | "\n",
555 | "employee = {1:\"Ram\",2:\"Raj\",3:\"Rahul\"} #Pickle the dictionary data type\n",
556 | "\n",
557 | "employee_pickle = open(\"employee_details\",\"wb\") #create a new file employee_details. file object name is employee_pickle\n",
558 | "pickle.dump(employee, employee_pickle) # pickle.dump(object, file)\n",
559 | "employee_pickle.close() #Close the file object"
560 | ],
561 | "execution_count": null,
562 | "outputs": []
563 | },
564 | {
565 | "cell_type": "code",
566 | "metadata": {
567 | "colab": {
568 | "base_uri": "https://localhost:8080/"
569 | },
570 | "id": "IogVEIczOmgX",
571 | "outputId": "23bdeabe-21ec-4480-d8b1-52d566783fc5"
572 | },
573 | "source": [
574 | "#Access the pickled file\n",
575 | "\n",
576 | "pickle_out = open(\"employee_details\",\"rb\")\n",
577 | "employee_out = pickle.load(pickle_out)\n",
578 | "print(employee_out)"
579 | ],
580 | "execution_count": null,
581 | "outputs": [
582 | {
583 | "output_type": "stream",
584 | "name": "stdout",
585 | "text": [
586 | "{1: 'Ram', 2: 'Raj', 3: 'Rahul'}\n"
587 | ]
588 | }
589 | ]
590 | },
591 | {
592 | "cell_type": "code",
593 | "metadata": {
594 | "id": "h3O1iu5KSMHf"
595 | },
596 | "source": [
597 | "#Example 2\n",
598 | "\n",
599 | "import pickle\n",
600 | "lst1 = ['mango', 'grapes', 'orange', 'banana']\n",
601 | "with open('lstfile.txt', 'wb') as f:\n",
602 | " pickle.dump(lst1, f)"
603 | ],
604 | "execution_count": null,
605 | "outputs": []
606 | },
607 | {
608 | "cell_type": "code",
609 | "metadata": {
610 | "id": "sYGgtyfoQpk0"
611 | },
612 | "source": [
613 | ""
614 | ],
615 | "execution_count": null,
616 | "outputs": []
617 | }
618 | ]
619 | }
620 |
--------------------------------------------------------------------------------
/4_2_Seek_Tell_Pickle.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Unit4.2_Seek_Tell_Pickle.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyMhyDA0wjkwpJgdOZOCMdJr",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "metadata": {
33 | "id": "rCZD_qOIRiLV"
34 | },
35 | "source": [
36 | ""
37 | ],
38 | "execution_count": null,
39 | "outputs": []
40 | },
41 | {
42 | "cell_type": "markdown",
43 | "metadata": {
44 | "id": "yU12w1RCRsMt"
45 | },
46 | "source": [
47 | "seek() and tell()\n",
48 | "The functions that we have learnt till now are used to access the data sequentially from a file. But if we want to access data in a random fashion, then Python gives us seek() and tell() functions to do so\n",
49 | "\n",
50 | "The tell() method This function returns an integer that specifies the current position of the file object in the file. The position so specified is the byte position from the beginning of the file till the current position of the file object. The syntax of using tell() is: file_object.tell()\n",
51 | "\n",
52 | "The seek() method This method is used to position the file object at a particular position in a file. The syntax of seek() is: file_object.seek(offset, fromwhere) In the above syntax, offset is the number of bytes by which the file object is to be moved. fromwhere indicates the starting position of the file object. That is, with reference to which position, the offset has to be counted. It can have any of the following values: 0 - beginning of the file 1 - current position of the file 2 - end of file By default, the value of reference_point is 0, i.e. the offset is counted from the beginning of the file. For example, the statement fileObject.seek(5,0) will position the file object at 5th byte position from the beginning of the file.\n",
53 | "\n",
54 | "To know the position of the pointer, we can use tell() method\n",
55 | "\n",
56 | "To move the the pointer to a paticular location, we can use seek() method"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "metadata": {
62 | "id": "FpY0NZ0bRwkw",
63 | "colab": {
64 | "base_uri": "https://localhost:8080/"
65 | },
66 | "outputId": "440670af-d8e8-4670-d1e3-d72419435a26"
67 | },
68 | "source": [
69 | "f= open(\"/content/Hello.txt\", \"r+b\")\n",
70 | "f.seek(10,0) # here offset is 14, and from where is 0. So from the beginning the file pointer will move to the 15th byte(14+1) from the beginning. \n"
71 | ],
72 | "execution_count": null,
73 | "outputs": [
74 | {
75 | "output_type": "execute_result",
76 | "data": {
77 | "text/plain": [
78 | "10"
79 | ]
80 | },
81 | "metadata": {},
82 | "execution_count": 17
83 | }
84 | ]
85 | },
86 | {
87 | "cell_type": "code",
88 | "metadata": {
89 | "id": "NbA8s_cRR0l3",
90 | "colab": {
91 | "base_uri": "https://localhost:8080/"
92 | },
93 | "outputId": "09212bab-54ce-420b-9fb7-b4546d17f302"
94 | },
95 | "source": [
96 | "print(f.tell())"
97 | ],
98 | "execution_count": null,
99 | "outputs": [
100 | {
101 | "output_type": "stream",
102 | "name": "stdout",
103 | "text": [
104 | "10\n"
105 | ]
106 | }
107 | ]
108 | },
109 | {
110 | "cell_type": "code",
111 | "metadata": {
112 | "colab": {
113 | "base_uri": "https://localhost:8080/"
114 | },
115 | "id": "HaNM_nIgeJrL",
116 | "outputId": "dc43db4a-a572-4d77-daf6-6507c7e27f28"
117 | },
118 | "source": [
119 | "print(f.readline())"
120 | ],
121 | "execution_count": null,
122 | "outputs": [
123 | {
124 | "output_type": "stream",
125 | "name": "stdout",
126 | "text": [
127 | "b'd\\r\\n'\n"
128 | ]
129 | }
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "metadata": {
135 | "colab": {
136 | "base_uri": "https://localhost:8080/"
137 | },
138 | "id": "M-pl2V3BeYYZ",
139 | "outputId": "37cce5e0-cd38-4370-9d3b-af358a62eb68"
140 | },
141 | "source": [
142 | "f.seek(20)"
143 | ],
144 | "execution_count": null,
145 | "outputs": [
146 | {
147 | "output_type": "execute_result",
148 | "data": {
149 | "text/plain": [
150 | "20"
151 | ]
152 | },
153 | "metadata": {},
154 | "execution_count": 20
155 | }
156 | ]
157 | },
158 | {
159 | "cell_type": "code",
160 | "metadata": {
161 | "colab": {
162 | "base_uri": "https://localhost:8080/"
163 | },
164 | "id": "efs25KzgedpK",
165 | "outputId": "bf484d3f-69d7-4fa2-9269-5c67fc59e69e"
166 | },
167 | "source": [
168 | "print(f.readline())"
169 | ],
170 | "execution_count": null,
171 | "outputs": [
172 | {
173 | "output_type": "stream",
174 | "name": "stdout",
175 | "text": [
176 | "b'ython\\r\\n'\n"
177 | ]
178 | }
179 | ]
180 | },
181 | {
182 | "cell_type": "markdown",
183 | "metadata": {
184 | "id": "m0zpf-ASR3aY"
185 | },
186 | "source": [
187 | "Pickle in Python
\n",
188 | "\n",
189 | "Pickling is the serializing and de-serializing of python objects to a byte stream.Pickling is used to store python objects. This means things like lists, dictionaries, class objects, and more\n",
190 | "\n",
191 | "Pickling is a process of converting a class object into byte stream so that it can be stored into a file"
192 | ]
193 | },
194 | {
195 | "cell_type": "code",
196 | "metadata": {
197 | "id": "AB8wbYN1R9kQ"
198 | },
199 | "source": [
200 | "#Example 1\n",
201 | "#create a pickle dictionary (serialization)\n",
202 | "import pickle\n",
203 | "employee= {1: \"Ram\", 2:\"Raj\", 3:\"Rahul\"} #Dictionary to be pickled\n",
204 | "\n",
205 | "with open(\"employee_details.txt\", \"wb\") as f3:\n",
206 | " pickle.dump(employee, f3)\n",
207 | "\n"
208 | ],
209 | "execution_count": null,
210 | "outputs": []
211 | },
212 | {
213 | "cell_type": "code",
214 | "metadata": {
215 | "id": "2gbc3fzKR_6M",
216 | "colab": {
217 | "base_uri": "https://localhost:8080/"
218 | },
219 | "outputId": "c203d45f-b942-4f4a-8238-f925baa2100b"
220 | },
221 | "source": [
222 | "#Access the pickled file\n",
223 | "import pickle\n",
224 | "with open(\"employee_details.txt\", \"rb\") as f4:\n",
225 | " d= pickle.load(f4)\n",
226 | "print(d)\n",
227 | "\n"
228 | ],
229 | "execution_count": null,
230 | "outputs": [
231 | {
232 | "output_type": "stream",
233 | "name": "stdout",
234 | "text": [
235 | "{1: 'Ram', 2: 'Raj', 3: 'Rahul'}\n"
236 | ]
237 | }
238 | ]
239 | },
240 | {
241 | "cell_type": "code",
242 | "metadata": {
243 | "id": "RUzUdJwiSRbp"
244 | },
245 | "source": [
246 | "#Example 2 Pickle a list\n",
247 | "\n",
248 | "import pickle\n",
249 | "\n",
250 | "lst1= [\"mango\", \"apple\", \"banana\", \"grapes\"] #create a list\n",
251 | "\n",
252 | "#Open a file for pickling the list\n",
253 | "with open(\"lstfile.txt\",\"wb\") as f1:\n",
254 | " pickle.dump(lst1,f1)\n",
255 | "\n"
256 | ],
257 | "execution_count": null,
258 | "outputs": []
259 | },
260 | {
261 | "cell_type": "code",
262 | "metadata": {
263 | "colab": {
264 | "base_uri": "https://localhost:8080/"
265 | },
266 | "id": "l9vc9AH4TIIc",
267 | "outputId": "f7605341-b463-4fd6-dc9c-e3bb700d6542"
268 | },
269 | "source": [
270 | "#Unpickling the list on above example\n",
271 | "\n",
272 | "import pickle\n",
273 | "\n",
274 | "with open(\"lstfile.txt\", \"rb\") as f:\n",
275 | " lst2= pickle.load(f)\n",
276 | "\n",
277 | "print(lst2)"
278 | ],
279 | "execution_count": null,
280 | "outputs": [
281 | {
282 | "output_type": "stream",
283 | "name": "stdout",
284 | "text": [
285 | "['mango', 'apple', 'banana', 'grapes']\n"
286 | ]
287 | }
288 | ]
289 | },
290 | {
291 | "cell_type": "code",
292 | "metadata": {
293 | "id": "dCQJcGmZUUtP"
294 | },
295 | "source": [
296 | "dict1= {\"Shoes\":[100, 5]}"
297 | ],
298 | "execution_count": null,
299 | "outputs": []
300 | }
301 | ]
302 | }
303 |
--------------------------------------------------------------------------------
/4_3_OSModule.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Unit4.3_OSModule.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyMqaBmUb+WP5xZl5BuMWu3d",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "bZZMVD2xVsfH"
34 | },
35 | "source": [
36 | "Operating System (OS) Module
\n",
37 | "It represents OS dependent functionality.
\n",
38 | "This module is useful to perform simple operations on the directories.\n",
39 | "\n",
40 | "For example, if we want to know the current working directory, we can use getcwd()method of OS module\n",
41 | "\n",
42 | "For creating or making our own directory, we can use mkdir() method"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "metadata": {
48 | "id": "06xQdU9rVUEV",
49 | "colab": {
50 | "base_uri": "https://localhost:8080/"
51 | },
52 | "outputId": "205e767a-f076-424e-e20b-b3f074066ae0"
53 | },
54 | "source": [
55 | "#get current working directory\n",
56 | "\n",
57 | "import os\n",
58 | "current= os.getcwd()\n",
59 | "print(\"Current directory is \", current)\n",
60 | "\n"
61 | ],
62 | "execution_count": null,
63 | "outputs": [
64 | {
65 | "output_type": "stream",
66 | "name": "stdout",
67 | "text": [
68 | "Current directory is /content\n"
69 | ]
70 | }
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "metadata": {
76 | "id": "p7KEPq7_WgVS"
77 | },
78 | "source": [
79 | "#create our own directory\n",
80 | "\n",
81 | "import os\n",
82 | "#create a directory with name mydir\n",
83 | "os.mkdir(\"mydir\")\n"
84 | ],
85 | "execution_count": null,
86 | "outputs": []
87 | },
88 | {
89 | "cell_type": "code",
90 | "metadata": {
91 | "id": "7EVZJ4wXXvx5"
92 | },
93 | "source": [
94 | "#Creates a sub directory called mysub\n",
95 | "import os\n",
96 | "os.mkdir(\"mydir/mysub\")\n"
97 | ],
98 | "execution_count": null,
99 | "outputs": []
100 | },
101 | {
102 | "cell_type": "code",
103 | "metadata": {
104 | "id": "KnRCRAP-YK8a"
105 | },
106 | "source": [
107 | "#Create directory and sub directory both together using makedirs\n",
108 | "import os\n",
109 | "os.makedirs(\"sub1/sub2\")\n"
110 | ],
111 | "execution_count": null,
112 | "outputs": []
113 | },
114 | {
115 | "cell_type": "code",
116 | "metadata": {
117 | "colab": {
118 | "base_uri": "https://localhost:8080/",
119 | "height": 35
120 | },
121 | "id": "yvZTAByHbQgd",
122 | "outputId": "43502223-bb74-46aa-c258-866cb99c0b38"
123 | },
124 | "source": [
125 | "#Change current working directory\n",
126 | "\n",
127 | "#First ascertain the working directory\n",
128 | "import os\n",
129 | "os.getcwd()\n"
130 | ],
131 | "execution_count": null,
132 | "outputs": [
133 | {
134 | "output_type": "execute_result",
135 | "data": {
136 | "application/vnd.google.colaboratory.intrinsic+json": {
137 | "type": "string"
138 | },
139 | "text/plain": [
140 | "'/content'"
141 | ]
142 | },
143 | "metadata": {},
144 | "execution_count": 5
145 | }
146 | ]
147 | },
148 | {
149 | "cell_type": "code",
150 | "metadata": {
151 | "id": "wzKLVELqbapV"
152 | },
153 | "source": [
154 | "#Change the working directory\n",
155 | "\n",
156 | "import os\n",
157 | "os.chdir(\"sub1\")\n",
158 | "\n"
159 | ],
160 | "execution_count": null,
161 | "outputs": []
162 | },
163 | {
164 | "cell_type": "code",
165 | "metadata": {
166 | "colab": {
167 | "base_uri": "https://localhost:8080/",
168 | "height": 35
169 | },
170 | "id": "yD6g4cK_bbsL",
171 | "outputId": "43b0037e-e031-48a4-98c3-4a791c61dd68"
172 | },
173 | "source": [
174 | "os.getcwd()"
175 | ],
176 | "execution_count": null,
177 | "outputs": [
178 | {
179 | "output_type": "execute_result",
180 | "data": {
181 | "application/vnd.google.colaboratory.intrinsic+json": {
182 | "type": "string"
183 | },
184 | "text/plain": [
185 | "'/content/sub1'"
186 | ]
187 | },
188 | "metadata": {},
189 | "execution_count": 7
190 | }
191 | ]
192 | },
193 | {
194 | "cell_type": "code",
195 | "metadata": {
196 | "id": "lmcev_mebqX9"
197 | },
198 | "source": [
199 | "os.chdir(\"/content/mydir/mysub\")"
200 | ],
201 | "execution_count": null,
202 | "outputs": []
203 | },
204 | {
205 | "cell_type": "code",
206 | "metadata": {
207 | "colab": {
208 | "base_uri": "https://localhost:8080/",
209 | "height": 35
210 | },
211 | "id": "AtuqcA_3b7Id",
212 | "outputId": "2ae85246-19f9-4d55-daaa-aa92b2590d6a"
213 | },
214 | "source": [
215 | "#Check if the working directory has changed\n",
216 | "os.getcwd()"
217 | ],
218 | "execution_count": null,
219 | "outputs": [
220 | {
221 | "output_type": "execute_result",
222 | "data": {
223 | "application/vnd.google.colaboratory.intrinsic+json": {
224 | "type": "string"
225 | },
226 | "text/plain": [
227 | "'/content/mydir/mysub'"
228 | ]
229 | },
230 | "metadata": {},
231 | "execution_count": 19
232 | }
233 | ]
234 | },
235 | {
236 | "cell_type": "code",
237 | "metadata": {
238 | "id": "7XCKAaiwcGh2"
239 | },
240 | "source": [
241 | "os.chdir(\"../\") #To come out of the current directory and go one level up"
242 | ],
243 | "execution_count": null,
244 | "outputs": []
245 | },
246 | {
247 | "cell_type": "code",
248 | "metadata": {
249 | "colab": {
250 | "base_uri": "https://localhost:8080/",
251 | "height": 35
252 | },
253 | "id": "dP1SogO6dCOX",
254 | "outputId": "657e39b0-8d38-4a6f-b40c-bf15fa401c6c"
255 | },
256 | "source": [
257 | "os.getcwd()"
258 | ],
259 | "execution_count": null,
260 | "outputs": [
261 | {
262 | "output_type": "execute_result",
263 | "data": {
264 | "application/vnd.google.colaboratory.intrinsic+json": {
265 | "type": "string"
266 | },
267 | "text/plain": [
268 | "'/content/mydir'"
269 | ]
270 | },
271 | "metadata": {},
272 | "execution_count": 21
273 | }
274 | ]
275 | },
276 | {
277 | "cell_type": "code",
278 | "metadata": {
279 | "id": "hYGpnVdRdHIG"
280 | },
281 | "source": [
282 | "os.chdir(\"../\")"
283 | ],
284 | "execution_count": null,
285 | "outputs": []
286 | },
287 | {
288 | "cell_type": "code",
289 | "metadata": {
290 | "colab": {
291 | "base_uri": "https://localhost:8080/",
292 | "height": 35
293 | },
294 | "id": "urTUwCnqdNHI",
295 | "outputId": "5709618f-f2f0-46b7-da45-30ea6e190f03"
296 | },
297 | "source": [
298 | "os.getcwd()"
299 | ],
300 | "execution_count": null,
301 | "outputs": [
302 | {
303 | "output_type": "execute_result",
304 | "data": {
305 | "application/vnd.google.colaboratory.intrinsic+json": {
306 | "type": "string"
307 | },
308 | "text/plain": [
309 | "'/content'"
310 | ]
311 | },
312 | "metadata": {},
313 | "execution_count": 23
314 | }
315 | ]
316 | },
317 | {
318 | "cell_type": "code",
319 | "metadata": {
320 | "id": "NzA5GUHedOdR"
321 | },
322 | "source": [
323 | "#Remove a directory using rmdir() method\n",
324 | "import os\n",
325 | "os.rmdir(\"sub1\")"
326 | ],
327 | "execution_count": null,
328 | "outputs": []
329 | },
330 | {
331 | "cell_type": "code",
332 | "metadata": {
333 | "id": "iGWdSnfKd5-O"
334 | },
335 | "source": [
336 | "import os\n",
337 | "os.makedirs(\"sub3/sub4\")"
338 | ],
339 | "execution_count": null,
340 | "outputs": []
341 | },
342 | {
343 | "cell_type": "code",
344 | "metadata": {
345 | "colab": {
346 | "base_uri": "https://localhost:8080/",
347 | "height": 35
348 | },
349 | "id": "LsMf4_SVeJDg",
350 | "outputId": "7168dac5-fd36-4219-dcbf-a31578900942"
351 | },
352 | "source": [
353 | "os.getcwd()"
354 | ],
355 | "execution_count": null,
356 | "outputs": [
357 | {
358 | "output_type": "execute_result",
359 | "data": {
360 | "application/vnd.google.colaboratory.intrinsic+json": {
361 | "type": "string"
362 | },
363 | "text/plain": [
364 | "'/content'"
365 | ]
366 | },
367 | "metadata": {},
368 | "execution_count": 28
369 | }
370 | ]
371 | },
372 | {
373 | "cell_type": "code",
374 | "metadata": {
375 | "id": "bqX5eni9eO0W"
376 | },
377 | "source": [
378 | "os.rmdir(\"/content/sub3/sub4\") #Delete the sub directory sub4"
379 | ],
380 | "execution_count": null,
381 | "outputs": []
382 | },
383 | {
384 | "cell_type": "code",
385 | "metadata": {
386 | "id": "a0ZgUQC8ee69"
387 | },
388 | "source": [
389 | "os.rmdir(\"sub3\")"
390 | ],
391 | "execution_count": null,
392 | "outputs": []
393 | },
394 | {
395 | "cell_type": "code",
396 | "metadata": {
397 | "id": "zJXiLdZ7eJLx"
398 | },
399 | "source": [
400 | "#DIY use removedirs() to recursively remove all the directories\n"
401 | ],
402 | "execution_count": null,
403 | "outputs": []
404 | },
405 | {
406 | "cell_type": "code",
407 | "metadata": {
408 | "id": "a9lqNR9-eb5Z"
409 | },
410 | "source": [
411 | "#Rename a directory\n",
412 | "os.rename(\"mydir\",\"newdir\")\n",
413 | "#First change the directory back to root directory\n"
414 | ],
415 | "execution_count": null,
416 | "outputs": []
417 | },
418 | {
419 | "cell_type": "code",
420 | "metadata": {
421 | "id": "MlX3YMBfgN5s"
422 | },
423 | "source": [
424 | "#Display all the content of the directory using walk()\n",
425 | "\n",
426 | "import os\n",
427 | "for dirpath, dirnames, filenames in os.walk(\".\"):\n",
428 | " print(\"current path: \", dirpath)\n",
429 | " print(\"directories\", dirnames)\n",
430 | " print(\"Files:\", filenames)\n",
431 | " print()\n",
432 | "\n"
433 | ],
434 | "execution_count": null,
435 | "outputs": []
436 | },
437 | {
438 | "cell_type": "code",
439 | "metadata": {
440 | "id": "wpOW86w7UxBA"
441 | },
442 | "source": [
443 | "#Check if the file exists or not\n",
444 | "#Uploaded Hello.txt file\n",
445 | "\n",
446 | "import os, sys\n",
447 | "\n",
448 | "#enter the file name for reading\n",
449 | "\n",
450 | "fname= input(\"Enter the file name: \")\n",
451 | "\n",
452 | "if os.path.isfile(fname):\n",
453 | " f=open(fname,\"r\")\n",
454 | " print(\"File contents are\")\n",
455 | " print(f.read())\n",
456 | "else:\n",
457 | " print(fname, \" does not exist\")\n",
458 | " sys.exit()\n",
459 | "\n"
460 | ],
461 | "execution_count": null,
462 | "outputs": []
463 | },
464 | {
465 | "cell_type": "code",
466 | "metadata": {
467 | "id": "8gBTfsnVV1ST",
468 | "colab": {
469 | "base_uri": "https://localhost:8080/",
470 | "height": 270
471 | },
472 | "outputId": "f021e62e-033a-49f7-a102-d7abe315c14f"
473 | },
474 | "source": [
475 | "import os, sys\n",
476 | "\n",
477 | "#enter the filename for reading\n",
478 | "\n",
479 | "fname= input(\"Enter the file that your want to open: \")\n",
480 | "\n",
481 | "if os.path.isfile(fname):\n",
482 | " f= open(fname, \"r\")\n",
483 | " print(\"The file contents are as follows-\")\n",
484 | " print(f.read())\n",
485 | "else:\n",
486 | " print(fname, \" does not exist\")\n",
487 | "sys.exit()"
488 | ],
489 | "execution_count": null,
490 | "outputs": [
491 | {
492 | "output_type": "stream",
493 | "name": "stdout",
494 | "text": [
495 | "Enter the file that your want to open: Hello.txt\n",
496 | "The file contents are as follows-\n",
497 | "Hello world\n",
498 | "Hello Python\n",
499 | "Good morning\n",
500 | "How are you\n"
501 | ]
502 | },
503 | {
504 | "output_type": "error",
505 | "ename": "SystemExit",
506 | "evalue": "ignored",
507 | "traceback": [
508 | "An exception has occurred, use %tb to see the full traceback.\n",
509 | "\u001b[0;31mSystemExit\u001b[0m\n"
510 | ]
511 | },
512 | {
513 | "output_type": "stream",
514 | "name": "stderr",
515 | "text": [
516 | "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py:2890: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n",
517 | " warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n"
518 | ]
519 | }
520 | ]
521 | },
522 | {
523 | "cell_type": "code",
524 | "metadata": {
525 | "id": "XQO2z7RogUFi"
526 | },
527 | "source": [
528 | ""
529 | ],
530 | "execution_count": null,
531 | "outputs": []
532 | }
533 | ]
534 | }
535 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Practical Python Programming (A collation for students/enthusiasts/ameteurs)
2 | Hands on programming to learn the basics of Python programming
3 | Some coding tasks for self-practice
4 | Covers the most fundamental and practical areas which each python programmer should be hands on with - data structures (list, strings, tuples, dictionaries etc), conditional blocks, loops, functions, files, classes and objects, numpy, pandas and matplotlib
5 |
6 | # Disclaimer - It's a collation
7 | This repo is a collation of work to assist enthusiasts and ameteur Python programmers come up the curve.
8 |
9 | The value add of this repo is to provide a central repo and a single place, based on the repo owner's own experience of learning. This repo is a mix of original work and re-use and collation of existing publicly accessible artefacts.
10 |
11 | Where possible, references are also provided within the notebooks/code
12 |
13 | # Help improve this
14 | IF you have something that can help enrich this repo further, please do fork and submit for consideration/addition to the repo by all means.
15 |
--------------------------------------------------------------------------------
/Task_Diff495.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "PythonLabTest_Batch2_Diff495.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyOg+DT7fupx+A5dtHt76anx",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "0t4ufpxoeB5e"
34 | },
35 | "source": [
36 | "Lab Test Question\n",
37 | "\n",
38 | "1.\tTake a list of three digit numbers from the user\n",
39 | "2.\tTake the first three-digit number from the above list\n",
40 | "3.\tFrom the same number obtain a smallest three-digit number and largest three-digit number.\n",
41 | "4.\tSubtract the smaller number from the larger number.\n",
42 | "5.\tIf the difference is equal to 495, you stop and take the next number.\n",
43 | "6.\tIf the difference is not 495, apply step 3 and 4 till the difference obtained is 495\n",
44 | "7.\tStore the original number from the list and number of iterations needed to obtain 495 in a dictionary.\n",
45 | "8.\tPrint the dictionary\n",
46 | "9.\tUse the same function for 4 digits number to obtain the constant of 6147\n",
47 | "\n",
48 | "Example\n",
49 | "\n",
50 | "Number=647\n",
51 | "\n",
52 | "Iteration 1\n",
53 | "\n",
54 | "Smallest= 467\n",
55 | "Largest= 764\n",
56 | "Difference= 764-467= 297\n",
57 | "\n",
58 | "297!= 495\n",
59 | "\n",
60 | "Iteration 2\n",
61 | "\n",
62 | "Smallest= 279\n",
63 | "Largest= 972\n",
64 | "Difference= 693\n",
65 | "\n",
66 | "693!=495\n",
67 | "\n",
68 | "Iteration 3\n",
69 | "\n",
70 | "963-369=594\n",
71 | "\n",
72 | "594 != 495\n",
73 | "\n",
74 | "Iteration4\n",
75 | "\n",
76 | "954-459= 495 \n",
77 | "\n",
78 | "Stop\n"
79 | ]
80 | },
81 | {
82 | "cell_type": "code",
83 | "metadata": {
84 | "id": "sDHGTiQmSM3I"
85 | },
86 | "source": [
87 | "#function to find smallest and largest number from the given three digit numbers in python\n",
88 | "\n",
89 | "def small_large(num):\n",
90 | "\n",
91 | " #Convert the given number to str, then list, then individual elements back to int, then sort them in ascending and descending\n",
92 | " num= list(str(num)) \n",
93 | " new=[int(i) for i in num] \n",
94 | " small= sorted(new) \n",
95 | " large=sorted(new, reverse=True) \n",
96 | "\n",
97 | " #Convert the sorted list elements to string, join them and then convert the number to int\n",
98 | " small=[str(i) for i in small]\n",
99 | " small=int(\"\".join(small))\n",
100 | " large=[str(i) for i in large]\n",
101 | " large=int(\"\".join(large))\n",
102 | "\n",
103 | " #Return the smallest and largest number obtained from the given number\n",
104 | " \n",
105 | " return small,large\n",
106 | "\n",
107 | " "
108 | ],
109 | "execution_count": null,
110 | "outputs": []
111 | },
112 | {
113 | "cell_type": "code",
114 | "metadata": {
115 | "id": "d3EgeG7Wdpmq"
116 | },
117 | "source": [
118 | "#Method 1 to write the difference function\n",
119 | "\n",
120 | "#Difference function which will take the num\n",
121 | "#Will check if the difference between the small and large number formed from that number is 495\n",
122 | "#Will call this function till the difference of 495 is obtained.\n",
123 | "#Will return the number of iterations required to obtain the difference of 495\n",
124 | "\n",
125 | "\n",
126 | "def difference(num, count=1):\n",
127 | " small,large= small_large(num)\n",
128 | " diff= large-small\n",
129 | " if diff==495:\n",
130 | " count1=count\n",
131 | " #print(diff, count1)\n",
132 | " return count\n",
133 | " \n",
134 | " else:\n",
135 | " count1=count+1\n",
136 | " #print(diff, count1)\n",
137 | " ctr= difference(diff,count1)\n",
138 | " return ctr "
139 | ],
140 | "execution_count": null,
141 | "outputs": []
142 | },
143 | {
144 | "cell_type": "code",
145 | "metadata": {
146 | "id": "wgD_VvZGj84c"
147 | },
148 | "source": [
149 | "#Method 2 to write the difference function\n",
150 | "\n",
151 | "#Difference function which will take the num\n",
152 | "#Will check if the difference between the small and large number formed from that number is 495\n",
153 | "#Will call this function till the difference of 495 is obtained.\n",
154 | "#Will retrun the number of iterations required to obtain the difference of 495\n",
155 | "\n",
156 | "\n",
157 | "def difference(num,count=1):\n",
158 | " small,large= small_large(num)\n",
159 | " ctr=count\n",
160 | " diff= large-small\n",
161 | " if diff!=495:\n",
162 | " ctr=ctr+1\n",
163 | " #print(\"diff= \", diff,\"ctr=\", ctr)\n",
164 | " ctr=difference(diff,ctr)\n",
165 | " return ctr"
166 | ],
167 | "execution_count": null,
168 | "outputs": []
169 | },
170 | {
171 | "cell_type": "code",
172 | "metadata": {
173 | "colab": {
174 | "base_uri": "https://localhost:8080/"
175 | },
176 | "id": "j4sNySjNit6K",
177 | "outputId": "09c553ef-9286-4089-a1f8-afcf1112fa19"
178 | },
179 | "source": [
180 | "#Example to check the small_large and difference functions\n",
181 | "num=647\n",
182 | "print(\"Number of iterations needed to obtain the difference of 495 for {} is \".format(num), difference(num)) "
183 | ],
184 | "execution_count": 15,
185 | "outputs": [
186 | {
187 | "output_type": "stream",
188 | "name": "stdout",
189 | "text": [
190 | "Number of iterations needed to obtain the difference of 495 for 647 is 4\n"
191 | ]
192 | }
193 | ]
194 | },
195 | {
196 | "cell_type": "code",
197 | "metadata": {
198 | "colab": {
199 | "base_uri": "https://localhost:8080/"
200 | },
201 | "id": "wDk6JvL6YLRa",
202 | "outputId": "b9413d8e-9126-4020-9db7-d0313d11823a"
203 | },
204 | "source": [
205 | "#Example to find the number of iterations needed to get a difference of 495\n",
206 | "\n",
207 | "\n",
208 | "list1=[456,756,331,245] #Take the list\n",
209 | "dict1={}\n",
210 | "\n",
211 | "#Iterate over the list to find out the number of iterations needed for each element of the lsit to obtain the difference of 495\n",
212 | "\n",
213 | "for i in list1:\n",
214 | " value= difference(i)\n",
215 | " dict1.update({i:value})\n",
216 | "print(dict1)\n",
217 | " \n",
218 | " \n"
219 | ],
220 | "execution_count": null,
221 | "outputs": [
222 | {
223 | "output_type": "stream",
224 | "name": "stdout",
225 | "text": [
226 | "{456: 5, 756: 5, 331: 5, 245: 4}\n"
227 | ]
228 | }
229 | ]
230 | },
231 | {
232 | "cell_type": "code",
233 | "metadata": {
234 | "id": "2oApPSBZUMUy"
235 | },
236 | "source": [
237 | ""
238 | ],
239 | "execution_count": null,
240 | "outputs": []
241 | }
242 | ]
243 | }
244 |
--------------------------------------------------------------------------------
/Task_LogicGates.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "LogicGates.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyNu+gQSUXs24bGMXjFXG8ZV",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "metadata": {
33 | "colab": {
34 | "base_uri": "https://localhost:8080/"
35 | },
36 | "id": "WRcZXtz5IsQB",
37 | "outputId": "018a5e8a-ff0a-43c7-e318-b410f4e1d2f0"
38 | },
39 | "source": [
40 | "choice= input(\"Enter your choice, not, and, or, xor: \") #Enter logic gates choice\n",
41 | "x= bool(int(input(\"enter 1 or 0: \"))) #Enter the two numbers\n",
42 | "y= bool(int(input(\"enter 1 or 0: \")))\n",
43 | "\n",
44 | "#NOT gate\n",
45 | "if choice==\"not\":\n",
46 | " print(\"not of \", x, \" is \", not x)\n",
47 | "\n",
48 | "#AND gate\n",
49 | "elif choice==\"and\":\n",
50 | " print(x, \" and \", y, \" is \", x and y)\n",
51 | "\n",
52 | "#OR gate\n",
53 | "elif choice==\"or\":\n",
54 | " print(x, \" or \", y, \" is \", x or y)\n",
55 | "\n",
56 | "#XOR gate\n",
57 | "elif choice==\"xor\":\n",
58 | " if x==y:\n",
59 | " print(x, \" xor \", y, \" is False\")\n",
60 | " else:\n",
61 | " print(x, \" xor \", y, \" is True\")\n",
62 | " "
63 | ],
64 | "execution_count": 6,
65 | "outputs": [
66 | {
67 | "output_type": "stream",
68 | "text": [
69 | "Enter your choice, not, and, or, xor: xor\n",
70 | "enter 1 or 0: 1\n",
71 | "enter 1 or 0: 0\n",
72 | "True xor False is True\n"
73 | ],
74 | "name": "stdout"
75 | }
76 | ]
77 | }
78 | ]
79 | }
80 |
--------------------------------------------------------------------------------
/Task_NumberGame.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "NumberGame.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyOmfwk0jVeRbYW6XQ6vn2Gl",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "8kInnvN0xH8O"
34 | },
35 | "source": [
36 | "Problem Statement\n",
37 | "\n",
38 | "Geeta and Seeta are playing a number game, where for every iteration a random number (1 to 50) will be generated first for Geeta and then for Seeta. Input the number of iterations to be played from the user. They get points as follows:
\n",
39 | "i.\tFor every even number, 2 point
\n",
40 | "ii.\tFor every odd number, 3 points
\n",
41 | "iii.\tFor every square number (even or odd), 4 points
\n",
42 | "Print the points obtained by Geeta and Seeta\n"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "metadata": {
48 | "id": "0PDiIjLjyBXz"
49 | },
50 | "source": [
51 | "import random\n",
52 | "import math"
53 | ],
54 | "execution_count": null,
55 | "outputs": []
56 | },
57 | {
58 | "cell_type": "code",
59 | "metadata": {
60 | "colab": {
61 | "base_uri": "https://localhost:8080/"
62 | },
63 | "id": "7SN392JyxD-H",
64 | "outputId": "5a2a7bdc-e7ce-459c-995e-9b1d8465857b"
65 | },
66 | "source": [
67 | "\n",
68 | "n= int(input(\"Enter the number of iterations: \"))\n",
69 | "# store the random numbers generated for each iteration for Geeta and Seeta in lists\n",
70 | "g_list=[]\n",
71 | "s_list=[]\n",
72 | "# store the score for each iteration aciheved by Geeta and Seeta in lists\n",
73 | "g_total=[]\n",
74 | "s_total=[]\n",
75 | "\n",
76 | "#Execute the for loop for the given number of iteraions\n",
77 | "for i in range(n):\n",
78 | "#Generate random numbers for Geeta and Seeta for each iteration\n",
79 | " g= random.randint(1,51)\n",
80 | " s= random.randint(1,51)\n",
81 | "#Append the numbers generated to the individual list for Geeta and Seeta\n",
82 | " g_list.append(g)\n",
83 | " s_list.append(s)\n",
84 | "\n",
85 | "#Check is the number generated by Geeta is a square number or even number or odd number. Append appropriate score in the list\n",
86 | " if math.sqrt(g) % 1==0:\n",
87 | " g_total.append(4)\n",
88 | " elif g%2==0:\n",
89 | " g_total.append(2)\n",
90 | " else:\n",
91 | " g_total.append(3)\n",
92 | "\n",
93 | "#Check is the number generated by Seeta is a square number or even number or odd number. Append appropriate score in the list\n",
94 | " if math.sqrt(s) % 1==0:\n",
95 | " s_total.append(4)\n",
96 | " elif s%2==0:\n",
97 | " s_total.append(2)\n",
98 | " else:\n",
99 | " s_total.append(3)\n",
100 | "\n",
101 | "#Find the total score for Geeta and Seeta\n",
102 | "g_sum= sum(g_total)\n",
103 | "s_sum= sum(s_total)\n",
104 | "\n",
105 | "#Print all the random numbers generated for each iteration, score for each round and total score\n",
106 | "print(\"Numbers generated for Geeta \", g_list)\n",
107 | "print(\"Numbers generated for Seeta \", s_list)\n",
108 | "print(\"Points for Geeta in each round \", g_total)\n",
109 | "print(\"Points for Seeta in each round \", s_total)\n",
110 | "print(\"Geeta's score is \",g_sum )\n",
111 | "print(\"Seeta's score is \",s_sum )\n",
112 | "\n",
113 | "#Declare the winner\n",
114 | "if g_sum==s_sum:\n",
115 | " print(\"Its a draw!\")\n",
116 | "elif g_sum>s_sum:\n",
117 | " print(\"Winner is Geeta!\")\n",
118 | "else:\n",
119 | " print(\"Winner is Seeta!\") "
120 | ],
121 | "execution_count": null,
122 | "outputs": [
123 | {
124 | "output_type": "stream",
125 | "text": [
126 | "Enter the number of iterations: 3\n",
127 | "Numbers generated for Geeta [38, 27, 8]\n",
128 | "Numbers generated for Seeta [27, 9, 50]\n",
129 | "Points for Geeta in each round [2, 3, 2]\n",
130 | "Points for Seeta in each round [3, 4, 2]\n",
131 | "Geeta's score is 7\n",
132 | "Seeta's score is 9\n",
133 | "Winner is Seeta!\n"
134 | ],
135 | "name": "stdout"
136 | }
137 | ]
138 | },
139 | {
140 | "cell_type": "code",
141 | "metadata": {
142 | "id": "9WpQY1WY3EGG"
143 | },
144 | "source": [
145 | ""
146 | ],
147 | "execution_count": null,
148 | "outputs": []
149 | }
150 | ]
151 | }
152 |
--------------------------------------------------------------------------------
/Task_Practice_Problems.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Practice_Problems1.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyPYTQTQNYUYvypdCbGiEXNa",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | },
15 | "language_info": {
16 | "name": "python"
17 | }
18 | },
19 | "cells": [
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {
23 | "id": "view-in-github",
24 | "colab_type": "text"
25 | },
26 | "source": [
27 | "
"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "J0vs7MFe14WI"
34 | },
35 | "source": [
36 | "**Problem1**
\n",
37 | "Create a program that asks the user to enter their name and their age.
\n",
38 | "Print a message with their name to tell what will be their age after 5 years"
39 | ]
40 | },
41 | {
42 | "cell_type": "markdown",
43 | "metadata": {
44 | "id": "WdlOM33g2SpS"
45 | },
46 | "source": [
47 | "**Problem2**
\n",
48 | "Take two integer as input.
\n",
49 | "Calculate their product.
\n",
50 | "If the product is greater than 500, then return their difference"
51 | ]
52 | },
53 | {
54 | "cell_type": "markdown",
55 | "metadata": {
56 | "id": "3rNukvGC5hv-"
57 | },
58 | "source": [
59 | "**Problem 3**
\n",
60 | "Input range of numbers from the user
\n",
61 | "Add all the numbers in that range\n",
62 | "Print the sum\n"
63 | ]
64 | },
65 | {
66 | "cell_type": "markdown",
67 | "metadata": {
68 | "id": "RpFSsJ8b6i6U"
69 | },
70 | "source": [
71 | "**Problem 4**
\n",
72 | "Find HCF of two numbers\n",
73 | "\n",
74 | "\n",
75 | "\n"
76 | ]
77 | },
78 | {
79 | "cell_type": "markdown",
80 | "metadata": {
81 | "id": "FrfoaUMl6k_t"
82 | },
83 | "source": [
84 | ""
85 | ]
86 | },
87 | {
88 | "cell_type": "code",
89 | "metadata": {
90 | "id": "vaUBDOJT1zeC"
91 | },
92 | "source": [
93 | ""
94 | ],
95 | "execution_count": null,
96 | "outputs": []
97 | }
98 | ]
99 | }
100 |
--------------------------------------------------------------------------------
/VacuumCleaner.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "VacuumCleaner.ipynb",
7 | "provenance": [],
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "source": [
32 | "Simple Vacuum Cleaner World\n",
33 | "\n",
34 | "Two implementations for Vacuum cleaner world are done.\n",
35 | "\n",
36 | "In first implementation, number of rooms will be given by the user. Status of each room whether clean or dirty will also be given. Vacuum cleaner will be initially located in the first room. Then it will traverse each room sequentially\n",
37 | "\n",
38 | "In the second implementation, only two vacuum cleaner rooms are taken into consideration (Similar to example in book by Russel Norwig). Intitial location and intitial status of both the room will be asked from the user. Accordingly cleaning will be done and cost will be printed."
39 | ],
40 | "metadata": {
41 | "id": "cr696BmZqCRY"
42 | }
43 | },
44 | {
45 | "cell_type": "markdown",
46 | "source": [
47 | "First Implementation"
48 | ],
49 | "metadata": {
50 | "id": "yRk41nj0HCBd"
51 | }
52 | },
53 | {
54 | "cell_type": "markdown",
55 | "source": [
56 | "Simple vaccum cleaner world program\n",
57 | "\n",
58 | "This program will ask from the user about the total number of rows and the number of rooms in each row.\n",
59 | "\n",
60 | "We will assume that the vacuum cleaner is initially in the 0th room of the 0th row. \n",
61 | "\n",
62 | "It will check if the room has dust or not. Accordingly it will suck the dust and move to the next room sequentially.\n",
63 | "\n",
64 | "Goal state- All rooms are clean\n",
65 | "\n",
66 | "\n"
67 | ],
68 | "metadata": {
69 | "id": "vSJCClscE0tT"
70 | }
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": null,
75 | "metadata": {
76 | "id": "kDtzDuCO7QMh"
77 | },
78 | "outputs": [],
79 | "source": [
80 | "#Import important packages\n",
81 | "import random\n",
82 | "import numpy as np"
83 | ]
84 | },
85 | {
86 | "cell_type": "code",
87 | "source": [
88 | "#Define the grid\n",
89 | "#Enter the number of floors (rows) and rooms on each floor (grid)\n",
90 | "x,y= input(\"Enter number of rows and columns\").split() "
91 | ],
92 | "metadata": {
93 | "colab": {
94 | "base_uri": "https://localhost:8080/"
95 | },
96 | "id": "ovdxzSlz_tnK",
97 | "outputId": "dd6e73bb-01bb-45ae-fd75-1a5021537b15"
98 | },
99 | "execution_count": null,
100 | "outputs": [
101 | {
102 | "name": "stdout",
103 | "output_type": "stream",
104 | "text": [
105 | "Enter number of rows and columns2 3\n"
106 | ]
107 | }
108 | ]
109 | },
110 | {
111 | "cell_type": "code",
112 | "source": [
113 | "#convert the entered rows and columns to int \n",
114 | "x=int(x)\n",
115 | "y=int(y)"
116 | ],
117 | "metadata": {
118 | "id": "6ibHJDETBDEg"
119 | },
120 | "execution_count": null,
121 | "outputs": []
122 | },
123 | {
124 | "cell_type": "code",
125 | "source": [
126 | "#enter the cleanliness status of the room. Put 1 for clean room and 0 for dirty room\n",
127 | "rooms= []\n",
128 | "for i in range(x):\n",
129 | " row=[]\n",
130 | " for j in range(y):\n",
131 | " row.append(input())\n",
132 | " rooms.append(row)"
133 | ],
134 | "metadata": {
135 | "colab": {
136 | "base_uri": "https://localhost:8080/"
137 | },
138 | "id": "Wu3dCgmLAXSs",
139 | "outputId": "88b5d985-9cf1-4502-96eb-449569b3e7dc"
140 | },
141 | "execution_count": null,
142 | "outputs": [
143 | {
144 | "name": "stdout",
145 | "output_type": "stream",
146 | "text": [
147 | "1\n",
148 | "0\n",
149 | "0\n",
150 | "1\n",
151 | "0\n",
152 | "0\n"
153 | ]
154 | }
155 | ]
156 | },
157 | {
158 | "cell_type": "code",
159 | "source": [
160 | "rooms= np.array(rooms)\n",
161 | "print(rooms) #Print the room cleaniliness status"
162 | ],
163 | "metadata": {
164 | "id": "nLB3h09OELss"
165 | },
166 | "execution_count": null,
167 | "outputs": []
168 | },
169 | {
170 | "cell_type": "code",
171 | "source": [
172 | ""
173 | ],
174 | "metadata": {
175 | "colab": {
176 | "base_uri": "https://localhost:8080/"
177 | },
178 | "id": "1Bbd-yw-7bPa",
179 | "outputId": "c303d5bb-5466-440f-a95b-431dd01c414c"
180 | },
181 | "execution_count": null,
182 | "outputs": [
183 | {
184 | "output_type": "stream",
185 | "name": "stdout",
186 | "text": [
187 | "[['1' '0' '0']\n",
188 | " ['1' '0' '0']]\n"
189 | ]
190 | }
191 | ]
192 | },
193 | {
194 | "cell_type": "code",
195 | "source": [
196 | "\"\"\"Loops to traverse through all the rooms\n",
197 | "Check if the current room is dirty or clean\n",
198 | "Perform the action and announce the action done\"\"\"\n",
199 | "\n",
200 | "\n",
201 | "for i in range(x):\n",
202 | " for j in range(y):\n",
203 | " if rooms[i][j]=='1':\n",
204 | " print(\"Room {}{} is already clean\".format(i,j) )\n",
205 | " else:\n",
206 | " rooms[i][j]='1'\n",
207 | " print(\"Room {}{} is now clean\".format(i,j))"
208 | ],
209 | "metadata": {
210 | "colab": {
211 | "base_uri": "https://localhost:8080/"
212 | },
213 | "id": "By1i1dMM-aw8",
214 | "outputId": "1b872873-832e-4d35-c0d7-33d90c29b775"
215 | },
216 | "execution_count": null,
217 | "outputs": [
218 | {
219 | "output_type": "stream",
220 | "name": "stdout",
221 | "text": [
222 | "Room 00 is already clean\n",
223 | "Room 01 is now clean\n",
224 | "Room 02 is now clean\n",
225 | "Room 10 is already clean\n",
226 | "Room 11 is now clean\n",
227 | "Room 12 is now clean\n"
228 | ]
229 | }
230 | ]
231 | },
232 | {
233 | "cell_type": "markdown",
234 | "source": [
235 | "Second Implementation\n",
236 | "\n"
237 | ],
238 | "metadata": {
239 | "id": "oGBx5uWoJQit"
240 | }
241 | },
242 | {
243 | "cell_type": "code",
244 | "source": [
245 | "\"\"\"\n",
246 | "There are two rooms A and B\n",
247 | "We will assume \"1\" for room clean and \"0\" for room dirty\n",
248 | "\"\"\"\n",
249 | "\n",
250 | "goal_status= {\"A\": \"1\", \"B\":\"1\"} #Goal status for the vacuum cleaner\n",
251 | "\n",
252 | "#Take the initial vacuum cleaner location from the user\n",
253 | "room= input(\"Enter the initial vacuum cleaner location A or B: \")\n",
254 | "\n",
255 | "#Take the current status of both the rooms from the user\n",
256 | "#\"1\" for clean room and \"0\" for dirty room\n",
257 | "\n",
258 | "current_status= {\"A\": input(\"Enter status of A: \"), \"B\": input(\"Enter status of B:\")}\n",
259 | "\n",
260 | "#Print the current status of the room\n",
261 | "\n",
262 | "print(\"Current status of both the rooms is \", current_status)\n",
263 | "\n"
264 | ],
265 | "metadata": {
266 | "colab": {
267 | "base_uri": "https://localhost:8080/"
268 | },
269 | "id": "cTqSpf6QHGYm",
270 | "outputId": "d34df300-ee83-49bd-886f-6cde4891e305"
271 | },
272 | "execution_count": 1,
273 | "outputs": [
274 | {
275 | "output_type": "stream",
276 | "name": "stdout",
277 | "text": [
278 | "Enter the initial vacuum cleaner location A or B: B\n",
279 | "Enter status of A: 0\n",
280 | "Enter status of B:1\n",
281 | "Current status of both the rooms is {'A': '0', 'B': '1'}\n"
282 | ]
283 | }
284 | ]
285 | },
286 | {
287 | "cell_type": "code",
288 | "source": [
289 | "#Function to clean both the rooms, update their status and cost incurred\n",
290 | "\n",
291 | "def clean(room, current_status):\n",
292 | "#Function will take the initial location and current status as arguments\n",
293 | "\n",
294 | " cost=0 #Initialize the cost\n",
295 | " \n",
296 | " # If initial location of the vacuum cleaner is A\n",
297 | " if room == \"A\":\n",
298 | " if current_status[\"A\"]== \"0\":\n",
299 | " print(\"Room A is dirty, cleaning to be done. It will cost 1 unit \")\n",
300 | " #action of sucking or cleaning to be performed\n",
301 | " current_status[\"A\"]= \"1\"\n",
302 | " cost+=1\n",
303 | " else:\n",
304 | " print(\"Room A is clean, no cost incurred. Moving to Room B\")\n",
305 | "\n",
306 | " #Then vacuum cleaner should go to room B and check for its status\n",
307 | " if current_status[\"B\"]==\"0\":\n",
308 | " print(\"Room B is dirty, cleaning to be done. It will cost 1 unit \")\n",
309 | " #action of sucking to be performed\n",
310 | " current_status[\"B\"]=\"1\"\n",
311 | " cost+=1\n",
312 | " else:\n",
313 | " print(\"Room B is clean, no cost incurred. Will update the current status and check with the goal status\")\n",
314 | " return current_status,cost\n",
315 | "\n",
316 | " # If initial location of the vacuum cleaner is B\n",
317 | " if room==\"B\":\n",
318 | " if current_status[\"B\"]==\"0\":\n",
319 | " print(\"Room B is dirty, cleaning to be done. It will cost 1 unit \")\n",
320 | " #action of sucking or cleaning to be performed\n",
321 | " current_status[\"B\"]= \"1\"\n",
322 | " cost+=1\n",
323 | " else:\n",
324 | " print(\"Room B is clean, no cost incurred. Moving to room A\")\n",
325 | "\n",
326 | " #Then vacuum cleaner should go to room A and check for its status\n",
327 | " if current_status[\"A\"]== \"0\":\n",
328 | " print(\"Room A is dirty, cleaning to be done. It will cost 1 unit \")\n",
329 | " #action of sucking or cleaning to be performed\n",
330 | " current_status[\"A\"]= \"1\"\n",
331 | " else:\n",
332 | " print(\"Room B is clean, no cost incurred. Will update the current status and check with the goal status\")\n",
333 | " return current_status, cost\n",
334 | "\n"
335 | ],
336 | "metadata": {
337 | "id": "XIYTXSWqcJXS"
338 | },
339 | "execution_count": null,
340 | "outputs": []
341 | },
342 | {
343 | "cell_type": "code",
344 | "source": [
345 | "#Check the updated status with the goal status after cleaning\n",
346 | "#Print the result if the goal is achieved\n",
347 | "update_status,cost= clean(room, current_status)\n",
348 | "if update_status==goal_status:\n",
349 | " print(\"Goal is achieved. Both the rooms are clean. Total cost incurred is \", cost)"
350 | ],
351 | "metadata": {
352 | "colab": {
353 | "base_uri": "https://localhost:8080/"
354 | },
355 | "id": "bVO-hIicew5O",
356 | "outputId": "7d30b10e-1edd-43fd-aafa-fdc796eacfee"
357 | },
358 | "execution_count": null,
359 | "outputs": [
360 | {
361 | "output_type": "stream",
362 | "name": "stdout",
363 | "text": [
364 | "Goal is achieved. Both the rooms are clean. Total cost incurred is 0\n"
365 | ]
366 | }
367 | ]
368 | },
369 | {
370 | "cell_type": "code",
371 | "source": [
372 | "print(\"Status of both the rooms after cleaning is\", update_status)"
373 | ],
374 | "metadata": {
375 | "colab": {
376 | "base_uri": "https://localhost:8080/"
377 | },
378 | "id": "BWkOZShkmGV6",
379 | "outputId": "284cbaed-f17d-4644-c303-614749844c19"
380 | },
381 | "execution_count": null,
382 | "outputs": [
383 | {
384 | "output_type": "stream",
385 | "name": "stdout",
386 | "text": [
387 | "Status of both the rooms after cleaning is {'A': '1', 'B': '1'}\n"
388 | ]
389 | }
390 | ]
391 | },
392 | {
393 | "cell_type": "code",
394 | "source": [
395 | ""
396 | ],
397 | "metadata": {
398 | "id": "19KT081gmINA"
399 | },
400 | "execution_count": null,
401 | "outputs": []
402 | }
403 | ]
404 | }
--------------------------------------------------------------------------------