Python Basic Tutorials

Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more

Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c

To make everything easy: We refer CPython as Python

Inheritance in Python

Inheritance is the important OOP concept in which a class can inherit the properties of another class. The class which inherits the properties is called the child class. The class from which the properties are inherited is called the parent class. So, in Inheritance, a child class can inherit all the attributes and behaviors of a superclass.

Example1

We are taking an example, in which there are two classes A and B with both having two methods. Here, Class B inherits Class A then all the features is accessible to Class B.


class A:
    def feature1(self):
        print("Feature 1 working.")

    def feature2(self):
        print("Feature 2 working.")

class B(A): #Class B is inheriting Class A.
    def feature3(self):
        print("Feature 3 working.")

    def feature4(self):
         print("Feature 4 working.")

b1=B()
#now b1 object can access features of A as well as feature of B.
b1.feature1()
b1.feature2()
b1.feature3()
b1.feature4()
    
Output

Feature 1 working.
Feature 2 working.
Feature 3 working.
Feature 4 working.
    
Example2

In this example, Dog is inheriting an Animal. After inheriting, Dog is now able to use the method of Animal as well as its own method.


class Animal:
    def eat(self):
        print("Eating...")

class Dog(Animal):
    def bark(self):
        print("Barking...")

dog1=Dog()
dog1.bark()
dog1.eat()
Output

Barking...
Eating...     
Multilevel Inheritance

If class B inherits class A. Also, if class B is further inherited by another class C, then such kind of inheritance is known as multilevel inheritance. In this case, objects of the C class can access the attributes and behaviors of classes A and B.

Example
     

class A:
    def feature1(self):
        print("Feature 1 working.")

    def feature2(self):
        print("Feature 2 working.")

class B(A): #Class B is inheriting Class A.
    def feature3(self):
        print("Feature 3 working.")

    def feature4(self):
        print("Feature 4 working.")

class C(B):
    def feature5(self):
        print("Feature 5 working.")

c1 = C()
c1.feature1()
c1.feature2()
c1.feature3()
c1.feature4()
c1.feature5() 
Output

Feature 1 working.
Feature 2 working.
Feature 3 working.
Feature 4 working.
Feature 5 working.
 
Multiple Inheritance

If a class is inheriting two or more than two classes at a time, then such kind of inheritance is known as Multiple inheritances.

Example

In this example, class C is inheriting class A and class D at the same time.


class A:
    def feature1(self):
        print("Feature 1 working.")

    def feature2(self):
        print("Feature 2 working.")

class B:
    def feature3(self):
        print("Feature 3 working.")

    def feature4(self):
        print("Feature 4 working.")

class D:
    def feature6():
        print("Feature 6 working.")

class C(A,D):
    def feature5():
        print("Feature 5 working.")

c1 = C()
c1.feature1()
c1.feature2()
c1.feature6()
c1.feature5()
    
Output

Feature 1 working.
Feature 2 working.
Feature 6 working.
Feature 5 working.