0% found this document useful (0 votes)
28 views9 pages

PYTHON (2)

The document contains Python code examples demonstrating the creation of classes, inheritance, and database operations using SQLite. It includes a Student class with attributes and methods for grading, a Vehicle class with a Car subclass, and a multilevel inheritance example with Person, Student, and Graduate classes. Additionally, it shows how to create and manipulate a student database, as well as operations on 1-D and 2-D arrays using NumPy.

Uploaded by

muchaarchana123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views9 pages

PYTHON (2)

The document contains Python code examples demonstrating the creation of classes, inheritance, and database operations using SQLite. It includes a Student class with attributes and methods for grading, a Vehicle class with a Car subclass, and a multilevel inheritance example with Person, Student, and Graduate classes. Additionally, it shows how to create and manipulate a student database, as well as operations on 1-D and 2-D arrays using NumPy.

Uploaded by

muchaarchana123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

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()

2. A class inherits from a parent class or superclass.Suppose we have three


classes: Vehicle, Wheel, and Car.Vehicle is the superclass, Wheel is the child
class of Vehicle, and Car is the child class of Wheel.The Vehicle class consists
of properties like vehiclename and model.The Wheel class consists of
properties like w_company and w_price.The Car class inherits properties and
methods from both Vehicle and Wheel classes.Develop a Python program for

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()

c1 = Car("Toyota", 2028, "Goodyear",170000)


c1.disp_car()

3. Identify the usage of __init__() method and super() method and Develop a multilevel

inheritance using __int__method ad super() method.

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()

6. Create following STUDENT database using sqlite

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)

print("Student records inserted")

print("\nAll students records")

for row in cur.execute("SELECT * FROM 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))

b) Insert a student record with missing missing value.


cur.execute("INSERT INTO student (Rollno,Name,Age,Marks) VALUES (?,?,?,?)",
(128,"Jennie",28,None))

c) Add a branch attribute to student table.


cur.execute("ALTER TABLE student ADD COLUMN Branch TEXT")
print("Branch column added")

d)Update the branch detail in student record.

for Rollno,Branch in
[(123,"CSE"),(124,"ECE"),(125,"CE"),(126,"CSN"),(127,"CSM"),(128,"CSN")]:

cur.execute("UPDATE student SET Branch =? WHERE Rollno =?",(Branch,Rollno))

8. Create a 1-D array with the following elements


[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
Develop the python program to print following output
[[10, 11, 12, 13],
[14, 15, 16, 17],
[18, 19, 20, 21]]

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.

v. Display transpose of the array1

import numpy as np

arr = np.array([[10, 20, 30], [40, 0, 60], [70, 80, 90]])

print("Dimensions :", arr.ndim)

print("Shape:", arr.shape)

print("Size:", arr.size)

print("Data type:", arr.dtype)

print("Item size:", arr.itemsize)

print("Elements:", arr[0, 1], arr[0, 2])

print("Rows:\n",arr[1:3, :])
print("Columns:\n", arr[:, 0:2])

print("Transpose:\n", arr.T)

You might also like