0% found this document useful (0 votes)
5 views27 pages

Unit 3 Lecture 1.2-Inheritance

Uploaded by

mahammadjubed213
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views27 pages

Unit 3 Lecture 1.2-Inheritance

Uploaded by

mahammadjubed213
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Established as per the Section 2(f) of the UGC Act, 1956

Approved by AICTE, COA and BCI, New Delhi

Lecture 1.2
Programming with
Python
S c h o o l o f C o m p u t i n g a n d I n f o r m a t i o n Te c h n o l o g y
Programming with Python
Recap of previous Lecture
RECAP OF PREVIOUS
LECTURE

Defining Classes

The_init_() Method

Instantiating Classes

Instance Variables
Programming with Python
Python Inheritance
PROGRAMMING WITH PYTHON
Python inheritance

It is a mechanism that allows you to create a hierarchy of classes that share


a set of properties and methods by deriving a class from another class.
Inheritance is the capability of one class to derive or inherit the properties
from another class.

Inheritance allows us to define a class that inherits all the methods and
properties from another class.

Parent class is the class being inherited from, also called base class.

Child class is the class that inherits from another class, also called derived
class.
PROGRAMMING WITH PYTHON
Python inheritance
Benefits of inheritance are:

Inheritance allows you to inherit the properties of a class, i.e., base class to
another, i.e., derived class. The benefits of Inheritance in python are as
follows:
1. It represents real-world relationships well.
2. It provides the reusability ouh11f a code. We don’t have to write the
same code again and again. Also, it allows us to add more features to a
class without modifying it.
3. It is transitive in nature, which means that if class B inherits from another
class A, then all the subclasses of B would automatically inherit from
class A.
4. Inheritance offers a simple, understandable model structure.
5. Less development and maintenance expenses result from an
inheritance.
PROGRAMMING WITH PYTHON
Python inheritance

Python Inheritance Syntax


The syntax of simple inheritance in Python is as follows:

Class BaseClass:
{Body}
Class DerivedClass(BaseClass):
{Body}

To create a class that inherits the functionality from another class,


send the parent class as a parameter when creating the child class:
PROGRAMMING WITH PYTHON
Python inheritance
# A Python program to demonstrate
inheritance
class Person(): Mayank 103
def __init__(self, name,id): Emp class called
self.name = name
self.id = id
def Display(self):
print(self.name, self.id)
class Emp(Person):
def Print(self):
print("Emp class called")
Emp_details = Emp("Mayank", 103)
Emp_details.Display()
Emp_details.Print()
PROGRAMMING WITH PYTHON
Python inheritance
PROGRAMMING WITH PYTHON
Single Inheritance:
Single level inheritance enables a derived class to inherit
characteristics from a single parent class.
Single Inheritance allows a derivate class to inherit properties of
one parent class, and this allows code reuse and the introduction
of additional features in existing code.
PROGRAMMING WITH PYTHON
Python inheritance
This function is defined inside the parent class.
This function is defined inside the child class.
class Parent1:
def fun1(self):

print("This function is defined inside the parent class.")


class Child1(Parent1):
def fun2(self):
print("This function is defined inside the child class.")
object = Child1()
object.fun1()
object.fun2()
PROGRAMMING WITH PYTHON
Python inheritance
class Child1: std = Child1("Jackie")
def __init__(self, name): print(std.getName1(), std.isStudent1())

self.name = name
std = Student1("johnny")
print(std.getName1(), std.isStudent1())
def getName1(self):
return self.name

def isStudent1(self): Jackie False


johnny True
return False

class Student1(Child1):
def isStudent1(self):
return True
Programming with Python
R e s o u r c e s a n d Ta s k s t o b e
completed
RESOURCES AND TASKS
Optional / Non-optional Reading resources for the
lecture:
Non-optional Reading resource

Mark Pilgrim, Dive Into Python 3, APress Media


LLC, 2009

Optional Reading resource


https://www.learnpython.org/en/Classes_and_Objects
https://www.w3schools.com/python/python_iterators.asp
RESOURCES AND TASKS
Optional / Non-optional Tasks to be completed:
Non-optional Assignments

Write a Python class to get all possible unique subsets from a set
of distinct integers.

Optional Assignments

Write a Python class named Circle constructed by a radius and two methods
which will compute the area and the perimeter of a circle
RESOURCES AND TASKS
Reading resources for next lecture :

Topic: Advanced Iterators: Finding all occurrences of a Pattern,


Finding the Unique items in a sequence

Mark Pilgrim, Dive Into Python 3, APress Media


LLC, 2009
DISCUSSION
5 MINUTES

Generators

Iterators
QUIZ TIME
10 MINUTES

“All of you have to give quiz where there will be 8 questions covering part1 and part 2
of lecture. It is compulsory as it is used for assessment and attendance”
QUESTIONS FROM LECTURE 1.1 &
1.2
Which Of The Following Can Be Used To Invoke The __init__ Method In B
From A, Where A Is A Subclass Of B?

A. B.
super().__init__() super().__init__(self)

C. D.
B.__init__() B.__init__(super)
QUESTIONS FROM LECTURE 1.1 &
1.2
Python is fully object-oriented, you can define your own classes,
inherit from your own or built-in classes, and instantiate the classes
you’ve defined. This statement is-

A.
True
B.
False

?
QUESTIONS FROM LECTURE 1.1 &
1.2
_____ represents an entity in the real world with its identity and behaviour.

A. B.
A method An object

C. D.
A class An operator
QUESTIONS FROM LECTURE 1.1 &
1.2
What is Instantiation in terms of OOP terminology?

A. B.
Deleting an instance of Modifying an instance of
class class

C. D.
Copying an instance of Creating an instance of
class class
QUESTIONS FROM LECTURE 1.1 &
1.2

The ______________ function returns an iterator containing the


Cartesian product of two sequences

A. B.
itertools.product() re.cartesian()

C. D.
CartesianProduct() itertools.CartesianProduc
t()
QUESTIONS FROM LECTURE 1.1 &
1.2

The _______________ function takes two iterators and returns an


iterator that contains all the items from the first iterator

A. B.
itertools.join() itertools.chain()

C. D.
itertools.product() itertools.group()
QUESTIONS FROM LECTURE 1.1 &
1.2

The itertools.groupby() function only works if the input sequence is


already sorted by the grouping function

A.
True
B.
False

?
QUESTIONS FROM LECTURE 1.1 &
1.2

__iter()__ and __next()__ methods are used with an iterator to


iterate through the given sequence.

A.
True
B.
False

?
THANK YOU

You might also like