Machine_Learning_Day_5_ppt (1)
Machine_Learning_Day_5_ppt (1)
TECHNOLOGY
Object
The object is an entity that has state and behavior. It may be any real-
world object like the mouse, keyboard, chair, table, pen, etc. Everything
in Python is an object, and almost everything has attributes and methods.
Class
The class can be defined as a collection of objects. It is a logical entity
that has some specific attributes and methods. For example: if you have
an employee class then it should contain an attribute and method, i.e. an
email id, name, age, salary, etc.
Course: Machine Learning using Python
Day : 5
Encapsulation
Method
It specifies that the child object acquires all the properties and
behaviors of the parent object. A class may use all the
properties and behavior of another class.
• The new class is known as a derived class or child class.
• The class whose properties are acquired is known as a
base class or parent class.
• It provides re-usability of the code.
Course: Machine Learning using Python
Day : 5
Polymorphism
Data Abstraction
def display(s):
print(“ID=”,s.id)
print(“Name”,s.name)
Ex: m=data()
m.display() #calling display function using object of
class my.
Course: Machine Learning using Python
Day : 5
Access Specifier
Public:
By default, all the variables and member functions of a class are
public in a python program.
Ex: x #will be public member of the
class
Private:
A variable can be made private by prefixing the __(double
underscore) before the variable name.
ex: __x will be the private member of the class.
Protected:
A variable can be made public by prefixing the _ (single
underscore) before the variable name.
Course: Machine Learning using Python
Day : 5
Constructor
A constructor is a special type of method (function) which is used to initialize the
members of the class. Constructor definition is executed when we create the object of
this class.
Types.
1. Parameterized Constructor
2. Non-parameterized Constructor
Course: Machine Learning using Python
Day : 5
Creating the constructor
In python, the method __init__ is used to create the constructor of the class. This
method is called when the object of the class is instantiated.
• We can pass any number of arguments at the time of creating the class object,
depending upon __init__ definition.
• It is mostly used to initialize the class attributes. Every class must have a
constructor, even if it simply relies on the default constructor.
Course: Machine Learning using Python
class my: Day : 5
Example of Constructor
id=101
name=“Rajat"
def display(s)
print(“Employee ID=”,s.emp_id, “\nEmployee
Name=”,s.emp_name,”\nSalary=”,s.emp_salary)
E=emp(101, “Rahul”, 36000)
E.display()
Course: Machine Learning using Python
Day : 5
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. The child class acquires the properties and can
access all the data members and functions defined in the parent class.
Course: Machine Learning using Python
Day : 5
Syntax:
class class_name(base_class_name):
class definition
class A:
# variable of class A
# functions of class A
class B:
# variable of class A
# functions of class A
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d=Derived()
print(d.Summation(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))
Course: Machine Learning using Python
Day : 5
Types of Inheritance in Python
Python - Multilevel Inheritance
In multilevel inheritance, we inherit the classes at multiple
separate levels. We have three classes A, B and C, where A is
the super class, B is its sub(child) class and C is the sub class of
B.
Here is a simple example, its just to explain you how this looks
in code:
class A:
# properties of class A
class B(A):
# class B inheriting property of class A # more
properties of class B
class C(B):
# class C inheriting property of class B
Course: Machine Learning using Python
Day : 5
Multi-Level Inheritance
class Animal:
def speak(self):
print("Animal Speaking")
class Dog(Animal):
def bark(self):
print("dog barking")
class DogChild(Dog):
def eat(self):
print("Eating bread...")
d = DogChild()
d.bark()
d.speak()
d.eat()
Course: Machine Learning using Python
Day : 5
Method Overloading
In Python you can define a method in such a way that there are
multiple ways to call it. Depending on the function definition, it can be
called with zero, one, two or more parameters. This is known as
method overloading.
In the given code, there is a class with one method sayHello(). We
rewrite as shown below. The first parameter of this method is set to
None, this gives us the option to call it with or without a parameter.
An object is created based on the class, and we call its method using
zero and one parameter. To implement method overloading, we call the
method sayHello() in two ways: 1. obj.sayHello() and
2.obj.sayHello('Rambo')
class Human:
def sayHello(self, name=None):
if name is not None:
print 'Hello ' + name
else:
print 'Hello '
obj = Human()
Course: Machine Learning using Python
Day : 5
Method Overriding
We can provide some specific implementation of the parent class
method in our child class. When the parent class method is defined in
the child class with some specific implementation, then the concept is
called method overriding. We may need to perform method overriding
in the scenario where the different definition of a parent class method
is needed in the child class.
class Animal:
def speak(self):
print("speaking")
class Dog(Animal):
def speak(self):
print("Barking")
d=Dog()
d.speak()
Course: Machine Learning using Python
Day : 5
Example of method overriding
class Bank:
def getroi(self):
return 10;
class SBI(Bank):
def getroi(self):
return 7;
class ICICI(Bank):
def getroi(self):
return 8;
b1=Bank()
b2=SBI()
b3=ICICI()
print("Bank Rate of interest:",b1.getroi());
print("SBI Rate of interest:",b2.getroi());
print("ICICI Rate of interest:",b3.getroi());
Course: Machine Learning using Python
Day : 5
Tomorrow Agenda
THANKS