oops concepts in python with real life example

By // No comments:

  •  OOP(object oriented programming) is used to solve the problems with the help of  classes and objects.
  • Python is an object oriented programming language like c++,java and c#.
  • We can easily develop the applications using this object oriented approach.
  • An object oriented paradigm is used to design the program using using classes and objects.
  • This oops concepts in python focuses on creating reusable code.
There are some major concepts is used in oops(object -oriented programming system) as given below.
  1. class 
  2. object
  3. method
  4. Inheritance
  5. Polymorphism
  6. Encapsulation
  7. Abstraction
1.) Class:
  • A class is a blue print of an object.
  • A class is a collection of data and methods
class student:
    def show_data(self):
      print("Value of sum is= ",self.s)
Value of sum is=  11

2.) Object:-
  • Every thing is an object in python.
  • Object is an instance of a class .Object is an entity that has some state and behavior.
  • We can access the class data with the help pf object(instance) of the class.
More details... 
class employee:
   def __init__(self,a,b,c):
   def display(self):
       print("Employee id is= ",self.id)
       print("Employee name is= ",self.name)
       print("Employee salary is= ",self.salary)
#create the object/instance of the class       
Employee id is=  102
Employee name is=  Ram
Employee salary is=  30000
3.) Method:-
  • Method is a function which is define inside the body of the class.
  • Methods are used to define and manipulate  the behaviours of an objects.
class student:
    def __init__(self,a,b,c):
    def show(self):
        print("student name is :",self.name)
    def display(self):
        print("Student id is: ",self.id)
        print("student school is: ",self.school)
s1=student(102,'Mohan',"Delhi public school")
student name is : Mohan
Student id is:  102
student school is:  Delhi public school
  • Here i have called show() method first,which is responsible to display the name of the student.
  • After that i have called display() method,which is responsible to display the student id and school name. 
4.) Inheritance:-
  • Inheritance is the most important concept of object-oriented programming  language.It simulates the real world concepts of inheritance.
  • In inheritance, a new class called derived class can be created to inherit features of an existing class called base class.
  • Base class is also known as super class or parent class.
  • Derived class is also know as sub class or child class.
  • In inheritance,when we create the object of child class.Then this child class object acquires all the properties and behaviors of the parent object. 
  • Inheritance concept provide the re-usability of code.
#parent class
class student:
    def __init__(self):
        print("I am parent class constructor")
    def display(self):
         print("I am parent class display method")
    def calculate(self,a,b,c):
        print("Sum of three numbers is= ",self.s)        
#child class
class student_child(student):
     def __init__(self):
         #call the super function
         print("I am child class constructor")
     def show(self):
         print("I am show method of child class")
#creating the instance of the child/derived class
x=int(input("Enter first number: "))
y=int(input("Enter second number: "))
z=int(input("Enter third number: "))
Enter first number: 20
Enter second number: 40
Enter third number: 50
I am parent class constructor
I am child class constructor
I am parent class display method
Sum of three numbers is=  110
I am show method of child class
Here i have created the object of the student_child class.All method of student class (parent class) and student_child(derived) class will be loaded in main memory.Because the student_child class inherited the all features of student class.
5.) Polymorphism:-
'Polymorphism' word  is made two word "poly" and  "morph".'poly' means many and 'morph' means shape/forms.In this concept we can perform one task in different ways for example,suppose we want to go 'Delhi ',we generally uses 'GPS' for navigating the routes.Here GPS located different routes for same destination Delhi.In programming, this is called 'polymorphism'.
  • In oop concept.one-task can be performed in several different ways.
Polymorphism is two types:-
  1. compile-time polymorphism(method overloading)
  2. Runtime polymorphism(method overloading)
We will learn it in detail in a separate lecture.
class student:
    def name(self):
        print("My good name is Ram")
    def display(self):
        print("I am a good student")
class employee:
    def name(self):
        print("My name is Rajesh chauhan")
    def display(self):
        print("I am not a good employee")
#common Interfacefunction
def func_name(object):
#creating the instace/object of the class
#Passing these class objects in common interface function one by one and get output
My good name is Ram
I am a good student
My name is Rajesh chauhan
I am not a good employee
6.) Encapsulation:-
  • Encapsulation is a important concept of object oriented programming language.
  • In encapsulation methodology we can restrict access to methods and variables.
  • To prevent the data access of the class from out side or same class, is known as encapsulation.
  • In python ,we denote private attributes using prefix underscore(single(_) or double(__)).
class employee:
    def __init__(self):
    def show(self):
        print("salary of the employee",self._salary)
    def display_data(self):
        print("Employee name is:",self.name)
        print("Employee name is:",self.age)
    def change_salary(self,sal):
