├── Abstraction.py ├── Attributes_using_Functions.py ├── Encapsulation_Private.py ├── Encapsulation_Protected.py ├── Inheritance_Construct_in_Inheritance.py ├── Inheritance_Hirarcheial_Inheritance.py ├── Inheritance_MultiLevel_Inheritance.py ├── Inheritance_Multiple_Interitance.py ├── Inheritance_SingleInheritance.py ├── Inheritanec_Hybrid_Inheritance.py ├── Object_and_Class.py ├── Polymorphism_Duck_Typing.py ├── Polymorphism_Method_Overloading.py ├── Polymorphism_Method_Overriding.py ├── Polymorphism_Operator_Overloading.py ├── Search_Binary.py ├── Search_Linear.py ├── Sort_Bubble.py ├── Sort_Heap.py ├── Sort_Insertion.py ├── Sort_Merge.py ├── Sort_Quick.py ├── Sort_Selection.py ├── Types0fMethods_1_Instance_Method.py ├── TypesOfMethods_2_Class_methods.py └── TypesOfMethods_3_Static_Methods.py /Abstraction.py: -------------------------------------------------------------------------------- 1 | #Abstraction = Hiding the functionality(behaviour) 2 | #An Abstract class cannot be instantiated i.e., we cannot create an object for the abstract class 3 | #An Abstract class can contain normal methods and Abstract mehtods. 4 | #Concrete(normal) classes cannot have abstract methods 5 | from abc import ABC,abstractmethod 6 | 7 | class Factory(ABC): 8 | @abstractmethod 9 | def Goods(self):#abstract method 10 | pass 11 | 12 | def Trasportation(self):#normal method 13 | print("Trucks are used for Trasporting") 14 | 15 | class ChocolateFactory(Factory): 16 | 17 | def Goods(self): 18 | print("Chocolate are made in this factory") 19 | 20 | class CokeFactory(Factory): 21 | def Goods(self): 22 | print("Coke is made in this factory") 23 | 24 | choc = ChocolateFactory() 25 | choc.Goods() 26 | ck = CokeFactory() 27 | ck.Goods() 28 | ck.Trasportation() -------------------------------------------------------------------------------- /Attributes_using_Functions.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | class CSE: 4 | Trainer = "vineeth" 5 | def __init__(self,a,b): 6 | self.a = a 7 | self.b = b 8 | 9 | 10 | obj = CSE("Prashanth",403) 11 | 12 | print(getattr(CSE,'Trainer')) 13 | print(getattr(obj,'b'))#gets the attribute 14 | 15 | print(hasattr(obj,'b'))#gives True if attribute is present else False 16 | 17 | setattr(obj,'b','20H55A0403')#set the attribute with new value 18 | 19 | print(getattr(obj,'b')) 20 | 21 | delattr(obj,'b')#deletes the attribute 22 | 23 | print(hasattr(obj,'b')) 24 | print(getattr(obj,'b'))#AttributeError: 'CSE' object has no attribute 'b' -------------------------------------------------------------------------------- /Encapsulation_Private.py: -------------------------------------------------------------------------------- 1 | 2 | ''' 3 | Protected members are denoted by using "_" before the name 4 | They can be accessed either inside the class or inside the subclass 5 | python allows the protected members to be accessed outside the class but you are not supposed to use like that 6 | ''' 7 | 8 | 9 | class Electronics: 10 | __battery = "3000A" 11 | def __init__(self): 12 | self.__Gadget() #Accessed inside the same class 13 | print(self.__battery) 14 | pass 15 | 16 | def IC(self): 17 | print("It needs a Processor") 18 | 19 | def __Gadget(self): 20 | print("Smart Watch") 21 | return "Gadget" 22 | 23 | class Watches(Electronics): 24 | def __init__(self): 25 | self.__Gadget() # cannot be Accessed using subclass 26 | print(self.__battery) 27 | pass 28 | 29 | elec = Electronics() 30 | wat = Watches() 31 | -------------------------------------------------------------------------------- /Encapsulation_Protected.py: -------------------------------------------------------------------------------- 1 | 2 | ''' 3 | Protected members are denoted by using "_" before the name 4 | They can be accessed either inside the class or inside the subclass 5 | python allows the protected members to be accessed outside the class but you are not supposed to use like that 6 | ''' 7 | 8 | 9 | class Electronics: 10 | _battery = "3000A" 11 | def __init__(self): 12 | self._Gadget() # Accessed using class 13 | print(self._battery) 14 | 15 | def IC(self): 16 | print("It needs a Processor") 17 | 18 | def _Gadget(self): 19 | print("Smart Watch") 20 | 21 | class Watches(Electronics): 22 | def __init__(self): 23 | self._Gadget() # Accessed using subclass 24 | print(self._battery) 25 | 26 | 27 | elec = Electronics() 28 | wat = Watches() 29 | -------------------------------------------------------------------------------- /Inheritance_Construct_in_Inheritance.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def __init__(self): 3 | print("init of A") 4 | 5 | def feature1(self): 6 | print("Feature 1") 7 | 8 | class B(A): 9 | def __init__(self): 10 | super().__init__() 11 | print("init of B") 12 | 13 | class C(B): 14 | def __init__(self): 15 | super().__init__() 16 | print("init of C") 17 | 18 | c = C() -------------------------------------------------------------------------------- /Inheritance_Hirarcheial_Inheritance.py: -------------------------------------------------------------------------------- 1 | class Father: 2 | def Wallet(self): 3 | print("This is Father's Wallet") 4 | 5 | class Son(Father): 6 | def gpay(self): 7 | print("This is son's Gpay") 8 | 9 | class Daughters_Husband: 10 | def phonepay(self): 11 | print("This is Daughters Husband's Phonepay") 12 | 13 | class Daughter(Father,Daughters_Husband): 14 | def handbag(self): 15 | print("This is Daughter's Handbag") 16 | 17 | s = Son() 18 | s.Wallet() 19 | d = Daughter() 20 | d.Wallet() -------------------------------------------------------------------------------- /Inheritance_MultiLevel_Inheritance.py: -------------------------------------------------------------------------------- 1 | class Grand_Father: 2 | def __init__(self): 3 | print("This is Grand Father's Init") 4 | 5 | def House(self): 6 | print("This is Grand father's House") 7 | 8 | class Father(Grand_Father): 9 | def __init__(self): 10 | print("This is Father's Init") 11 | 12 | def Car(self): 13 | print("This is Father's Car") 14 | 15 | class Son(Father): 16 | def __init__(self): 17 | print("This is Son's Init") 18 | 19 | def Bike(self): 20 | print("This is Son's Bike") 21 | 22 | g = Grand_Father() 23 | f = Father() 24 | s = Son() 25 | g.House() 26 | f.House() 27 | f.Car() 28 | s.House() 29 | s.Car() 30 | s.Bike() -------------------------------------------------------------------------------- /Inheritance_Multiple_Interitance.py: -------------------------------------------------------------------------------- 1 | class Mother: 2 | def __init__(self): 3 | print("This is Mother's Init") 4 | 5 | def Gold(self): 6 | print("This is Mother's Gold") 7 | 8 | class Father: 9 | def __init__(self): 10 | print("This is Father's Init") 11 | 12 | def Car(self): 13 | print("This is Father's Car") 14 | 15 | class Son(Father,Mother): 16 | def __init__(self): 17 | print("This is Son's Init") 18 | 19 | def Bike(self): 20 | print("This is Son's Bike") 21 | 22 | m = Mother() 23 | f = Father() 24 | s = Son() 25 | m.Gold() 26 | f.Car() 27 | s.Gold() 28 | s.Car() 29 | s.Bike() -------------------------------------------------------------------------------- /Inheritance_SingleInheritance.py: -------------------------------------------------------------------------------- 1 | 2 | class Father: 3 | def Car(self): 4 | print("This is Father's Car") 5 | 6 | class Son(Father): 7 | def Bike(self): 8 | print("This is Son's Bike") 9 | 10 | f = Father() 11 | s = Son() 12 | f.Car() 13 | s.Car() 14 | s.Bike() -------------------------------------------------------------------------------- /Inheritanec_Hybrid_Inheritance.py: -------------------------------------------------------------------------------- 1 | class A(): 2 | def show(self): 3 | print("i am in show of A") 4 | 5 | class B(A): 6 | def Display(self): 7 | print("I am in Display of B") 8 | 9 | 10 | a = A() 11 | a = B() 12 | a.show() 13 | a.Display() -------------------------------------------------------------------------------- /Object_and_Class.py: -------------------------------------------------------------------------------- 1 | 2 | class Computer: 3 | def __init__(self,cpu,ram,memory): 4 | self.cpu = cpu 5 | self.ram = ram 6 | self.memory = memory 7 | 8 | def config(self): 9 | print("Configuaration is ",self.cpu,self.ram,self.memory) 10 | 11 | 12 | com1 = Computer('I7',16,'1Tb') 13 | com2 = Computer('I5',8,'500MB') 14 | 15 | com1.config() 16 | com2.config() 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Polymorphism_Duck_Typing.py: -------------------------------------------------------------------------------- 1 | class Pycharm: 2 | def Execute(self): 3 | print("compiling") 4 | print("running") 5 | 6 | class Editor: 7 | def Execute(self): 8 | print("spell check") 9 | print("Convention checking") 10 | print("compiling") 11 | print("running") 12 | 13 | class Laptop: 14 | def Code(self,ide): 15 | ide.Execute()#obj1.Execute() 16 | 17 | obj = Pycharm() 18 | obj1 = Editor() 19 | lap = Laptop() 20 | lap.Code(obj1) -------------------------------------------------------------------------------- /Polymorphism_Method_Overloading.py: -------------------------------------------------------------------------------- 1 | class Student: 2 | def sum(self,a=None,b=None,c=None): 3 | s = 0 4 | if a!=None and b!=None and c!=None: 5 | s = a+b+c 6 | elif a!=None and b!=None: 7 | s = a+b 8 | else: 9 | s = a 10 | return s 11 | 12 | s1 = Student() 13 | print(s1.sum(3,4,5)) 14 | print(s1.sum(1,2)) 15 | print(s1.sum(9)) 16 | -------------------------------------------------------------------------------- /Polymorphism_Method_Overriding.py: -------------------------------------------------------------------------------- 1 | class A(): 2 | def show(self): 3 | print("First show in class A") 4 | 5 | class B(A): 6 | def show(self): 7 | print("First show in class B") 8 | 9 | def show(self): 10 | print("second show in class B") 11 | 12 | 13 | obj = B() 14 | obj.show() -------------------------------------------------------------------------------- /Polymorphism_Operator_Overloading.py: -------------------------------------------------------------------------------- 1 | ''' 2 | a = '5' 3 | b = '6' 4 | print(a+b)#synthetic sugar 5 | print(str.__add__(a,b))#magic method 6 | magic methods 7 | __add__() 8 | __sub__() 9 | __mul__() 10 | ''' 11 | 12 | class Student: 13 | def __init__(self,c,java,python): 14 | self.c = c 15 | self.java = java 16 | self.python = python 17 | 18 | 19 | 20 | def __gt__(self, other): 21 | a = self.c + self.java + self.python 22 | b = other.c + other.java + other.python 23 | if a>b: 24 | return True 25 | else: 26 | return False 27 | 28 | Anand = Student(99,12,80) 29 | Mujahid = Student(100,50,85) 30 | 31 | if Anand>Mujahid: 32 | print("Anand scored more marks") 33 | else: 34 | print("Mujahid Scored more marks") 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | class Student: 49 | def __init__(self,c,java,python): 50 | self.c = c 51 | self.java = java 52 | self.python = python 53 | ''' 54 | def __str__(self): 55 | return self.c,self.java,self.python 56 | ''' 57 | def __str__(self): 58 | return '{2} {0} {1}'.format(self.c,self.java,self.python) 59 | 60 | a = Student('seventy','eighty','ninety') 61 | b = Student('Adarsh','pavan','Hemanth') 62 | print(a.__str__()) 63 | print(b) 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /Search_Binary.py: -------------------------------------------------------------------------------- 1 | def binarySearch(arr, l, r, x): 2 | if r >= l: 3 | mid = l + (r - l) // 2 4 | if arr[mid] == x: 5 | return mid 6 | elif arr[mid] > x: 7 | return binarySearch(arr, l, mid - 1, x) 8 | else: 9 | return binarySearch(arr, mid + 1, r, x) 10 | else: 11 | return -1 12 | 13 | 14 | arr = [10, 4, 40, 3, 2] 15 | arr.sort() 16 | x = 10 17 | result = binarySearch(arr, 0, len(arr) - 1, x) 18 | if result != -1: 19 | print("Element is present at index % d" % result) 20 | else: 21 | print("Element is not present in array") 22 | 23 | -------------------------------------------------------------------------------- /Search_Linear.py: -------------------------------------------------------------------------------- 1 | def Linear_search(arr, n, x): 2 | 3 | for i in range(0, n): 4 | if (arr[i] == x): 5 | return i 6 | return -1 7 | 8 | arr = [2, 3, 4, 10, 40] 9 | x = 10 10 | n = len(arr) 11 | result = Linear_search(arr, n, x) 12 | if(result == -1): 13 | print("Element is not present in array") 14 | else: 15 | print("Element is present at index", result) 16 | -------------------------------------------------------------------------------- /Sort_Bubble.py: -------------------------------------------------------------------------------- 1 | # Bubble Sort 2 | def bubbleSort(arr): 3 | n = len(arr) 4 | for i in range(n): 5 | for j in range(0, n - i - 1): 6 | if arr[j] > arr[j + 1]: 7 | arr[j], arr[j + 1] = arr[j + 1], arr[j] 8 | 9 | 10 | arr = [64, 34, 25, 12, 22, 11, 90] 11 | bubbleSort(arr) 12 | print("Sorted array is:") 13 | for i in arr: 14 | print(i, end=" ") 15 | -------------------------------------------------------------------------------- /Sort_Heap.py: -------------------------------------------------------------------------------- 1 | # Heap Sort 2 | def heapify(arr, n, i): 3 | largest = i # Initialize largest as root 4 | l = 2 * i + 1 # left = 2*i + 1 5 | r = 2 * i + 2 # right = 2*i + 2 6 | if l < n and arr[largest] < arr[l]: 7 | largest = l 8 | if r < n and arr[largest] < arr[r]: 9 | largest = r 10 | if largest != i: 11 | arr[i], arr[largest] = arr[largest], arr[i] # swap 12 | heapify(arr, n, largest) 13 | 14 | def heapSort(arr): 15 | n = len(arr) 16 | for i in range(n//2 - 1, -1, -1): 17 | heapify(arr, n, i) 18 | for i in range(n-1, 0, -1): 19 | arr[i], arr[0] = arr[0], arr[i] # swap 20 | heapify(arr, i, 0) 21 | 22 | 23 | arr = [12, 11, 13, 5, 6, 7] 24 | heapSort(arr) 25 | n = len(arr) 26 | print("Sorted array is") 27 | print(arr) -------------------------------------------------------------------------------- /Sort_Insertion.py: -------------------------------------------------------------------------------- 1 | #Insertion Sort 2 | 3 | def insertionSort(arr): 4 | for i in range(1, len(arr)): 5 | key = arr[i] 6 | j = i-1 7 | while j >= 0 and key < arr[j] : 8 | arr[j + 1] = arr[j] 9 | j -= 1 10 | arr[j + 1] = key 11 | 12 | arr = [12, 11, 13, 5, 6] 13 | insertionSort(arr) 14 | for i in arr: 15 | print(i,end=" ") 16 | -------------------------------------------------------------------------------- /Sort_Merge.py: -------------------------------------------------------------------------------- 1 | # Merge Sort 2 | def mergeSort(arr): 3 | if len(arr) > 1: 4 | mid = len(arr)//2 5 | L = arr[:mid] 6 | R = arr[mid:] 7 | mergeSort(L) 8 | mergeSort(R) 9 | 10 | i = j = k = 0 11 | while i < len(L) and j < len(R): 12 | if L[i] < R[j]: 13 | arr[k] = L[i] 14 | i += 1 15 | else: 16 | arr[k] = R[j] 17 | j += 1 18 | k += 1 19 | while i < len(L): 20 | arr[k] = L[i] 21 | i += 1 22 | k += 1 23 | 24 | while j < len(R): 25 | arr[k] = R[j] 26 | j += 1 27 | k += 1 28 | 29 | arr = [12, 11, 13, 5, 6, 7] 30 | mergeSort(arr) 31 | print("Sorted array is: ") 32 | print(arr) 33 | -------------------------------------------------------------------------------- /Sort_Quick.py: -------------------------------------------------------------------------------- 1 | # Quick Sort 2 | def partition(arr, low, high): 3 | pivot = arr[high] 4 | i = low - 1 5 | 6 | for j in range(low, high): 7 | if arr[j] <= pivot: 8 | i = i + 1 9 | arr[i], arr[j] = arr[j], arr[i] 10 | 11 | arr[i + 1], arr[high] = arr[high], arr[i + 1] 12 | 13 | return i + 1 14 | 15 | 16 | def quick_sort(arr, low, high): 17 | if low < high: 18 | pi = partition(arr, low, high) 19 | 20 | quick_sort(arr, low, pi - 1) 21 | 22 | quick_sort(arr, pi + 1, high) 23 | 24 | 25 | arr = [10, 7, 8, 9, 1, 5] 26 | quick_sort(arr, 0, len(arr) - 1) 27 | 28 | print('Sorted array: ') 29 | print(arr) 30 | -------------------------------------------------------------------------------- /Sort_Selection.py: -------------------------------------------------------------------------------- 1 | # Selection Sort 2 | def Selection_sort(arr): 3 | for i in range(len(arr)): 4 | min_idx = i 5 | for j in range(i + 1, len(arr)): 6 | if arr[min_idx] > arr[j]: 7 | min_idx = j 8 | arr[i], arr[min_idx] = arr[min_idx], arr[i] 9 | 10 | 11 | arr = [64, 25, 12, 22, 11] 12 | Selection_sort(arr) 13 | print("Sorted array") 14 | for i in arr: 15 | print(i, end=" ") 16 | -------------------------------------------------------------------------------- /Types0fMethods_1_Instance_Method.py: -------------------------------------------------------------------------------- 1 | class Food: 2 | def __init__(self,starter,maincourse,dessert): 3 | self.starter = starter 4 | self.maincourse = maincourse 5 | self.dessert = dessert 6 | 7 | def show(self):#instance method 8 | print(self.starter, self.maincourse, self.dessert) 9 | 10 | def get_maincourse(self):#getter method or Accessor 11 | return self.maincourse 12 | 13 | def set_maincourse(self,newmaincourse): 14 | self.maincourse = newmaincourse 15 | 16 | 17 | meal1 = Food("chi cken_tikka","Biryani","gualb_jamun") 18 | meal2 = Food("Manchuriyan","Mutton_biryani","Icecream") 19 | 20 | print(meal1.get_maincourse()) 21 | meal1.set_maincourse("Fish_man") 22 | print(meal1.get_maincourse()) 23 | 24 | ''' 25 | ===> It contains keyword "self" as Default first Implicit Argument 26 | ===> An Instance method is bound to the object of the class. 27 | ===> An instance method is created to be used by the class members 28 | or the objects 29 | ===> An Instance method can be invoked(called) using the object of the 30 | particular class. 31 | ''' -------------------------------------------------------------------------------- /TypesOfMethods_2_Class_methods.py: -------------------------------------------------------------------------------- 1 | class Food: 2 | Restuarent = "CMR Canteen" 3 | def __init__(self,starter,maincourse,dessert): 4 | self.starter = starter 5 | self.maincourse = maincourse 6 | self.dessert = dessert 7 | 8 | def show(self): 9 | print(self.starter, self.maincourse, self.dessert) 10 | 11 | @classmethod #decorator 12 | def place(cls,a):#class method 13 | cls.Restuarent = a 14 | return Food.Restuarent 15 | 16 | meal1 = Food("chicken_tikka","Biryani","gualb_jamun") 17 | meal2 = Food("Manchuriyan","Mutton_biryani","Icecream") 18 | 19 | print(Food.place("Platfor65")) 20 | meal1.show() 21 | meal2.show() 22 | 23 | ''' 24 | ===> It contains keyword "cls" as Default first Implicit Argument 25 | ===> it has a decorator @classmethod above it. 26 | ===> class methods are bounded to class not to the Object of class 27 | ===> class methods can modify a class state that would apply across all 28 | the instances of the class 29 | ''' -------------------------------------------------------------------------------- /TypesOfMethods_3_Static_Methods.py: -------------------------------------------------------------------------------- 1 | class Food: 2 | Restuarent = "CMR Canteen" 3 | def __init__(self,starter,maincourse,dessert): 4 | self.starter = starter 5 | self.maincourse = maincourse 6 | self.dessert = dessert 7 | 8 | def show(self): 9 | print(self.starter, self.maincourse, self.dessert) 10 | 11 | @staticmethod #decorator 12 | def Display(): 13 | print(" We are inside the Restuarent. ") 14 | 15 | @staticmethod 16 | def change(): 17 | Food.Restuarent = "Bounce" 18 | 19 | meal1 = Food("chicken_tikka","Biryani","gualb_jamun") 20 | meal2 = Food("Manchuriyan","Mutton_biryani","Icecream") 21 | 22 | print("Restuarent Name :- ",Food.Restuarent) 23 | Food.change() 24 | print("Restuarent Name :- ",Food.Restuarent) 25 | Food.Display() 26 | 27 | ''' 28 | ===> a static method doesnt have implicit first argument 29 | ===> it has a decorator @staticmethod above it. 30 | ===> it is bound to class not the object of class 31 | ===> it cant modify the state of class 32 | 33 | ''' --------------------------------------------------------------------------------