├── Introduction_to_Python.ipynb
├── Python Crash Course.ipynb
├── Python_Loops.ipynb
├── Python_for_Machine_Learning.ipynb
├── README.md
├── areacircle.py
├── diff_while1.py
├── first_prog.py
├── flight_program.py
├── forloop.py
├── forloop2.py
├── function_problem.py
├── if_elif.py
├── list_in_list.py
├── list_to_tuple.py
├── list_user_input.py
├── listprog.py
├── search_in_list.py
├── smallest_no.py
├── student_records.py
├── typecasting.py
└── whileloop.py
/Introduction_to_Python.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Introduction to Python",
7 | "provenance": [],
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | }
14 | },
15 | "cells": [
16 | {
17 | "cell_type": "markdown",
18 | "metadata": {
19 | "id": "view-in-github",
20 | "colab_type": "text"
21 | },
22 | "source": [
23 | "
"
24 | ]
25 | },
26 | {
27 | "cell_type": "markdown",
28 | "metadata": {
29 | "id": "ei-GOj0Gu-9S",
30 | "colab_type": "text"
31 | },
32 | "source": [
33 | "#Introduction to Colab for the classroom\n",
34 | "\n",
35 | "This is an \"Google Colab\" notebook. It's a shared document where everybody can see and make changes to the same document.\n",
36 | "\n",
37 | "The first few problems will get us acquainted with working together;\n",
38 | "\n",
39 | "1.Go to file in the upper left corner\n",
40 | "\n",
41 | "2.Click Save copy in drive\n",
42 | "\n",
43 | "3.Follow along and use your own notebook!"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "metadata": {
49 | "id": "1D9tQyRbuQwg",
50 | "colab_type": "code",
51 | "colab": {}
52 | },
53 | "source": [
54 | "# This is a comment\n",
55 | "# in a code cell\n",
56 | "# Press shift + enter to execute a cell\n",
57 | "# Nothing will print from this cell since these are all comments"
58 | ],
59 | "execution_count": 1,
60 | "outputs": []
61 | },
62 | {
63 | "cell_type": "markdown",
64 | "metadata": {
65 | "id": "-TjZmYl523lH",
66 | "colab_type": "text"
67 | },
68 | "source": [
69 | "# **What is Python?**\n",
70 | "\n",
71 | "Python is an open-source object-oriented programming language. It first \n",
72 | "appeared in 1991 and has become extremely popular among data scientists. StackOverflow calls it as the fastest growing programming language.\n",
73 | "\n",
74 | "It is used for:\n",
75 | "\n",
76 | "* Web development\n",
77 | "* Software Development\n",
78 | "* Mathematics\n",
79 | "* System Scripting and Much more\n",
80 | "\n",
81 | "\n",
82 | "# **What Python can do?**\n",
83 | "\n",
84 | "* Python can be used on a server to create web applications.\n",
85 | "* Python can be used alongside software to create workflows.\n",
86 | "* Python can connect to database systems. It can also read and modify files.\n",
87 | "* Python can be used to handle big data and perform complex mathematics.\n",
88 | "* Python can be used for rapid prototyping, or for production-ready software development."
89 | ]
90 | },
91 | {
92 | "cell_type": "markdown",
93 | "metadata": {
94 | "id": "Jj2dwPJGxZDy",
95 | "colab_type": "text"
96 | },
97 | "source": [
98 | "# Why Learn Python?\n",
99 | "Python’s syntax is very simple hence it's easy for the beginners to understand. The lines of code required for a task is less compared to other languages.\n",
100 | "\n",
101 | "\n",
102 | "* Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).\n",
103 | "* Python has a simple syntax similar to the English language.\n",
104 | "* Python has syntax that allows developers to write programs with fewer lines than some other programming languages.\n",
105 | "* Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick.\n",
106 | "* Python can be treated in a procedural way, an object-orientated way or a functional way."
107 | ]
108 | },
109 | {
110 | "cell_type": "markdown",
111 | "metadata": {
112 | "id": "XnoczNYXz1TI",
113 | "colab_type": "text"
114 | },
115 | "source": [
116 | "# **Let’s look at some cool features of Python:**\n",
117 | "\n",
118 | "1. Simple and easy to learn\n",
119 | "2. Free and Open Source\n",
120 | "3. Portable\n",
121 | "4. Supports different programming paradigm\n",
122 | "5. Extensible"
123 | ]
124 | },
125 | {
126 | "cell_type": "markdown",
127 | "metadata": {
128 | "id": "tjwVKXvB0VKw",
129 | "colab_type": "text"
130 | },
131 | "source": [
132 | "# **Python Applications:**\n",
133 | "\n",
134 | "1. Artificial Intelligence\n",
135 | "2. Desktop Application\n",
136 | "3. Automation\n",
137 | "4. Web Development\n",
138 | "5. Data Wrangling, Exploration\n",
139 | "6. Visualization"
140 | ]
141 | },
142 | {
143 | "cell_type": "markdown",
144 | "metadata": {
145 | "id": "huR1RLMp1FgR",
146 | "colab_type": "text"
147 | },
148 | "source": [
149 | "# **Python Setup:**\n",
150 | "\n",
151 | "For Windows PC, search in the start bar for Python or run the following on the Command Line (cmd.exe):\n",
152 | "\n",
153 | "```\n",
154 | "C:\\Users\\Your Name>python --version\n",
155 | "```\n",
156 | "\n",
157 | "To check if you have python installed on a Linux or Mac, then on linux open the command line or on Mac open the Terminal and type:\n",
158 | "\n",
159 | "```\n",
160 | "python --version\n",
161 | "```\n",
162 | "\n",
163 | "\n",
164 | "---\n",
165 | "\n",
166 | "\n",
167 | "* If you are on Windows OS download Python by [clicking here](https://www.python.org/downloads/windows/) and now install from the setup and in the start menu type IDLE.IDLE, you can think it as an Python’s IDE to run the Python Scripts.\n",
168 | "* If you are on Linux/Unix-like just open the terminal and on 99% linux OS Python comes preinstalled with the OS.Just type ‘python3’ in terminal and you are ready to go.\n"
169 | ]
170 | },
171 | {
172 | "cell_type": "markdown",
173 | "metadata": {
174 | "id": "cc1FDi8M26vh",
175 | "colab_type": "text"
176 | },
177 | "source": [
178 | "# **Starting with Python**\n",
179 | "\n",
180 | "Python is an interpreted programming language, this means that as a developer you write Python (.py) files in a text editor and then put those files into the python interpreter to be executed.\n",
181 | "\n",
182 | "The way to run a python file is like this on the command line:\n",
183 | "\n",
184 | "\n",
185 | "\n",
186 | "```\n",
187 | "C:\\Users\\Your Name>python helloworld.py\n",
188 | "```\n",
189 | "Where \"helloworld.py\" is the name of your python file.\n",
190 | "\n",
191 | "Let's write our first Python file, called helloworld.py, which can be done in any text editor.\n",
192 | "\n",
193 | "\n",
194 | "helloworld.py\n",
195 | "```\n",
196 | "print(\"Hello, World! :) \")\n",
197 | "```\n",
198 | "Simple as that. Save your file. Open your command line, navigate to the directory where you saved your file, and run:\n",
199 | "\n",
200 | "```\n",
201 | "C:\\Users\\Your Name>python helloworld.py\n",
202 | "```\n",
203 | "\n",
204 | "The output should read:\n",
205 | "\n",
206 | "\n",
207 | "\n",
208 | "```\n",
209 | "Hello, World! :)\n",
210 | "```\n",
211 | "\n"
212 | ]
213 | },
214 | {
215 | "cell_type": "markdown",
216 | "metadata": {
217 | "id": "qViu26SV6-TK",
218 | "colab_type": "text"
219 | },
220 | "source": [
221 | "# **Python Syntax & Indentation**\n",
222 | "\n",
223 | "Python syntax can be executed by writing directly in the Command Line:\n",
224 | "\n",
225 | "\n",
226 | "\n",
227 | "```\n",
228 | ">>> print(\"Hello, World! :)\")\n",
229 | "Hello, World! :)\n",
230 | "```\n",
231 | "\n",
232 | "Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:\n",
233 | "\n",
234 | "```\n",
235 | "C:\\Users\\Your Name>python myfile.py\n",
236 | "```\n",
237 | "\n",
238 | "\n",
239 | "**Python Indentation**\n",
240 | "\n",
241 | "\n",
242 | "Indentation refers to the spaces at the beginning of a code line.\n",
243 | "\n",
244 | "Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.\n",
245 | "\n",
246 | "Python uses indentation to indicate a block of code.\n",
247 | "\n",
248 | "Example :\n",
249 | "\n"
250 | ]
251 | },
252 | {
253 | "cell_type": "code",
254 | "metadata": {
255 | "id": "eR0pjRyn7Tta",
256 | "colab_type": "code",
257 | "colab": {
258 | "base_uri": "https://localhost:8080/",
259 | "height": 128
260 | },
261 | "outputId": "a63f7475-4f20-4e1d-b9ba-b5845cfd3bbd"
262 | },
263 | "source": [
264 | "if 5 > 2:\n",
265 | "print(\"Five is greater than two!\")"
266 | ],
267 | "execution_count": 2,
268 | "outputs": [
269 | {
270 | "output_type": "error",
271 | "ename": "IndentationError",
272 | "evalue": "ignored",
273 | "traceback": [
274 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print(\"Five is greater than two!\")\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n"
275 | ]
276 | }
277 | ]
278 | },
279 | {
280 | "cell_type": "markdown",
281 | "metadata": {
282 | "id": "k0xS4nZX9Y04",
283 | "colab_type": "text"
284 | },
285 | "source": [
286 | "The number of spaces is up to you as a programmer, but it has to be at least one."
287 | ]
288 | },
289 | {
290 | "cell_type": "code",
291 | "metadata": {
292 | "id": "Zq9-XLgz9cZ_",
293 | "colab_type": "code",
294 | "colab": {
295 | "base_uri": "https://localhost:8080/",
296 | "height": 67
297 | },
298 | "outputId": "25f5686b-b6e7-4786-dd51-840e229ed9ed"
299 | },
300 | "source": [
301 | "if 10 > 5:\n",
302 | " print(\"Ten is greater than Five!\") \n",
303 | "if 10 > 5:\n",
304 | " print(\"Ten is greater than Five!\") \n",
305 | " print(\"Ten is greater than Five!\") "
306 | ],
307 | "execution_count": 30,
308 | "outputs": [
309 | {
310 | "output_type": "stream",
311 | "text": [
312 | "Ten is greater than Five!\n",
313 | "Ten is greater than Five!\n",
314 | "Ten is greater than Five!\n"
315 | ],
316 | "name": "stdout"
317 | }
318 | ]
319 | },
320 | {
321 | "cell_type": "markdown",
322 | "metadata": {
323 | "id": "btd_GvHt9upn",
324 | "colab_type": "text"
325 | },
326 | "source": [
327 | "# **Comments:**\n",
328 | "\n",
329 | "Python has commenting capability for the purpose of in-code documentation.\n",
330 | "\n",
331 | "Comments start with a #, and Python will render the rest of the line as a comment:"
332 | ]
333 | },
334 | {
335 | "cell_type": "code",
336 | "metadata": {
337 | "id": "a_YeED9D9x6s",
338 | "colab_type": "code",
339 | "colab": {
340 | "base_uri": "https://localhost:8080/",
341 | "height": 33
342 | },
343 | "outputId": "750ea6f9-2874-49de-912c-ea63d8b77013"
344 | },
345 | "source": [
346 | "#This is a comment, this line will not execute.\n",
347 | "print(\"Yoooo!\")"
348 | ],
349 | "execution_count": 31,
350 | "outputs": [
351 | {
352 | "output_type": "stream",
353 | "text": [
354 | "Yoooo!\n"
355 | ],
356 | "name": "stdout"
357 | }
358 | ]
359 | },
360 | {
361 | "cell_type": "markdown",
362 | "metadata": {
363 | "id": "pft3WfTe-LUe",
364 | "colab_type": "text"
365 | },
366 | "source": [
367 | "* Comments can be used to explain Python code.\n",
368 | "\n",
369 | "* Comments can be used to make the code more readable.\n",
370 | "* Comments can be used to prevent execution when testing code."
371 | ]
372 | },
373 | {
374 | "cell_type": "markdown",
375 | "metadata": {
376 | "id": "wNrzNU5mC4W0",
377 | "colab_type": "text"
378 | },
379 | "source": [
380 | "**Multi Line Comments**\n",
381 | "\n",
382 | "You can add a multiline string (triple quotes) in your code, and place your comment inside it:"
383 | ]
384 | },
385 | {
386 | "cell_type": "code",
387 | "metadata": {
388 | "id": "fOW3Pgt5-fc8",
389 | "colab_type": "code",
390 | "colab": {
391 | "base_uri": "https://localhost:8080/",
392 | "height": 33
393 | },
394 | "outputId": "553687bd-02ff-4951-dd09-9cbb39c28747"
395 | },
396 | "source": [
397 | "\"\"\"\n",
398 | "This is a comment\n",
399 | "written in\n",
400 | "more than just one line\n",
401 | "\"\"\"\n",
402 | "print(\"Hello, World! :)\")"
403 | ],
404 | "execution_count": 32,
405 | "outputs": [
406 | {
407 | "output_type": "stream",
408 | "text": [
409 | "Hello, World! :)\n"
410 | ],
411 | "name": "stdout"
412 | }
413 | ]
414 | },
415 | {
416 | "cell_type": "markdown",
417 | "metadata": {
418 | "id": "MiCtOU0F-uLN",
419 | "colab_type": "text"
420 | },
421 | "source": [
422 | "# **Variables in Python:**\n",
423 | "\n",
424 | "Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.\n",
425 | "\n",
426 | "In Python you don’t need to declare variables before using it, unlike other languages like Java, C etc.\n",
427 | "\n",
428 | "# **Assigning values to a variable:**\n",
429 | "\n",
430 | "Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. \n",
431 | "The equal sign (=) is used to assign values to variables."
432 | ]
433 | },
434 | {
435 | "cell_type": "code",
436 | "metadata": {
437 | "id": "WOgTcEk2_J21",
438 | "colab_type": "code",
439 | "colab": {
440 | "base_uri": "https://localhost:8080/",
441 | "height": 50
442 | },
443 | "outputId": "448c8eff-cd3b-4e4c-acfb-ba7208c9077a"
444 | },
445 | "source": [
446 | "x = 11\n",
447 | "y = \"Google\"\n",
448 | "print(x)\n",
449 | "print(y)"
450 | ],
451 | "execution_count": 33,
452 | "outputs": [
453 | {
454 | "output_type": "stream",
455 | "text": [
456 | "11\n",
457 | "Google\n"
458 | ],
459 | "name": "stdout"
460 | }
461 | ]
462 | },
463 | {
464 | "cell_type": "markdown",
465 | "metadata": {
466 | "id": "1RlM5twW_VQB",
467 | "colab_type": "text"
468 | },
469 | "source": [
470 | "Variables do not need to be declared with any particular type and can even change type after they have been set."
471 | ]
472 | },
473 | {
474 | "cell_type": "code",
475 | "metadata": {
476 | "id": "fo-8MR7q_b-o",
477 | "colab_type": "code",
478 | "colab": {
479 | "base_uri": "https://localhost:8080/",
480 | "height": 33
481 | },
482 | "outputId": "96a12289-3297-4395-9e8c-ab9acfe92484"
483 | },
484 | "source": [
485 | "x = 4 # x is of type int\n",
486 | "x = \"Frost forbite\" # x is now of type str\n",
487 | "print(x)"
488 | ],
489 | "execution_count": 34,
490 | "outputs": [
491 | {
492 | "output_type": "stream",
493 | "text": [
494 | "Frost forbite\n"
495 | ],
496 | "name": "stdout"
497 | }
498 | ]
499 | },
500 | {
501 | "cell_type": "markdown",
502 | "metadata": {
503 | "id": "N09PcvZvFPt9",
504 | "colab_type": "text"
505 | },
506 | "source": [
507 | "String variables can be declared either by using single or double quotes:"
508 | ]
509 | },
510 | {
511 | "cell_type": "code",
512 | "metadata": {
513 | "id": "CGG7Pza5FQzg",
514 | "colab_type": "code",
515 | "colab": {}
516 | },
517 | "source": [
518 | "x = \"Marcus Burns\"\n",
519 | "# is the same as\n",
520 | "x = 'Marcus Burns'"
521 | ],
522 | "execution_count": 35,
523 | "outputs": []
524 | },
525 | {
526 | "cell_type": "markdown",
527 | "metadata": {
528 | "id": "_SR0saT9KvwE",
529 | "colab_type": "text"
530 | },
531 | "source": [
532 | "# **Assign Value to Multiple Variables:**\n",
533 | "\n",
534 | "Python allows you to assign values to multiple variables in one line:"
535 | ]
536 | },
537 | {
538 | "cell_type": "code",
539 | "metadata": {
540 | "id": "tTsMoY7MK1wx",
541 | "colab_type": "code",
542 | "colab": {
543 | "base_uri": "https://localhost:8080/",
544 | "height": 67
545 | },
546 | "outputId": "8730664d-7b1a-4dcb-d3ac-f680384b3c77"
547 | },
548 | "source": [
549 | "x, y, z = \"Apples\", \"Grapes\", \"Mangoes\"\n",
550 | "print(x)\n",
551 | "print(y)\n",
552 | "print(z)"
553 | ],
554 | "execution_count": 36,
555 | "outputs": [
556 | {
557 | "output_type": "stream",
558 | "text": [
559 | "Apples\n",
560 | "Grapes\n",
561 | "Mangoes\n"
562 | ],
563 | "name": "stdout"
564 | }
565 | ]
566 | },
567 | {
568 | "cell_type": "markdown",
569 | "metadata": {
570 | "id": "TyJ-roCOK9zT",
571 | "colab_type": "text"
572 | },
573 | "source": [
574 | "And you can assign the same value to multiple variables in one line:"
575 | ]
576 | },
577 | {
578 | "cell_type": "code",
579 | "metadata": {
580 | "id": "AC-SxzcVK_6J",
581 | "colab_type": "code",
582 | "colab": {
583 | "base_uri": "https://localhost:8080/",
584 | "height": 67
585 | },
586 | "outputId": "d0b5fb1c-c1da-4c8f-f019-d6bc8312c1cf"
587 | },
588 | "source": [
589 | "x = y = z = \"Kiwi\"\n",
590 | "print(x)\n",
591 | "print(y)\n",
592 | "print(z)"
593 | ],
594 | "execution_count": 37,
595 | "outputs": [
596 | {
597 | "output_type": "stream",
598 | "text": [
599 | "Kiwi\n",
600 | "Kiwi\n",
601 | "Kiwi\n"
602 | ],
603 | "name": "stdout"
604 | }
605 | ]
606 | },
607 | {
608 | "cell_type": "markdown",
609 | "metadata": {
610 | "id": "CiqSG-_tLQDR",
611 | "colab_type": "text"
612 | },
613 | "source": [
614 | "# **Assign Value to Multiple Variables:**\n",
615 | "\n",
616 | "The Python print statement is often used to output variables.\n",
617 | "\n",
618 | "To combine both text and a variable, Python uses the + character:"
619 | ]
620 | },
621 | {
622 | "cell_type": "code",
623 | "metadata": {
624 | "id": "ULO5y2EOLS8J",
625 | "colab_type": "code",
626 | "colab": {
627 | "base_uri": "https://localhost:8080/",
628 | "height": 33
629 | },
630 | "outputId": "7d5eb0fb-9b33-47ec-89f7-ad0c0b19c088"
631 | },
632 | "source": [
633 | "x = \"amazing!\"\n",
634 | "print(\"You are \" + x)"
635 | ],
636 | "execution_count": 38,
637 | "outputs": [
638 | {
639 | "output_type": "stream",
640 | "text": [
641 | "You are amazing!\n"
642 | ],
643 | "name": "stdout"
644 | }
645 | ]
646 | },
647 | {
648 | "cell_type": "markdown",
649 | "metadata": {
650 | "id": "FzVHp-HhORrv",
651 | "colab_type": "text"
652 | },
653 | "source": [
654 | "You can also use the + character to add a variable to another variable:"
655 | ]
656 | },
657 | {
658 | "cell_type": "code",
659 | "metadata": {
660 | "id": "MnHDOMNzOVWd",
661 | "colab_type": "code",
662 | "colab": {
663 | "base_uri": "https://localhost:8080/",
664 | "height": 33
665 | },
666 | "outputId": "7faf6a5f-5615-4889-e59a-19daa26aa179"
667 | },
668 | "source": [
669 | "x = \"You're \"\n",
670 | "y = \"awesome!\"\n",
671 | "z = x + y\n",
672 | "print(z)"
673 | ],
674 | "execution_count": 39,
675 | "outputs": [
676 | {
677 | "output_type": "stream",
678 | "text": [
679 | "You're awesome!\n"
680 | ],
681 | "name": "stdout"
682 | }
683 | ]
684 | },
685 | {
686 | "cell_type": "markdown",
687 | "metadata": {
688 | "id": "aMb4ewOgOb8I",
689 | "colab_type": "text"
690 | },
691 | "source": [
692 | "\n",
693 | "\n",
694 | "For numbers, the + character works as a mathematical operator:\n"
695 | ]
696 | },
697 | {
698 | "cell_type": "code",
699 | "metadata": {
700 | "id": "u6MSflGbOhOw",
701 | "colab_type": "code",
702 | "colab": {
703 | "base_uri": "https://localhost:8080/",
704 | "height": 33
705 | },
706 | "outputId": "eab438a7-a1d0-4ddd-fa7f-eba7f20a268f"
707 | },
708 | "source": [
709 | "x = 20\n",
710 | "y = 10\n",
711 | "print(x + y)"
712 | ],
713 | "execution_count": 40,
714 | "outputs": [
715 | {
716 | "output_type": "stream",
717 | "text": [
718 | "30\n"
719 | ],
720 | "name": "stdout"
721 | }
722 | ]
723 | },
724 | {
725 | "cell_type": "markdown",
726 | "metadata": {
727 | "id": "pULZk2i3OpH8",
728 | "colab_type": "text"
729 | },
730 | "source": [
731 | "# **Data Types in Python:**\n",
732 | "Python supports various data types, these data types defines the operations possible on the variables and the storage method. \n",
733 | "\n",
734 | "\n",
735 | "* Text Type:\t`str`\n",
736 | "* Numeric Types:\t`int, float, complex`\n",
737 | "* Sequence Types:\t`list, tuple, range`\n",
738 | "* Mapping Type:\t`dict`\n",
739 | "* Set Types:\t`set, frozenset`\n",
740 | "* Boolean Type:\t`bool`\n",
741 | "* Binary Types:\t`bytes, bytearray, memoryview`"
742 | ]
743 | },
744 | {
745 | "cell_type": "markdown",
746 | "metadata": {
747 | "id": "QVlmz3KzOvEv",
748 | "colab_type": "text"
749 | },
750 | "source": [
751 | "# **Getting the Data Type:**\n",
752 | "\n",
753 | "You can get the data type of any object by using the type() function:"
754 | ]
755 | },
756 | {
757 | "cell_type": "code",
758 | "metadata": {
759 | "id": "ZN5P37qLOwYW",
760 | "colab_type": "code",
761 | "colab": {
762 | "base_uri": "https://localhost:8080/",
763 | "height": 33
764 | },
765 | "outputId": "be6c07e0-ee1d-4d4b-d94c-93febd9340b1"
766 | },
767 | "source": [
768 | "x = 5\n",
769 | "print(type(x))"
770 | ],
771 | "execution_count": 41,
772 | "outputs": [
773 | {
774 | "output_type": "stream",
775 | "text": [
776 | "\n"
777 | ],
778 | "name": "stdout"
779 | }
780 | ]
781 | },
782 | {
783 | "cell_type": "markdown",
784 | "metadata": {
785 | "id": "JmJ0drEEO7Sp",
786 | "colab_type": "text"
787 | },
788 | "source": [
789 | "**Numeric:**\n",
790 | "\n",
791 | "Just as expected Numeric data types store numeric values. They are immutable data types, this means that you cannot change it’s value. Python supports three different Numeric data types:\n",
792 | "\n",
793 | "1. Integer type: It holds all the integer values i.e. all the positive and negative whole numbers, example – 10.\n",
794 | "\n",
795 | "2. Float type: It holds the real numbers and are represented by decimal and sometimes even scientific notations with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250), example – 10.24.\n",
796 | "\n",
797 | "3. Complex type: These are of the form a + bj, where a and b are floats and J represents the square root of -1 (which is an imaginary number), example – 10+6j.\n",
798 | "\n",
799 | "Now you can even perform type conversion. For example, you can convert the integer value to a float value and vice-versa."
800 | ]
801 | },
802 | {
803 | "cell_type": "code",
804 | "metadata": {
805 | "id": "860P3--YPFvM",
806 | "colab_type": "code",
807 | "colab": {
808 | "base_uri": "https://localhost:8080/",
809 | "height": 67
810 | },
811 | "outputId": "55f98dc8-646f-4f19-b0f1-b7b0545d824b"
812 | },
813 | "source": [
814 | "x = 11 # int\n",
815 | "y = 2.8 # float\n",
816 | "z = 3+1j # complex\n",
817 | "\n",
818 | "print(type(x))\n",
819 | "print(type(y))\n",
820 | "print(type(z))"
821 | ],
822 | "execution_count": 42,
823 | "outputs": [
824 | {
825 | "output_type": "stream",
826 | "text": [
827 | "\n",
828 | "\n",
829 | "\n"
830 | ],
831 | "name": "stdout"
832 | }
833 | ]
834 | },
835 | {
836 | "cell_type": "markdown",
837 | "metadata": {
838 | "id": "tMCB-d5SPPcz",
839 | "colab_type": "text"
840 | },
841 | "source": [
842 | "**Type Conversion**\n",
843 | "\n",
844 | "You can convert from one type to another with the int(), float(), and complex() methods:"
845 | ]
846 | },
847 | {
848 | "cell_type": "code",
849 | "metadata": {
850 | "id": "7SPYqyfwPQoj",
851 | "colab_type": "code",
852 | "colab": {
853 | "base_uri": "https://localhost:8080/",
854 | "height": 33
855 | },
856 | "outputId": "559349da-df07-423c-d93d-7c7cc2b4879c"
857 | },
858 | "source": [
859 | "a = 101\n",
860 | "# Convert it into float type\n",
861 | "b = float(a)\n",
862 | "print(b)"
863 | ],
864 | "execution_count": 43,
865 | "outputs": [
866 | {
867 | "output_type": "stream",
868 | "text": [
869 | "101.0\n"
870 | ],
871 | "name": "stdout"
872 | }
873 | ]
874 | },
875 | {
876 | "cell_type": "code",
877 | "metadata": {
878 | "id": "jLEAczK2Pf3_",
879 | "colab_type": "code",
880 | "colab": {
881 | "base_uri": "https://localhost:8080/",
882 | "height": 33
883 | },
884 | "outputId": "fb2eb2aa-6ed1-4cb3-ecb6-78ee1611e7ba"
885 | },
886 | "source": [
887 | "a = 8.16\n",
888 | "# Convert it into int type\n",
889 | "b = int(a)\n",
890 | "print(b)"
891 | ],
892 | "execution_count": 44,
893 | "outputs": [
894 | {
895 | "output_type": "stream",
896 | "text": [
897 | "8\n"
898 | ],
899 | "name": "stdout"
900 | }
901 | ]
902 | },
903 | {
904 | "cell_type": "markdown",
905 | "metadata": {
906 | "id": "uevK81TSPqiU",
907 | "colab_type": "text"
908 | },
909 | "source": [
910 | "**Strings:**\n",
911 | "\n",
912 | "Strings are amongst the most popular data types in Python. We can create them simply by enclosing characters in quotes. Python treats single and double quotes in exactly the same fashion. "
913 | ]
914 | },
915 | {
916 | "cell_type": "markdown",
917 | "metadata": {
918 | "id": "PFRQnE8vPswE",
919 | "colab_type": "text"
920 | },
921 | "source": [
922 | "**String Methods**\n",
923 | "\n",
924 | "Python has a set of built-in methods that you can use on strings.\n",
925 | "\n",
926 | "* capitalize() : Converts the first character to upper case\n",
927 | "* casefold() :\tConverts string into lower case\n",
928 | "* count() :\tReturns the number of times a specified value occurs in a string\n",
929 | "* encode()\t: Returns an encoded version of the string\n",
930 | "* find() :\tSearches the string for a specified value and returns the position of where it was found\n",
931 | "* format() :\tFormats specified values in a string\n",
932 | "* index()\t: Searches the string for a specified value and returns the position of where it was found\n",
933 | "* isdecimal() :\tReturns True if all characters in the string are decimals\n",
934 | "* isdigit() :\tReturns True if all characters in the string are digits\n",
935 | "* isidentifier() :\tReturns True if the string is an identifier\n",
936 | "* islower()\t: Returns True if all characters in the string are lower case\n",
937 | "* isnumeric()\t: Returns True if all characters in the string are numeric\n",
938 | "* isupper()\t: Returns True if all characters in the string are upper case\n",
939 | "* lower() :\tConverts a string into lower case\n",
940 | "* upper()\t: Converts a string into upper case"
941 | ]
942 | },
943 | {
944 | "cell_type": "markdown",
945 | "metadata": {
946 | "id": "URny1DMeQEUF",
947 | "colab_type": "text"
948 | },
949 | "source": [
950 | "**Strings are Arrays**\n",
951 | "\n",
952 | "Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters.\n",
953 | "\n",
954 | "However, Python does not have a character data type, a single character is simply a string with a length of 1.\n",
955 | "\n",
956 | "Square brackets can be used to access elements of the string."
957 | ]
958 | },
959 | {
960 | "cell_type": "code",
961 | "metadata": {
962 | "id": "d32_CS-LQLO9",
963 | "colab_type": "code",
964 | "colab": {
965 | "base_uri": "https://localhost:8080/",
966 | "height": 33
967 | },
968 | "outputId": "2df018a4-f5f1-4a86-f1ca-86cc53d5e612"
969 | },
970 | "source": [
971 | "#Get the character at position 1 (remember that the first character has the position 0):\n",
972 | "\n",
973 | "a = \"Hello, World!\"\n",
974 | "print(a[1])"
975 | ],
976 | "execution_count": 45,
977 | "outputs": [
978 | {
979 | "output_type": "stream",
980 | "text": [
981 | "e\n"
982 | ],
983 | "name": "stdout"
984 | }
985 | ]
986 | },
987 | {
988 | "cell_type": "code",
989 | "metadata": {
990 | "id": "J3T1vjztQhRo",
991 | "colab_type": "code",
992 | "colab": {
993 | "base_uri": "https://localhost:8080/",
994 | "height": 134
995 | },
996 | "outputId": "61335e11-0477-4244-a6d7-7ddbcd131ecc"
997 | },
998 | "source": [
999 | "a = \"Hello World!\"\n",
1000 | "b = 'Welcome'\n",
1001 | "\n",
1002 | "#operations on Strings-*\n",
1003 | "\n",
1004 | "print (len(b))\t #String Length\n",
1005 | "print (a.index('e')) #Locate a character in String\n",
1006 | "print (a.count('l'))\t #Count the number of times a character is repeated in a String\n",
1007 | "print (a[1:5])\t #Slicing\n",
1008 | "print (a[::-1])\t #Reverse a String\n",
1009 | "print (b.upper())\t #Convert the letters in a String to upper-case\n",
1010 | "print (b.lower()) #Convert the letters in a String to lower-case"
1011 | ],
1012 | "execution_count": 46,
1013 | "outputs": [
1014 | {
1015 | "output_type": "stream",
1016 | "text": [
1017 | "7\n",
1018 | "1\n",
1019 | "3\n",
1020 | "ello\n",
1021 | "!dlroW olleH\n",
1022 | "WELCOME\n",
1023 | "welcome\n"
1024 | ],
1025 | "name": "stdout"
1026 | }
1027 | ]
1028 | },
1029 | {
1030 | "cell_type": "markdown",
1031 | "metadata": {
1032 | "id": "KWC2_vcGRCig",
1033 | "colab_type": "text"
1034 | },
1035 | "source": [
1036 | "**List:**\n",
1037 | "\n",
1038 | "You can consider the Lists as Arrays in C, but in List you can store elements of different types, but in Array all the elements should of the same type.\n",
1039 | "List is the most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets."
1040 | ]
1041 | },
1042 | {
1043 | "cell_type": "markdown",
1044 | "metadata": {
1045 | "id": "CZjaRi7jRHxs",
1046 | "colab_type": "text"
1047 | },
1048 | "source": [
1049 | "**List Methods**\n",
1050 | "\n",
1051 | "Python has a set of built-in methods that you can use on lists.\n",
1052 | "\n",
1053 | "\n",
1054 | "* append() : Adds an element at the end of the list\n",
1055 | "* clear()\t: Removes all the elements from the list\n",
1056 | "* copy() :\tReturns a copy of the list\n",
1057 | "* count() :\tReturns the number of elements with the specified value\n",
1058 | "* extend(): Add the elements of a list (or any iterable), to the end of the current list\n",
1059 | "* index()\t: Returns the index of the first element with the specified value\n",
1060 | "* insert() :\tAdds an element at the specified position\n",
1061 | "* pop() :\tRemoves the element at the specified position\n",
1062 | "* remove() :\tRemoves the item with the specified value\n",
1063 | "* reverse() :\tReverses the order of the list\n",
1064 | "* sort() :\tSorts the list"
1065 | ]
1066 | },
1067 | {
1068 | "cell_type": "code",
1069 | "metadata": {
1070 | "id": "0XmYBMyGRS62",
1071 | "colab_type": "code",
1072 | "colab": {
1073 | "base_uri": "https://localhost:8080/",
1074 | "height": 134
1075 | },
1076 | "outputId": "264d3f8c-88a2-43e9-ac86-4e0689615ae1"
1077 | },
1078 | "source": [
1079 | "Sports = ['Basketball', 'Cricket', 'Football', 1,5,6.0]\n",
1080 | "print(Sports)\n",
1081 | "\n",
1082 | "#operations on lists\n",
1083 | "\n",
1084 | "print (Sports[0]) #printing the value at index\n",
1085 | "print (Sports[0:3]) #slicing the values in the List\n",
1086 | "Sports[2]='Rugby' #replacing the value at particular index\n",
1087 | "print (Sports[2])\n",
1088 | "del Sports[1] #deleting a value at an index \n",
1089 | "print (Sports)\n",
1090 | "print (len(Sports)) #get the length of the string\n",
1091 | "print (Sports*2)\n",
1092 | "\n"
1093 | ],
1094 | "execution_count": 47,
1095 | "outputs": [
1096 | {
1097 | "output_type": "stream",
1098 | "text": [
1099 | "['Basketball', 'Cricket', 'Football', 1, 5, 6.0]\n",
1100 | "Basketball\n",
1101 | "['Basketball', 'Cricket', 'Football']\n",
1102 | "Rugby\n",
1103 | "['Basketball', 'Rugby', 1, 5, 6.0]\n",
1104 | "5\n",
1105 | "['Basketball', 'Rugby', 1, 5, 6.0, 'Basketball', 'Rugby', 1, 5, 6.0]\n"
1106 | ],
1107 | "name": "stdout"
1108 | }
1109 | ]
1110 | },
1111 | {
1112 | "cell_type": "markdown",
1113 | "metadata": {
1114 | "id": "zee3cqulS5t-",
1115 | "colab_type": "text"
1116 | },
1117 | "source": [
1118 | "**Tuples:**\n",
1119 | "\n",
1120 | "A Tuple is a sequence of immutable Python objects. Tuples are sequences, just like Lists. The differences between tuples and lists are:\n",
1121 | "\n",
1122 | "\n",
1123 | "\n",
1124 | "* Tuples cannot be changed unlike lists\n",
1125 | "* Tuples use parentheses, whereas lists use square brackets."
1126 | ]
1127 | },
1128 | {
1129 | "cell_type": "code",
1130 | "metadata": {
1131 | "id": "QS6W9GjLS9-d",
1132 | "colab_type": "code",
1133 | "colab": {
1134 | "base_uri": "https://localhost:8080/",
1135 | "height": 33
1136 | },
1137 | "outputId": "18a33288-88c0-4af2-edaf-bdddc7f0d384"
1138 | },
1139 | "source": [
1140 | "Datatypes = ('Numeric', 'List', 'Tuples')\n",
1141 | "print (Datatypes)\n"
1142 | ],
1143 | "execution_count": 48,
1144 | "outputs": [
1145 | {
1146 | "output_type": "stream",
1147 | "text": [
1148 | "('Numeric', 'List', 'Tuples')\n"
1149 | ],
1150 | "name": "stdout"
1151 | }
1152 | ]
1153 | },
1154 | {
1155 | "cell_type": "markdown",
1156 | "metadata": {
1157 | "id": "a8lTMliwTZYa",
1158 | "colab_type": "text"
1159 | },
1160 | "source": [
1161 | "**Negative Indexing**\n",
1162 | "\n",
1163 | "Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc."
1164 | ]
1165 | },
1166 | {
1167 | "cell_type": "code",
1168 | "metadata": {
1169 | "id": "IM25puDcTaxu",
1170 | "colab_type": "code",
1171 | "colab": {
1172 | "base_uri": "https://localhost:8080/",
1173 | "height": 33
1174 | },
1175 | "outputId": "88ee09ca-f11a-4147-b55f-9a94d6fff923"
1176 | },
1177 | "source": [
1178 | "thistuple = (\"apple\", \"banana\", \"cherry\")\n",
1179 | "print(thistuple[-1])"
1180 | ],
1181 | "execution_count": 49,
1182 | "outputs": [
1183 | {
1184 | "output_type": "stream",
1185 | "text": [
1186 | "cherry\n"
1187 | ],
1188 | "name": "stdout"
1189 | }
1190 | ]
1191 | },
1192 | {
1193 | "cell_type": "markdown",
1194 | "metadata": {
1195 | "id": "ZSKtUe83TpKF",
1196 | "colab_type": "text"
1197 | },
1198 | "source": [
1199 | "**Range of Indexes**\n",
1200 | "\n",
1201 | "You can specify a range of indexes by specifying where to start and where to end the range.\n",
1202 | "\n",
1203 | "When specifying a range, the return value will be a new tuple with the specified items."
1204 | ]
1205 | },
1206 | {
1207 | "cell_type": "code",
1208 | "metadata": {
1209 | "id": "SkSnMdsCTwyG",
1210 | "colab_type": "code",
1211 | "colab": {
1212 | "base_uri": "https://localhost:8080/",
1213 | "height": 33
1214 | },
1215 | "outputId": "3182f1a0-f4dc-4b0d-d97b-1bf178ef85c1"
1216 | },
1217 | "source": [
1218 | "thistuple = (\"apple\", \"banana\", \"cherry\", \"orange\", \"kiwi\", \"melon\", \"mango\")\n",
1219 | "print(thistuple[2:5])"
1220 | ],
1221 | "execution_count": 51,
1222 | "outputs": [
1223 | {
1224 | "output_type": "stream",
1225 | "text": [
1226 | "('cherry', 'orange', 'kiwi')\n"
1227 | ],
1228 | "name": "stdout"
1229 | }
1230 | ]
1231 | },
1232 | {
1233 | "cell_type": "markdown",
1234 | "metadata": {
1235 | "id": "mwhRYU5UUWDL",
1236 | "colab_type": "text"
1237 | },
1238 | "source": [
1239 | "**Tuple Methods**\n",
1240 | "\n",
1241 | "Python has two built-in methods that you can use on tuples.\n",
1242 | "\n",
1243 | "* count()\t: Returns the number of times a specified value occurs in a tuple\n",
1244 | "* index()\t: Searches the tuple for a specified value and returns the position of where it was found"
1245 | ]
1246 | },
1247 | {
1248 | "cell_type": "markdown",
1249 | "metadata": {
1250 | "id": "NcdqnKdRUt-0",
1251 | "colab_type": "text"
1252 | },
1253 | "source": [
1254 | "**Set:**\n",
1255 | "\n",
1256 | "A Set is an unordered collection of items. Every element is unique.\n",
1257 | "A Set is created by placing all the items (elements) inside curly braces {}, separated by comma. "
1258 | ]
1259 | },
1260 | {
1261 | "cell_type": "code",
1262 | "metadata": {
1263 | "id": "z9PH4XAXUxS_",
1264 | "colab_type": "code",
1265 | "colab": {
1266 | "base_uri": "https://localhost:8080/",
1267 | "height": 100
1268 | },
1269 | "outputId": "7e5b1ec8-bf20-4e60-8c8a-daa685d06f07"
1270 | },
1271 | "source": [
1272 | "Set1 = {1, 2, 3}\n",
1273 | "print (Set1)\n",
1274 | "Set2 = {4, 5, 5, 6}\n",
1275 | "print (Set2)\n",
1276 | "\n",
1277 | "#set opreations\n",
1278 | "\n",
1279 | "A = {1, 2, 3, 4}\n",
1280 | "B = {3, 4, 5, 6}\n",
1281 | "print ( A | B) #union\n",
1282 | "print ( A & B ) #intersection\n",
1283 | "print(A - B) #difference"
1284 | ],
1285 | "execution_count": 52,
1286 | "outputs": [
1287 | {
1288 | "output_type": "stream",
1289 | "text": [
1290 | "{1, 2, 3}\n",
1291 | "{4, 5, 6}\n",
1292 | "{1, 2, 3, 4, 5, 6}\n",
1293 | "{3, 4}\n",
1294 | "{1, 2}\n"
1295 | ],
1296 | "name": "stdout"
1297 | }
1298 | ]
1299 | },
1300 | {
1301 | "cell_type": "markdown",
1302 | "metadata": {
1303 | "id": "BG33LT1jWVxD",
1304 | "colab_type": "text"
1305 | },
1306 | "source": [
1307 | "**Dictionary Methods**\n",
1308 | "\n",
1309 | "Python has a set of built-in methods that you can use on dictionaries.\n",
1310 | "\n",
1311 | "* clear() :\tRemoves all the elements from the dictionary\n",
1312 | "* copy()\t: Returns a copy of the dictionary\n",
1313 | "* get()\t: Returns the value of the specified key\n",
1314 | "* items()\t: Returns a list containing a tuple for each key value pair\n",
1315 | "* keys()\t: Returns a list containing the dictionary's keys\n",
1316 | "* pop()\t: Removes the element with the specified key\n",
1317 | "* popitem() :\tRemoves the last inserted key-value pair\n",
1318 | "* update() :\tUpdates the dictionary with the specified key-value pairs\n",
1319 | "* values() :\tReturns a list of all the values in the dictionary"
1320 | ]
1321 | },
1322 | {
1323 | "cell_type": "code",
1324 | "metadata": {
1325 | "id": "XBVUOI6mWbW5",
1326 | "colab_type": "code",
1327 | "colab": {
1328 | "base_uri": "https://localhost:8080/",
1329 | "height": 33
1330 | },
1331 | "outputId": "06cb6730-389c-4013-83e6-0058c5c6f312"
1332 | },
1333 | "source": [
1334 | "family = {\n",
1335 | " \"child1\" : {\n",
1336 | " \"name\" : \"Walter White\",\n",
1337 | " \"year\" : 2004\n",
1338 | " },\n",
1339 | " \"child2\" : {\n",
1340 | " \"name\" : \"Jesse Pinkman\",\n",
1341 | " \"year\" : 2007\n",
1342 | " },\n",
1343 | " \"child3\" : {\n",
1344 | " \"name\" : \"Harvey Specter\",\n",
1345 | " \"year\" : 2011\n",
1346 | " }\n",
1347 | "}\n",
1348 | "print (family)\n",
1349 | "\n"
1350 | ],
1351 | "execution_count": 54,
1352 | "outputs": [
1353 | {
1354 | "output_type": "stream",
1355 | "text": [
1356 | "{'child1': {'name': 'Walter White', 'year': 2004}, 'child2': {'name': 'Jesse Pinkman', 'year': 2007}, 'child3': {'name': 'Harvey Specter', 'year': 2011}}\n"
1357 | ],
1358 | "name": "stdout"
1359 | }
1360 | ]
1361 | },
1362 | {
1363 | "cell_type": "markdown",
1364 | "metadata": {
1365 | "id": "tvslFLCWZ1bH",
1366 | "colab_type": "text"
1367 | },
1368 | "source": [
1369 | "Operators in Python:\n",
1370 | "\n",
1371 | "Operators are the constructs which can manipulate the values of the operands. Consider the expression 2 + 3 = 5, here 2 and 3 are operands and + is called operator.\n",
1372 | "\n",
1373 | "*Types of Operators in Python: *\n",
1374 | "\n",
1375 | "Arithmetic\n",
1376 | "\n",
1377 | "Comparision\n",
1378 | "\n",
1379 | "Logical\n",
1380 | "\n",
1381 | "Assignment\n",
1382 | "\n",
1383 | "Bitwise"
1384 | ]
1385 | },
1386 | {
1387 | "cell_type": "markdown",
1388 | "metadata": {
1389 | "id": "RIeUyG_4Z__-",
1390 | "colab_type": "text"
1391 | },
1392 | "source": [
1393 | "Arithmetic Operation:\n",
1394 | "\n",
1395 | "These Operators are used to perform mathematical operations like addition, subtraction etc.\n",
1396 | "\n"
1397 | ]
1398 | },
1399 | {
1400 | "cell_type": "code",
1401 | "metadata": {
1402 | "id": "cSkXbDXiaCJ2",
1403 | "colab_type": "code",
1404 | "colab": {
1405 | "base_uri": "https://localhost:8080/",
1406 | "height": 117
1407 | },
1408 | "outputId": "998ea758-61e7-4381-a8e9-4664cf4123ff"
1409 | },
1410 | "source": [
1411 | "a = 11\n",
1412 | "b = 20\n",
1413 | "c = 0\n",
1414 | " \n",
1415 | "c = a + b\n",
1416 | "print ( c )\n",
1417 | " \n",
1418 | "c = a - b\n",
1419 | "print ( c )\n",
1420 | " \n",
1421 | "c = a * b\n",
1422 | "print ( c )\n",
1423 | " \n",
1424 | "c = a / b\n",
1425 | "print ( c )\n",
1426 | " \n",
1427 | "c = b % a\n",
1428 | "print ( c )\n",
1429 | "\n",
1430 | "a = 2\n",
1431 | "b = 3\n",
1432 | "c = a ** b\n",
1433 | "print ( c )"
1434 | ],
1435 | "execution_count": 55,
1436 | "outputs": [
1437 | {
1438 | "output_type": "stream",
1439 | "text": [
1440 | "31\n",
1441 | "-9\n",
1442 | "220\n",
1443 | "0.55\n",
1444 | "9\n",
1445 | "8\n"
1446 | ],
1447 | "name": "stdout"
1448 | }
1449 | ]
1450 | },
1451 | {
1452 | "cell_type": "markdown",
1453 | "metadata": {
1454 | "id": "UiO2-g1laJrO",
1455 | "colab_type": "text"
1456 | },
1457 | "source": [
1458 | "Comparision Operation:\n",
1459 | "\n",
1460 | "These Operators compare the values on either sides of them and decide the relation among them."
1461 | ]
1462 | },
1463 | {
1464 | "cell_type": "code",
1465 | "metadata": {
1466 | "id": "_RKwhzvWaNRu",
1467 | "colab_type": "code",
1468 | "colab": {
1469 | "base_uri": "https://localhost:8080/",
1470 | "height": 117
1471 | },
1472 | "outputId": "fa210382-cc30-4838-f666-c336759b41b8"
1473 | },
1474 | "source": [
1475 | "a = 11\n",
1476 | "b = 20\n",
1477 | "result = (a == b)\n",
1478 | "\n",
1479 | "print(result)\n",
1480 | "\n",
1481 | "result = (a != b)\n",
1482 | "\n",
1483 | "print(result)\n",
1484 | "\n",
1485 | "result = ( a < b )\n",
1486 | "\n",
1487 | "print(result)\n",
1488 | " \n",
1489 | "result = ( a > b )\n",
1490 | "\n",
1491 | "print(result)\n",
1492 | "\n",
1493 | "result = ( a <= b )\n",
1494 | "\n",
1495 | "print(result)\n",
1496 | "\n",
1497 | "type(result)\n"
1498 | ],
1499 | "execution_count": 64,
1500 | "outputs": [
1501 | {
1502 | "output_type": "stream",
1503 | "text": [
1504 | "False\n",
1505 | "True\n",
1506 | "True\n",
1507 | "False\n",
1508 | "True\n"
1509 | ],
1510 | "name": "stdout"
1511 | },
1512 | {
1513 | "output_type": "execute_result",
1514 | "data": {
1515 | "text/plain": [
1516 | "bool"
1517 | ]
1518 | },
1519 | "metadata": {
1520 | "tags": []
1521 | },
1522 | "execution_count": 64
1523 | }
1524 | ]
1525 | },
1526 | {
1527 | "cell_type": "code",
1528 | "metadata": {
1529 | "id": "oXcDjDAAsHsh",
1530 | "colab_type": "code",
1531 | "colab": {
1532 | "base_uri": "https://localhost:8080/",
1533 | "height": 33
1534 | },
1535 | "outputId": "d668fdc3-70c9-442c-de2e-2fcb30f83fde"
1536 | },
1537 | "source": [
1538 | "result = ( a >= b)\n",
1539 | "result"
1540 | ],
1541 | "execution_count": 66,
1542 | "outputs": [
1543 | {
1544 | "output_type": "execute_result",
1545 | "data": {
1546 | "text/plain": [
1547 | "False"
1548 | ]
1549 | },
1550 | "metadata": {
1551 | "tags": []
1552 | },
1553 | "execution_count": 66
1554 | }
1555 | ]
1556 | },
1557 | {
1558 | "cell_type": "markdown",
1559 | "metadata": {
1560 | "id": "tTilkmuJYRLR",
1561 | "colab_type": "text"
1562 | },
1563 | "source": [
1564 | "# Logical Operators\n",
1565 | "* ( and ) to check if the LHS and RHS conditions both conditions are satisfied. Basically checking for truthy values\n",
1566 | "* ( or ) to check if atleast one of the side satisfy the condition. Basically checking for truthy values\n",
1567 | "* ( not ) to check if the condition is not satified. Basically checking for falsy values"
1568 | ]
1569 | },
1570 | {
1571 | "cell_type": "code",
1572 | "metadata": {
1573 | "id": "yktGQJDnaVtv",
1574 | "colab_type": "code",
1575 | "colab": {
1576 | "base_uri": "https://localhost:8080/",
1577 | "height": 67
1578 | },
1579 | "outputId": "5d185f5f-4faa-4fc8-9902-b131e01b946b"
1580 | },
1581 | "source": [
1582 | "x = True\n",
1583 | "y = False\n",
1584 | " \n",
1585 | "print('x and y is',x and y)\n",
1586 | " \n",
1587 | "print('x or y is',x or y)\n",
1588 | " \n",
1589 | "print('not x is',not x)"
1590 | ],
1591 | "execution_count": 67,
1592 | "outputs": [
1593 | {
1594 | "output_type": "stream",
1595 | "text": [
1596 | "x and y is False\n",
1597 | "x or y is True\n",
1598 | "not x is False\n"
1599 | ],
1600 | "name": "stdout"
1601 | }
1602 | ]
1603 | },
1604 | {
1605 | "cell_type": "markdown",
1606 | "metadata": {
1607 | "id": "1E1lQlM6YZEn",
1608 | "colab_type": "text"
1609 | },
1610 | "source": [
1611 | "# **Input Output:**\n",
1612 | "Take input from the user and work with it"
1613 | ]
1614 | },
1615 | {
1616 | "cell_type": "code",
1617 | "metadata": {
1618 | "id": "XNUhYjMwegiX",
1619 | "colab_type": "code",
1620 | "colab": {
1621 | "base_uri": "https://localhost:8080/",
1622 | "height": 70
1623 | },
1624 | "outputId": "5df88946-3872-41b1-9392-9d58b79be435"
1625 | },
1626 | "source": [
1627 | "my_age = input(\"Enter your age: \")\n",
1628 | "\n",
1629 | "add_age = my_age + 2 #It will throw an error\n",
1630 | "\n",
1631 | "#lets check why?\n",
1632 | "type(my_age)\n",
1633 | "\n",
1634 | "#Lets fix this;\n",
1635 | "\n"
1636 | ],
1637 | "execution_count": 25,
1638 | "outputs": [
1639 | {
1640 | "output_type": "stream",
1641 | "text": [
1642 | "Enter your age: 10\n"
1643 | ],
1644 | "name": "stdout"
1645 | },
1646 | {
1647 | "output_type": "execute_result",
1648 | "data": {
1649 | "text/plain": [
1650 | "str"
1651 | ]
1652 | },
1653 | "metadata": {
1654 | "tags": []
1655 | },
1656 | "execution_count": 25
1657 | }
1658 | ]
1659 | },
1660 | {
1661 | "cell_type": "markdown",
1662 | "metadata": {
1663 | "id": "astaY2wWuCZM",
1664 | "colab_type": "text"
1665 | },
1666 | "source": [
1667 | "Bitwise Operators"
1668 | ]
1669 | },
1670 | {
1671 | "cell_type": "code",
1672 | "metadata": {
1673 | "id": "-kFgNadkuB_S",
1674 | "colab_type": "code",
1675 | "colab": {
1676 | "base_uri": "https://localhost:8080/",
1677 | "height": 67
1678 | },
1679 | "outputId": "ff494818-315e-4a37-ca91-25fc5a684ec3"
1680 | },
1681 | "source": [
1682 | "a = 1\n",
1683 | "b = 2\n",
1684 | "\n",
1685 | "#0001\n",
1686 | "#0010\n",
1687 | "\n",
1688 | "# OR Operator |\n",
1689 | "\n",
1690 | "print(a | b)\n",
1691 | "# 0011\n",
1692 | "\n",
1693 | "# and operator &\n",
1694 | "print(a & b)\n",
1695 | "#0000\n",
1696 | "\n",
1697 | "# XOR operator\n",
1698 | "print(a ^ b)"
1699 | ],
1700 | "execution_count": 68,
1701 | "outputs": [
1702 | {
1703 | "output_type": "stream",
1704 | "text": [
1705 | "3\n",
1706 | "0\n",
1707 | "3\n"
1708 | ],
1709 | "name": "stdout"
1710 | }
1711 | ]
1712 | },
1713 | {
1714 | "cell_type": "markdown",
1715 | "metadata": {
1716 | "id": "ycdHU0kBvVxY",
1717 | "colab_type": "text"
1718 | },
1719 | "source": [
1720 | "Assignment operators"
1721 | ]
1722 | },
1723 | {
1724 | "cell_type": "code",
1725 | "metadata": {
1726 | "id": "S9NKUi70vYWm",
1727 | "colab_type": "code",
1728 | "colab": {}
1729 | },
1730 | "source": [
1731 | "# =\n",
1732 | "c = 1\n",
1733 | "a = 2\n",
1734 | "b = 3\n",
1735 | "#c = a + b\n",
1736 | "#c = c + a + b\n",
1737 | "\n",
1738 | "# c += a\n",
1739 | "# print(c)\n",
1740 | "\n",
1741 | "# -= subtract your value and assign it\n",
1742 | "\n",
1743 | "# c -= a\n",
1744 | "# c - a\n",
1745 | "# c = c - a\n",
1746 | "# print(c)\n",
1747 | "\n",
1748 | "# *= multiplication assignment operator\n",
1749 | "# c *= a\n",
1750 | "# print(c)\n",
1751 | "\n",
1752 | "# /= division assignment operator\n",
1753 | "# c /= a\n",
1754 | "# print(c)\n",
1755 | "\n",
1756 | "# //= floor division assignment operator\n",
1757 | "\n",
1758 | "# a //= c\n",
1759 | "# c / a\n",
1760 | " # 0.5 floor\n",
1761 | " # 0\n",
1762 | "# c = 0\n",
1763 | "# print(c)\n",
1764 | "\n",
1765 | "# **= power assignment operator\n",
1766 | "\n",
1767 | "# a **= b\n",
1768 | "# print(a)"
1769 | ],
1770 | "execution_count": 69,
1771 | "outputs": []
1772 | },
1773 | {
1774 | "cell_type": "markdown",
1775 | "metadata": {
1776 | "id": "tRGAaBYocSF8",
1777 | "colab_type": "text"
1778 | },
1779 | "source": [
1780 | "# HW\n",
1781 | "\n",
1782 | "WAP to create a basic data collection system using python where: \n",
1783 | "* It takes username, password and age from input.\n",
1784 | "* It must store atleast 2 user information taken from input.\n",
1785 | "* After that, it should display the name, total age and avg.\n",
1786 | "* Atleast 1 of the datastructures should be used.\n",
1787 | "* You are allowed to use more than one code block\n",
1788 | "\n"
1789 | ]
1790 | },
1791 | {
1792 | "cell_type": "code",
1793 | "metadata": {
1794 | "id": "a8jl4juoy-2o",
1795 | "colab_type": "code",
1796 | "colab": {}
1797 | },
1798 | "source": [
1799 | "#Sample Output:\n",
1800 | "\n",
1801 | "# enter details for user1\n",
1802 | "# Name: Ali\n",
1803 | "# Password: admin\n",
1804 | "# Age: 23\n",
1805 | "\n",
1806 | "# enter details for user2\n",
1807 | "# Name: Ram\n",
1808 | "# Password: admin\n",
1809 | "# Age: 25\n",
1810 | "\n",
1811 | "# Users: Ali and Ram\n",
1812 | "#Total Age = 48\n",
1813 | "#Avg Age = 24\n",
1814 | "\n"
1815 | ],
1816 | "execution_count": 21,
1817 | "outputs": []
1818 | },
1819 | {
1820 | "cell_type": "code",
1821 | "metadata": {
1822 | "id": "jjuPYlcqzPiz",
1823 | "colab_type": "code",
1824 | "colab": {}
1825 | },
1826 | "source": [
1827 | ""
1828 | ],
1829 | "execution_count": null,
1830 | "outputs": []
1831 | }
1832 | ]
1833 | }
--------------------------------------------------------------------------------
/Python Crash Course.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Python Crash Course \n",
8 | "\n",
9 | "## Topics : \n",
10 | "\n",
11 | "* Data Types \n",
12 | " * numbers - float, int\n",
13 | " * String and its method\n",
14 | " * Boolean\n",
15 | " \n",
16 | "* Data Structure \n",
17 | " * List - array of python\n",
18 | " * Dictionary\n",
19 | " * Tuples\n",
20 | " * Sets\n",
21 | "\n",
22 | "* Loops - For While\n",
23 | "* Decision making = if, elseif\n",
24 | "* function\n",
25 | "* Lambda\n",
26 | "* map\n",
27 | "* Filter\n"
28 | ]
29 | },
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "collapsed": true
34 | },
35 | "source": [
36 | "## Numbers"
37 | ]
38 | },
39 | {
40 | "cell_type": "code",
41 | "execution_count": 2,
42 | "metadata": {},
43 | "outputs": [
44 | {
45 | "data": {
46 | "text/plain": [
47 | "3"
48 | ]
49 | },
50 | "execution_count": 2,
51 | "metadata": {},
52 | "output_type": "execute_result"
53 | }
54 | ],
55 | "source": [
56 | "3"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": 3,
62 | "metadata": {},
63 | "outputs": [
64 | {
65 | "data": {
66 | "text/plain": [
67 | "4.5"
68 | ]
69 | },
70 | "execution_count": 3,
71 | "metadata": {},
72 | "output_type": "execute_result"
73 | }
74 | ],
75 | "source": [
76 | "4.5"
77 | ]
78 | },
79 | {
80 | "cell_type": "code",
81 | "execution_count": 4,
82 | "metadata": {},
83 | "outputs": [
84 | {
85 | "data": {
86 | "text/plain": [
87 | "9"
88 | ]
89 | },
90 | "execution_count": 4,
91 | "metadata": {},
92 | "output_type": "execute_result"
93 | }
94 | ],
95 | "source": [
96 | "3 + 6"
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": 5,
102 | "metadata": {},
103 | "outputs": [
104 | {
105 | "data": {
106 | "text/plain": [
107 | "17.0"
108 | ]
109 | },
110 | "execution_count": 5,
111 | "metadata": {},
112 | "output_type": "execute_result"
113 | }
114 | ],
115 | "source": [
116 | "3.4 * 5"
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": 6,
122 | "metadata": {},
123 | "outputs": [
124 | {
125 | "data": {
126 | "text/plain": [
127 | "0.5"
128 | ]
129 | },
130 | "execution_count": 6,
131 | "metadata": {},
132 | "output_type": "execute_result"
133 | }
134 | ],
135 | "source": [
136 | "2/4"
137 | ]
138 | },
139 | {
140 | "cell_type": "code",
141 | "execution_count": null,
142 | "metadata": {
143 | "collapsed": true
144 | },
145 | "outputs": [],
146 | "source": []
147 | },
148 | {
149 | "cell_type": "code",
150 | "execution_count": null,
151 | "metadata": {
152 | "collapsed": true
153 | },
154 | "outputs": [],
155 | "source": []
156 | },
157 | {
158 | "cell_type": "code",
159 | "execution_count": null,
160 | "metadata": {
161 | "collapsed": true
162 | },
163 | "outputs": [],
164 | "source": []
165 | },
166 | {
167 | "cell_type": "code",
168 | "execution_count": null,
169 | "metadata": {
170 | "collapsed": true
171 | },
172 | "outputs": [],
173 | "source": []
174 | },
175 | {
176 | "cell_type": "code",
177 | "execution_count": null,
178 | "metadata": {
179 | "collapsed": true
180 | },
181 | "outputs": [],
182 | "source": []
183 | },
184 | {
185 | "cell_type": "code",
186 | "execution_count": 7,
187 | "metadata": {},
188 | "outputs": [
189 | {
190 | "data": {
191 | "text/plain": [
192 | "33"
193 | ]
194 | },
195 | "execution_count": 7,
196 | "metadata": {},
197 | "output_type": "execute_result"
198 | }
199 | ],
200 | "source": [
201 | "3 + 6*5"
202 | ]
203 | },
204 | {
205 | "cell_type": "markdown",
206 | "metadata": {},
207 | "source": [
208 | "# String"
209 | ]
210 | },
211 | {
212 | "cell_type": "code",
213 | "execution_count": 8,
214 | "metadata": {},
215 | "outputs": [
216 | {
217 | "data": {
218 | "text/plain": [
219 | "'Hello World'"
220 | ]
221 | },
222 | "execution_count": 8,
223 | "metadata": {},
224 | "output_type": "execute_result"
225 | }
226 | ],
227 | "source": [
228 | "'Hello World'"
229 | ]
230 | },
231 | {
232 | "cell_type": "code",
233 | "execution_count": 9,
234 | "metadata": {},
235 | "outputs": [
236 | {
237 | "data": {
238 | "text/plain": [
239 | "'Hello World'"
240 | ]
241 | },
242 | "execution_count": 9,
243 | "metadata": {},
244 | "output_type": "execute_result"
245 | }
246 | ],
247 | "source": [
248 | "\"Hello World\""
249 | ]
250 | },
251 | {
252 | "cell_type": "code",
253 | "execution_count": 10,
254 | "metadata": {},
255 | "outputs": [
256 | {
257 | "data": {
258 | "text/plain": [
259 | "\"I can't\""
260 | ]
261 | },
262 | "execution_count": 10,
263 | "metadata": {},
264 | "output_type": "execute_result"
265 | }
266 | ],
267 | "source": [
268 | "\"I can't\""
269 | ]
270 | },
271 | {
272 | "cell_type": "markdown",
273 | "metadata": {},
274 | "source": [
275 | "# Variable"
276 | ]
277 | },
278 | {
279 | "cell_type": "code",
280 | "execution_count": 11,
281 | "metadata": {
282 | "collapsed": true
283 | },
284 | "outputs": [],
285 | "source": [
286 | "a = 3"
287 | ]
288 | },
289 | {
290 | "cell_type": "code",
291 | "execution_count": 12,
292 | "metadata": {},
293 | "outputs": [
294 | {
295 | "data": {
296 | "text/plain": [
297 | "3"
298 | ]
299 | },
300 | "execution_count": 12,
301 | "metadata": {},
302 | "output_type": "execute_result"
303 | }
304 | ],
305 | "source": [
306 | "a"
307 | ]
308 | },
309 | {
310 | "cell_type": "code",
311 | "execution_count": 13,
312 | "metadata": {
313 | "collapsed": true
314 | },
315 | "outputs": [],
316 | "source": [
317 | "x = \"Helo World\""
318 | ]
319 | },
320 | {
321 | "cell_type": "code",
322 | "execution_count": 14,
323 | "metadata": {},
324 | "outputs": [
325 | {
326 | "data": {
327 | "text/plain": [
328 | "'Helo World'"
329 | ]
330 | },
331 | "execution_count": 14,
332 | "metadata": {},
333 | "output_type": "execute_result"
334 | }
335 | ],
336 | "source": [
337 | "x"
338 | ]
339 | },
340 | {
341 | "cell_type": "code",
342 | "execution_count": 15,
343 | "metadata": {},
344 | "outputs": [
345 | {
346 | "data": {
347 | "text/plain": [
348 | "int"
349 | ]
350 | },
351 | "execution_count": 15,
352 | "metadata": {},
353 | "output_type": "execute_result"
354 | }
355 | ],
356 | "source": [
357 | "type(a)"
358 | ]
359 | },
360 | {
361 | "cell_type": "code",
362 | "execution_count": 16,
363 | "metadata": {},
364 | "outputs": [
365 | {
366 | "data": {
367 | "text/plain": [
368 | "str"
369 | ]
370 | },
371 | "execution_count": 16,
372 | "metadata": {},
373 | "output_type": "execute_result"
374 | }
375 | ],
376 | "source": [
377 | "type(x)"
378 | ]
379 | },
380 | {
381 | "cell_type": "code",
382 | "execution_count": 18,
383 | "metadata": {
384 | "collapsed": true
385 | },
386 | "outputs": [],
387 | "source": [
388 | "x12 = 23"
389 | ]
390 | },
391 | {
392 | "cell_type": "code",
393 | "execution_count": 19,
394 | "metadata": {},
395 | "outputs": [
396 | {
397 | "data": {
398 | "text/plain": [
399 | "23"
400 | ]
401 | },
402 | "execution_count": 19,
403 | "metadata": {},
404 | "output_type": "execute_result"
405 | }
406 | ],
407 | "source": [
408 | "x12"
409 | ]
410 | },
411 | {
412 | "cell_type": "markdown",
413 | "metadata": {},
414 | "source": [
415 | "# Operation on Strings"
416 | ]
417 | },
418 | {
419 | "cell_type": "code",
420 | "execution_count": 20,
421 | "metadata": {},
422 | "outputs": [
423 | {
424 | "data": {
425 | "text/plain": [
426 | "'Helo World'"
427 | ]
428 | },
429 | "execution_count": 20,
430 | "metadata": {},
431 | "output_type": "execute_result"
432 | }
433 | ],
434 | "source": [
435 | "x"
436 | ]
437 | },
438 | {
439 | "cell_type": "code",
440 | "execution_count": 21,
441 | "metadata": {},
442 | "outputs": [
443 | {
444 | "data": {
445 | "text/plain": [
446 | "3"
447 | ]
448 | },
449 | "execution_count": 21,
450 | "metadata": {},
451 | "output_type": "execute_result"
452 | }
453 | ],
454 | "source": [
455 | "a"
456 | ]
457 | },
458 | {
459 | "cell_type": "code",
460 | "execution_count": 23,
461 | "metadata": {},
462 | "outputs": [
463 | {
464 | "data": {
465 | "text/plain": [
466 | "'HELO WORLD'"
467 | ]
468 | },
469 | "execution_count": 23,
470 | "metadata": {},
471 | "output_type": "execute_result"
472 | }
473 | ],
474 | "source": [
475 | "x.upper()"
476 | ]
477 | },
478 | {
479 | "cell_type": "code",
480 | "execution_count": 24,
481 | "metadata": {},
482 | "outputs": [
483 | {
484 | "data": {
485 | "text/plain": [
486 | "'helo world'"
487 | ]
488 | },
489 | "execution_count": 24,
490 | "metadata": {},
491 | "output_type": "execute_result"
492 | }
493 | ],
494 | "source": [
495 | "x.lower()"
496 | ]
497 | },
498 | {
499 | "cell_type": "code",
500 | "execution_count": 27,
501 | "metadata": {},
502 | "outputs": [
503 | {
504 | "data": {
505 | "text/plain": [
506 | "False"
507 | ]
508 | },
509 | "execution_count": 27,
510 | "metadata": {},
511 | "output_type": "execute_result"
512 | }
513 | ],
514 | "source": [
515 | "x.isdigit()"
516 | ]
517 | },
518 | {
519 | "cell_type": "code",
520 | "execution_count": 28,
521 | "metadata": {},
522 | "outputs": [
523 | {
524 | "data": {
525 | "text/plain": [
526 | "'Helo World Bye'"
527 | ]
528 | },
529 | "execution_count": 28,
530 | "metadata": {},
531 | "output_type": "execute_result"
532 | }
533 | ],
534 | "source": [
535 | "x + \" Bye\""
536 | ]
537 | },
538 | {
539 | "cell_type": "markdown",
540 | "metadata": {},
541 | "source": [
542 | "# Slicing in String"
543 | ]
544 | },
545 | {
546 | "cell_type": "code",
547 | "execution_count": 29,
548 | "metadata": {},
549 | "outputs": [
550 | {
551 | "data": {
552 | "text/plain": [
553 | "'Helo World'"
554 | ]
555 | },
556 | "execution_count": 29,
557 | "metadata": {},
558 | "output_type": "execute_result"
559 | }
560 | ],
561 | "source": [
562 | "x"
563 | ]
564 | },
565 | {
566 | "cell_type": "code",
567 | "execution_count": 30,
568 | "metadata": {},
569 | "outputs": [
570 | {
571 | "data": {
572 | "text/plain": [
573 | "'H'"
574 | ]
575 | },
576 | "execution_count": 30,
577 | "metadata": {},
578 | "output_type": "execute_result"
579 | }
580 | ],
581 | "source": [
582 | "x[0]"
583 | ]
584 | },
585 | {
586 | "cell_type": "code",
587 | "execution_count": 31,
588 | "metadata": {},
589 | "outputs": [
590 | {
591 | "data": {
592 | "text/plain": [
593 | "'W'"
594 | ]
595 | },
596 | "execution_count": 31,
597 | "metadata": {},
598 | "output_type": "execute_result"
599 | }
600 | ],
601 | "source": [
602 | "x[5]"
603 | ]
604 | },
605 | {
606 | "cell_type": "code",
607 | "execution_count": 34,
608 | "metadata": {},
609 | "outputs": [
610 | {
611 | "data": {
612 | "text/plain": [
613 | "'o World'"
614 | ]
615 | },
616 | "execution_count": 34,
617 | "metadata": {},
618 | "output_type": "execute_result"
619 | }
620 | ],
621 | "source": [
622 | "x[3:]"
623 | ]
624 | },
625 | {
626 | "cell_type": "markdown",
627 | "metadata": {},
628 | "source": [
629 | "# List, Tuples, Dictionary, Set"
630 | ]
631 | },
632 | {
633 | "cell_type": "code",
634 | "execution_count": 35,
635 | "metadata": {},
636 | "outputs": [
637 | {
638 | "data": {
639 | "text/plain": [
640 | "[3, 5, 8]"
641 | ]
642 | },
643 | "execution_count": 35,
644 | "metadata": {},
645 | "output_type": "execute_result"
646 | }
647 | ],
648 | "source": [
649 | "[3,5,8]"
650 | ]
651 | },
652 | {
653 | "cell_type": "code",
654 | "execution_count": 39,
655 | "metadata": {
656 | "collapsed": true
657 | },
658 | "outputs": [],
659 | "source": [
660 | "lst = [\"Hello\",3 , 5, [2, 6]]"
661 | ]
662 | },
663 | {
664 | "cell_type": "code",
665 | "execution_count": 40,
666 | "metadata": {},
667 | "outputs": [
668 | {
669 | "data": {
670 | "text/plain": [
671 | "['Hello', 3, 5, [2, 6]]"
672 | ]
673 | },
674 | "execution_count": 40,
675 | "metadata": {},
676 | "output_type": "execute_result"
677 | }
678 | ],
679 | "source": [
680 | "lst"
681 | ]
682 | },
683 | {
684 | "cell_type": "code",
685 | "execution_count": 41,
686 | "metadata": {
687 | "collapsed": true
688 | },
689 | "outputs": [],
690 | "source": [
691 | "# Hello > World\n",
692 | "lst[0] = \"World\""
693 | ]
694 | },
695 | {
696 | "cell_type": "code",
697 | "execution_count": 42,
698 | "metadata": {},
699 | "outputs": [
700 | {
701 | "data": {
702 | "text/plain": [
703 | "['World', 3, 5, [2, 6]]"
704 | ]
705 | },
706 | "execution_count": 42,
707 | "metadata": {},
708 | "output_type": "execute_result"
709 | }
710 | ],
711 | "source": [
712 | "lst"
713 | ]
714 | },
715 | {
716 | "cell_type": "code",
717 | "execution_count": 43,
718 | "metadata": {
719 | "collapsed": true
720 | },
721 | "outputs": [],
722 | "source": [
723 | "lst.append(\"Bye\")"
724 | ]
725 | },
726 | {
727 | "cell_type": "code",
728 | "execution_count": 44,
729 | "metadata": {},
730 | "outputs": [
731 | {
732 | "data": {
733 | "text/plain": [
734 | "['World', 3, 5, [2, 6], 'Bye']"
735 | ]
736 | },
737 | "execution_count": 44,
738 | "metadata": {},
739 | "output_type": "execute_result"
740 | }
741 | ],
742 | "source": [
743 | "lst"
744 | ]
745 | },
746 | {
747 | "cell_type": "code",
748 | "execution_count": 45,
749 | "metadata": {},
750 | "outputs": [
751 | {
752 | "data": {
753 | "text/plain": [
754 | "5"
755 | ]
756 | },
757 | "execution_count": 45,
758 | "metadata": {},
759 | "output_type": "execute_result"
760 | }
761 | ],
762 | "source": [
763 | "lst[2]"
764 | ]
765 | },
766 | {
767 | "cell_type": "code",
768 | "execution_count": 47,
769 | "metadata": {},
770 | "outputs": [
771 | {
772 | "data": {
773 | "text/plain": [
774 | "6"
775 | ]
776 | },
777 | "execution_count": 47,
778 | "metadata": {},
779 | "output_type": "execute_result"
780 | }
781 | ],
782 | "source": [
783 | "lst[3][1]"
784 | ]
785 | },
786 | {
787 | "cell_type": "markdown",
788 | "metadata": {},
789 | "source": [
790 | "# Tuples"
791 | ]
792 | },
793 | {
794 | "cell_type": "code",
795 | "execution_count": 48,
796 | "metadata": {
797 | "collapsed": true
798 | },
799 | "outputs": [],
800 | "source": [
801 | "#Immutable , Can't modify'"
802 | ]
803 | },
804 | {
805 | "cell_type": "code",
806 | "execution_count": 49,
807 | "metadata": {
808 | "collapsed": true
809 | },
810 | "outputs": [],
811 | "source": [
812 | "tup = (2, 4, 6)"
813 | ]
814 | },
815 | {
816 | "cell_type": "code",
817 | "execution_count": 50,
818 | "metadata": {},
819 | "outputs": [
820 | {
821 | "data": {
822 | "text/plain": [
823 | "(2, 4, 6)"
824 | ]
825 | },
826 | "execution_count": 50,
827 | "metadata": {},
828 | "output_type": "execute_result"
829 | }
830 | ],
831 | "source": [
832 | "tup"
833 | ]
834 | },
835 | {
836 | "cell_type": "code",
837 | "execution_count": 51,
838 | "metadata": {},
839 | "outputs": [
840 | {
841 | "data": {
842 | "text/plain": [
843 | "tuple"
844 | ]
845 | },
846 | "execution_count": 51,
847 | "metadata": {},
848 | "output_type": "execute_result"
849 | }
850 | ],
851 | "source": [
852 | "type(tup)"
853 | ]
854 | },
855 | {
856 | "cell_type": "code",
857 | "execution_count": 52,
858 | "metadata": {},
859 | "outputs": [
860 | {
861 | "ename": "TypeError",
862 | "evalue": "'tuple' object does not support item assignment",
863 | "output_type": "error",
864 | "traceback": [
865 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
866 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
867 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtup\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
868 | "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
869 | ]
870 | }
871 | ],
872 | "source": [
873 | "tup[0] = 5"
874 | ]
875 | },
876 | {
877 | "cell_type": "code",
878 | "execution_count": 54,
879 | "metadata": {},
880 | "outputs": [
881 | {
882 | "data": {
883 | "text/plain": [
884 | "6"
885 | ]
886 | },
887 | "execution_count": 54,
888 | "metadata": {},
889 | "output_type": "execute_result"
890 | }
891 | ],
892 | "source": [
893 | "tup[2]"
894 | ]
895 | },
896 | {
897 | "cell_type": "markdown",
898 | "metadata": {},
899 | "source": [
900 | "# Dictionary"
901 | ]
902 | },
903 | {
904 | "cell_type": "code",
905 | "execution_count": 55,
906 | "metadata": {
907 | "collapsed": true
908 | },
909 | "outputs": [],
910 | "source": [
911 | "#0,1,2,3,4,5,6, \"Key1\""
912 | ]
913 | },
914 | {
915 | "cell_type": "code",
916 | "execution_count": 67,
917 | "metadata": {
918 | "collapsed": true
919 | },
920 | "outputs": [],
921 | "source": [
922 | "dic = {'key1':'val1', 'key2':'val2'}"
923 | ]
924 | },
925 | {
926 | "cell_type": "code",
927 | "execution_count": 68,
928 | "metadata": {},
929 | "outputs": [
930 | {
931 | "data": {
932 | "text/plain": [
933 | "{'key1': 'val1', 'key2': 'val2'}"
934 | ]
935 | },
936 | "execution_count": 68,
937 | "metadata": {},
938 | "output_type": "execute_result"
939 | }
940 | ],
941 | "source": [
942 | "dic"
943 | ]
944 | },
945 | {
946 | "cell_type": "code",
947 | "execution_count": 69,
948 | "metadata": {},
949 | "outputs": [
950 | {
951 | "data": {
952 | "text/plain": [
953 | "'val1'"
954 | ]
955 | },
956 | "execution_count": 69,
957 | "metadata": {},
958 | "output_type": "execute_result"
959 | }
960 | ],
961 | "source": [
962 | "dic['key1']"
963 | ]
964 | },
965 | {
966 | "cell_type": "markdown",
967 | "metadata": {},
968 | "source": [
969 | "# Set"
970 | ]
971 | },
972 | {
973 | "cell_type": "code",
974 | "execution_count": 72,
975 | "metadata": {
976 | "collapsed": true
977 | },
978 | "outputs": [],
979 | "source": [
980 | "st = {1,2,4,5,4,5 ,6,7}"
981 | ]
982 | },
983 | {
984 | "cell_type": "code",
985 | "execution_count": 73,
986 | "metadata": {},
987 | "outputs": [
988 | {
989 | "data": {
990 | "text/plain": [
991 | "{1, 2, 4, 5, 6, 7}"
992 | ]
993 | },
994 | "execution_count": 73,
995 | "metadata": {},
996 | "output_type": "execute_result"
997 | }
998 | ],
999 | "source": [
1000 | "st"
1001 | ]
1002 | },
1003 | {
1004 | "cell_type": "code",
1005 | "execution_count": 74,
1006 | "metadata": {
1007 | "collapsed": true
1008 | },
1009 | "outputs": [],
1010 | "source": [
1011 | "st.add(8)"
1012 | ]
1013 | },
1014 | {
1015 | "cell_type": "code",
1016 | "execution_count": 75,
1017 | "metadata": {},
1018 | "outputs": [
1019 | {
1020 | "data": {
1021 | "text/plain": [
1022 | "{1, 2, 4, 5, 6, 7, 8}"
1023 | ]
1024 | },
1025 | "execution_count": 75,
1026 | "metadata": {},
1027 | "output_type": "execute_result"
1028 | }
1029 | ],
1030 | "source": [
1031 | "st"
1032 | ]
1033 | },
1034 | {
1035 | "cell_type": "code",
1036 | "execution_count": 76,
1037 | "metadata": {
1038 | "collapsed": true
1039 | },
1040 | "outputs": [],
1041 | "source": [
1042 | "st.add(4)"
1043 | ]
1044 | },
1045 | {
1046 | "cell_type": "code",
1047 | "execution_count": 77,
1048 | "metadata": {},
1049 | "outputs": [
1050 | {
1051 | "data": {
1052 | "text/plain": [
1053 | "{1, 2, 4, 5, 6, 7, 8}"
1054 | ]
1055 | },
1056 | "execution_count": 77,
1057 | "metadata": {},
1058 | "output_type": "execute_result"
1059 | }
1060 | ],
1061 | "source": [
1062 | "st"
1063 | ]
1064 | },
1065 | {
1066 | "cell_type": "markdown",
1067 | "metadata": {},
1068 | "source": [
1069 | "# Decision making, looping"
1070 | ]
1071 | },
1072 | {
1073 | "cell_type": "code",
1074 | "execution_count": null,
1075 | "metadata": {
1076 | "collapsed": true
1077 | },
1078 | "outputs": [],
1079 | "source": [
1080 | "if condition 1\n",
1081 | " perform task1\n",
1082 | "else \n",
1083 | " perform task2"
1084 | ]
1085 | },
1086 | {
1087 | "cell_type": "code",
1088 | "execution_count": 78,
1089 | "metadata": {},
1090 | "outputs": [
1091 | {
1092 | "data": {
1093 | "text/plain": [
1094 | "True"
1095 | ]
1096 | },
1097 | "execution_count": 78,
1098 | "metadata": {},
1099 | "output_type": "execute_result"
1100 | }
1101 | ],
1102 | "source": [
1103 | "True"
1104 | ]
1105 | },
1106 | {
1107 | "cell_type": "code",
1108 | "execution_count": 79,
1109 | "metadata": {},
1110 | "outputs": [
1111 | {
1112 | "data": {
1113 | "text/plain": [
1114 | "False"
1115 | ]
1116 | },
1117 | "execution_count": 79,
1118 | "metadata": {},
1119 | "output_type": "execute_result"
1120 | }
1121 | ],
1122 | "source": [
1123 | "False"
1124 | ]
1125 | },
1126 | {
1127 | "cell_type": "code",
1128 | "execution_count": 81,
1129 | "metadata": {},
1130 | "outputs": [
1131 | {
1132 | "data": {
1133 | "text/plain": [
1134 | "True"
1135 | ]
1136 | },
1137 | "execution_count": 81,
1138 | "metadata": {},
1139 | "output_type": "execute_result"
1140 | }
1141 | ],
1142 | "source": [
1143 | "4 < 5"
1144 | ]
1145 | },
1146 | {
1147 | "cell_type": "code",
1148 | "execution_count": 86,
1149 | "metadata": {},
1150 | "outputs": [
1151 | {
1152 | "name": "stdout",
1153 | "output_type": "stream",
1154 | "text": [
1155 | "Task2\n"
1156 | ]
1157 | }
1158 | ],
1159 | "source": [
1160 | "if 2 < 0:\n",
1161 | " print('Task1')\n",
1162 | "elif 5 < 3:\n",
1163 | " print('Task 3')\n",
1164 | "else:\n",
1165 | " print('Task2')"
1166 | ]
1167 | },
1168 | {
1169 | "cell_type": "markdown",
1170 | "metadata": {},
1171 | "source": [
1172 | "# For While"
1173 | ]
1174 | },
1175 | {
1176 | "cell_type": "code",
1177 | "execution_count": 88,
1178 | "metadata": {},
1179 | "outputs": [
1180 | {
1181 | "data": {
1182 | "text/plain": [
1183 | "[0, 1, 2, 3, 4, 5]"
1184 | ]
1185 | },
1186 | "execution_count": 88,
1187 | "metadata": {},
1188 | "output_type": "execute_result"
1189 | }
1190 | ],
1191 | "source": [
1192 | "list(range(6))"
1193 | ]
1194 | },
1195 | {
1196 | "cell_type": "code",
1197 | "execution_count": 89,
1198 | "metadata": {},
1199 | "outputs": [
1200 | {
1201 | "name": "stdout",
1202 | "output_type": "stream",
1203 | "text": [
1204 | "0\n",
1205 | "1\n",
1206 | "2\n",
1207 | "3\n",
1208 | "4\n",
1209 | "5\n"
1210 | ]
1211 | }
1212 | ],
1213 | "source": [
1214 | "for temp in range(6):\n",
1215 | " print(temp)"
1216 | ]
1217 | },
1218 | {
1219 | "cell_type": "code",
1220 | "execution_count": 90,
1221 | "metadata": {
1222 | "collapsed": true
1223 | },
1224 | "outputs": [],
1225 | "source": [
1226 | "lst = [3,5,'Hi',8]"
1227 | ]
1228 | },
1229 | {
1230 | "cell_type": "code",
1231 | "execution_count": 91,
1232 | "metadata": {},
1233 | "outputs": [
1234 | {
1235 | "name": "stdout",
1236 | "output_type": "stream",
1237 | "text": [
1238 | "3\n",
1239 | "5\n",
1240 | "Hi\n",
1241 | "8\n"
1242 | ]
1243 | }
1244 | ],
1245 | "source": [
1246 | "for temp in lst:\n",
1247 | " print(temp)"
1248 | ]
1249 | },
1250 | {
1251 | "cell_type": "code",
1252 | "execution_count": 100,
1253 | "metadata": {},
1254 | "outputs": [
1255 | {
1256 | "name": "stdout",
1257 | "output_type": "stream",
1258 | "text": [
1259 | "HI\n",
1260 | "HI\n",
1261 | "HI\n",
1262 | "HI\n",
1263 | "HI\n",
1264 | "HI\n",
1265 | "HI\n",
1266 | "HI\n",
1267 | "HI\n",
1268 | "HI\n"
1269 | ]
1270 | }
1271 | ],
1272 | "source": [
1273 | "i = 0\n",
1274 | "while i<10:\n",
1275 | " print('HI')\n",
1276 | " i = i+1\n"
1277 | ]
1278 | },
1279 | {
1280 | "cell_type": "code",
1281 | "execution_count": null,
1282 | "metadata": {
1283 | "collapsed": true
1284 | },
1285 | "outputs": [],
1286 | "source": []
1287 | },
1288 | {
1289 | "cell_type": "markdown",
1290 | "metadata": {},
1291 | "source": [
1292 | "# Function, Lambda, Map, Filter"
1293 | ]
1294 | },
1295 | {
1296 | "cell_type": "code",
1297 | "execution_count": 111,
1298 | "metadata": {
1299 | "collapsed": true
1300 | },
1301 | "outputs": [],
1302 | "source": [
1303 | "def printHello():\n",
1304 | " print(\"Hello\")"
1305 | ]
1306 | },
1307 | {
1308 | "cell_type": "code",
1309 | "execution_count": 112,
1310 | "metadata": {},
1311 | "outputs": [
1312 | {
1313 | "name": "stdout",
1314 | "output_type": "stream",
1315 | "text": [
1316 | "Hello\n"
1317 | ]
1318 | }
1319 | ],
1320 | "source": [
1321 | "printHello()"
1322 | ]
1323 | },
1324 | {
1325 | "cell_type": "code",
1326 | "execution_count": 116,
1327 | "metadata": {
1328 | "collapsed": true
1329 | },
1330 | "outputs": [],
1331 | "source": [
1332 | "def printHello1(param=\"Default Value\"):\n",
1333 | " print(param)"
1334 | ]
1335 | },
1336 | {
1337 | "cell_type": "code",
1338 | "execution_count": 117,
1339 | "metadata": {},
1340 | "outputs": [
1341 | {
1342 | "name": "stdout",
1343 | "output_type": "stream",
1344 | "text": [
1345 | "Hi\n"
1346 | ]
1347 | }
1348 | ],
1349 | "source": [
1350 | "printHello1(\"Hi\")"
1351 | ]
1352 | },
1353 | {
1354 | "cell_type": "code",
1355 | "execution_count": 118,
1356 | "metadata": {},
1357 | "outputs": [
1358 | {
1359 | "name": "stdout",
1360 | "output_type": "stream",
1361 | "text": [
1362 | "Default Value\n"
1363 | ]
1364 | }
1365 | ],
1366 | "source": [
1367 | "printHello1()"
1368 | ]
1369 | },
1370 | {
1371 | "cell_type": "code",
1372 | "execution_count": 119,
1373 | "metadata": {
1374 | "collapsed": true
1375 | },
1376 | "outputs": [],
1377 | "source": [
1378 | "def add(a, b):\n",
1379 | " c = a + b\n",
1380 | " return c"
1381 | ]
1382 | },
1383 | {
1384 | "cell_type": "code",
1385 | "execution_count": 122,
1386 | "metadata": {},
1387 | "outputs": [],
1388 | "source": [
1389 | "d = add(4,6)"
1390 | ]
1391 | },
1392 | {
1393 | "cell_type": "code",
1394 | "execution_count": 123,
1395 | "metadata": {},
1396 | "outputs": [
1397 | {
1398 | "data": {
1399 | "text/plain": [
1400 | "10"
1401 | ]
1402 | },
1403 | "execution_count": 123,
1404 | "metadata": {},
1405 | "output_type": "execute_result"
1406 | }
1407 | ],
1408 | "source": [
1409 | "d"
1410 | ]
1411 | },
1412 | {
1413 | "cell_type": "markdown",
1414 | "metadata": {},
1415 | "source": [
1416 | "# Lambda"
1417 | ]
1418 | },
1419 | {
1420 | "cell_type": "code",
1421 | "execution_count": 125,
1422 | "metadata": {
1423 | "collapsed": true
1424 | },
1425 | "outputs": [],
1426 | "source": [
1427 | "def addFive(a):\n",
1428 | " return a+ 5"
1429 | ]
1430 | },
1431 | {
1432 | "cell_type": "code",
1433 | "execution_count": 126,
1434 | "metadata": {},
1435 | "outputs": [
1436 | {
1437 | "data": {
1438 | "text/plain": [
1439 | "13"
1440 | ]
1441 | },
1442 | "execution_count": 126,
1443 | "metadata": {},
1444 | "output_type": "execute_result"
1445 | }
1446 | ],
1447 | "source": [
1448 | "addFive(8)"
1449 | ]
1450 | },
1451 | {
1452 | "cell_type": "code",
1453 | "execution_count": 127,
1454 | "metadata": {
1455 | "collapsed": true
1456 | },
1457 | "outputs": [],
1458 | "source": [
1459 | "addFive1 = lambda a : a+5"
1460 | ]
1461 | },
1462 | {
1463 | "cell_type": "code",
1464 | "execution_count": 128,
1465 | "metadata": {},
1466 | "outputs": [
1467 | {
1468 | "data": {
1469 | "text/plain": [
1470 | "13"
1471 | ]
1472 | },
1473 | "execution_count": 128,
1474 | "metadata": {},
1475 | "output_type": "execute_result"
1476 | }
1477 | ],
1478 | "source": [
1479 | "addFive1(8)"
1480 | ]
1481 | },
1482 | {
1483 | "cell_type": "markdown",
1484 | "metadata": {},
1485 | "source": [
1486 | "# Map "
1487 | ]
1488 | },
1489 | {
1490 | "cell_type": "code",
1491 | "execution_count": 129,
1492 | "metadata": {
1493 | "collapsed": true
1494 | },
1495 | "outputs": [],
1496 | "source": [
1497 | "lst = [2,5,7,8]"
1498 | ]
1499 | },
1500 | {
1501 | "cell_type": "code",
1502 | "execution_count": 130,
1503 | "metadata": {},
1504 | "outputs": [
1505 | {
1506 | "data": {
1507 | "text/plain": [
1508 | ""
1509 | ]
1510 | },
1511 | "execution_count": 130,
1512 | "metadata": {},
1513 | "output_type": "execute_result"
1514 | }
1515 | ],
1516 | "source": [
1517 | "addFive"
1518 | ]
1519 | },
1520 | {
1521 | "cell_type": "code",
1522 | "execution_count": 132,
1523 | "metadata": {},
1524 | "outputs": [
1525 | {
1526 | "data": {
1527 | "text/plain": [
1528 | "[7, 10, 12, 13]"
1529 | ]
1530 | },
1531 | "execution_count": 132,
1532 | "metadata": {},
1533 | "output_type": "execute_result"
1534 | }
1535 | ],
1536 | "source": [
1537 | "list(map(addFive, lst))"
1538 | ]
1539 | },
1540 | {
1541 | "cell_type": "markdown",
1542 | "metadata": {},
1543 | "source": [
1544 | "# Filter"
1545 | ]
1546 | },
1547 | {
1548 | "cell_type": "code",
1549 | "execution_count": 133,
1550 | "metadata": {},
1551 | "outputs": [
1552 | {
1553 | "data": {
1554 | "text/plain": [
1555 | "[2, 5, 7, 8]"
1556 | ]
1557 | },
1558 | "execution_count": 133,
1559 | "metadata": {},
1560 | "output_type": "execute_result"
1561 | }
1562 | ],
1563 | "source": [
1564 | "lst"
1565 | ]
1566 | },
1567 | {
1568 | "cell_type": "code",
1569 | "execution_count": 135,
1570 | "metadata": {},
1571 | "outputs": [
1572 | {
1573 | "data": {
1574 | "text/plain": [
1575 | "[5, 7]"
1576 | ]
1577 | },
1578 | "execution_count": 135,
1579 | "metadata": {},
1580 | "output_type": "execute_result"
1581 | }
1582 | ],
1583 | "source": [
1584 | "list(filter(lambda a: a % 2 == 1, lst))"
1585 | ]
1586 | },
1587 | {
1588 | "cell_type": "code",
1589 | "execution_count": null,
1590 | "metadata": {
1591 | "collapsed": true
1592 | },
1593 | "outputs": [],
1594 | "source": []
1595 | }
1596 | ],
1597 | "metadata": {
1598 | "kernelspec": {
1599 | "display_name": "Python 3",
1600 | "language": "python",
1601 | "name": "python3"
1602 | },
1603 | "language_info": {
1604 | "codemirror_mode": {
1605 | "name": "ipython",
1606 | "version": 3
1607 | },
1608 | "file_extension": ".py",
1609 | "mimetype": "text/x-python",
1610 | "name": "python",
1611 | "nbconvert_exporter": "python",
1612 | "pygments_lexer": "ipython3",
1613 | "version": "3.6.1"
1614 | }
1615 | },
1616 | "nbformat": 4,
1617 | "nbformat_minor": 2
1618 | }
1619 |
--------------------------------------------------------------------------------
/Python_Loops.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Python Loops.ipynb",
7 | "provenance": [],
8 | "collapsed_sections": [
9 | "lftlrceMeClR",
10 | "WCjQDt3_lCxz"
11 | ],
12 | "include_colab_link": true
13 | },
14 | "kernelspec": {
15 | "name": "python3",
16 | "display_name": "Python 3"
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": "RSVI7V3vMMpX",
34 | "colab_type": "text"
35 | },
36 | "source": [
37 | "# **Loops:**\n",
38 | "In general, statements are executed sequentially. The first statement in a function is executed first, followed by the second, and so on.\n",
39 | "\n",
40 | "There may be a situation when you need to execute a block of code several number of times\n",
41 | "A loop statement allows us to execute a statement or group of statements multiple times. \n",
42 | "\n",
43 | "In Python, there are three loops:\n",
44 | "\n",
45 | "1. While\n",
46 | "2. For\n",
47 | "3. Nested"
48 | ]
49 | },
50 | {
51 | "cell_type": "markdown",
52 | "metadata": {
53 | "id": "CdrL8DsGJ0Cc",
54 | "colab_type": "text"
55 | },
56 | "source": [
57 | "**While loop:**\n",
58 | "\n",
59 | "Here, first the condition is checked and if it’s true, control will move inside the loop and execute the statements inside the loop until the condition becomes false. We use this loop when we are not sure how many times we need to execute a group of statements or you can say that when we are unsure about the number of iterations."
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "metadata": {
65 | "id": "N2aeOTRWJvXG",
66 | "colab_type": "code",
67 | "colab": {
68 | "base_uri": "https://localhost:8080/",
69 | "height": 225
70 | },
71 | "outputId": "fe90d71d-caaf-48c0-f7a1-986cdc7cdb4d"
72 | },
73 | "source": [
74 | "count = 0\n",
75 | "while (count < 10):\n",
76 | " print ( count )\n",
77 | " count += 1\n",
78 | " \n",
79 | "print (\"While loop end\")\n",
80 | "print (count)"
81 | ],
82 | "execution_count": null,
83 | "outputs": [
84 | {
85 | "output_type": "stream",
86 | "text": [
87 | "0\n",
88 | "1\n",
89 | "2\n",
90 | "3\n",
91 | "4\n",
92 | "5\n",
93 | "6\n",
94 | "7\n",
95 | "8\n",
96 | "9\n",
97 | "While loop end\n",
98 | "10\n"
99 | ],
100 | "name": "stdout"
101 | }
102 | ]
103 | },
104 | {
105 | "cell_type": "markdown",
106 | "metadata": {
107 | "id": "W42i-o-cMRXz",
108 | "colab_type": "text"
109 | },
110 | "source": [
111 | "**For Loop:** \n",
112 | "\n",
113 | "Like the While loop, the For loop also allows a code block to be repeated certain number of times. The difference is, in For loop we know the amount of iterations required unlike While loop, where iterations depends on the condition. "
114 | ]
115 | },
116 | {
117 | "cell_type": "code",
118 | "metadata": {
119 | "id": "tqiSire4MTaN",
120 | "colab_type": "code",
121 | "colab": {
122 | "base_uri": "https://localhost:8080/",
123 | "height": 69
124 | },
125 | "outputId": "09f2248a-46c1-47e3-d23c-151ae9289a5f"
126 | },
127 | "source": [
128 | "food = ['Spinach', 'Mango', 'Tomato'] # Iterate through the elements in the Lists\n",
129 | "\n",
130 | "for i in range(len(food)):\n",
131 | " print (food[i])\n",
132 | " "
133 | ],
134 | "execution_count": null,
135 | "outputs": [
136 | {
137 | "output_type": "stream",
138 | "text": [
139 | "Spinach\n",
140 | "Mango\n",
141 | "Tomato\n"
142 | ],
143 | "name": "stdout"
144 | }
145 | ]
146 | },
147 | {
148 | "cell_type": "code",
149 | "metadata": {
150 | "id": "xsPwuCc2Rx_o",
151 | "colab_type": "code",
152 | "colab": {
153 | "base_uri": "https://localhost:8080/",
154 | "height": 34
155 | },
156 | "outputId": "6b30d51b-ccf4-45a8-acfb-b6dd9d5c161c"
157 | },
158 | "source": [
159 | "food = ['Spinach', 'Mango', 'Tomato'] \n",
160 | "print (food[1])"
161 | ],
162 | "execution_count": null,
163 | "outputs": [
164 | {
165 | "output_type": "stream",
166 | "text": [
167 | "Mango\n"
168 | ],
169 | "name": "stdout"
170 | }
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "metadata": {
176 | "id": "eOvCa_YCNuOv",
177 | "colab_type": "code",
178 | "colab": {
179 | "base_uri": "https://localhost:8080/",
180 | "height": 68
181 | },
182 | "outputId": "c9baf5e6-b6fe-4642-be8f-2a5c4ec4205a"
183 | },
184 | "source": [
185 | "family = {'Dad':'Goku', 'Mom':'Chichi', 'No. of Children':2} # iterate through two things at once (using tuple )\n",
186 | "for key, value in family.items():\n",
187 | " print(key, value)"
188 | ],
189 | "execution_count": null,
190 | "outputs": [
191 | {
192 | "output_type": "stream",
193 | "text": [
194 | "Dad Goku\n",
195 | "Mom Chichi\n",
196 | "No. of Children 2\n"
197 | ],
198 | "name": "stdout"
199 | }
200 | ]
201 | },
202 | {
203 | "cell_type": "code",
204 | "metadata": {
205 | "id": "AufenN-nOkym",
206 | "colab_type": "code",
207 | "colab": {
208 | "base_uri": "https://localhost:8080/",
209 | "height": 34
210 | },
211 | "outputId": "c8fafa14-3c80-406b-f3c3-2f0199b74662"
212 | },
213 | "source": [
214 | "#An example of for-each loop\n",
215 | "food = ['Spinach', \"Tomato\",\"Mango\"] \n",
216 | "for i in food:\n",
217 | " if i == 'Mango':\n",
218 | " m = 1\n",
219 | " print('Found Mango!')\n",
220 | " # exit the loop and skip the 'else' block\n",
221 | " break\n",
222 | "else :\n",
223 | " print(\"Can't find Mango\") \n",
224 | "\n",
225 | " "
226 | ],
227 | "execution_count": null,
228 | "outputs": [
229 | {
230 | "output_type": "stream",
231 | "text": [
232 | "Can't find Mango\n"
233 | ],
234 | "name": "stdout"
235 | }
236 | ]
237 | },
238 | {
239 | "cell_type": "markdown",
240 | "metadata": {
241 | "id": "POX1jpBnRMup",
242 | "colab_type": "text"
243 | },
244 | "source": [
245 | "**Nested Loops**\n",
246 | "\n",
247 | "A nested loop is a loop inside a loop.\n",
248 | "\n",
249 | "The \"inner loop\" will be executed one time for each iteration of the \"outer loop\":"
250 | ]
251 | },
252 | {
253 | "cell_type": "code",
254 | "metadata": {
255 | "id": "72iV-7CyRWuJ",
256 | "colab_type": "code",
257 | "colab": {
258 | "base_uri": "https://localhost:8080/",
259 | "height": 175
260 | },
261 | "outputId": "71285a34-ce18-434c-fb45-0a7e297b61e5"
262 | },
263 | "source": [
264 | "colour = [\"Red\", \"Yellow\", \"Green\"]\n",
265 | "fruits = [\"Apples\", \"Mangoes\", \"Grapes\"]\n",
266 | "\n",
267 | "for x in colour:\n",
268 | " for y in fruits:\n",
269 | " print(x +\" -\", y)"
270 | ],
271 | "execution_count": null,
272 | "outputs": [
273 | {
274 | "output_type": "stream",
275 | "text": [
276 | "Red - Apples\n",
277 | "Red - Mangoes\n",
278 | "Red - Grapes\n",
279 | "Yellow - Apples\n",
280 | "Yellow - Mangoes\n",
281 | "Yellow - Grapes\n",
282 | "Green - Apples\n",
283 | "Green - Mangoes\n",
284 | "Green - Grapes\n"
285 | ],
286 | "name": "stdout"
287 | }
288 | ]
289 | },
290 | {
291 | "cell_type": "code",
292 | "metadata": {
293 | "id": "t-YNe2y5aLE-",
294 | "colab_type": "code",
295 | "colab": {
296 | "base_uri": "https://localhost:8080/",
297 | "height": 70
298 | },
299 | "outputId": "db559a76-3976-45bd-cf57-81d66b8bb6d5"
300 | },
301 | "source": [
302 | "colour = [\"Red\", \"Yellow\", \"Green\"]\n",
303 | "fruits = [\"Apples\", \"Mangoes\", \"Grapes\"]\n",
304 | "\n",
305 | "\n",
306 | "for x,y in zip(range(len(colour)), range(len(fruits))) :\n",
307 | " print(colour[x],fruits[y])"
308 | ],
309 | "execution_count": null,
310 | "outputs": [
311 | {
312 | "output_type": "stream",
313 | "text": [
314 | "Red Apples\n",
315 | "Yellow Mangoes\n",
316 | "Green Grapes\n"
317 | ],
318 | "name": "stdout"
319 | }
320 | ]
321 | },
322 | {
323 | "cell_type": "code",
324 | "metadata": {
325 | "id": "3oXw24ZMR51_",
326 | "colab_type": "code",
327 | "colab": {
328 | "base_uri": "https://localhost:8080/",
329 | "height": 323
330 | },
331 | "outputId": "bca19e58-ab9b-488e-ac0f-78d63bead8d8"
332 | },
333 | "source": [
334 | "colour = [\"Red\", \"Yellow\", \"Green\"]\n",
335 | "fruits = [\"Apples\", \"Mangoes\", \"Grapes\"]\n",
336 | "surface = [\"Smooth\", \"Rough\"]\n",
337 | "\n",
338 | "for x in colour:\n",
339 | " for y in fruits:\n",
340 | " for z in surface:\n",
341 | " print(x +\" -\", y +\" -\", z)"
342 | ],
343 | "execution_count": null,
344 | "outputs": [
345 | {
346 | "output_type": "stream",
347 | "text": [
348 | "Red - Apples - Smooth\n",
349 | "Red - Apples - Rough\n",
350 | "Red - Mangoes - Smooth\n",
351 | "Red - Mangoes - Rough\n",
352 | "Red - Grapes - Smooth\n",
353 | "Red - Grapes - Rough\n",
354 | "Yellow - Apples - Smooth\n",
355 | "Yellow - Apples - Rough\n",
356 | "Yellow - Mangoes - Smooth\n",
357 | "Yellow - Mangoes - Rough\n",
358 | "Yellow - Grapes - Smooth\n",
359 | "Yellow - Grapes - Rough\n",
360 | "Green - Apples - Smooth\n",
361 | "Green - Apples - Rough\n",
362 | "Green - Mangoes - Smooth\n",
363 | "Green - Mangoes - Rough\n",
364 | "Green - Grapes - Smooth\n",
365 | "Green - Grapes - Rough\n"
366 | ],
367 | "name": "stdout"
368 | }
369 | ]
370 | },
371 | {
372 | "cell_type": "code",
373 | "metadata": {
374 | "id": "qL9d3LlXdVsO",
375 | "colab_type": "code",
376 | "colab": {
377 | "base_uri": "https://localhost:8080/",
378 | "height": 70
379 | },
380 | "outputId": "46096ef9-dade-4eca-eb94-e77812f521f4"
381 | },
382 | "source": [
383 | "colour = [\"Red\", \"Yellow\", \"Green\"]\n",
384 | "fruits = [\"Apples\", \"Mangoes\", \"Grapes\"]\n",
385 | "surface = [\"Smooth\", \"Rough\", \"Soft\"]\n",
386 | "\n",
387 | "for x,y,z in zip(range(len(colour)), range(len(fruits)), range(len(surface))) :\n",
388 | " print(colour[x],fruits[y],surface[z])"
389 | ],
390 | "execution_count": null,
391 | "outputs": [
392 | {
393 | "output_type": "stream",
394 | "text": [
395 | "Red Apples Smooth\n",
396 | "Yellow Mangoes Rough\n",
397 | "Green Grapes Soft\n"
398 | ],
399 | "name": "stdout"
400 | }
401 | ]
402 | },
403 | {
404 | "cell_type": "code",
405 | "metadata": {
406 | "id": "SjK_B6HHSg-S",
407 | "colab_type": "code",
408 | "colab": {
409 | "base_uri": "https://localhost:8080/",
410 | "height": 35
411 | },
412 | "outputId": "18a88831-4550-4f40-8884-2f2cbc096ea9"
413 | },
414 | "source": [
415 | "#Matrix Example\n",
416 | "# for loop to flatten a 2d-matrix\n",
417 | "matrix = [[1, 2], [3, 4]]\n",
418 | "matrix2 = []\n",
419 | "for i in matrix:\n",
420 | " for j in i:\n",
421 | " matrix2.append(j)\n",
422 | "print (matrix2)"
423 | ],
424 | "execution_count": null,
425 | "outputs": [
426 | {
427 | "output_type": "stream",
428 | "text": [
429 | "[1, 2, 3, 4]\n"
430 | ],
431 | "name": "stdout"
432 | }
433 | ]
434 | },
435 | {
436 | "cell_type": "code",
437 | "metadata": {
438 | "id": "_6csInMk5uoH",
439 | "colab_type": "code",
440 | "colab": {
441 | "base_uri": "https://localhost:8080/",
442 | "height": 119
443 | },
444 | "outputId": "d90ed92f-d51a-429f-f69c-483e85ad51ee"
445 | },
446 | "source": [
447 | "# populate a dictionary using a for loop\n",
448 | "name = ['Naruto', 'Ryuzaki', 'Vegeta', 'Kuroko', 'Zoro', 'Hina']\n",
449 | "weight = [15, 18, 17, 30, 25, 23]\n",
450 | "dictionary = {}\n",
451 | "\n",
452 | "for i in range(0,6):\n",
453 | " dictionary[name[i]] = weight[i]\n",
454 | "\n",
455 | "dictionary"
456 | ],
457 | "execution_count": null,
458 | "outputs": [
459 | {
460 | "output_type": "execute_result",
461 | "data": {
462 | "text/plain": [
463 | "{'Hina': 23,\n",
464 | " 'Kuroko': 30,\n",
465 | " 'Naruto': 15,\n",
466 | " 'Ryuzaki': 18,\n",
467 | " 'Vegeta': 17,\n",
468 | " 'Zoro': 25}"
469 | ]
470 | },
471 | "metadata": {
472 | "tags": []
473 | },
474 | "execution_count": 22
475 | }
476 | ]
477 | },
478 | {
479 | "cell_type": "code",
480 | "metadata": {
481 | "id": "zvDGp4TRYNjk",
482 | "colab_type": "code",
483 | "colab": {
484 | "base_uri": "https://localhost:8080/",
485 | "height": 191
486 | },
487 | "outputId": "6d16ee28-9e53-4596-c4be-c6302e48980d"
488 | },
489 | "source": [
490 | "# Lets print some shapes to get comfortable with loops\n",
491 | "\n",
492 | "# pyramid\n",
493 | "for i in range(10):\n",
494 | " print((10-i)*' ' + i*'* ')\n",
495 | "\n",
496 | "\n"
497 | ],
498 | "execution_count": null,
499 | "outputs": [
500 | {
501 | "output_type": "stream",
502 | "text": [
503 | " \n",
504 | " * \n",
505 | " * * \n",
506 | " * * * \n",
507 | " * * * * \n",
508 | " * * * * * \n",
509 | " * * * * * * \n",
510 | " * * * * * * * \n",
511 | " * * * * * * * * \n",
512 | " * * * * * * * * * \n"
513 | ],
514 | "name": "stdout"
515 | }
516 | ]
517 | },
518 | {
519 | "cell_type": "code",
520 | "metadata": {
521 | "id": "DcmrF2TkulGA",
522 | "colab_type": "code",
523 | "colab": {
524 | "base_uri": "https://localhost:8080/",
525 | "height": 191
526 | },
527 | "outputId": "dbb76a73-cb0d-4156-f6b2-dad45a8c20bc"
528 | },
529 | "source": [
530 | "# half diamond\n",
531 | "for i in range(10):\n",
532 | " if i < 6:\n",
533 | " print(i*' *')\n",
534 | " else:\n",
535 | " print((10 - i)*' *')"
536 | ],
537 | "execution_count": null,
538 | "outputs": [
539 | {
540 | "output_type": "stream",
541 | "text": [
542 | "\n",
543 | " *\n",
544 | " * *\n",
545 | " * * *\n",
546 | " * * * *\n",
547 | " * * * * *\n",
548 | " * * * *\n",
549 | " * * *\n",
550 | " * *\n",
551 | " *\n"
552 | ],
553 | "name": "stdout"
554 | }
555 | ]
556 | },
557 | {
558 | "cell_type": "markdown",
559 | "metadata": {
560 | "id": "u64LQwaoY3sn",
561 | "colab_type": "text"
562 | },
563 | "source": [
564 | "# **Functions:**\n",
565 | "\n",
566 | "Functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it more readable, reuse it and save some time."
567 | ]
568 | },
569 | {
570 | "cell_type": "markdown",
571 | "metadata": {
572 | "id": "41Sl4Q_RZOqY",
573 | "colab_type": "text"
574 | },
575 | "source": [
576 | "**Define a function with no arguments and no return values:**"
577 | ]
578 | },
579 | {
580 | "cell_type": "code",
581 | "metadata": {
582 | "id": "7lPlGuNiY_xP",
583 | "colab_type": "code",
584 | "colab": {}
585 | },
586 | "source": [
587 | "def print_text():\n",
588 | " print('this is text')"
589 | ],
590 | "execution_count": null,
591 | "outputs": []
592 | },
593 | {
594 | "cell_type": "code",
595 | "metadata": {
596 | "id": "_9uSsLHPZKi5",
597 | "colab_type": "code",
598 | "colab": {
599 | "base_uri": "https://localhost:8080/",
600 | "height": 35
601 | },
602 | "outputId": "ccafa6df-ef75-45e4-e6e5-8e1939771963"
603 | },
604 | "source": [
605 | "# call the function\n",
606 | "print_text()"
607 | ],
608 | "execution_count": null,
609 | "outputs": [
610 | {
611 | "output_type": "stream",
612 | "text": [
613 | "this is text\n"
614 | ],
615 | "name": "stdout"
616 | }
617 | ]
618 | },
619 | {
620 | "cell_type": "markdown",
621 | "metadata": {
622 | "id": "2iYTL18xZS6Q",
623 | "colab_type": "text"
624 | },
625 | "source": [
626 | "**Define a function with one argument and no return values:**"
627 | ]
628 | },
629 | {
630 | "cell_type": "code",
631 | "metadata": {
632 | "id": "Vo-cD2OxZUuB",
633 | "colab_type": "code",
634 | "colab": {}
635 | },
636 | "source": [
637 | "def print_this(x):\n",
638 | " print(x)"
639 | ],
640 | "execution_count": null,
641 | "outputs": []
642 | },
643 | {
644 | "cell_type": "code",
645 | "metadata": {
646 | "id": "7D16LaH5ZWHY",
647 | "colab_type": "code",
648 | "colab": {
649 | "base_uri": "https://localhost:8080/",
650 | "height": 34
651 | },
652 | "outputId": "79b32571-d094-41b2-cc0d-e1f85741ae6d"
653 | },
654 | "source": [
655 | "# call the function\n",
656 | "print_this(3)"
657 | ],
658 | "execution_count": null,
659 | "outputs": [
660 | {
661 | "output_type": "stream",
662 | "text": [
663 | "3\n"
664 | ],
665 | "name": "stdout"
666 | }
667 | ]
668 | },
669 | {
670 | "cell_type": "markdown",
671 | "metadata": {
672 | "id": "35bK9N-FZbsg",
673 | "colab_type": "text"
674 | },
675 | "source": [
676 | "**Define a function with one argument and one return value:**"
677 | ]
678 | },
679 | {
680 | "cell_type": "code",
681 | "metadata": {
682 | "id": "z1jqUXCiZfk5",
683 | "colab_type": "code",
684 | "colab": {
685 | "base_uri": "https://localhost:8080/",
686 | "height": 34
687 | },
688 | "outputId": "7173699b-fee4-4388-838f-dc5ed904a0ba"
689 | },
690 | "source": [
691 | "def square(x):\n",
692 | " return x**2\n",
693 | "square(5)"
694 | ],
695 | "execution_count": null,
696 | "outputs": [
697 | {
698 | "output_type": "execute_result",
699 | "data": {
700 | "text/plain": [
701 | "25"
702 | ]
703 | },
704 | "metadata": {
705 | "tags": []
706 | },
707 | "execution_count": 28
708 | }
709 | ]
710 | },
711 | {
712 | "cell_type": "code",
713 | "metadata": {
714 | "id": "ne0NdmCn5ul9",
715 | "colab_type": "code",
716 | "colab": {}
717 | },
718 | "source": [
719 | "#Example\n",
720 | "\n",
721 | "def calc(a, b, op='add'):\n",
722 | "\n",
723 | " if op == 'add':\n",
724 | " return a + b\n",
725 | "\n",
726 | " elif op == 'sub':\n",
727 | " return a - b\n",
728 | " \n",
729 | " else:\n",
730 | " print('valid operations are add and sub')"
731 | ],
732 | "execution_count": null,
733 | "outputs": []
734 | },
735 | {
736 | "cell_type": "code",
737 | "metadata": {
738 | "id": "hH_4MlBcaOjZ",
739 | "colab_type": "code",
740 | "colab": {
741 | "base_uri": "https://localhost:8080/",
742 | "height": 35
743 | },
744 | "outputId": "14253348-7ae7-419c-80d0-2a30885cbad9"
745 | },
746 | "source": [
747 | "# call the function\n",
748 | "calc(10, 4, op='add')"
749 | ],
750 | "execution_count": null,
751 | "outputs": [
752 | {
753 | "output_type": "execute_result",
754 | "data": {
755 | "text/plain": [
756 | "14"
757 | ]
758 | },
759 | "metadata": {
760 | "tags": []
761 | },
762 | "execution_count": 17
763 | }
764 | ]
765 | },
766 | {
767 | "cell_type": "code",
768 | "metadata": {
769 | "id": "6yJj0Zr4af0w",
770 | "colab_type": "code",
771 | "colab": {
772 | "base_uri": "https://localhost:8080/",
773 | "height": 34
774 | },
775 | "outputId": "0008db64-99e9-495d-bae4-f9227f322e73"
776 | },
777 | "source": [
778 | "# unnamed arguments are inferred by position\n",
779 | "calc(10, 4, 'add')"
780 | ],
781 | "execution_count": null,
782 | "outputs": [
783 | {
784 | "output_type": "execute_result",
785 | "data": {
786 | "text/plain": [
787 | "14"
788 | ]
789 | },
790 | "metadata": {
791 | "tags": []
792 | },
793 | "execution_count": 31
794 | }
795 | ]
796 | },
797 | {
798 | "cell_type": "code",
799 | "metadata": {
800 | "id": "yy-kreh6agU4",
801 | "colab_type": "code",
802 | "colab": {
803 | "base_uri": "https://localhost:8080/",
804 | "height": 34
805 | },
806 | "outputId": "ea6bdf09-47e6-4656-eab6-a57246940674"
807 | },
808 | "source": [
809 | "# default for 'op' is 'add'\n",
810 | "calc(10, 4)"
811 | ],
812 | "execution_count": null,
813 | "outputs": [
814 | {
815 | "output_type": "execute_result",
816 | "data": {
817 | "text/plain": [
818 | "14"
819 | ]
820 | },
821 | "metadata": {
822 | "tags": []
823 | },
824 | "execution_count": 32
825 | }
826 | ]
827 | },
828 | {
829 | "cell_type": "code",
830 | "metadata": {
831 | "id": "TnLSMIGeahaZ",
832 | "colab_type": "code",
833 | "colab": {
834 | "base_uri": "https://localhost:8080/",
835 | "height": 35
836 | },
837 | "outputId": "c7796064-8c36-4306-856b-aead947e65f7"
838 | },
839 | "source": [
840 | "calc(10, 4, 'sub')"
841 | ],
842 | "execution_count": null,
843 | "outputs": [
844 | {
845 | "output_type": "execute_result",
846 | "data": {
847 | "text/plain": [
848 | "6"
849 | ]
850 | },
851 | "metadata": {
852 | "tags": []
853 | },
854 | "execution_count": 18
855 | }
856 | ]
857 | },
858 | {
859 | "cell_type": "markdown",
860 | "metadata": {
861 | "id": "J-VXN_PKasK5",
862 | "colab_type": "text"
863 | },
864 | "source": [
865 | "**Use pass as a placeholder if you haven't written the function body:**"
866 | ]
867 | },
868 | {
869 | "cell_type": "code",
870 | "metadata": {
871 | "id": "TwMQFziQatdI",
872 | "colab_type": "code",
873 | "colab": {}
874 | },
875 | "source": [
876 | "def stub():\n",
877 | " pass"
878 | ],
879 | "execution_count": null,
880 | "outputs": []
881 | },
882 | {
883 | "cell_type": "markdown",
884 | "metadata": {
885 | "id": "8XwkxOYsayA5",
886 | "colab_type": "text"
887 | },
888 | "source": [
889 | "**Return two values from a single function:**"
890 | ]
891 | },
892 | {
893 | "cell_type": "code",
894 | "metadata": {
895 | "id": "lMkT5MqKa5vg",
896 | "colab_type": "code",
897 | "colab": {}
898 | },
899 | "source": [
900 | "def min_max(nums):\n",
901 | " return min(nums), max(nums)"
902 | ],
903 | "execution_count": null,
904 | "outputs": []
905 | },
906 | {
907 | "cell_type": "code",
908 | "metadata": {
909 | "id": "4kGkSJ4Aa9Lo",
910 | "colab_type": "code",
911 | "colab": {
912 | "base_uri": "https://localhost:8080/",
913 | "height": 34
914 | },
915 | "outputId": "8668913a-6814-4fbc-ba3b-159071e0021d"
916 | },
917 | "source": [
918 | "# return values can be assigned to a single variable as a tuple\n",
919 | "nums = [1, 2, 3]\n",
920 | "output = min_max(nums)\n",
921 | "output"
922 | ],
923 | "execution_count": null,
924 | "outputs": [
925 | {
926 | "output_type": "execute_result",
927 | "data": {
928 | "text/plain": [
929 | "(1, 3)"
930 | ]
931 | },
932 | "metadata": {
933 | "tags": []
934 | },
935 | "execution_count": 35
936 | }
937 | ]
938 | },
939 | {
940 | "cell_type": "markdown",
941 | "metadata": {
942 | "id": "lftlrceMeClR",
943 | "colab_type": "text"
944 | },
945 | "source": [
946 | "# **Anonymous Functions (Lambda Functions)**\n",
947 | "Primarily used to temporarily define a function for use by another function.\n",
948 | "* Anonymous\n",
949 | "* Single use, simple 1=line function\n",
950 | "* Throw-away functions\n",
951 | "\n",
952 | "**Syntax :**\n",
953 | "\n",
954 | "`lambda arguments : expression`\n"
955 | ]
956 | },
957 | {
958 | "cell_type": "markdown",
959 | "metadata": {
960 | "id": "DuIye2TkeK_R",
961 | "colab_type": "text"
962 | },
963 | "source": [
964 | "**Here are some single-argument Lambdas:**"
965 | ]
966 | },
967 | {
968 | "cell_type": "code",
969 | "metadata": {
970 | "id": "oTZQdaT9enQi",
971 | "colab_type": "code",
972 | "colab": {
973 | "base_uri": "https://localhost:8080/",
974 | "height": 34
975 | },
976 | "outputId": "1469c5b7-bc25-4a92-8969-f0427f2a5266"
977 | },
978 | "source": [
979 | "add10 = lambda x : x + 10\n",
980 | "print (add10(5))"
981 | ],
982 | "execution_count": null,
983 | "outputs": [
984 | {
985 | "output_type": "stream",
986 | "text": [
987 | "15\n"
988 | ],
989 | "name": "stdout"
990 | }
991 | ]
992 | },
993 | {
994 | "cell_type": "code",
995 | "metadata": {
996 | "id": "HS2ZWAjsezaS",
997 | "colab_type": "code",
998 | "colab": {
999 | "base_uri": "https://localhost:8080/",
1000 | "height": 34
1001 | },
1002 | "outputId": "f6863170-8a07-47ba-9572-34ec5a2ba015"
1003 | },
1004 | "source": [
1005 | "square = lambda x : x * x\n",
1006 | "print (square(10))"
1007 | ],
1008 | "execution_count": null,
1009 | "outputs": [
1010 | {
1011 | "output_type": "stream",
1012 | "text": [
1013 | "100\n"
1014 | ],
1015 | "name": "stdout"
1016 | }
1017 | ]
1018 | },
1019 | {
1020 | "cell_type": "markdown",
1021 | "metadata": {
1022 | "id": "XdZhPXI_f736",
1023 | "colab_type": "text"
1024 | },
1025 | "source": [
1026 | "**Sorting a List of Tuples using Lambda**"
1027 | ]
1028 | },
1029 | {
1030 | "cell_type": "code",
1031 | "metadata": {
1032 | "id": "ApvFOgRhf01h",
1033 | "colab_type": "code",
1034 | "colab": {
1035 | "base_uri": "https://localhost:8080/",
1036 | "height": 34
1037 | },
1038 | "outputId": "ec17bfad-41bd-428a-e858-55bc10a335f3"
1039 | },
1040 | "source": [
1041 | "List = [('carrots', 20), ('potatoes', 50), ('cabbage', 25), ('peas', 15)]\n",
1042 | "\n",
1043 | "List.sort (key = lambda x : x[0])\n",
1044 | "print (List)"
1045 | ],
1046 | "execution_count": null,
1047 | "outputs": [
1048 | {
1049 | "output_type": "stream",
1050 | "text": [
1051 | "[('cabbage', 25), ('carrots', 20), ('peas', 15), ('potatoes', 50)]\n"
1052 | ],
1053 | "name": "stdout"
1054 | }
1055 | ]
1056 | },
1057 | {
1058 | "cell_type": "markdown",
1059 | "metadata": {
1060 | "id": "6QEfcDqqhOkK",
1061 | "colab_type": "text"
1062 | },
1063 | "source": [
1064 | "**Sorting a list of Dictionaries using Lambda**"
1065 | ]
1066 | },
1067 | {
1068 | "cell_type": "code",
1069 | "metadata": {
1070 | "id": "_CtKVqjRg99i",
1071 | "colab_type": "code",
1072 | "colab": {
1073 | "base_uri": "https://localhost:8080/",
1074 | "height": 55
1075 | },
1076 | "outputId": "d9c197e2-ef10-484a-c93f-ae300c1a9e0b"
1077 | },
1078 | "source": [
1079 | "List1 = [{'Game': 'GTA5', 'Company': 'Rockstar Games', 'Year': 2013} , {'Game': 'Jump Force', 'Company': 'Bandai Namco', 'Year': 2018} , {'Game': 'Fortnite', 'Company': 'Epic Games', 'Year': 2017}]\n",
1080 | "Output = []\n",
1081 | "Output = sorted (List1, key = lambda x : x['Company'])\n",
1082 | "print (Output)"
1083 | ],
1084 | "execution_count": null,
1085 | "outputs": [
1086 | {
1087 | "output_type": "stream",
1088 | "text": [
1089 | "[{'Game': 'Jump Force', 'Company': 'Bandai Namco', 'Year': 2018}, {'Game': 'Fortnite', 'Company': 'Epic Games', 'Year': 2017}, {'Game': 'GTA5', 'Company': 'Rockstar Games', 'Year': 2013}]\n"
1090 | ],
1091 | "name": "stdout"
1092 | }
1093 | ]
1094 | },
1095 | {
1096 | "cell_type": "markdown",
1097 | "metadata": {
1098 | "id": "WCjQDt3_lCxz",
1099 | "colab_type": "text"
1100 | },
1101 | "source": [
1102 | "# **Map, Filter and Reduce**"
1103 | ]
1104 | },
1105 | {
1106 | "cell_type": "markdown",
1107 | "metadata": {
1108 | "id": "6eCGX6IOlbkj",
1109 | "colab_type": "text"
1110 | },
1111 | "source": [
1112 | "**Map Function**"
1113 | ]
1114 | },
1115 | {
1116 | "cell_type": "markdown",
1117 | "metadata": {
1118 | "id": "eB_Xcx85lNY6",
1119 | "colab_type": "text"
1120 | },
1121 | "source": [
1122 | "The Map function `map()` :\n",
1123 | "\n",
1124 | "* Applies a function to every element of a sequence \n",
1125 | "* Returns the modified list\n",
1126 | "\n",
1127 | "\n"
1128 | ]
1129 | },
1130 | {
1131 | "cell_type": "code",
1132 | "metadata": {
1133 | "id": "x5MD2wD85unn",
1134 | "colab_type": "code",
1135 | "colab": {
1136 | "base_uri": "https://localhost:8080/",
1137 | "height": 68
1138 | },
1139 | "outputId": "3fa572e2-4a35-4205-fda0-0c214584b0d5"
1140 | },
1141 | "source": [
1142 | "L1 = ['Unity', 'Unreal', 'Godot']\n",
1143 | "mapping = (map(len, L1)) #where len is the function and L1 is the list name which are the parameters of the map function \n",
1144 | "for i in mapping: \n",
1145 | " print(i)"
1146 | ],
1147 | "execution_count": null,
1148 | "outputs": [
1149 | {
1150 | "output_type": "stream",
1151 | "text": [
1152 | "5\n",
1153 | "6\n",
1154 | "5\n"
1155 | ],
1156 | "name": "stdout"
1157 | }
1158 | ]
1159 | },
1160 | {
1161 | "cell_type": "code",
1162 | "metadata": {
1163 | "id": "7f02NRxioiQS",
1164 | "colab_type": "code",
1165 | "colab": {
1166 | "base_uri": "https://localhost:8080/",
1167 | "height": 35
1168 | },
1169 | "outputId": "361d4845-c40b-442e-b2bd-b79178a88f55"
1170 | },
1171 | "source": [
1172 | "#lambda function on list using Map\n",
1173 | "\n",
1174 | "n1,n2 = [3,5,7,9],[]\n",
1175 | "n2 = list((map(lambda x : x **2, n1)))\n",
1176 | "print(n2)"
1177 | ],
1178 | "execution_count": null,
1179 | "outputs": [
1180 | {
1181 | "output_type": "stream",
1182 | "text": [
1183 | "[9, 25, 49, 81]\n"
1184 | ],
1185 | "name": "stdout"
1186 | }
1187 | ]
1188 | },
1189 | {
1190 | "cell_type": "markdown",
1191 | "metadata": {
1192 | "id": "Cl-pl0XeqlBd",
1193 | "colab_type": "text"
1194 | },
1195 | "source": [
1196 | "**Filter Function**\n",
1197 | "\n",
1198 | "The Filter Function `filter()` :\n",
1199 | "\n",
1200 | "* Filters items out of a sequence\n",
1201 | "* Return filtered list"
1202 | ]
1203 | },
1204 | {
1205 | "cell_type": "code",
1206 | "metadata": {
1207 | "id": "2bed5vJo5un5",
1208 | "colab_type": "code",
1209 | "colab": {
1210 | "base_uri": "https://localhost:8080/",
1211 | "height": 68
1212 | },
1213 | "outputId": "693c891d-e78b-40b9-add1-6b4be02449b8"
1214 | },
1215 | "source": [
1216 | "nums = range(5)\n",
1217 | "filtering = filter(lambda x: x % 2 == 0, nums) #where this lambda function describes the condition and nums is the set of numbers ranging between 0-5\n",
1218 | "for i in filtering:\n",
1219 | " print(i)"
1220 | ],
1221 | "execution_count": null,
1222 | "outputs": [
1223 | {
1224 | "output_type": "stream",
1225 | "text": [
1226 | "0\n",
1227 | "2\n",
1228 | "4\n"
1229 | ],
1230 | "name": "stdout"
1231 | }
1232 | ]
1233 | },
1234 | {
1235 | "cell_type": "code",
1236 | "metadata": {
1237 | "id": "EP2KA4pPsNmS",
1238 | "colab_type": "code",
1239 | "colab": {
1240 | "base_uri": "https://localhost:8080/",
1241 | "height": 34
1242 | },
1243 | "outputId": "93de9146-f4aa-4333-d12e-97950610e0a5"
1244 | },
1245 | "source": [
1246 | "n1,n2 = [3,5,7,9], []\n",
1247 | "n2 = list(filter(lambda x : x > 5 , n1))\n",
1248 | "print (n2)"
1249 | ],
1250 | "execution_count": null,
1251 | "outputs": [
1252 | {
1253 | "output_type": "stream",
1254 | "text": [
1255 | "[7, 9]\n"
1256 | ],
1257 | "name": "stdout"
1258 | }
1259 | ]
1260 | },
1261 | {
1262 | "cell_type": "markdown",
1263 | "metadata": {
1264 | "id": "m0lhP7TNunB9",
1265 | "colab_type": "text"
1266 | },
1267 | "source": [
1268 | "**Reduce Function**\n",
1269 | "\n",
1270 | "The Reduce Function `reduce()` :\n",
1271 | "\n",
1272 | "* Applies same operation to items of a sequence\n",
1273 | "* Uses result of operation as first parameter of next operation\n",
1274 | "* Returns an item, not a list\n",
1275 | "\n",
1276 | " It returns a single value after applying a function on an iterable example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5)."
1277 | ]
1278 | },
1279 | {
1280 | "cell_type": "code",
1281 | "metadata": {
1282 | "id": "kk02Jihcxi7T",
1283 | "colab_type": "code",
1284 | "colab": {
1285 | "base_uri": "https://localhost:8080/",
1286 | "height": 34
1287 | },
1288 | "outputId": "5d809bd1-fd49-42d6-9909-e02cffab91cf"
1289 | },
1290 | "source": [
1291 | "from functools import reduce\n",
1292 | "data = [4,3,2,1]\n",
1293 | "reduce(lambda x,y: x * y, data)"
1294 | ],
1295 | "execution_count": null,
1296 | "outputs": [
1297 | {
1298 | "output_type": "execute_result",
1299 | "data": {
1300 | "text/plain": [
1301 | "24"
1302 | ]
1303 | },
1304 | "metadata": {
1305 | "tags": []
1306 | },
1307 | "execution_count": 76
1308 | }
1309 | ]
1310 | },
1311 | {
1312 | "cell_type": "code",
1313 | "metadata": {
1314 | "id": "eKAld0st829Q",
1315 | "colab_type": "code",
1316 | "colab": {}
1317 | },
1318 | "source": [
1319 | "#day 2 - HW\n",
1320 | "\n",
1321 | " * \n",
1322 | " * * \n",
1323 | " * * * \n",
1324 | " * * * * \n",
1325 | " * * * * * \n",
1326 | " * * * * \n",
1327 | " * * * \n",
1328 | " * * \n",
1329 | " *\n",
1330 | "\n",
1331 | "#make a function, get input from user, pass it as a parameter to the function, and print the dimond shape\n",
1332 | "\n",
1333 | "#input from the user can be anything like \"$,@,1,A\"\n"
1334 | ],
1335 | "execution_count": null,
1336 | "outputs": []
1337 | }
1338 | ]
1339 | }
--------------------------------------------------------------------------------
/Python_for_Machine_Learning.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Python for Machine Learning",
7 | "provenance": [],
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | }
14 | },
15 | "cells": [
16 | {
17 | "cell_type": "markdown",
18 | "metadata": {
19 | "id": "view-in-github",
20 | "colab_type": "text"
21 | },
22 | "source": [
23 | "
"
24 | ]
25 | },
26 | {
27 | "cell_type": "markdown",
28 | "metadata": {
29 | "id": "ei-GOj0Gu-9S"
30 | },
31 | "source": [
32 | "#Introduction to Colab for the classroom\n",
33 | "\n",
34 | "This is an \"Google Colab\" notebook. It's a shared document where everybody can see and make changes to the same document.\n",
35 | "\n",
36 | "The first few problems will get us acquainted with working together;\n",
37 | "\n",
38 | "1.Go to file in the upper left corner\n",
39 | "\n",
40 | "2.Click Save copy in drive\n",
41 | "\n",
42 | "3.Follow along and use your own notebook!"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "metadata": {
48 | "id": "1D9tQyRbuQwg"
49 | },
50 | "source": [
51 | "# This is a comment\n",
52 | "# in a code cell\n",
53 | "# Press shift + enter to execute a cell\n",
54 | "# Nothing will print from this cell since these are all comments"
55 | ],
56 | "execution_count": null,
57 | "outputs": []
58 | },
59 | {
60 | "cell_type": "markdown",
61 | "metadata": {
62 | "id": "-TjZmYl523lH"
63 | },
64 | "source": [
65 | "# **What is Python?**\n",
66 | "\n",
67 | "Python is an open-source object-oriented programming language. It first \n",
68 | "appeared in 1991 and has become extremely popular among data scientists. StackOverflow calls it as the fastest growing programming language.\n",
69 | "\n",
70 | "It is used for:\n",
71 | "\n",
72 | "* Web development\n",
73 | "* Software Development\n",
74 | "* Mathematics\n",
75 | "* System Scripting and Much more\n",
76 | "\n",
77 | "\n",
78 | "# **What Python can do?**\n",
79 | "\n",
80 | "* Python can be used on a server to create web applications.\n",
81 | "* Python can be used alongside software to create workflows.\n",
82 | "* Python can connect to database systems. It can also read and modify files.\n",
83 | "* Python can be used to handle big data and perform complex mathematics.\n",
84 | "* Python can be used for rapid prototyping, or for production-ready software development."
85 | ]
86 | },
87 | {
88 | "cell_type": "markdown",
89 | "metadata": {
90 | "id": "Jj2dwPJGxZDy"
91 | },
92 | "source": [
93 | "# Why Learn Python?\n",
94 | "Python’s syntax is very simple hence it's easy for the beginners to understand. The lines of code required for a task is less compared to other languages.\n",
95 | "\n",
96 | "\n",
97 | "* Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).\n",
98 | "* Python has a simple syntax similar to the English language.\n",
99 | "* Python has syntax that allows developers to write programs with fewer lines than some other programming languages.\n",
100 | "* Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick.\n",
101 | "* Python can be treated in a procedural way, an object-orientated way or a functional way."
102 | ]
103 | },
104 | {
105 | "cell_type": "markdown",
106 | "metadata": {
107 | "id": "XnoczNYXz1TI"
108 | },
109 | "source": [
110 | "# **Let’s look at some cool features of Python:**\n",
111 | "\n",
112 | "1. Simple and easy to learn\n",
113 | "2. Free and Open Source\n",
114 | "3. Portable\n",
115 | "4. Supports different programming paradigm\n",
116 | "5. Extensible"
117 | ]
118 | },
119 | {
120 | "cell_type": "markdown",
121 | "metadata": {
122 | "id": "tjwVKXvB0VKw"
123 | },
124 | "source": [
125 | "# **Python Applications:**\n",
126 | "\n",
127 | "1. Artificial Intelligence\n",
128 | "2. Desktop Application\n",
129 | "3. Automation\n",
130 | "4. Web Development\n",
131 | "5. Data Wrangling, Exploration\n",
132 | "6. Visualization"
133 | ]
134 | },
135 | {
136 | "cell_type": "markdown",
137 | "metadata": {
138 | "id": "huR1RLMp1FgR"
139 | },
140 | "source": [
141 | "# **Python Setup:**\n",
142 | "\n",
143 | "For Windows PC, search in the start bar for Python or run the following on the Command Line (cmd.exe):\n",
144 | "\n",
145 | "```\n",
146 | "C:\\Users\\Your Name>python --version\n",
147 | "```\n",
148 | "\n",
149 | "To check if you have python installed on a Linux or Mac, then on linux open the command line or on Mac open the Terminal and type:\n",
150 | "\n",
151 | "```\n",
152 | "python --version\n",
153 | "```\n",
154 | "\n",
155 | "\n",
156 | "---\n",
157 | "\n",
158 | "\n",
159 | "* If you are on Windows OS download Python by [clicking here](https://www.python.org/downloads/windows/) and now install from the setup and in the start menu type IDLE.IDLE, you can think it as an Python’s IDE to run the Python Scripts.\n",
160 | "* If you are on Linux/Unix-like just open the terminal and on 99% linux OS Python comes preinstalled with the OS.Just type ‘python3’ in terminal and you are ready to go.\n"
161 | ]
162 | },
163 | {
164 | "cell_type": "code",
165 | "metadata": {
166 | "id": "1bl4wCqBnDv8",
167 | "colab": {
168 | "base_uri": "https://localhost:8080/"
169 | },
170 | "outputId": "eb728101-99ce-4994-a1fd-e12c8f60386e"
171 | },
172 | "source": [
173 | "# uncomment and run this code (Rember in colab you need to ass a \"!\" before running linux commands)\r\n",
174 | "!python --version "
175 | ],
176 | "execution_count": null,
177 | "outputs": [
178 | {
179 | "output_type": "stream",
180 | "text": [
181 | "Python 3.7.10\n"
182 | ],
183 | "name": "stdout"
184 | }
185 | ]
186 | },
187 | {
188 | "cell_type": "markdown",
189 | "metadata": {
190 | "id": "cc1FDi8M26vh"
191 | },
192 | "source": [
193 | "# **Starting with Python**\n",
194 | "\n",
195 | "Python is an interpreted programming language, this means that as a developer you write Python (.py) files in a text editor and then put those files into the python interpreter to be executed.\n",
196 | "\n",
197 | "The way to run a python file is like this on the command line:\n",
198 | "\n",
199 | "\n",
200 | "\n",
201 | "```\n",
202 | "C:\\Users\\Your Name>python helloworld.py\n",
203 | "```\n",
204 | "Where \"helloworld.py\" is the name of your python file.\n",
205 | "\n",
206 | "Let's write our first Python file, called helloworld.py, which can be done in any text editor.\n",
207 | "\n",
208 | "\n",
209 | "helloworld.py\n",
210 | "```\n",
211 | "print(\"Hello, World! :) \")\n",
212 | "```\n",
213 | "Simple as that. Save your file. Open your command line, navigate to the directory where you saved your file, and run:\n",
214 | "\n",
215 | "```\n",
216 | "C:\\Users\\Your Name>python helloworld.py\n",
217 | "```\n",
218 | "\n",
219 | "The output should read:\n",
220 | "\n",
221 | "\n",
222 | "\n",
223 | "```\n",
224 | "Hello, World! :)\n",
225 | "```\n",
226 | "\n"
227 | ]
228 | },
229 | {
230 | "cell_type": "code",
231 | "metadata": {
232 | "id": "YIQ3UsYAm-kI",
233 | "colab": {
234 | "base_uri": "https://localhost:8080/"
235 | },
236 | "outputId": "5b231871-b27e-41d2-c8ab-331dc4e4e14e"
237 | },
238 | "source": [
239 | "#TO-DO Print Hello World using print command!\r\n",
240 | "\r\n",
241 | "print(\"\")"
242 | ],
243 | "execution_count": null,
244 | "outputs": [
245 | {
246 | "output_type": "stream",
247 | "text": [
248 | "\n"
249 | ],
250 | "name": "stdout"
251 | }
252 | ]
253 | },
254 | {
255 | "cell_type": "markdown",
256 | "metadata": {
257 | "id": "qViu26SV6-TK"
258 | },
259 | "source": [
260 | "# **Python Syntax & Indentation**\n",
261 | "\n",
262 | "Python syntax can be executed by writing directly in the Command Line:\n",
263 | "\n",
264 | "\n",
265 | "\n",
266 | "```\n",
267 | ">>> print(\"Hello, World! :)\")\n",
268 | "Hello, World! :)\n",
269 | "```\n",
270 | "\n",
271 | "Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:\n",
272 | "\n",
273 | "```\n",
274 | "C:\\Users\\Your Name>python myfile.py\n",
275 | "```\n",
276 | "\n",
277 | "\n",
278 | "**Python Indentation**\n",
279 | "\n",
280 | "\n",
281 | "Indentation refers to the spaces at the beginning of a code line.\n",
282 | "\n",
283 | "Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.\n",
284 | "\n",
285 | "Python uses indentation to indicate a block of code.\n",
286 | "\n",
287 | "Example :\n",
288 | "\n"
289 | ]
290 | },
291 | {
292 | "cell_type": "code",
293 | "metadata": {
294 | "id": "eR0pjRyn7Tta",
295 | "colab": {
296 | "base_uri": "https://localhost:8080/",
297 | "height": 128
298 | },
299 | "outputId": "a63f7475-4f20-4e1d-b9ba-b5845cfd3bbd"
300 | },
301 | "source": [
302 | "#indentation is imp in python below code will give you error\n",
303 | "\n",
304 | "if 5 > 2:\n",
305 | "print(\"Five is greater than two!\")"
306 | ],
307 | "execution_count": null,
308 | "outputs": [
309 | {
310 | "output_type": "error",
311 | "ename": "IndentationError",
312 | "evalue": "ignored",
313 | "traceback": [
314 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print(\"Five is greater than two!\")\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n"
315 | ]
316 | }
317 | ]
318 | },
319 | {
320 | "cell_type": "markdown",
321 | "metadata": {
322 | "id": "k0xS4nZX9Y04"
323 | },
324 | "source": [
325 | "The number of spaces is up to you as a programmer, but it has to be at least one."
326 | ]
327 | },
328 | {
329 | "cell_type": "code",
330 | "metadata": {
331 | "id": "Zq9-XLgz9cZ_",
332 | "colab": {
333 | "base_uri": "https://localhost:8080/"
334 | },
335 | "outputId": "d8fa3cbe-9b71-4d49-b207-7bff76b84d38"
336 | },
337 | "source": [
338 | "#Example (Generally we prefer a tab's space)\n",
339 | "\n",
340 | "if 10 > 5:\n",
341 | " print(\"Ten is greater than Five!\") \n",
342 | "if 10 > 5:\n",
343 | " print(\"Ten is greater than Five!\") \n",
344 | " print(\"Ten is greater than Five!\") "
345 | ],
346 | "execution_count": null,
347 | "outputs": [
348 | {
349 | "output_type": "stream",
350 | "text": [
351 | "Ten is greater than Five!\n",
352 | "Ten is greater than Five!\n",
353 | "Ten is greater than Five!\n"
354 | ],
355 | "name": "stdout"
356 | }
357 | ]
358 | },
359 | {
360 | "cell_type": "markdown",
361 | "metadata": {
362 | "id": "btd_GvHt9upn"
363 | },
364 | "source": [
365 | "# **Comments:**\n",
366 | "\n",
367 | "Python has commenting capability for the purpose of in-code documentation.\n",
368 | "\n",
369 | "Comments start with a #, and Python will render the rest of the line as a comment:"
370 | ]
371 | },
372 | {
373 | "cell_type": "code",
374 | "metadata": {
375 | "id": "a_YeED9D9x6s",
376 | "colab": {
377 | "base_uri": "https://localhost:8080/"
378 | },
379 | "outputId": "9b2aae17-5869-41c1-e679-88a7159ed5c5"
380 | },
381 | "source": [
382 | "#This is a comment, this line will not execute.\n",
383 | "\n",
384 | "print(\"Yoooo!\")"
385 | ],
386 | "execution_count": null,
387 | "outputs": [
388 | {
389 | "output_type": "stream",
390 | "text": [
391 | "Yoooo!\n"
392 | ],
393 | "name": "stdout"
394 | }
395 | ]
396 | },
397 | {
398 | "cell_type": "markdown",
399 | "metadata": {
400 | "id": "pft3WfTe-LUe"
401 | },
402 | "source": [
403 | "* Comments can be used to explain Python code.\n",
404 | "\n",
405 | "* Comments can be used to make the code more readable.\n",
406 | "* Comments can be used to prevent execution when testing code.\n",
407 | "* In `Colab` we have a option to comment a block using `ctrl+/`"
408 | ]
409 | },
410 | {
411 | "cell_type": "code",
412 | "metadata": {
413 | "id": "B77HLpYIoIXN",
414 | "colab": {
415 | "base_uri": "https://localhost:8080/"
416 | },
417 | "outputId": "63e7c6e2-6ba2-41e8-b967-3455fbbc26db"
418 | },
419 | "source": [
420 | "print(\"Comment in colab line 1\")\r\n",
421 | "print(\"Comment in colab line 2\")\r\n",
422 | "print(\"Comment in colab line 3\")\r\n",
423 | "print(\"select all the lines and then do crtl+/ to comment and again to reverse back\")"
424 | ],
425 | "execution_count": null,
426 | "outputs": [
427 | {
428 | "output_type": "stream",
429 | "text": [
430 | "Comment in colab line 1\n",
431 | "Comment in colab line 2\n",
432 | "Comment in colab line 3\n",
433 | "select all the lines and then do crtl+/ to comment and again to reverse back\n"
434 | ],
435 | "name": "stdout"
436 | }
437 | ]
438 | },
439 | {
440 | "cell_type": "markdown",
441 | "metadata": {
442 | "id": "MiCtOU0F-uLN"
443 | },
444 | "source": [
445 | "# **Variables in Python:**\n",
446 | "\n",
447 | "Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.\n",
448 | "\n",
449 | "In Python you don’t need to declare variables before using it, unlike other languages like Java, C etc.\n",
450 | "\n",
451 | "# **Assigning values to a variable:**\n",
452 | "\n",
453 | "Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. \n",
454 | "The equal sign (=) is used to assign values to variables."
455 | ]
456 | },
457 | {
458 | "cell_type": "code",
459 | "metadata": {
460 | "id": "WOgTcEk2_J21",
461 | "colab": {
462 | "base_uri": "https://localhost:8080/"
463 | },
464 | "outputId": "482bb554-6c33-48e5-b674-ce220231c2eb"
465 | },
466 | "source": [
467 | "x = 11 \n",
468 | "y = \"Google\" \n",
469 | "print(x)\n",
470 | "print(y)"
471 | ],
472 | "execution_count": null,
473 | "outputs": [
474 | {
475 | "output_type": "stream",
476 | "text": [
477 | "11\n",
478 | "Google\n"
479 | ],
480 | "name": "stdout"
481 | }
482 | ]
483 | },
484 | {
485 | "cell_type": "markdown",
486 | "metadata": {
487 | "id": "1RlM5twW_VQB"
488 | },
489 | "source": [
490 | "Variables do not need to be declared with any particular type and can even change type after they have been set."
491 | ]
492 | },
493 | {
494 | "cell_type": "code",
495 | "metadata": {
496 | "id": "fo-8MR7q_b-o",
497 | "colab": {
498 | "base_uri": "https://localhost:8080/"
499 | },
500 | "outputId": "4c16f97d-236d-4875-95b1-2c5455acdc1d"
501 | },
502 | "source": [
503 | "x = 4 # x is of type int\n",
504 | "x = \"Frost forbite\" # x is now of type str\n",
505 | "print(x)"
506 | ],
507 | "execution_count": null,
508 | "outputs": [
509 | {
510 | "output_type": "stream",
511 | "text": [
512 | "Frost forbite\n"
513 | ],
514 | "name": "stdout"
515 | }
516 | ]
517 | },
518 | {
519 | "cell_type": "markdown",
520 | "metadata": {
521 | "id": "N09PcvZvFPt9"
522 | },
523 | "source": [
524 | "String variables can be declared either by using single or double quotes:"
525 | ]
526 | },
527 | {
528 | "cell_type": "code",
529 | "metadata": {
530 | "id": "CGG7Pza5FQzg"
531 | },
532 | "source": [
533 | "x = \"Marcus Burns\"\n",
534 | "# is the same as\n",
535 | "x = 'Marcus Burns'\n",
536 | "y = 10"
537 | ],
538 | "execution_count": null,
539 | "outputs": []
540 | },
541 | {
542 | "cell_type": "markdown",
543 | "metadata": {
544 | "id": "H_tMoZ7toprY"
545 | },
546 | "source": [
547 | "At any point if you want to see the `type of the variable` like `int` or `str` use `type()` function."
548 | ]
549 | },
550 | {
551 | "cell_type": "code",
552 | "metadata": {
553 | "id": "KyXGQZQkoo5q",
554 | "colab": {
555 | "base_uri": "https://localhost:8080/"
556 | },
557 | "outputId": "d5d3386c-7aae-436d-c9dc-d27ee82696b5"
558 | },
559 | "source": [
560 | "type(x) # replace \"x\" with \"y\" and see its type"
561 | ],
562 | "execution_count": null,
563 | "outputs": [
564 | {
565 | "output_type": "execute_result",
566 | "data": {
567 | "text/plain": [
568 | "str"
569 | ]
570 | },
571 | "metadata": {
572 | "tags": []
573 | },
574 | "execution_count": 16
575 | }
576 | ]
577 | },
578 | {
579 | "cell_type": "markdown",
580 | "metadata": {
581 | "id": "_SR0saT9KvwE"
582 | },
583 | "source": [
584 | "# **Assign Value to Multiple Variables:**\n",
585 | "\n",
586 | "Python allows you to assign values to multiple variables in one line:"
587 | ]
588 | },
589 | {
590 | "cell_type": "code",
591 | "metadata": {
592 | "id": "tTsMoY7MK1wx",
593 | "colab": {
594 | "base_uri": "https://localhost:8080/"
595 | },
596 | "outputId": "ae118ee7-238d-492d-c35f-756f02aea2be"
597 | },
598 | "source": [
599 | "x, y, z = \"Apples\", \"Grapes\", \"Mangoes\" #very useful in spliting the data\n",
600 | "print(x)\n",
601 | "print(y)\n",
602 | "print(z)"
603 | ],
604 | "execution_count": null,
605 | "outputs": [
606 | {
607 | "output_type": "stream",
608 | "text": [
609 | "Apples\n",
610 | "Grapes\n",
611 | "Mangoes\n"
612 | ],
613 | "name": "stdout"
614 | }
615 | ]
616 | },
617 | {
618 | "cell_type": "markdown",
619 | "metadata": {
620 | "id": "TyJ-roCOK9zT"
621 | },
622 | "source": [
623 | "And you can assign the same value to multiple variables in one line:"
624 | ]
625 | },
626 | {
627 | "cell_type": "code",
628 | "metadata": {
629 | "id": "AC-SxzcVK_6J",
630 | "colab": {
631 | "base_uri": "https://localhost:8080/"
632 | },
633 | "outputId": "b2be17d9-adf2-4b0c-d742-b717c66cae5c"
634 | },
635 | "source": [
636 | "x = y = z = \"Kiwi\"\n",
637 | "print(x)\n",
638 | "print(y)\n",
639 | "print(z)"
640 | ],
641 | "execution_count": null,
642 | "outputs": [
643 | {
644 | "output_type": "stream",
645 | "text": [
646 | "Kiwi\n",
647 | "Kiwi\n",
648 | "Kiwi\n"
649 | ],
650 | "name": "stdout"
651 | }
652 | ]
653 | },
654 | {
655 | "cell_type": "markdown",
656 | "metadata": {
657 | "id": "pULZk2i3OpH8"
658 | },
659 | "source": [
660 | "# **Data Types in Python:**\n",
661 | "Python supports various data types, these data types defines the operations possible on the variables and the storage method. \n",
662 | "\n",
663 | "\n",
664 | "* Text Type:\t`str`\n",
665 | "* Numeric Types:\t`int, float, complex`\n",
666 | "* Sequence Types:\t`list, tuple, range`\n",
667 | "* Mapping Type:\t`dict`\n",
668 | "* Set Types:\t`set, frozenset`\n",
669 | "* Boolean Type:\t`bool`\n",
670 | "* Binary Types:\t`bytes, bytearray, memoryview`"
671 | ]
672 | },
673 | {
674 | "cell_type": "markdown",
675 | "metadata": {
676 | "id": "QVlmz3KzOvEv"
677 | },
678 | "source": [
679 | "# **Getting the Data Type:**\n",
680 | "\n",
681 | "You can get the data type of any object by using the type() function:"
682 | ]
683 | },
684 | {
685 | "cell_type": "code",
686 | "metadata": {
687 | "id": "ZN5P37qLOwYW",
688 | "colab": {
689 | "base_uri": "https://localhost:8080/"
690 | },
691 | "outputId": "86231f02-4361-41a8-b65e-8c7877011efc"
692 | },
693 | "source": [
694 | "x = 5\n",
695 | "print(type(x))"
696 | ],
697 | "execution_count": null,
698 | "outputs": [
699 | {
700 | "output_type": "stream",
701 | "text": [
702 | "\n"
703 | ],
704 | "name": "stdout"
705 | }
706 | ]
707 | },
708 | {
709 | "cell_type": "markdown",
710 | "metadata": {
711 | "id": "JmJ0drEEO7Sp"
712 | },
713 | "source": [
714 | "**Numeric:**\n",
715 | "\n",
716 | "Just as expected Numeric data types store numeric values. They are immutable data types, this means that you cannot change it’s value. Python supports three different Numeric data types:\n",
717 | "\n",
718 | "1. Integer type: It holds all the integer values i.e. all the positive and negative whole numbers, example – 10.\n",
719 | "\n",
720 | "2. Float type: It holds the real numbers and are represented by decimal and sometimes even scientific notations with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250), example – 10.24.\n",
721 | "\n",
722 | "3. Complex type: These are of the form a + bj, where a and b are floats and J represents the square root of -1 (which is an imaginary number), example – 10+6j.\n",
723 | "\n",
724 | "Now you can even perform type conversion. For example, you can convert the integer value to a float value and vice-versa."
725 | ]
726 | },
727 | {
728 | "cell_type": "code",
729 | "metadata": {
730 | "id": "860P3--YPFvM",
731 | "colab": {
732 | "base_uri": "https://localhost:8080/"
733 | },
734 | "outputId": "89046cd3-79e8-436a-afb3-67632c7db494"
735 | },
736 | "source": [
737 | "x = 11 # int\n",
738 | "y = 2.8 # float\n",
739 | "\n",
740 | "print(type(x))\n",
741 | "print(type(y))\n",
742 | "print(type(z))"
743 | ],
744 | "execution_count": null,
745 | "outputs": [
746 | {
747 | "output_type": "stream",
748 | "text": [
749 | "\n",
750 | "\n",
751 | "\n"
752 | ],
753 | "name": "stdout"
754 | }
755 | ]
756 | },
757 | {
758 | "cell_type": "markdown",
759 | "metadata": {
760 | "id": "tMCB-d5SPPcz"
761 | },
762 | "source": [
763 | "**Type Conversion**\n",
764 | "\n",
765 | "You can convert from one type to another with the int(), float(), and complex() methods:"
766 | ]
767 | },
768 | {
769 | "cell_type": "code",
770 | "metadata": {
771 | "id": "jLEAczK2Pf3_",
772 | "colab": {
773 | "base_uri": "https://localhost:8080/"
774 | },
775 | "outputId": "e492ee83-017b-455e-ec93-4d2aeb3add1c"
776 | },
777 | "source": [
778 | "# we usually use int conversion for some values in ML\n",
779 | "a = 8.16\n",
780 | "# Convert it into int type\n",
781 | "b = int(a)\n",
782 | "print(b)"
783 | ],
784 | "execution_count": null,
785 | "outputs": [
786 | {
787 | "output_type": "stream",
788 | "text": [
789 | "8\n"
790 | ],
791 | "name": "stdout"
792 | }
793 | ]
794 | },
795 | {
796 | "cell_type": "markdown",
797 | "metadata": {
798 | "id": "uevK81TSPqiU"
799 | },
800 | "source": [
801 | "**Strings:**\n",
802 | "\n",
803 | "Strings are amongst the most popular data types in Python. We can create them simply by enclosing characters in quotes. Python treats single and double quotes in exactly the same fashion. "
804 | ]
805 | },
806 | {
807 | "cell_type": "markdown",
808 | "metadata": {
809 | "id": "PFRQnE8vPswE"
810 | },
811 | "source": [
812 | "**String Methods**\n",
813 | "\n",
814 | "Python has a set of built-in methods that you can use on strings.\n",
815 | "\n",
816 | "* capitalize() : Converts the first character to upper case\n",
817 | "* casefold() :\tConverts string into lower case\n",
818 | "* count() :\tReturns the number of times a specified value occurs in a string\n",
819 | "* encode()\t: Returns an encoded version of the string\n",
820 | "* find() :\tSearches the string for a specified value and returns the position of where it was found\n",
821 | "* format() :\tFormats specified values in a string\n",
822 | "* index()\t: Searches the string for a specified value and returns the position of where it was found\n",
823 | "* isdecimal() :\tReturns True if all characters in the string are decimals\n",
824 | "* isdigit() :\tReturns True if all characters in the string are digits\n",
825 | "* isidentifier() :\tReturns True if the string is an identifier\n",
826 | "* islower()\t: Returns True if all characters in the string are lower case\n",
827 | "* isnumeric()\t: Returns True if all characters in the string are numeric\n",
828 | "* isupper()\t: Returns True if all characters in the string are upper case\n",
829 | "* lower() :\tConverts a string into lower case\n",
830 | "* upper()\t: Converts a string into upper case"
831 | ]
832 | },
833 | {
834 | "cell_type": "markdown",
835 | "metadata": {
836 | "id": "URny1DMeQEUF"
837 | },
838 | "source": [
839 | "**Strings are Arrays**\n",
840 | "\n",
841 | "Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters.\n",
842 | "\n",
843 | "However, Python does not have a character data type, a single character is simply a string with a length of 1.\n",
844 | "\n",
845 | "Square brackets can be used to access elements of the string."
846 | ]
847 | },
848 | {
849 | "cell_type": "code",
850 | "metadata": {
851 | "id": "d32_CS-LQLO9",
852 | "colab": {
853 | "base_uri": "https://localhost:8080/",
854 | "height": 33
855 | },
856 | "outputId": "2df018a4-f5f1-4a86-f1ca-86cc53d5e612"
857 | },
858 | "source": [
859 | "#Get the character at position 1 (remember that the first character has the position 0):\n",
860 | "\n",
861 | "a = \"Hello, World!\"\n",
862 | "print(a[1])"
863 | ],
864 | "execution_count": null,
865 | "outputs": [
866 | {
867 | "output_type": "stream",
868 | "text": [
869 | "e\n"
870 | ],
871 | "name": "stdout"
872 | }
873 | ]
874 | },
875 | {
876 | "cell_type": "code",
877 | "metadata": {
878 | "id": "J3T1vjztQhRo",
879 | "colab": {
880 | "base_uri": "https://localhost:8080/",
881 | "height": 134
882 | },
883 | "outputId": "61335e11-0477-4244-a6d7-7ddbcd131ecc"
884 | },
885 | "source": [
886 | "a = \"Hello World!\"\n",
887 | "b = 'Welcome'\n",
888 | "\n",
889 | "#operations on Strings-*\n",
890 | "\n",
891 | "print (len(b))\t #String Length\n",
892 | "print (a.index('e')) #Locate a character in String\n",
893 | "print (a[1:5])\t #Slicing\n",
894 | "print (a[::-1])\t #Reverse a String\n",
895 | "print (b.upper())\t #Convert the letters in a String to upper-case\n",
896 | "\n",
897 | "#TO-DO Convert \"a\" to lower case \n",
898 | "\n",
899 | "\n",
900 | "#TO-DO print reverse of b\n",
901 | "\n"
902 | ],
903 | "execution_count": null,
904 | "outputs": [
905 | {
906 | "output_type": "stream",
907 | "text": [
908 | "7\n",
909 | "1\n",
910 | "3\n",
911 | "ello\n",
912 | "!dlroW olleH\n",
913 | "WELCOME\n",
914 | "welcome\n"
915 | ],
916 | "name": "stdout"
917 | }
918 | ]
919 | },
920 | {
921 | "cell_type": "markdown",
922 | "metadata": {
923 | "id": "KWC2_vcGRCig"
924 | },
925 | "source": [
926 | "**List:**\n",
927 | "\n",
928 | "You can consider the Lists as Arrays in C, but in List you can store elements of different types, but in Array all the elements should of the same type.\n",
929 | "List is the most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets."
930 | ]
931 | },
932 | {
933 | "cell_type": "markdown",
934 | "metadata": {
935 | "id": "CZjaRi7jRHxs"
936 | },
937 | "source": [
938 | "**List Methods**\n",
939 | "\n",
940 | "Python has a set of built-in methods that you can use on lists.\n",
941 | "\n",
942 | "\n",
943 | "* append() : Adds an element at the end of the list\n",
944 | "* clear()\t: Removes all the elements from the list\n",
945 | "* copy() :\tReturns a copy of the list\n",
946 | "* count() :\tReturns the number of elements with the specified value\n",
947 | "* extend(): Add the elements of a list (or any iterable), to the end of the current list\n",
948 | "* index()\t: Returns the index of the first element with the specified value\n",
949 | "* insert() :\tAdds an element at the specified position\n",
950 | "* pop() :\tRemoves the element at the specified position\n",
951 | "* remove() :\tRemoves the item with the specified value\n",
952 | "* reverse() :\tReverses the order of the list\n",
953 | "* sort() :\tSorts the list"
954 | ]
955 | },
956 | {
957 | "cell_type": "code",
958 | "metadata": {
959 | "id": "0XmYBMyGRS62",
960 | "colab": {
961 | "base_uri": "https://localhost:8080/"
962 | },
963 | "outputId": "0f933e6d-1f1f-457b-982e-d6490e803430"
964 | },
965 | "source": [
966 | "Sports = ['Basketball', 'Cricket', 'Football', 1,5,6.0] #unlike array in c list in pyhthoin can store any data type\n",
967 | "print(Sports)"
968 | ],
969 | "execution_count": null,
970 | "outputs": [
971 | {
972 | "output_type": "stream",
973 | "text": [
974 | "['Basketball', 'Cricket', 'Football', 1, 5, 6.0]\n"
975 | ],
976 | "name": "stdout"
977 | }
978 | ]
979 | },
980 | {
981 | "cell_type": "markdown",
982 | "metadata": {
983 | "id": "8TGv1lb4sOko"
984 | },
985 | "source": [
986 | "## Operations on lists\r\n"
987 | ]
988 | },
989 | {
990 | "cell_type": "code",
991 | "metadata": {
992 | "id": "AJMM4LoEsMYT",
993 | "colab": {
994 | "base_uri": "https://localhost:8080/"
995 | },
996 | "outputId": "8a13a113-c553-4a2d-b9af-3699c4c3237c"
997 | },
998 | "source": [
999 | "print (Sports[0]) #printing the value at index"
1000 | ],
1001 | "execution_count": null,
1002 | "outputs": [
1003 | {
1004 | "output_type": "stream",
1005 | "text": [
1006 | "Basketball\n"
1007 | ],
1008 | "name": "stdout"
1009 | }
1010 | ]
1011 | },
1012 | {
1013 | "cell_type": "code",
1014 | "metadata": {
1015 | "id": "BTvaBT1msTTt"
1016 | },
1017 | "source": [
1018 | "#TO-DO Print first three elements using the slicing we learnt earlier\r\n",
1019 | "\r\n",
1020 | "print (Sports{[:]}) "
1021 | ],
1022 | "execution_count": null,
1023 | "outputs": []
1024 | },
1025 | {
1026 | "cell_type": "code",
1027 | "metadata": {
1028 | "id": "JKyk4NHksU1f",
1029 | "colab": {
1030 | "base_uri": "https://localhost:8080/"
1031 | },
1032 | "outputId": "e1be3db1-1178-46ba-8483-c44adb9051c3"
1033 | },
1034 | "source": [
1035 | "Sports[2]='baseball' #replacing the value at particular index\r\n",
1036 | "\r\n",
1037 | "print (Sports)\r\n"
1038 | ],
1039 | "execution_count": null,
1040 | "outputs": [
1041 | {
1042 | "output_type": "stream",
1043 | "text": [
1044 | "['Basketball', 'Rugby', 'baseball', 5, 6.0]\n"
1045 | ],
1046 | "name": "stdout"
1047 | }
1048 | ]
1049 | },
1050 | {
1051 | "cell_type": "code",
1052 | "metadata": {
1053 | "id": "mq3inSA0sYeW",
1054 | "colab": {
1055 | "base_uri": "https://localhost:8080/"
1056 | },
1057 | "outputId": "ec64802f-85b5-4156-ecb8-28c62c423bf9"
1058 | },
1059 | "source": [
1060 | "del Sports[1] #deleting a value at an index\r\n",
1061 | " \r\n",
1062 | "print (Sports)"
1063 | ],
1064 | "execution_count": null,
1065 | "outputs": [
1066 | {
1067 | "output_type": "stream",
1068 | "text": [
1069 | "['Basketball', 'baseball', 5, 6.0]\n"
1070 | ],
1071 | "name": "stdout"
1072 | }
1073 | ]
1074 | },
1075 | {
1076 | "cell_type": "code",
1077 | "metadata": {
1078 | "id": "g3ldEjkKsaZo",
1079 | "colab": {
1080 | "base_uri": "https://localhost:8080/"
1081 | },
1082 | "outputId": "1d5a5c58-370c-48ec-faee-058dedb5514e"
1083 | },
1084 | "source": [
1085 | "print (len(Sports)) #get the length of the string"
1086 | ],
1087 | "execution_count": null,
1088 | "outputs": [
1089 | {
1090 | "output_type": "stream",
1091 | "text": [
1092 | "4\n"
1093 | ],
1094 | "name": "stdout"
1095 | }
1096 | ]
1097 | },
1098 | {
1099 | "cell_type": "markdown",
1100 | "metadata": {
1101 | "id": "a8lTMliwTZYa"
1102 | },
1103 | "source": [
1104 | "**Negative Indexing**\n",
1105 | "\n",
1106 | "Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc."
1107 | ]
1108 | },
1109 | {
1110 | "cell_type": "code",
1111 | "metadata": {
1112 | "id": "IM25puDcTaxu",
1113 | "colab": {
1114 | "base_uri": "https://localhost:8080/"
1115 | },
1116 | "outputId": "f1b1119e-df06-4291-cb50-9be867a106c9"
1117 | },
1118 | "source": [
1119 | "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
1120 | "print(fruits[-1])\n",
1121 | "\n",
1122 | "#TO-DO Print apple using negative indexing\n",
1123 | "\n"
1124 | ],
1125 | "execution_count": null,
1126 | "outputs": [
1127 | {
1128 | "output_type": "stream",
1129 | "text": [
1130 | "cherry\n"
1131 | ],
1132 | "name": "stdout"
1133 | }
1134 | ]
1135 | },
1136 | {
1137 | "cell_type": "markdown",
1138 | "metadata": {
1139 | "id": "BG33LT1jWVxD"
1140 | },
1141 | "source": [
1142 | "**Dictionary Methods**\n",
1143 | "\n",
1144 | "Python has a set of built-in methods that you can use on dictionaries.\n",
1145 | "\n",
1146 | "* clear() :\tRemoves all the elements from the dictionary\n",
1147 | "* copy()\t: Returns a copy of the dictionary\n",
1148 | "* get()\t: Returns the value of the specified key\n",
1149 | "* items()\t: Returns a list containing a tuple for each key value pair\n",
1150 | "* keys()\t: Returns a list containing the dictionary's keys\n",
1151 | "* pop()\t: Removes the element with the specified key\n",
1152 | "* popitem() :\tRemoves the last inserted key-value pair\n",
1153 | "* update() :\tUpdates the dictionary with the specified key-value pairs\n",
1154 | "* values() :\tReturns a list of all the values in the dictionary"
1155 | ]
1156 | },
1157 | {
1158 | "cell_type": "code",
1159 | "metadata": {
1160 | "id": "XBVUOI6mWbW5",
1161 | "colab": {
1162 | "base_uri": "https://localhost:8080/"
1163 | },
1164 | "outputId": "7a40c4cd-c876-4079-8e50-89067251ec9a"
1165 | },
1166 | "source": [
1167 | "family = {\n",
1168 | " \"child1\" : {\n",
1169 | " \"name\" : \"Walter White\",\n",
1170 | " \"year\" : 2004\n",
1171 | " },\n",
1172 | " \"child2\" : {\n",
1173 | " \"name\" : \"Jesse Pinkman\",\n",
1174 | " \"year\" : 2007\n",
1175 | " },\n",
1176 | " \"child3\" : {\n",
1177 | " \"name\" : \"Harvey Specter\",\n",
1178 | " \"year\" : 2011\n",
1179 | " }\n",
1180 | "}\n",
1181 | "\n",
1182 | "print(family)"
1183 | ],
1184 | "execution_count": null,
1185 | "outputs": [
1186 | {
1187 | "output_type": "stream",
1188 | "text": [
1189 | "{'child1': {'name': 'Walter White', 'year': 2004}, 'child2': {'name': 'Jesse Pinkman', 'year': 2007}, 'child3': {'name': 'Harvey Specter', 'year': 2011}}\n"
1190 | ],
1191 | "name": "stdout"
1192 | }
1193 | ]
1194 | },
1195 | {
1196 | "cell_type": "code",
1197 | "metadata": {
1198 | "id": "Ir6lXKCmu1Uo"
1199 | },
1200 | "source": [
1201 | "# TO-DO make a dictionay of people sitting beside you and print it\r\n",
1202 | "\r\n",
1203 | "neighbor = {}"
1204 | ],
1205 | "execution_count": null,
1206 | "outputs": []
1207 | },
1208 | {
1209 | "cell_type": "markdown",
1210 | "metadata": {
1211 | "id": "tvslFLCWZ1bH"
1212 | },
1213 | "source": [
1214 | "Operators in Python:\n",
1215 | "\n",
1216 | "Operators are the constructs which can manipulate the values of the operands. Consider the expression 2 + 3 = 5, here 2 and 3 are operands and + is called operator.\n",
1217 | "\n",
1218 | "*Types of Operators in Python: *\n",
1219 | "\n",
1220 | "Arithmetic\n",
1221 | "\n",
1222 | "Comparision\n",
1223 | "\n",
1224 | "Logical\n",
1225 | "\n",
1226 | "Assignment\n",
1227 | "\n",
1228 | "Bitwise"
1229 | ]
1230 | },
1231 | {
1232 | "cell_type": "markdown",
1233 | "metadata": {
1234 | "id": "RIeUyG_4Z__-"
1235 | },
1236 | "source": [
1237 | "Arithmetic Operation:\n",
1238 | "\n",
1239 | "These Operators are used to perform mathematical operations like addition, subtraction etc.\n",
1240 | "\n"
1241 | ]
1242 | },
1243 | {
1244 | "cell_type": "code",
1245 | "metadata": {
1246 | "id": "cSkXbDXiaCJ2",
1247 | "colab": {
1248 | "base_uri": "https://localhost:8080/",
1249 | "height": 117
1250 | },
1251 | "outputId": "998ea758-61e7-4381-a8e9-4664cf4123ff"
1252 | },
1253 | "source": [
1254 | "a = 11\n",
1255 | "b = 20\n",
1256 | "c = 0\n",
1257 | " \n",
1258 | "c = a + b\n",
1259 | "print ( c )\n",
1260 | " \n",
1261 | "c = a - b\n",
1262 | "print ( c )\n",
1263 | " \n",
1264 | "c = a * b\n",
1265 | "print ( c )\n",
1266 | " \n",
1267 | "c = a / b\n",
1268 | "print ( c )\n",
1269 | " \n",
1270 | "c = b % a\n",
1271 | "print ( c )\n",
1272 | "\n",
1273 | "a = 2\n",
1274 | "b = 3\n",
1275 | "c = a ** b\n",
1276 | "print ( c )"
1277 | ],
1278 | "execution_count": null,
1279 | "outputs": [
1280 | {
1281 | "output_type": "stream",
1282 | "text": [
1283 | "31\n",
1284 | "-9\n",
1285 | "220\n",
1286 | "0.55\n",
1287 | "9\n",
1288 | "8\n"
1289 | ],
1290 | "name": "stdout"
1291 | }
1292 | ]
1293 | },
1294 | {
1295 | "cell_type": "markdown",
1296 | "metadata": {
1297 | "id": "UiO2-g1laJrO"
1298 | },
1299 | "source": [
1300 | "Comparision Operation:\n",
1301 | "\n",
1302 | "These Operators compare the values on either sides of them and decide the relation among them."
1303 | ]
1304 | },
1305 | {
1306 | "cell_type": "code",
1307 | "metadata": {
1308 | "id": "_RKwhzvWaNRu",
1309 | "colab": {
1310 | "base_uri": "https://localhost:8080/",
1311 | "height": 117
1312 | },
1313 | "outputId": "fa210382-cc30-4838-f666-c336759b41b8"
1314 | },
1315 | "source": [
1316 | "a = 11\n",
1317 | "b = 20\n",
1318 | "result = (a == b)\n",
1319 | "\n",
1320 | "print(result)\n",
1321 | "\n",
1322 | "result = (a != b)\n",
1323 | "\n",
1324 | "print(result)\n",
1325 | "\n",
1326 | "result = ( a < b )\n",
1327 | "\n",
1328 | "print(result)\n",
1329 | " \n",
1330 | "result = ( a > b )\n",
1331 | "\n",
1332 | "print(result)\n",
1333 | "\n",
1334 | "result = ( a <= b )\n",
1335 | "\n",
1336 | "print(result)\n",
1337 | "\n",
1338 | "type(result)\n"
1339 | ],
1340 | "execution_count": null,
1341 | "outputs": [
1342 | {
1343 | "output_type": "stream",
1344 | "text": [
1345 | "False\n",
1346 | "True\n",
1347 | "True\n",
1348 | "False\n",
1349 | "True\n"
1350 | ],
1351 | "name": "stdout"
1352 | },
1353 | {
1354 | "output_type": "execute_result",
1355 | "data": {
1356 | "text/plain": [
1357 | "bool"
1358 | ]
1359 | },
1360 | "metadata": {
1361 | "tags": []
1362 | },
1363 | "execution_count": 64
1364 | }
1365 | ]
1366 | },
1367 | {
1368 | "cell_type": "code",
1369 | "metadata": {
1370 | "id": "oXcDjDAAsHsh",
1371 | "colab": {
1372 | "base_uri": "https://localhost:8080/",
1373 | "height": 33
1374 | },
1375 | "outputId": "d668fdc3-70c9-442c-de2e-2fcb30f83fde"
1376 | },
1377 | "source": [
1378 | "result = ( a >= b)\n",
1379 | "result"
1380 | ],
1381 | "execution_count": null,
1382 | "outputs": [
1383 | {
1384 | "output_type": "execute_result",
1385 | "data": {
1386 | "text/plain": [
1387 | "False"
1388 | ]
1389 | },
1390 | "metadata": {
1391 | "tags": []
1392 | },
1393 | "execution_count": 66
1394 | }
1395 | ]
1396 | },
1397 | {
1398 | "cell_type": "markdown",
1399 | "metadata": {
1400 | "id": "tTilkmuJYRLR"
1401 | },
1402 | "source": [
1403 | "# Logical Operators\n",
1404 | "* ( and ) to check if the LHS and RHS conditions both conditions are satisfied. Basically checking for truthy values\n",
1405 | "* ( or ) to check if atleast one of the side satisfy the condition. Basically checking for truthy values\n",
1406 | "* ( not ) to check if the condition is not satified. Basically checking for falsy values"
1407 | ]
1408 | },
1409 | {
1410 | "cell_type": "code",
1411 | "metadata": {
1412 | "id": "yktGQJDnaVtv",
1413 | "colab": {
1414 | "base_uri": "https://localhost:8080/",
1415 | "height": 67
1416 | },
1417 | "outputId": "5d185f5f-4faa-4fc8-9902-b131e01b946b"
1418 | },
1419 | "source": [
1420 | "x = True\n",
1421 | "y = False\n",
1422 | " \n",
1423 | "print('x and y is',x and y)\n",
1424 | " \n",
1425 | "print('x or y is',x or y)\n",
1426 | " \n",
1427 | "print('not x is',not x)"
1428 | ],
1429 | "execution_count": null,
1430 | "outputs": [
1431 | {
1432 | "output_type": "stream",
1433 | "text": [
1434 | "x and y is False\n",
1435 | "x or y is True\n",
1436 | "not x is False\n"
1437 | ],
1438 | "name": "stdout"
1439 | }
1440 | ]
1441 | },
1442 | {
1443 | "cell_type": "markdown",
1444 | "metadata": {
1445 | "id": "1E1lQlM6YZEn"
1446 | },
1447 | "source": [
1448 | "# **Input Output:**\n",
1449 | "Take input from the user and work with it"
1450 | ]
1451 | },
1452 | {
1453 | "cell_type": "code",
1454 | "metadata": {
1455 | "id": "XNUhYjMwegiX",
1456 | "colab": {
1457 | "base_uri": "https://localhost:8080/",
1458 | "height": 70
1459 | },
1460 | "outputId": "5df88946-3872-41b1-9392-9d58b79be435"
1461 | },
1462 | "source": [
1463 | "my_age = input(\"Enter your age: \")\n",
1464 | "\n",
1465 | "# add code here \n",
1466 | "\n",
1467 | "# end code here\n",
1468 | "\n",
1469 | "add_age = my_age + 2 #It will throw an error\n",
1470 | "\n",
1471 | "#lets check why?\n",
1472 | "type(my_age)\n",
1473 | "\n",
1474 | "#Lets fix this;\n",
1475 | "\n",
1476 | "# TO-DO Convert my_age to int using int() \n",
1477 | "\n",
1478 | "\n"
1479 | ],
1480 | "execution_count": null,
1481 | "outputs": [
1482 | {
1483 | "output_type": "stream",
1484 | "text": [
1485 | "Enter your age: 10\n"
1486 | ],
1487 | "name": "stdout"
1488 | },
1489 | {
1490 | "output_type": "execute_result",
1491 | "data": {
1492 | "text/plain": [
1493 | "str"
1494 | ]
1495 | },
1496 | "metadata": {
1497 | "tags": []
1498 | },
1499 | "execution_count": 25
1500 | }
1501 | ]
1502 | },
1503 | {
1504 | "cell_type": "markdown",
1505 | "metadata": {
1506 | "id": "9QWIlKu6vnLl"
1507 | },
1508 | "source": [
1509 | "## Decision making and loops"
1510 | ]
1511 | },
1512 | {
1513 | "cell_type": "code",
1514 | "metadata": {
1515 | "id": "jjuPYlcqzPiz"
1516 | },
1517 | "source": [
1518 | "# don't run will throw error\r\n",
1519 | "\r\n",
1520 | "if condition 1\r\n",
1521 | " perform task1\r\n",
1522 | "else \r\n",
1523 | " perform task2"
1524 | ],
1525 | "execution_count": null,
1526 | "outputs": []
1527 | },
1528 | {
1529 | "cell_type": "markdown",
1530 | "metadata": {
1531 | "id": "G8H6QPmlwnrK"
1532 | },
1533 | "source": [
1534 | "Example\r\n",
1535 | "\r\n",
1536 | "Take input and see if its even or odd"
1537 | ]
1538 | },
1539 | {
1540 | "cell_type": "code",
1541 | "metadata": {
1542 | "id": "qZuL2AEAwlaT",
1543 | "colab": {
1544 | "base_uri": "https://localhost:8080/"
1545 | },
1546 | "outputId": "f5952ceb-dc90-4592-cb30-f5b58a588765"
1547 | },
1548 | "source": [
1549 | "# TO-DO take input from the user and store in \"number\" variable\r\n",
1550 | "\r\n",
1551 | "\r\n",
1552 | "# TO-DO Convert it to int\r\n",
1553 | "\r\n",
1554 | "# if else logic here\r\n",
1555 | "if (number%2 ==0):\r\n",
1556 | " print(\"Event Number\")\r\n",
1557 | "else:\r\n",
1558 | " print(\"Odd Number\")\r\n"
1559 | ],
1560 | "execution_count": null,
1561 | "outputs": [
1562 | {
1563 | "output_type": "stream",
1564 | "text": [
1565 | "Event Number\n"
1566 | ],
1567 | "name": "stdout"
1568 | }
1569 | ]
1570 | },
1571 | {
1572 | "cell_type": "markdown",
1573 | "metadata": {
1574 | "id": "sT0Io0tJxXTL"
1575 | },
1576 | "source": [
1577 | "Example of 3 or more conditions"
1578 | ]
1579 | },
1580 | {
1581 | "cell_type": "code",
1582 | "metadata": {
1583 | "id": "9ZkdGfwMxL_a"
1584 | },
1585 | "source": [
1586 | "# TO-DO Take input from user and store it in \"number\" variable\r\n",
1587 | "\r\n",
1588 | "\r\n",
1589 | "if number == 1:\r\n",
1590 | " print('Task 1')\r\n",
1591 | "elif number == 2: # we don't write else if we write elif here\r\n",
1592 | " print('Task 2')\r\n",
1593 | "else:\r\n",
1594 | " print('Task 3')"
1595 | ],
1596 | "execution_count": null,
1597 | "outputs": []
1598 | },
1599 | {
1600 | "cell_type": "markdown",
1601 | "metadata": {
1602 | "id": "3vXrg8CGyDpN"
1603 | },
1604 | "source": [
1605 | "# **Loops:**\r\n",
1606 | "In general, statements are executed sequentially. The first statement in a function is executed first, followed by the second, and so on.\r\n",
1607 | "\r\n",
1608 | "There may be a situation when you need to execute a block of code several number of times\r\n",
1609 | "A loop statement allows us to execute a statement or group of statements multiple times. \r\n",
1610 | "\r\n",
1611 | "In Python, there are three loops:\r\n",
1612 | "\r\n",
1613 | "1. While\r\n",
1614 | "2. For\r\n",
1615 | "3. Nested"
1616 | ]
1617 | },
1618 | {
1619 | "cell_type": "code",
1620 | "metadata": {
1621 | "id": "emvamYGCyuWY",
1622 | "colab": {
1623 | "base_uri": "https://localhost:8080/"
1624 | },
1625 | "outputId": "1837b77e-dbb0-4712-df1e-0fa400adfb35"
1626 | },
1627 | "source": [
1628 | "# before we go into loops understand range() function\r\n",
1629 | "\r\n",
1630 | "list(range(10)) # will generate list of 10 numbers from 0-9\r\n"
1631 | ],
1632 | "execution_count": null,
1633 | "outputs": [
1634 | {
1635 | "output_type": "execute_result",
1636 | "data": {
1637 | "text/plain": [
1638 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
1639 | ]
1640 | },
1641 | "metadata": {
1642 | "tags": []
1643 | },
1644 | "execution_count": 46
1645 | }
1646 | ]
1647 | },
1648 | {
1649 | "cell_type": "code",
1650 | "metadata": {
1651 | "id": "la0iS_TbzPjN",
1652 | "colab": {
1653 | "base_uri": "https://localhost:8080/"
1654 | },
1655 | "outputId": "216ab8b9-48f8-4527-c96a-ab05191c8ae3"
1656 | },
1657 | "source": [
1658 | "list(range(2,10)) # will start from 2 and print till 9"
1659 | ],
1660 | "execution_count": null,
1661 | "outputs": [
1662 | {
1663 | "output_type": "execute_result",
1664 | "data": {
1665 | "text/plain": [
1666 | "[2, 3, 4, 5, 6, 7, 8, 9]"
1667 | ]
1668 | },
1669 | "metadata": {
1670 | "tags": []
1671 | },
1672 | "execution_count": 48
1673 | }
1674 | ]
1675 | },
1676 | {
1677 | "cell_type": "code",
1678 | "metadata": {
1679 | "id": "5oxJZtbxzWFI",
1680 | "colab": {
1681 | "base_uri": "https://localhost:8080/"
1682 | },
1683 | "outputId": "ebe97d87-3573-49db-9ad0-586d88903a97"
1684 | },
1685 | "source": [
1686 | "#very simple loop\r\n",
1687 | "\r\n",
1688 | "for temp in range(6):\r\n",
1689 | " print(temp)"
1690 | ],
1691 | "execution_count": null,
1692 | "outputs": [
1693 | {
1694 | "output_type": "stream",
1695 | "text": [
1696 | "0\n",
1697 | "1\n",
1698 | "2\n",
1699 | "3\n",
1700 | "4\n",
1701 | "5\n"
1702 | ],
1703 | "name": "stdout"
1704 | }
1705 | ]
1706 | },
1707 | {
1708 | "cell_type": "code",
1709 | "metadata": {
1710 | "id": "R-di82WKzgN5",
1711 | "colab": {
1712 | "base_uri": "https://localhost:8080/"
1713 | },
1714 | "outputId": "2c38e0b7-09e1-4cb2-c620-b86d1b5c00ec"
1715 | },
1716 | "source": [
1717 | "mytask = [3,5,'Hi',8]\r\n",
1718 | "\r\n",
1719 | "for temp in mytask: #you can iterate anything in For Loop\r\n",
1720 | " print(temp)"
1721 | ],
1722 | "execution_count": null,
1723 | "outputs": [
1724 | {
1725 | "output_type": "stream",
1726 | "text": [
1727 | "3\n",
1728 | "5\n",
1729 | "Hi\n",
1730 | "8\n"
1731 | ],
1732 | "name": "stdout"
1733 | }
1734 | ]
1735 | },
1736 | {
1737 | "cell_type": "code",
1738 | "metadata": {
1739 | "id": "ailAK2-nz02d",
1740 | "colab": {
1741 | "base_uri": "https://localhost:8080/"
1742 | },
1743 | "outputId": "42d2b4eb-a00f-4632-9b1f-892cd784a39a"
1744 | },
1745 | "source": [
1746 | "# While Loop\r\n",
1747 | "\r\n",
1748 | "i = 0\r\n",
1749 | "while i<10:\r\n",
1750 | " print('HI')\r\n",
1751 | " i = i+1\r\n"
1752 | ],
1753 | "execution_count": null,
1754 | "outputs": [
1755 | {
1756 | "output_type": "stream",
1757 | "text": [
1758 | "HI\n",
1759 | "HI\n",
1760 | "HI\n",
1761 | "HI\n",
1762 | "HI\n",
1763 | "HI\n",
1764 | "HI\n",
1765 | "HI\n",
1766 | "HI\n",
1767 | "HI\n"
1768 | ],
1769 | "name": "stdout"
1770 | }
1771 | ]
1772 | },
1773 | {
1774 | "cell_type": "markdown",
1775 | "metadata": {
1776 | "id": "u64LQwaoY3sn"
1777 | },
1778 | "source": [
1779 | "# **Functions:**\n",
1780 | "\n",
1781 | "Functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it more readable, reuse it and save some time."
1782 | ]
1783 | },
1784 | {
1785 | "cell_type": "markdown",
1786 | "metadata": {
1787 | "id": "41Sl4Q_RZOqY"
1788 | },
1789 | "source": [
1790 | "**Define a function with no arguments and no return values:**"
1791 | ]
1792 | },
1793 | {
1794 | "cell_type": "code",
1795 | "metadata": {
1796 | "id": "7lPlGuNiY_xP"
1797 | },
1798 | "source": [
1799 | "def print_text():\n",
1800 | " print('this is text')"
1801 | ],
1802 | "execution_count": null,
1803 | "outputs": []
1804 | },
1805 | {
1806 | "cell_type": "code",
1807 | "metadata": {
1808 | "id": "_9uSsLHPZKi5",
1809 | "colab": {
1810 | "base_uri": "https://localhost:8080/"
1811 | },
1812 | "outputId": "8ca6270e-15b1-4a2b-9bbb-0687812a797e"
1813 | },
1814 | "source": [
1815 | "# call the function\n",
1816 | "print_text()"
1817 | ],
1818 | "execution_count": null,
1819 | "outputs": [
1820 | {
1821 | "output_type": "stream",
1822 | "text": [
1823 | "this is text\n"
1824 | ],
1825 | "name": "stdout"
1826 | }
1827 | ]
1828 | },
1829 | {
1830 | "cell_type": "markdown",
1831 | "metadata": {
1832 | "id": "2iYTL18xZS6Q"
1833 | },
1834 | "source": [
1835 | "**Define a function with one argument and no return values:**"
1836 | ]
1837 | },
1838 | {
1839 | "cell_type": "code",
1840 | "metadata": {
1841 | "id": "Vo-cD2OxZUuB"
1842 | },
1843 | "source": [
1844 | "def print_this(x):\n",
1845 | " print(x)"
1846 | ],
1847 | "execution_count": null,
1848 | "outputs": []
1849 | },
1850 | {
1851 | "cell_type": "code",
1852 | "metadata": {
1853 | "id": "7D16LaH5ZWHY",
1854 | "colab": {
1855 | "base_uri": "https://localhost:8080/"
1856 | },
1857 | "outputId": "f2450e21-9702-4fac-d0f9-515a05d2fb18"
1858 | },
1859 | "source": [
1860 | "# call the function\n",
1861 | "print_this(3)"
1862 | ],
1863 | "execution_count": null,
1864 | "outputs": [
1865 | {
1866 | "output_type": "stream",
1867 | "text": [
1868 | "3\n"
1869 | ],
1870 | "name": "stdout"
1871 | }
1872 | ]
1873 | },
1874 | {
1875 | "cell_type": "markdown",
1876 | "metadata": {
1877 | "id": "35bK9N-FZbsg"
1878 | },
1879 | "source": [
1880 | "**Define a function with one argument and one return value:**"
1881 | ]
1882 | },
1883 | {
1884 | "cell_type": "code",
1885 | "metadata": {
1886 | "id": "z1jqUXCiZfk5",
1887 | "colab": {
1888 | "base_uri": "https://localhost:8080/"
1889 | },
1890 | "outputId": "f2e5d200-8768-4172-844e-6b441a884e71"
1891 | },
1892 | "source": [
1893 | "def square(x):\n",
1894 | " return x**2\n",
1895 | " \n",
1896 | "square(5)"
1897 | ],
1898 | "execution_count": null,
1899 | "outputs": [
1900 | {
1901 | "output_type": "execute_result",
1902 | "data": {
1903 | "text/plain": [
1904 | "25"
1905 | ]
1906 | },
1907 | "metadata": {
1908 | "tags": []
1909 | },
1910 | "execution_count": 56
1911 | }
1912 | ]
1913 | },
1914 | {
1915 | "cell_type": "code",
1916 | "metadata": {
1917 | "id": "ne0NdmCn5ul9"
1918 | },
1919 | "source": [
1920 | "#Example\n",
1921 | "\n",
1922 | "def calc(a, b, op='add'):\n",
1923 | "\n",
1924 | " if op == 'add':\n",
1925 | " return a + b\n",
1926 | "\n",
1927 | " elif op == 'sub':\n",
1928 | " return a - b\n",
1929 | " \n",
1930 | " else:\n",
1931 | " print('valid operations are add and sub')"
1932 | ],
1933 | "execution_count": null,
1934 | "outputs": []
1935 | },
1936 | {
1937 | "cell_type": "code",
1938 | "metadata": {
1939 | "id": "hH_4MlBcaOjZ",
1940 | "colab": {
1941 | "base_uri": "https://localhost:8080/"
1942 | },
1943 | "outputId": "f1187e78-88a2-4e09-c654-5cff89893044"
1944 | },
1945 | "source": [
1946 | "# call the function\n",
1947 | "calc(10, 4, op='add')"
1948 | ],
1949 | "execution_count": null,
1950 | "outputs": [
1951 | {
1952 | "output_type": "execute_result",
1953 | "data": {
1954 | "text/plain": [
1955 | "14"
1956 | ]
1957 | },
1958 | "metadata": {
1959 | "tags": []
1960 | },
1961 | "execution_count": 58
1962 | }
1963 | ]
1964 | },
1965 | {
1966 | "cell_type": "code",
1967 | "metadata": {
1968 | "id": "6yJj0Zr4af0w",
1969 | "colab": {
1970 | "base_uri": "https://localhost:8080/"
1971 | },
1972 | "outputId": "cc12e37a-ab83-4ed3-f6e9-e92c0bf2219e"
1973 | },
1974 | "source": [
1975 | "# unnamed arguments are inferred by position\n",
1976 | "calc(10, 4, 'add')"
1977 | ],
1978 | "execution_count": null,
1979 | "outputs": [
1980 | {
1981 | "output_type": "execute_result",
1982 | "data": {
1983 | "text/plain": [
1984 | "14"
1985 | ]
1986 | },
1987 | "metadata": {
1988 | "tags": []
1989 | },
1990 | "execution_count": 59
1991 | }
1992 | ]
1993 | },
1994 | {
1995 | "cell_type": "code",
1996 | "metadata": {
1997 | "id": "yy-kreh6agU4",
1998 | "colab": {
1999 | "base_uri": "https://localhost:8080/"
2000 | },
2001 | "outputId": "8c7c59a7-8ee7-4f0c-efc1-c81a10a3af5e"
2002 | },
2003 | "source": [
2004 | "# default for 'op' is 'add'\n",
2005 | "calc(10, 4)"
2006 | ],
2007 | "execution_count": null,
2008 | "outputs": [
2009 | {
2010 | "output_type": "execute_result",
2011 | "data": {
2012 | "text/plain": [
2013 | "14"
2014 | ]
2015 | },
2016 | "metadata": {
2017 | "tags": []
2018 | },
2019 | "execution_count": 60
2020 | }
2021 | ]
2022 | },
2023 | {
2024 | "cell_type": "code",
2025 | "metadata": {
2026 | "id": "TnLSMIGeahaZ",
2027 | "colab": {
2028 | "base_uri": "https://localhost:8080/"
2029 | },
2030 | "outputId": "f05b5a05-1502-4d08-df0d-52130044e3fd"
2031 | },
2032 | "source": [
2033 | "calc(10, 4, 'sub')"
2034 | ],
2035 | "execution_count": null,
2036 | "outputs": [
2037 | {
2038 | "output_type": "execute_result",
2039 | "data": {
2040 | "text/plain": [
2041 | "6"
2042 | ]
2043 | },
2044 | "metadata": {
2045 | "tags": []
2046 | },
2047 | "execution_count": 61
2048 | }
2049 | ]
2050 | },
2051 | {
2052 | "cell_type": "markdown",
2053 | "metadata": {
2054 | "id": "lftlrceMeClR"
2055 | },
2056 | "source": [
2057 | "# **Anonymous Functions (Lambda Functions)**\n",
2058 | "Primarily used to temporarily define a function for use by another function.\n",
2059 | "* Anonymous\n",
2060 | "* Single use, simple 1=line function\n",
2061 | "* Throw-away functions\n",
2062 | "\n",
2063 | "**Syntax :**\n",
2064 | "\n",
2065 | "`lambda arguments : expression`\n"
2066 | ]
2067 | },
2068 | {
2069 | "cell_type": "markdown",
2070 | "metadata": {
2071 | "id": "DuIye2TkeK_R"
2072 | },
2073 | "source": [
2074 | "**Here are some single-argument Lambdas:**"
2075 | ]
2076 | },
2077 | {
2078 | "cell_type": "code",
2079 | "metadata": {
2080 | "id": "oTZQdaT9enQi",
2081 | "colab": {
2082 | "base_uri": "https://localhost:8080/",
2083 | "height": 34
2084 | },
2085 | "outputId": "1469c5b7-bc25-4a92-8969-f0427f2a5266"
2086 | },
2087 | "source": [
2088 | "add10 = lambda x : x + 10\n",
2089 | "print (add10(5))"
2090 | ],
2091 | "execution_count": null,
2092 | "outputs": [
2093 | {
2094 | "output_type": "stream",
2095 | "text": [
2096 | "15\n"
2097 | ],
2098 | "name": "stdout"
2099 | }
2100 | ]
2101 | },
2102 | {
2103 | "cell_type": "code",
2104 | "metadata": {
2105 | "id": "HS2ZWAjsezaS",
2106 | "colab": {
2107 | "base_uri": "https://localhost:8080/",
2108 | "height": 34
2109 | },
2110 | "outputId": "f6863170-8a07-47ba-9572-34ec5a2ba015"
2111 | },
2112 | "source": [
2113 | "square = lambda x : x * x\n",
2114 | "print (square(10))"
2115 | ],
2116 | "execution_count": null,
2117 | "outputs": [
2118 | {
2119 | "output_type": "stream",
2120 | "text": [
2121 | "100\n"
2122 | ],
2123 | "name": "stdout"
2124 | }
2125 | ]
2126 | },
2127 | {
2128 | "cell_type": "code",
2129 | "metadata": {
2130 | "id": "0Bj4AQ8m0ftF"
2131 | },
2132 | "source": [
2133 | "# TO-DO print even odd using Lambda function\r\n",
2134 | "\r\n"
2135 | ],
2136 | "execution_count": null,
2137 | "outputs": []
2138 | }
2139 | ]
2140 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Introduction to Python Programming for Machine Learning
2 | =====================================================
3 |
4 | All of the code is given in specific files or use the Notebooks.
5 |
6 | know more about me:
7 | http://iali.dev
8 | http://twitter.com/ialimustufa
9 |
10 | #PythonBasicsForML by Ali Mustufa
11 |
--------------------------------------------------------------------------------
/areacircle.py:
--------------------------------------------------------------------------------
1 | radius=5
2 | area=3.14*radius*radius
3 | print(area)
--------------------------------------------------------------------------------
/diff_while1.py:
--------------------------------------------------------------------------------
1 | num=int(input("Enter a number:"))
2 | total=0
3 | if(num>9):
4 |
5 | while(num>0):
6 | digit=num%10
7 | total=total+digit
8 | n=n//10
9 | print("The total sum of digits is:",tot)
10 | else:
11 | print("Type a greater no")
12 |
--------------------------------------------------------------------------------
/first_prog.py:
--------------------------------------------------------------------------------
1 | num=5
2 | num2=10
3 | print('The Addition of both numbers are',num+num2)
--------------------------------------------------------------------------------
/flight_program.py:
--------------------------------------------------------------------------------
1 | no_of_landings=356
2 | no_of_takeoffs=245
3 | initial_no_of_flights=100
4 | current_no_of_flights=initial_no_of_flights+no_of_landings-no_of_takeoffs
5 | print("Current number of flights:",current_no_of_flights)
--------------------------------------------------------------------------------
/forloop.py:
--------------------------------------------------------------------------------
1 | num=6
2 | for i in range(0,num,2): #can also be negative
3 | print(i,"--This is data")
--------------------------------------------------------------------------------
/forloop2.py:
--------------------------------------------------------------------------------
1 | name1="si iadr"
2 | name2="afnma"
3 | fullname=""
4 | i=0
5 | for i in name1:
6 | for j in name2:
7 | fullname+=i+j
8 | print(fullname)
--------------------------------------------------------------------------------
/function_problem.py:
--------------------------------------------------------------------------------
1 | #PF-Exer-7
2 |
3 | def calculate_total_ticket_cost(no_of_adults, no_of_children):
4 | total_ticket_cost=0
5 | rate_for_adult=37550.0
6 | rate_for_child=(1/3)*rate_for_adult
7 | service_tax=1.07
8 | holiday_discount=0.1
9 |
10 |
11 | total_ticket_cost=(no_of_adults*rate_for_adult)+(no_of_children*(rate_for_child))
12 | total_ticket_cost*=service_tax
13 | total_ticket_cost*=holiday_discount
14 |
15 | return total_ticket_cost
16 |
17 |
18 | #Provide different values for no_of_adults, no_of_children and test your program
19 | total_ticket_cost=calculate_total_ticket_cost(5,2)
20 | print("Total Ticket Cost:",total_ticket_cost)
--------------------------------------------------------------------------------
/if_elif.py:
--------------------------------------------------------------------------------
1 | marks=int(input("Enter marks of student"))
2 | if(marks>35):
3 | print("Student has passed\n")
4 | if(marks<60):
5 | print("With B grade")
6 | if(marks<75):
7 | print("With A grade")
8 | if(marks>75):
9 | print("With Distinction")
10 |
11 |
--------------------------------------------------------------------------------
/list_in_list.py:
--------------------------------------------------------------------------------
1 | airline_details=[["AI",8], ["EM",10],["BA",7]]
2 |
3 | #To get the details of Emirates (EM) airline
4 | #Prints a list
5 | print(airline_details[1])
6 |
7 | #To get the number of flights operated by British Airways (BA)
8 | #[2][1] refers to 2nd list and 1st value, inside airline_details
9 | #Remember counting is 0 based
10 | print(airline_details[2][1])
11 |
12 | #To display the details of all airlines
13 | print("Airline details as a list:")
14 | for airline in airline_details:
15 | print(airline)
16 |
17 | #To display the number of flights operated by each airline
18 | print("No. of flights operated by each airline:")
19 | for airline in airline_details:
20 | print(airline[1])
--------------------------------------------------------------------------------
/list_to_tuple.py:
--------------------------------------------------------------------------------
1 | list1=[1,2,3,4,"Saif"]
2 | tup1=tuple(list1)
3 | print(tup1)
--------------------------------------------------------------------------------
/list_user_input.py:
--------------------------------------------------------------------------------
1 | list1=[]
2 | for i in range(0,5):
3 | list1.append(input("Enter Element {0}".format(i)))
4 | tupli=tuple(list1)
5 |
6 | print("Elements are {0}".format(tupli))
--------------------------------------------------------------------------------
/listprog.py:
--------------------------------------------------------------------------------
1 | list1=["saif","inamdar",21,969995883]
2 | print("Elements of list are:",format(list1[0:len(list1)]))
--------------------------------------------------------------------------------
/search_in_list.py:
--------------------------------------------------------------------------------
1 | list_of_airlines=["AI","EM","BA"]
2 |
3 | keyword="AI"
4 | if keyword in list_of_airlines:
5 | print("Airline found")
6 | else:
7 | print("Airline not found")
--------------------------------------------------------------------------------
/smallest_no.py:
--------------------------------------------------------------------------------
1 | num1=int(input("Enter the first number: "))
2 | num2=int(input("Enter the second number: "))
3 | num3=int(input("Enter the third number: "))
4 | if(num1<=num2 and num1<=num3):
5 | print(num1," is the smallest")
6 | elif(num2<=num1 and num2<=num3):
7 | print(num2," is the smallest")
8 | else:
9 | print(num3," is the smallest")
--------------------------------------------------------------------------------
/student_records.py:
--------------------------------------------------------------------------------
1 | students=[]
2 | temp=[]
3 | for i in range(0,2):
4 | temp.append(input("Enter Student Name"))
5 | temp.append(int(input("Enter marks of subject 1:")))
6 | temp.append(int(input("Enter marks of subject 2:")))
7 | students.insert(i,temp[0:3])
8 | temp.clear()
9 | print(students)
--------------------------------------------------------------------------------
/typecasting.py:
--------------------------------------------------------------------------------
1 | num1,num2=(int)input("Enter 2 no :")
2 | print("Two numbers are {} {}".format(num1,num2))
--------------------------------------------------------------------------------
/whileloop.py:
--------------------------------------------------------------------------------
1 | num2=20
2 | while(num2>0):
3 | print(num2,"This is data")
4 | num2-=1
--------------------------------------------------------------------------------