├── 23-web-development ├── ChatApp │ ├── runtime.txt │ ├── Procfile │ ├── requirements.txt │ ├── static │ │ ├── index.html │ │ └── main.js │ └── app.py └── .DS_Store ├── .DS_Store ├── 16-randomness.pdf ├── README.md ├── 15-functional-programming.pdf ├── Web-Development-Notes ├── .DS_Store ├── Web Development.html.resources │ ├── image.png │ ├── image_1.png │ ├── B312387C-26EF-4971-8FDD-D44A9B25274F.png │ ├── B781EDEE-0968-4048-8420-8721D90C53E9.png │ ├── C64253C0-038A-49F3-8EB1-38B5ED7A298D.png │ └── EA105921-FC50-4D41-A5BE-4636C1DD83B1.png └── Web Development.html ├── heaps-hashtable ├── Whiteboard[10]-01.png ├── Whiteboard[1]-01.png ├── Whiteboard[2]-01.png ├── Whiteboard[3]-01.png ├── Whiteboard[4]-01.png ├── Whiteboard[5]-01.png ├── Whiteboard[6]-01.png ├── Whiteboard[7]-01.png ├── Whiteboard[8]-01.png └── Whiteboard[9]-01.png ├── trees-and-graphs ├── Whiteboard[1]-01.png ├── Whiteboard[2]-01.png ├── Whiteboard[3]-01.png ├── Whiteboard[4]-01.png ├── Whiteboard[5]-01.png ├── Whiteboard[6]-01.png ├── Whiteboard[7]-01.png ├── Whiteboard[8]-01.png └── Whiteboard[9]-01.png ├── .ipynb_checkpoints ├── 23-Web-Development-checkpoint.ipynb ├── 14-heaps-hash-table-checkpoint.ipynb ├── TreesAndGraphs-checkpoint.ipynb ├── 13-trees-and-graphs-checkpoint.ipynb ├── 12-stacks-queues-linkedlist-checkpoint.ipynb └── StacksAndQueues-checkpoint.ipynb ├── 14-heaps-hash-table.ipynb ├── 03-loops.py ├── 02-conditions.py ├── 04-functions.py ├── 04b-functions-practice.ipynb ├── 22-web-automation.ipynb ├── 13-trees-and-graphs.ipynb ├── 12-stacks-queues-linkedlist.ipynb ├── 09-recursion.ipynb ├── 07-strings.ipynb ├── 08-searching-Swapping-Sorting.ipynb ├── 05-memory.ipynb ├── 06-lists.ipynb ├── 10-tuples-dictionary-sets.ipynb └── 11-classes-and-objects.ipynb /23-web-development/ChatApp/runtime.txt: -------------------------------------------------------------------------------- 1 | python-3.6.6 2 | -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/.DS_Store -------------------------------------------------------------------------------- /23-web-development/ChatApp/Procfile: -------------------------------------------------------------------------------- 1 | web: gunicorn --worker-class eventlet -w 1 module:app -------------------------------------------------------------------------------- /16-randomness.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/16-randomness.pdf -------------------------------------------------------------------------------- /23-web-development/ChatApp/requirements.txt: -------------------------------------------------------------------------------- 1 | Flask 2 | Flask-SocketIO 3 | gunicorn 4 | eventlet -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-lectures' code 2 | 3 | Do not make any changes to this repository! 4 | -------------------------------------------------------------------------------- /15-functional-programming.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/15-functional-programming.pdf -------------------------------------------------------------------------------- /23-web-development/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/23-web-development/.DS_Store -------------------------------------------------------------------------------- /Web-Development-Notes/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/Web-Development-Notes/.DS_Store -------------------------------------------------------------------------------- /heaps-hashtable/Whiteboard[10]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/heaps-hashtable/Whiteboard[10]-01.png -------------------------------------------------------------------------------- /heaps-hashtable/Whiteboard[1]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/heaps-hashtable/Whiteboard[1]-01.png -------------------------------------------------------------------------------- /heaps-hashtable/Whiteboard[2]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/heaps-hashtable/Whiteboard[2]-01.png -------------------------------------------------------------------------------- /heaps-hashtable/Whiteboard[3]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/heaps-hashtable/Whiteboard[3]-01.png -------------------------------------------------------------------------------- /heaps-hashtable/Whiteboard[4]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/heaps-hashtable/Whiteboard[4]-01.png -------------------------------------------------------------------------------- /heaps-hashtable/Whiteboard[5]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/heaps-hashtable/Whiteboard[5]-01.png -------------------------------------------------------------------------------- /heaps-hashtable/Whiteboard[6]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/heaps-hashtable/Whiteboard[6]-01.png -------------------------------------------------------------------------------- /heaps-hashtable/Whiteboard[7]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/heaps-hashtable/Whiteboard[7]-01.png -------------------------------------------------------------------------------- /heaps-hashtable/Whiteboard[8]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/heaps-hashtable/Whiteboard[8]-01.png -------------------------------------------------------------------------------- /heaps-hashtable/Whiteboard[9]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/heaps-hashtable/Whiteboard[9]-01.png -------------------------------------------------------------------------------- /trees-and-graphs/Whiteboard[1]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/trees-and-graphs/Whiteboard[1]-01.png -------------------------------------------------------------------------------- /trees-and-graphs/Whiteboard[2]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/trees-and-graphs/Whiteboard[2]-01.png -------------------------------------------------------------------------------- /trees-and-graphs/Whiteboard[3]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/trees-and-graphs/Whiteboard[3]-01.png -------------------------------------------------------------------------------- /trees-and-graphs/Whiteboard[4]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/trees-and-graphs/Whiteboard[4]-01.png -------------------------------------------------------------------------------- /trees-and-graphs/Whiteboard[5]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/trees-and-graphs/Whiteboard[5]-01.png -------------------------------------------------------------------------------- /trees-and-graphs/Whiteboard[6]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/trees-and-graphs/Whiteboard[6]-01.png -------------------------------------------------------------------------------- /trees-and-graphs/Whiteboard[7]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/trees-and-graphs/Whiteboard[7]-01.png -------------------------------------------------------------------------------- /trees-and-graphs/Whiteboard[8]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/trees-and-graphs/Whiteboard[8]-01.png -------------------------------------------------------------------------------- /trees-and-graphs/Whiteboard[9]-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/trees-and-graphs/Whiteboard[9]-01.png -------------------------------------------------------------------------------- /.ipynb_checkpoints/23-Web-Development-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /Web-Development-Notes/Web Development.html.resources/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/Web-Development-Notes/Web Development.html.resources/image.png -------------------------------------------------------------------------------- /Web-Development-Notes/Web Development.html.resources/image_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/Web-Development-Notes/Web Development.html.resources/image_1.png -------------------------------------------------------------------------------- /Web-Development-Notes/Web Development.html.resources/B312387C-26EF-4971-8FDD-D44A9B25274F.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/Web-Development-Notes/Web Development.html.resources/B312387C-26EF-4971-8FDD-D44A9B25274F.png -------------------------------------------------------------------------------- /Web-Development-Notes/Web Development.html.resources/B781EDEE-0968-4048-8420-8721D90C53E9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/Web-Development-Notes/Web Development.html.resources/B781EDEE-0968-4048-8420-8721D90C53E9.png -------------------------------------------------------------------------------- /Web-Development-Notes/Web Development.html.resources/C64253C0-038A-49F3-8EB1-38B5ED7A298D.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/Web-Development-Notes/Web Development.html.resources/C64253C0-038A-49F3-8EB1-38B5ED7A298D.png -------------------------------------------------------------------------------- /Web-Development-Notes/Web Development.html.resources/EA105921-FC50-4D41-A5BE-4636C1DD83B1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anant15/python-lectures/HEAD/Web-Development-Notes/Web Development.html.resources/EA105921-FC50-4D41-A5BE-4636C1DD83B1.png -------------------------------------------------------------------------------- /23-web-development/ChatApp/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Chat App 4 | 5 | 6 | 7 | 8 | 9 | 10 |

Coding Elements Chatting App

11 |
12 | 13 | 14 |
15 |
16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /14-heaps-hash-table.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Heaps\n", 8 | "\n", 9 | "![](heaps-hashtable/Whiteboard[1]-01.png)\n", 10 | "\n", 11 | "![](heaps-hashtable/Whiteboard[2]-01.png)\n", 12 | "\n", 13 | "![](heaps-hashtable/Whiteboard[3]-01.png)\n", 14 | "\n", 15 | "![](heaps-hashtable/Whiteboard[4]-01.png)\n", 16 | "\n", 17 | "![](heaps-hashtable/Whiteboard[5]-01.png)\n", 18 | "\n", 19 | "### Priority Queues\n", 20 | "\n", 21 | "![](heaps-hashtable/Whiteboard[6]-01.png)\n", 22 | "\n", 23 | "### Hash Table\n", 24 | "\n", 25 | "![](heaps-hashtable/Whiteboard[7]-01.png)\n", 26 | "\n", 27 | "![](heaps-hashtable/Whiteboard[8]-01.png)\n", 28 | "\n", 29 | "### Complexity Analysis\n", 30 | "![](heaps-hashtable/Whiteboard[10]-01.png)" 31 | ] 32 | } 33 | ], 34 | "metadata": { 35 | "kernelspec": { 36 | "display_name": "Python 3", 37 | "language": "python", 38 | "name": "python3" 39 | }, 40 | "language_info": { 41 | "codemirror_mode": { 42 | "name": "ipython", 43 | "version": 3 44 | }, 45 | "file_extension": ".py", 46 | "mimetype": "text/x-python", 47 | "name": "python", 48 | "nbconvert_exporter": "python", 49 | "pygments_lexer": "ipython3", 50 | "version": "3.7.6" 51 | } 52 | }, 53 | "nbformat": 4, 54 | "nbformat_minor": 4 55 | } 56 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/14-heaps-hash-table-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Heaps\n", 8 | "\n", 9 | "![](heaps-hashtable/Whiteboard[1]-01.png)\n", 10 | "\n", 11 | "![](heaps-hashtable/Whiteboard[2]-01.png)\n", 12 | "\n", 13 | "![](heaps-hashtable/Whiteboard[3]-01.png)\n", 14 | "\n", 15 | "![](heaps-hashtable/Whiteboard[4]-01.png)\n", 16 | "\n", 17 | "![](heaps-hashtable/Whiteboard[5]-01.png)\n", 18 | "\n", 19 | "### Priority Queues\n", 20 | "\n", 21 | "![](heaps-hashtable/Whiteboard[6]-01.png)\n", 22 | "\n", 23 | "### Hash Table\n", 24 | "\n", 25 | "![](heaps-hashtable/Whiteboard[7]-01.png)\n", 26 | "\n", 27 | "![](heaps-hashtable/Whiteboard[8]-01.png)\n", 28 | "\n", 29 | "### Complexity Analysis\n", 30 | "![](heaps-hashtable/Whiteboard[10]-01.png)" 31 | ] 32 | } 33 | ], 34 | "metadata": { 35 | "kernelspec": { 36 | "display_name": "Python 3", 37 | "language": "python", 38 | "name": "python3" 39 | }, 40 | "language_info": { 41 | "codemirror_mode": { 42 | "name": "ipython", 43 | "version": 3 44 | }, 45 | "file_extension": ".py", 46 | "mimetype": "text/x-python", 47 | "name": "python", 48 | "nbconvert_exporter": "python", 49 | "pygments_lexer": "ipython3", 50 | "version": "3.7.6" 51 | } 52 | }, 53 | "nbformat": 4, 54 | "nbformat_minor": 4 55 | } 56 | -------------------------------------------------------------------------------- /23-web-development/ChatApp/static/main.js: -------------------------------------------------------------------------------- 1 | console.log("hello") 2 | 3 | var socket = io.connect(location.protocol+'//'+document.domain+':'+location.port); 4 | // socket.on('connect', function(){ 5 | // socket.emit('my event', {data: 'I\'m connected!'}); 6 | // }); 7 | //console.log(socket) 8 | 9 | function send(){ 10 | var msgBox = document.getElementById('msgBox') 11 | // client will send the message to the server 12 | socket.emit('msg', msgBox.value) 13 | //console.log(msgBox.value) 14 | console.log("Server should print the message") 15 | msgBox.value = "" 16 | } 17 | 18 | 19 | // listener for the message received from the server 20 | socket.on("push", function(data){ 21 | console.log(data) 22 | var msgList = document.getElementById('msgList') 23 | msgList.innerHTML += "

" + data + "

" 24 | 25 | }) 26 | 27 | // Now, we want Javascript to create fetching like calls. 28 | function fetchUsers() 29 | { 30 | fetch('/users').then(function(res) 31 | { 32 | res.json().then( 33 | function(data) 34 | { 35 | console.log(data) 36 | } 37 | ) 38 | }) 39 | } 40 | 41 | // var x = 5 42 | 43 | // if (x>2) 44 | // { 45 | // console.log("greater") 46 | // } 47 | // else 48 | // { 49 | // console.log("smaller") 50 | // } 51 | // function area(l, b) 52 | // { 53 | // var a = l * b 54 | // return a 55 | // } 56 | 57 | // a1 = area(4, 5) 58 | 59 | // for (var i=1; i<=5; i+=1) 60 | // { 61 | // console.log(i) 62 | // } 63 | -------------------------------------------------------------------------------- /03-loops.py: -------------------------------------------------------------------------------- 1 | # For loops 2 | # 3 | # # print first 100 natural numbers 4 | # for i in range(1, 101): 5 | # if i < 100: 6 | # print(i, end=",") 7 | # else: 8 | # print(i) 9 | # 10 | # # print square of first 100 natural numbers 11 | # for num in range(1, 101): 12 | # print(num**2, end=",") 13 | # print() 14 | 15 | # # print "hello world" 10 times 16 | # for i in range(10): 17 | # print("Hello World") #This will print 'Hello World' 10x times and i from 0 to 9 as index initialize from 0 18 | # print(i) 19 | # 20 | # # print even numbers in [2, 100] 21 | # for i in range(2, 101): 22 | # if i % 2 == 0: 23 | # print(i, end=", ") 24 | # print() 25 | # 26 | # # multiples of 13 between [100, 200] 27 | # for num in range(100, 201): 28 | # if num % 13 == 0: 29 | # print(num, end=",") #We are using end="," to make the consecutive output in the same line and not next line 30 | # print() 31 | # print("-"*20) 32 | 33 | # check if a number is prime or not 34 | num = 7 35 | result = True 36 | for i in range(2, int(num**2)+1): 37 | if num % i == 0: 38 | result = False 39 | print(result) 40 | 41 | # how to take input from user 42 | output = int(input("Enter number: ")) 43 | print(output*2) 44 | 45 | output = int(input("Enter a number: ")) 46 | i = 0 47 | while i<=output: 48 | print(i) 49 | i = i + 1 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /02-conditions.py: -------------------------------------------------------------------------------- 1 | # Conditional Flow 2 | 3 | # if-elif-else structure 4 | 5 | # if is mandatory, elif is optional [0, 1, ...N], else is optional [0, 1] 6 | age = 65 7 | # if age > 20: 8 | # if age > 60: 9 | # print("Senior") 10 | # print("Adult") 11 | # elif age > 5: 12 | # print("Kid") 13 | # else: 14 | # print("Baby") 15 | 16 | if age > 60: 17 | print("Senior") 18 | 19 | if age > 20: 20 | print("Adult") 21 | elif age > 5: 22 | print("Kid") 23 | else: 24 | print("Baby") 25 | 26 | # Odd, Even problem 27 | num = 6 28 | # print("Odd") 29 | # print("Even") 30 | if num % 2 == 0: 31 | print("Even") 32 | else: 33 | print("Odd") 34 | 35 | # Logical Operators - and, or and not 36 | a, b, c, d = 5, 4, 3, 8 37 | # Find the max - the easy way 38 | if a > b and a > c and a > d: 39 | print(a) 40 | elif b > a and b > c and b > d: 41 | print(b) 42 | elif c > a and c > b and c > d: 43 | print(c) 44 | else: 45 | print(d) 46 | 47 | # better way to calculate maximum 48 | max = b 49 | if a > max: 50 | max = a 51 | if c > max: 52 | max = c 53 | if d > max: 54 | max = d 55 | print(max) 56 | 57 | 58 | a, b, c = 6, 7, 8 59 | if (a+c>b) and (b+c>a) and (a+b>c): 60 | print("Valid") 61 | else: 62 | print("Invalid") 63 | 64 | # to check, whether a triangle is equilateral, isosceles, or scalene 65 | if a == b and b == c and c == a: 66 | print("e") 67 | elif a == b or b == c or c == a: 68 | print("i") 69 | else: 70 | print("s") 71 | 72 | # find the minimum of five numbers a, b, c, d, e 73 | min= b 74 | if a < min: 75 | min = a 76 | if c < min: 77 | min = c 78 | if d < min: 79 | min = d 80 | if e < min: 81 | min = e 82 | print(min) 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/TreesAndGraphs-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "class Node:\n", 10 | " def __init__(self, val):\n", 11 | " self.value = val\n", 12 | " self.left = None\n", 13 | " self.right = None" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": null, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "class BST:\n", 23 | " def __init__(self):\n", 24 | " self.root = None\n", 25 | " \n", 26 | " def insert(self, val):\n", 27 | " new_node = Node(val)\n", 28 | " if self.root == None:\n", 29 | " self.root = new_node\n", 30 | " else:\n", 31 | " _insert(self.root, new_node)\n", 32 | " \n", 33 | " def _insert(self, start_node, new_node):\n", 34 | " # helper method\n", 35 | " if start_node.value == new_node.value:\n", 36 | " return None\n", 37 | " elif start_node > new_node.value:\n", 38 | " if start_node.left == None:\n", 39 | " start_node.left = new_node\n", 40 | " else:\n", 41 | " _insert(start_node.left, new_node)\n", 42 | " else:\n", 43 | " if start_node.right == None:\n", 44 | " start_node.right = new_node\n", 45 | " else:\n", 46 | " _insert(start_node.right, new_node)" 47 | ] 48 | } 49 | ], 50 | "metadata": { 51 | "kernelspec": { 52 | "display_name": "Python 3", 53 | "language": "python", 54 | "name": "python3" 55 | }, 56 | "language_info": { 57 | "codemirror_mode": { 58 | "name": "ipython", 59 | "version": 3 60 | }, 61 | "file_extension": ".py", 62 | "mimetype": "text/x-python", 63 | "name": "python", 64 | "nbconvert_exporter": "python", 65 | "pygments_lexer": "ipython3", 66 | "version": "3.7.6" 67 | } 68 | }, 69 | "nbformat": 4, 70 | "nbformat_minor": 4 71 | } 72 | -------------------------------------------------------------------------------- /04-functions.py: -------------------------------------------------------------------------------- 1 | # Functions 2 | 3 | # for i in range(5): 4 | # print(i) 5 | # for power in range(1, 4): 6 | # print(i**power, end = " ") 7 | # print() 8 | 9 | # Functions 10 | 11 | # def f(x): 12 | # y = x**2 + x + 2 13 | # return y 14 | # 15 | # print(f(4)) 16 | # print(f(5)) 17 | # print(f(10)) 18 | # 19 | # 20 | # def area(l, b): # parameters 21 | # a = l * b 22 | # return a 23 | # 24 | # var = area(5, 2) # arguments 25 | # var2 = area(3, 4) 26 | # print(var, var2) 27 | # 28 | # def hello(name): # parameter 29 | # print("Hello " + name) 30 | # 31 | # print(hello("Anant")) 32 | # hello("Rishi") 33 | # hello("Priya") 34 | 35 | 36 | def is_prime(num): 37 | for i in range(2, int(num**0.5+1)): 38 | if num % i == 0: 39 | return False 40 | return True 41 | 42 | # Python function can have multiple return statements 43 | # print(is_prime(2)) 44 | 45 | # def dummy(x): 46 | # print(x) 47 | # return None 48 | # print(x**2) 49 | # 50 | # dummy(3) 51 | 52 | # def print_primes(lower, upper): 53 | # for i in range(lower, upper+1): 54 | # if is_prime(i): 55 | # print(i) 56 | # 57 | # print_primes(2, 50) 58 | 59 | 60 | 61 | def minimum(a, b): 62 | if a > b: 63 | return b 64 | else: 65 | return a 66 | 67 | def hcf(a, b): 68 | smaller = minimum(a, b) 69 | result = 1 70 | for i in range(1, smaller+1): 71 | if a % i == 0 and b % i == 0: 72 | result = i 73 | return result 74 | 75 | print(hcf(12, 11)) 76 | 77 | 78 | # Euclid's Algorithm 79 | def hcf(a, b): 80 | while a != 0 and b != 0: 81 | if a > b: 82 | a = a - b 83 | else: 84 | b = b - a 85 | return a + b 86 | 87 | # Optimised Eulcid's algorithm 88 | def hcf(a, b): 89 | while a != 0 and b != 0: 90 | if a > b: 91 | a = a % b 92 | else: 93 | b = b % a 94 | return a + b 95 | 96 | # 39924 - count the number of digits 97 | def count(num, digit): 98 | count = 0 99 | while num > 0: 100 | d = num % 10 101 | num = num // 10 102 | if d == digit: 103 | count = count + 1 104 | return count 105 | 106 | # log 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | -------------------------------------------------------------------------------- /04b-functions-practice.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 12, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def custom_sqrt(num):\n", 10 | " left = 0\n", 11 | " right = num\n", 12 | " mid = (left + right) / 2\n", 13 | " sq = mid * mid\n", 14 | " while abs(sq - num) > 1e-10: \n", 15 | " if sq < num:\n", 16 | " left = mid\n", 17 | " else:\n", 18 | " right = mid\n", 19 | " mid = (left + right)/2\n", 20 | " sq = mid * mid\n", 21 | " return mid" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 13, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/plain": [ 32 | "10.000000000002274" 33 | ] 34 | }, 35 | "execution_count": 13, 36 | "metadata": {}, 37 | "output_type": "execute_result" 38 | } 39 | ], 40 | "source": [ 41 | "custom_sqrt(100)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 17, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "data": { 51 | "text/plain": [ 52 | "4" 53 | ] 54 | }, 55 | "execution_count": 17, 56 | "metadata": {}, 57 | "output_type": "execute_result" 58 | } 59 | ], 60 | "source": [ 61 | "def custom_log(x, b):\n", 62 | " count = 0\n", 63 | " while x > 1:\n", 64 | " x = x / b\n", 65 | " count += 1\n", 66 | " return count\n", 67 | "\n", 68 | "custom_log(8, 2)\n", 69 | "custom_log(2, 2)\n", 70 | "custom_log(27, 3)\n", 71 | "custom_log(28, 3)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 27, 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "3.0331032563043436" 83 | ] 84 | }, 85 | "execution_count": 27, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "def newton_log(num, b):\n", 92 | " left = 0\n", 93 | " right = num\n", 94 | " mid = (left + right) / 2\n", 95 | " temp = b ** mid\n", 96 | " while abs(temp - num) > 1e-12: \n", 97 | " if temp < num:\n", 98 | " left = mid\n", 99 | " else:\n", 100 | " right = mid\n", 101 | " mid = (left + right)/2\n", 102 | " temp = b ** mid\n", 103 | " return mid\n", 104 | "\n", 105 | "newton_log(8, 2)\n", 106 | "newton_log(2, 2)\n", 107 | "newton_log(27, 3)\n", 108 | "newton_log(28, 3)" 109 | ] 110 | }, 111 | { 112 | "cell_type": "markdown", 113 | "metadata": {}, 114 | "source": [ 115 | "### Bit, Binary, ASCII" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [] 124 | } 125 | ], 126 | "metadata": { 127 | "kernelspec": { 128 | "display_name": "Python 3", 129 | "language": "python", 130 | "name": "python3" 131 | }, 132 | "language_info": { 133 | "codemirror_mode": { 134 | "name": "ipython", 135 | "version": 3 136 | }, 137 | "file_extension": ".py", 138 | "mimetype": "text/x-python", 139 | "name": "python", 140 | "nbconvert_exporter": "python", 141 | "pygments_lexer": "ipython3", 142 | "version": "3.7.2" 143 | } 144 | }, 145 | "nbformat": 4, 146 | "nbformat_minor": 2 147 | } 148 | -------------------------------------------------------------------------------- /23-web-development/ChatApp/app.py: -------------------------------------------------------------------------------- 1 | # # flask basics 2 | # from flask import Flask, jsonify, request 3 | # app = Flask(__name__, static_url_path="/static") 4 | # # If this file is getting directly run from the terminal, 5 | # # by default __name__ will be set to "__main__" . Otherwise, 6 | # # if we are importing this file in some other Python script, 7 | # # then it will different. We will get to see it's importance later. 8 | 9 | # # Function decorator 10 | # # @app.route("/") # "/" is one of the end-points 11 | # # def hello(): 12 | # # # function name can be anything 13 | # # return "Hello" 14 | 15 | # # # Example of creating our own API 16 | # users = [{'id':2, 'name':'Anne', 'age':20}, {'id':1, 'name':'Cathy', 'age':21}] 17 | 18 | # @app.route("/users/") 19 | # def get_users(): 20 | # return jsonify(users) # jsonify is used to convert the dict to string 21 | # # you can only send a string 22 | 23 | # # # Creating a dynamic route, search user by ID 24 | # @app.route("/users/id/") 25 | # def get_user(id): 26 | # result = [u for u in users if u['id'] == int(id)] # check the datatype 27 | # result = list(filter(lambda u: u['id'] == int(id), users)) # check the data type here, remember input() 28 | # print(result) # the output of this will be seen in the terminal 29 | # return jsonify(result) 30 | 31 | # # HTML end-points 32 | # @app.route("/") 33 | # def index(): 34 | # return app.send_static_file("index.html") 35 | # # if we change the content of the html file, we don't need to re-start the server - app.py 36 | 37 | # # # Another end-point, search by some other variable 38 | # @app.route('/users/name/') 39 | # def getUser(name): 40 | # result = [u for u in users if u['name']==name] 41 | # #result = list(filter(lambda u: u['id']==id, users)) # check the data type here, remember input() 42 | # return jsonify(result) 43 | 44 | # # Adding some Javascipt to the code 45 | # # Now, we want Javascript to create fetching like calls AJAX 46 | # # Check the index.html and main.js files 47 | # if __name__ == "__main__": 48 | # app.run() # runs the server 49 | 50 | 51 | # flask socketio 52 | from flask import Flask, jsonify, request 53 | from flask_socketio import SocketIO 54 | 55 | app = Flask(__name__) 56 | app.config['SECRET_KEY'] = 'secret!' # completely optional 57 | socketio = SocketIO(app) 58 | 59 | # Function decorator 60 | # @app.route("/") # "/" is one of the end-points 61 | # def hello(): 62 | # # function name can be anything 63 | # return "Hello" 64 | 65 | # # Example of creating our own API 66 | users = [{'id':2, 'name':'Anne', 'age':20}, {'id':1, 'name':'Cathy', 'age':21}] 67 | 68 | @app.route("/users/") 69 | def get_users(): 70 | return jsonify(users) # jsonify is used to convert the dict to string 71 | # you can only send a string 72 | 73 | # # Creating a dynamic route, search user by ID 74 | @app.route("/users/id/") 75 | def get_user(id): 76 | result = [u for u in users if u['id'] == int(id)] # check the datatype 77 | result = list(filter(lambda u: u['id'] == int(id), users)) # check the data type here, remember input() 78 | print(result) # the output of this will be seen in the terminal 79 | return jsonify(result) 80 | 81 | # this is similar to onconnect or onclick, all of these are called listener 82 | @socketio.on("msg") 83 | def handle_msg(data): 84 | print(data) 85 | socketio.emit("push", data, broadcast=True, include_self=False) 86 | 87 | # HTML end-points 88 | @app.route("/") 89 | def index(): 90 | return app.send_static_file("index.html") 91 | # if we change the content of the html file, we don't need to re-start the server - app.py 92 | 93 | # # Another end-point, search by some other variable 94 | @app.route('/users/name/') 95 | def getUser(name): 96 | result = [u for u in users if u['name']==name] 97 | #result = list(filter(lambda u: u['id']==id, users)) # check the data type here, remember input() 98 | return jsonify(result) 99 | 100 | # Adding some Javascipt to the code 101 | # Now, we want Javascript to create fetching like calls AJAX 102 | # Check the index.html and main.js files 103 | if __name__ == "__main__": 104 | #app.run() # runs the server 105 | socketio.run(app) 106 | 107 | -------------------------------------------------------------------------------- /22-web-automation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Requirement already satisfied: selenium in /Users/anant/anaconda3/lib/python3.7/site-packages (3.141.0)\r\n", 13 | "Requirement already satisfied: urllib3 in /Users/anant/anaconda3/lib/python3.7/site-packages (from selenium) (1.25.8)\r\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "!pip install selenium" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "from selenium import webdriver" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 3, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "" 39 | ] 40 | }, 41 | "execution_count": 3, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "webdriver.Chrome(\"/Users/anant/Downloads/chromedriver\")\n", 48 | "\n", 49 | "# For Windows users\n", 50 | "#webdriver.Chrome(r\"C:\\..\\chromdriver.exe\")" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 8, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "'Users/anant/Downloads/chromedriver'" 62 | ] 63 | }, 64 | "execution_count": 8, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "import os\n", 71 | "os.path.join(\"Users\", \"anant\", \"Downloads\", \"chromedriver\")" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 19, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "import time\n", 81 | "from selenium import webdriver\n", 82 | "\n", 83 | "browser = webdriver.Chrome(\"/Users/anant/Downloads/chromedriver\")\n", 84 | "browser.get(\"https://www.google.com\")\n", 85 | "search_box = browser.find_element_by_name(\"q\")\n", 86 | "search_box.send_keys(\"Kenya\")\n", 87 | "\n", 88 | "time.sleep(1)\n", 89 | "\n", 90 | "search_button = browser.find_element_by_name(\"btnK\")\n", 91 | "search_button.click()\n", 92 | "\n", 93 | "time.sleep(1)\n", 94 | "\n", 95 | "wikipedia_link = browser.find_element_by_partial_link_text(\"wikipedia\")\n", 96 | "wikipedia_link.click()" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 23, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "Didn't work for -3\n", 109 | "Didn't work for -2\n", 110 | "Didn't work for -1\n", 111 | "Didn't work for 1\n", 112 | "Didn't work for 2\n", 113 | "Didn't work for 3\n", 114 | "Didn't work for 4\n", 115 | "Didn't work for 5\n", 116 | "Didn't work for 6\n", 117 | "Didn't work for 7\n", 118 | "Didn't work for 8\n", 119 | "Didn't work for 9\n", 120 | "Didn't work for 10\n", 121 | "Didn't work for 11\n", 122 | "Didn't work for 12\n", 123 | "Didn't work for 13\n" 124 | ] 125 | } 126 | ], 127 | "source": [ 128 | "import time\n", 129 | "from selenium import webdriver\n", 130 | "\n", 131 | "browser = webdriver.Chrome(\"/Users/anant/Downloads/chromedriver\")\n", 132 | "browser.get(\"https://www.facebook.com\")\n", 133 | "email_box = browser.find_element_by_name(\"email\")\n", 134 | "email_box.send_keys(\"anantm@facebook.com\")\n", 135 | "password_box = browser.find_element_by_name(\"pass\")\n", 136 | "password_box.send_keys(\"1234\")\n", 137 | "\n", 138 | "# error handling mechanism\n", 139 | "for i in range(-3, 14, 1):\n", 140 | " try:\n", 141 | " login_button = browser.find_element_by_id(f\"u_{i}_b\")\n", 142 | " login_button.click()\n", 143 | " except:\n", 144 | " print(\"Didn't work for\", i)" 145 | ] 146 | } 147 | ], 148 | "metadata": { 149 | "kernelspec": { 150 | "display_name": "Python 3", 151 | "language": "python", 152 | "name": "python3" 153 | }, 154 | "language_info": { 155 | "codemirror_mode": { 156 | "name": "ipython", 157 | "version": 3 158 | }, 159 | "file_extension": ".py", 160 | "mimetype": "text/x-python", 161 | "name": "python", 162 | "nbconvert_exporter": "python", 163 | "pygments_lexer": "ipython3", 164 | "version": "3.7.6" 165 | } 166 | }, 167 | "nbformat": 4, 168 | "nbformat_minor": 4 169 | } 170 | -------------------------------------------------------------------------------- /13-trees-and-graphs.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Graphs and Trees\n", 8 | "\n", 9 | "![](trees-and-graphs/Whiteboard[1]-01.png)\n", 10 | "\n", 11 | "### Graphs, Trees, Binary Trees and Binary Search Trees\n", 12 | "\n", 13 | "![](trees-and-graphs/Whiteboard[3]-01.png)\n", 14 | "\n", 15 | "### Linked List is a Binary Tree\n", 16 | "\n", 17 | "![](trees-and-graphs/Whiteboard[4]-01.png)\n", 18 | "\n", 19 | "### Binary Search Tree\n", 20 | "\n", 21 | "![](trees-and-graphs/Whiteboard[5]-01.png) \n", 22 | "\n", 23 | "### Binary Search Tree Complexity Analysis\n", 24 | "\n", 25 | "![](trees-and-graphs/Whiteboard[6]-01.png) \n", 26 | "\n", 27 | "### Printing/Searching in a tree, and Breadth First Search (BFS)\n", 28 | "\n", 29 | "![](trees-and-graphs/Whiteboard[8]-01.png) \n", 30 | "\n", 31 | "### Printing/Searching in a tree, and Depth First Search (DFS)\n", 32 | "\n", 33 | "![](trees-and-graphs/Whiteboard[9]-01.png) " 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 1, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "class Node:\n", 43 | " def __init__(self, val):\n", 44 | " self.value = val\n", 45 | " self.left = None\n", 46 | " self.right = None\n", 47 | " \n", 48 | " def __str__(self):\n", 49 | " return str(self.value)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 19, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "class BST:\n", 59 | " def __init__(self):\n", 60 | " self.root = None\n", 61 | " \n", 62 | " def _insert(self, start_node, new_node):\n", 63 | " # helper method\n", 64 | " if start_node.value == new_node.value:\n", 65 | " return None\n", 66 | " elif start_node.value > new_node.value:\n", 67 | " if start_node.left == None:\n", 68 | " start_node.left = new_node\n", 69 | " else:\n", 70 | " self._insert(start_node.left, new_node)\n", 71 | " else:\n", 72 | " if start_node.right == None:\n", 73 | " start_node.right = new_node\n", 74 | " else:\n", 75 | " self._insert(start_node.right, new_node)\n", 76 | " \n", 77 | " def insert(self, val):\n", 78 | " new_node = Node(val)\n", 79 | " if self.root == None:\n", 80 | " self.root = new_node\n", 81 | " else:\n", 82 | " self._insert(self.root, new_node)\n", 83 | " \n", 84 | " def printBF(self):\n", 85 | " q = []\n", 86 | " if self.root:\n", 87 | " q.append(self.root)\n", 88 | " # run until the queue is not empty\n", 89 | " while len(q) > 0:\n", 90 | " # keep adding the L and R children if they exist for the first node in the queue\n", 91 | " if q[0].left:\n", 92 | " q.append(q[0].left)\n", 93 | " if q[0].right:\n", 94 | " q.append(q[0].right)\n", 95 | " temp = q.pop(0)\n", 96 | " print(temp, end=\" \")\n", 97 | " print()\n", 98 | " \n", 99 | " def _printInDF(self, start):\n", 100 | " if start == None:\n", 101 | " return\n", 102 | " self._printInDF(start.left)\n", 103 | " print(start, end=\" \")\n", 104 | " self._printInDF(start.right)\n", 105 | " \n", 106 | " def _printPreDF(self, start):\n", 107 | " if start == None:\n", 108 | " return\n", 109 | " print(start, end=\" \")\n", 110 | " self._printPreDF(start.left)\n", 111 | " self._printPreDF(start.right)\n", 112 | " \n", 113 | " def _printPostDF(self, start):\n", 114 | " if start == None:\n", 115 | " return\n", 116 | " self._printPostDF(start.left)\n", 117 | " self._printPostDF(start.right)\n", 118 | " print(start, end=\" \")\n", 119 | " \n", 120 | " def printDF(self, order=\"in\"):\n", 121 | " if order == \"in\":\n", 122 | " self._printInDF(self.root)\n", 123 | " elif order == \"pre\":\n", 124 | " self._printPreDF(self.root)\n", 125 | " else:\n", 126 | " self._printPostDF(self.root)\n", 127 | " print()\n", 128 | " \n", 129 | " \n", 130 | " def search(self, target):\n", 131 | " if self.root:\n", 132 | " return self._search(self.root, target)\n", 133 | " \n", 134 | " def _search(self, start, target):\n", 135 | " if start.value == target.value:\n", 136 | " return start\n", 137 | " elif start.value > target.value:\n", 138 | " # left sub-tree\n", 139 | " if start.left:\n", 140 | " return _search(start.left, target)\n", 141 | " return None\n", 142 | " else:\n", 143 | " # right sub-tree\n", 144 | " if start.right:\n", 145 | " return _search(start.right, target)\n", 146 | " return None" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 21, 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "name": "stdout", 156 | "output_type": "stream", 157 | "text": [ 158 | "5 3 7 1 4 6 9 \n", 159 | "5 3 1 4 7 6 9 \n", 160 | "1 4 3 6 9 7 5 \n", 161 | "1 3 4 5 6 7 9 \n" 162 | ] 163 | } 164 | ], 165 | "source": [ 166 | "t1 = BST()\n", 167 | "t1.insert(5)\n", 168 | "t1.insert(3)\n", 169 | "t1.insert(4)\n", 170 | "t1.insert(1)\n", 171 | "t1.insert(7)\n", 172 | "t1.insert(9)\n", 173 | "t1.insert(6)\n", 174 | "t1.printBF()\n", 175 | "t1.printDF(order=\"pre\")\n", 176 | "t1.printDF(order=\"post\")\n", 177 | "t1.printDF(order=\"in\")" 178 | ] 179 | } 180 | ], 181 | "metadata": { 182 | "kernelspec": { 183 | "display_name": "Python 3", 184 | "language": "python", 185 | "name": "python3" 186 | }, 187 | "language_info": { 188 | "codemirror_mode": { 189 | "name": "ipython", 190 | "version": 3 191 | }, 192 | "file_extension": ".py", 193 | "mimetype": "text/x-python", 194 | "name": "python", 195 | "nbconvert_exporter": "python", 196 | "pygments_lexer": "ipython3", 197 | "version": "3.7.6" 198 | } 199 | }, 200 | "nbformat": 4, 201 | "nbformat_minor": 4 202 | } 203 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/13-trees-and-graphs-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Graphs and Trees\n", 8 | "\n", 9 | "![](trees-and-graphs/Whiteboard[1]-01.png)\n", 10 | "\n", 11 | "### Graphs, Trees, Binary Trees and Binary Search Trees\n", 12 | "\n", 13 | "![](trees-and-graphs/Whiteboard[3]-01.png)\n", 14 | "\n", 15 | "### Linked List is a Binary Tree\n", 16 | "\n", 17 | "![](trees-and-graphs/Whiteboard[4]-01.png)\n", 18 | "\n", 19 | "### Binary Search Tree\n", 20 | "\n", 21 | "![](trees-and-graphs/Whiteboard[5]-01.png) \n", 22 | "\n", 23 | "### Binary Search Tree Complexity Analysis\n", 24 | "\n", 25 | "![](trees-and-graphs/Whiteboard[6]-01.png) \n", 26 | "\n", 27 | "### Printing/Searching in a tree, and Breadth First Search (BFS)\n", 28 | "\n", 29 | "![](trees-and-graphs/Whiteboard[8]-01.png) \n", 30 | "\n", 31 | "### Printing/Searching in a tree, and Depth First Search (DFS)\n", 32 | "\n", 33 | "![](trees-and-graphs/Whiteboard[9]-01.png) " 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 1, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "class Node:\n", 43 | " def __init__(self, val):\n", 44 | " self.value = val\n", 45 | " self.left = None\n", 46 | " self.right = None\n", 47 | " \n", 48 | " def __str__(self):\n", 49 | " return str(self.value)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 19, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "class BST:\n", 59 | " def __init__(self):\n", 60 | " self.root = None\n", 61 | " \n", 62 | " def _insert(self, start_node, new_node):\n", 63 | " # helper method\n", 64 | " if start_node.value == new_node.value:\n", 65 | " return None\n", 66 | " elif start_node.value > new_node.value:\n", 67 | " if start_node.left == None:\n", 68 | " start_node.left = new_node\n", 69 | " else:\n", 70 | " self._insert(start_node.left, new_node)\n", 71 | " else:\n", 72 | " if start_node.right == None:\n", 73 | " start_node.right = new_node\n", 74 | " else:\n", 75 | " self._insert(start_node.right, new_node)\n", 76 | " \n", 77 | " def insert(self, val):\n", 78 | " new_node = Node(val)\n", 79 | " if self.root == None:\n", 80 | " self.root = new_node\n", 81 | " else:\n", 82 | " self._insert(self.root, new_node)\n", 83 | " \n", 84 | " def printBF(self):\n", 85 | " q = []\n", 86 | " if self.root:\n", 87 | " q.append(self.root)\n", 88 | " # run until the queue is not empty\n", 89 | " while len(q) > 0:\n", 90 | " # keep adding the L and R children if they exist for the first node in the queue\n", 91 | " if q[0].left:\n", 92 | " q.append(q[0].left)\n", 93 | " if q[0].right:\n", 94 | " q.append(q[0].right)\n", 95 | " temp = q.pop(0)\n", 96 | " print(temp, end=\" \")\n", 97 | " print()\n", 98 | " \n", 99 | " def _printInDF(self, start):\n", 100 | " if start == None:\n", 101 | " return\n", 102 | " self._printInDF(start.left)\n", 103 | " print(start, end=\" \")\n", 104 | " self._printInDF(start.right)\n", 105 | " \n", 106 | " def _printPreDF(self, start):\n", 107 | " if start == None:\n", 108 | " return\n", 109 | " print(start, end=\" \")\n", 110 | " self._printPreDF(start.left)\n", 111 | " self._printPreDF(start.right)\n", 112 | " \n", 113 | " def _printPostDF(self, start):\n", 114 | " if start == None:\n", 115 | " return\n", 116 | " self._printPostDF(start.left)\n", 117 | " self._printPostDF(start.right)\n", 118 | " print(start, end=\" \")\n", 119 | " \n", 120 | " def printDF(self, order=\"in\"):\n", 121 | " if order == \"in\":\n", 122 | " self._printInDF(self.root)\n", 123 | " elif order == \"pre\":\n", 124 | " self._printPreDF(self.root)\n", 125 | " else:\n", 126 | " self._printPostDF(self.root)\n", 127 | " print()\n", 128 | " \n", 129 | " \n", 130 | " def search(self, target):\n", 131 | " if self.root:\n", 132 | " return self._search(self.root, target)\n", 133 | " \n", 134 | " def _search(self, start, target):\n", 135 | " if start.value == target.value:\n", 136 | " return start\n", 137 | " elif start.value > target.value:\n", 138 | " # left sub-tree\n", 139 | " if start.left:\n", 140 | " return _search(start.left, target)\n", 141 | " return None\n", 142 | " else:\n", 143 | " # right sub-tree\n", 144 | " if start.right:\n", 145 | " return _search(start.right, target)\n", 146 | " return None" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 21, 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "name": "stdout", 156 | "output_type": "stream", 157 | "text": [ 158 | "5 3 7 1 4 6 9 \n", 159 | "5 3 1 4 7 6 9 \n", 160 | "1 4 3 6 9 7 5 \n", 161 | "1 3 4 5 6 7 9 \n" 162 | ] 163 | } 164 | ], 165 | "source": [ 166 | "t1 = BST()\n", 167 | "t1.insert(5)\n", 168 | "t1.insert(3)\n", 169 | "t1.insert(4)\n", 170 | "t1.insert(1)\n", 171 | "t1.insert(7)\n", 172 | "t1.insert(9)\n", 173 | "t1.insert(6)\n", 174 | "t1.printBF()\n", 175 | "t1.printDF(order=\"pre\")\n", 176 | "t1.printDF(order=\"post\")\n", 177 | "t1.printDF(order=\"in\")" 178 | ] 179 | } 180 | ], 181 | "metadata": { 182 | "kernelspec": { 183 | "display_name": "Python 3", 184 | "language": "python", 185 | "name": "python3" 186 | }, 187 | "language_info": { 188 | "codemirror_mode": { 189 | "name": "ipython", 190 | "version": 3 191 | }, 192 | "file_extension": ".py", 193 | "mimetype": "text/x-python", 194 | "name": "python", 195 | "nbconvert_exporter": "python", 196 | "pygments_lexer": "ipython3", 197 | "version": "3.7.6" 198 | } 199 | }, 200 | "nbformat": 4, 201 | "nbformat_minor": 4 202 | } 203 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/12-stacks-queues-linkedlist-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Stack is a LIFO structure\n", 10 | "\n", 11 | "class Stack:\n", 12 | " def __init__(self):\n", 13 | " self.data = []\n", 14 | " \n", 15 | " def push(self, value):\n", 16 | " self.data.append(value)\n", 17 | " \n", 18 | " def pop(self):\n", 19 | " return self.data.pop(-1)\n", 20 | " \n", 21 | " def top(self):\n", 22 | " return self.data[-1]\n", 23 | " \n", 24 | " def size(self):\n", 25 | " return len(self.data)\n", 26 | " \n", 27 | " def is_empty(self):\n", 28 | " return len(self.data) == 0\n", 29 | " \n", 30 | " def __str__(self):\n", 31 | " return str(self.data)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 12, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "True\n", 44 | "0\n", 45 | "False\n", 46 | "1\n", 47 | "[1]\n", 48 | "[1, 2, 3]\n", 49 | "3\n", 50 | "[1, 2]\n", 51 | "2\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "s1 = Stack()\n", 57 | "print(s1.is_empty())\n", 58 | "print(s1.size())\n", 59 | "s1.push(1)\n", 60 | "print(s1.is_empty())\n", 61 | "print(s1.size())\n", 62 | "print(s1)\n", 63 | "s1.push(2)\n", 64 | "s1.push(3)\n", 65 | "print(s1)\n", 66 | "print(s1.pop())\n", 67 | "print(s1)\n", 68 | "print(s1.top())" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 13, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "# Queues\n", 78 | "\n", 79 | "class Queue:\n", 80 | " def __init__(self):\n", 81 | " self.data = []\n", 82 | " \n", 83 | " # this is similar to push in stacks, adding element to the queue\n", 84 | " def enqueue(self, value):\n", 85 | " self.data.append(value)\n", 86 | " \n", 87 | " def dequeue(self):\n", 88 | " return self.data.pop(0)\n", 89 | " \n", 90 | " def is_empty(self):\n", 91 | " return len(self.data) == 0\n", 92 | " \n", 93 | " def size(self):\n", 94 | " return len(self.data)\n", 95 | " \n", 96 | " def front(self):\n", 97 | " return self.data[0]\n", 98 | " \n", 99 | " def end(self):\n", 100 | " return self.data[-1]\n", 101 | " \n", 102 | " def __str__(self):\n", 103 | " return str(self.data)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 18, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "[1, 2, 3, ]\n", 116 | "3\n", 117 | "[2, 3, ]\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "# Linked List\n", 123 | "\n", 124 | "class Node:\n", 125 | " \n", 126 | " def __init__(self, value):\n", 127 | " self.value = value\n", 128 | " self.next = None\n", 129 | "\n", 130 | "class CustomQueue:\n", 131 | " \n", 132 | " def __init__(self):\n", 133 | " self.start = None\n", 134 | " self.end = None\n", 135 | " self.size = 0\n", 136 | " \n", 137 | " def enqueue(self, value):\n", 138 | " node = Node(value)\n", 139 | " if self.start == None:\n", 140 | " self.start = node\n", 141 | " # self.end = node\n", 142 | " else:\n", 143 | " self.end.next = node\n", 144 | " # self.end = node\n", 145 | " self.end = node\n", 146 | " self.size += 1\n", 147 | " \n", 148 | " def dequeue(self):\n", 149 | " if self.start == None:\n", 150 | " return None\n", 151 | " elif self.start == self.end:\n", 152 | " removed_value = self.start\n", 153 | " self.start = None\n", 154 | " self.end = None\n", 155 | " self.size -= 1\n", 156 | " return removed_value\n", 157 | " else:\n", 158 | " returned_value = self.start\n", 159 | " self.start = self.start.next\n", 160 | " returned_value.next = None\n", 161 | " self.size -= 1\n", 162 | " return returned_value\n", 163 | " \n", 164 | " def is_empty(self):\n", 165 | " return self.size == 0\n", 166 | " \n", 167 | " def qsize(self):\n", 168 | " return self.size\n", 169 | " \n", 170 | " def front(self):\n", 171 | " if self.start == None:\n", 172 | " return None\n", 173 | " else:\n", 174 | " return self.start.value\n", 175 | " \n", 176 | " def end(self):\n", 177 | " if self.end == None:\n", 178 | " return None\n", 179 | " else:\n", 180 | " return self.end.value\n", 181 | " \n", 182 | " def __str__(self):\n", 183 | " result = \"[\"\n", 184 | " i = self.start\n", 185 | " while i is not None:\n", 186 | " result += str(i.value) \n", 187 | " result += \", \"\n", 188 | " i = i.next\n", 189 | " result += \"]\"\n", 190 | " return result\n", 191 | " \n", 192 | "\n", 193 | "q1 = CustomQueue()\n", 194 | "q1.enqueue(1)\n", 195 | "q1.enqueue(2)\n", 196 | "q1.enqueue(3)\n", 197 | "print(q1)\n", 198 | "print(q1.qsize())\n", 199 | "q1.dequeue()\n", 200 | "print(q1)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": null, 206 | "metadata": {}, 207 | "outputs": [], 208 | "source": [ 209 | "### Making Queue using stack\n", 210 | "\n", 211 | "class QueueUsingStacks:\n", 212 | " def __init__(self):\n", 213 | " self.s1 = Stack()\n", 214 | " self.s2 = Stack()\n", 215 | " \n", 216 | " def enqueue(self, val):\n", 217 | " self.s1.push(val)\n", 218 | " \n", 219 | " def dequeue(self):\n", 220 | " if self.s2.is_empty():\n", 221 | " if self.s1.is_empty():\n", 222 | " return None\n", 223 | " else:\n", 224 | " while not self.s1.is_empty():\n", 225 | " v = self.s1.pop()\n", 226 | " self.s2.push(v)\n", 227 | " return self.s2.pop()" 228 | ] 229 | } 230 | ], 231 | "metadata": { 232 | "kernelspec": { 233 | "display_name": "Python 3", 234 | "language": "python", 235 | "name": "python3" 236 | }, 237 | "language_info": { 238 | "codemirror_mode": { 239 | "name": "ipython", 240 | "version": 3 241 | }, 242 | "file_extension": ".py", 243 | "mimetype": "text/x-python", 244 | "name": "python", 245 | "nbconvert_exporter": "python", 246 | "pygments_lexer": "ipython3", 247 | "version": "3.7.6" 248 | } 249 | }, 250 | "nbformat": 4, 251 | "nbformat_minor": 4 252 | } 253 | -------------------------------------------------------------------------------- /12-stacks-queues-linkedlist.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Stack is a LIFO structure\n", 10 | "\n", 11 | "class Stack:\n", 12 | " def __init__(self):\n", 13 | " self.data = []\n", 14 | " \n", 15 | " def push(self, value):\n", 16 | " self.data.append(value)\n", 17 | " \n", 18 | " def pop(self):\n", 19 | " return self.data.pop(-1)\n", 20 | " \n", 21 | " def top(self):\n", 22 | " return self.data[-1]\n", 23 | " \n", 24 | " def size(self):\n", 25 | " return len(self.data)\n", 26 | " \n", 27 | " def is_empty(self):\n", 28 | " return len(self.data) == 0\n", 29 | " \n", 30 | " def __str__(self):\n", 31 | " return str(self.data)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 12, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "True\n", 44 | "0\n", 45 | "False\n", 46 | "1\n", 47 | "[1]\n", 48 | "[1, 2, 3]\n", 49 | "3\n", 50 | "[1, 2]\n", 51 | "2\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "s1 = Stack()\n", 57 | "print(s1.is_empty())\n", 58 | "print(s1.size())\n", 59 | "s1.push(1)\n", 60 | "print(s1.is_empty())\n", 61 | "print(s1.size())\n", 62 | "print(s1)\n", 63 | "s1.push(2)\n", 64 | "s1.push(3)\n", 65 | "print(s1)\n", 66 | "print(s1.pop())\n", 67 | "print(s1)\n", 68 | "print(s1.top())" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 13, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "# Queues\n", 78 | "\n", 79 | "class Queue:\n", 80 | " def __init__(self):\n", 81 | " self.data = []\n", 82 | " \n", 83 | " # this is similar to push in stacks, adding element to the queue\n", 84 | " def enqueue(self, value):\n", 85 | " self.data.append(value)\n", 86 | " \n", 87 | " def dequeue(self):\n", 88 | " return self.data.pop(0)\n", 89 | " \n", 90 | " def is_empty(self):\n", 91 | " return len(self.data) == 0\n", 92 | " \n", 93 | " def size(self):\n", 94 | " return len(self.data)\n", 95 | " \n", 96 | " def front(self):\n", 97 | " return self.data[0]\n", 98 | " \n", 99 | " def end(self):\n", 100 | " return self.data[-1]\n", 101 | " \n", 102 | " def __str__(self):\n", 103 | " return str(self.data)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 18, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "[1, 2, 3, ]\n", 116 | "3\n", 117 | "[2, 3, ]\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "# Linked List\n", 123 | "\n", 124 | "class Node:\n", 125 | " \n", 126 | " def __init__(self, value):\n", 127 | " self.value = value\n", 128 | " self.next = None\n", 129 | "\n", 130 | "class CustomQueue:\n", 131 | " \n", 132 | " def __init__(self):\n", 133 | " self.start = None\n", 134 | " self.end = None\n", 135 | " self.size = 0\n", 136 | " \n", 137 | " def enqueue(self, value):\n", 138 | " node = Node(value)\n", 139 | " if self.start == None:\n", 140 | " self.start = node\n", 141 | " # self.end = node\n", 142 | " else:\n", 143 | " self.end.next = node\n", 144 | " # self.end = node\n", 145 | " self.end = node\n", 146 | " self.size += 1\n", 147 | " \n", 148 | " def dequeue(self):\n", 149 | " if self.start == None:\n", 150 | " return None\n", 151 | " elif self.start == self.end:\n", 152 | " removed_value = self.start\n", 153 | " self.start = None\n", 154 | " self.end = None\n", 155 | " self.size -= 1\n", 156 | " return removed_value\n", 157 | " else:\n", 158 | " returned_value = self.start\n", 159 | " self.start = self.start.next\n", 160 | " returned_value.next = None\n", 161 | " self.size -= 1\n", 162 | " return returned_value\n", 163 | " \n", 164 | " def is_empty(self):\n", 165 | " return self.size == 0\n", 166 | " \n", 167 | " def qsize(self):\n", 168 | " return self.size\n", 169 | " \n", 170 | " def front(self):\n", 171 | " if self.start == None:\n", 172 | " return None\n", 173 | " else:\n", 174 | " return self.start.value\n", 175 | " \n", 176 | " def end(self):\n", 177 | " if self.end == None:\n", 178 | " return None\n", 179 | " else:\n", 180 | " return self.end.value\n", 181 | " \n", 182 | " def __str__(self):\n", 183 | " result = \"[\"\n", 184 | " i = self.start\n", 185 | " while i is not None:\n", 186 | " result += str(i.value) \n", 187 | " result += \", \"\n", 188 | " i = i.next\n", 189 | " result += \"]\"\n", 190 | " return result\n", 191 | " \n", 192 | "\n", 193 | "q1 = CustomQueue()\n", 194 | "q1.enqueue(1)\n", 195 | "q1.enqueue(2)\n", 196 | "q1.enqueue(3)\n", 197 | "print(q1)\n", 198 | "print(q1.qsize())\n", 199 | "q1.dequeue()\n", 200 | "print(q1)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 1, 206 | "metadata": {}, 207 | "outputs": [], 208 | "source": [ 209 | "### Making Queue using stack\n", 210 | "\n", 211 | "class QueueUsingStacks:\n", 212 | " def __init__(self):\n", 213 | " self.s1 = Stack()\n", 214 | " self.s2 = Stack()\n", 215 | " \n", 216 | " def enqueue(self, val):\n", 217 | " self.s1.push(val)\n", 218 | " \n", 219 | " def dequeue(self):\n", 220 | " if self.s2.is_empty():\n", 221 | " if self.s1.is_empty():\n", 222 | " return None\n", 223 | " else:\n", 224 | " while not self.s1.is_empty():\n", 225 | " v = self.s1.pop()\n", 226 | " self.s2.push(v)\n", 227 | " return self.s2.pop()" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "metadata": {}, 234 | "outputs": [], 235 | "source": [] 236 | } 237 | ], 238 | "metadata": { 239 | "kernelspec": { 240 | "display_name": "Python 3", 241 | "language": "python", 242 | "name": "python3" 243 | }, 244 | "language_info": { 245 | "codemirror_mode": { 246 | "name": "ipython", 247 | "version": 3 248 | }, 249 | "file_extension": ".py", 250 | "mimetype": "text/x-python", 251 | "name": "python", 252 | "nbconvert_exporter": "python", 253 | "pygments_lexer": "ipython3", 254 | "version": "3.7.6" 255 | } 256 | }, 257 | "nbformat": 4, 258 | "nbformat_minor": 4 259 | } 260 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/StacksAndQueues-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Stack is a LIFO structure\n", 10 | "\n", 11 | "class Stack:\n", 12 | " def __init__(self):\n", 13 | " self.data = []\n", 14 | " \n", 15 | " def push(self, value):\n", 16 | " self.data.append(value)\n", 17 | " \n", 18 | " def pop(self):\n", 19 | " return self.data.pop(-1)\n", 20 | " \n", 21 | " def top(self):\n", 22 | " return self.data[-1]\n", 23 | " \n", 24 | " def size(self):\n", 25 | " return len(self.data)\n", 26 | " \n", 27 | " def is_empty(self):\n", 28 | " return len(self.data) == 0\n", 29 | " \n", 30 | " def __str__(self):\n", 31 | " return str(self.data)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 12, 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "True\n", 44 | "0\n", 45 | "False\n", 46 | "1\n", 47 | "[1]\n", 48 | "[1, 2, 3]\n", 49 | "3\n", 50 | "[1, 2]\n", 51 | "2\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "s1 = Stack()\n", 57 | "print(s1.is_empty())\n", 58 | "print(s1.size())\n", 59 | "s1.push(1)\n", 60 | "print(s1.is_empty())\n", 61 | "print(s1.size())\n", 62 | "print(s1)\n", 63 | "s1.push(2)\n", 64 | "s1.push(3)\n", 65 | "print(s1)\n", 66 | "print(s1.pop())\n", 67 | "print(s1)\n", 68 | "print(s1.top())" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 13, 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "# Queues\n", 78 | "\n", 79 | "class Queue:\n", 80 | " def __init__(self):\n", 81 | " self.data = []\n", 82 | " \n", 83 | " # this is similar to push in stacks, adding element to the queue\n", 84 | " def enqueue(self, value):\n", 85 | " self.data.append(value)\n", 86 | " \n", 87 | " def dequeue(self):\n", 88 | " return self.data.pop(0)\n", 89 | " \n", 90 | " def is_empty(self):\n", 91 | " return len(self.data) == 0\n", 92 | " \n", 93 | " def size(self):\n", 94 | " return len(self.data)\n", 95 | " \n", 96 | " def front(self):\n", 97 | " return self.data[0]\n", 98 | " \n", 99 | " def end(self):\n", 100 | " return self.data[-1]\n", 101 | " \n", 102 | " def __str__(self):\n", 103 | " return str(self.data)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 16, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "[1, 2, 3, ]\n" 116 | ] 117 | }, 118 | { 119 | "ename": "TypeError", 120 | "evalue": "'int' object is not callable", 121 | "output_type": "error", 122 | "traceback": [ 123 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 124 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 125 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 75\u001b[0m \u001b[0mq1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0menqueue\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 76\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mq1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 77\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mq1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 126 | "\u001b[0;31mTypeError\u001b[0m: 'int' object is not callable" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "# Linked List\n", 132 | "\n", 133 | "class Node:\n", 134 | " \n", 135 | " def __init__(self, value):\n", 136 | " self.value = value\n", 137 | " self.next = None\n", 138 | "\n", 139 | "class CustomQueue:\n", 140 | " \n", 141 | " def __init__(self):\n", 142 | " self.start = None\n", 143 | " self.end = None\n", 144 | " self.size = 0\n", 145 | " \n", 146 | " def enqueue(self, value):\n", 147 | " node = Node(value)\n", 148 | " if self.start == None:\n", 149 | " self.start = node\n", 150 | " # self.end = node\n", 151 | " else:\n", 152 | " self.end.next = node\n", 153 | " # self.end = node\n", 154 | " self.end = node\n", 155 | " self.size += 1\n", 156 | " \n", 157 | " def dequeue(self):\n", 158 | " if self.start == None:\n", 159 | " return None\n", 160 | " elif self.start == self.end:\n", 161 | " removed_value = self.start\n", 162 | " self.start = None\n", 163 | " self.end = None\n", 164 | " self.size -= 1\n", 165 | " return removed_value\n", 166 | " else:\n", 167 | " returned_value = self.start\n", 168 | " self.start = self.start.next\n", 169 | " returned_value.next = None\n", 170 | " self.size -= 1\n", 171 | " return returned_value\n", 172 | " \n", 173 | " def is_empty(self):\n", 174 | " return self.size == 0\n", 175 | " \n", 176 | " def qsize(self):\n", 177 | " return self.size\n", 178 | " \n", 179 | " def front(self):\n", 180 | " if self.start == None:\n", 181 | " return None\n", 182 | " else:\n", 183 | " return self.start.value\n", 184 | " \n", 185 | " def end(self):\n", 186 | " if self.end == None:\n", 187 | " return None\n", 188 | " else:\n", 189 | " return self.end.value\n", 190 | " \n", 191 | " def __str__(self):\n", 192 | " result = \"[\"\n", 193 | " i = self.start\n", 194 | " while i is not None:\n", 195 | " result += str(i.value) \n", 196 | " result += \", \"\n", 197 | " i = i.next\n", 198 | " result += \"]\"\n", 199 | " return result\n", 200 | " \n", 201 | "\n", 202 | "q1 = CustomQueue()\n", 203 | "q1.enqueue(1)\n", 204 | "q1.enqueue(2)\n", 205 | "q1.enqueue(3)\n", 206 | "print(q1)\n", 207 | "print(q1.size())" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": null, 213 | "metadata": {}, 214 | "outputs": [], 215 | "source": [] 216 | } 217 | ], 218 | "metadata": { 219 | "kernelspec": { 220 | "display_name": "Python 3", 221 | "language": "python", 222 | "name": "python3" 223 | }, 224 | "language_info": { 225 | "codemirror_mode": { 226 | "name": "ipython", 227 | "version": 3 228 | }, 229 | "file_extension": ".py", 230 | "mimetype": "text/x-python", 231 | "name": "python", 232 | "nbconvert_exporter": "python", 233 | "pygments_lexer": "ipython3", 234 | "version": "3.7.6" 235 | } 236 | }, 237 | "nbformat": 4, 238 | "nbformat_minor": 4 239 | } 240 | -------------------------------------------------------------------------------- /09-recursion.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Factorial" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 7, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "data": { 17 | "text/plain": [ 18 | "120" 19 | ] 20 | }, 21 | "execution_count": 7, 22 | "metadata": {}, 23 | "output_type": "execute_result" 24 | } 25 | ], 26 | "source": [ 27 | "# factorial\n", 28 | "def factorial(n):\n", 29 | " if n == 1:\n", 30 | " return 1\n", 31 | " else:\n", 32 | " return n * factorial(n-1)\n", 33 | "\n", 34 | "factorial(5)" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "### Exponent" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 13, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "32\n", 54 | "1000\n" 55 | ] 56 | } 57 | ], 58 | "source": [ 59 | "\n", 60 | "# pow(a, b) = a ** b\n", 61 | "# pow(2, 5) = 1* 2 * 2 * 2 * 2 * 2\n", 62 | "\n", 63 | "def pow_loop(a, b):\n", 64 | " result = 1\n", 65 | " for i in range(b):\n", 66 | " result *= a\n", 67 | " return result\n", 68 | "\n", 69 | "print(pow_loop(2, 5))\n", 70 | "\n", 71 | "# pow(2, 5) = 2 * pow(2, 4)\n", 72 | "\n", 73 | "def pow_rec(a, b):\n", 74 | " if b == 1:\n", 75 | " return a\n", 76 | " return a * pow(a, b-1)\n", 77 | "\n", 78 | "print(pow_rec(10, 3))" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "### Pattern Printing" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 22, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "name": "stdout", 95 | "output_type": "stream", 96 | "text": [ 97 | "5\n", 98 | "4\n", 99 | "3\n", 100 | "2\n", 101 | "1\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "def print_dec(n):\n", 107 | " if n == 0:\n", 108 | " return\n", 109 | " print(n)\n", 110 | " print_dec(n-1)\n", 111 | "\n", 112 | "print_dec(5)" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 23, 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "name": "stdout", 122 | "output_type": "stream", 123 | "text": [ 124 | "5\n", 125 | "6\n", 126 | "5\n", 127 | "4\n", 128 | "3\n", 129 | "2\n", 130 | "1\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "# input as 5\n", 136 | "# 1\n", 137 | "# 2\n", 138 | "# 3\n", 139 | "# 4\n", 140 | "# 5\n", 141 | "\n", 142 | "def print_inc(n):\n", 143 | " if n == 0:\n", 144 | " return\n", 145 | " print_inc(n-1)\n", 146 | " print(n)\n", 147 | "\n", 148 | "print_inc(5)" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 26, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "5\n", 161 | "4\n", 162 | "3\n", 163 | "2\n", 164 | "1\n", 165 | "1\n", 166 | "2\n", 167 | "3\n", 168 | "4\n", 169 | "5\n", 170 | "None\n" 171 | ] 172 | } 173 | ], 174 | "source": [ 175 | "N = 5\n", 176 | "5\n", 177 | "4\n", 178 | "3\n", 179 | "2\n", 180 | "1\n", 181 | "1\n", 182 | "2\n", 183 | "3\n", 184 | "4\n", 185 | "5\n", 186 | "\n", 187 | "def print_dec_inc(n):\n", 188 | " if n == 0:\n", 189 | " return\n", 190 | " print(n)\n", 191 | " print_dec_inc(n-1)\n", 192 | " print(n)\n", 193 | "\n", 194 | "print_dec_inc(5)" 195 | ] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": {}, 200 | "source": [ 201 | "### HCF" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 32, 207 | "metadata": {}, 208 | "outputs": [ 209 | { 210 | "name": "stdout", 211 | "output_type": "stream", 212 | "text": [ 213 | "1\n", 214 | "1\n" 215 | ] 216 | } 217 | ], 218 | "source": [ 219 | "# Eucliud's HCF Theorem \n", 220 | "def hcf(a, b):\n", 221 | " while a !=0 and b != 0:\n", 222 | " if a > b:\n", 223 | " a = a - b\n", 224 | " else:\n", 225 | " b = b - a\n", 226 | " return (a + b)\n", 227 | "\n", 228 | "def hcf_rec(a, b):\n", 229 | " if a==0 or b==0:\n", 230 | " return a+b\n", 231 | " if a > b:\n", 232 | " return hcf_rec(a-b, b)\n", 233 | " else:\n", 234 | " return hcf_rec(b-a, a)\n", 235 | "\n", 236 | "print(hcf(12, 13))\n", 237 | "print(hcf_rec(12, 13))" 238 | ] 239 | }, 240 | { 241 | "cell_type": "markdown", 242 | "metadata": {}, 243 | "source": [ 244 | "### Printing Fibonacci Number" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": 39, 250 | "metadata": {}, 251 | "outputs": [ 252 | { 253 | "name": "stdout", 254 | "output_type": "stream", 255 | "text": [ 256 | "1548008755920\n" 257 | ] 258 | } 259 | ], 260 | "source": [ 261 | "# 0 1 1 2 3 5 8 13 21 ....\n", 262 | "# 0 1 2 3 4 5 6 7 8 ....\n", 263 | "# Nth fibo number = f(N)\n", 264 | "\n", 265 | "def fibo(n):\n", 266 | " a = 0\n", 267 | " b = 1\n", 268 | " for x in range(n-1):\n", 269 | " c = a + b\n", 270 | " a = b\n", 271 | " b = c\n", 272 | " return c\n", 273 | "\n", 274 | "print(fibo(60))" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 48, 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "name": "stdout", 284 | "output_type": "stream", 285 | "text": [ 286 | "43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875\n" 287 | ] 288 | } 289 | ], 290 | "source": [ 291 | "# f(6) = f(5) + f(4)\n", 292 | "# f(n) = f(n-1) + f(n-2)\n", 293 | "\n", 294 | "# Memoisation\n", 295 | "memory = [None]*2000\n", 296 | "\n", 297 | "def fibo_rec(n):\n", 298 | " if memory[n] is not None:\n", 299 | " return memory[n]\n", 300 | " if n == 0 or n == 1:\n", 301 | " return n\n", 302 | " result = fibo_rec(n-1) + fibo_rec(n-2)\n", 303 | " memory[n] = result\n", 304 | " return result\n", 305 | "\n", 306 | "print(fibo_rec(1000))" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": null, 312 | "metadata": {}, 313 | "outputs": [], 314 | "source": [ 315 | "# is_palindrome\n", 316 | "# binary search\n", 317 | "# is_sorted\n", 318 | "\n", 319 | "# homework" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 6, 325 | "metadata": {}, 326 | "outputs": [ 327 | { 328 | "name": "stdout", 329 | "output_type": "stream", 330 | "text": [ 331 | "True\n" 332 | ] 333 | } 334 | ], 335 | "source": [ 336 | "# to check whether a particular string is palindrome or not\n", 337 | "#if str_a == reverse(str_a) --> it is a palindrome, LEVEL\n", 338 | "\n", 339 | "def is_palindrome(s):\n", 340 | " n = len(s)\n", 341 | " if n <= 1:\n", 342 | " return True\n", 343 | " return (is_palindrome(s[1:n-1]) and (s[0] == s[n-1]))\n", 344 | "\n", 345 | "print(is_palindrome(\"level\"))" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": null, 351 | "metadata": {}, 352 | "outputs": [], 353 | "source": [] 354 | } 355 | ], 356 | "metadata": { 357 | "kernelspec": { 358 | "display_name": "Python 3", 359 | "language": "python", 360 | "name": "python3" 361 | }, 362 | "language_info": { 363 | "codemirror_mode": { 364 | "name": "ipython", 365 | "version": 3 366 | }, 367 | "file_extension": ".py", 368 | "mimetype": "text/x-python", 369 | "name": "python", 370 | "nbconvert_exporter": "python", 371 | "pygments_lexer": "ipython3", 372 | "version": "3.7.2" 373 | } 374 | }, 375 | "nbformat": 4, 376 | "nbformat_minor": 2 377 | } 378 | -------------------------------------------------------------------------------- /07-strings.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 8, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "['a', 'n', 'a', 'n', 't']\n", 13 | "n\n", 14 | "['a', 'N', 'a', 'n', 't']\n", 15 | "anant\n", 16 | "n\n" 17 | ] 18 | } 19 | ], 20 | "source": [ 21 | "list_alphabets = [\"a\", \"n\", \"a\", \"n\", \"t\"]\n", 22 | "print(list_alphabets)\n", 23 | "print(list_alphabets[1]) # access element by index\n", 24 | "list_alphabets[1] = \"N\" # change is possible because lists are mutable\n", 25 | "print(list_alphabets)\n", 26 | "\n", 27 | "str_alphabets = \"anant\"\n", 28 | "print(str_alphabets)\n", 29 | "print(str_alphabets[1]) # accessing element by index is same as lists\n", 30 | "# str_alphabets[1] = \"N\" # change is not possible because str is an immutable object" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 10, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "Hello World!\n", 43 | "llo World!\n", 44 | "!\n", 45 | "HloWrd\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "s = \"Hello World!\"\n", 51 | "print(s[:])\n", 52 | "print(s[2:])\n", 53 | "print(s[-1])\n", 54 | "print(s[::2])" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 12, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "e\n", 67 | "l\n", 68 | "l\n", 69 | "o\n", 70 | " \n", 71 | "W\n", 72 | "o\n", 73 | "r\n", 74 | "l\n", 75 | "d\n", 76 | "!\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "for character in s[1:]:\n", 82 | " print(character)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 15, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "name": "stdout", 92 | "output_type": "stream", 93 | "text": [ 94 | "Jesslin Ann\n", 95 | "Jesslin Jesslin Jesslin \n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "fname = \"Jesslin\"\n", 101 | "lname = \"Ann\"\n", 102 | "print(fname + \" \" + lname)\n", 103 | "print((fname + \" \")*3)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "### Operations on String" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 32, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "name": "stdout", 120 | "output_type": "stream", 121 | "text": [ 122 | "HELLO WORLD!\n", 123 | "hello world!\n", 124 | "3\n", 125 | "2\n", 126 | "3\n", 127 | "1\n", 128 | "True\n", 129 | "False\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "s = \"Hello World!\"\n", 135 | "print(s.upper())\n", 136 | "print(s.lower())\n", 137 | "print(s.count(\"l\"))\n", 138 | "print(s.count(\"l\", 3))\n", 139 | "print(s.count(\"l\", 2))\n", 140 | "print(s.count(\"l\", 4))\n", 141 | "print(s.startswith(\"He\"))\n", 142 | "print(s.endswith(\"e!\"))" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 40, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "['2-conditions.py', 'Memory.ipynb', 'Functions_practice.ipynb', 'Lists.ipynb', '3-loops.py', '.ipynb_checkpoints', 'Strings.ipynb', '4-functions.py', '.idea']\n", 155 | "['Memory.ipynb', 'Functions_practice.ipynb', 'Lists.ipynb', 'Strings.ipynb']\n" 156 | ] 157 | } 158 | ], 159 | "source": [ 160 | " # Application of endswith method on strings\n", 161 | "import os\n", 162 | "names = os.listdir(\".\")\n", 163 | "print(names)\n", 164 | "python_files = []\n", 165 | "for name in names:\n", 166 | " if name.endswith(\".ipynb\"):\n", 167 | " python_files.append(name)\n", 168 | "print(python_files)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 49, 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "name": "stdout", 178 | "output_type": "stream", 179 | "text": [ 180 | "https://www.reue\n" 181 | ] 182 | }, 183 | { 184 | "data": { 185 | "text/plain": [ 186 | "True" 187 | ] 188 | }, 189 | "execution_count": 49, 190 | "metadata": {}, 191 | "output_type": "execute_result" 192 | } 193 | ], 194 | "source": [ 195 | " # Application of starts method on strings\n", 196 | "link1 = \"hTTps://www.reue\"\n", 197 | "link2 = \"Http://www.rebner\"\n", 198 | "link3 = \"edhsuhuwe://\"\n", 199 | "\n", 200 | "link2.lower().startswith(\"http\")" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 52, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "name": "stdout", 210 | "output_type": "stream", 211 | "text": [ 212 | "['the', 'tallest', 'tree', 'is', 'also', 'the', 'oldest', 'tree']\n" 213 | ] 214 | } 215 | ], 216 | "source": [ 217 | "sentence = \"the tallest tree is also the oldest tree\"\n", 218 | "result = sentence.split(\" \")\n", 219 | "print(result)" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 57, 225 | "metadata": {}, 226 | "outputs": [ 227 | { 228 | "name": "stdout", 229 | "output_type": "stream", 230 | "text": [ 231 | "8\n", 232 | "8\n", 233 | "40\n" 234 | ] 235 | } 236 | ], 237 | "source": [ 238 | "# Solution-1\n", 239 | "count = 0\n", 240 | "for i in result:\n", 241 | " count += 1\n", 242 | "print(count)\n", 243 | "\n", 244 | "# Solution-2\n", 245 | "print(len(result))\n", 246 | "print(len(sentence))" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "### f-strings" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 66, 259 | "metadata": {}, 260 | "outputs": [ 261 | { 262 | "name": "stdout", 263 | "output_type": "stream", 264 | "text": [ 265 | "ANANT MITTAL 24\n", 266 | "Anant Mittal\n", 267 | "Anant Mittal 24\n", 268 | "{fname} {lname} {age}\n" 269 | ] 270 | } 271 | ], 272 | "source": [ 273 | "fname = \"Anant\"\n", 274 | "lname = \"Mittal\"\n", 275 | "age = 24\n", 276 | "\n", 277 | "print(fname.upper() + \" \" + lname.upper() + \" \" + str(age))\n", 278 | "\n", 279 | "print(fname + \" \" + lname)\n", 280 | "\n", 281 | "print(f\"{fname} {lname} {age}\")\n", 282 | "print(\"{fname} {lname} {age}\")" 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": {}, 288 | "source": [ 289 | "### Palindrome strings\n", 290 | "\n", 291 | "LEVEL is a palindrome \\\n", 292 | "MADAM is a palindrome" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 71, 298 | "metadata": {}, 299 | "outputs": [ 300 | { 301 | "name": "stdout", 302 | "output_type": "stream", 303 | "text": [ 304 | "[4, 3, 2, 1]\n", 305 | "tnana\n", 306 | "False\n", 307 | "True\n" 308 | ] 309 | } 310 | ], 311 | "source": [ 312 | "a = [1, 2, 3, 4]\n", 313 | "print(a[::-1])\n", 314 | "b = \"anant\"\n", 315 | "print(b[::-1])\n", 316 | "\n", 317 | "def is_palindrome(string):\n", 318 | " if string == string[::-1]:\n", 319 | " return True\n", 320 | " else:\n", 321 | " return False\n", 322 | "\n", 323 | "print(is_palindrome(\"anant\"))\n", 324 | "print(is_palindrome(\"level\"))" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": 77, 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "name": "stdout", 334 | "output_type": "stream", 335 | "text": [ 336 | "0\n", 337 | "False\n", 338 | "True\n" 339 | ] 340 | } 341 | ], 342 | "source": [ 343 | "def is_palindrome(string):\n", 344 | " count = 0\n", 345 | " left = 0\n", 346 | " right = len(string) - 1\n", 347 | " while left <= right:\n", 348 | " if string[left] == string[right]:\n", 349 | " left += 1\n", 350 | " right -= 1\n", 351 | " count += 1\n", 352 | " else:\n", 353 | " print(count)\n", 354 | " return False\n", 355 | " return True\n", 356 | "\n", 357 | "print(is_palindrome(\"anant\"))\n", 358 | "print(is_palindrome(\"level\"))" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": null, 364 | "metadata": {}, 365 | "outputs": [], 366 | "source": [] 367 | } 368 | ], 369 | "metadata": { 370 | "kernelspec": { 371 | "display_name": "Python 3", 372 | "language": "python", 373 | "name": "python3" 374 | }, 375 | "language_info": { 376 | "codemirror_mode": { 377 | "name": "ipython", 378 | "version": 3 379 | }, 380 | "file_extension": ".py", 381 | "mimetype": "text/x-python", 382 | "name": "python", 383 | "nbconvert_exporter": "python", 384 | "pygments_lexer": "ipython3", 385 | "version": "3.7.2" 386 | } 387 | }, 388 | "nbformat": 4, 389 | "nbformat_minor": 2 390 | } 391 | -------------------------------------------------------------------------------- /08-searching-Swapping-Sorting.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Linear Search" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 17, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "data": { 17 | "text/plain": [ 18 | "4" 19 | ] 20 | }, 21 | "execution_count": 17, 22 | "metadata": {}, 23 | "output_type": "execute_result" 24 | } 25 | ], 26 | "source": [ 27 | "temp_list = [32, 34, 54, 65, 12, 78]\n", 28 | "\n", 29 | "def linear_search(arr, target):\n", 30 | " # return the index of the target element\n", 31 | " n = len(arr)\n", 32 | " for index in range(n):\n", 33 | " if arr[index] == target:\n", 34 | " return index\n", 35 | " return None\n", 36 | "\n", 37 | "def linear_search(arr, target):\n", 38 | " # return the index of the target element\n", 39 | " n = len(arr)\n", 40 | " for index, element in enumerate(arr):\n", 41 | " if element == target:\n", 42 | " return index\n", 43 | " return None\n", 44 | "\n", 45 | "linear_search(temp_list, 54)\n", 46 | "linear_search(temp_list, 12)\n", 47 | "#linear_search(temp_list, 100)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "### Binary Search" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 26, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "def binary_search(arr, target):\n", 64 | " n = len(arr)\n", 65 | " left = 0\n", 66 | " right = n - 1\n", 67 | " mid = (left + right) // 2\n", 68 | " \n", 69 | " while left <= right:\n", 70 | " if target == arr[mid]:\n", 71 | " return mid\n", 72 | " elif target < arr[mid]:\n", 73 | " # left subarray\n", 74 | " right = mid - 1\n", 75 | " mid = (left + right) // 2\n", 76 | " else:\n", 77 | " # right subarray\n", 78 | " left = mid + 1\n", 79 | " mid = (left + right) // 2\n", 80 | " return None\n", 81 | "\n", 82 | "temp_list = [7, 8, 10, 45, 56, 78, 100]\n", 83 | "# for this to work, the array needs to sorted\n", 84 | "binary_search(temp_list, 56)\n", 85 | "binary_search(temp_list, 8)\n", 86 | "binary_search(temp_list, 79)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "### Swapping" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 37, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "5 6 4335567120 4335567152\n", 106 | "6 5 4335567152 4335567120\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "x, y = 5, 6\n", 112 | "\n", 113 | "def swap(x, y):\n", 114 | " return y, x\n", 115 | "\n", 116 | "print(x, y, id(x), id(y))\n", 117 | "x, y = swap(x, y)\n", 118 | "print(x, y, id(x), id(y))" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "#### Reference by value, scoping" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 34, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "name": "stdout", 135 | "output_type": "stream", 136 | "text": [ 137 | "5 6\n", 138 | "10 8\n" 139 | ] 140 | } 141 | ], 142 | "source": [ 143 | "x,y = 5, 6\n", 144 | "\n", 145 | "# Call by value\n", 146 | "def dummy_func(x, y):\n", 147 | " x = 10\n", 148 | " y = 8\n", 149 | " return None\n", 150 | "\n", 151 | "# print(x, y)\n", 152 | "# dummy_func(x, y)\n", 153 | "# print(x, y)\n", 154 | "\n", 155 | "def dummy_func2(a, b):\n", 156 | " global x\n", 157 | " global y\n", 158 | " x = 10\n", 159 | " y = 8\n", 160 | " return None\n", 161 | "\n", 162 | "print(x, y)\n", 163 | "dummy_func2(x, y)\n", 164 | "print(x, y)" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": null, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [ 173 | "global_list = [10, 20, 30, 40, 50, 60]\n", 174 | "global_int = 15\n", 175 | "\n", 176 | "def dummy_list_func1(local_list):\n", 177 | " local_list[1] += 5\n", 178 | " global_int += 5\n", 179 | " return None\n", 180 | "\n", 181 | "print(global_list, global_int)\n", 182 | "dummy_list_func1_func1(global_list, global_int)\n", 183 | "print(global_list, global_int) " 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 35, 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "name": "stdout", 193 | "output_type": "stream", 194 | "text": [ 195 | "[10, 20, 30, 40, 50, 60, 70]\n", 196 | "[10, 20, 30, 60, 50, 40, 70]\n" 197 | ] 198 | } 199 | ], 200 | "source": [ 201 | "arr = [10, 20, 30, 40, 50, 60, 70]\n", 202 | "\n", 203 | "def swap(arr, index1, index2):\n", 204 | " arr[index1], arr[index2] = arr[index2], arr[index1]\n", 205 | " \n", 206 | "print(arr)\n", 207 | "swap(arr, 3, 5)\n", 208 | "print(arr)" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": {}, 214 | "source": [ 215 | "### Sorting" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 39, 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "name": "stdout", 225 | "output_type": "stream", 226 | "text": [ 227 | "3\n" 228 | ] 229 | } 230 | ], 231 | "source": [ 232 | "# try to find the minimum number in a list\n", 233 | "\n", 234 | "arr = [9, 8, 7, 3, 5, 10]\n", 235 | "\n", 236 | "# a, b, c = 3, 2, 4\n", 237 | "# min_ = a\n", 238 | "# if min_ > b:\n", 239 | "# min_ = b\n", 240 | "# if min_ > c:\n", 241 | "# min_ = c\n", 242 | "# print(min_)\n", 243 | "\n", 244 | "min_ = arr[0]\n", 245 | "for i in range(1, len(arr)):\n", 246 | " if arr[i] < min_:\n", 247 | " min_ = arr[i]\n", 248 | "print(min_)" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 45, 254 | "metadata": {}, 255 | "outputs": [ 256 | { 257 | "name": "stdout", 258 | "output_type": "stream", 259 | "text": [ 260 | "[9, 8, 7, 3, 5, 10]\n", 261 | "[3, 5, 7, 8, 9, 10]\n" 262 | ] 263 | } 264 | ], 265 | "source": [ 266 | "# selection sort\n", 267 | "arr = [9, 8, 7, 3, 5, 10]\n", 268 | "\n", 269 | "def selection_sort(arr):\n", 270 | " n = len(arr)\n", 271 | " for index in range(0, n-1):\n", 272 | " min_idx = index\n", 273 | " for i in range(min_idx+1, n):\n", 274 | " #print(i)\n", 275 | " if arr[i] < arr[min_idx]:\n", 276 | " min_idx = i\n", 277 | " arr[min_idx], arr[index] = arr[index], arr[min_idx]\n", 278 | " #print(arr)\n", 279 | "\n", 280 | "print(arr)\n", 281 | "selection_sort(arr)\n", 282 | "print(arr)" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": null, 288 | "metadata": {}, 289 | "outputs": [], 290 | "source": [ 291 | "# to watch the bubble sort video" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": 4, 297 | "metadata": {}, 298 | "outputs": [ 299 | { 300 | "name": "stdout", 301 | "output_type": "stream", 302 | "text": [ 303 | "[10, 9, 8, 7, 6, 5]\n", 304 | "After Pass 1 array is [9, 8, 7, 6, 5, 10]\n", 305 | "After Pass 2 array is [8, 7, 6, 5, 9, 10]\n", 306 | "After Pass 3 array is [7, 6, 5, 8, 9, 10]\n", 307 | "After Pass 4 array is [6, 5, 7, 8, 9, 10]\n", 308 | "After Pass 5 array is [5, 6, 7, 8, 9, 10]\n", 309 | "[5, 6, 7, 8, 9, 10]\n" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "arr = [10, 9, 8, 7, 6, 5]\n", 315 | "\n", 316 | "def bubble_sort(arr):\n", 317 | " n = len(arr)\n", 318 | " for j in range(n-1):\n", 319 | " for i in range(0, n-1-j):\n", 320 | " if arr[i] > arr[i+1]:\n", 321 | " arr[i], arr[i+1] = arr[i+1], arr[i]\n", 322 | " print(f\"After Pass {j+1} array is {arr}\")\n", 323 | " \n", 324 | "print(arr)\n", 325 | "bubble_sort(arr)\n", 326 | "print(arr)" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "execution_count": null, 332 | "metadata": {}, 333 | "outputs": [], 334 | "source": [ 335 | "### Insertion Sort\n", 336 | "def insertion_sort(arr):\n", 337 | " n = len(arr)\n", 338 | " for i in range(1, n):\n", 339 | " value = arr[i]\n", 340 | " j = i - 1\n", 341 | " while j >= 0 and value < arr[j]:\n", 342 | " arr[j+1] = arr[j]\n", 343 | " j = j - 1\n", 344 | " arr[j+1] = value" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": null, 350 | "metadata": {}, 351 | "outputs": [], 352 | "source": [] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": null, 357 | "metadata": {}, 358 | "outputs": [], 359 | "source": [] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": null, 364 | "metadata": {}, 365 | "outputs": [], 366 | "source": [] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": null, 371 | "metadata": {}, 372 | "outputs": [], 373 | "source": [] 374 | } 375 | ], 376 | "metadata": { 377 | "kernelspec": { 378 | "display_name": "Python 3", 379 | "language": "python", 380 | "name": "python3" 381 | }, 382 | "language_info": { 383 | "codemirror_mode": { 384 | "name": "ipython", 385 | "version": 3 386 | }, 387 | "file_extension": ".py", 388 | "mimetype": "text/x-python", 389 | "name": "python", 390 | "nbconvert_exporter": "python", 391 | "pygments_lexer": "ipython3", 392 | "version": "3.7.2" 393 | } 394 | }, 395 | "nbformat": 4, 396 | "nbformat_minor": 2 397 | } 398 | -------------------------------------------------------------------------------- /05-memory.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 5, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "41\n", 13 | "\n", 14 | "4440642960\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "x = 41\n", 20 | "print(x)\n", 21 | "print(type(x))\n", 22 | "print(id(x))" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 7, 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "name": "stdout", 32 | "output_type": "stream", 33 | "text": [ 34 | "[10, 20, 30]\n", 35 | "[10, 20, 30]\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "a = [10, 20, 30] # mutable object\n", 41 | "print(a)\n", 42 | "b = a\n", 43 | "print(b)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 9, 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "name": "stdout", 53 | "output_type": "stream", 54 | "text": [ 55 | "[10, 12, 30]\n", 56 | "[10, 12, 30]\n" 57 | ] 58 | } 59 | ], 60 | "source": [ 61 | "a[1] = 12\n", 62 | "print(a)\n", 63 | "print(b)" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 10, 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdout", 73 | "output_type": "stream", 74 | "text": [ 75 | "4483821256\n", 76 | "4483821256\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "print(id(a))\n", 82 | "print(id(b)) # b is referencing to the same object as a" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 12, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "name": "stdout", 92 | "output_type": "stream", 93 | "text": [ 94 | "4440641968 4440641968\n", 95 | "4440642608 4440641968\n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "a = 10\n", 101 | "b = a\n", 102 | "print(id(a), id(b))\n", 103 | "a = 30\n", 104 | "print(id(a), id(b)) # b still points to the old object\n", 105 | "# a starts pointing to a new object" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 13, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "4483863688 4483863880\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "a = [10, 20, 30]\n", 123 | "b = [10, 20, 30]\n", 124 | "print(id(a), id(b))" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 14, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "4440641968 4440641968\n" 137 | ] 138 | } 139 | ], 140 | "source": [ 141 | "a = 10\n", 142 | "b = 10\n", 143 | "print(id(a), id(b))" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 15, 149 | "metadata": {}, 150 | "outputs": [ 151 | { 152 | "name": "stdout", 153 | "output_type": "stream", 154 | "text": [ 155 | "4484380944 4484380912\n" 156 | ] 157 | } 158 | ], 159 | "source": [ 160 | "a = 1346327372\n", 161 | "b = 1346327372\n", 162 | "print(id(a), id(b))" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 19, 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "4475029896 4484629000\n", 175 | "4475029896 4484629000 4475029896\n", 176 | "4475029896 4484629000 4475029896 4485311048\n", 177 | "4475029896 4484629000 4475029896 4485311048 4485273352\n" 178 | ] 179 | } 180 | ], 181 | "source": [ 182 | "a = [10, 20, 30]\n", 183 | "b = [10, 20, 30]\n", 184 | "print(id(a), id(b))\n", 185 | "c = a\n", 186 | "print(id(a), id(b), id(c))\n", 187 | "d = a[:]\n", 188 | "print(id(a), id(b), id(c), id(d))\n", 189 | "e = list(a) # use this to create sep copy when you don't know the data\n", 190 | "print(id(a), id(b), id(c), id(d), id(e))" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 22, 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "name": "stdout", 200 | "output_type": "stream", 201 | "text": [ 202 | "True\n", 203 | "False\n", 204 | "True\n" 205 | ] 206 | } 207 | ], 208 | "source": [ 209 | "print(a == b)\n", 210 | "print(a is b)\n", 211 | "print(a is c)" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "### Swapping Memory" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 24, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "name": "stdout", 228 | "output_type": "stream", 229 | "text": [ 230 | "10 5 4440641968 4440641808\n", 231 | "5 10 4440641808 4440641968\n" 232 | ] 233 | } 234 | ], 235 | "source": [ 236 | "x, y = 10, 5\n", 237 | "print(x, y, id(x), id(y))\n", 238 | "temp = x\n", 239 | "x = y\n", 240 | "y = temp\n", 241 | "print(x, y, id(x), id(y)) # This is general method for swapping in any programming language" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 26, 247 | "metadata": {}, 248 | "outputs": [ 249 | { 250 | "name": "stdout", 251 | "output_type": "stream", 252 | "text": [ 253 | "10 5 4440641968 4440641808\n", 254 | "5 10 4440641808 4440641968\n" 255 | ] 256 | } 257 | ], 258 | "source": [ 259 | "x, y = 10, 5\n", 260 | "print(x, y, id(x), id(y))\n", 261 | "x, y = y, x\n", 262 | "print(x, y, id(x), id(y))" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": 27, 268 | "metadata": {}, 269 | "outputs": [ 270 | { 271 | "name": "stdout", 272 | "output_type": "stream", 273 | "text": [ 274 | "10 5 4440641968 4440641808\n", 275 | "5 10 4440641808 4440641968\n" 276 | ] 277 | } 278 | ], 279 | "source": [ 280 | "def swap(x, y):\n", 281 | " return y, x\n", 282 | "\n", 283 | "x, y = 10, 5\n", 284 | "print(x, y, id(x), id(y))\n", 285 | "x, y = swap(x, y)\n", 286 | "print(x, y, id(x), id(y))\n", 287 | "\n", 288 | "# functions also take references to the objects\n", 289 | "\n", 290 | "# Homework is to check how to pass arguments as well but not references" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": 29, 296 | "metadata": {}, 297 | "outputs": [ 298 | { 299 | "name": "stdout", 300 | "output_type": "stream", 301 | "text": [ 302 | "[10, 20, 30]\n", 303 | "[10, 30, 20]\n" 304 | ] 305 | } 306 | ], 307 | "source": [ 308 | "arr = [10, 20, 30] # if I have to swap elements at index 1 and 2\n", 309 | "print(arr)\n", 310 | "arr[1], arr[2] = swap(arr[1], arr[2])\n", 311 | "print(arr)" 312 | ] 313 | }, 314 | { 315 | "cell_type": "markdown", 316 | "metadata": {}, 317 | "source": [ 318 | "### Linear Search" 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": 30, 324 | "metadata": {}, 325 | "outputs": [ 326 | { 327 | "name": "stdout", 328 | "output_type": "stream", 329 | "text": [ 330 | "4\n" 331 | ] 332 | } 333 | ], 334 | "source": [ 335 | "marks = [10, 20, 40, 90, 50, 70, 45, 67, 34, 56, 21, 11]\n", 336 | "\n", 337 | "def linear_search(arr, target):\n", 338 | " for i in range(0, len(arr)):\n", 339 | " if target == arr[i]:\n", 340 | " return i\n", 341 | " return None\n", 342 | "\n", 343 | "index = linear_search(marks, 50)\n", 344 | "print(index)" 345 | ] 346 | }, 347 | { 348 | "cell_type": "markdown", 349 | "metadata": {}, 350 | "source": [ 351 | "### List Comprehension" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": 37, 357 | "metadata": { 358 | "scrolled": true 359 | }, 360 | "outputs": [ 361 | { 362 | "name": "stdout", 363 | "output_type": "stream", 364 | "text": [ 365 | "[100, 144, 169, 225, 400]\n" 366 | ] 367 | } 368 | ], 369 | "source": [ 370 | "arr = [10, 12, 13, 15, 20] \n", 371 | "\n", 372 | "sq = []\n", 373 | "# append the squares of all the numbers\n", 374 | "for number in arr:\n", 375 | " sq.append(number ** 2)\n", 376 | " \n", 377 | "print(sq)" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 41, 383 | "metadata": {}, 384 | "outputs": [ 385 | { 386 | "name": "stdout", 387 | "output_type": "stream", 388 | "text": [ 389 | "[100, 144, 169, 225, 400]\n" 390 | ] 391 | } 392 | ], 393 | "source": [ 394 | "sq = [number**2 for number in arr]\n", 395 | "print(sq)" 396 | ] 397 | }, 398 | { 399 | "cell_type": "markdown", 400 | "metadata": {}, 401 | "source": [ 402 | "### 2D lists" 403 | ] 404 | }, 405 | { 406 | "cell_type": "code", 407 | "execution_count": 52, 408 | "metadata": {}, 409 | "outputs": [ 410 | { 411 | "name": "stdout", 412 | "output_type": "stream", 413 | "text": [ 414 | "[7, 8, 9]\n", 415 | "[7, 8, 9]\n", 416 | "[1, 2, 3]\n", 417 | "[1, 2, 3]\n" 418 | ] 419 | } 420 | ], 421 | "source": [ 422 | "###### lists within a list\n", 423 | "# 1 2 3\n", 424 | "# 4 5 6\n", 425 | "# 7 8 9\n", 426 | "\n", 427 | "arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", 428 | "\n", 429 | "#arr = [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]]]\n", 430 | "\n", 431 | "# print(arr[0])\n", 432 | "# print(arr[1])\n", 433 | "# print(arr[2])\n", 434 | "# print(arr[0][1])\n", 435 | "# print(arr[2][2])\n", 436 | "# print(arr[1][2])\n", 437 | "# print(arr[-1][0])\n", 438 | "\n", 439 | "print(arr[-1])\n", 440 | "print(arr[-1][:])\n", 441 | "\n", 442 | "x = [1, 2, 3]\n", 443 | "print(x)\n", 444 | "print(x[:])" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": null, 450 | "metadata": {}, 451 | "outputs": [], 452 | "source": [] 453 | } 454 | ], 455 | "metadata": { 456 | "kernelspec": { 457 | "display_name": "Python 3", 458 | "language": "python", 459 | "name": "python3" 460 | }, 461 | "language_info": { 462 | "codemirror_mode": { 463 | "name": "ipython", 464 | "version": 3 465 | }, 466 | "file_extension": ".py", 467 | "mimetype": "text/x-python", 468 | "name": "python", 469 | "nbconvert_exporter": "python", 470 | "pygments_lexer": "ipython3", 471 | "version": "3.7.2" 472 | } 473 | }, 474 | "nbformat": 4, 475 | "nbformat_minor": 2 476 | } 477 | -------------------------------------------------------------------------------- /06-lists.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "[100, 99, 56, 76, 45, 30]\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "marks = [100, 99, 56, 76, 45, 30]\n", 18 | "print(marks)" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "name": "stdout", 28 | "output_type": "stream", 29 | "text": [ 30 | "100\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "print(marks[0]) # indexing" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 3, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "99\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "print(marks[1]) " 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 4, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "name": "stdout", 62 | "output_type": "stream", 63 | "text": [ 64 | "30\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "print(marks[-1]) # indexing" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 5, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "45\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "print(marks[-2])" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 6, 92 | "metadata": {}, 93 | "outputs": [ 94 | { 95 | "name": "stdout", 96 | "output_type": "stream", 97 | "text": [ 98 | "[1, 2, 3, 4.5, 8, 9.2]\n" 99 | ] 100 | } 101 | ], 102 | "source": [ 103 | "a = [1, 2, 3, 4.5, 8, 9.2]\n", 104 | "print(a)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 7, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "b = [\"a\", 1, \"anant\", 2.1, True]" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 8, 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "name": "stdout", 123 | "output_type": "stream", 124 | "text": [ 125 | "['a', 1, 'anant', 2.1, True, [1, 2, 3, 4.5, 8, 9.2]]\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "c = [\"a\", 1, \"anant\", 2.1, True, a]\n", 131 | "print(c)" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "### Value Assignment with index" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 9, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "[100, 101, 56, 76, 45, 30]\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "x = 1\n", 156 | "marks[1] = 101\n", 157 | "print(marks)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "### Splicing" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 14, 170 | "metadata": {}, 171 | "outputs": [ 172 | { 173 | "name": "stdout", 174 | "output_type": "stream", 175 | "text": [ 176 | "[101, 56, 76, 45]\n", 177 | "[100, 101, 56, 76, 45]\n", 178 | "[76, 45, 30]\n", 179 | "[101, 76, 30]\n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "print(marks[1:5])\n", 185 | "print(marks[:5])\n", 186 | "print(marks[3:])\n", 187 | "print(marks[1::2])" 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": {}, 193 | "source": [ 194 | "### Appending" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 16, 200 | "metadata": {}, 201 | "outputs": [ 202 | { 203 | "name": "stdout", 204 | "output_type": "stream", 205 | "text": [ 206 | "[100, 101, 56, 76, 45, 30]\n", 207 | "[100, 101, 56, 76, 45, 30, 33]\n" 208 | ] 209 | } 210 | ], 211 | "source": [ 212 | "print(marks)\n", 213 | "marks.append(33) # inserts the element in the end\n", 214 | "print(marks)" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 18, 220 | "metadata": {}, 221 | "outputs": [ 222 | { 223 | "name": "stdout", 224 | "output_type": "stream", 225 | "text": [ 226 | "[100, 101, 34, 56, 76, 45, 30, 33]\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "marks.insert(2, 34)\n", 232 | "print(marks)" 233 | ] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": {}, 238 | "source": [ 239 | "### Extending" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 19, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "name": "stdout", 249 | "output_type": "stream", 250 | "text": [ 251 | "[100, 101, 34, 56, 76, 45, 30, 33, 1, 2, 3]\n" 252 | ] 253 | } 254 | ], 255 | "source": [ 256 | "marks.extend([1, 2, 3])\n", 257 | "print(marks)" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 23, 263 | "metadata": {}, 264 | "outputs": [], 265 | "source": [ 266 | "[100, 101, 34, 56, 76, 45, 30, 33, [1, 2, 3]]\n", 267 | "marks2 = [1, 2, 3]\n", 268 | "# marks.append([1, 2, 3])\n", 269 | "# print(marks)" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 26, 275 | "metadata": {}, 276 | "outputs": [ 277 | { 278 | "name": "stdout", 279 | "output_type": "stream", 280 | "text": [ 281 | "[100, 101, 34, 56, 76, 45, 30, 33, 1, 2, 3, [1, 2, 3]]\n", 282 | "[100, 101, 34, 56, 76, 45, 30, 33, 1, 2, 3]\n" 283 | ] 284 | } 285 | ], 286 | "source": [ 287 | "print(marks)\n", 288 | "marks.remove([1, 2, 3])\n", 289 | "print(marks)" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 27, 295 | "metadata": {}, 296 | "outputs": [ 297 | { 298 | "name": "stdout", 299 | "output_type": "stream", 300 | "text": [ 301 | "[100, 101, 34, 56, 76, 45, 30, 33, 1, 2, 3, 101]\n" 302 | ] 303 | } 304 | ], 305 | "source": [ 306 | "marks.append(101)\n", 307 | "print(marks)" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 28, 313 | "metadata": {}, 314 | "outputs": [ 315 | { 316 | "name": "stdout", 317 | "output_type": "stream", 318 | "text": [ 319 | "[100, 34, 56, 76, 45, 30, 33, 1, 2, 3, 101]\n" 320 | ] 321 | } 322 | ], 323 | "source": [ 324 | "marks.remove(101)\n", 325 | "print(marks)" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 32, 331 | "metadata": {}, 332 | "outputs": [ 333 | { 334 | "name": "stdout", 335 | "output_type": "stream", 336 | "text": [ 337 | "[100, 34, 45, 30, 33, 1, 2, 3, 101]\n" 338 | ] 339 | } 340 | ], 341 | "source": [ 342 | "marks.pop(2)\n", 343 | "print(marks)\n", 344 | "marks.append(101)\n", 345 | "marks.append(100)" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 33, 351 | "metadata": {}, 352 | "outputs": [ 353 | { 354 | "name": "stdout", 355 | "output_type": "stream", 356 | "text": [ 357 | "[100, 34, 45, 30, 33, 1, 2, 3, 101, 101, 100]\n" 358 | ] 359 | } 360 | ], 361 | "source": [ 362 | "print(marks)" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": 34, 368 | "metadata": {}, 369 | "outputs": [ 370 | { 371 | "name": "stdout", 372 | "output_type": "stream", 373 | "text": [ 374 | "2\n" 375 | ] 376 | } 377 | ], 378 | "source": [ 379 | "print(marks.count(100))" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": 35, 385 | "metadata": {}, 386 | "outputs": [ 387 | { 388 | "name": "stdout", 389 | "output_type": "stream", 390 | "text": [ 391 | "1\n" 392 | ] 393 | } 394 | ], 395 | "source": [ 396 | "print(marks.count(33))" 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 36, 402 | "metadata": {}, 403 | "outputs": [ 404 | { 405 | "name": "stdout", 406 | "output_type": "stream", 407 | "text": [ 408 | "550\n" 409 | ] 410 | } 411 | ], 412 | "source": [ 413 | "print(sum(marks))" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": 37, 419 | "metadata": {}, 420 | "outputs": [ 421 | { 422 | "name": "stdout", 423 | "output_type": "stream", 424 | "text": [ 425 | "11\n" 426 | ] 427 | } 428 | ], 429 | "source": [ 430 | "print(len(marks))" 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "execution_count": 38, 436 | "metadata": {}, 437 | "outputs": [ 438 | { 439 | "name": "stdout", 440 | "output_type": "stream", 441 | "text": [ 442 | "101 1\n" 443 | ] 444 | } 445 | ], 446 | "source": [ 447 | "print(max(marks), min(marks))" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 40, 453 | "metadata": {}, 454 | "outputs": [ 455 | { 456 | "name": "stdout", 457 | "output_type": "stream", 458 | "text": [ 459 | "[1, 2, 3, 4]\n" 460 | ] 461 | } 462 | ], 463 | "source": [ 464 | "y = [1, 2] + [3, 4] # list concatenation\n", 465 | "print(y)" 466 | ] 467 | }, 468 | { 469 | "cell_type": "code", 470 | "execution_count": 41, 471 | "metadata": {}, 472 | "outputs": [ 473 | { 474 | "name": "stdout", 475 | "output_type": "stream", 476 | "text": [ 477 | "1\n", 478 | "2\n", 479 | "3\n", 480 | "4\n" 481 | ] 482 | } 483 | ], 484 | "source": [ 485 | "for i in y:\n", 486 | " print(i)" 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "execution_count": 42, 492 | "metadata": {}, 493 | "outputs": [ 494 | { 495 | "name": "stdout", 496 | "output_type": "stream", 497 | "text": [ 498 | "0\n", 499 | "1\n", 500 | "2\n", 501 | "3\n" 502 | ] 503 | } 504 | ], 505 | "source": [ 506 | "for i in range(0, 4):\n", 507 | " print(i)" 508 | ] 509 | }, 510 | { 511 | "cell_type": "code", 512 | "execution_count": 50, 513 | "metadata": {}, 514 | "outputs": [ 515 | { 516 | "name": "stdout", 517 | "output_type": "stream", 518 | "text": [ 519 | "[100, 34, 45, 30, 33, 1, 2, 3, 101, 101, 100]\n", 520 | "0 100\n", 521 | "1 34\n", 522 | "2 45\n", 523 | "3 30\n", 524 | "--------------------\n", 525 | "100\n", 526 | "34\n", 527 | "45\n", 528 | "30\n", 529 | "33\n", 530 | "1\n", 531 | "2\n", 532 | "3\n", 533 | "101\n", 534 | "101\n", 535 | "100\n" 536 | ] 537 | } 538 | ], 539 | "source": [ 540 | "# 0 1\n", 541 | "# 1 2\n", 542 | "# 2 3\n", 543 | "# 3 4\n", 544 | "print(marks)\n", 545 | "\n", 546 | "for i in range(0, 4):\n", 547 | " print(i, marks[i])\n", 548 | "print(\"-\"*20)\n", 549 | "\n", 550 | "\n", 551 | "for mark in marks:\n", 552 | " print(mark)" 553 | ] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": 51, 558 | "metadata": {}, 559 | "outputs": [ 560 | { 561 | "name": "stdout", 562 | "output_type": "stream", 563 | "text": [ 564 | "0 100\n", 565 | "1 34\n", 566 | "2 45\n", 567 | "3 30\n", 568 | "4 33\n", 569 | "5 1\n", 570 | "6 2\n", 571 | "7 3\n", 572 | "8 101\n", 573 | "9 101\n", 574 | "10 100\n" 575 | ] 576 | } 577 | ], 578 | "source": [ 579 | "for i, mark in enumerate(marks):\n", 580 | " print(i, mark)" 581 | ] 582 | }, 583 | { 584 | "cell_type": "code", 585 | "execution_count": 54, 586 | "metadata": {}, 587 | "outputs": [ 588 | { 589 | "name": "stdout", 590 | "output_type": "stream", 591 | "text": [ 592 | "1\n", 593 | "25\n", 594 | "49\n", 595 | "81\n", 596 | "81\n", 597 | "[]\n", 598 | "[1, 25, 49, 81, 81]\n" 599 | ] 600 | } 601 | ], 602 | "source": [ 603 | "nums = [1, 5, 7, 9, 9]\n", 604 | "sq = []\n", 605 | "for i in nums:\n", 606 | " print(i**2)\n", 607 | "print(sq)\n", 608 | "\n", 609 | "for i in nums:\n", 610 | " sq.append(i**2)\n", 611 | "print(sq)" 612 | ] 613 | }, 614 | { 615 | "cell_type": "code", 616 | "execution_count": null, 617 | "metadata": {}, 618 | "outputs": [], 619 | "source": [] 620 | } 621 | ], 622 | "metadata": { 623 | "kernelspec": { 624 | "display_name": "Python 3", 625 | "language": "python", 626 | "name": "python3" 627 | }, 628 | "language_info": { 629 | "codemirror_mode": { 630 | "name": "ipython", 631 | "version": 3 632 | }, 633 | "file_extension": ".py", 634 | "mimetype": "text/x-python", 635 | "name": "python", 636 | "nbconvert_exporter": "python", 637 | "pygments_lexer": "ipython3", 638 | "version": "3.7.2" 639 | } 640 | }, 641 | "nbformat": 4, 642 | "nbformat_minor": 2 643 | } 644 | -------------------------------------------------------------------------------- /10-tuples-dictionary-sets.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 28, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Lists \n", 10 | "#Data Structure - Ways to store your data (multiple Python objects) in a structured way\n", 11 | "# Lists - An ordered collection of Python objects, [], int, float, str, bool\n", 12 | "# Lists are mutable - id(dummy_list) remains same\n", 13 | "# This is different from the fundamental Python objects, int, float, str, bool\n", 14 | "# Lists is also a Python object why? because everything in Python is an object\n", 15 | "# Lists can contains lists themselves as a valid element" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "### Tuples" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 3, 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "name": "stdout", 32 | "output_type": "stream", 33 | "text": [ 34 | "()\n", 35 | "(1, 2, 3)\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "# We denote a tuple using ()\n", 41 | "tup0 = ()\n", 42 | "print(tup0)\n", 43 | "tup1 = (1, 2, 3)\n", 44 | "print(tup1)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 11, 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "name": "stdout", 54 | "output_type": "stream", 55 | "text": [ 56 | "2\n", 57 | "[1, 4, 3]\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "# Indexing in tuple, indexing is always done using [] brackets\n", 63 | "print(tup1[1])\n", 64 | "\n", 65 | "# Try updating an element using index\n", 66 | "list1 = [1, 2, 3]\n", 67 | "list1[1] = 4\n", 68 | "print(list1)\n", 69 | "\n", 70 | "tup1 = (1, 2, 3)\n", 71 | "#tup1[1] = 4\n", 72 | "#print(tup1)\n", 73 | "# 'tuple' object does not support item assignment - similar to strings\n", 74 | "# Strings are immutable\n", 75 | "# Tuple is a immutable version of a list\n" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 12, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "1\n", 88 | "2\n", 89 | "3\n" 90 | ] 91 | } 92 | ], 93 | "source": [ 94 | "# for loop on a tuple\n", 95 | "for element in (1, 2, 3):\n", 96 | " print(element)\n", 97 | "# tuples are iterable" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 14, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "True\n", 110 | "True\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "print((1, 2) == (1, 2)) # same for lists as well\n", 116 | "print((1, 2) is (1, 2)) # not same as that of lists, answer for tuple will be true" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 19, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "1 2\n", 129 | "(1, 2)\n", 130 | "(3, 4)\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "# Packing and unpacking\n", 136 | "x, y = 1, 2 # packing and unpacking\n", 137 | "print(x, y)\n", 138 | "\n", 139 | "z = 1, 2 # packing\n", 140 | "print(z)\n", 141 | "\n", 142 | "def foo():\n", 143 | " return 3, 4\n", 144 | "\n", 145 | "a = foo()\n", 146 | "print(a)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 23, 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "name": "stdout", 156 | "output_type": "stream", 157 | "text": [ 158 | "1\n", 159 | "2\n", 160 | "3\n", 161 | "0 1\n", 162 | "1 2\n", 163 | "2 3\n", 164 | "(0, 1)\n", 165 | "(1, 2)\n", 166 | "(2, 3)\n" 167 | ] 168 | } 169 | ], 170 | "source": [ 171 | "x = [1, 2, 3]\n", 172 | "\n", 173 | "# enhanced for loop\n", 174 | "for element in x:\n", 175 | " print(element)\n", 176 | " \n", 177 | "for index, element in enumerate(x):\n", 178 | " print(index, element)\n", 179 | "\n", 180 | "for element in enumerate(x):\n", 181 | " print(element)" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 27, 187 | "metadata": {}, 188 | "outputs": [ 189 | { 190 | "name": "stdout", 191 | "output_type": "stream", 192 | "text": [ 193 | "(1, 2)\n", 194 | "[1, 2]\n" 195 | ] 196 | } 197 | ], 198 | "source": [ 199 | "# list to tuple or vice-versa is a breeze\n", 200 | "x = tuple([1, 2])\n", 201 | "print(x)\n", 202 | "y = list((1, 2))\n", 203 | "print(y)" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "### Dictionary " 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 30, 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "data": { 220 | "text/plain": [ 221 | "'CE0001'" 222 | ] 223 | }, 224 | "execution_count": 30, 225 | "metadata": {}, 226 | "output_type": "execute_result" 227 | } 228 | ], 229 | "source": [ 230 | "# 0, 1, 2, 3, 4....\n", 231 | "\n", 232 | "roll_numbers = ['CE0001', 'CE0002', 'CE0003', 'CE0004']\n", 233 | "# roll_numbers[4] is it practical for a real-world use case?\n", 234 | "\n", 235 | "# dictionary is denoated using curly brackets\n", 236 | "roll_number_dict = {'prashant': 'CE0001', 'anant': 'CE0002'}\n", 237 | "# an named index in a dictionary is called a key\n", 238 | "roll_number_dict[\"anant\"]\n", 239 | "# every key in dictionary needs to be unique" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 37, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "name": "stdout", 249 | "output_type": "stream", 250 | "text": [ 251 | "{'Anne': 12234, 'Bob': 3456, 'Cathy': 6789}\n", 252 | "{'Anne': 12234, 'Bob': 3456, 'Cathy': 6789}\n" 253 | ] 254 | } 255 | ], 256 | "source": [ 257 | "phonebook = {\n", 258 | " 'Anne': 12234,\n", 259 | " 'Bob':3456,\n", 260 | " 'Cathy':6789\n", 261 | "}\n", 262 | "print(phonebook)\n", 263 | "\n", 264 | "phonebook_1 = dict([['Anne', 12234], ['Bob', 3456], ['Cathy', 6789]])\n", 265 | "print(phonebook_1)" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 41, 271 | "metadata": {}, 272 | "outputs": [ 273 | { 274 | "name": "stdout", 275 | "output_type": "stream", 276 | "text": [ 277 | "\n", 278 | "\n", 279 | "\n", 280 | "[1, 2, 3]\n" 281 | ] 282 | } 283 | ], 284 | "source": [ 285 | "print(type([1, 2, 3]))\n", 286 | "print(type((1, 2, 3)))\n", 287 | "print(type(phonebook_1))\n", 288 | "\n", 289 | "s = [1, 2, 3,]\n", 290 | "print(s)" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": 45, 296 | "metadata": {}, 297 | "outputs": [ 298 | { 299 | "name": "stdout", 300 | "output_type": "stream", 301 | "text": [ 302 | "{'name': 'Shubham', 'age': 20, 'address': {'city': 'Delhi', 'zip': 110001, 'house-no': 29}, 'friends': [{'name': 'Anant', 'age': 21, 'address': {'city': 'Noida', 'zip': 201301, 'house-no': 28}, 'friends': []}, {'name': 'Shivam', 'age': 20, 'address': {'city': 'Hyderabad', 'zip': 110002, 'house-no': 39}, 'friends': [{'name': 'Anant', 'age': 21, 'address': {'city': 'Noida', 'zip': 201301, 'house-no': 28}, 'friends': []}]}]}\n" 303 | ] 304 | } 305 | ], 306 | "source": [ 307 | "# Complex dictionary struture\n", 308 | "\n", 309 | "person2 = {\n", 310 | " 'name': \"Anant\",\n", 311 | " 'age' : 21,\n", 312 | " 'address': {'city':'Noida', 'zip':201301, 'house-no': 28},\n", 313 | " 'friends' : []\n", 314 | "}\n", 315 | "\n", 316 | "person3 = {\n", 317 | " 'name': \"Shivam\",\n", 318 | " 'age' : 20,\n", 319 | " 'address': {'city':'Hyderabad', 'zip':110002, 'house-no': 39},\n", 320 | " 'friends' : [person2]\n", 321 | "}\n", 322 | "\n", 323 | "person1 = {\n", 324 | " 'name': \"Shubham\",\n", 325 | " 'age' : 20,\n", 326 | " 'address': {'city':'Delhi', 'zip':110001, 'house-no': 29},\n", 327 | " 'friends' : [person2, person3]\n", 328 | "}\n", 329 | "\n", 330 | "print(person1)" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": 55, 336 | "metadata": {}, 337 | "outputs": [ 338 | { 339 | "name": "stdout", 340 | "output_type": "stream", 341 | "text": [ 342 | "dict_keys(['name', 'age', 'address', 'friends'])\n", 343 | "None\n" 344 | ] 345 | } 346 | ], 347 | "source": [ 348 | "#print(person1.keys())\n", 349 | "# print(person1.values())\n", 350 | "# print(person1.get(\"name\"))\n", 351 | "# print(person1[\"name\"])\n", 352 | "\n", 353 | "print(person1.get(\"roll-number\")) # safer way to access value using key\n", 354 | "# print(person1[\"roll-number\"])" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 66, 360 | "metadata": {}, 361 | "outputs": [ 362 | { 363 | "name": "stdout", 364 | "output_type": "stream", 365 | "text": [ 366 | "name Shubham\n", 367 | "age 20\n", 368 | "address {'city': 'Delhi', 'zip': 110001, 'house-no': 29}\n", 369 | "friends [{'name': 'Anant', 'age': 21, 'address': {'city': 'Noida', 'zip': 201301, 'house-no': 28}, 'friends': []}, {'name': 'Shivam', 'age': 20, 'address': {'city': 'Hyderabad', 'zip': 110002, 'house-no': 39}, 'friends': [{'name': 'Anant', 'age': 21, 'address': {'city': 'Noida', 'zip': 201301, 'house-no': 28}, 'friends': []}]}]\n" 370 | ] 371 | } 372 | ], 373 | "source": [ 374 | "# for key in person1.keys():\n", 375 | "# print(key)\n", 376 | "\n", 377 | "# for key in person1.keys():\n", 378 | "# print(key, \":\", person1.get(key))\n", 379 | " \n", 380 | "# for value in person1.values():\n", 381 | "# print(value)\n", 382 | "\n", 383 | "for key, value in person1.items():\n", 384 | " print(key, value)" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 68, 390 | "metadata": {}, 391 | "outputs": [ 392 | { 393 | "data": { 394 | "text/plain": [ 395 | "'Delhi'" 396 | ] 397 | }, 398 | "execution_count": 68, 399 | "metadata": {}, 400 | "output_type": "execute_result" 401 | } 402 | ], 403 | "source": [ 404 | "person1.get('address').get('city')" 405 | ] 406 | }, 407 | { 408 | "cell_type": "code", 409 | "execution_count": 70, 410 | "metadata": {}, 411 | "outputs": [ 412 | { 413 | "data": { 414 | "text/plain": [ 415 | "'Noida'" 416 | ] 417 | }, 418 | "execution_count": 70, 419 | "metadata": {}, 420 | "output_type": "execute_result" 421 | } 422 | ], 423 | "source": [ 424 | "person1.get('friends')[0].get(\"address\").get(\"city\")" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": 73, 430 | "metadata": {}, 431 | "outputs": [ 432 | { 433 | "name": "stdout", 434 | "output_type": "stream", 435 | "text": [ 436 | "Noida\n" 437 | ] 438 | } 439 | ], 440 | "source": [ 441 | "for friend in person1.get('friends'):\n", 442 | " if friend.get(\"name\") == \"Anant\":\n", 443 | " print(friend.get(\"address\").get(\"city\"))" 444 | ] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "execution_count": 87, 449 | "metadata": {}, 450 | "outputs": [ 451 | { 452 | "name": "stdout", 453 | "output_type": "stream", 454 | "text": [ 455 | "125\n" 456 | ] 457 | } 458 | ], 459 | "source": [ 460 | "# List Comprehension\n", 461 | "natural_number = list(range(0, 11))\n", 462 | "\n", 463 | "# square_natual_number = []\n", 464 | "# for element in natural_number:\n", 465 | "# square_natual_number.append(element*element)\n", 466 | "# print(square_natual_number)\n", 467 | "\n", 468 | "# Dictionary Comprehension\n", 469 | "square_natual_number = dict([(element, element**3) for element in natural_number])\n", 470 | "#print(square_natual_number)\n", 471 | "print(square_natual_number[5])" 472 | ] 473 | }, 474 | { 475 | "cell_type": "markdown", 476 | "metadata": {}, 477 | "source": [ 478 | "### Sets" 479 | ] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": 90, 484 | "metadata": {}, 485 | "outputs": [ 486 | { 487 | "name": "stdout", 488 | "output_type": "stream", 489 | "text": [ 490 | "4\n" 491 | ] 492 | } 493 | ], 494 | "source": [ 495 | "# [], (), {}\n", 496 | "# Sets are also denoted using {}\n", 497 | "x = {1, 2, 3, 4, 5} # set\n", 498 | "x = {1:1, 2:4, 3:9} # dictionary\n", 499 | "x = [1, 2, 1, 3, 4, 4, 2]\n", 500 | "print(len(set(x)))" 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "execution_count": 101, 506 | "metadata": {}, 507 | "outputs": [ 508 | { 509 | "name": "stdout", 510 | "output_type": "stream", 511 | "text": [ 512 | "{1, 2, 3, 4} {2, 4}\n" 513 | ] 514 | } 515 | ], 516 | "source": [ 517 | "s1 = set([2, 4])\n", 518 | "s2 = set([2, 4, 5, 6])\n", 519 | "print(s1, s2)" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 107, 525 | "metadata": {}, 526 | "outputs": [ 527 | { 528 | "data": { 529 | "text/plain": [ 530 | "True" 531 | ] 532 | }, 533 | "execution_count": 107, 534 | "metadata": {}, 535 | "output_type": "execute_result" 536 | } 537 | ], 538 | "source": [ 539 | "s1.union(s2) # OR\n", 540 | "s1.intersection(s2) # AND\n", 541 | "s1.difference(s2) # s1 - s2\n", 542 | "s2.difference(s1) # s2 - s1\n", 543 | "s1.symmetric_difference(s2) # XOR - exclusilvely or\n", 544 | "s2.issubset(s1)\n", 545 | "s1.issuperset(s2)" 546 | ] 547 | } 548 | ], 549 | "metadata": { 550 | "kernelspec": { 551 | "display_name": "Python 3", 552 | "language": "python", 553 | "name": "python3" 554 | }, 555 | "language_info": { 556 | "codemirror_mode": { 557 | "name": "ipython", 558 | "version": 3 559 | }, 560 | "file_extension": ".py", 561 | "mimetype": "text/x-python", 562 | "name": "python", 563 | "nbconvert_exporter": "python", 564 | "pygments_lexer": "ipython3", 565 | "version": "3.7.2" 566 | } 567 | }, 568 | "nbformat": 4, 569 | "nbformat_minor": 2 570 | } 571 | -------------------------------------------------------------------------------- /Web-Development-Notes/Web Development.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Web Development

