PYTHON (2)
PYTHON (2)
Define a class Student that keeps a record of students and Develop a python
program for the class
The class should contain the following:
data members for every student: rollNo, Name, Class, Section, marks1,
marks2, and marks3. Assume that the maximum marks for each subject are
100.
an additional data member count to keep a record of the number of objects
created for this class. Add appropriate statements in the code to increment
the value of count by 1, when an object is created.
Also define function members:
a constructor function to initialize the members
a function grade, that returns the overall grade of the student according
to the following criteria:
A : if percentage ≥ 90
B :if percentage ≥ 80 and < 90
C: if percentage ≥ 70 and < 80
D : if percentage < 70
class Student:
ts = 0
def __init__(self,rollno,name,clss,sec,m1,m2,m3):
self.rollno=rollno
self.name=name
self.clss=clss
self.sec=sec
self.m1=m1
self.m2=m2
self.m3=m3
Student.ts+=1
def Grade(self):
total = self.m1+self.m2+self.m3
p = total/3
if p >=90:
return 'A'
elif p >=80:
return 'B'
elif p >= 70:
return 'C'
else:
return 'D'
def display(self):
print(f"Roll No :{self.rollno}")
print(f"Name :{self.name}")
print(f"Class :{self.clss}")
print(f"Section: {self.sec}")
print(f"Marks1 :{self.m1,self.m2,self.m3}")
print(f"Grades :{self.Grade()}")
s = Student(51,"Anirudh",13,1,30,30,39)
s.display()
the Car class and display all properties of the base class.
class Vehicle:
def __init__(self,v_name,v_model):
self.v_name=v_name
self.v_model=v_model
def disp_vehicle(self) :
print(self.v_name)
print(self.v_model)
class Wheel(Vehicle):
def __init__(self,v_name,v_model,w_comp,w_cost):
super().__init__(v_name,v_model)
self.w_comp=w_comp
self.w_cost=w_cost
def disp_wheel(self):
super().disp_vehicle()
print(self.w_comp)
print(self.w_cost)
class Car(Wheel):
def __init__(self,v_name,v_model,w_comp,w_cost):
super().__init__(v_name,v_model,w_comp,w_cost)
def disp_car(self):
self.disp_wheel()
print()
3. Identify the usage of __init__() method and super() method and Develop a multilevel
class Person:
def __init__(self,name,age):
self.name=name
self.age=age
def display(self):
print("Name :",self.name)
print("Age :",self.age)
class Student(Person):
def __init__(self,name,age,stu_id):
super().__init__(name,age)
self.stu_id=stu_id
def display(self):
super().display()
print("StudentId :",self.stu_id)
class Graduate(Student):
def __init__(self,name,age,stu_id,branch):
super().__init__(name,age,stu_id)
self.branch=branch
def display(self):
super().display()
print("Branch :",self.branch)
s1 = Graduate("Siri",10,143,"CE")
s1.display()
import sqlite3
conn = sqlite3.connect(':memory:')
cur = conn.cursor()
cur.execute("""
Name TEXT,
Age INTEGER,
Marks INTEGER
""")
student = [
(123,"B.Srinivas",35,80),
(124,"B.Raju",None,79),
(125,"B.Amal",20,None),
(126,"Saritha",24,65)
]
cur.executemany("INSERT INTO student VALUES (?,?,?,?)",student)
print(row)
conn.commit()
conn.close()
7. For the above given database STUDENT, Develop a python program for the
following operations
a) Insert a student record with multiple values.
import sqlite3
conn = sqlite3.connect(':memory:')
cur = conn.cursor()
cur.execute("""
CREATE TABLE student(
Rollno INTEGER PRIMARY KEY,
Name TEXT,
Age INTEGER,
Marks INTEGER
)
""")
print("Table was created")
student = [
(123,"B.Srinivas",35,80),
(124,"B.Raju",None,79),
(125,"B.Amal",20,None),
(126,"Saritha",24,65),
]
cur.executemany("INSERT INTO student VALUES (?,?,?,?)",student)
cur.execute("INSERT INTO student (Rollno,Name,Age,Marks) VALUES (?,?,?,?)",
(127,"Lisa",27,80))
for Rollno,Branch in
[(123,"CSE"),(124,"ECE"),(125,"CE"),(126,"CSN"),(127,"CSM"),(128,"CSN")]:
import numpy as np
arr = np.array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
reshaped = arr.reshape(3, 4)
print(reshaped)
9. Create a 2-D array called array1 having 3 rows and 3 columns and store
the following data:
10 20 30
40 0 60
70 80 90
Identify the outputs of the following statements:
i. Find the dimensions, shape, size, data type of the items and itemsize of array
ii. Display the 2nd and 3rd element of the array.
iii. Display all elements in the 2nd and 3rd row of the array array1.
iv. Display the elements in the 1st and 2nd column of the array array1.
import numpy as np
print("Shape:", arr.shape)
print("Size:", arr.size)
print("Rows:\n",arr[1:3, :])
print("Columns:\n", arr[:, 0:2])
print("Transpose:\n", arr.T)