0% found this document useful (0 votes)
18 views

Python For ML - Recursion, Inheritence, Data Hiding

This document provides a summary of recursion, inheritance, and data hiding in Python. It discusses how recursion allows functions to call themselves, leading to looping behavior. Inheritance allows new classes to inherit properties from parent classes, following an "is-a" relationship. Types of inheritance include single, multiple, multilevel, and more. Data hiding uses double underscores to prefix private variables and prevent direct access from outside a class.

Uploaded by

john pradeep
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Python For ML - Recursion, Inheritence, Data Hiding

This document provides a summary of recursion, inheritance, and data hiding in Python. It discusses how recursion allows functions to call themselves, leading to looping behavior. Inheritance allows new classes to inherit properties from parent classes, following an "is-a" relationship. Types of inheritance include single, multiple, multilevel, and more. Data hiding uses double underscores to prefix private variables and prevent direct access from outside a class.

Uploaded by

john pradeep
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

#LifeKoKaroLift

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

In the last session, you learnt about: 

• Should be updated by the instructor


Recursion

● Recursion is a common mathematical and programming concept.


● Recursion is a technique where functions or methods makes a call to itself.
● This has the benefit of meaning that you can loop through data to reach a
result.
Recursion
Recursion

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

Pros and Cons:


● Recursive functions are in-memory functions and take up more memory
than iterative counterparts.
● However, their runtime performance is faster in general.
Class Inheritence

● Inheritance is a key concept in Object-Oriented Programming.


● It enables us to create a new class from an existing class.
● The new class is a specialized version of the existing class and it inherits all
the non-private variables and methods of the existing class.
● Well you may know everything in python is an object. Also all classes by
default is inherited from built-in Object class implicitly

Syntax
Class ParentClass:
Class ChildClass (ParentClass):
Class Inheritence

When to use Inheritance


● We use inheritance when we have “is a” relationship between objects.
Like, an employee is a Person, a car is a vehicle, a dog is an animal.

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

Example of Inheritance in Python:

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

Single Invariance class Parent:


def func1(self):
print("this is function one")
class Child(Parent):
def func2(self):
print(" this is function 2 ")
ob = Child()
ob.func1()
ob.func2()
Multiple Inheritance class Parent:
def func1(self):
print("this is function 1")
class Parent2:
def func2(self):
print("this is function 2")
class Child(Parent , Parent2):
def func3(self):
print("this is function 3")ob = Child()
ob.func1()
ob.func2()
ob.func3()
Class Inheritance
Type of Inheritance Example Syntax

Multilevel Invariance class Parent:


def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child2(Child):
def func3("this is function 3")
ob = Child2()
ob.func1()
ob.func2()
ob.func3()
Hierarchical Inheritance class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child2(Parent):
def func3(self):
print("this is function 3")
ob = Child()
ob1 = Child2()
ob.func1()
ob.func2()
Class Inheritance
Type of Inheritance Example Syntax

Hybrid Invariance class Parent:


def func1(self):
print("this is function one")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child1(Parent):
def func3(self):
print(" this is function 3"):
class Child3(Parent , Child1):
def func4(self):
print(" this is function 4")
ob = Child3()
ob.func1()
Class Inheritance

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

● Data hiding is a part of object-oriented programming, which is generally


used to hide the data information from the user.
● We can perform data hiding in Python using the __ double underscore
before prefix.

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!

You might also like