Download the sublime text editor and chrome extension called JSONView. Also, learn how to show hidden files on Windows and Mac, search it over internet. Now, we will open the whole project in sublime.

Let's create a new file in Sublime. Let's call it "app.py". Remember to save the files manually.

print("Hello")

Type "ls" or "dir" to check the content of folder.

python app.py



Flask Basics (Chatting App)

from flask import Flask, jsonify, request
app = Flask(__name__)

If this file is getting directly run from the terminal, by default  __name__ will be set to __main__ . Otherwise, if we are importing this file in some other Python script, then it will different. We will get to see it's importance later.

The code that we are writing  on the app.py script will be the back-end code.




Although, we are writing this code locally, we deploy it on the server later. We will now have to tell the server what all it needs to do when it receives requests from the client.

Function Decorator

@app.route('/')
def hello():
    return "Hello"

Inside the route(), we mention one of the end-points. Let's say that the server is hosted at domain, "www.example.com". We need to say "/" to go the home page.

@app.route('/')
def hello():
    return "Hello"

if __name__ == "__main__":
    app.run() # runs the server

This time, when run this file from terminal, you will notice that it shows some link which says that this app is running at some local host domain.

Let's try changing at the end-point. But you will have to re-start the server again.

@app.route('/hello')
def hello():
    return "Hello"

