class-object-asignment
September 24, 2024
[ ]:
0.1 1. Create Python class named Student and display its type
[1]: class Student:
pass
print(type(Student))
<class 'type'>
0.2 2. How to call a variable and function by creating a simple class
[2]: class Dog:
#CODE HERE
print("This is Variable")
print("I'm function This is Variable1")
a=Dog()
This is Variable
I'm function This is Variable1
0.3 3. Create a class with one static variable and two instace variables
[6]: class Demo:
def __init__(self,name,age,static):
self.instance_name=name#instance variable1
self.instance_age=age#instance variable2
self.static_name=static#static variable
obj=Demo("instance_var1","instance_var2","I am Static variable")# object
print(obj.static_name)
print(obj.instance_name)
print(obj.instance_age)
I am Static variable
instance_var1
instance_var2
1
0.4 4. Create a Vehicle class with max_speed and mileage instance attributes
[7]: # CODE HERE
class vehicle:
def __init__(self,max_speed,mileage):
self.max_speed=max_speed
self.mileage=mileage
obj = vehicle(240,18)
print(obj.max_speed,end=" ")
print(obj.mileage)
240 18
0.5 5. Create a simple class Person with constructer and one argument name
[8]: # A Sample class with init method
class Person:
def __init__(self,name):
self.name=name
def say_hi(self):
print(f"Hello, my name is {self.name}")
p = Person('Ram')
p.say_hi()
Hello, my name is Ram
0.6 6. Create a class for Students information like name, rollno, marks and
school name with static variable.
• create a function for displaying student information
• create one more function for getting students marks and percentage
[11]: class student:
static_school_name = "DIT"
def __init__(self,name,rollno,marks):
self.name=name
self.rollno=rollno
self.marks=marks
self.b={"name":self.name,"rno":self.rollno,"marks":self.marks}
def display(self):
print(f"Name:{self.name}, RollNo: {self.rollno}, Marks: {self.marks},␣
↪College:{self.static_school_name}")
def dictionary(self):
return f"Dictionary : {self.b}"
def perct(self):
ab=self.marks/5
print("percentahe of 399 of 500 is :",ab)
obj =student("abc",1,399)
2
obj.display()
print(obj.dictionary())
obj.perct()
Name:abc, RollNo: 1, Marks: 399, College:DIT
Dictionary : {'name': 'abc', 'rno': 1, 'marks': 399}
percentahe of 399 of 500 is : 79.8
0.7 7. Create a child class Bus that will inherit all of the variables and methods
of the Vehicle class
[12]: # CODE HERE
class Vehicle:
def __init__(self,vehicle,speed,mileage):
self.vehicle_name = vehicle
self.bus_speed = speed
self.mileage = mileage
def __str__(self):
return f"Vehicle name: {self.vehicle_name} speed: {self.bus_speed}␣
↪Mileage: {self.mileage}"
class bus(Vehicle):
def __init__(self,vehicle,speed,mileage):
super().__init__(vehicle,speed,mileage)
ab=bus("school Bus",180,12)
print(ab)
Vehicle name: school Bus speed: 180 Mileage: 12
0.8 8. Create a class Employee with two arguments name and salary
• create a function which displays the employee information
• Print total number of employees
[13]: class Employee:
static_count=0
def __init__(self,name,salary):
Employee.static_count += 1
self.name=name
self.salary=salary
def __str__(self):
return f"Name : {self.name} , Salary: {self.salary}"
ab = Employee("Zara",2000)
ac = Employee("Manni",5000)
print(ab)
print(ac)
print("Total employee",Employee.static_count)
Name : Zara , Salary: 2000
3
Name : Manni , Salary: 5000
Total employee 2
[ ]: