\n",
323 | "5\n"
324 | ]
325 | }
326 | ],
327 | "source": [
328 | "sea_creatures = ['shark', 'cuttlefish', 'squid', 'mantisshrimp', 'anemone']\n",
329 | "\n",
330 | "print(sea_creatures)\n",
331 | "\n",
332 | "print(type(sea_creatures))\n",
333 | "\n",
334 | "print(len(sea_creatures))"
335 | ]
336 | },
337 | {
338 | "cell_type": "code",
339 | "execution_count": 7,
340 | "metadata": {
341 | "ExecuteTime": {
342 | "end_time": "2018-05-23T14:29:43.585890Z",
343 | "start_time": "2018-05-23T14:29:43.579874Z"
344 | }
345 | },
346 | "outputs": [
347 | {
348 | "name": "stdout",
349 | "output_type": "stream",
350 | "text": [
351 | "shark\n",
352 | "cuttlefish\n",
353 | "squid\n",
354 | "mantisshrimp\n",
355 | "anemone\n",
356 | "********************\n",
357 | "anemone\n",
358 | "mantisshrimp\n",
359 | "squid\n",
360 | "cuttlefish\n",
361 | "shark\n"
362 | ]
363 | }
364 | ],
365 | "source": [
366 | "print(sea_creatures[0])\n",
367 | "\n",
368 | "print(sea_creatures[1])\n",
369 | "\n",
370 | "print(sea_creatures[2])\n",
371 | "\n",
372 | "print(sea_creatures[3])\n",
373 | "\n",
374 | "print(sea_creatures[4])\n",
375 | "\n",
376 | "# try print(sea_creatures[5]) it will throw out of range error\n",
377 | "\n",
378 | "print('*'*20)\n",
379 | "\n",
380 | "print(sea_creatures[-1])\n",
381 | "\n",
382 | "print(sea_creatures[-2])\n",
383 | "\n",
384 | "print(sea_creatures[-3])\n",
385 | "\n",
386 | "print(sea_creatures[-4])\n",
387 | "\n",
388 | "print(sea_creatures[-5])\n",
389 | "\n",
390 | "# try print(sea_creatures[-6]) it will throw out of range error\n"
391 | ]
392 | },
393 | {
394 | "cell_type": "code",
395 | "execution_count": 36,
396 | "metadata": {
397 | "ExecuteTime": {
398 | "end_time": "2018-05-23T08:13:31.049201Z",
399 | "start_time": "2018-05-23T08:13:31.044688Z"
400 | }
401 | },
402 | "outputs": [
403 | {
404 | "name": "stdout",
405 | "output_type": "stream",
406 | "text": [
407 | "[10, 'Hi', 20.2, True]\n"
408 | ]
409 | }
410 | ],
411 | "source": [
412 | "l = []\n",
413 | "\n",
414 | "l.append(10)\n",
415 | "\n",
416 | "l.append('Hi')\n",
417 | "\n",
418 | "l.append(20.20)\n",
419 | "\n",
420 | "l.append(True)\n",
421 | "\n",
422 | "print(l)\n",
423 | "\n",
424 | "'''\n",
425 | "This is a multi line comment demonstrated by using triple single-quote\n",
426 | "\n",
427 | "Above mentioned list 'l' is a collection of heterogeneous data types.\n",
428 | "'''"
429 | ]
430 | },
431 | {
432 | "cell_type": "code",
433 | "execution_count": 9,
434 | "metadata": {
435 | "ExecuteTime": {
436 | "end_time": "2018-05-23T14:32:13.289116Z",
437 | "start_time": "2018-05-23T14:32:13.285105Z"
438 | }
439 | },
440 | "outputs": [
441 | {
442 | "name": "stdout",
443 | "output_type": "stream",
444 | "text": [
445 | "a\n",
446 | "5\n"
447 | ]
448 | }
449 | ],
450 | "source": [
451 | "\"\"\"\n",
452 | "This is a multi line comment demonstrated by using triple double-quote\n",
453 | "\n",
454 | "This is an example for nested lists.\n",
455 | "\"\"\"\n",
456 | "\n",
457 | "nest_list = ['Happy', [2, 0, 1, 5]]\n",
458 | "\n",
459 | "print(nest_list[0][1])\n",
460 | "\n",
461 | "print(nest_list[1][3])"
462 | ]
463 | },
464 | {
465 | "cell_type": "code",
466 | "execution_count": 15,
467 | "metadata": {
468 | "ExecuteTime": {
469 | "end_time": "2018-05-23T14:34:14.069750Z",
470 | "start_time": "2018-05-23T14:34:14.065381Z"
471 | }
472 | },
473 | "outputs": [
474 | {
475 | "name": "stdout",
476 | "output_type": "stream",
477 | "text": [
478 | "['shark', 'cuttlefish', 'squid', 'mantisshrimp', 'anemone']\n",
479 | "['shark', 'octopus', 'squid', 'mantisshrimp', 'anemone']\n",
480 | "['shark', 'octopus', 'blobfish', 'mantisshrimp', 'anemone']\n"
481 | ]
482 | }
483 | ],
484 | "source": [
485 | "\"\"\"\n",
486 | "Because lists are mutable, below written code will\n",
487 | "overwrite existing list\n",
488 | "\n",
489 | "\"\"\"\n",
490 | "\n",
491 | "sea_creatures = ['shark', 'cuttlefish', 'squid', 'mantisshrimp', 'anemone']\n",
492 | "\n",
493 | "print(sea_creatures)\n",
494 | "\n",
495 | "sea_creatures[1] = 'octopus'\n",
496 | "\n",
497 | "print(sea_creatures)\n",
498 | "\n",
499 | "sea_creatures[-3] = 'blobfish'\n",
500 | "\n",
501 | "print(sea_creatures)"
502 | ]
503 | },
504 | {
505 | "cell_type": "code",
506 | "execution_count": 16,
507 | "metadata": {
508 | "ExecuteTime": {
509 | "end_time": "2018-05-23T14:35:23.128994Z",
510 | "start_time": "2018-05-23T14:35:23.121951Z"
511 | }
512 | },
513 | "outputs": [
514 | {
515 | "name": "stdout",
516 | "output_type": "stream",
517 | "text": [
518 | "['o', 'g', 'r']\n",
519 | "['p', 'r', 'o', 'g']\n",
520 | "['r', 'a', 'm', 'i', 'z']\n",
521 | "['a', 'm', 'i', 'z']\n",
522 | "['p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z']\n",
523 | "['a', 'm']\n",
524 | "['m', 'i', 'z']\n"
525 | ]
526 | }
527 | ],
528 | "source": [
529 | "# Below we are demonstrating the list slicing operation.\n",
530 | "\n",
531 | "my_list = ['p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z']\n",
532 | "\n",
533 | "print(my_list[2:5])\n",
534 | "\n",
535 | "print(my_list[:-5])\n",
536 | "\n",
537 | "print(my_list[-5:])\n",
538 | "\n",
539 | "print(my_list[5:])\n",
540 | "\n",
541 | "print(my_list[:])\n",
542 | "\n",
543 | "print(my_list[-4:-2])\n",
544 | "\n",
545 | "print(my_list[-3:])\n"
546 | ]
547 | },
548 | {
549 | "cell_type": "code",
550 | "execution_count": 70,
551 | "metadata": {
552 | "ExecuteTime": {
553 | "end_time": "2018-05-23T03:22:53.059116Z",
554 | "start_time": "2018-05-23T03:22:53.049688Z"
555 | }
556 | },
557 | "outputs": [],
558 | "source": [
559 | "# append(element)\n",
560 | "\n",
561 | "# insert(index, element) This will insert 'element' at the given 'index'\n",
562 | "\n",
563 | "# list1.extend(list2) appends list2 at the end of list1\n",
564 | "\n",
565 | "# list.index(element) \n",
566 | "\n",
567 | "# list.index(element, index) find next element starting at 'index'\n",
568 | "\n",
569 | "# list.remove(element)\n",
570 | "\n",
571 | "# list.sort() here list should contain one kind of data type\n",
572 | "\n",
573 | "# list.reverse()\n",
574 | "\n",
575 | "# list.pop(index)\n",
576 | "\n",
577 | "# list.count(element)"
578 | ]
579 | }
580 | ],
581 | "metadata": {
582 | "kernelspec": {
583 | "display_name": "Python 3",
584 | "language": "python",
585 | "name": "python3"
586 | },
587 | "language_info": {
588 | "codemirror_mode": {
589 | "name": "ipython",
590 | "version": 3
591 | },
592 | "file_extension": ".py",
593 | "mimetype": "text/x-python",
594 | "name": "python",
595 | "nbconvert_exporter": "python",
596 | "pygments_lexer": "ipython3",
597 | "version": "3.6.5"
598 | },
599 | "toc": {
600 | "nav_menu": {},
601 | "number_sections": true,
602 | "sideBar": true,
603 | "skip_h1_title": false,
604 | "title_cell": "Table of Contents",
605 | "title_sidebar": "Contents",
606 | "toc_cell": false,
607 | "toc_position": {},
608 | "toc_section_display": true,
609 | "toc_window_display": false
610 | },
611 | "varInspector": {
612 | "cols": {
613 | "lenName": 16,
614 | "lenType": 16,
615 | "lenVar": 40
616 | },
617 | "kernels_config": {
618 | "python": {
619 | "delete_cmd_postfix": "",
620 | "delete_cmd_prefix": "del ",
621 | "library": "var_list.py",
622 | "varRefreshCmd": "print(var_dic_list())"
623 | },
624 | "r": {
625 | "delete_cmd_postfix": ") ",
626 | "delete_cmd_prefix": "rm(",
627 | "library": "var_list.r",
628 | "varRefreshCmd": "cat(var_dic_list()) "
629 | }
630 | },
631 | "types_to_exclude": [
632 | "module",
633 | "function",
634 | "builtin_function_or_method",
635 | "instance",
636 | "_Feature"
637 | ],
638 | "window_display": false
639 | }
640 | },
641 | "nbformat": 4,
642 | "nbformat_minor": 2
643 | }
644 |
--------------------------------------------------------------------------------
/3. Data Types - 2/1.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/3. Data Types - 2/1.jpeg
--------------------------------------------------------------------------------
/4. Decision Statements/1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/4. Decision Statements/1.jpg
--------------------------------------------------------------------------------
/4. Decision Statements/2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/4. Decision Statements/2.jpg
--------------------------------------------------------------------------------
/4. Decision Statements/3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/4. Decision Statements/3.jpg
--------------------------------------------------------------------------------
/4. Decision Statements/decision_control_statement.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## if Statement"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "
"
15 | ]
16 | },
17 | {
18 | "cell_type": "code",
19 | "execution_count": 10,
20 | "metadata": {
21 | "ExecuteTime": {
22 | "end_time": "2018-05-25T06:51:08.686929Z",
23 | "start_time": "2018-05-25T06:51:08.683420Z"
24 | }
25 | },
26 | "outputs": [
27 | {
28 | "name": "stdout",
29 | "output_type": "stream",
30 | "text": [
31 | "This line will print always\n"
32 | ]
33 | }
34 | ],
35 | "source": [
36 | "num = 0\n",
37 | "\n",
38 | "if -1 and 0:\n",
39 | " print('Number is positive')\n",
40 | "print('This line will print always')\n",
41 | "\n",
42 | "# 0, None, -ve, +ve"
43 | ]
44 | },
45 | {
46 | "cell_type": "markdown",
47 | "metadata": {},
48 | "source": [
49 | "## if.....else Statement"
50 | ]
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "metadata": {},
55 | "source": [
56 | "
"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": 11,
62 | "metadata": {
63 | "ExecuteTime": {
64 | "end_time": "2018-05-25T06:54:11.171237Z",
65 | "start_time": "2018-05-25T06:54:11.163714Z"
66 | }
67 | },
68 | "outputs": [
69 | {
70 | "name": "stdout",
71 | "output_type": "stream",
72 | "text": [
73 | "Positive\n",
74 | "This line will print anyways\n"
75 | ]
76 | }
77 | ],
78 | "source": [
79 | "num = 10\n",
80 | "\n",
81 | "if num > 0:\n",
82 | " print('Positive')\n",
83 | "else:\n",
84 | " print('Negative')\n",
85 | "print('This line will print anyways')"
86 | ]
87 | },
88 | {
89 | "cell_type": "markdown",
90 | "metadata": {},
91 | "source": [
92 | "## if......elif......else Statement"
93 | ]
94 | },
95 | {
96 | "cell_type": "markdown",
97 | "metadata": {},
98 | "source": [
99 | "
"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": 9,
105 | "metadata": {
106 | "ExecuteTime": {
107 | "end_time": "2018-05-24T17:14:38.565029Z",
108 | "start_time": "2018-05-24T17:14:38.561020Z"
109 | },
110 | "scrolled": true
111 | },
112 | "outputs": [
113 | {
114 | "name": "stdout",
115 | "output_type": "stream",
116 | "text": [
117 | "Positive\n"
118 | ]
119 | }
120 | ],
121 | "source": [
122 | "num = 10\n",
123 | "\n",
124 | "if num > 0:\n",
125 | " print('Positive')\n",
126 | "elif num == 0:\n",
127 | " print('ZERO')\n",
128 | "else:\n",
129 | " print('Negative')"
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "execution_count": 13,
135 | "metadata": {
136 | "ExecuteTime": {
137 | "end_time": "2018-05-25T07:02:11.826441Z",
138 | "start_time": "2018-05-25T07:02:11.821435Z"
139 | }
140 | },
141 | "outputs": [
142 | {
143 | "name": "stdout",
144 | "output_type": "stream",
145 | "text": [
146 | "B grade\n"
147 | ]
148 | }
149 | ],
150 | "source": [
151 | "grade = 110\n",
152 | "\n",
153 | "if grade >= 90 and grade<=100:\n",
154 | " print(\"A grade\")\n",
155 | "\n",
156 | "elif grade >=80:\n",
157 | " print(\"B grade\")\n",
158 | "\n",
159 | "elif grade >=70:\n",
160 | " print(\"C grade\")\n",
161 | "\n",
162 | "elif grade >= 65:\n",
163 | " print(\"D grade\")\n",
164 | "\n",
165 | "else:\n",
166 | " print(\"Failing grade\")"
167 | ]
168 | },
169 | {
170 | "cell_type": "markdown",
171 | "metadata": {},
172 | "source": [
173 | "## Nested if - else Statements"
174 | ]
175 | },
176 | {
177 | "cell_type": "code",
178 | "execution_count": 16,
179 | "metadata": {
180 | "ExecuteTime": {
181 | "end_time": "2018-05-24T17:35:58.681472Z",
182 | "start_time": "2018-05-24T17:35:58.677461Z"
183 | }
184 | },
185 | "outputs": [
186 | {
187 | "name": "stdout",
188 | "output_type": "stream",
189 | "text": [
190 | "Positive\n"
191 | ]
192 | }
193 | ],
194 | "source": [
195 | "num = 10\n",
196 | "\n",
197 | "if num >= 0:\n",
198 | " if num == 0:\n",
199 | " print('Zero')\n",
200 | " else:\n",
201 | " print('Positive')\n",
202 | "else:\n",
203 | " print('Negative')"
204 | ]
205 | },
206 | {
207 | "cell_type": "code",
208 | "execution_count": 14,
209 | "metadata": {
210 | "ExecuteTime": {
211 | "end_time": "2018-05-25T07:12:38.862788Z",
212 | "start_time": "2018-05-25T07:12:38.855774Z"
213 | }
214 | },
215 | "outputs": [
216 | {
217 | "name": "stdout",
218 | "output_type": "stream",
219 | "text": [
220 | "Passing grade of: D\n"
221 | ]
222 | }
223 | ],
224 | "source": [
225 | "grade = 66\n",
226 | "\n",
227 | "if grade >= 65:\n",
228 | " print(\"Passing grade of:\", end = ' ')\n",
229 | "\n",
230 | " if grade >= 90:\n",
231 | " print(\"A\")\n",
232 | "\n",
233 | " elif grade >=80:\n",
234 | " print(\"B\")\n",
235 | "\n",
236 | " elif grade >=70:\n",
237 | " print(\"C\")\n",
238 | "\n",
239 | " elif grade >= 65:\n",
240 | " print(\"D\")\n",
241 | "\n",
242 | "else:\n",
243 | " print(\"Failing grade\")"
244 | ]
245 | },
246 | {
247 | "cell_type": "markdown",
248 | "metadata": {},
249 | "source": [
250 | "## Miscellaneous Topics"
251 | ]
252 | },
253 | {
254 | "cell_type": "markdown",
255 | "metadata": {},
256 | "source": [
257 | "### Input Type Casting"
258 | ]
259 | },
260 | {
261 | "cell_type": "code",
262 | "execution_count": 15,
263 | "metadata": {
264 | "ExecuteTime": {
265 | "end_time": "2018-05-25T07:15:16.983076Z",
266 | "start_time": "2018-05-25T07:15:12.002148Z"
267 | },
268 | "scrolled": true
269 | },
270 | "outputs": [
271 | {
272 | "name": "stdout",
273 | "output_type": "stream",
274 | "text": [
275 | "Enter 1st number: 12\n",
276 | "Enter 2nd number: 13\n",
277 | "25\n"
278 | ]
279 | }
280 | ],
281 | "source": [
282 | "a = int(input('Enter 1st number: '))\n",
283 | "\n",
284 | "b = int(input('Enter 2nd number: '))\n",
285 | "\n",
286 | "print(a+b)"
287 | ]
288 | },
289 | {
290 | "cell_type": "markdown",
291 | "metadata": {},
292 | "source": [
293 | "### Count even and odd"
294 | ]
295 | },
296 | {
297 | "cell_type": "code",
298 | "execution_count": 16,
299 | "metadata": {
300 | "ExecuteTime": {
301 | "end_time": "2018-05-25T07:28:53.215365Z",
302 | "start_time": "2018-05-25T07:28:15.434453Z"
303 | }
304 | },
305 | "outputs": [
306 | {
307 | "name": "stdout",
308 | "output_type": "stream",
309 | "text": [
310 | "Enter the length of list you want to create: 5\n",
311 | "Enter a number at index 0: 1\n",
312 | "Enter a number at index 1: 2\n",
313 | "Enter a number at index 2: 3\n",
314 | "Enter a number at index 3: 4\n",
315 | "Enter a number at index 4: 5\n",
316 | "Even Count: 2\n",
317 | "Odd Count: 3\n"
318 | ]
319 | }
320 | ],
321 | "source": [
322 | "# Count even and odd occurences in a List\n",
323 | "\n",
324 | "even = 0\n",
325 | "odd = 0\n",
326 | "\n",
327 | "lst_len = int(input('Enter the length of list you want to create: '))\n",
328 | "\n",
329 | "lst_num = []\n",
330 | "\n",
331 | "for i in range(lst_len):\n",
332 | " num = int(input('Enter a number at index %d: ' % (i)))\n",
333 | " lst_num.append(num)\n",
334 | "\n",
335 | "for num in lst_num:\n",
336 | " if num % 2 == 0:\n",
337 | " even = even + 1\n",
338 | " else:\n",
339 | " odd = odd + 1\n",
340 | "\n",
341 | "print('Even Count: ',even)\n",
342 | "\n",
343 | "print('Odd Count: ', odd)"
344 | ]
345 | },
346 | {
347 | "cell_type": "markdown",
348 | "metadata": {},
349 | "source": [
350 | "### Output Format"
351 | ]
352 | },
353 | {
354 | "cell_type": "code",
355 | "execution_count": 25,
356 | "metadata": {
357 | "ExecuteTime": {
358 | "end_time": "2018-05-24T18:07:43.944516Z",
359 | "start_time": "2018-05-24T18:07:43.940506Z"
360 | }
361 | },
362 | "outputs": [
363 | {
364 | "name": "stdout",
365 | "output_type": "stream",
366 | "text": [
367 | "The value of a is 1 and b is 0\n",
368 | "The value of a is 1 and b is 0\n",
369 | "The value of b is 0 and a is 1\n"
370 | ]
371 | }
372 | ],
373 | "source": [
374 | "a = 1; b = 0\n",
375 | "\n",
376 | "print('The value of a is %d and b is %d' % (a, b))\n",
377 | "print('The value of a is {} and b is {}'.format(a, b))\n",
378 | "\n",
379 | "print('The value of b is {} and a is {}'.format(a, b))"
380 | ]
381 | },
382 | {
383 | "cell_type": "code",
384 | "execution_count": null,
385 | "metadata": {},
386 | "outputs": [],
387 | "source": []
388 | }
389 | ],
390 | "metadata": {
391 | "kernelspec": {
392 | "display_name": "Python 3",
393 | "language": "python",
394 | "name": "python3"
395 | },
396 | "language_info": {
397 | "codemirror_mode": {
398 | "name": "ipython",
399 | "version": 3
400 | },
401 | "file_extension": ".py",
402 | "mimetype": "text/x-python",
403 | "name": "python",
404 | "nbconvert_exporter": "python",
405 | "pygments_lexer": "ipython3",
406 | "version": "3.6.5"
407 | },
408 | "toc": {
409 | "nav_menu": {},
410 | "number_sections": true,
411 | "sideBar": true,
412 | "skip_h1_title": false,
413 | "title_cell": "Table of Contents",
414 | "title_sidebar": "Contents",
415 | "toc_cell": false,
416 | "toc_position": {},
417 | "toc_section_display": true,
418 | "toc_window_display": false
419 | },
420 | "varInspector": {
421 | "cols": {
422 | "lenName": 16,
423 | "lenType": 16,
424 | "lenVar": 40
425 | },
426 | "kernels_config": {
427 | "python": {
428 | "delete_cmd_postfix": "",
429 | "delete_cmd_prefix": "del ",
430 | "library": "var_list.py",
431 | "varRefreshCmd": "print(var_dic_list())"
432 | },
433 | "r": {
434 | "delete_cmd_postfix": ") ",
435 | "delete_cmd_prefix": "rm(",
436 | "library": "var_list.r",
437 | "varRefreshCmd": "cat(var_dic_list()) "
438 | }
439 | },
440 | "types_to_exclude": [
441 | "module",
442 | "function",
443 | "builtin_function_or_method",
444 | "instance",
445 | "_Feature"
446 | ],
447 | "window_display": false
448 | }
449 | },
450 | "nbformat": 4,
451 | "nbformat_minor": 2
452 | }
453 |
--------------------------------------------------------------------------------
/5. Loops/1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/5. Loops/1.jpg
--------------------------------------------------------------------------------
/5. Loops/2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/5. Loops/2.jpg
--------------------------------------------------------------------------------
/5. Loops/loops.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## Dictionary Sorting"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 3,
13 | "metadata": {
14 | "ExecuteTime": {
15 | "end_time": "2018-05-27T14:46:37.010510Z",
16 | "start_time": "2018-05-27T14:46:37.005497Z"
17 | },
18 | "scrolled": true
19 | },
20 | "outputs": [
21 | {
22 | "name": "stdout",
23 | "output_type": "stream",
24 | "text": [
25 | "dict_keys([100, 102, 103, 104])\n",
26 | "dict_values(['abc', 'xyz', 'lmn', 'def'])\n",
27 | "dict_items([(100, 'abc'), (102, 'xyz'), (103, 'lmn'), (104, 'def')])\n"
28 | ]
29 | }
30 | ],
31 | "source": [
32 | "d = {100:'abc', 102:'xyz', 103:'lmn', 104:'def'}\n",
33 | "\n",
34 | "print(d.keys())\n",
35 | "\n",
36 | "print(d.values())\n",
37 | "\n",
38 | "print(d.items())"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": 2,
44 | "metadata": {
45 | "ExecuteTime": {
46 | "end_time": "2018-05-28T06:59:51.989069Z",
47 | "start_time": "2018-05-28T06:59:51.983563Z"
48 | }
49 | },
50 | "outputs": [
51 | {
52 | "name": "stdout",
53 | "output_type": "stream",
54 | "text": [
55 | "[\"don't\", 'me', 'Please', 'sort']\n"
56 | ]
57 | }
58 | ],
59 | "source": [
60 | "# d = eval(input('Enter Dictionary: '))\n",
61 | "\n",
62 | "# print(sum(d.values()))\n",
63 | "\n",
64 | "print(sorted(\"Please don't sort me\".split(), key=str.lower))\n",
65 | "# {'a':100,'b':75,'c':90, 'd': 100, 'e':95}"
66 | ]
67 | },
68 | {
69 | "cell_type": "code",
70 | "execution_count": 17,
71 | "metadata": {
72 | "ExecuteTime": {
73 | "end_time": "2018-05-27T16:09:41.156477Z",
74 | "start_time": "2018-05-27T16:08:44.194584Z"
75 | }
76 | },
77 | "outputs": [
78 | {
79 | "name": "stdout",
80 | "output_type": "stream",
81 | "text": [
82 | "Enter Dictionary: {'a':100,'b':75,'c':90, 'd': 100, 'e':95}\n",
83 | "[('b', 75), ('c', 90), ('e', 95), ('a', 100), ('d', 100)]\n"
84 | ]
85 | }
86 | ],
87 | "source": [
88 | "import operator\n",
89 | "\n",
90 | "d = eval(input('Enter Dictionary: '))\n",
91 | "\n",
92 | "sorted_d = sorted(d.items(), key=operator.itemgetter(1))\n",
93 | "\n",
94 | "print(sorted_d)"
95 | ]
96 | },
97 | {
98 | "cell_type": "markdown",
99 | "metadata": {},
100 | "source": [
101 | "## Membership Operators"
102 | ]
103 | },
104 | {
105 | "cell_type": "markdown",
106 | "metadata": {},
107 | "source": [
108 | "in and not in are membership operators in Python.\n",
109 | "\n",
110 | "Used to find whether a value or variable is found in a sequence."
111 | ]
112 | },
113 | {
114 | "cell_type": "code",
115 | "execution_count": 3,
116 | "metadata": {
117 | "ExecuteTime": {
118 | "end_time": "2018-05-28T07:05:28.934187Z",
119 | "start_time": "2018-05-28T07:05:28.929176Z"
120 | }
121 | },
122 | "outputs": [
123 | {
124 | "name": "stdout",
125 | "output_type": "stream",
126 | "text": [
127 | "False\n"
128 | ]
129 | }
130 | ],
131 | "source": [
132 | "lst = [1,2,3,4]\n",
133 | "\n",
134 | "print(5 in lst)"
135 | ]
136 | },
137 | {
138 | "cell_type": "code",
139 | "execution_count": 4,
140 | "metadata": {
141 | "ExecuteTime": {
142 | "end_time": "2018-05-28T07:06:59.266839Z",
143 | "start_time": "2018-05-28T07:06:59.261826Z"
144 | }
145 | },
146 | "outputs": [
147 | {
148 | "name": "stdout",
149 | "output_type": "stream",
150 | "text": [
151 | "False\n"
152 | ]
153 | }
154 | ],
155 | "source": [
156 | "dict = {1:'a', 2:'b'}\n",
157 | "\n",
158 | "print(2 not in dict)"
159 | ]
160 | },
161 | {
162 | "cell_type": "code",
163 | "execution_count": 5,
164 | "metadata": {
165 | "ExecuteTime": {
166 | "end_time": "2018-05-28T07:07:53.240036Z",
167 | "start_time": "2018-05-28T07:07:53.232939Z"
168 | }
169 | },
170 | "outputs": [
171 | {
172 | "name": "stdout",
173 | "output_type": "stream",
174 | "text": [
175 | "True\n"
176 | ]
177 | }
178 | ],
179 | "source": [
180 | "s = 'Python'\n",
181 | "\n",
182 | "print('P' in s)"
183 | ]
184 | },
185 | {
186 | "cell_type": "markdown",
187 | "metadata": {},
188 | "source": [
189 | "## while loop"
190 | ]
191 | },
192 | {
193 | "cell_type": "code",
194 | "execution_count": 6,
195 | "metadata": {
196 | "ExecuteTime": {
197 | "end_time": "2018-05-28T07:16:03.029318Z",
198 | "start_time": "2018-05-28T07:16:03.014570Z"
199 | }
200 | },
201 | "outputs": [
202 | {
203 | "name": "stdout",
204 | "output_type": "stream",
205 | "text": [
206 | "The count is: 0\n",
207 | "The count is: 1\n",
208 | "The count is: 2\n",
209 | "The count is: 3\n",
210 | "The count is: 4\n",
211 | "The count is: 5\n",
212 | "The count is: 6\n",
213 | "The count is: 7\n",
214 | "The count is: 8\n",
215 | "The count is: 9\n",
216 | "Good bye! count is: 10\n"
217 | ]
218 | }
219 | ],
220 | "source": [
221 | "count = 0\n",
222 | "while(count < 10):\n",
223 | " print('The count is: ', count)\n",
224 | " count = count + 1\n",
225 | "print('Good bye! count is: ', count)"
226 | ]
227 | },
228 | {
229 | "cell_type": "code",
230 | "execution_count": 21,
231 | "metadata": {
232 | "ExecuteTime": {
233 | "end_time": "2018-05-26T12:28:15.698509Z",
234 | "start_time": "2018-05-26T12:28:15.693997Z"
235 | }
236 | },
237 | "outputs": [
238 | {
239 | "name": "stdout",
240 | "output_type": "stream",
241 | "text": [
242 | "Product = 120\n"
243 | ]
244 | }
245 | ],
246 | "source": [
247 | "tup = (1,2,3,4,5)\n",
248 | "\n",
249 | "prod = 1\n",
250 | "index = 0\n",
251 | "\n",
252 | "while index < len(tup):\n",
253 | " prod *= tup[index]\n",
254 | " index += 1\n",
255 | "\n",
256 | "print('Product = {}'.format(prod))"
257 | ]
258 | },
259 | {
260 | "cell_type": "markdown",
261 | "metadata": {},
262 | "source": [
263 | "### while....else"
264 | ]
265 | },
266 | {
267 | "cell_type": "code",
268 | "execution_count": 8,
269 | "metadata": {
270 | "ExecuteTime": {
271 | "end_time": "2018-05-28T07:23:33.929194Z",
272 | "start_time": "2018-05-28T07:23:33.921193Z"
273 | }
274 | },
275 | "outputs": [
276 | {
277 | "name": "stdout",
278 | "output_type": "stream",
279 | "text": [
280 | "No more numbers in tuple.\n",
281 | "Product = 120\n"
282 | ]
283 | }
284 | ],
285 | "source": [
286 | "tup = (1,2,3,4,5)\n",
287 | "\n",
288 | "prod = 1\n",
289 | "index = 0\n",
290 | "\n",
291 | "while index < len(tup):\n",
292 | " prod *= tup[index]\n",
293 | " index += 1\n",
294 | "else:\n",
295 | " print('No more numbers in tuple.')\n",
296 | "\n",
297 | "print('Product = {}'.format(prod))"
298 | ]
299 | },
300 | {
301 | "cell_type": "code",
302 | "execution_count": 24,
303 | "metadata": {
304 | "ExecuteTime": {
305 | "end_time": "2018-05-26T15:47:15.771672Z",
306 | "start_time": "2018-05-26T15:47:12.807696Z"
307 | },
308 | "scrolled": true
309 | },
310 | "outputs": [
311 | {
312 | "name": "stdout",
313 | "output_type": "stream",
314 | "text": [
315 | "Enter a number: 5\n",
316 | "16\n",
317 | "8\n",
318 | "4\n",
319 | "2\n",
320 | "1\n"
321 | ]
322 | }
323 | ],
324 | "source": [
325 | "def collatz(number):\n",
326 | " if number % 2 == 0:\n",
327 | " print(number // 2)\n",
328 | " return number // 2\n",
329 | " elif number % 2 == 1:\n",
330 | " result = 3 * number + 1\n",
331 | " print(result)\n",
332 | " return result\n",
333 | "\n",
334 | "num = int(input('Enter a number: '))\n",
335 | "\n",
336 | "while num != 1:\n",
337 | " num = collatz(num)"
338 | ]
339 | },
340 | {
341 | "cell_type": "markdown",
342 | "metadata": {},
343 | "source": [
344 | "## for loop"
345 | ]
346 | },
347 | {
348 | "cell_type": "code",
349 | "execution_count": 27,
350 | "metadata": {
351 | "ExecuteTime": {
352 | "end_time": "2018-05-26T15:51:09.047649Z",
353 | "start_time": "2018-05-26T15:51:09.044141Z"
354 | }
355 | },
356 | "outputs": [
357 | {
358 | "name": "stdout",
359 | "output_type": "stream",
360 | "text": [
361 | "Product = 120\n"
362 | ]
363 | }
364 | ],
365 | "source": [
366 | "tup = (1,2,3,4,5)\n",
367 | "\n",
368 | "prod = 1\n",
369 | "\n",
370 | "for item in tup:\n",
371 | " prod *= item\n",
372 | "\n",
373 | "print('Product = {}'.format(prod))"
374 | ]
375 | },
376 | {
377 | "cell_type": "code",
378 | "execution_count": 29,
379 | "metadata": {
380 | "ExecuteTime": {
381 | "end_time": "2018-05-26T15:53:24.867100Z",
382 | "start_time": "2018-05-26T15:53:24.863090Z"
383 | }
384 | },
385 | "outputs": [
386 | {
387 | "name": "stdout",
388 | "output_type": "stream",
389 | "text": [
390 | "The count is: 0\n",
391 | "The count is: 1\n",
392 | "The count is: 2\n",
393 | "The count is: 3\n",
394 | "The count is: 4\n",
395 | "The count is: 5\n",
396 | "The count is: 6\n",
397 | "The count is: 7\n",
398 | "The count is: 8\n",
399 | "The count is: 9\n",
400 | "Good bye!\n"
401 | ]
402 | }
403 | ],
404 | "source": [
405 | "for count in range(10):\n",
406 | " print('The count is: ', count)\n",
407 | "print('Good bye!')\n",
408 | "\n",
409 | "# try range(1, 10, 1)"
410 | ]
411 | },
412 | {
413 | "cell_type": "code",
414 | "execution_count": 9,
415 | "metadata": {
416 | "ExecuteTime": {
417 | "end_time": "2018-05-28T07:54:28.023220Z",
418 | "start_time": "2018-05-28T07:54:28.018707Z"
419 | }
420 | },
421 | "outputs": [
422 | {
423 | "name": "stdout",
424 | "output_type": "stream",
425 | "text": [
426 | "10\n",
427 | "20\n",
428 | "30\n",
429 | "40\n",
430 | "50\n",
431 | "60\n"
432 | ]
433 | }
434 | ],
435 | "source": [
436 | "# Iterating directly element by element\n",
437 | "\n",
438 | "\n",
439 | "# Iterating by sequence index\n",
440 | "lst = [10,20,30,40,50,60]\n",
441 | "for index in range(len(lst)):\n",
442 | " print(lst[index])"
443 | ]
444 | },
445 | {
446 | "cell_type": "code",
447 | "execution_count": 24,
448 | "metadata": {
449 | "ExecuteTime": {
450 | "end_time": "2018-05-27T16:29:06.269555Z",
451 | "start_time": "2018-05-27T16:29:06.256095Z"
452 | }
453 | },
454 | "outputs": [
455 | {
456 | "name": "stdout",
457 | "output_type": "stream",
458 | "text": [
459 | "LIST ITERATION: \n",
460 | "10 20 30 40 50 60 \n",
461 | "TUPLE ITERATION: \n",
462 | "10 20 30 40 50 60 \n",
463 | "STRING ITERATION: \n",
464 | "P y t h o n \n",
465 | "DICTIONARY ITERATION: \n",
466 | "xyz 123\n",
467 | "abc 345\n"
468 | ]
469 | }
470 | ],
471 | "source": [
472 | "print('LIST ITERATION: ')\n",
473 | "lst = [10,20,30,40,50,60]\n",
474 | "for element in lst:\n",
475 | " print(element, end=' ')\n",
476 | "print()\n",
477 | "\n",
478 | "print('TUPLE ITERATION: ')\n",
479 | "tup = (10,20,30,40,50,60)\n",
480 | "for element in tup:\n",
481 | " print(element, end=' ')\n",
482 | "print()\n",
483 | "\n",
484 | "print('STRING ITERATION: ')\n",
485 | "myStr = 'Python'\n",
486 | "for element in myStr:\n",
487 | " print(element, end=' ')\n",
488 | "print()\n",
489 | "\n",
490 | "print('DICTIONARY ITERATION: ')\n",
491 | "dict = {}\n",
492 | "dict['xyz'] = 123\n",
493 | "dict['abc'] = 345\n",
494 | "for key in dict:\n",
495 | " print(key, dict[key])"
496 | ]
497 | },
498 | {
499 | "cell_type": "markdown",
500 | "metadata": {},
501 | "source": [
502 | "### for.....else"
503 | ]
504 | },
505 | {
506 | "cell_type": "code",
507 | "execution_count": 3,
508 | "metadata": {
509 | "ExecuteTime": {
510 | "end_time": "2018-05-27T03:53:22.112032Z",
511 | "start_time": "2018-05-27T03:53:22.096617Z"
512 | }
513 | },
514 | "outputs": [
515 | {
516 | "name": "stdout",
517 | "output_type": "stream",
518 | "text": [
519 | "You have exhausted the list\n",
520 | "Product = 120\n"
521 | ]
522 | }
523 | ],
524 | "source": [
525 | "tup = (1,2,3,4,5)\n",
526 | "\n",
527 | "prod = 1\n",
528 | "\n",
529 | "for item in tup:\n",
530 | " prod *= item\n",
531 | "else:\n",
532 | " print('You have exhausted the list')\n",
533 | "\n",
534 | "print('Product = {}'.format(prod))"
535 | ]
536 | },
537 | {
538 | "cell_type": "markdown",
539 | "metadata": {},
540 | "source": [
541 | "## Nested Loops"
542 | ]
543 | },
544 | {
545 | "cell_type": "code",
546 | "execution_count": 11,
547 | "metadata": {
548 | "ExecuteTime": {
549 | "end_time": "2018-05-28T08:03:01.951895Z",
550 | "start_time": "2018-05-28T08:03:01.941868Z"
551 | }
552 | },
553 | "outputs": [
554 | {
555 | "name": "stdout",
556 | "output_type": "stream",
557 | "text": [
558 | "i: 0, j:0\n",
559 | "i: 0, j:1\n",
560 | "i: 0, j:2\n",
561 | "i: 1, j:0\n",
562 | "i: 1, j:1\n",
563 | "i: 1, j:2\n",
564 | "i: 2, j:0\n",
565 | "i: 2, j:1\n",
566 | "i: 2, j:2\n",
567 | "i: 3, j:0\n",
568 | "i: 3, j:1\n",
569 | "i: 3, j:2\n",
570 | "i: 4, j:0\n",
571 | "i: 4, j:1\n",
572 | "i: 4, j:2\n",
573 | "i: 5, j:0\n",
574 | "i: 5, j:1\n",
575 | "i: 5, j:2\n",
576 | "i: 6, j:0\n",
577 | "i: 6, j:1\n",
578 | "i: 6, j:2\n",
579 | "i: 7, j:0\n",
580 | "i: 7, j:1\n",
581 | "i: 7, j:2\n",
582 | "i: 8, j:0\n",
583 | "i: 8, j:1\n",
584 | "i: 8, j:2\n",
585 | "i: 9, j:0\n",
586 | "i: 9, j:1\n",
587 | "i: 9, j:2\n"
588 | ]
589 | }
590 | ],
591 | "source": [
592 | "i = 0\n",
593 | "\n",
594 | "while i < 10:\n",
595 | " j = 0\n",
596 | " while j < 3:\n",
597 | " print('i: {}, j:{}'.format(i, j))\n",
598 | " j += 1\n",
599 | " \n",
600 | " i += 1"
601 | ]
602 | },
603 | {
604 | "cell_type": "code",
605 | "execution_count": 12,
606 | "metadata": {
607 | "ExecuteTime": {
608 | "end_time": "2018-05-28T08:09:17.932127Z",
609 | "start_time": "2018-05-28T08:09:17.926647Z"
610 | }
611 | },
612 | "outputs": [
613 | {
614 | "name": "stdout",
615 | "output_type": "stream",
616 | "text": [
617 | "Prime Numbers between 1 & 11 are: \n",
618 | "2\n",
619 | "3\n",
620 | "5\n",
621 | "7\n",
622 | "11\n"
623 | ]
624 | }
625 | ],
626 | "source": [
627 | "num_1 = 1\n",
628 | "num_2 = 11\n",
629 | "\n",
630 | "print('Prime Numbers between {} & {} are: '.format(num_1,num_2))\n",
631 | "\n",
632 | "for num in range(num_1, num_2+1):\n",
633 | " if num > 1:\n",
634 | " isDivisible = False\n",
635 | " for index in range(2, num):\n",
636 | " if num % index == 0:\n",
637 | " isDivisible = True\n",
638 | " if not isDivisible:\n",
639 | " print(num)\n"
640 | ]
641 | },
642 | {
643 | "cell_type": "markdown",
644 | "metadata": {},
645 | "source": [
646 | "## BREAK & CONTINUE"
647 | ]
648 | },
649 | {
650 | "cell_type": "markdown",
651 | "metadata": {
652 | "ExecuteTime": {
653 | "end_time": "2018-05-27T04:06:33.272402Z",
654 | "start_time": "2018-05-27T04:06:33.265224Z"
655 | }
656 | },
657 | "source": [
658 | "
"
659 | ]
660 | },
661 | {
662 | "cell_type": "code",
663 | "execution_count": 10,
664 | "metadata": {
665 | "ExecuteTime": {
666 | "end_time": "2018-05-27T04:32:35.020303Z",
667 | "start_time": "2018-05-27T04:32:35.011591Z"
668 | }
669 | },
670 | "outputs": [
671 | {
672 | "name": "stdout",
673 | "output_type": "stream",
674 | "text": [
675 | "The count is: 0\n",
676 | "The count is: 1\n",
677 | "The count is: 2\n",
678 | "The count is: 3\n",
679 | "The count is: 4\n",
680 | "Good bye!\n"
681 | ]
682 | }
683 | ],
684 | "source": [
685 | "for count in range(10):\n",
686 | " if count == 5:\n",
687 | " break\n",
688 | " print('The count is: ', count)\n",
689 | "print('Good bye!')\n",
690 | "# try else"
691 | ]
692 | },
693 | {
694 | "cell_type": "markdown",
695 | "metadata": {},
696 | "source": [
697 | "
"
698 | ]
699 | },
700 | {
701 | "cell_type": "code",
702 | "execution_count": 7,
703 | "metadata": {
704 | "ExecuteTime": {
705 | "end_time": "2018-05-27T15:09:27.512288Z",
706 | "start_time": "2018-05-27T15:09:27.508280Z"
707 | },
708 | "scrolled": false
709 | },
710 | "outputs": [
711 | {
712 | "name": "stdout",
713 | "output_type": "stream",
714 | "text": [
715 | "The count is: 0\n",
716 | "The count is: 1\n",
717 | "The count is: 2\n",
718 | "The count is: 3\n",
719 | "The count is: 4\n",
720 | "The count is: 6\n",
721 | "The count is: 7\n",
722 | "The count is: 8\n",
723 | "The count is: 9\n",
724 | "Good bye!\n"
725 | ]
726 | }
727 | ],
728 | "source": [
729 | "for count in range(10):\n",
730 | " if count == 5:\n",
731 | " continue\n",
732 | " print('The count is: ', count)\n",
733 | "print('Good bye!')"
734 | ]
735 | },
736 | {
737 | "cell_type": "code",
738 | "execution_count": 14,
739 | "metadata": {
740 | "ExecuteTime": {
741 | "end_time": "2018-05-28T08:17:28.941451Z",
742 | "start_time": "2018-05-28T08:17:23.611233Z"
743 | }
744 | },
745 | "outputs": [
746 | {
747 | "name": "stdout",
748 | "output_type": "stream",
749 | "text": [
750 | "Enter a number to check if its even or odd: 7\n",
751 | "Wrong Number!!\\nGAME OVER\n"
752 | ]
753 | }
754 | ],
755 | "source": [
756 | "while True:\n",
757 | " num = int(input(\"Enter a number to check if its even or odd: \"))\n",
758 | " if num % 2 == 0:\n",
759 | " print('Congratulations, you entered an even number.')\n",
760 | " else:\n",
761 | " break\n",
762 | "print('Wrong Number!!\\\\nGAME OVER')"
763 | ]
764 | },
765 | {
766 | "cell_type": "markdown",
767 | "metadata": {},
768 | "source": [
769 | "## Assignment Discussions"
770 | ]
771 | },
772 | {
773 | "cell_type": "markdown",
774 | "metadata": {},
775 | "source": [
776 | "\n",
777 | "Q.4- From a list containing ints, strings and floats, make three lists to store them separately
\n",
778 | "Q.7- Create a user defined dictionary and get keys corresponding to the value using for loop.
\n",
779 | "Q.8- Take inputs from user to make a list. Again take one input from user and search it in the list and delete that element, if found. Iterate over list using for loop.\n",
780 | "
"
781 | ]
782 | }
783 | ],
784 | "metadata": {
785 | "kernelspec": {
786 | "display_name": "Python 3",
787 | "language": "python",
788 | "name": "python3"
789 | },
790 | "language_info": {
791 | "codemirror_mode": {
792 | "name": "ipython",
793 | "version": 3
794 | },
795 | "file_extension": ".py",
796 | "mimetype": "text/x-python",
797 | "name": "python",
798 | "nbconvert_exporter": "python",
799 | "pygments_lexer": "ipython3",
800 | "version": "3.6.5"
801 | },
802 | "toc": {
803 | "nav_menu": {},
804 | "number_sections": true,
805 | "sideBar": true,
806 | "skip_h1_title": false,
807 | "title_cell": "Table of Contents",
808 | "title_sidebar": "Contents",
809 | "toc_cell": false,
810 | "toc_position": {},
811 | "toc_section_display": true,
812 | "toc_window_display": false
813 | },
814 | "varInspector": {
815 | "cols": {
816 | "lenName": 16,
817 | "lenType": 16,
818 | "lenVar": 40
819 | },
820 | "kernels_config": {
821 | "python": {
822 | "delete_cmd_postfix": "",
823 | "delete_cmd_prefix": "del ",
824 | "library": "var_list.py",
825 | "varRefreshCmd": "print(var_dic_list())"
826 | },
827 | "r": {
828 | "delete_cmd_postfix": ") ",
829 | "delete_cmd_prefix": "rm(",
830 | "library": "var_list.r",
831 | "varRefreshCmd": "cat(var_dic_list()) "
832 | }
833 | },
834 | "types_to_exclude": [
835 | "module",
836 | "function",
837 | "builtin_function_or_method",
838 | "instance",
839 | "_Feature"
840 | ],
841 | "window_display": false
842 | }
843 | },
844 | "nbformat": 4,
845 | "nbformat_minor": 2
846 | }
847 |
--------------------------------------------------------------------------------
/6. Functions/functions.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 3,
6 | "metadata": {
7 | "ExecuteTime": {
8 | "end_time": "2018-05-29T06:39:16.857371Z",
9 | "start_time": "2018-05-29T06:39:16.681907Z"
10 | }
11 | },
12 | "outputs": [
13 | {
14 | "name": "stdout",
15 | "output_type": "stream",
16 | "text": [
17 | "1\n",
18 | "2\n",
19 | "4\n",
20 | "huraaaay!\n"
21 | ]
22 | },
23 | {
24 | "ename": "AttributeError",
25 | "evalue": "'list' object has no attribute 'discard'",
26 | "output_type": "error",
27 | "traceback": [
28 | "\u001b[1;31m------------------------------------------------------------------\u001b[0m",
29 | "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
30 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'huraaaay!'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0mlst\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdiscard\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
31 | "\u001b[1;31mAttributeError\u001b[0m: 'list' object has no attribute 'discard'"
32 | ]
33 | }
34 | ],
35 | "source": [
36 | "lst = [1,2,3,4]\n",
37 | "\n",
38 | "\n",
39 | "for item in lst:\n",
40 | " if item == 3:\n",
41 | " continue\n",
42 | " print(item)\n",
43 | "else:\n",
44 | " print('huraaaay!')\n",
45 | " \n",
46 | "lst.discard(3)"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "## Python Functions"
54 | ]
55 | },
56 | {
57 | "cell_type": "markdown",
58 | "metadata": {},
59 | "source": [
60 | "### Function Definition & Calling a Function"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": 8,
66 | "metadata": {
67 | "ExecuteTime": {
68 | "end_time": "2018-05-29T06:56:09.081826Z",
69 | "start_time": "2018-05-29T06:56:09.078321Z"
70 | }
71 | },
72 | "outputs": [
73 | {
74 | "name": "stdout",
75 | "output_type": "stream",
76 | "text": [
77 | "Hello World\n"
78 | ]
79 | }
80 | ],
81 | "source": [
82 | "def myFirstFunction():\n",
83 | " '''This is my first function. It will print Hello World always.'''\n",
84 | " print('Hello World')\n",
85 | " \n",
86 | "myFirstFunction()\n",
87 | "\n",
88 | "# try to change indentation and line of the code."
89 | ]
90 | },
91 | {
92 | "cell_type": "code",
93 | "execution_count": 11,
94 | "metadata": {
95 | "ExecuteTime": {
96 | "end_time": "2018-05-29T03:12:57.806089Z",
97 | "start_time": "2018-05-29T03:12:57.799554Z"
98 | }
99 | },
100 | "outputs": [
101 | {
102 | "name": "stdout",
103 | "output_type": "stream",
104 | "text": [
105 | "This is my first function. It will print Hello World always.\n"
106 | ]
107 | }
108 | ],
109 | "source": [
110 | "print(myFirstFunction.__doc__)"
111 | ]
112 | },
113 | {
114 | "cell_type": "code",
115 | "execution_count": 9,
116 | "metadata": {
117 | "ExecuteTime": {
118 | "end_time": "2018-05-29T06:59:51.586341Z",
119 | "start_time": "2018-05-29T06:59:51.582833Z"
120 | }
121 | },
122 | "outputs": [
123 | {
124 | "name": "stdout",
125 | "output_type": "stream",
126 | "text": [
127 | "Return the tuple (x//y, x%y). Invariant: div*y + mod == x.\n"
128 | ]
129 | }
130 | ],
131 | "source": [
132 | "print(divmod.__doc__)"
133 | ]
134 | },
135 | {
136 | "cell_type": "markdown",
137 | "metadata": {},
138 | "source": [
139 | "### Function With Arguments/Parameters"
140 | ]
141 | },
142 | {
143 | "cell_type": "code",
144 | "execution_count": 3,
145 | "metadata": {
146 | "ExecuteTime": {
147 | "end_time": "2018-05-28T16:41:59.478050Z",
148 | "start_time": "2018-05-28T16:41:59.466948Z"
149 | }
150 | },
151 | "outputs": [
152 | {
153 | "name": "stdout",
154 | "output_type": "stream",
155 | "text": [
156 | "Hello Dehradun\n"
157 | ]
158 | }
159 | ],
160 | "source": [
161 | "# Function with arguments/parameter\n",
162 | "\n",
163 | "def sayHello(name):\n",
164 | " '''This function wishes hello'''\n",
165 | " print('Hello', name)\n",
166 | " \n",
167 | "sayHello('Dehradun')\n"
168 | ]
169 | },
170 | {
171 | "cell_type": "code",
172 | "execution_count": 4,
173 | "metadata": {
174 | "ExecuteTime": {
175 | "end_time": "2018-05-28T16:43:47.212524Z",
176 | "start_time": "2018-05-28T16:43:47.207303Z"
177 | }
178 | },
179 | "outputs": [
180 | {
181 | "name": "stdout",
182 | "output_type": "stream",
183 | "text": [
184 | "The square of 10 is 100\n",
185 | "The square of 5 is 25\n"
186 | ]
187 | }
188 | ],
189 | "source": [
190 | "# Write a function to take a number and square it\n",
191 | "\n",
192 | "def square(num):\n",
193 | " '''This function prints square'''\n",
194 | " print('The square of {} is {}'.format(num, num**2))\n",
195 | "\n",
196 | "square(10)\n",
197 | "square(5)"
198 | ]
199 | },
200 | {
201 | "cell_type": "markdown",
202 | "metadata": {},
203 | "source": [
204 | "### Return Statement"
205 | ]
206 | },
207 | {
208 | "cell_type": "code",
209 | "execution_count": 10,
210 | "metadata": {
211 | "ExecuteTime": {
212 | "end_time": "2018-05-29T07:10:31.305919Z",
213 | "start_time": "2018-05-29T07:10:31.302411Z"
214 | }
215 | },
216 | "outputs": [
217 | {
218 | "name": "stdout",
219 | "output_type": "stream",
220 | "text": [
221 | "100\n",
222 | "25\n"
223 | ]
224 | }
225 | ],
226 | "source": [
227 | "def square(num):\n",
228 | " '''This function returns square'''\n",
229 | " return num*num\n",
230 | "\n",
231 | "x = square(10)\n",
232 | "print(x)\n",
233 | "print(square(5))"
234 | ]
235 | },
236 | {
237 | "cell_type": "code",
238 | "execution_count": 11,
239 | "metadata": {
240 | "ExecuteTime": {
241 | "end_time": "2018-05-29T07:13:09.063774Z",
242 | "start_time": "2018-05-29T07:13:09.059767Z"
243 | }
244 | },
245 | "outputs": [
246 | {
247 | "name": "stdout",
248 | "output_type": "stream",
249 | "text": [
250 | "Hello\n",
251 | "None\n"
252 | ]
253 | }
254 | ],
255 | "source": [
256 | "def fun():\n",
257 | " print('Hello')\n",
258 | "\n",
259 | "print(fun())"
260 | ]
261 | },
262 | {
263 | "cell_type": "code",
264 | "execution_count": 9,
265 | "metadata": {
266 | "ExecuteTime": {
267 | "end_time": "2018-05-28T17:05:24.757427Z",
268 | "start_time": "2018-05-28T17:05:24.751898Z"
269 | }
270 | },
271 | "outputs": [
272 | {
273 | "name": "stdout",
274 | "output_type": "stream",
275 | "text": [
276 | "20 10\n"
277 | ]
278 | }
279 | ],
280 | "source": [
281 | "# Multiple Returns\n",
282 | "\n",
283 | "def add_sub(a,b):\n",
284 | " add = a+b\n",
285 | " sub = a-b\n",
286 | " return add, sub\n",
287 | "\n",
288 | "addition, subtraction = add_sub(15,5)\n",
289 | "\n",
290 | "print(addition, subtraction)"
291 | ]
292 | },
293 | {
294 | "cell_type": "markdown",
295 | "metadata": {},
296 | "source": [
297 | "### Function Arguments"
298 | ]
299 | },
300 | {
301 | "cell_type": "code",
302 | "execution_count": 13,
303 | "metadata": {
304 | "ExecuteTime": {
305 | "end_time": "2018-05-29T07:21:31.064663Z",
306 | "start_time": "2018-05-29T07:21:31.052129Z"
307 | },
308 | "scrolled": true
309 | },
310 | "outputs": [
311 | {
312 | "name": "stdout",
313 | "output_type": "stream",
314 | "text": [
315 | "10\n",
316 | "-10\n"
317 | ]
318 | },
319 | {
320 | "ename": "TypeError",
321 | "evalue": "sub() takes 2 positional arguments but 3 were given",
322 | "output_type": "error",
323 | "traceback": [
324 | "\u001b[1;31m------------------------------------------------------------------\u001b[0m",
325 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
326 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
327 | "\u001b[1;31mTypeError\u001b[0m: sub() takes 2 positional arguments but 3 were given"
328 | ]
329 | }
330 | ],
331 | "source": [
332 | "# Positional Argument\n",
333 | "def sub(a, b):\n",
334 | " return a - b\n",
335 | "print(sub(20,10))\n",
336 | "\n",
337 | "print(sub(10,20))\n",
338 | "print(sub(20,10,10))"
339 | ]
340 | },
341 | {
342 | "cell_type": "code",
343 | "execution_count": 20,
344 | "metadata": {
345 | "ExecuteTime": {
346 | "end_time": "2018-05-29T07:29:40.372159Z",
347 | "start_time": "2018-05-29T07:29:40.360127Z"
348 | },
349 | "scrolled": true
350 | },
351 | "outputs": [
352 | {
353 | "name": "stdout",
354 | "output_type": "stream",
355 | "text": [
356 | "Hello abc hurrraay!\n"
357 | ]
358 | },
359 | {
360 | "ename": "TypeError",
361 | "evalue": "sayAnything() got multiple values for argument 'name'",
362 | "output_type": "error",
363 | "traceback": [
364 | "\u001b[1;31m------------------------------------------------------------------\u001b[0m",
365 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
366 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0msayAnything\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'abc'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0manything\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'hurrraay!'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0msayAnything\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'abc'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'hurrraay!'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
367 | "\u001b[1;31mTypeError\u001b[0m: sayAnything() got multiple values for argument 'name'"
368 | ]
369 | }
370 | ],
371 | "source": [
372 | "# Keyword Argument\n",
373 | "def sayAnything(name, anything):\n",
374 | " print('Hello', name, anything)\n",
375 | "\n",
376 | "sayAnything(name='abc', anything='hurrraay!')\n",
377 | "\n",
378 | "sayAnything('abc', name='hurrraay!')"
379 | ]
380 | },
381 | {
382 | "cell_type": "code",
383 | "execution_count": 21,
384 | "metadata": {
385 | "ExecuteTime": {
386 | "end_time": "2018-05-29T07:32:02.041082Z",
387 | "start_time": "2018-05-29T07:32:02.031056Z"
388 | }
389 | },
390 | "outputs": [
391 | {
392 | "name": "stdout",
393 | "output_type": "stream",
394 | "text": [
395 | "Hello abc hurray\n",
396 | "Hello Guest Welcome\n"
397 | ]
398 | }
399 | ],
400 | "source": [
401 | "# Default Argument\n",
402 | "def sayAnything(name = 'Guest', anything='Welcome'):\n",
403 | " print('Hello', name, anything)\n",
404 | "\n",
405 | "sayAnything('abc', 'hurray')\n",
406 | "sayAnything()"
407 | ]
408 | },
409 | {
410 | "cell_type": "code",
411 | "execution_count": 18,
412 | "metadata": {
413 | "ExecuteTime": {
414 | "end_time": "2018-05-28T17:40:21.621257Z",
415 | "start_time": "2018-05-28T17:40:21.613168Z"
416 | }
417 | },
418 | "outputs": [
419 | {
420 | "name": "stdout",
421 | "output_type": "stream",
422 | "text": [
423 | "Name: abc\n",
424 | "Age: 14\n",
425 | "Marks: 100\n",
426 | "Message: Good Morning\n"
427 | ]
428 | }
429 | ],
430 | "source": [
431 | "def printInfo(marks, age, name='Guest', msg='Good Morning'):\n",
432 | " print('Name: ', name)\n",
433 | " print('Age: ', age)\n",
434 | " print('Marks: ', marks)\n",
435 | " print('Message: ', msg)\n",
436 | "\n",
437 | "printInfo(100, 14, 'abc')"
438 | ]
439 | },
440 | {
441 | "cell_type": "markdown",
442 | "metadata": {},
443 | "source": [
444 | "### Scope of Variables"
445 | ]
446 | },
447 | {
448 | "cell_type": "code",
449 | "execution_count": 24,
450 | "metadata": {
451 | "ExecuteTime": {
452 | "end_time": "2018-05-29T07:38:47.189368Z",
453 | "start_time": "2018-05-29T07:38:47.184856Z"
454 | }
455 | },
456 | "outputs": [
457 | {
458 | "name": "stdout",
459 | "output_type": "stream",
460 | "text": [
461 | "30\n",
462 | "30\n"
463 | ]
464 | }
465 | ],
466 | "source": [
467 | "# Global variable is accessible everywhere\n",
468 | "\n",
469 | "add = 0\n",
470 | "\n",
471 | "def sum(a,b):\n",
472 | " add = a+b\n",
473 | " print(add)\n",
474 | " return add\n",
475 | "\n",
476 | "print(sum(10,20))"
477 | ]
478 | },
479 | {
480 | "cell_type": "code",
481 | "execution_count": 26,
482 | "metadata": {
483 | "ExecuteTime": {
484 | "end_time": "2018-05-29T07:39:54.039759Z",
485 | "start_time": "2018-05-29T07:39:54.027225Z"
486 | },
487 | "scrolled": true
488 | },
489 | "outputs": [
490 | {
491 | "name": "stdout",
492 | "output_type": "stream",
493 | "text": [
494 | "30\n"
495 | ]
496 | },
497 | {
498 | "ename": "NameError",
499 | "evalue": "name 'total' is not defined",
500 | "output_type": "error",
501 | "traceback": [
502 | "\u001b[1;31m------------------------------------------------------------------\u001b[0m",
503 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
504 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0madd\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtotal\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
505 | "\u001b[1;31mNameError\u001b[0m: name 'total' is not defined"
506 | ]
507 | }
508 | ],
509 | "source": [
510 | "# Local Variable\n",
511 | "\n",
512 | "add = 0\n",
513 | "\n",
514 | "def sum(a,b):\n",
515 | " total = a+b\n",
516 | " return total\n",
517 | "add = sum(10,20)\n",
518 | "print(add)\n",
519 | "\n",
520 | "print(total)"
521 | ]
522 | },
523 | {
524 | "cell_type": "markdown",
525 | "metadata": {},
526 | "source": [
527 | "### Pass By Reference"
528 | ]
529 | },
530 | {
531 | "cell_type": "code",
532 | "execution_count": 21,
533 | "metadata": {
534 | "ExecuteTime": {
535 | "end_time": "2018-05-28T18:10:44.763863Z",
536 | "start_time": "2018-05-28T18:10:44.726402Z"
537 | }
538 | },
539 | "outputs": [
540 | {
541 | "name": "stdout",
542 | "output_type": "stream",
543 | "text": [
544 | "Inside Function: [10, 20, 30, [1, 2, 3]]\n",
545 | "Main Program: [10, 20, 30, [1, 2, 3]]\n"
546 | ]
547 | }
548 | ],
549 | "source": [
550 | "# Pass by reference\n",
551 | "\n",
552 | "def changeMe(lst):\n",
553 | " '''Changes List'''\n",
554 | " lst.append([1,2,3])\n",
555 | " print('Inside Function: ', lst)\n",
556 | "\n",
557 | "lst = [10,20,30]\n",
558 | "changeMe(lst)\n",
559 | "print('Main Program: ', lst)"
560 | ]
561 | },
562 | {
563 | "cell_type": "code",
564 | "execution_count": 22,
565 | "metadata": {
566 | "ExecuteTime": {
567 | "end_time": "2018-05-28T18:11:51.778255Z",
568 | "start_time": "2018-05-28T18:11:51.762590Z"
569 | }
570 | },
571 | "outputs": [
572 | {
573 | "name": "stdout",
574 | "output_type": "stream",
575 | "text": [
576 | "Inside Function: [1, 2, 3]\n",
577 | "Main Program: [10, 20, 30]\n"
578 | ]
579 | }
580 | ],
581 | "source": [
582 | "def changeMe(lst):\n",
583 | " '''Changes List'''\n",
584 | " lst = [1, 2, 3]\n",
585 | " print('Inside Function: ', lst)\n",
586 | "\n",
587 | "lst = [10, 20, 30]\n",
588 | "changeMe(lst)\n",
589 | "print('Main Program: ', lst)"
590 | ]
591 | },
592 | {
593 | "cell_type": "markdown",
594 | "metadata": {},
595 | "source": [
596 | "## Recursion"
597 | ]
598 | },
599 | {
600 | "cell_type": "markdown",
601 | "metadata": {},
602 | "source": [
603 | "A function calling itself. Recursive Functions.\n"
604 | ]
605 | },
606 | {
607 | "cell_type": "code",
608 | "execution_count": 28,
609 | "metadata": {
610 | "ExecuteTime": {
611 | "end_time": "2018-05-29T08:05:26.397403Z",
612 | "start_time": "2018-05-29T08:05:26.388377Z"
613 | }
614 | },
615 | "outputs": [
616 | {
617 | "name": "stdout",
618 | "output_type": "stream",
619 | "text": [
620 | "Factorial of 6 is 720\n"
621 | ]
622 | }
623 | ],
624 | "source": [
625 | "def factorial(num):\n",
626 | " '''Recursive Function to find Factorial'''\n",
627 | "# if num == 1:\n",
628 | "# return 1\n",
629 | "# else:\n",
630 | "# return (num * factorial(num-1))\n",
631 | " return 1 if num == 1 else (num * factorial(num-1))\n",
632 | "num = 6\n",
633 | "print('Factorial of {} is {}'.format(num, factorial(num)))"
634 | ]
635 | },
636 | {
637 | "cell_type": "code",
638 | "execution_count": 34,
639 | "metadata": {
640 | "ExecuteTime": {
641 | "end_time": "2018-05-28T18:48:35.454692Z",
642 | "start_time": "2018-05-28T18:48:35.444682Z"
643 | }
644 | },
645 | "outputs": [
646 | {
647 | "name": "stdout",
648 | "output_type": "stream",
649 | "text": [
650 | "0\n",
651 | "1\n",
652 | "1\n",
653 | "2\n",
654 | "3\n",
655 | "5\n",
656 | "8\n",
657 | "13\n",
658 | "21\n",
659 | "34\n"
660 | ]
661 | }
662 | ],
663 | "source": [
664 | "def fibonacci(num):\n",
665 | " '''Recursive function to find Fibonacci Sequence'''\n",
666 | " if num == 0 or num== 1:\n",
667 | " return num\n",
668 | " else:\n",
669 | " return fibonacci(num-1) + fibonacci(num-2)\n",
670 | " \n",
671 | "n = 10\n",
672 | "for num in range(n):\n",
673 | " print(fibonacci(num))"
674 | ]
675 | },
676 | {
677 | "cell_type": "markdown",
678 | "metadata": {},
679 | "source": [
680 | "## Assignments"
681 | ]
682 | },
683 | {
684 | "cell_type": "markdown",
685 | "metadata": {},
686 | "source": [
687 | "Q.2- Write a function “perfect()” that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000.\n",
688 | "[An integer number is said to be “perfect number” if its factors, including 1(but not the number itself), sum to the number. E.g., 6 is a perfect number because 6=1+2+3].\n",
689 | "\n",
690 | "Q.3- Print multiplication table of 12 using recursion. \n",
691 | "\n",
692 | "Q.4- Write a function to calculate power of a number raised to other ( a^b ) using recursion. \n",
693 | "\n",
694 | "Q.5- Write a function to find factorial of a number but also store the factorials calculated in a dictionary "
695 | ]
696 | },
697 | {
698 | "cell_type": "markdown",
699 | "metadata": {},
700 | "source": [
701 | "## Anonymous or Lambda Functions"
702 | ]
703 | },
704 | {
705 | "cell_type": "code",
706 | "execution_count": 37,
707 | "metadata": {
708 | "ExecuteTime": {
709 | "end_time": "2018-05-28T18:56:59.658953Z",
710 | "start_time": "2018-05-28T18:56:59.648942Z"
711 | }
712 | },
713 | "outputs": [
714 | {
715 | "name": "stdout",
716 | "output_type": "stream",
717 | "text": [
718 | "100\n"
719 | ]
720 | }
721 | ],
722 | "source": [
723 | "def square(num):\n",
724 | " '''This function returns square'''\n",
725 | " return num*num\n",
726 | " \n",
727 | "x = square(10)\n",
728 | "print(x)"
729 | ]
730 | },
731 | {
732 | "cell_type": "code",
733 | "execution_count": 36,
734 | "metadata": {
735 | "ExecuteTime": {
736 | "end_time": "2018-05-28T18:56:02.239044Z",
737 | "start_time": "2018-05-28T18:56:02.234039Z"
738 | }
739 | },
740 | "outputs": [
741 | {
742 | "name": "stdout",
743 | "output_type": "stream",
744 | "text": [
745 | "25\n"
746 | ]
747 | }
748 | ],
749 | "source": [
750 | "square = lambda x : x**2\n",
751 | "print(square(5))"
752 | ]
753 | }
754 | ],
755 | "metadata": {
756 | "kernelspec": {
757 | "display_name": "Python 3",
758 | "language": "python",
759 | "name": "python3"
760 | },
761 | "language_info": {
762 | "codemirror_mode": {
763 | "name": "ipython",
764 | "version": 3
765 | },
766 | "file_extension": ".py",
767 | "mimetype": "text/x-python",
768 | "name": "python",
769 | "nbconvert_exporter": "python",
770 | "pygments_lexer": "ipython3",
771 | "version": "3.6.5"
772 | },
773 | "toc": {
774 | "nav_menu": {},
775 | "number_sections": true,
776 | "sideBar": true,
777 | "skip_h1_title": false,
778 | "title_cell": "Table of Contents",
779 | "title_sidebar": "Contents",
780 | "toc_cell": false,
781 | "toc_position": {},
782 | "toc_section_display": true,
783 | "toc_window_display": false
784 | },
785 | "varInspector": {
786 | "cols": {
787 | "lenName": 16,
788 | "lenType": 16,
789 | "lenVar": 40
790 | },
791 | "kernels_config": {
792 | "python": {
793 | "delete_cmd_postfix": "",
794 | "delete_cmd_prefix": "del ",
795 | "library": "var_list.py",
796 | "varRefreshCmd": "print(var_dic_list())"
797 | },
798 | "r": {
799 | "delete_cmd_postfix": ") ",
800 | "delete_cmd_prefix": "rm(",
801 | "library": "var_list.r",
802 | "varRefreshCmd": "cat(var_dic_list()) "
803 | }
804 | },
805 | "types_to_exclude": [
806 | "module",
807 | "function",
808 | "builtin_function_or_method",
809 | "instance",
810 | "_Feature"
811 | ],
812 | "window_display": false
813 | }
814 | },
815 | "nbformat": 4,
816 | "nbformat_minor": 2
817 | }
818 |
--------------------------------------------------------------------------------
/8. OOPs - 1/OOPs_1.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## Object Oriented Programming"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### Class Syntax"
15 | ]
16 | },
17 | {
18 | "cell_type": "code",
19 | "execution_count": 1,
20 | "metadata": {
21 | "ExecuteTime": {
22 | "end_time": "2018-05-31T06:29:51.398848Z",
23 | "start_time": "2018-05-31T06:29:51.391831Z"
24 | }
25 | },
26 | "outputs": [
27 | {
28 | "name": "stdout",
29 | "output_type": "stream",
30 | "text": [
31 | "This is my First Class.\n",
32 | "Help on class MyFirstClass in module __main__:\n",
33 | "\n",
34 | "class MyFirstClass(builtins.object)\n",
35 | " | This is my First Class.\n",
36 | " | \n",
37 | " | Data descriptors defined here:\n",
38 | " | \n",
39 | " | __dict__\n",
40 | " | dictionary for instance variables (if defined)\n",
41 | " | \n",
42 | " | __weakref__\n",
43 | " | list of weak references to the object (if defined)\n",
44 | "\n"
45 | ]
46 | }
47 | ],
48 | "source": [
49 | "class MyFirstClass:\n",
50 | " '''This is my First Class.'''\n",
51 | " pass\n",
52 | " \n",
53 | "print(MyFirstClass.__doc__)\n",
54 | "help(MyFirstClass)"
55 | ]
56 | },
57 | {
58 | "cell_type": "markdown",
59 | "metadata": {},
60 | "source": [
61 | "### Object Creation"
62 | ]
63 | },
64 | {
65 | "cell_type": "code",
66 | "execution_count": 3,
67 | "metadata": {
68 | "ExecuteTime": {
69 | "end_time": "2018-05-31T00:44:40.430601Z",
70 | "start_time": "2018-05-31T00:44:40.426591Z"
71 | }
72 | },
73 | "outputs": [],
74 | "source": [
75 | "my_ref_var = MyFirstClass()"
76 | ]
77 | },
78 | {
79 | "cell_type": "markdown",
80 | "metadata": {},
81 | "source": [
82 | "### Accessing Methods of Class"
83 | ]
84 | },
85 | {
86 | "cell_type": "code",
87 | "execution_count": 5,
88 | "metadata": {
89 | "ExecuteTime": {
90 | "end_time": "2018-05-31T06:59:54.975039Z",
91 | "start_time": "2018-05-31T06:59:54.968021Z"
92 | }
93 | },
94 | "outputs": [
95 | {
96 | "name": "stdout",
97 | "output_type": "stream",
98 | "text": [
99 | "Hello World\n"
100 | ]
101 | }
102 | ],
103 | "source": [
104 | "class HelloWorld:\n",
105 | " '''This is my Hello World class'''\n",
106 | " \n",
107 | " def display(self):\n",
108 | " print('Hello World')\n",
109 | " \n",
110 | "h = HelloWorld()\n",
111 | "\n",
112 | "h.display()"
113 | ]
114 | },
115 | {
116 | "cell_type": "markdown",
117 | "metadata": {},
118 | "source": [
119 | "### Constructor"
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": 26,
125 | "metadata": {
126 | "ExecuteTime": {
127 | "end_time": "2018-05-31T01:42:36.794529Z",
128 | "start_time": "2018-05-31T01:42:36.789527Z"
129 | }
130 | },
131 | "outputs": [
132 | {
133 | "name": "stdout",
134 | "output_type": "stream",
135 | "text": [
136 | "abc 1 90\n"
137 | ]
138 | }
139 | ],
140 | "source": [
141 | "class Student:\n",
142 | " def __init__(self):\n",
143 | " self.name = 'abc'\n",
144 | " self.rno = 1\n",
145 | " self.marks = 90\n",
146 | " \n",
147 | "s1 = Student()\n",
148 | "\n",
149 | "print(s1.name, s1.rno, s1.marks)"
150 | ]
151 | },
152 | {
153 | "cell_type": "code",
154 | "execution_count": 8,
155 | "metadata": {
156 | "ExecuteTime": {
157 | "end_time": "2018-05-31T01:04:42.032829Z",
158 | "start_time": "2018-05-31T01:04:42.028818Z"
159 | }
160 | },
161 | "outputs": [
162 | {
163 | "name": "stdout",
164 | "output_type": "stream",
165 | "text": [
166 | "<__main__.Student object at 0x087E0E50>\n"
167 | ]
168 | }
169 | ],
170 | "source": [
171 | "print(s1)"
172 | ]
173 | },
174 | {
175 | "cell_type": "code",
176 | "execution_count": 6,
177 | "metadata": {
178 | "ExecuteTime": {
179 | "end_time": "2018-05-31T07:12:30.112227Z",
180 | "start_time": "2018-05-31T07:12:30.105709Z"
181 | }
182 | },
183 | "outputs": [
184 | {
185 | "name": "stdout",
186 | "output_type": "stream",
187 | "text": [
188 | "Constructor is executed\n",
189 | "Constructor is executed\n",
190 | "Constructor is executed\n",
191 | "Function is executed\n",
192 | "Function is executed\n"
193 | ]
194 | }
195 | ],
196 | "source": [
197 | "class Test:\n",
198 | " def __init__(self):\n",
199 | " print('Constructor is executed')\n",
200 | " \n",
201 | " def fun(self):\n",
202 | " print('Function is executed')\n",
203 | " \n",
204 | "t1 = Test()\n",
205 | "t2 = Test()\n",
206 | "t3 = Test()\n",
207 | "t1.fun()\n",
208 | "t2.fun()\n",
209 | "# def display(self):\n",
210 | "# print('Name: {}, RollNo: {}, Marks: {}'.format(name, rno, marks))\n",
211 | " \n",
212 | "# t3.fun()"
213 | ]
214 | },
215 | {
216 | "cell_type": "markdown",
217 | "metadata": {},
218 | "source": [
219 | "### Constructor With Multiple Parameters"
220 | ]
221 | },
222 | {
223 | "cell_type": "code",
224 | "execution_count": 11,
225 | "metadata": {
226 | "ExecuteTime": {
227 | "end_time": "2018-05-31T07:33:46.776383Z",
228 | "start_time": "2018-05-31T07:33:46.748825Z"
229 | }
230 | },
231 | "outputs": [
232 | {
233 | "name": "stdout",
234 | "output_type": "stream",
235 | "text": [
236 | "Name: abc, RollNo: 1, Marks: 90\n",
237 | "Name: xyz, RollNo: 2, Marks: 80\n"
238 | ]
239 | },
240 | {
241 | "data": {
242 | "text/plain": [
243 | "\"INSTANCE variable should be used using 'self', \\nwhether inside constructor or method\\n\""
244 | ]
245 | },
246 | "execution_count": 11,
247 | "metadata": {},
248 | "output_type": "execute_result"
249 | }
250 | ],
251 | "source": [
252 | "class Student:\n",
253 | " def __init__(self, name, rno, marks):\n",
254 | " self.name = name\n",
255 | " self.rno = rno\n",
256 | " self.marks = marks\n",
257 | " def display(self):\n",
258 | " print('Name: {}, RollNo: {}, Marks: {}'.\\\n",
259 | " format(self.name, self.rno, self.marks))\n",
260 | "\n",
261 | " \n",
262 | "s1 = Student('abc', 1, 90)\n",
263 | "\n",
264 | "s1.display()\n",
265 | "\n",
266 | "s2 = Student('xyz', 2, 80)\n",
267 | "\n",
268 | "s2.display()\n",
269 | "\n",
270 | "'''INSTANCE variable should be used using 'self', \n",
271 | "whether inside constructor or method\n",
272 | "'''"
273 | ]
274 | },
275 | {
276 | "cell_type": "markdown",
277 | "metadata": {},
278 | "source": [
279 | "## Variables "
280 | ]
281 | },
282 | {
283 | "cell_type": "markdown",
284 | "metadata": {},
285 | "source": [
286 | "### Instance Variable"
287 | ]
288 | },
289 | {
290 | "cell_type": "code",
291 | "execution_count": 13,
292 | "metadata": {
293 | "ExecuteTime": {
294 | "end_time": "2018-05-31T07:39:52.774447Z",
295 | "start_time": "2018-05-31T07:39:52.767429Z"
296 | }
297 | },
298 | "outputs": [
299 | {
300 | "name": "stdout",
301 | "output_type": "stream",
302 | "text": [
303 | "{'name': 'abc', 'rno': 1, 'marks': 90}\n",
304 | "abc\n",
305 | "Name: abc, RollNo: 1, Marks: 90,\n",
306 | "Name: xyz, RollNo: 2, Marks: 80,\n"
307 | ]
308 | }
309 | ],
310 | "source": [
311 | "class Student:\n",
312 | " def __init__(self, name, rno, marks):\n",
313 | " self.name = name\n",
314 | " self.rno = rno\n",
315 | " self.marks = marks\n",
316 | " \n",
317 | " def display(self):\n",
318 | " print('Name: {}, RollNo: {}, Marks: {},'.\\\n",
319 | " format(self.name, self.rno, self.marks))\n",
320 | " \n",
321 | "s1 = Student('abc', 1, 90)\n",
322 | "\n",
323 | "print(s1.__dict__)\n",
324 | "\n",
325 | "print(s1.name)\n",
326 | "\n",
327 | "s1.display()\n",
328 | "\n",
329 | "s2 = Student('xyz', 2, 80)\n",
330 | "\n",
331 | "s2.display()"
332 | ]
333 | },
334 | {
335 | "cell_type": "markdown",
336 | "metadata": {},
337 | "source": [
338 | "### Static Variable"
339 | ]
340 | },
341 | {
342 | "cell_type": "code",
343 | "execution_count": 15,
344 | "metadata": {
345 | "ExecuteTime": {
346 | "end_time": "2018-05-31T07:54:33.058179Z",
347 | "start_time": "2018-05-31T07:54:33.051659Z"
348 | }
349 | },
350 | "outputs": [
351 | {
352 | "name": "stdout",
353 | "output_type": "stream",
354 | "text": [
355 | "Name: abc, RollNo: 1, Marks: 90, College: DIT\n",
356 | "Name: xyz, RollNo: 2, Marks: 80, College: DIT\n"
357 | ]
358 | }
359 | ],
360 | "source": [
361 | "class Student:\n",
362 | " college = 'DIT'\n",
363 | " def __init__(self, name, rno, marks):\n",
364 | " self.name = name\n",
365 | " self.rno = rno\n",
366 | " self.marks = marks\n",
367 | " \n",
368 | " def display(self):\n",
369 | " print('Name: {}, RollNo: {}, Marks: {},'.\\\n",
370 | " format(self.name, self.rno, self.marks), end=' ')\n",
371 | " print('College:', self.college)\n",
372 | " \n",
373 | "s1 = Student('abc', 1, 90)\n",
374 | "\n",
375 | "s1.display()\n",
376 | "\n",
377 | "s2 = Student('xyz', 2, 80)\n",
378 | "\n",
379 | "s2.display()"
380 | ]
381 | },
382 | {
383 | "cell_type": "code",
384 | "execution_count": 16,
385 | "metadata": {
386 | "ExecuteTime": {
387 | "end_time": "2018-05-31T07:56:03.118244Z",
388 | "start_time": "2018-05-31T07:56:03.115236Z"
389 | }
390 | },
391 | "outputs": [],
392 | "source": [
393 | "# # Lets try to change the STATIC variable using s1\n",
394 | "\n",
395 | "s1.college = 'UIT'\n",
396 | "\n",
397 | "s1.display()\n",
398 | "\n",
399 | "s2.display()"
400 | ]
401 | },
402 | {
403 | "cell_type": "markdown",
404 | "metadata": {},
405 | "source": [
406 | "### Local Variable"
407 | ]
408 | },
409 | {
410 | "cell_type": "code",
411 | "execution_count": 18,
412 | "metadata": {
413 | "ExecuteTime": {
414 | "end_time": "2018-05-31T08:01:15.905176Z",
415 | "start_time": "2018-05-31T08:01:15.900162Z"
416 | }
417 | },
418 | "outputs": [
419 | {
420 | "name": "stdout",
421 | "output_type": "stream",
422 | "text": [
423 | "10\n",
424 | "20\n"
425 | ]
426 | }
427 | ],
428 | "source": [
429 | "class Test:\n",
430 | " def m1(self):\n",
431 | " x = 10\n",
432 | " print(x)\n",
433 | " def m2(self):\n",
434 | " y = 20\n",
435 | " print(y)\n",
436 | " \n",
437 | "t = Test()\n",
438 | "t.m1()\n",
439 | "t.m2()"
440 | ]
441 | },
442 | {
443 | "cell_type": "markdown",
444 | "metadata": {},
445 | "source": [
446 | "## Setters & Getters"
447 | ]
448 | },
449 | {
450 | "cell_type": "code",
451 | "execution_count": 19,
452 | "metadata": {
453 | "ExecuteTime": {
454 | "end_time": "2018-05-31T08:07:08.913643Z",
455 | "start_time": "2018-05-31T08:07:08.898091Z"
456 | }
457 | },
458 | "outputs": [
459 | {
460 | "name": "stdout",
461 | "output_type": "stream",
462 | "text": [
463 | "Name: abc, RollNo: 1, Marks: 90, College: UIT\n",
464 | "Name: xyz, RollNo: 2, Marks: 80, College: DIT\n"
465 | ]
466 | }
467 | ],
468 | "source": [
469 | "class Student:\n",
470 | " college = 'DIT'\n",
471 | " def __init__(self, name, rno, marks):\n",
472 | " self.name = name\n",
473 | " self.rno = rno\n",
474 | " self.marks = marks\n",
475 | " \n",
476 | " def display(self):\n",
477 | " print('Name: {}, RollNo: {}, Marks: {},'.format(self.name, self.rno, self.marks), end=' ')\n",
478 | " print('College:', self.college)\n",
479 | " \n",
480 | " def setCollege(self, cname):\n",
481 | " self.college = cname\n",
482 | " \n",
483 | " def getName(self):\n",
484 | " return self.name\n",
485 | " \n",
486 | "s1 = Student('abc', 1, 90)\n",
487 | "\n",
488 | "s1.setCollege('UIT')\n",
489 | "\n",
490 | "s1.display()\n",
491 | "\n",
492 | "s2 = Student('xyz', 2, 80)\n",
493 | "\n",
494 | "s2.display()"
495 | ]
496 | },
497 | {
498 | "cell_type": "code",
499 | "execution_count": 20,
500 | "metadata": {
501 | "ExecuteTime": {
502 | "end_time": "2018-05-31T08:08:19.518050Z",
503 | "start_time": "2018-05-31T08:08:19.513040Z"
504 | }
505 | },
506 | "outputs": [
507 | {
508 | "data": {
509 | "text/plain": [
510 | "'xyz'"
511 | ]
512 | },
513 | "execution_count": 20,
514 | "metadata": {},
515 | "output_type": "execute_result"
516 | }
517 | ],
518 | "source": [
519 | "s2.getName()"
520 | ]
521 | },
522 | {
523 | "cell_type": "markdown",
524 | "metadata": {},
525 | "source": [
526 | "## Assignment"
527 | ]
528 | },
529 | {
530 | "cell_type": "markdown",
531 | "metadata": {},
532 | "source": [
533 | "Q.1- Create a circle class and initialize it with radius. Make two methods getArea and getCircumference inside this class.\n",
534 | "\n",
535 | "Q.2- Create a Student class and initialize it with name and roll number. Make methods to :\n",
536 | "1. Display - It should display all informations of the student.\n",
537 | "2. setAge - It should assign age to student \n",
538 | "3. setMarks - It should assign marks to the student.\n",
539 | "\n",
540 | "Q.3- Create a Temprature class. Make two methods :\n",
541 | "1. convertFahrenheit - It will take celsius and will print it into Fahrenheit.\n",
542 | "2. convertCelsius - It will take Fahrenheit and will convert it into Celsius.\n",
543 | "\n",
544 | "Q.4- Create a class MovieDetails and initialize it with artistname,Year of release and ratings .\n",
545 | "Make methods to\n",
546 | "1. Display-Display the details.\n",
547 | "2. Add- Add the movie details.\n",
548 | "\n",
549 | "Q.5- Create a class Expenditure and initialize it with salary,savings, category , total expenditure.Make the following methods.\n",
550 | "1. Add expenditure according to category .\n",
551 | "2. Calculate total expenditure.\n",
552 | "3. Calculate per day and monthly expenditure.\n",
553 | "\n"
554 | ]
555 | }
556 | ],
557 | "metadata": {
558 | "kernelspec": {
559 | "display_name": "Python 3",
560 | "language": "python",
561 | "name": "python3"
562 | },
563 | "language_info": {
564 | "codemirror_mode": {
565 | "name": "ipython",
566 | "version": 3
567 | },
568 | "file_extension": ".py",
569 | "mimetype": "text/x-python",
570 | "name": "python",
571 | "nbconvert_exporter": "python",
572 | "pygments_lexer": "ipython3",
573 | "version": "3.6.5"
574 | },
575 | "toc": {
576 | "nav_menu": {},
577 | "number_sections": true,
578 | "sideBar": true,
579 | "skip_h1_title": false,
580 | "title_cell": "Table of Contents",
581 | "title_sidebar": "Contents",
582 | "toc_cell": false,
583 | "toc_position": {},
584 | "toc_section_display": true,
585 | "toc_window_display": false
586 | },
587 | "varInspector": {
588 | "cols": {
589 | "lenName": 16,
590 | "lenType": 16,
591 | "lenVar": 40
592 | },
593 | "kernels_config": {
594 | "python": {
595 | "delete_cmd_postfix": "",
596 | "delete_cmd_prefix": "del ",
597 | "library": "var_list.py",
598 | "varRefreshCmd": "print(var_dic_list())"
599 | },
600 | "r": {
601 | "delete_cmd_postfix": ") ",
602 | "delete_cmd_prefix": "rm(",
603 | "library": "var_list.r",
604 | "varRefreshCmd": "cat(var_dic_list()) "
605 | }
606 | },
607 | "types_to_exclude": [
608 | "module",
609 | "function",
610 | "builtin_function_or_method",
611 | "instance",
612 | "_Feature"
613 | ],
614 | "window_display": false
615 | }
616 | },
617 | "nbformat": 4,
618 | "nbformat_minor": 2
619 | }
620 |
--------------------------------------------------------------------------------
/Assignments/1 - Intro to Python.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/1 - Intro to Python.docx
--------------------------------------------------------------------------------
/Assignments/10 - Threads and Processes.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/10 - Threads and Processes.docx
--------------------------------------------------------------------------------
/Assignments/11 - Web Basics and API.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/11 - Web Basics and API.docx
--------------------------------------------------------------------------------
/Assignments/12 - Exception Handling.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/12 - Exception Handling.docx
--------------------------------------------------------------------------------
/Assignments/13 - File Handling.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/13 - File Handling.docx
--------------------------------------------------------------------------------
/Assignments/14 - Regular Expressions.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/14 - Regular Expressions.docx
--------------------------------------------------------------------------------
/Assignments/15 - Databses in Python.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/15 - Databses in Python.docx
--------------------------------------------------------------------------------
/Assignments/16 - GUI - I.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/16 - GUI - I.docx
--------------------------------------------------------------------------------
/Assignments/17 - GUI - II.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/17 - GUI - II.docx
--------------------------------------------------------------------------------
/Assignments/18 - Pandas.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/18 - Pandas.docx
--------------------------------------------------------------------------------
/Assignments/2 - Data Types I.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/2 - Data Types I.docx
--------------------------------------------------------------------------------
/Assignments/3 - Data Types II.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/3 - Data Types II.docx
--------------------------------------------------------------------------------
/Assignments/4 - Decision Control System.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/4 - Decision Control System.docx
--------------------------------------------------------------------------------
/Assignments/5 - Loops.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/5 - Loops.docx
--------------------------------------------------------------------------------
/Assignments/6 - Functions.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/6 - Functions.docx
--------------------------------------------------------------------------------
/Assignments/7 - Common Modules in Python.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/7 - Common Modules in Python.docx
--------------------------------------------------------------------------------
/Assignments/8 - Classes and Modules I.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/8 - Classes and Modules I.docx
--------------------------------------------------------------------------------
/Assignments/9 - Classes and Modules II.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Assignments/9 - Classes and Modules II.docx
--------------------------------------------------------------------------------
/Helicopter/Helicopter.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/Helicopter/Helicopter.png
--------------------------------------------------------------------------------
/Helicopter/helicopter_game.py:
--------------------------------------------------------------------------------
1 | import pygame
2 | import time
3 | from random import randint,randrange
4 |
5 | black = (0,0,0)
6 | white = (255,255,255)
7 |
8 | sunset = (253,72,47)
9 |
10 | greenyellow = (184,255,0)
11 | brightblue = (47,228,253)
12 | orange = (255,113,0)
13 | yellow = (255,236,0)
14 | purple = (252,67,255)
15 |
16 | colorChoices = [greenyellow,brightblue,orange,yellow,purple]
17 |
18 | pygame.init()
19 |
20 | surfaceWidth = 800
21 | surfaceHeight = 500
22 |
23 | imageHeight = 43
24 | imageWidth = 100
25 |
26 | surface = pygame.display.set_mode((surfaceWidth,surfaceHeight))
27 | pygame.display.set_caption('Helicopter')
28 | clock = pygame.time.Clock()
29 |
30 | img = pygame.image.load('Helicopter.png')
31 |
32 | def score(count):
33 | font = pygame.font.Font('freesansbold.ttf', 20)
34 | text = font.render("Score: "+str(count), True, white)
35 | surface.blit(text, [0,0])
36 |
37 |
38 | def blocks(x_block, y_block, block_width, block_height, gap, colorChoice):
39 |
40 | pygame.draw.rect(surface, colorChoice, [x_block,y_block,block_width,block_height])
41 | pygame.draw.rect(surface, colorChoice, [x_block,y_block+block_height+gap,block_width, surfaceHeight])
42 |
43 |
44 | def replay_or_quit():
45 | for event in pygame.event.get([pygame.KEYDOWN, pygame.KEYUP, pygame.QUIT]):
46 | if event.type == pygame.QUIT:
47 | pygame.quit()
48 | quit()
49 |
50 | elif event.type == pygame.KEYDOWN:
51 | continue
52 |
53 | return event.key
54 |
55 | return None
56 |
57 | def makeTextObjs(text, font):
58 | textSurface = font.render(text, True, sunset)
59 | return textSurface, textSurface.get_rect()
60 |
61 | def msgSurface(text):
62 | smallText = pygame.font.Font('freesansbold.ttf', 20)
63 | largeText = pygame.font.Font('freesansbold.ttf', 150)
64 |
65 | titleTextSurf, titleTextRect = makeTextObjs(text, largeText)
66 | titleTextRect.center = surfaceWidth / 2, surfaceHeight / 2
67 | surface.blit(titleTextSurf, titleTextRect)
68 |
69 | typTextSurf, typTextRect = makeTextObjs('Press any key to continue', smallText)
70 | typTextRect.center = surfaceWidth / 2, ((surfaceHeight / 2) + 100)
71 | surface.blit(typTextSurf, typTextRect)
72 |
73 | pygame.display.update()
74 | time.sleep(1)
75 |
76 | while replay_or_quit() == None:
77 | clock.tick()
78 |
79 | main()
80 |
81 |
82 |
83 | def gameOver():
84 | msgSurface('Kaboom!')
85 |
86 | def helicopter(x, y, image):
87 | surface.blit(img, (x,y))
88 |
89 |
90 | def main():
91 | x = 150
92 | y = 200
93 | y_move = 0
94 |
95 | x_block = surfaceWidth
96 | y_block = 0
97 |
98 | block_width = 75
99 | block_height = randint(0,(surfaceHeight/2))
100 | gap = imageHeight * 3
101 | block_move = 4
102 | current_score = 0
103 |
104 |
105 | blockColor = colorChoices[randrange(0,len(colorChoices))]
106 |
107 | game_over = False
108 |
109 | while not game_over:
110 |
111 | for event in pygame.event.get():
112 | if event.type == pygame.QUIT:
113 | game_over = True
114 |
115 | if event.type == pygame.KEYDOWN:
116 | if event.key == pygame.K_UP:
117 | y_move = -5
118 |
119 | if event.type == pygame.KEYUP:
120 | if event.key == pygame.K_UP:
121 | y_move = 5
122 |
123 | y += y_move
124 |
125 | surface.fill(black)
126 | helicopter(x ,y, img)
127 |
128 |
129 | blocks(x_block, y_block, block_width, block_height, gap, blockColor)
130 | score(current_score)
131 | x_block -= block_move
132 |
133 | if y > surfaceHeight-40 or y < 0:
134 | gameOver()
135 |
136 | if x_block < (-1*block_width):
137 | x_block = surfaceWidth
138 | block_height = randint(0, (surfaceHeight / 2))
139 | blockColor = colorChoices[randrange(0,len(colorChoices))]
140 | current_score+=1
141 |
142 | if x + imageWidth > x_block:
143 | if x < x_block + block_width:
144 | if y < block_height:
145 | if x - imageWidth < block_width + x_block:
146 | gameOver()
147 |
148 | if x + imageWidth > x_block:
149 | if y + imageHeight > block_height+gap:
150 | if x < block_width + x_block:
151 | gameOver()
152 |
153 | #if x_block < (x - block_width) < x_block + block_move:
154 | # current_score += 1
155 |
156 | if 3 <= current_score < 5:
157 | block_move = 5
158 | gap = imageHeight * 2.9
159 | if 5 <= current_score < 8:
160 | block_move = 6
161 | gap = imageHeight *2.8
162 | if 8 <= current_score < 14:
163 | block_move = 7
164 | gap = imageHeight *2.7
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 | pygame.display.update()
173 | clock.tick(60)
174 |
175 | main()
176 | pygame.quit()
177 | quit()
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
--------------------------------------------------------------------------------
/Library Management System/backend.py:
--------------------------------------------------------------------------------
1 | import pymysql as pm
2 |
3 | def connectdb():
4 | try:
5 | con = pm.connect(host='localhost', database='books', \
6 | user='root', password='root')
7 | cursor = con.cursor()
8 | query = 'create table if not exists book(id int(5) primary key auto_increment, title varchar(10), author varchar(10), year int(4), isbn int(20))'
9 | cursor.execute(query)
10 | con.commit()
11 |
12 | except pm.DatabaseError as e:
13 | if con:
14 | con.rollback()
15 | print('Problem occured: ', e)
16 |
17 | finally:
18 | if cursor:
19 | cursor.close()
20 | if con:
21 | con.close()
22 |
23 | def insert(title, author, year, isbn):
24 | try:
25 | con = pm.connect(host='localhost', database='books', \
26 | user='root', password='root')
27 | cursor = con.cursor()
28 | query = 'insert into book values (NULL, %s, %s, %s, %s)'
29 | data = (title, author, year, isbn)
30 | cursor.execute(query, data)
31 | con.commit()
32 |
33 | except pm.DatabaseError as e:
34 | if con:
35 | con.rollback()
36 | print('Problem occured: ', e)
37 |
38 | finally:
39 | if cursor:
40 | cursor.close()
41 | if con:
42 | con.close()
43 |
44 | def view():
45 | try:
46 | con = pm.connect(host='localhost', database='books', \
47 | user='root', password='root')
48 | cursor = con.cursor()
49 | query = 'select * from book'
50 | cursor.execute(query)
51 | data = cursor.fetchall()
52 | return data
53 |
54 | except pm.DatabaseError as e:
55 | if con:
56 | con.rollback()
57 | print('Problem occured: ', e)
58 |
59 | finally:
60 | if cursor:
61 | cursor.close()
62 | if con:
63 | con.close()
64 |
65 | def search(title='', author='', year='', isbn=''):
66 | try:
67 | con = pm.connect(host='localhost', database='books', \
68 | user='root', password='root')
69 | cursor = con.cursor()
70 | query = 'select * from book where title=%s OR author=%s OR year=%s OR isbn=%s'
71 | d = (title, author, year, isbn)
72 | cursor.execute(query, d)
73 | data = cursor.fetchall()
74 | return data
75 |
76 | except pm.DatabaseError as e:
77 | if con:
78 | con.rollback()
79 | print('Problem occured: ', e)
80 |
81 | finally:
82 | if cursor:
83 | cursor.close()
84 | if con:
85 | con.close()
86 |
87 | def delete(id):
88 | try:
89 | con = pm.connect(host='localhost', database='books', \
90 | user='root', password='root')
91 | cursor = con.cursor()
92 | query = 'delete from book where id=%s'
93 | cursor.execute(query, id)
94 | con.commit()
95 |
96 | except pm.DatabaseError as e:
97 | if con:
98 | con.rollback()
99 | print('Problem occured: ', e)
100 |
101 | finally:
102 | if cursor:
103 | cursor.close()
104 | if con:
105 | con.close()
106 |
107 | def update(id, title, author, year, isbn):
108 | try:
109 | con = pm.connect(host='localhost', database='books', \
110 | user='root', password='root')
111 | cursor = con.cursor()
112 | query = 'update book set title=%s, author=%s, year=%s, isbn=%s where id=%s'
113 | data = (title, author, year, isbn, id)
114 | cursor.execute(query, data)
115 | con.commit()
116 |
117 | except pm.DatabaseError as e:
118 | if con:
119 | con.rollback()
120 | print('Problem occured: ', e)
121 |
122 | finally:
123 | if cursor:
124 | cursor.close()
125 | if con:
126 | con.close()
127 |
--------------------------------------------------------------------------------
/Library Management System/frontend.py:
--------------------------------------------------------------------------------
1 | from tkinter import *
2 | from backend import *
3 |
4 | root = Tk()
5 |
6 | root.title('Library Management System')
7 |
8 | def get_data():
9 | title=e1.get()
10 | author=e2.get()
11 | year=e3.get()
12 | isbn=e4.get()
13 | return title, author, year, isbn
14 |
15 |
16 | def get_id():
17 | row = lstbox.curselection()
18 | myString = lstbox.get(row)
19 | myString = myString.split()
20 | id = myString[0]
21 | return (id,)
22 |
23 |
24 | def front_fill(event):
25 | row = lstbox.curselection()
26 | myString = lstbox.get(row)
27 | myString = myString.split(' | ')
28 | title_text.set(myString[1])
29 | author_text.set(myString[2])
30 | year_text.set(myString[3])
31 | isbn_text.set(myString[4])
32 |
33 |
34 | def front_view():
35 | lstbox.delete(0, END)
36 | front_data = view()
37 | for row in front_data:
38 | lstbox.insert(END, str(row[0])+' | '+row[1]+' | '+row[2]\
39 | + ' | '+str(row[3])+' | '+str(row[4]))
40 |
41 |
42 | def front_search():
43 | lstbox.delete(0, END)
44 | title, author, year, isbn = get_data()
45 | front_data = search(title, author, year, isbn)
46 | for row in front_data:
47 | lstbox.insert(END, str(row[0]) + ' | ' + row[1] + ' | ' + row[2]\
48 | + ' | ' + str(row[3]) + ' | ' + str(row[4]))
49 |
50 |
51 | def front_add():
52 | title, author, year, isbn = get_data()
53 | insert(title, author, year, isbn)
54 | front_view()
55 |
56 |
57 | def front_update():
58 | id = get_id()
59 | title, author, year, isbn = get_data()
60 | update(id, title, author, year, isbn)
61 | front_view()
62 |
63 |
64 | def front_delete():
65 | id = get_id()
66 | delete(id)
67 | front_view()
68 |
69 |
70 | # define Labels
71 | l1 = Label(root, text='Title')
72 | l1.grid(row=0, column=0)
73 |
74 | l2 = Label(root, text='Author')
75 | l2.grid(row=0, column=2)
76 |
77 | l3 = Label(root, text='Year')
78 | l3.grid(row=1, column=0)
79 |
80 | l4 = Label(root, text='ISBN')
81 | l4.grid(row=1, column=2)
82 |
83 | # define Entries
84 | title_text=StringVar()
85 | e1=Entry(root,textvariable=title_text)
86 | e1.grid(row=0, column=1)
87 |
88 | author_text=StringVar()
89 | e2=Entry(root,textvariable=author_text)
90 | e2.grid(row=0, column=3)
91 |
92 | year_text=StringVar()
93 | e3=Entry(root,textvariable=year_text)
94 | e3.grid(row=1, column=1)
95 |
96 | isbn_text=StringVar()
97 | e4=Entry(root,textvariable=isbn_text)
98 | e4.grid(row=1, column=3)
99 |
100 | # define Listbox
101 | lstbox = Listbox(root, height=6, width=35)
102 | lstbox.grid(row=2, column=0, rowspan=6, columnspan=2)
103 | lstbox.bind('', front_fill)
104 |
105 |
106 | # add scrollbar to listbox
107 | sb = Scrollbar(root)
108 | sb.grid(row=2, column=2, rowspan=6)
109 |
110 | lstbox.configure(yscrollcommand=sb.set)
111 | sb.configure(command=lstbox.yview)
112 |
113 | # define Buttons
114 | b1 = Button(root, text='View All', width=12, command=front_view)
115 | b1.grid(row=2, column=3)
116 |
117 | b1 = Button(root, text='Search Entry', width=12, command=front_search)
118 | b1.grid(row=3, column=3)
119 |
120 | b1 = Button(root, text='Add Entry', width=12, command=front_add)
121 | b1.grid(row=4, column=3)
122 |
123 | b1 = Button(root, text='Update Selected', width=12, command=front_update)
124 | b1.grid(row=5, column=3)
125 |
126 | b1 = Button(root, text='Delete Selected', width=12, command=front_delete)
127 | b1.grid(row=6, column=3)
128 |
129 | b1 = Button(root, text='Close', width=12, command=root.destroy)
130 | b1.grid(row=7, column=3)
131 |
132 | connectdb()
133 | front_view()
134 |
135 | root.resizable(False, False)
136 |
137 | root.mainloop()
138 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # python_notes
2 |
3 | ## Chapters
4 | 1. Intro to Python
5 | 2. Data Types- 1
6 | 3. Data Types- 2
7 | 4. Decision Statement
8 | 5. Loops
9 | 6. Functions
10 | 7. Some important Modules
11 | 8. OOPs - 1
12 | 9. OOPs - 2
13 | 10. Threads
14 | 11. Exception Handling
15 | 12. File Handling
16 | 13. Regular Expression
17 | 14. Databases - MySQL
18 | 15. GUI - 1
19 | 16. GUI - 2
20 | 17. Numpy
21 | 18. Pandas
22 |
23 | ## Other Stuff
24 | 1. Assignments
25 | 2. Mini Project - Helicopter Game
26 | 3. Mini Project - Car Game
27 | 4. Mini Project - Library Management System
28 |
29 | ## References
30 | Time Complexity Analysis CLICK HERE
31 |
32 |
--------------------------------------------------------------------------------
/RaceCar/Helicopter.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/RaceCar/Helicopter.png
--------------------------------------------------------------------------------
/RaceCar/racecar.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/RaceCar/racecar.png
--------------------------------------------------------------------------------
/RaceCar/racecar.py:
--------------------------------------------------------------------------------
1 | import time
2 |
3 | import pygame
4 |
5 | # initiating pygame
6 | pygame.init()
7 |
8 | display_width = 800
9 | display_height = 500
10 |
11 | # Picking the resolution of our display
12 | surface = pygame.display.set_mode((display_width, display_height))
13 |
14 | pygame.display.set_caption('Helicopter')
15 |
16 | black = (0, 0, 0) # RGB
17 | white = (255, 255, 255)
18 |
19 | helicopter = pygame.image.load('Helicopter.png')
20 | x = 150
21 | y = 200
22 |
23 | y_move = 0
24 |
25 | ####################################
26 |
27 |
28 | def Helicopter(x, y):
29 | surface.blit(helicopter, (x, y))
30 |
31 |
32 | # def msgSurface(text):
33 | # smallText = pygame.font.Font('freesansbold.ttf', 20)
34 | # largeText = pygame.font.Font('freesansbold.ttf', 150)
35 | #
36 | # titleTextSurf, titleTextRect = makeTextObjs(text, largeText)
37 | # titleTextRect.center = display_width/2, display_height/2
38 | # surface.blit(titleTextSurf, titleTextRect)
39 | #
40 | # typTextSurf, typTextRect = makeTextObjs('Press any key to continue', smallText)
41 | # titleTextRect.center = display_width/2, ((display_height/2)+100)
42 | # surface.blit(typTextSurf, typTextRect)
43 | #
44 | # pygame.display.update()
45 | # time.sleep(1)
46 | #
47 |
48 |
49 | def gameOver():
50 | # msgSurface('Kaboom!')
51 | quit()
52 |
53 |
54 | ####################################
55 |
56 | # Measuring FPS-Frame per seconds
57 | clock = pygame.time.Clock()
58 |
59 | # Creating a game loop which will run many times a second
60 | game_over = False
61 |
62 | while not game_over:
63 | for event in pygame.event.get():
64 | if event.type == pygame.QUIT:
65 | game_over = True
66 |
67 | ###################################
68 | if event.type == pygame.KEYDOWN: # if a key is pressed
69 | if event.key == pygame.K_UP: # if that key is UP arrow key
70 | y_move = -5
71 | if event.type == pygame.KEYUP: # if we released a key
72 | if event.key == pygame.K_UP:
73 | y_move = 5
74 | ###################################
75 |
76 | y += y_move # y = y + y_move
77 |
78 | surface.fill(black)
79 | Helicopter(x, y)
80 |
81 | if y > display_height-43 or y < 0:
82 | gameOver()
83 |
84 | pygame.display.update() # update the display
85 | clock.tick(60) # run the above code 60 times per second
86 |
87 | pygame.quit()
88 |
89 | quit()
90 |
--------------------------------------------------------------------------------
/car-game-master/README.md:
--------------------------------------------------------------------------------
1 | # car_game
2 | game built using pygame
3 | By Harshit Ahluwalia
4 |
--------------------------------------------------------------------------------
/car-game-master/__pycache__/pygame_car.cpython-36.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/__pycache__/pygame_car.cpython-36.pyc
--------------------------------------------------------------------------------
/car-game-master/images/car.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/images/car.png
--------------------------------------------------------------------------------
/car-game-master/images/car1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/images/car1.png
--------------------------------------------------------------------------------
/car-game-master/images/carcrash.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/images/carcrash.png
--------------------------------------------------------------------------------
/car-game-master/images/trees.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/images/trees.jpg
--------------------------------------------------------------------------------
/car-game-master/music/atlanta.wav:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/music/atlanta.wav
--------------------------------------------------------------------------------
/car-game-master/music/coffee_stains.wav:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/music/coffee_stains.wav
--------------------------------------------------------------------------------
/car-game-master/music/crash.wav:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Singh-K01/Python-tutorials-Kanav-Bansal/05065cb71ab0a5b9ff17b5befe485294c8395d47/car-game-master/music/crash.wav
--------------------------------------------------------------------------------
/car-game-master/pygame_car.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | """
4 | Hi everyone
5 | This is a simple car game developed using pygame witn python 3
6 |
7 | """
8 | __author__ = "Mohamed Hamidat, C# and python Developer, hamidatmohamed@yahoo.fr"
9 |
10 | import pygame
11 | import time
12 | import random
13 |
14 |
15 |
16 | #display dimension
17 | display_width = 800
18 | display_height = 600
19 |
20 | #car dimension
21 | car_width = 70
22 | car_height = 140
23 |
24 | #colors
25 | black = (0,0,0)
26 | white = (255,255,255)
27 | blue =(53, 115, 255)
28 | red = (200,0,0)
29 | green = (0, 200, 0)
30 | bright_red = (255,0,0)
31 | bright_green = (0,255,0)
32 | pause = False
33 | score_game = 0
34 |
35 | game_display = pygame.display.set_mode((display_width, display_height))
36 | clock = pygame.time.Clock()
37 | #game setup
38 | def game_init():
39 | pygame.init()
40 | pygame.display.set_caption('Fast_and_Curious/Mohamed')
41 |
42 | #game_icon = pygame.image.load('carIcon.png')
43 | #pygame.display.set_icon(game_icon)
44 |
45 | #backgroundImg = pygame.image.load ('way.png')
46 |
47 | ##############---------FONCTIONS--------------##################
48 |
49 | def display(count, x, y, message_format = 'Dodged: %d'):
50 | """display the score"""
51 | # max_dodged = 10
52 | font = pygame.font.SysFont("comicsansms", 20)
53 | text = font.render(message_format%count, True, black)
54 | game_display.blit(text, (x, y))
55 |
56 | def things(thingX, thingY, thingW, thingH, color):
57 | """draw random things (car or anything)"""
58 | pygame.draw.rect(game_display, color, [thingX, thingY, thingW, thingH])
59 |
60 |
61 | def line(lineX, lineY, lineW, lineH, color):
62 | """draw way lines """
63 | pygame.draw.rect(game_display, color, [lineX, lineY, lineW, lineH])
64 |
65 |
66 | def load_image(x , y, image_name):
67 | img = pygame.image.load(image_name)
68 | game_display.blit(img, (x, y))
69 |
70 | def text_object(text, font):
71 | textSurface = font.render(text, True, black)
72 | return textSurface, textSurface.get_rect()
73 |
74 | def message_display(text):
75 | """display message after crash"""
76 | largeText = pygame.font.SysFont("comicsansms",115)
77 | textSurf, textRect = text_object(text, largeText)
78 | textRect.center = ((display_width/2) , (display_height/2))
79 | game_display.blit(textSurf, textRect)
80 |
81 | pygame.display.update()
82 |
83 | time.sleep(2)
84 |
85 | game_loop()
86 |
87 |
88 | def crash(x, y):
89 | car_crash = pygame.image.load('images/carcrash.png')
90 | game_display.blit(car_crash, ((x - 45), (y - 30)))
91 | crash_sound = pygame.mixer.Sound("music/crash.wav")
92 | pygame.mixer.Sound.play(crash_sound)
93 | pygame.mixer.music.stop()
94 | largeText = pygame.font.SysFont("comicsansms",90)
95 | textSurf, textRect = text_object("You Crashed!", largeText)
96 | textRect.center = ((display_width/2) , (display_height/4))
97 | game_display.blit(textSurf, textRect)
98 |
99 | while True:
100 | for event in pygame.event.get():
101 | if event.type == pygame.QUIT:
102 | pygame.quit()
103 | quit()
104 |
105 |
106 |
107 | button("Play Again", 150,250,100,50, green, bright_green, game_loop)
108 | button("Quit", 550,250,100,50, red, bright_red, quitgame)
109 |
110 |
111 | pygame.display.update()
112 | clock.tick(15)
113 |
114 |
115 | def button(msg, x, y, w, h, ic, ac, action=None):
116 | """message, dimension, active/inactive color"""
117 |
118 | mouse = pygame.mouse.get_pos()
119 | click = pygame.mouse.get_pressed()
120 | #print(mouse)
121 |
122 | if x+w > mouse[0] > x and y+h > mouse[1] > y:
123 | pygame.draw.rect(game_display, ac,(x, y,w,h))
124 | if click[0] == 1 and action != None:
125 | action()
126 |
127 | else:
128 | pygame.draw.rect(game_display, ic,(x, y,w,h))
129 |
130 | smallText = pygame.font.SysFont("comicsansms",20)
131 | textSurf, textRect = text_object(msg, smallText)
132 | textRect.center = ( (x+(w/2)), (y+(h/2)) )
133 | game_display.blit(textSurf, textRect)
134 |
135 |
136 | def quitgame():
137 | pygame.quit()
138 | quit()
139 |
140 | def game_unpause():
141 | global pause
142 | pause = False
143 |
144 | def game_pause():
145 | ############
146 | pygame.mixer.music.pause()
147 | #############
148 | while pause:
149 | for event in pygame.event.get():
150 | if event.type == pygame.QUIT:
151 | pygame.quit()
152 | quit()
153 |
154 | #gameDisplay.fill(white)
155 | largeText = pygame.font.SysFont("comicsansms",90)
156 | textSurf, textRect = text_object("Pause!", largeText)
157 | textRect.center = ((display_width/2) , (display_height/4))
158 | game_display.blit(textSurf, textRect)
159 |
160 | button("Continue !", 150,250,100,50, green, bright_green, game_unpause)
161 | button("Quit", 550,250,100,50, red, bright_red, quitgame)
162 |
163 |
164 | pygame.display.update()
165 | clock.tick(15)
166 |
167 |
168 | def game_intro():
169 |
170 | pygame.mixer.music.load("music/atlanta.wav")
171 | pygame.mixer.music.play(-1)
172 |
173 | intro = True
174 |
175 | while intro:
176 | for event in pygame.event.get():
177 | if event.type == pygame.QUIT:
178 | pygame.quit()
179 | quit()
180 | game_display.fill(white)
181 |
182 | largeText = pygame.font.SysFont("comicsansms",80)
183 | textSurf, textRect = text_object("Let's Ride !", largeText)
184 | textRect.center = ((display_width/2) , (display_height/2))
185 | game_display.blit(textSurf, textRect)
186 |
187 | button("GO !", 150,450,100,50, green, bright_green, game_loop)
188 | button("Quit", 550,450,100,50, red, bright_red, quitgame)
189 |
190 |
191 | pygame.display.update()
192 | clock.tick(15)
193 |
194 | def game_loop():
195 | global pause
196 | global score_game
197 |
198 | pygame.mixer.music.load('music/coffee_stains.wav')
199 | pygame.mixer.music.play(-1)
200 |
201 | x = (display_width * 0.45)
202 | y = (display_height * 0.75)
203 |
204 | x_change = 0
205 | y_change = 0
206 | speed_change = 0
207 |
208 | thing_width = 70
209 | thing_height = 140
210 |
211 | thing_startx = random.randrange(100, display_width - 200)
212 | thing_starty = -600
213 | thing_speed = 4
214 |
215 | lineX = 400
216 | lineY = 0
217 | lineW = 20
218 | lineH = 450
219 | line_speed = 10
220 |
221 | tree_y_right = 600
222 | tree_y_left = 300
223 | tree_h = 600
224 | tree_speed = 10
225 |
226 | dodged = 0
227 |
228 | gameExit = False
229 |
230 | while not gameExit:
231 | for event in pygame.event.get():
232 | if event.type == pygame.QUIT:
233 | pygame.quit()
234 | quit()
235 |
236 | if event.type== pygame.KEYDOWN:
237 |
238 | if event.key==pygame.K_LEFT:
239 | x_change=-5
240 | if event.key == pygame.K_RIGHT:
241 | x_change=5
242 |
243 | if event.key == pygame.K_p:
244 | pause = True
245 | game_pause()
246 |
247 | if event.type == pygame.KEYUP:
248 | x_change= 0
249 | x += x_change
250 |
251 | game_display.fill(white)
252 |
253 | line(150, 0, 20, display_height, blue)
254 | line(display_width-150, 0, 20, display_height, blue)
255 |
256 | load_image(thing_startx, thing_starty, 'images/car.png')
257 | load_image(80, tree_y_left, 'images/trees.jpg')
258 | load_image(700, tree_y_right, 'images/trees.jpg')
259 | load_image(x,y, 'images/car1.png')
260 |
261 | thing_starty += thing_speed
262 | lineY += line_speed
263 | tree_y_left += tree_speed
264 | tree_y_right += tree_speed
265 |
266 | display(dodged, 5, 25)
267 | display(thing_speed*60 , 5, 50, "Spd: %d px/s")
268 | display(score_game, 5, 5, "Final Score: %d")
269 |
270 | if x > display_width - car_width - 150 or x < 150 :
271 | # 100 way background image
272 | crash(x,y)
273 |
274 | if thing_starty > display_height :
275 | thing_starty = 0 - thing_height # reset y
276 | thing_startx = random.randrange(170, display_width-thing_width-150)
277 | dodged += 1
278 | score_game += 1
279 | thing_speed += 1/20 # accelarate
280 |
281 | if lineY > display_height :
282 | lineY = 0 - lineH # reset y
283 | thing_speed += 1/15
284 |
285 | if tree_y_left > display_height :
286 | tree_y_left = 0 - tree_h # reset y
287 | thing_speed += 1/15
288 |
289 | if tree_y_right > display_height :
290 | tree_y_right = 0 - tree_h # reset y
291 | thing_speed += 1/15
292 |
293 | ### check crash
294 | if y < (thing_starty + thing_height) and y+ car_height >= thing_starty + thing_height:
295 | if x > thing_startx and x < (thing_startx + thing_width) or x + car_width > thing_startx \
296 | and x + car_width < thing_startx + thing_width :
297 | crash(x, y)
298 |
299 | pygame.display.update()
300 | clock.tick(60)
301 |
302 | def main():
303 | game_init()
304 | game_intro()
305 | game_loop()
306 | pygame.quit()
307 | quit()
308 |
309 | if __name__ == '__main__':
310 | main()
311 |
312 |
313 |
--------------------------------------------------------------------------------
/car-game-master/setup.py:
--------------------------------------------------------------------------------
1 | import cx_Freeze
2 |
3 |
4 |
5 | executables = [cx_Freeze.Executable("pygame_car.py")]
6 |
7 | cx_Freeze.setup(
8 |
9 | name="Fast&curious",
10 | options={"build_exe": {"packages":["pygame"],
11 | "include_files":["racecar.png"]}},
12 | executables = executables
13 |
14 | )
15 |
16 |
17 | # shift and right click
18 | # C:/Python34/python setup.py build
19 |
20 | # You can also do something like
21 | # python setup.py bdist_msi
22 |
23 | # If you're on a mac, then you would do
24 | # python setup.py bdist_dmg
--------------------------------------------------------------------------------