if __name__ == "__main__":
    app.run() # runs the server

You will get an error message because you don't have an end-point corresponding to the home-page.

Creating our own API

@app.route('/hello')
def hello():
    return "Hello"

users = [
{'id':2, 'name':'Anne', 'age':20}, {'id':1, 'name':'Cathy', 'age':21}]

@app.route('/users/')
def getUsers():
    return jsonify(users) # because you can only send a string

if __name__ == "__main__":
    app.run() # runs the server



Creating dynamic routes, search by ID

@app.route('/hello')
def hello():
    return "Hello"

users = [
{'id':2, 'name':'Anne', 'age':20}, {'id':1, 'name':'Cathy', 'age':21}]

@app.route('/users/<id>')
def getUser(id):
    result = [u for u in users if u['id']==id]
    result = list(filter(lambda u: u['id']==id, users))) # check the data type here, remember input()
    return jsonify(result)

if __name__ == "__main__":
    app.run() # runs the server



HTML endpoint

app.route('/hello')
def hello():
    return "Hello"

app = Flask(__name__, static_url_path="static")

users = [
{'id':2, 'name':'Anne', 'age':20}, {'id':1, 'name':'Cathy', 'age':21}]

@app.route('/users/<id>')
def getUser(id):
    result = [u for u in users if u['id']==id]
    result = list(filter(lambda u: u['id']==id, users))) # check the data type here, remember input()
    return jsonify(result)

