top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Python Concepts for Data Science: Inheritance

Inheritance generally means to inherit the characters from another things(can be person, object, class or anything)


As in the diagram, the son inherits the characters from Dad and Mom. Futher Dad and Mom have the characters inherited from a general human class.

Similar to human, inheritance is useful in programming so as child(derived) class inherits from base/parent class.

class Human:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    def printname(self):
        print(self.firstname, self.lastname)
    
class Man(Human):
    pass

x= Man("Ram","Shyam")
x.printname()

Output
Ram Shyam

In above example, Human class is the base class and Man is derived from it inheriting all the functions of Human. We created an object x of class Man. The printname() function of Human class is callable from Man object due to inheritance.

We can also add other function or attributes in derived class as:

class Human:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    def printname(self):
        print(self.firstname, self.lastname)
    
class Man(Human):
    def __init__(self, firstname, lastname):
        super().__init__(firstname,lastname)
        self.gender = "Male" 
    def printgender(self):
        print(self.gender)

x= Man("Ram","Shyam")
x.printname()
x.printgender()

The functions of base class can be manipulated in derived class as:

class Human:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    def printname(self):
        print(self.firstname, self.lastname)
    
class Man(Human):
    def __init__(self, firstname, lastname):
        super().__init__(firstname,lastname)
        self.gender = "Male"
    def printgender(self):
        print(self.gender)

class Daughter(Man):
    def __init__(self, firstname, lastname):
        super().__init__(firstname,lastname)
        self.gender = "Female"

y= Daughter("ABC","XYZ")
y.printname()
y.printgender()

Output
ABC XYZ
Female
 
 
 

Comments


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page