Python Topics
1.
OOP’s Concepts
a.
Features & applications of oops
b.
Concept of oops
c.
Class, object & Methods
d.
Constructor & Destructor
e.
Polymorphism
f.
Inheritance
Python OOPS:
----------------------
-Python
is an object oriented programming language.
-Almost
everything in Python is an object,
-with
its properties or attributes and methods or function
Concept of oops
---------------
1.
class
2.
Method
3.
Object
4.
Polymorphism
5.
Inheritance
6.
Data Abstraction
7.
Data Encapsulation
1. class:
--------
-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.
Syntax:
----------------
class class_name:
sta1
sta2
.....
.....
sta-N
---------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2. Method
-----------
-Method is a function that is associated with
an object.
-In Python, method is not unique to class
instances.
-Any object type can have methods.
Functions
------------
-A Function is a self block of code which
is used to organize the functional code.
-Function can be called as a section of a
program that is written once and can be executed whenever required in the
program, thus making code reusability.
-Function is a subprogram that works on
data and produces some output.
Syntax:
-----------------------------
def Function_name(args):
sta-1
sta-2
....
....
sta-n
------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3. Object:
---------
-An
Object is a real time entity
-An
Object it has state and behaviour
-An
Object contains Data's and methods
Creating
an object in Python
----------------------------
Syntax:
--------------------------
class Class_name:
def
function_name(parameters):
statement-1
statement-2
....
statement-n
object_name
= Class_name() #create object
object_name.Method_name()
#invoke Method
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 1: Program for Class,
object & Methods
class
test:
def msg(self):
print("Hi Welcome")
t1=test()
t1.msg()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 2: Addition of
two numbers using oops
class
test:
def get1(self):
self.a=int(input("Enter First
Value :"))
self.b=int(input("Enter Second
Value :"))
def calc1(self):
self.c=self.a+self.b
def put1(self):
print ("Addition Result
:",self.c)
obj
= test()
obj.get1()
obj.calc1()
obj.put1()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class
test:
def get1(self,x,y):
print ("Inside function")
self.x=x
self.y=y
def calc1(self):
self.z=self.x+self.y
def put1(self):
return(self.z)
obj
= test()
print
("Main Program")
a=int(input("Enter
first value :"))
b=int(input("Enter
Second value :"))
obj.get1(a,b)
obj.calc1()
print(obj.put1())
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-----------------------------------------------------
Constructor &
Destructor
-----------------------------------------------------
Constructor:
-----------
-A constructor is a special type of method
(function)
-This Method is used to initialize (assign
values) to the instance variables.
-When an object is created constructor
will be automatically called.
Syntax:
------------------------------
def _ _init_ _(self):
sta-1
sta-2
....
sta-n
----------------------------------------------
Destructor
----------
-A
Destructor is a special type of method (function)
-This
Method is used to destroy (assign values) to the instance variables.
Syntax:
-------------
def _ _del_ _(self):
sta-1
sta-2
....
sta-n
----------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class
test:
def __init__(self):
self.x=int(input("Enter First
Input :"))
self.y=int(input("Enter Second
Input :"))
def __del__(self):
self.z=self.x-self.y
print ("Result is ",self.z)
print ("Delete All objects")
test() #invoke Constructor & Destructor
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 5: Example
Program for constructor & Destructor
class
test:
def __init__(self): #Constructor
self.x=int(input("Enter First
Input :"))
self.y=int(input("Enter Second
Input :"))
def calc(self):
self.z=self.x+self.y
print ("Result is :",self.z)
def __del__(self): #destructor
print ("Delete All objects")
obj
= test()
obj.calc()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Python Polymorphism
----------------------------------------------------
Polymorphism:
------------
-Polymorphism
is based on the Greek Words Poly (many) and morphism (forms or shape).
-We
will create a structure that can take or use many forms of objects
Ex1: +
-integer addition
-float addition
-string concatenation
Ex2: drawing
-line
-circle
-rectangle
Ex: button
-single click
-double click
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ex for polymorphism
1) Function (method) Overloading
2) Operator
Overloading
3) Method Overriding
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function overloading:
--------------------------------
-same name assign to multiple method but passing different arguments
#Program 1: Program for function Overloading
def msg(x=1,y=2,z=3):
print(x+y+z)
msg()
msg(10)
msg(10,20)
msg(10,20,30)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2. Python Operator
Overloading:
------------ ---------------------------
-Python operators work for built-in
classes.
-Operator assign to function name
-For example, the + operator will, perform arithmetic addition on two numbers, merge two lists and concatenate two strings.
----------------------------------------------------------
1)
__add__
2)
__sub__
3)
__mul__
4)
__mod__
4)
__pow__
5)
__and__
6)
__or__
7)
__xor__
8)
__invert__
9)
__str__
Comparison
operator overloading in Python:
------------------------------------------
1)
__lt__
2)
__le__
3)
__eq__
4)
__ne__
5)
__gt__
6)
__ge__
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Example program for operator
overloading
class
test:
def __init__(self): #constructor
self.x=0
self.y=0
def __add__(self):
self.x=10
self.y=20
def __mul__(self):
self.x=-self.x
self.y=-self.y
def display(self):
print("x :",self.x)
print("y :",self.y)
def __del__(self): #destructor
print("Destroy All Objects")
obj=test()
obj.display()
obj.__add__()
obj.display()
obj.__mul__()
obj.display()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
Method Overriding:
------------------------
-Same method name assign to different class
-derived class can have override the base class method
Syntax:
class one:
def
msg(self):
print("base class method")
class two(one):
def msg(self):
print("derived class method")
t1=two()
t1.msg()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Python
Inheritance
Inheritance:
----------------
-Inheritance
is a feature of Object Oriented Programming.
-Deriving
a new class (derived class or child class or sub class) from an old one (Super
class or base class or parent class)
Advantages
-------------
-Derived
class can have access the base class Properties and methods without re-declaration
-Reusability
of code, To Reduced size of our program
-To
Avoid Complexity of our program
-Easy
to find (Bugging) and remove (Debugging) errors
Types of Inheritance
--------------------
1.
Single Inheritance
2.
Multiple Inheritance
3.
Multilevel Inheritance
4.
Hybrid Inheritance
5.
Hierarchical Inheritance
1. Single Inheritance
---------------------
-Its
Consist of one base class and one derived class
-A
single inheritance is when a single class inherits from a class.
Syntax:
-------
class base:
#variable & functions
class derived(base):
#variable &functions
#Example 1: Program for
single inheritance
class
fruit:
def __init__(self):
print("I'm a fruit")
class
citrus(fruit):
def __init__(self):
super().__init__()
print("I'm citrus")
t1=citrus()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Example 2: Program for
single inheritance
class
one:
def get1(self):
self.a=int(input("Enter First value
:"))
self.b=int(input("Enter Second
value :"))
class
two(one):
def out1(self):
super().get1()
self.c=self.a+self.b
print("Result is :",self.c)
t1=two()
t1.out1()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2. Multiple Inheritance
---------------------
-Its
Consist of More than one base class and only one derived class
Syntax:
-------
class base1:
#variable
#functions
class base2:
#variable
#functions
class derived(base1,base2):
#variable
#functions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Example 1: Program for
Multiple Inheritance
class
base1:
def get1(self):
self.a=int(input("Enter First value
:"))
class
base2:
def get2(self):
self.b=int(input("Enter Second
value :"))
class
derived(base1,base2):
def out1(self):
super().get1()
super().get2()
self.c=self.a+self.b
print("Result is :",self.c)
t1=derived()
t1.out1()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3. Multilevel Inheritance
---------------------
-When
one class inherits from another, which in turn inherits from another,
it is multilevel python inheritance.
Syntax:
-------
class one:
#variable & functions
class two(one):
#variable
#functions
class three(two):
#variable & functions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Example 1: Program for
Multilevel Inheritance
class
one:
def get1(self):
self.a=int(input("Enter First value
:"))
class
two(one):
def get2(self):
super().get1()
self.b=int(input("Enter Second
value :"))
class
three(two):
def out1(self):
super().get2()
self.c=self.a+self.b
print("Result is :",self.c)
t1=three()
t1.out1()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4. Hierarchical Inheritance
--------------------------------
-Only
one base class but more than one derived class
Syntax:
-------
class one:
#variable & functions
class two(one):
#variable & functions
class three(one):
#variable & functions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Example 1: Program for
Hierarchical Inheritance
class
one:
def get1(self):
self.a=int(input("Enter First value
:"))
self.b=int(input("Enter Second
value :"))
class
two(one):
def add(self):
super().get1()
self.c=self.a+self.b
print("Addition is :",self.c)
class
three(one):
def sub(self):
super().get1()
self.c=self.a-self.b
print("Subtraction is
:",self.c)
t1=two()
t1.add()
t2=three()
t2.sub()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5. Hybrid Inheritance
---------------------
-Combine
Hierarchical and Multiple Inheritance
Syntax:
-------
class one:
#variable & functions
class two(one):
#variable & functions
class three(one):
#variable & functions
class four(two,three):
#variable & functions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Example 1: Program for
Hybrid Inheritance (Hierarchical + Multiple)
class
one:
def get1(self):
self.a=int(input("Enter First value
:"))
self.b=int(input("Enter Second
value :"))
class
two(one):
def add(self):
super().get1()
self.c=self.a+self.b
print("Addition is :",self.c)
class
three(one):
def sub(self):
super().get1()
self.c=self.a-self.b
print("Subtraction is
:",self.c)
class
four(one):
def mul(self):
super().get1()
self.c=self.a*self.b
print("Multiplication is
:",self.c)
class
five(one):
def div(self):
super().get1()
self.c=self.a/self.b
print("Division is :",self.c)
class
six(one):
def rem(self):
super().get1()
self.c=self.a%self.b
print("Remainder is :",self.c)
class
derived (two, three, four, five, six):
def output(self):
super().add()
super().sub()
super().mul()
super().div()
super().rem()
t1=derived
t1.output()
~~~~~~~~~~~~~~~~~~~~~~~~~~~
No comments:
Post a Comment