@app.route('/')
def index():
    # TODO
    return app.send_static_file('index.html")

if __name__ == "__main__":
    app.run() # runs the server

Although, we could create a big HTML string in the function itself, but we will rather generate a static HTML file and read the content of it in a variable and then return it. For this, we will create a folder called "static" and store an HTML file in that folder. We will create file called index.html and we will try writing "html" to that press tab for sublime to help us in writing a basic HTML page.

<html>
<head>
    <title>Chat App</title>
</head>
<body>
    <h1>Coding Elements chatting App</h1>
</body>
</html>

Now, if we change the HTML content, we don't need to re-start the server.



Another end-point, search by some other variable

app.route('/hello')
def hello():
    return "Hello"

app = Flask(__name__, static_url_path="static")

users = [
{'id':2, 'name':'Anne', 'age':20}, {'id':1, 'name':'Cathy', 'age':21}]

@app.route('/users/<id>')
def getUser(id):
    result = [u for u in users if u['id']==id]
    result = list(filter(lambda u: u['id']==id, users))) # check the data type here, remember input()
    return jsonify(result)

@app.route('/users/<name>')
def getUser(name):
    result = [u for u in users if u['id']==id]
    result = list(filter(lambda u: u['id']==id, users))) # check the data type here, remember input()
    return jsonify(result)

