'Write a program to create a Student class with name, age
and marks as data members. Also create a method named display()
to view the student details.
Create an object to Student class and call the method using the object'''
class Student:
def __init__(self,name1='abc',age1=18,marks1=35):
self.name=name1
self.age=age1
self.marks=marks1
def display(self):
print("student name is",self.name)
print("student age is",self.age)
print("Student marks is",self.marks)
s1=Student()
s1.display()
s2=Student('def',20,80)
s2.display()
Write a program to create Student class with a constructor having more than one parameter.
class Student:
def __init__(self,id1='1',name1='abc'):
self.id=id1
self.name=name1
def printdata(self):
print("Student id is",self.id)
print("Student name is",self.name)
s1=Student()
s1.printdata()
s2=Student(2,"def");
s2.printdata()
Write a program to demonstrate the use of instance and class/static variables.
class test:
x=5 #class variable
def __init__(self):
self.y=5 #instance variable
@classmethod
def printdata(cls):
cls.x=cls.x+1
print("Class variable-X value is",cls.x)
def printdata1(self):
self.y=self.y+1
print("Instance variable-y value is",self.y)
print("First object")
t1=test()
t1.printdata()
t1.printdata1()
print("Second object")
t2=test()
t2.printdata()
t2.printdata1()
Write a program to store data into instances using mutator methods and to retrieve data from the instances using accessor methods.'
There are two types of instance methods
1. accessor methods /getter methods
2. mutator methods / setter methods
class book:
def setid(self,id):
self.id=id
def setname(self,name):
self.name=name
def getid(self):
return self.id
def getname(self):
return self.name
b1=book()
x=int(input("Enter id"))
y=input("Enter name")
b1.setid(x)
b1.setname(y)
print("The book id is ",b1.getid(),"and book name is ", b1.getname())
'''Write a program to use class method to handle the common
features of all the instance of Student class.'''
class Student:
subject='python'
@classmethod
def setsubjectname(cls,sem):
print("your semester is ",sem," Subject is ",cls.subject)
s1=Student()
x=int(input("Enter semester"))
Student.setsubjectname(x)
'''Write a program to create a static
method that counts the number of instances created for a class.'''
'''static method-does not requires class or instance, it is used
to set environment variable'''
class countobj:
stvar=0
def __init__(self):
countobj.stvar=countobj.stvar+1
@staticmethod
def findtotobj():
print("The total number of objects are: ", countobj.stvar)
obj1=countobj()
obj2=countobj()
obj3=countobj()
countobj.findtotobj()
'''Create a Bank class with two variables name and balance.
Implement a constructor to initialize the variables.
Also implement deposit and withdrawals using instance methods.'''
class Bank:
def __init__(self,name='abc',balance=5000):
self.name=name
self.balance=balance
def deposit(self, amt):
self.balance=self.balance+amt
self.display()
def withdrawals(self,amt):
self.balance=self.balance-amt
self.display()
def display(self):
print(self.name," ",self.balance)
b1= Bank('def',10000)
amt=int(input("Enter amount to deposite"))
b1.deposit(amt)
amt=int(input("Enter amount to withdraw"))
b1.withdrawals(amt)
b2= Bank()
amt=int(input("Enter amount to deposite"))
b2.deposit(amt)
amt=int(input("Enter amount to withdraw"))
b2.withdrawals(amt)
'''Write a program to create a Emp class and make
all the members of the Emp class available to another class (Myclass).
[By passing members of one class to another]'''
class Emp:
def __init__(self):
self.id=1
self.name='abc'
def printdetails(self):
print("Id is",self.id,"Name is",self.name)
class Myclass:
def printobj(self,e1):
e1.id=2
e1.name='def'
e1.printdetails()
e1=Emp()
e1.printdetails()
e2=Myclass()
e2.printobj(e1)
e1.printdetails()
'''Create a Student class to with the methods set_id,
get_id, set_name, get_name, set_marks and get_marks where the
method name starting with set are used to assign the values and
method name starting with get are returning the values. Save the
program by student.py. Create another program
to use the Student class which is already available in student.py.'''
from Student import Student
s1=Student()
s1.setid(1)
s1.setname('abc')
s1.setmarks(50)
print("Students Details is: ")
print(s1.getid()," ",s1.getname()," ",s1.getmarks())
class Student:
def setid(self,id):
self.id=id
def getid(self):
return self.id
def setname(self,name):
self.name=name
def getname(self):
return self.name
def setmarks(self,marks):
self.marks=marks
def getmarks(self):
return self.marks
and marks as data members. Also create a method named display()
to view the student details.
Create an object to Student class and call the method using the object'''
class Student:
def __init__(self,name1='abc',age1=18,marks1=35):
self.name=name1
self.age=age1
self.marks=marks1
def display(self):
print("student name is",self.name)
print("student age is",self.age)
print("Student marks is",self.marks)
s1=Student()
s1.display()
s2=Student('def',20,80)
s2.display()
Write a program to create Student class with a constructor having more than one parameter.
class Student:
def __init__(self,id1='1',name1='abc'):
self.id=id1
self.name=name1
def printdata(self):
print("Student id is",self.id)
print("Student name is",self.name)
s1=Student()
s1.printdata()
s2=Student(2,"def");
s2.printdata()
Write a program to demonstrate the use of instance and class/static variables.
class test:
x=5 #class variable
def __init__(self):
self.y=5 #instance variable
@classmethod
def printdata(cls):
cls.x=cls.x+1
print("Class variable-X value is",cls.x)
def printdata1(self):
self.y=self.y+1
print("Instance variable-y value is",self.y)
print("First object")
t1=test()
t1.printdata()
t1.printdata1()
print("Second object")
t2=test()
t2.printdata()
t2.printdata1()
Write a program to store data into instances using mutator methods and to retrieve data from the instances using accessor methods.'
There are two types of instance methods
1. accessor methods /getter methods
2. mutator methods / setter methods
class book:
def setid(self,id):
self.id=id
def setname(self,name):
self.name=name
def getid(self):
return self.id
def getname(self):
return self.name
b1=book()
x=int(input("Enter id"))
y=input("Enter name")
b1.setid(x)
b1.setname(y)
print("The book id is ",b1.getid(),"and book name is ", b1.getname())
'''Write a program to use class method to handle the common
features of all the instance of Student class.'''
class Student:
subject='python'
@classmethod
def setsubjectname(cls,sem):
print("your semester is ",sem," Subject is ",cls.subject)
s1=Student()
x=int(input("Enter semester"))
Student.setsubjectname(x)
'''Write a program to create a static
method that counts the number of instances created for a class.'''
'''static method-does not requires class or instance, it is used
to set environment variable'''
class countobj:
stvar=0
def __init__(self):
countobj.stvar=countobj.stvar+1
@staticmethod
def findtotobj():
print("The total number of objects are: ", countobj.stvar)
obj1=countobj()
obj2=countobj()
obj3=countobj()
countobj.findtotobj()
'''Create a Bank class with two variables name and balance.
Implement a constructor to initialize the variables.
Also implement deposit and withdrawals using instance methods.'''
class Bank:
def __init__(self,name='abc',balance=5000):
self.name=name
self.balance=balance
def deposit(self, amt):
self.balance=self.balance+amt
self.display()
def withdrawals(self,amt):
self.balance=self.balance-amt
self.display()
def display(self):
print(self.name," ",self.balance)
b1= Bank('def',10000)
amt=int(input("Enter amount to deposite"))
b1.deposit(amt)
amt=int(input("Enter amount to withdraw"))
b1.withdrawals(amt)
b2= Bank()
amt=int(input("Enter amount to deposite"))
b2.deposit(amt)
amt=int(input("Enter amount to withdraw"))
b2.withdrawals(amt)
'''Write a program to create a Emp class and make
all the members of the Emp class available to another class (Myclass).
[By passing members of one class to another]'''
class Emp:
def __init__(self):
self.id=1
self.name='abc'
def printdetails(self):
print("Id is",self.id,"Name is",self.name)
class Myclass:
def printobj(self,e1):
e1.id=2
e1.name='def'
e1.printdetails()
e1=Emp()
e1.printdetails()
e2=Myclass()
e2.printobj(e1)
e1.printdetails()
'''Create a Student class to with the methods set_id,
get_id, set_name, get_name, set_marks and get_marks where the
method name starting with set are used to assign the values and
method name starting with get are returning the values. Save the
program by student.py. Create another program
to use the Student class which is already available in student.py.'''
from Student import Student
s1=Student()
s1.setid(1)
s1.setname('abc')
s1.setmarks(50)
print("Students Details is: ")
print(s1.getid()," ",s1.getname()," ",s1.getmarks())
class Student:
def setid(self,id):
self.id=id
def getid(self):
return self.id
def setname(self,name):
self.name=name
def getname(self):
return self.name
def setmarks(self,marks):
self.marks=marks
def getmarks(self):
return self.marks