#creating the instance/object of the employee class
salary of the employee 2000
Employee name is: Ram
Employee name is: 30
salary of the employee 50000
7.) Abstraction:-
  • Abstraction is used to hide internal details and show only functionalities.
  • A class from which an object can not created is called an abstract class.
  • A class from which an object can not be created is called an abstract class.
from abc import ABC,abstractmethod
class shape(ABC):
    def draw(self):
class circle(shape):
    def draw(self):
        print("Draw the circle")
class square(shape):
    def draw(self):
        print("Draw the square")
#s=shape()  --> this will show error because shape is abstract class

Draw the circle
  • abc stands for abstract base classes.To create an abstract class we need to derive it from class ABC parent in abc module.
  • Now we need to mark draw() as abstract method using the decorator @abstractmethod
  • If an abstract class contains only methods marked by the decorator @abstractmethod,it is called interface.
Watch complete lecture video:-
----------------coming soon-----------------
For More...


Constructor concepts in python

By // No comments:

 A Constructor is a special type of method which is used to initialize the object of the class.In c++ and java ,the constructor is the same name as its class.But in python,it is different .Hence we use __init__() as a special method to simulates the constructor of the class.This special method is called when the object of the class is created.

  • The __init__() method accepts the self keyword as a first argument which is used to access the methods or attributes of the class.
  • We can pass the any number of arguments at the time of object creation according to definition of __init__() method.
  • The constructors are mainly used to initialize the class attributes.
  • In python,every class must have a constructor even it may be a default constructor.

Classes and Objects Concepts in Python

By // No comments:
Python classes and objects

Python is an object oriented programming language .Every things in python treated as an object.python supports structured as well as object oriented programming language paradigms.

  • A class contains data and  methods that can access or manipulate this data.Thus a class is a collection of data and methods.A class is a blueprint (prototype) of an object. Thus a class lets us bundle data and functionality together.
  • A user-defined blueprint/prototype for an object is known as class.example : House is an object where blueprint/prototype/model of house is known as a Class.
  • A class is generic in nature,whereas an object is specific in nature.
  • We can create many objects from one class.We can easily access the class member with the help of these objects.
  • An object is also an instance of a class The process of creating this object  is called instantiation
  • We can  also create our own classes.These are often called user defined data types but we generally uses ready-made classes in python.

Arrays Concept in Python With Examples

By // No comments:
Python Arrays 
  • An array is a collection of elements that are stored in contiguous memory locations.
  • An array holds fixed number of elements of the same data types.
  • An array can store multiple elements of the same data types.
  • An array is used to store multiple values in single variable.
  • An array in python can be handled a module named array
import array as aar

Representation of array:-
An array can be declared in different ways and different languages.Here We are going to declared the array in python language. 
  • Python array starts with 0 index.
  • Python array's elements can be accessed via its index.
  • The length of an array = total number of index+1.

Modules and Package Concepts in Python

By // No comments:

 Today, we will learn about most important concepts of modules in python.

Modules in Python:-

  • A modules is a .py files containing definitions and statements. So all .py files that we created for our python programs are modules.
  • A python modules can be defined a python program file which contains .py files including python variable , class and functions etc.
  • A python modules provides us the flexibility to organize the code in a logical way.
e.g. Create a modules named as modules.py
def show ():
    print("Welcome to MNT LAB")
#calling the show() function
Welcome to MNT LAB
The Main Modules:-
  • When we execute a program its module name is __main__.
  • This name is available in the variable __name__.
  • This technique is executed only when we run program directly and not executed when imported as a module.
  • When we run any python codes ,python interpreter start interpreting the codes inside it. At that time it sets some implicit variable values like math, random etc. ,one of them is __name__ whose value set as __main__.
  • If the program source file imported as a module , At that situation interpreter sets the __name__ value to module name. Then this main  method will not work.

Lambda and Recursive functions in python

By // No comments:

Lambda function in python:

  • Normal functions have their names and they are defined using the def keyword.An anonymous functions do not have names and they are defined using the lambda keyword.
lambda arguments:expression
  • An anonymous function defined using lambda can take any number of arguments but can return only one value.
  • Lambda functions can be used wherever function objects are required usually.It is used as an argument to other functions.
  • An anonymous function contains a small piece of code.

Function concepts in python Part 2

By // No comments:

We have already explained some basic features of functions in our previous lectures.Before learn this part ,you have to learn function concepts in python part 1.

Types of Arguments:-

There are four types of arguments in python functions as given below:-

  1. Required arguments or Positional arguments.
  2. Keyword arguments
  3. Default arguments
  4. Variable Length arguments
    1.) Required arguments or Positional arguments
Required arguments must be passed in correct positional order.For example,if a function want an int, float and string to be passed it,then the call to this function should look like:
func(10,5.12,'MNT LAB')                #Correct function call
func(5.12,10,'MNT LAB')                #Incorrect function call gives error

In required arguments number of arguments passed must be match with number of arguments received.
Powered by Blogger.