@app.rounte('/')
def index():
    # TODO
    return app.send_static_file('index.html")

if __name__ == "__main__":
    app.run() # runs the server

If you try doing something like this, this won't work as it will be still mapped to the first function. One of doing this could be as follows.

@app.route('/users/name/<name>')
@app.route('/users/id/<id>')



Adding Javascript code

Create a file called main.js

<html>
<head>
    <title>Chat App</title>
</head>
<body>
    <h1>Coding Elements chatting App</h1>
    <script src="static/main.js"></script>
</body>
</html>

Now, we will read some Javascript code. It will be very easy to pick-up.

console.log("hello") # similar to print in Python, this shows the on the developer console

var x = 5
if (x>2)
{
    console.log('greater')
}
else
{
    console.log('smaller')
}

function area(l, b)
{
    a = l * b
    return a
}

a1 = area(4, 5)

for (var i=1; i,=5;i+=1)
{
    console.log(i)
}

We may have to do a hard reload to see the new changes appearing.


JS Fetch (Ajax)
Now, we want Javascript to create fetching like calls.

function fetchUsers()
{
    fetch('/users').then(function(response){
        response.json().then(function(data)
    {
        console.log(data}
    })
}

<html>
<head>
    <title>Chat App</title>
</head>
<body>
    <h1>Coding Elements chatting App</h1>
    <script src="static/main.js"></script>
    <button onClick="fetchUsers()">Get Users</button>
</body>
</html>

Notice that the front-end code is written in HTML and Javascript and the back-end code is written in Python. The output of whatever you do in the Python file will be shown on the terminal and output of whatever you do in JS file will be shown in the developer console. Although, the HTML and JS files are stored on the server, they run on the client-side.


Web Sockets



B in this case will have to keep sending the request to the server to check if new message has come which is quite inefficient both for B and server. Long Polling is final method that we will use in which server will hold the Bs request and close this request when A sends a message for B. But we will talk about Web Sockets for now. In this case, the server won't have to hold B's request. Instead, there will be a persistent connection between the server and the client and both sends the message to each other whenever required. It's a two way persistent connection. 


SocketIO Setup

SocketIO is one the most popular package for making sockets but since we are using Flask, we will use the Flask version of SocketIO API. Google search for Google-SocketIO flask.

!pip3 install flask-socketio #first activate the virtual environment
!pip3 freeze # check socket-io dependencies

Mention about the "eventlet" package. We won’t install it now.



Adding socket IO in the backend

from flask import Flask, jsonify, request
from flask_socketio import SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = ’secret' # completely optional

socketio = SocketIO(app)

@app.route('/hello')
def hello():
    return "Hello"

users = [
{'id':2, 'name':'Anne', 'age':20}, {'id':1, 'name':'Cathy', 'age':21}]

@app.route('/users/')
def getUsers():
    return jsonify(users) # because you can only send a string

if __name__ == "__main__":
    socketio.run(app) # runs the server


Now, we also have to the add the Socket IO support in the front-end.

Copy the first script and come back to index.html and paste it inside the head tag

<html>
<head>
    <title>Chat App</title>
    <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="sha256-yr4fRk/GU1ehYJPAs8P4JlTgu0Hdsp4ZKrx8bDEDC3I=" crossorigin="anonymous"></script>
</head>

We can at the protocal here - https:

Copy this link and try pasting it in the browser. This is the Javascript code for socketio.

Copy and add the second script tag to index.html, but we can just copy the javascript part inside the script tag. Because we have a separate file called main.js for that. We can paste this in main.js.


The documentation has full code for how to send and receive messages using socketio but I will run just write the code necessary for chatting app.

SocketIO client

Lets try understanding whats written in the javascript code.


We won’t use any of it. We will add one button to the front-end.

<button onClick="send()">Send()</button>

We will add the code for send() in main.js.

function send()
{
    # have to pass two values, key and value
    socket.emit("msg", "Hello")
}

Now, we need to tell the server, how to receive the message.

# this is similar to onconnect or onclick, all of these are called listener
@socketio.on("msg")
def handle_msg(data):
    print(data)



Socket-io Broadcasting

Now we want the server to broadcast the message to other client which is sent by A.

@socketio.on("msg")
def handle_msg(data):
    print(data)
    socketio.emit("push", data, broadcast=True, include_self=False)

Now clients should have a way to receive the message as well. So we add the code for that in main.js. We will use socket.on(), “on” gives you a hint that it is a listener.

socket.on(‘push’, function(data){
    console.log(data)
})



Input Box

Lets create an input box, the id that we use in index.html should match with one in Javscript.

<input id="msgBox"></input>

Lets add support for reading the message from input box in main.js

function send(){
    var msgInBox = document.getElementById('msgBox')
    socket.emit('msg', msgInBox.value)
    console.log("Server should print the message”)
}

Now, we would want the input box to get empty as soon as the “send” button is clicked.



Now, we would want to display the message on the web page instead of developer console.

<div id="msgList"></div>

msgList.innerHTML += "<p>" + data + "</p>”



Preparation for deployment on Heroku

We need to tell the server which type of app it is, so we will create a file under ChatApp folder which is called runtime.txt

python-3.6.6

Now, we need to tell te server what other package we need to add. We will create another file under ChatApp called requirements .txt. We can do that using simple command called pip freeze.

Flask
Flask-SocketIO
gunicorn
eventlet

Eventlet is a worker used by gunicorn web server.



Now we need to tell the server how to start the app. We have to create file called Procfile without any extension. You can findin the flask socketio documentation.

web: gunicorn --worker-class eventlet -w 1 module:app



Search for getting started with deploying python app on heroku. You can create an account on Heroku and start with Python app.




Git Theory






 Install heroku, check its version and use “heroku login”. It will take you to the login page.










-------------------------------------------------------------------------------- /11-classes-and-objects.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "11-classes-and-objects.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | } 14 | }, 15 | "cells": [ 16 | { 17 | "cell_type": "code", 18 | "metadata": { 19 | "id": "jjTAvny9veRi", 20 | "colab_type": "code", 21 | "colab": {} 22 | }, 23 | "source": [ 24 | "# # Object Oriented Programming\n", 25 | "\n", 26 | "# student\n", 27 | "# {\n", 28 | "# name -- attribute \n", 29 | "# roll number -- attribute\n", 30 | "# learns_coding -- function / behavior / \"method\"\n", 31 | "# }" 32 | ], 33 | "execution_count": 0, 34 | "outputs": [] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "metadata": { 39 | "id": "6rHTf57C5AJt", 40 | "colab_type": "code", 41 | "outputId": "f4a635bc-c242-479e-8e97-d504e003374a", 42 | "colab": { 43 | "base_uri": "https://localhost:8080/", 44 | "height": 67 45 | } 46 | }, 47 | "source": [ 48 | "class Student:\n", 49 | " pass # empty class\n", 50 | "\n", 51 | "s1 = Student() # object of class Student referenced with name s1\n", 52 | "s2 = Student()\n", 53 | "\n", 54 | "print(Student)\n", 55 | "print(s1)\n", 56 | "print(s2)" 57 | ], 58 | "execution_count": 0, 59 | "outputs": [ 60 | { 61 | "output_type": "stream", 62 | "text": [ 63 | "\n", 64 | "<__main__.Student object at 0x7fde44a034e0>\n", 65 | "<__main__.Student object at 0x7fde44a034a8>\n" 66 | ], 67 | "name": "stdout" 68 | } 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": { 74 | "id": "lHAMM3zw4mXl", 75 | "colab_type": "text" 76 | }, 77 | "source": [ 78 | "" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "metadata": { 84 | "id": "lFYabdY56e9A", 85 | "colab_type": "code", 86 | "colab": {} 87 | }, 88 | "source": [ 89 | "s1.name = \"Anant\"\n", 90 | "s1.roll_number = 12\n", 91 | "s2.name = \"Billy\"" 92 | ], 93 | "execution_count": 0, 94 | "outputs": [] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "metadata": { 99 | "id": "KPe0T7hT6gCL", 100 | "colab_type": "code", 101 | "outputId": "77263664-e1a4-4eb4-f425-fe6db0eb77ed", 102 | "colab": { 103 | "base_uri": "https://localhost:8080/", 104 | "height": 34 105 | } 106 | }, 107 | "source": [ 108 | "print(s1.name)" 109 | ], 110 | "execution_count": 0, 111 | "outputs": [ 112 | { 113 | "output_type": "stream", 114 | "text": [ 115 | "Anant\n" 116 | ], 117 | "name": "stdout" 118 | } 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "metadata": { 124 | "id": "fhPYcBFq6gHp", 125 | "colab_type": "code", 126 | "outputId": "0bc60663-a076-42bd-d62b-51627770386b", 127 | "colab": { 128 | "base_uri": "https://localhost:8080/", 129 | "height": 34 130 | } 131 | }, 132 | "source": [ 133 | "print(s1.roll_number)" 134 | ], 135 | "execution_count": 0, 136 | "outputs": [ 137 | { 138 | "output_type": "stream", 139 | "text": [ 140 | "12\n" 141 | ], 142 | "name": "stdout" 143 | } 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "metadata": { 149 | "id": "k4zT1QHK6gGF", 150 | "colab_type": "code", 151 | "outputId": "893fec12-ae47-4783-b106-b4aa78647c12", 152 | "colab": { 153 | "base_uri": "https://localhost:8080/", 154 | "height": 34 155 | } 156 | }, 157 | "source": [ 158 | "print(s2.name)" 159 | ], 160 | "execution_count": 0, 161 | "outputs": [ 162 | { 163 | "output_type": "stream", 164 | "text": [ 165 | "Billy\n" 166 | ], 167 | "name": "stdout" 168 | } 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "metadata": { 174 | "id": "Hzc6nOxX61i8", 175 | "colab_type": "code", 176 | "outputId": "5ee6464a-4ca9-4ee1-e018-e811674ede57", 177 | "colab": { 178 | "base_uri": "https://localhost:8080/", 179 | "height": 162 180 | } 181 | }, 182 | "source": [ 183 | "print(s2.roll_number)" 184 | ], 185 | "execution_count": 0, 186 | "outputs": [ 187 | { 188 | "output_type": "error", 189 | "ename": "AttributeError", 190 | "evalue": "ignored", 191 | "traceback": [ 192 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 193 | "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", 194 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mroll_number\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 195 | "\u001b[0;31mAttributeError\u001b[0m: 'Student' object has no attribute 'roll_number'" 196 | ] 197 | } 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "metadata": { 203 | "id": "5dQZjPfO63gu", 204 | "colab_type": "code", 205 | "outputId": "73382d05-c238-4d58-f11d-57131065b531", 206 | "colab": { 207 | "base_uri": "https://localhost:8080/", 208 | "height": 162 209 | } 210 | }, 211 | "source": [ 212 | "Student.name" 213 | ], 214 | "execution_count": 0, 215 | "outputs": [ 216 | { 217 | "output_type": "error", 218 | "ename": "AttributeError", 219 | "evalue": "ignored", 220 | "traceback": [ 221 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 222 | "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", 223 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mStudent\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 224 | "\u001b[0;31mAttributeError\u001b[0m: type object 'Student' has no attribute 'name'" 225 | ] 226 | } 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "metadata": { 232 | "id": "FWHZjY927NsH", 233 | "colab_type": "code", 234 | "outputId": "cae1ecff-e935-4502-d5ad-6093f4230cc9", 235 | "colab": { 236 | "base_uri": "https://localhost:8080/", 237 | "height": 50 238 | } 239 | }, 240 | "source": [ 241 | "class Student:\n", 242 | " roll_number = None # class attribute\n", 243 | " name = None # class attribute\n", 244 | "\n", 245 | "s1 = Student()\n", 246 | "s2 = Student()\n", 247 | "s1.name = \"Anant\" # object attribute\n", 248 | "s1.roll_number = 12\n", 249 | "\n", 250 | "print(s1.name, s2.name)\n", 251 | "print(s1.roll_number, s2.roll_number)" 252 | ], 253 | "execution_count": 0, 254 | "outputs": [ 255 | { 256 | "output_type": "stream", 257 | "text": [ 258 | "Anant None\n", 259 | "12 None\n" 260 | ], 261 | "name": "stdout" 262 | } 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "metadata": { 268 | "id": "T7lt8hyl7oeJ", 269 | "colab_type": "code", 270 | "outputId": "3d1958f7-ee4a-4f7f-d3fc-dd83df6c9d06", 271 | "colab": { 272 | "base_uri": "https://localhost:8080/", 273 | "height": 34 274 | } 275 | }, 276 | "source": [ 277 | "print(Student.name)" 278 | ], 279 | "execution_count": 0, 280 | "outputs": [ 281 | { 282 | "output_type": "stream", 283 | "text": [ 284 | "None\n" 285 | ], 286 | "name": "stdout" 287 | } 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "metadata": { 293 | "id": "l9_JC8Nk7-uI", 294 | "colab_type": "code", 295 | "outputId": "f28d71c3-e9d4-4e65-97d6-e5c476bcea84", 296 | "colab": { 297 | "base_uri": "https://localhost:8080/", 298 | "height": 84 299 | } 300 | }, 301 | "source": [ 302 | "class Student:\n", 303 | " # dunder init method, __init__ method is called during object instantiation, similar to constructor\n", 304 | " # overriding the method - defining it again\n", 305 | " def __init__(self, name, roll_number):\n", 306 | " #print(self)\n", 307 | " # variable self contains reference to the object, the name can be anything besides self as well\n", 308 | " self.name = name\n", 309 | " self.roll_number = roll_number\n", 310 | " print(f\"Name of the object is {self.name}\")\n", 311 | " print(f\"Roll NUmber of the object is {self.roll_number}\")\n", 312 | "\n", 313 | "s1 = Student(\"Anant\", 12)\n", 314 | "s2 = Student(\"Pranshu\", 15)" 315 | ], 316 | "execution_count": 0, 317 | "outputs": [ 318 | { 319 | "output_type": "stream", 320 | "text": [ 321 | "Name of the object is Anant\n", 322 | "Roll NUmber of the object is 12\n", 323 | "Name of the object is Pranshu\n", 324 | "Roll NUmber of the object is 15\n" 325 | ], 326 | "name": "stdout" 327 | } 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "metadata": { 333 | "id": "dwG4iZ4w9qgn", 334 | "colab_type": "code", 335 | "colab": {} 336 | }, 337 | "source": [ 338 | "s2.works_for = \"Google\"" 339 | ], 340 | "execution_count": 0, 341 | "outputs": [] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "metadata": { 346 | "id": "Ew_rgYIq_jFD", 347 | "colab_type": "code", 348 | "outputId": "00db2e96-8ea1-488e-fd54-04199c415472", 349 | "colab": { 350 | "base_uri": "https://localhost:8080/", 351 | "height": 162 352 | } 353 | }, 354 | "source": [ 355 | "s1.studies" 356 | ], 357 | "execution_count": 0, 358 | "outputs": [ 359 | { 360 | "output_type": "error", 361 | "ename": "AttributeError", 362 | "evalue": "ignored", 363 | "traceback": [ 364 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 365 | "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", 366 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ms1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstudies\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 367 | "\u001b[0;31mAttributeError\u001b[0m: 'Student' object has no attribute 'studies'" 368 | ] 369 | } 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "metadata": { 375 | "id": "qh6x6cvR_lKp", 376 | "colab_type": "code", 377 | "outputId": "488306b0-6d13-43e3-9cec-f70af39d26f5", 378 | "colab": { 379 | "base_uri": "https://localhost:8080/", 380 | "height": 34 381 | } 382 | }, 383 | "source": [ 384 | "s2.works_for" 385 | ], 386 | "execution_count": 0, 387 | "outputs": [ 388 | { 389 | "output_type": "execute_result", 390 | "data": { 391 | "text/plain": [ 392 | "'Google'" 393 | ] 394 | }, 395 | "metadata": { 396 | "tags": [] 397 | }, 398 | "execution_count": 33 399 | } 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "metadata": { 405 | "id": "mqpwSR93_mdj", 406 | "colab_type": "code", 407 | "outputId": "2dcffcc8-82dc-41c3-ad05-27bf934b2a42", 408 | "colab": { 409 | "base_uri": "https://localhost:8080/", 410 | "height": 84 411 | } 412 | }, 413 | "source": [ 414 | "print(s1.name, s1.roll_number)\n", 415 | "print(s2.name, s2.roll_number, s2.works_for)\n", 416 | "print(s1)\n", 417 | "print(s2)" 418 | ], 419 | "execution_count": 0, 420 | "outputs": [ 421 | { 422 | "output_type": "stream", 423 | "text": [ 424 | "Anant 12\n", 425 | "Pranshu 15 Google\n", 426 | "<__main__.Student object at 0x7fde4497a5c0>\n", 427 | "<__main__.Student object at 0x7fde4497a550>\n" 428 | ], 429 | "name": "stdout" 430 | } 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "metadata": { 436 | "id": "uqhfWcQYADCm", 437 | "colab_type": "code", 438 | "colab": {} 439 | }, 440 | "source": [ 441 | "# __init__ is called dunder init\n", 442 | "# __del__ is called dunder del, and is a destructor and gets called when object has to be deleted\n", 443 | "# __str__ is called dunder std, and short string, by default it returs self" 444 | ], 445 | "execution_count": 0, 446 | "outputs": [] 447 | }, 448 | { 449 | "cell_type": "code", 450 | "metadata": { 451 | "id": "ZNnARYu7CD4P", 452 | "colab_type": "code", 453 | "colab": {} 454 | }, 455 | "source": [ 456 | "" 457 | ], 458 | "execution_count": 0, 459 | "outputs": [] 460 | }, 461 | { 462 | "cell_type": "code", 463 | "metadata": { 464 | "id": "q8yNd1_QBCgG", 465 | "colab_type": "code", 466 | "outputId": "d948dc74-2420-4b49-b732-67bb27eb6d7f", 467 | "colab": { 468 | "base_uri": "https://localhost:8080/", 469 | "height": 84 470 | } 471 | }, 472 | "source": [ 473 | "class Student:\n", 474 | " # dunder init method, __init__ method is called during object instantiation, similar to constructor\n", 475 | " # overriding the method - defining it again\n", 476 | " def __init__(self, name, roll_number):\n", 477 | " #print(self)\n", 478 | " # variable self contains reference to the object, the name can be anything besides self as well\n", 479 | " self.name = name\n", 480 | " self.roll_number = roll_number\n", 481 | " print(f\"Name of the object is {self.name}\")\n", 482 | " print(f\"Roll NUmber of the object is {self.roll_number}\")\n", 483 | " \n", 484 | " def __del__(self):\n", 485 | " print(\"Del destuctor is called\")\n", 486 | "\n", 487 | " def __str__(self):\n", 488 | " return f\"Name: {self.name} Roll Number:{self.roll_number}\"\n", 489 | "\n", 490 | "s3 = Student(\"Moscow\", 4)\n", 491 | "print(s3)" 492 | ], 493 | "execution_count": 0, 494 | "outputs": [ 495 | { 496 | "output_type": "stream", 497 | "text": [ 498 | "Name of the object is Moscow\n", 499 | "Roll NUmber of the object is 4\n", 500 | "Del destuctor is called\n", 501 | "Name: Moscow Roll Number:4\n" 502 | ], 503 | "name": "stdout" 504 | } 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "metadata": { 510 | "id": "twB2_n-6BaPt", 511 | "colab_type": "code", 512 | "outputId": "d0202cb7-7e70-4a3f-c662-579078272644", 513 | "colab": { 514 | "base_uri": "https://localhost:8080/", 515 | "height": 34 516 | } 517 | }, 518 | "source": [ 519 | "s3.__del__()" 520 | ], 521 | "execution_count": 0, 522 | "outputs": [ 523 | { 524 | "output_type": "stream", 525 | "text": [ 526 | "Del destuctor is called\n" 527 | ], 528 | "name": "stdout" 529 | } 530 | ] 531 | }, 532 | { 533 | "cell_type": "code", 534 | "metadata": { 535 | "id": "DBUGysfqBleR", 536 | "colab_type": "code", 537 | "colab": {} 538 | }, 539 | "source": [ 540 | "class Student1:\n", 541 | " counter = 0\n", 542 | " def __init__(self, name):\n", 543 | " self.name = name\n", 544 | " Student1.counter += 1\n", 545 | " self.roll_number = Student1.counter\n", 546 | " \n", 547 | " def __str__(self):\n", 548 | " return f\"Name: {self.name}, Roll Number: {self.roll_number}, Counter: {Student1.counter}\"" 549 | ], 550 | "execution_count": 0, 551 | "outputs": [] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "metadata": { 556 | "id": "3bEbvIARD1B5", 557 | "colab_type": "code", 558 | "colab": {} 559 | }, 560 | "source": [ 561 | "s1 = Student1(\"Anant\")\n", 562 | "s2 = Student1(\"Pranshu\")\n", 563 | "s3 = Student1(\"Anubhav\")" 564 | ], 565 | "execution_count": 0, 566 | "outputs": [] 567 | }, 568 | { 569 | "cell_type": "code", 570 | "metadata": { 571 | "id": "_PPdaFkUD_HU", 572 | "colab_type": "code", 573 | "outputId": "3a9964c8-dcd5-4832-c6b1-0161fba3d719", 574 | "colab": { 575 | "base_uri": "https://localhost:8080/", 576 | "height": 101 577 | } 578 | }, 579 | "source": [ 580 | "print(Student1.counter)\n", 581 | "print(s1.counter)\n", 582 | "print(s1)\n", 583 | "print(s2)\n", 584 | "print(s3)" 585 | ], 586 | "execution_count": 0, 587 | "outputs": [ 588 | { 589 | "output_type": "stream", 590 | "text": [ 591 | "3\n", 592 | "3\n", 593 | "Name: Anant, Roll Number: 1, Counter: 3\n", 594 | "Name: Pranshu, Roll Number: 2, Counter: 3\n", 595 | "Name: Anubhav, Roll Number: 3, Counter: 3\n" 596 | ], 597 | "name": "stdout" 598 | } 599 | ] 600 | }, 601 | { 602 | "cell_type": "code", 603 | "metadata": { 604 | "id": "oOs6_MVuESLK", 605 | "colab_type": "code", 606 | "outputId": "1da5f2da-d523-470a-9b2a-a2322d50f66c", 607 | "colab": { 608 | "base_uri": "https://localhost:8080/", 609 | "height": 128 610 | } 611 | }, 612 | "source": [ 613 | "class Account:\n", 614 | " counter = 0\n", 615 | " # binding atttributes and methods which execute on attributes is called \"Encapsulation\"\n", 616 | " def __init__(self, balance=0):\n", 617 | " Account.counter += 1\n", 618 | " self.account_number = Account.counter\n", 619 | " self.balance = balance\n", 620 | " \n", 621 | " def deposit(self, amount):\n", 622 | " if amount > 0:\n", 623 | " self.balance += amount\n", 624 | " else:\n", 625 | " return False\n", 626 | " return True\n", 627 | " \n", 628 | " def withdraw(self, amount):\n", 629 | " if amount <= self.balance:\n", 630 | " self.balance -= amount\n", 631 | " else:\n", 632 | " return False\n", 633 | " return True \n", 634 | "\n", 635 | "class Bank:\n", 636 | " # constructore or init method\n", 637 | " def __init__(self, name):\n", 638 | " self.name = name\n", 639 | " self.accounts = []\n", 640 | "\n", 641 | "citibank = Bank(\"Citi Bank\")\n", 642 | "a1 = Account(5000)\n", 643 | "citibank.accounts.append(a1)\n", 644 | "citibank.accounts.append(Account(10000))\n", 645 | "print(a1.balance, citibank.accounts[0].balance)\n", 646 | "print(citibank.accounts[1].balance)" 647 | ], 648 | "execution_count": 0, 649 | "outputs": [ 650 | { 651 | "output_type": "error", 652 | "ename": "SyntaxError", 653 | "evalue": "ignored", 654 | "traceback": [ 655 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m23\u001b[0m\n\u001b[0;31m def\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 656 | ] 657 | } 658 | ] 659 | }, 660 | { 661 | "cell_type": "code", 662 | "metadata": { 663 | "id": "QmNVVSS-JnGk", 664 | "colab_type": "code", 665 | "outputId": "69936d61-5917-4c73-c350-5c39e295424e", 666 | "colab": { 667 | "base_uri": "https://localhost:8080/", 668 | "height": 168 669 | } 670 | }, 671 | "source": [ 672 | "a1 = Account()\n", 673 | "print(a1.balance)\n", 674 | "print(a1.deposit(100))\n", 675 | "print(a1.balance)\n", 676 | "print(a1.deposit(-100))\n", 677 | "print(a1.balance)\n", 678 | "print(a1.withdraw(200))\n", 679 | "print(a1.withdraw(80))\n", 680 | "print(a1.balance)\n", 681 | "a1.balance = 1000000000\n", 682 | "print(a1.balance)" 683 | ], 684 | "execution_count": 0, 685 | "outputs": [ 686 | { 687 | "output_type": "stream", 688 | "text": [ 689 | "0\n", 690 | "True\n", 691 | "100\n", 692 | "False\n", 693 | "100\n", 694 | "False\n", 695 | "True\n", 696 | "20\n", 697 | "1000000000\n" 698 | ], 699 | "name": "stdout" 700 | } 701 | ] 702 | }, 703 | { 704 | "cell_type": "code", 705 | "metadata": { 706 | "id": "sbrJ5BCWJ4jw", 707 | "colab_type": "code", 708 | "colab": {} 709 | }, 710 | "source": [ 711 | "class Account:\n", 712 | " counter = 0\n", 713 | " # binding atttributes and methods which execute on attributes is called \"Encapsulation\"\n", 714 | " def __init__(self, balance=0):\n", 715 | " Account.counter += 1\n", 716 | " self.__account_number = Account.counter # Data Hiding using Mangling\n", 717 | " self.__balance = balance # data gets mangled, attributes get saved with a different name internally\n", 718 | " self.transaction = 0\n", 719 | " self.max_transaction = 10\n", 720 | "\n", 721 | " def get_max_transaction(self):\n", 722 | " return self.max_transaction\n", 723 | " \n", 724 | " def deposit(self, amount):\n", 725 | " if amount > 0 and self.transaction < self.max_transaction:\n", 726 | " self.__balance += amount\n", 727 | " self.transaction += 1\n", 728 | " else:\n", 729 | " return False\n", 730 | " return True\n", 731 | " \n", 732 | " def withdraw(self, amount):\n", 733 | " if amount <= self.__balance and self.transaction < self.max_transaction:\n", 734 | " self.__balance -= amount\n", 735 | " self.transaction += 1\n", 736 | " else:\n", 737 | " return False\n", 738 | " return True\n", 739 | " \n", 740 | " def get_balance(self):\n", 741 | " return self.__balance\n", 742 | " \n", 743 | " def get_account_number(self):\n", 744 | " return self.__account_number\n", 745 | "\n", 746 | "class Bank:\n", 747 | " # constructore or init method\n", 748 | " def __init__(self, name):\n", 749 | " self.name = name\n", 750 | " self.accounts = []\n", 751 | "\n", 752 | "citibank = Bank(\"Citi Bank\")\n", 753 | "a1 = Account(5000)\n", 754 | "# citibank.accounts.append(a1)\n", 755 | "# citibank.accounts.append(Account(10000))\n", 756 | "# print(a1.balance, citibank.accounts[0].balance)\n", 757 | "# print(citibank.accounts[1].balance)" 758 | ], 759 | "execution_count": 0, 760 | "outputs": [] 761 | }, 762 | { 763 | "cell_type": "code", 764 | "metadata": { 765 | "colab_type": "code", 766 | "outputId": "fd6a38a0-e870-4c0d-aa97-65dbce2181cd", 767 | "id": "GhbRGIJjM_Tf", 768 | "colab": { 769 | "base_uri": "https://localhost:8080/", 770 | "height": 168 771 | } 772 | }, 773 | "source": [ 774 | "a1 = Account()\n", 775 | "print(a1.get_balance())\n", 776 | "print(a1.deposit(100))\n", 777 | "print(a1.get_balance())\n", 778 | "print(a1.deposit(-100))\n", 779 | "print(a1.get_balance())\n", 780 | "print(a1.withdraw(200))\n", 781 | "print(a1.withdraw(80))\n", 782 | "#print(a1.__balance)\n", 783 | "a1.__balance = 1000000000\n", 784 | "print(a1.__balance)\n", 785 | "print(a1.get_balance())" 786 | ], 787 | "execution_count": 0, 788 | "outputs": [ 789 | { 790 | "output_type": "stream", 791 | "text": [ 792 | "0\n", 793 | "True\n", 794 | "100\n", 795 | "False\n", 796 | "100\n", 797 | "False\n", 798 | "True\n", 799 | "1000000000\n", 800 | "20\n" 801 | ], 802 | "name": "stdout" 803 | } 804 | ] 805 | }, 806 | { 807 | "cell_type": "code", 808 | "metadata": { 809 | "id": "sciSK8PONAFT", 810 | "colab_type": "code", 811 | "colab": {} 812 | }, 813 | "source": [ 814 | "# Inheritance\n", 815 | "\n", 816 | " # Parent Class (Account)\n", 817 | " # / \\\n", 818 | " # SavingsAccount CurrentAccount\n", 819 | " # (Child Classes)" 820 | ], 821 | "execution_count": 0, 822 | "outputs": [] 823 | }, 824 | { 825 | "cell_type": "code", 826 | "metadata": { 827 | "id": "9gogE52cSPSl", 828 | "colab_type": "code", 829 | "outputId": "7d8218af-e667-4d74-bf37-64bca39770d5", 830 | "colab": { 831 | "base_uri": "https://localhost:8080/", 832 | "height": 414 833 | } 834 | }, 835 | "source": [ 836 | "class SavingsAccount(Account):\n", 837 | " def __init__(self, balance):\n", 838 | " super().__init__(balance)\n", 839 | "\n", 840 | " def get_interest(self):\n", 841 | " interest = super().get_balance() * 0.03\n", 842 | " return interest\n", 843 | "\n", 844 | " def add_interest(self):\n", 845 | " interest = self.get_interest()\n", 846 | " if super().deposit(interest):\n", 847 | " return True\n", 848 | " else:\n", 849 | " return False\n", 850 | "\n", 851 | "class CurrentAccount(Account):\n", 852 | "\n", 853 | " @property\n", 854 | " def __init__(self, balance=0):\n", 855 | " super().__init__(balance) # super keyword is used to call the parent class\n", 856 | " self.max_transaction = super().max_transaction * 20\n", 857 | "\n", 858 | "sa1 = SavingsAccount(1000)\n", 859 | "print(sa1.get_balance())\n", 860 | "print(sa1.get_interest())\n", 861 | "print(sa1.add_interest())\n", 862 | "print(sa1.get_balance())\n", 863 | "\n", 864 | "ca = CurrentAccount(2000)\n", 865 | "print(ca.get_balance())\n", 866 | "print(ca.max_transaction)\n", 867 | "print(sa1.max_transaction)" 868 | ], 869 | "execution_count": 0, 870 | "outputs": [ 871 | { 872 | "output_type": "stream", 873 | "text": [ 874 | "1000\n", 875 | "30.0\n", 876 | "True\n", 877 | "1030.0\n" 878 | ], 879 | "name": "stdout" 880 | }, 881 | { 882 | "output_type": "error", 883 | "ename": "AttributeError", 884 | "evalue": "ignored", 885 | "traceback": [ 886 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 887 | "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", 888 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msa1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_balance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 28\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 29\u001b[0;31m \u001b[0mca\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mCurrentAccount\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 30\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mca\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_balance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 31\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mca\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmax_transaction\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 889 | "\u001b[0;32m\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, balance)\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbalance\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbalance\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# super keyword is used to call the parent class\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmax_transaction\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmax_transaction\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;36m20\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0msa1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSavingsAccount\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 890 | "\u001b[0;31mAttributeError\u001b[0m: 'super' object has no attribute 'max_transaction'" 891 | ] 892 | } 893 | ] 894 | }, 895 | { 896 | "cell_type": "code", 897 | "metadata": { 898 | "id": "TSA7Nil-SsEb", 899 | "colab_type": "code", 900 | "colab": {} 901 | }, 902 | "source": [ 903 | "" 904 | ], 905 | "execution_count": 0, 906 | "outputs": [] 907 | } 908 | ] 909 | } --------------------------------------------------------------------------------