Python For ML - Recursion, Inheritence, Data Hiding
Python For ML - Recursion, Inheritence, Data Hiding
Python For ML - Recursion, Inheritence, Data Hiding
Python For ML
Module Name - Introduction
to Python
Class - Day 7
EditEdit
MasterMaster
Topic - Recursion,
texttext stylesstyles
Inheritance and Data Hiding
● Revision
● Recursion
● Class Inheritance
● Data Hiding
Revision
Applications:
● Puzzle games uses recursion.
● The most important data structure ‘Tree’ doesn’t exist without recursion we
can solve that in iterative way also but that will be a very tough task.
● The NP problem can’t be solved without recursion.
● Sorting algorithms like (Quick sort, Merge sort, etc.) uses recursion.
Recursion
Syntax
Class ParentClass:
Class ChildClass (ParentClass):
Class Inheritence
Benefits of Inheritance:
● Code reusability- we do not have to write the same code, again and again,
we can just inherit the properties we need in a child class.
● It represents a real-world relationship between parent class and child class.
● It is transitive in nature.
Class Inheritance
class Parent():
def first(self):
print('first function')
Output
class Child(Parent): first function
def second(self): second function
print('second function')
ob = Child()
ob.first()
ob.second()
Class Inheritance
Types Of Inheritance
● Single Inheritance
● Multiple Inheritance
● Multilevel Inheritance
● Hierarchical Inheritance
● Hybrid Inheritance
Class Inheritance
Type of Inheritance Example Syntax
Super() Function:
● Super function allows us to call a method from the parent class.
class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
Super().func1()
print("this is function 2")
ob = Child()
ob.func2()
Data Hiding
Syntax
class CounterClass:
__privateCount = 0
def count(self):
self.__privateCount += 1
print(self.__privateCount)
Key Takeaways
● What is Recursion
● Applications of Recursion and its pros and cons
● What is Class Inheritance
● Types of Class Inheritance
● Data Hiding
#LifeKoKaroLift
Thank You!
Happy learning!