OOPS in Python:-
Object-Oriented Programming Structure, it is used to create a real-world based application using class and object.
OOP'S programming is based on Object because we will define the characteristics and behavior of Objects using class.
OOP provides better security, reusability, modularity, extendibility, usability, and accessibility features in real-world applications.
OOP provides individual memory allocation to individual objects for individual users.
Rule's of OOP'S:-
1 Class and Object:-
Class:-
It is user define datatype which is used to define the characteristics of an object using data member (variable) and member function(method).class is a blueprint of an object, we can define multiple objects definition underclass but all object should be related with each other.
class Classname:
def methodname(self):
statements
statements
...
...
ref = Classname() #object
ref.methodname() # calling of method
self is a keyword that is used to contain the address of the current object.
Example of a Student object that represents the characteristics of Student using rno and name that has been defined as to accept() and display().
class Student:
def accept(self):
self.rno= input("Enter rno")
self.sname= input("Enter name")
def display(self):
print("Rno is ",self.rno, " name is ",self.sname)
obj = Student()
obj.accept()
obj.display()
obj1 = Student()
obj1.accept()
obj1.display()
Object:-
It is a real-world entity that has identity, state, and behavior. Identity provides the address of the object which will be unique. Memory allocation of objects is used to specify the state, and the calling of the method will be defined as the behavior of the object.
"for example apple, orange these are the objects which will be defined by Fruit class".
if we want to create Object by python script:-
ref = Classname()
ref = Student()
Example of Class and Object?
class Employee:
def accept(self,empid,empname,job,salary):
self.empid=empid
self.empname=empname
self.job=job
self.salary=salary
def display(self):
print("ID is "+str(self.empid))
print("name is "+str(self.empname))
print("job is "+str(self.job))
print("salary is "+str(self.salary))
obj = Employee()
obj.accept(1001,"XYZ","CLEARK",15000)
obj.display()
obj1 = Employee()
obj1.accept(1002,"MNO","MANAGER",25000)
obj1.display()
................................................................................................................................
Program to accept and display student record:-
class Student:
def accept(self,rno,sname,branch,fees):
self.rno=rno
self.sname=sname
self.branch=branch
self.fees=fees
def display(self):
print("rno is "+str(self.rno) + " name is "+self.sname + " branch is " + self.branch +" fees is "+str(self.fees));
obj = Student()
obj.accept(1001,"xyz","CS",45000)
obj.display()
obj1 = Student()
obj1.accept(1002,"mno","IT",70000)
obj1.display()
Component of class:-
1 Data member or variable:-
It is used to define attributes of the object for example Employee object contains Empid, Empname, job, etc as an attribute of this, it will be declared as a variable.
We can define a data member using two different types:-
1.1) class type or static:- it will be declared underclass or static method, this type of variable use class memory to store data.
static variable memory will be fixed means it has a single memory on the complete program.
The static variable will call using Classname no need to create an object.
class Classname:
a=10 #class type
Example of Static Data Member in Python:-
class A:
a=100 #class type or static
b=200
c = A.a+A.b
print(c)
#WAP to calculate Simple Interest using static data member only
class SI:
p,r,t = 140000,2,2 #class type
res = (SI.p* SI.r* SI.t)/100
print(res)
1.2) instance type or dynamic:- It will be declared by self keyword under dynamic method
class Classname:
def methodname(self):
self.a=100 #instance type
Example of Instance variable:-
class A:
def fun(self,a,b):
self.a=a #instance type or dynamic
self.b=b
def display(self):
print(self.a, self.b)
obj = A()
obj.fun(10,20)
obj.display()
obj1 = A()
obj1.fun(30,40)
obj1.b=500
obj1.display()
#WAP to calculate Simple Interest using dynamic data member only
class SI:
def fun(self,p,r,t):
self.p,self.r,self.t = p,r,t
res = (self.p* self.r* self.t)/100
print(res)
obj = SI()
obj.fun(450000,2,2)
Program Solution:-
class Datamemberexample:
a=100 #class type
def fun(self):
self.b=200 #reference type
print(self.b)
obj = Datamemberexample()
obj.fun()
print(Datamemberexample.a) #class name
2 Member function or function:-
It is used to define data member of the class to implement data encapsulation
Type of member function
2.1 ) class type or static:-
It is used to define class type variables in the program.
class Classname:
def Methodname():
statements
statements
Classname.Methodname()
It has only a one-time memory allocation in the program because it will use the Class memory of the program.
Example of Static:-
class StaticFunction:
def fun():
a=int(input("enter first number"))
b=int(input("enter second number"))
c=a+b
print(c)
StaticFunction.fun()
StaticFunction.fun()
2.2) Instance type or dynamic:-
this type of member function will be declared using the self keyword and called by object .it will be allocated individual memory for the different objects because it has a unique address for Object.
class DynamicFunction:
def fun(self):
a=int(input("enter first number"))
b=int(input("enter second number"))
c=a+b
print(c)
obj=DynamicFunction()
obj.fun()
obj1=DynamicFunction()
obj1.fun()
It is used to define data member of the class to implement data encapsulation
Type of member function
2.1 ) class type or static:-
It is used to define class type variables in the program.
class Classname:
def Methodname():
statements
statements
Classname.Methodname()
It has only a one-time memory allocation in the program because it will use the Class memory of the program.
Example of Static:-
class StaticFunction:
def fun():
a=int(input("enter first number"))
b=int(input("enter second number"))
c=a+b
print(c)
StaticFunction.fun()
StaticFunction.fun()
2.2) Instance type or dynamic:-
this type of member function will be declared using the self keyword and called by object .it will be allocated individual memory for the different objects because it has a unique address for Object.
class DynamicFunction:
def fun(self):
a=int(input("enter first number"))
b=int(input("enter second number"))
c=a+b
print(c)
obj=DynamicFunction()
obj.fun()
obj1=DynamicFunction()
obj1.fun()
Standard Program Examle using OOPS:-
class SI:
def accept(self,p,r,t):
self.p=p
self.r=r
self.t=t
def logic(self):
self.si = (self.p*self.r*self.t)/100
def display(self):
print("Result is ",self.si)
obj = SI()
p = float(input("Enter amount"))
r = float(input("Enter rate"))
t = float(input("Enter time"))
obj.accept(p,r,t)
obj.logic()
obj.display()
3 Constructor or Init Method:-
It is a special component of the class that will be called when we create an object. A constructor is used to initializing the dynamic data member of the class.
In Python constructor will be declared by __init__() method,
class Consdemo:
def __init__(self):
statements
Example of Constructor:-
class SI:
def __init__(self,p,r,t):
self.p=p
self.r=r
self.t=t
def logic(self):
self.si = (self.p*self.r*self.t)/100
def display(self):
print("Result is ",self.si)
p = float(input("Enter amount"))
r = float(input("Enter rate"))
t = float(input("Enter time"))
obj = SI(p,r,t)
obj.logic()
obj.display()
1 Single parameter:-
we can pass only self as a parameter to a single parameter constructor
class A:
def __init__(self):
statements
2 Multiple Parameter:-
We can pass multiple values as an argument to multiple parameter constructor.
class A:
def __init__(self,a,b):
self.a=10
self.b=20
Note: we should create only one type of constructor in the program because only one constructor will be called.
The constructor is only for data member initialization not for functionality.
4 Destructor or Delete Method:-
This method will be called when we destroy or delete the object
It is used to remove dynamic memory space in the program.
this block by default created by class, if we want to implement any functionality then we can manually re-create the destructor.
Complete Program Explanation of Constructor and Destructor:-
class Addition:
def __init__(self):
self.a=100
self.b=200
def __init__(self,a,b):
self.a=a
self.b=b
def add(self):
print("result is "+str(self.a+self.b))
def __del__(self):
print("Destructor will be called")
obj = Addition(10,20)
obj.add()
del obj
2 Data Abstraction and Data Encapsulation:-
Data abstraction:-
To show essential details and hide non-essential details in the program is managed by data abstraction.
it is used to provide data hiding from end-users in an application.
It is used to implement security and accessibility features in the program.
How we implement Data abstraction practically:-
we will use access specifier, python support access specifier using the symbol
if we use __ private (accessed in the same class, means it can not access outside of the class)
if we use _ protected (accessed from base class to derived class )
if we use without symbol then public (accessed in all classes by default python member function is public)
class Classname:
def __fun(self):
print("Private")
def _fun2(self):
print("Protected")
def fun3(self):
print("public")
Example of Data abstraction in python:-
class A:
def __fun(self):
print("Private")
def _fun2(self):
print("Protected")
def fun3(self):
self.__fun()
print("public")
obj = A()
obj.fun3()
Example of Data abstraction using Bank Class With Credit, Debit, and Login Functions.
class Bank:
balance=500000
def __debit(self,amount): #private method
self.balance-=amount
print("Current balance is ",self.balance)
print("Debit Amount is ",amount)
def __credit(self,amount): #private method
self.balance+=amount
print("Current balance is ",self.balance)
print("Credit Amount is ",amount)
def login(self,pin): #public method
if pin==12345:
self.__credit(int(input("enter amount")))
self.__debit(int(input("enter amount")))
obj = Bank()
obj.login(12345)
Data Encapsulation:-
It is used to bind all the details using a single unit. if we declare data members under the member function then it is called data encapsulation. we will use a data binding principal to implement encapsulation functionality.
In the python script, we can not access any instance type data member directly .instance type variable will be defined by self keyword under member function.
class A:
a=1000 #class type
def fun(self):
a=10 #local instance type
print(A.a) #call class type
print(a) #call local instance type
self.a=100
def fun1(self):
print(self.a) #call self type
print(A.a) #call class type
obj = A()
obj.fun()
obj.fun1()
3 Polymorphism:-
Poly means many and morph-ism means forms, using this we will create the same name method or operator which will be implemented into multiple forms.
function overloading, operator overloading and function overriding is the practical example of Polymorphism.
Function Overloading is not managed by python script because we can define multiple functions but call only one function at a time.
Operator overloading:- using this we can reload or redefine operator definition. A single operator can be used in multiple forms using an operator overloading concept.
python use predefined method to implement operator definition:-
1 function __add__(self):
statements
2 function __sub__(self):
statements
3 function __mul__(self):
statements
4 function __truediv__(self):
statements
5 function __floordiv__(self):
statements
Operator overloading example in Python using + Operator:-
class Ope:
def __init__(self,num):
self.num=num
def __add__(self,other):
return Ope(self.num*other.num)
def __str__(self): #TO RETURN VALUE OF CURRENT OBJECT WHEN WE PRINT(OBJ) THEN IT CALL
return "result is "+str(self.num)
obj = Ope(100)
obj1 = Ope(200)
obj3 = obj + obj1 #__add__(obj,obj1)
print(obj3)
4 Inheritance:-
It is used to provide re-usability means we can adapt the features of base class to the derived class.
Type of Inheritance:-
4.1) Single Inheritance:-
From Base Class to Derived Class
class A:
methods
class B(A):
methods
4.2) Multilevel Inheritance:-
From base class to derived class and derived class to sub derived class.
class A:
methods
class B(A):
methods
class C(B):
methods
class A:
def fun(self):
print("A")
class B(A):
def fun1(self):
super().fun()
print("B")
class C(B):
def fun2(self):
super().fun1()
print("C")
obj = C()
#obj.fun()
#obj.fun()
#obj.fun1()
obj.fun2()
4.3) Hierarchical Inheritance:-
One base class to different child class
class A:
methods
class B(A):
methods
class C(A):
methods
4.4) Multiple Inheritance:-
Two base classes in one child class
class A:
methods
class B:
methods
class C(A, B):
methods
Example of Single Inheritance:-
ADMIN ------> EMPLOYEE
class Admin:
def accept(self,id,name):
self.id=id
self.name=name
def display(self):
print("ID is "+str(self.id)+" name is "+str(self.name))
class Employee(Admin):
def accept1(self,sal):
self.sal=sal
def display1(self):
print("Salary is "+str(self.sal))
print("Employee Information")
obj = Employee()
obj.accept(1001,"emp")
obj.accept1(12000)
obj.display()
obj.display1()
print("Admin Info")
obj1 = Admin()
obj1.accept(1000,"admin")
obj1.display()
Example of Multilevel Inheritance:-
ADMIN -----> EMPLOYEE------> OTHERSTAFF
class Admin:
def accept(self,id,name):
self.id=id
self.name=name
def display(self):
print("ID is "+str(self.id)+" name is "+str(self.name))
class Employee(Admin):
def accept1(self,sal):
self.sal=sal
def display1(self):
print("Salary is "+str(self.sal))
print("Employee Information")
obj = Employee()
obj.accept(1001,"emp")
obj.accept1(12000)
obj.display()
obj.display1()
print("Admin Info")
obj1 = Admin()
obj1.accept(1000,"admin")
obj1.display()
ADMIN -----> EMPLOYEE------> OTHERSTAFF
class Admin:
def accept(self,id,name):
self.id=id
self.name=name
def display(self):
print("ID is "+str(self.id)+" name is "+str(self.name))
class Employee(Admin):
def accept1(self,sal):
self.sal=sal
def display1(self):
print("Salary is "+str(self.sal))
class Otherstaff(Employee):
def accept2(self,bonus):
self.bonus=bonus
def display2(self):
print("Bonus is "+str(self.bonus))
print("Employee Information")
obj = Employee()
obj.accept(1001,"emp")
obj.accept1(12000)
obj.display()
obj.display1()
print("Admin Info")
obj1 = Admin()
obj1.accept(1000,"admin")
obj1.display()
print("Other Staff Info")
obj2 = Otherstaff()
obj2.accept(1002,"otherstaff")
obj2.accept1(12000)
obj2.accept2(200)
obj2.display()
obj2.display1()
obj2.display2()
Example of Hierarchical Inheritance:-
ADMIN
EMPLOYEE OTHERSTAFF
class Admin:
def accept(self,id,name):
self.id=id
self.name=name
def display(self):
print("ID is "+str(self.id)+" name is "+str(self.name))
class Employee(Admin):
def accept1(self,sal):
self.sal=sal
def display1(self):
print("Salary is "+str(self.sal))
class Admin:
def accept(self,id,name):
self.id=id
self.name=name
def display(self):
print("ID is "+str(self.id)+" name is "+str(self.name))
class Employee(Admin):
def accept1(self,sal):
self.sal=sal
def display1(self):
print("Salary is "+str(self.sal))
class Otherstaff(Admin):
def accept2(self,bonus):
self.bonus=bonus
def display2(self):
print("Bonus is "+str(self.bonus))
print("Employee Information")
obj = Employee()
obj.accept(1001,"emp")
obj.accept1(12000)
obj.display()
obj.display1()
print("Admin Info")
obj1 = Admin()
obj1.accept(1000,"admin")
obj1.display()
print("Other Staff Info")
obj2 = Otherstaff()
obj2.accept(1002,"otherstaff")
#obj2.accept1(12000)
obj2.accept2(200)
obj2.display()
#obj2.display1()
obj2.display2()
..................................................................................
Example of Multiple Inheritance:-
Example of Multiple Inheritance:-
ADMIN EMPLOYEE
OTHERSTAFF
class Admin:
def fun(self):
print("Admin")
def accept(self,id,name):
self.id=id
self.name=name
def display(self):
print("ID is "+str(self.id)+" name is "+str(self.name))
class Employee:
def fun(self):
print("Employee")
def accept1(self,sal):
self.sal=sal
def display1(self):
print("Salary is "+str(self.sal))
class Otherstaff(Admin,Employee):
def accept2(self,bonus):
self.bonus=bonus
def display2(self):
print("Bonus is "+str(self.bonus))
print("Employee Information")
obj = Employee()
#obj.accept(1001,"emp")
obj.accept1(12000)
#obj.display()
obj.display1()
print("Admin Info")
obj1 = Admin()
obj1.accept(1000,"admin")
obj1.display()
print("Other Staff Info")
obj2 = Otherstaff()
obj2.fun()
obj2.accept(1002,"otherstaff")
obj2.accept1(12000)
obj2.accept2(200)
obj2.display()
obj2.display1()
obj2.display2()
Inheritance Example for Area of the circle, triangle, and rectangle:-
class Circle:
def accept(self,param1):
self.param1=param1
def areaofcirlce(self):
self.area = 3.14*self.param1*self.param1
def display(self):
print("Area is "+str(self.area))
class Triangle(Circle):
def accept1(self,param2):
self.param2=param2
def areaoftriangle(self):
self.area = (self.param1*self.param2)/2
class Rect(Triangle):
def areaofrectangle(self):
self.area = (self.param1*self.param2)
print("Area of the circle")
obj = Circle()
obj.accept(12)
obj.areaofcirlce()
obj.display()
print("Area of Triangle")
obj1 =Triangle()
obj1.accept(12)
obj1.accept1(2)
obj1.areaoftriangle()
obj1.display()
print("Area of Rectangle")
obj2 =Rect()
obj2.accept(12)
obj2.accept1(2)
obj2.areaofrectangle()
obj2.display()
Function Overriding:-
we can create the same name method from base class to derived class only functionality will be different.
no need to use any keyword for function overriding, it will be implemented automatically.
class A:
def fun(self):
print("A")
class B(A):
def fun(self):
print("B")
obj = B()
obj.fun()
Example of Inheritance:-
class Course:
def accept(self,courseid,coursename):
self.courseid=courseid
self.coursename=coursename
def display(self):
print("courseid is"+str(self.courseid) + "coursename is "+self.coursename)
class Student(Course):
def accept1(self,name):
self.name=name
def display1(self):
print("name is "+self.name)
obj = Course()
obj.accept(1001,"Java")
obj.display()
obj1 = Student()
obj1.accept(1002,".NET")
obj1.accept1("Manish")
obj1.display()
obj1.display1()
Assignment:-
1) Manage Patient, Doctor, and Appointment using all possible Inheritance?
2) Manage Coach, Team, and Player using all possible Inheritance?
3) Manage Branch, Location, and Institute using all possible Inheritance
How to call class under Module?
we can call module using two ways
1) import filename
2) from filename import Classname
Practice Example:-
1) Create First StrExample.py
class A:
def __init__(self):
self.a=10
def __str__(self):
return str(self.a)
2) Call this module into another file
#import StrExample
from StrExample import A
obj = A()
print(obj)
doubt-not able to understand this question
ReplyDeleteclass Dog:
def __init__(self, name, age):
self.name = name
self.age = age
The correct way to instantiate the above Dog class is:
class Dog:
ReplyDelete... def walk(self):
... return "*walking*"
...
... def speak(self):
... return "Woof!"
...
>>> class JackRussellTerrier(Dog):
... def speak(self):
... return "Arff!"
...
>>> bobo = JackRussellTerrier()
>>> bobo.speak()
output of this program is Arff could not understand
class Dog:
ReplyDeletedef __init__(self, name, age):
self.name = name
self.age = age
class JackRussellTerrier(Dog):
pass
class Dachshund(Dog):
pass
class Bulldog(Dog):
pass
miles = JackRussellTerrier("Miles", 4)
buddy = Dachshund("Buddy", 9)
jack = Bulldog("Jack", 3)
jim = Bulldog("Jim", 5)
output-isinstance(miles,bulldog)
class Person:
ReplyDeletedef __init__(self, id):
self.id = id
sam = Person(100)
sam.__dict__['age'] = 49
print (sam.age + len(sam.__dict__))
output is 51 how please explain it
#Manage Patient, Doctor, and Appointment using all possible Inheritance?
ReplyDelete#single inheritance
class Doctor:
def Doctorinfo(self,specality,location):
self.specality=specality
self.location=location
def display(self):
print("specality"+str(self.specality),"location"+str(self.location))
class Patient(Doctor):
def Patientinfo(self,id,name,age):
self.id=id
self.name=name
self.age=age
def display1(self):
print("id is"+str(self.id),"name"+str(self.name),"age"+str(self.age))
class Appointement(Patient):
def appoint(self,time):
self.time=time
def display2(self):
print("time is "+str(self.time))
print("doctor info")
obj=Doctor()
obj.Doctorinfo("surgoen","indore")
obj.display()
print("patient info")
obj1=Patient()
obj1.Patientinfo(101,"abc",40)
obj1.Doctorinfo("neurosurgeon","bhopal")
obj1.display()
obj1.display1()
print("appointement info")
obj2=Appointement()
obj2.Doctorinfo("eyespecalist","india")
obj2.Patientinfo(102,"xyz",40)
obj2.appoint("6pm")
obj2.display()
obj2.display1()
obj2.display2()
#Manage Patient, Doctor, and Appointment using all possible Inheritance?
ReplyDelete# Hierarchical inheritance
class Doctor:
def Doctorinfo(self,specality,location):
self.specality=specality
self.location=location
def display(self):
print("specality"+str(self.specality),"location"+str(self.location))
class Patient(Doctor):
def Patientinfo(self,id,name,age):
self.id=id
self.name=name
self.age=age
def display1(self):
print("id is"+str(self.id),"name"+str(self.name),"age"+str(self.age))
class Appointement(Doctor):
def appoint(self,time):
self.time=time
def display2(self):
print("time is "+str(self.time))
print("doctor info")
obj=Doctor()
obj.Doctorinfo("surgoen","indore")
obj.display()
print("patient info")
obj1=Patient()
obj1.Patientinfo(101,"abc",40)
obj1.Doctorinfo("neurosurgeon","bhopal")
obj1.display()
obj1.display1()
print("appointement info")
obj2=Appointement()
obj2.Doctorinfo("eyespecalist","india")
obj2.appoint("6pm")
obj2.display()
obj2.display2()
#Manage Patient, Doctor, and Appointment using all possible Inheritance?
ReplyDelete# multiple inheritance
class Doctor:
def Doctorinfo(self,specality,location):
self.specality=specality
self.location=location
def display(self):
print("specality"+str(self.specality),"location"+str(self.location))
class Patient():
def Patientinfo(self,id,name,age):
self.id=id
self.name=name
self.age=age
def display1(self):
print("id is"+str(self.id),"name"+str(self.name),"age"+str(self.age))
class Appointement(Doctor,Patient):
def appoint(self,time):
self.time=time
def display2(self):
print("time is "+str(self.time))
print("doctor info")
obj=Doctor()
obj.Doctorinfo("surgoen","indore")
obj.display()
print("patient info")
obj1=Patient()
obj1.Patientinfo(101,"abc",40)
obj1.display1()
print("appointement info")
obj2=Appointement()
obj2.Doctorinfo("eyespecalist","india")
obj2.Patientinfo(101,"xyz",50)
obj2.appoint("6pm")
obj2.display()
obj2.display1()
obj2.display2()
#Manage Coach, Team, and Player using all possible Inheritance?
ReplyDelete#multilevel inheritance
class Team:
def Teaminfo(self,country,group):
self.country=country
self.group=group
def display(self):
print("country"+str(self.country),"group"+str(self.group))
class Coach(Team):
def Coachinfo(self,id,name,age):
self.id=id
self.name=name
self.age=age
def display1(self):
print("id is"+str(self.id),"name"+str(self.name),"age"+str(self.age))
class Player(Coach):
def Playerinfo(self,age):
self.age=age
def display2(self):
print("age of player "+str(self.age))
print("Team info")
obj=Team()
obj.Teaminfo("india","A")
obj.display()
print("coach info")
obj1=Coach()
obj1.Coachinfo(101,"abc",40)
obj1.Teaminfo("austraila","B")
obj1.display()
obj1.display1()
print("player info")
obj2=Player()
obj2.Teaminfo("eyespecalist","india")
obj2.Coachinfo(102,"xyz",20)
obj2.Playerinfo(20)
obj2.display()
obj2.display1()
obj2.display2()
#Manage Coach, Team, and Player using all possible Inheritance?
ReplyDelete#heiarchial inheritance
class Team:
def Teaminfo(self,country,group):
self.country=country
self.group=group
def display(self):
print("country"+str(self.country),"group"+str(self.group))
class Coach(Team):
def Coachinfo(self,id,name,age):
self.id=id
self.name=name
self.age=age
def display1(self):
print("id is"+str(self.id),"name"+str(self.name),"age"+str(self.age))
class Player(Team):
def Playerinfo(self,age):
self.age=age
def display2(self):
print("age of player "+str(self.age))
print("Team info")
obj=Team()
obj.Teaminfo("india","A")
obj.display()
print("coach info")
obj1=Coach()
obj1.Coachinfo(101,"abc",40)
obj1.Teaminfo("austraila","B")
obj1.display()
obj1.display1()
print("player info")
obj2=Player()
obj2.Teaminfo("eyespecalist","india")
obj2.Playerinfo(30)
obj2.display()
obj2.display2()
#Manage Coach, Team, and Player using all possible Inheritance?
ReplyDelete#multiple inheritance
class Team:
def Teaminfo(self,country,group):
self.country=country
self.group=group
def display(self):
print("country"+str(self.country),"group"+str(self.group))
class Coach():
def Coachinfo(self,id,name,age):
self.id=id
self.name=name
self.age=age
def display1(self):
print("id is"+str(self.id),"name"+str(self.name),"age"+str(self.age))
class Player(Team,Coach):
def Playerinfo(self,age):
self.age=age
def display2(self):
print("age of player "+str(self.age))
print("Team info")
obj=Team()
obj.Teaminfo("india","A")
obj.display()
print("coach info")
obj1=Coach()
obj1.Coachinfo(101,"abc",40)
obj1.display1()
print("player info")
obj2=Player()
obj2.Teaminfo("india","C")
obj2.Coachinfo(103,"pakistan",20)
obj2.Playerinfo(30)
obj2.display()
obj2.display1()
obj2.display2()
#Manage Branch, Location, and Institute using all possible Inheritanc
ReplyDelete#multilevel inheritance
class Insitute:
def insituteinfo(self,name,location):
self.name=name
self.location=location
def display(self):
print("specality"+str(self.name),"location"+str(self.location))
class Branch(Insitute):
def branchinfo(self,id,name,pincode):
self.id=id
self.name=name
self.pincode=pincode
def display1(self):
print("id is"+str(self.id),"name"+str(self.name),"age"+str(self.pincode))
class location(Branch):
def locatinfo(self,address):
self.address=address
def display2(self):
print("adress is "+str(self.address))
print("insitute info")
obj=Insitute()
obj.insituteinfo("ips","indore")
obj.display()
print("branch info")
obj1=Branch()
obj1.branchinfo(101,"abc",453)
obj1.insituteinfo("ibp","bhopal")
obj1.display()
obj1.display1()
print("location info")
obj2=location()
obj2.insituteinfo("lnct","india")
obj2.branchinfo(102,"xyz",407)
obj2.locatinfo("anfsdjf")
obj2.display()
obj2.display1()
obj2.display2()
#Manage Branch, Location, and Institute using all possible Inheritanc
ReplyDelete#Hierarchica inheritance
class Insitute:
def insituteinfo(self,name,location):
self.name=name
self.location=location
def display(self):
print("specality"+str(self.name),"location"+str(self.location))
class Branch(Insitute):
def branchinfo(self,id,name,pincode):
self.id=id
self.name=name
self.pincode=pincode
def display1(self):
print("id is"+str(self.id),"name"+str(self.name),"age"+str(self.pincode))
class location(Insitute):
def locatinfo(self,address):
self.address=address
def display2(self):
print("adress is "+str(self.address))
print("insitute info")
obj=Insitute()
obj.insituteinfo("ips","indore")
obj.display()
print("branch info")
obj1=Branch()
obj1.branchinfo(101,"abc",453)
obj1.insituteinfo("ibp","bhopal")
obj1.display()
obj1.display1()
print("location info")
obj2=location()
obj2.insituteinfo("lnct","india")
obj2.locatinfo("anfsdjf")
obj2.display()
obj2.display2()
#Manage Branch, Location, and Institute using all possible Inheritanc
ReplyDelete#multiple inheritance
class Insitute:
def insituteinfo(self,name,location):
self.name=name
self.location=location
def display(self):
print("specality"+str(self.name),"location"+str(self.location))
class Branch:
def branchinfo(self,id,name,pincode):
self.id=id
self.name=name
self.pincode=pincode
def display1(self):
print("id is"+str(self.id),"name"+str(self.name),"age"+str(self.pincode))
class location(Insitute,Branch):
def locatinfo(self,address):
self.address=address
def display2(self):
print("adress is "+str(self.address))
print("insitute info")
obj=Insitute()
obj.insituteinfo("ips","indore")
obj.display()
print("branch info")
obj1=Branch()
obj1.branchinfo(101,"abc",453)
obj1.display1()
print("location info")
obj2=location()
obj2.insituteinfo("lnct","india")
obj2.branchinfo(101,"sad",20)
obj2.locatinfo("anfsdjf")
obj2.display()
obj2.display2()
Program of Multilevel inheritance for Doctor,Patient,Appointment-->>
ReplyDeleteclass Doctor:
def accept(self,id,name):
self.id=id
self.name=name
def display(self):
print("Id is"+str(self.id)+"Name is"+str(self.name))
class Patient(Doctor):
def accept1(self,name):
self.name=name
def display1(self):
print("Name is"+str(self.name))
class Appointment(Patient):
def accept2(self,time):
self.time=time
def display2(self):
print("Time is"+str(self.time))
print("Doctor information")
obj=Doctor()
obj.accept(420,"Robert")
obj.display()
print("Patient information")
obj1=Patient()
obj1.accept1("Alexander")
obj1.display1()
print("Appointment time")
obj2=Appointment()
obj2.accept2("11 AM")
obj2.display2()
Program of Hierarchical inheritance for Doctor,patient,Appointment-->>
ReplyDeleteclass Doctor:
def accept(self,id,name):
self.id=id
self.name=name
def display(self):
print("Id is"+str(self.id)+"Name is"+str(self.name))
class Patient(Doctor):
def accept1(self,name):
self.name=name
def display1(self):
print("Name is"+str(self.name))
class Appointment(Doctor):
def accept2(self,time):
self.time=time
def display2(self):
print("Time is"+str(self.time))
print("Doctor information")
obj=Doctor()
obj.accept(420,"Robert")
obj.display()
print("Patient information")
obj1=Patient()
obj1.accept1("Alexander")
obj1.display1()
print("Appointment time")
obj2=Appointment()
obj2.accept2("11 AM")
obj2.display2()
Program of multiple inheritance for Doctor,Patient,Appointment-->>
ReplyDeleteclass Doctor:
def fun(self):
print("Doctor")
def accept(self,id,name):
self.id=id
self.name=name
def display(self):
print("Id is"+str(self.id)+"Name is"+str(self.name))
class Patient:
def fun(self):
print("Patient")
def accept1(self,name):
self.name=name
def display1(self):
print("Name is"+str(self.name))
class Appointment(Doctor,Patient):
def accept2(self,time):
self.time=time
def display2(self):
print("Time is"+str(self.time))
print("Doctor information")
obj=Doctor()
obj.accept(420,"Robert")
obj.display()
print("Patient information")
obj1=Patient()
obj1.accept1("Alexander")
obj1.display1()
print("Appointment time")
obj2=Appointment()
obj2.accept2("11 AM")
obj2.display2()
Program of Multilevel inheritance of Team,coach,player-->>
ReplyDeleteclass Team:
def accept(self,name,country):
self.name=name
self.country=country
def display(self):
print("Team name is"+str(self.name)+"Team's country is"+str(self.country))
class Coach:
def accept1(self,coachname):
self.coachname=coachname
def display1(self):
print("Coach Name is"+str(self.coachname))
class Player:
def accept2(self,playerinfo):
self.playerinfo=playerinfo
def display2(self):
print("Player is"+str(self.playerinfo))
print("Team details")
obj=Team()
obj.accept("Deccan chargers","India")
obj.display()
print("Coach information")
obj1=Coach()
obj1.accept1("Bryan lara")
obj1.display1()
print("Player's specialisation")
obj2=Player()
obj2.accept2("All rounder")
obj2.display2()
Program of multilevel inheritance for institute,branch,location-->>
ReplyDeleteclass Institute:
def accept(self,institutename,institutetype):
self.institutename=institutename
self.institutetype=institutetype
def display(self):
print("Institute name is"+str(self.institutename)+"Institute's type is"+str(self.institutetype))
class Branch:
def accept1(self,bhopal):
self.bhopal=bhopal
def display1(self):
print("Branch of"+str(self.bhopal))
class Location:
def accept2(self,locationinfo):
self.locationinfo=locationinfo
def display2(self):
print("Located at"+str(self.locationinfo))
print("Institute details")
obj=Institute()
obj.accept("SCS","Software training institute")
obj.display()
print("Branch name")
obj1=Branch()
obj1.accept1("Bhopal")
obj1.display1()
print("Location")
obj2=Location()
obj2.accept2("Lakeview")
obj2.display2()
Program of Hierarchical inheritance for Team,coach,player-->>
ReplyDeleteclass Team:
def accept(self,name,country):
self.name=name
self.country=country
def display(self):
print("Team name is"+str(self.name)+"Team's country is"+str(self.country))
class Coach(Team):
def accept1(self,coachname):
self.coachname=coachname
def display1(self):
print("Coach Name is"+str(self.coachname))
class Player(Team):
def accept2(self,playerinfo):
self.playerinfo=playerinfo
def display2(self):
print("Player is"+str(self.playerinfo))
print("Team details")
obj=Team()
obj.accept("Deccan chargers","India")
obj.display()
print("Coach information")
obj1=Coach()
obj1.accept1("Don bradman")
obj1.display1()
print("Player's specialisation")
obj2=Player()
obj2.accept2("All rounder")
obj2.display2()
Program of Multiple inheritance for Team,coach,player-->>
ReplyDeleteclass Team:
def fun(self):
print("Team")
def accept(self,name,country):
self.name=name
self.country=country
def display(self):
print("Team name is"+str(self.name)+"Team's country is"+str(self.country))
class Coach:
def fun(self):
print("coach")
def accept1(self,coachname):
self.coachname=coachname
def display1(self):
print("Coach Name is"+str(self.coachname))
class Player(Team,Coach):
def accept2(self,playerinfo):
self.playerinfo=playerinfo
def display2(self):
print("Player is"+str(self.playerinfo))
print("Team details")
obj=Team()
obj.accept("Deccan chargers","India")
obj.display()
print("Coach information")
obj1=Coach()
obj1.accept1("Don bradman")
obj1.display1()
print("Player's specialisation")
obj2=Player()
obj2.accept2("All rounder")
obj2.display2()
Program of Hierarchical inheritance for Institute,branch,location-->>
ReplyDeleteclass Institute:
def accept(self,institutename,institutetype):
self.institutename=institutename
self.institutetype=institutetype
def display(self):
print("Institute name is"+str(self.institutename)+"Institute's type is"+str(self.institutetype))
class Branch(Institute):
def accept1(self,bhopal):
self.bhopal=bhopal
def display1(self):
print("Branch of"+str(self.bhopal))
class Location(Institute):
def accept2(self,locationinfo):
self.locationinfo=locationinfo
def display2(self):
print("Located at"+str(self.locationinfo))
print("Institute details")
obj=Institute()
obj.accept("SCS","Software training institute")
obj.display()
print("Branch name")
obj1=Branch()
obj1.accept1("Bhopal")
obj1.display1()
print("Location")
obj2=Location()
obj2.accept2("Lakeview")
obj2.display2()
Program of multiple inheritance for institute,branch,location-->>
ReplyDeleteclass Institute:
def fun(self):
print("Institute")
def accept(self,institutename,institutetype):
self.institutename=institutename
self.institutetype=institutetype
def display(self):
print("Institute name is"+str(self.institutename)+"Institute's type is"+str(self.institutetype))
class Branch:
def fun(self):
print("Branch")
def accept1(self,bhopal):
self.bhopal=bhopal
def display1(self):
print("Branch of"+str(self.bhopal))
class Location(Institute,Branch):
def accept2(self,locationinfo):
self.locationinfo=locationinfo
def display2(self):
print("Located at"+str(self.locationinfo))
print("Institute details")
obj=Institute()
obj.accept("SCS","Software training institute")
obj.display()
print("Branch name")
obj1=Branch()
obj1.accept1("Bhopal")
obj1.display1()
print("Location")
obj2=Location()
obj2.accept2("Lakeview")
obj2.display2()
# Check Prime number
ReplyDeleteclass Prime:
def accept(self):
self.n = int(input("Enter number to check Prime"))
def ope(self):
self.count = 0
for i in range(2, (self.n)):
if (self.n)%i == 0:
self.count = self.count+1
def display(self):
if self.count != 0:
print(self.n,"Not Prime")
else:
print(self.n,"Prime number")
r= Prime()
r.accept()
r.ope()
r.display()
Jayant Chawliya
ReplyDeleteclass HomeWork:
def input_prime(self):
self.num = int(input("Enter the number here: "))
def prime(self):
if self.num > 1:
for i in range(2, self.num):
if (self.num % i) == 0:
print(self.num, "is not a prime number")
break
else:
print(self.num, "is a prime number")
def diamond_accept(self):
self.a=int(input("Enter the number of Rows : "))
def Diamond(self):
n = 0
for i in range(1, self.a + 1):
for j in range(1, (self.a - i) + 1):
print(end=" ")
while n != (2 * i - 1):
print("*", end="")
n = n + 1
n = 0
print()
k = 1
n = 1
for i in range(1, self.a):
for j in range(1, k + 1):
print(end=" ")
k = k + 1
while n <= (2 * (self.a - i) - 1):
print("*", end="")
n = n + 1
n = 1
print()
def marksheet_accept(self):
self.M=int(input("Enter the marks of maths :"))
self.J=int(input("Enter the marks of java :"))
def marksheet(self):
self.add=self.M+self.J
print("Total Marks of Maths and Java is ",self.add)
self.per=self.add/200*100
print("Percentage ",self.per)
#Supplementry
self.s=0
if (self.M < 33):
print("supply in Maths")
self.s+=1
elif (self.J < 33):
print("supply in java")
self.s += 1
#Grace_concept
if (self.s <= 1):
if (self.M <= 30):
self.M += 3
print("pass with grace 3 ")
elif(self.s <= 1):
if (self.J <= 30):
self.J += 3
print("pass with grace 3 ")
obj=HomeWork()
obj.input_prime()
obj.prime()
obj1=HomeWork()
obj1.diamond_accept()
obj1.Diamond()
obj2=HomeWork()
obj2.marksheet_accept()
obj2.marksheet()
Program for prime number?
ReplyDeleteclass Prime:
def accept(self,num):
self.num = num
def logic(self):
for i in range(2,self.num):
if self.num%i==0:
self.result="not prime"
break
else:
self.result = "prime"
def display(self):
print(self.result)
obj = Prime()
obj.accept(int(input("enter number")))
obj.logic()
obj.display()
Solution of fibonacci series?
ReplyDeleteclass Fibonacci:
def accept(self,num):
self.num = num
def logic(self):
a=-1
b=1
self.result=''
for i in range(1,self.num+1):
c=a+b
self.result += str(c) + "\n"
a=b
b=c
def display(self):
print(self.result)
obj = Fibonacci()
obj.accept(int(input("enter number of terms to display series")))
obj.logic()
obj.display()
#WAP to calculate Simple Interest using static data member only
ReplyDeleteclass Si:
p=5000
r= 4
t = 2
r =(Si.p*Si.r*Si.t)/100
print(r)
#WAP to calculate Simple Interest using dynamic data member only
ReplyDeleteclass si:
def accept(self):
self.p =int(input("Enter Principal amount = "))
self.r = int(input("Enter interest rate = "))
self.t = int(input("Enter time period = "))
def s_i(self):
self.r = self.p*self.r*self.t/100
def display(self):
print("SI is = " + str(self.r))
b= si()
b.accept()
b.s_i()
b.display()
ReplyDelete#Program for prime number?
class Prime:
def accept(self,num):
self.num = num
def logic(self):
for i in range(2,self.num):
if self.num%i==0:
self.result=("not prime")
break
else:
self.result =("prime")
def display(self):
print(self.result)
obj = Prime()
obj.accept(int(input("enter number")))
obj.logic()
obj.display()
# EXAMPLE MULTI PARAMETER CONSTRUCTER
ReplyDeleteclass Addition:
def __init__(self,f,s):
self.f = f
self.s = s
def display(self):
print("First number is " +str(self.f))
print("Second number is "+ str(self.s))
print("Sum is " + str(self.add))
print("Multiplication = " +str(self.multi))
def logic(self):
self.add = self.f + self.s
self.multi = self.f*self.s
f =float(input("Enter first number = "))
s =float(input("Enter second number = "))
obj = Addition(f,s)
obj.logic()
obj.display()
#ATM
ReplyDeleteclass Bank:
balance = 50000
def __bal(self):
print("Your balance is = " +str(self.balance))
def __credit(self,amount):
self.balance += amount
print("Current balance is = "+str(self.balance))
def __debit(self,amount):
self.balance -=amount
print("Current balance = "+str(self.balance))
def login(self, pin):
if pin == 12345:
print("WELCOME")
x = input("Press B for balance enquiry. Press D for debit amount. Press C for credit amount = ")
if x=='b' or x=='B':
self.__bal()
if x == 'c' or x=='C':
self.__credit(int(input("Enter amount to credit")))
if x == 'd' or x == 'D':
self.__debit(int(input("Enter amount to withdraw")))
else:
print("Incorrect pin")
pin = int(input("Enter pin"))
obj = Bank()
obj.login(pin)
#Star Pattern
ReplyDeleteclass Star:
def accept(self):
self.n= int(input("Enter number of rows= "))
def display(self):
for i in range(0,self.n):
print(' '*(self.n-i-1) + '* '*(i+1))
for j in range(self.n-1,0,-1):
print(' '*(self.n-j) + '* '*(j))
obj= Star()
obj.accept()
obj.display()
# Manage Patient, Doctor, and Appointment using all possible Inheritance
ReplyDelete#single inheritance
class Doctor:
def doctor(self,name,location):
self.name=name
self.location=location
def display(self):
print("Doctor Details: ")
print("Doctor_name : "+str(self.name),"\nlocation : "+str(self.location))
class Patient(Doctor):
def patient(self,id,name1,age):
self.id=id
self.name1=name1
self.age=age
def display1(self):
print("id is : "+str(self.id),"name : "+str(self.name1),"age: "+str(self.age))
class Appointement(Patient):
def appoint(self,time):
self.time=time
def display2(self):
print("time is : "+str(self.time))
obj=Appointement()
obj.doctor(" Manish "," Indore " )
obj.patient(101," Rahul ",40)
obj.appoint("6 pm")
obj.display()
obj.display1()
obj.display2()
#hierarchical
ReplyDeleteclass Team:
def func1(self):
print("This function is in Team class.")
def Teaminfo(self, country, group):
self.country = country
self.group = group
def display(self):
print("country" + str(self.country), "group" + str(self.group))
class Child1(Team):
def func2(self):
print("This function is in child 1.")
class Child2(Team):
def func3(self):
print("This function is in child 2.")
object1 = Child1()
object1.func1()
object1.Teaminfo(" INDIA "," A")
object1.display()
object1.func2()
object2 = Child2()
object2.func1()
object2.Teaminfo(" INDIA "," B")
object2.display()
#Multi-Level Inheritance Example for Area of the circle, triangle, and rectangle:-
ReplyDeleteclass Areaofcircle:
def accept(self,a):
self.a =a
def logic(self):
self.r = 3.14 * self.a *self.a
def display(self):
print("Area of circle is " +str(self.r))
class Areaoftriangle(Areaofcircle):
def accept1(self,b):
self.b = b
def logic1(self):
self.r = self.a *self.b/2
def display1(self):
print("Area of triangle is " +str(self.r))
class Areaofrectangle(Areaoftriangle):
def logic2(self):
self.r = self.a*self.b
def display2(self):
print("Area of rectangle is " +str(self.r))
print("For circle")
obj = Areaofcircle()
obj.accept(5)
obj.logic()
obj.display()
print("For Triangle")
obj1 = Areaoftriangle()
obj1.accept(4)
obj1.accept1(7)
obj1.logic1()
obj1.display1()
print("For rectangle")
obj2 = Areaofrectangle()
obj2.accept(8)
obj2.accept1(7)
obj2.logic2()
obj2.display2()
By Multi-level, Manage Coach, Team, and Player using all possible Inheritance?
ReplyDeleteclass Coach:
def accept(self,name):
self.name=name
def display(self):
print("Coach is " +str(self.name))
class Team(Coach):
def accept1(self,team):
self.team = team
def display1(self):
print("Team is "+ str(self.team))
class Player(Team):
def display2(self):
print("Player name is " +str(self.name) +" & Player team is "+ str(self.team))
print("Coach Information")
obj =Coach()
obj.accept('Ravi')
obj.display()
print("Team Information")
obj1 =Team()
obj1.accept('Mukes')
obj1.accept1('India')
obj1.display1()
print("Player Information")
obj2 =Player()
obj2.accept('Virat')
obj2.accept1('India')
obj2.display2()
#By Multiple Inheritance Manage Coach, Team, and Player using all possible Inheritance?
ReplyDeleteclass Coach:
def accept(self,name):
self.name=name
def display(self):
print("Coach is " +str(self.name))
class Team:
def accept1(self,team):
self.team = team
def display1(self):
print("Team is "+ str(self.team))
class Player(Team,Coach):
def display2(self):
print("Player name is " +str(self.name) +" & Player team is "+ str(self.team))
print("Coach Information")
obj =Coach()
obj.accept('Ravi Shastri')
obj.display()
print("Team Information")
obj1 =Team()
obj1.accept1('India')
obj1.display1()
print("Player Information")
obj2 =Player()
obj2.accept('Virat')
obj2.accept1('India')
obj2.display2()
#By Hierarchial inheritance, Manage Coach, Team, and Player using all possible Inheritance?
ReplyDeleteclass Coach:
def accept(self,name):
self.name=name
def display(self):
print("Coach is " +str(self.name))
class Team(Coach):
def accept1(self,team):
self.team = team
def display1(self):
print("Team is "+ str(self.team))
class Player(Coach):
def accept2(self,t):
self.t = t
def display2(self):
print("Player name is " +str(self.name) +" & Player team is "+ str(self.t))
print("Coach Information")
obj =Coach()
obj.accept('Justin Langer')
obj.display()
print("Team Information")
obj1 =Team()
obj1.accept('Justin Langer')
obj1.accept1('Australia')
obj1.display()
obj1.display1()
print("Player Information")
obj2 =Player()
obj2.accept('Steve Smith')
obj2.accept2('Australia')
obj2.display2()
Thanks for this wonderful post.
ReplyDeletePython Online Training
Post a Comment
If you have any doubt in programming or join online classes then you can contact us by comment .