Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Python_OOPs

Uploaded by

helloworldw793
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python_OOPs

Uploaded by

helloworldw793
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Python

OOPS Using Python


• Classes and objects: class methods and self arguments, the
_init_(), class variable and object variable, _del_(),public and
private data members, calling a class method from another class
• method, builtin funcitons to set,get and delete class attributes
Inheritance, types, composition or containership, abstract classes
or interfaces
• Operator overloading: implementing Operator overloading,
reverse adding, overriding _getitem_() and _setitem_() methods,
overriding the in operator, overloading the misc functions

Instructor: Charulata Palai


Python
OOPs (Object-Oriented Programming System)

• The real world entity that has state and behavior is known
as an object e.g. chair, bike, pen, table, car, mobile,bird
etc.
• Object is an instance of a class.
• An object has three characteristics:
state: represents data (value) of an object.
behavior: represents the behavior (functionality) of an object.
identity: Object identity Name or ID.
• Object-Oriented Programming is a methodology or paradigm
to design a program using classes and objects
Instructor: Charulata Palai
Python

Instructor: Charulata Palai


Python

Object in Python
• A class is a template or blueprint from which objects
are created. So, an object is the instance(result) of a
class.
• Object Definitions:
– An object is a real-world entity.
– An object is a runtime entity which has state and
behavior.
– The object is an instance of a class.

Instructor: Charulata Palai


Python
Class in Python

Instructor: Charulata Palai


Python
Class in Python

• A class is a group of objects which have


common properties.
• It is a template or blueprint from which objects
are created. It is a logical entity.
• A class in Python can contain:
• fields
• methods
• constructors
• blocks
• nested class and interface
Instructor: Charulata Palai
Python

Instructor: Charulata Palai


Python Instance variable in Python

• A variable which is created inside the class but outside


the method is known as an instance variable.
• It gets memory at runtime when an object or instance is
created.
• In python, a method is like a function which is used to
expose the behavior of an object.
• No need of new keyword to allocate memory at
runtime.

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python
Example
• Create a Python class named Circle constructed by
a radius and two methods which will compute the
area and the perimeter of a circle.
• Create a Python class named Rectangle constructed
by a length and width and a method which will
compute the area of a rectangle

Instructor: Charulata Palai


Python
Rectangle class
class Rectangle:
def __init__(self, l,w):
self.l = l
self.w = w
def area(self):
return self.w * self.l
length = int(input("Enter the length of the rectangle: "))
width = int(input("Enter the width of the rectangle: "))
obj = Rectangle(length, width)
print('Area of the rectangle: ', obj.area())

Instructor: Charulata Palai


Python
Circle Class
import math
class Circle:
def __init__(self, r):
self.r = r
def area(self):
return math.pi * (self.r ** 2)
def perimeter(self):
return 2 * math.pi * self.r
radius = float(input("Enter the radius of the circle: "))
obj = Circle(radius)
print('Area =' , obj.area())
Instructor: Charulata Palai
Python
Example
Create a Python class named Employee with data
member’s id, name, salary and display n number of
employee details using list of object.

Python Program using classes and objects to deposit


and withdraw money in a Bank Account

Instructor: Charulata Palai


Python
Employee Class
class Employee:
def __init__(self, id, name, salary):
self.id = id
self.name = name
self.salary = salary
n = int(input('Enter number of employees: '))
employees = []
for i in range(n):
print(f'Enter {i + 1} Employee Details:')
id = int(input('Enter Employee ID: '))
name = input('Enter Employee Name: ')
salary = int(input('Enter Employee Salary: '))
employees.append(Employee(id, name, salary))
for employee in employees:
print(f'{employee.id} : {employee.name} : {employee.salary}')

Instructor: Charulata Palai


Python
Bank class
class Bank:
def __init__(self, name):
self.name = name
self.balance = 0
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
def check_balance(self):
print('Current Balance: ', self.balance)
obj = Bank('Abcd')
obj.deposit(500)
obj.check_balance()
obj.withdraw(350)
obj.check_balance()

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

WAP to define a Person class having data members


regno,rollno and college_name.The Student class extending
from Person class which having branch as its own instance
variables. Define suitable constructors and display the
properties of Student class object with suitable methods.

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python
Multilevel Inheritance

• Define a class point, inherit class line from point,


rectangle from line, and cube from rectangle. Write a
print statement in these constructors mentioning which
class it is. Create an object of the cube class and
implements method overriding of show() in each class

Instructor: Charulata Palai


Python
Multilevel Inheritance

class Point(object):
def __init__(self):
print("Point Const")
class Line(Point):
def __init__(self):
Point.__init__(self)
print("Line Const")
class Rectangle(Line):
def __init__(self):
Line.__init__(self)
print("Rectangle Const")
class Cube(Rectangle):
def __init__(self):
Rectangle.__init__(self)
print("Cube Const")

c = Cube()
Instructor: Charulata Palai
Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Create a student class with member function getdata() and


show(). Create derived classes GradStudent and UGStudent
from the student class and override both the member
functions of the class. Student class must have
rollno,name,branch fields. GradStudent must have an extra
field specialization and UGStudent should have an extra
field Elective that stores the name of the elective paper opted
for.

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python
interfaces Operator overloading

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

Instructor: Charulata Palai


Python

THANK
YOU
Instructor: Charulata Palai

You might also like