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

ANIRUTH SRINIVASAN Board Praticals CS

The document contains examples of Python programs and SQL queries to perform various operations like stack implementation, file handling, database connectivity etc. Multiple functions are defined to push, pop and display stack elements. Records are inserted into tables and queries are used to retrieve, update and delete records based on certain conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views9 pages

ANIRUTH SRINIVASAN Board Praticals CS

The document contains examples of Python programs and SQL queries to perform various operations like stack implementation, file handling, database connectivity etc. Multiple functions are defined to push, pop and display stack elements. Records are inserted into tables and queries are used to retrieve, update and delete records based on certain conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

AECS ANUPURAM

AISSCE PRACTICAL,2022-2023
COMPUTER SCIENCE

1.(I). A list of 10 students’ marks is given below. Write a program with


separate user defined functions to perform the following operation. a.
Traverse the content of the list and push the numbers higher than 33 into a
stack.
b. Pop and display the content of the stack.
For example: If N=[12,13,34,56,21,79,98,22,35,38]
Sample output should be 38,35,98,79,56,34

N=[12,13,34,56,21,79,98,22,35,38]
stk=[]
def push():
for i in N:
if i>33:
stk.append(i)
def pop():
if stk==[]:
print("Stack Empty!Underflow!")
else:
print(‘Popped out item:’,stk.pop())
def display():
for i in range(len(stk)-1,-1,-1):
print(stk[i],end=",")
push()
pop()
display()

1.(II). Write a Python & MySQL connectivity program to solve the queries.
a. Display students’ records from the table based on gender as female. b.
Display name and age of students whose age is 23 and above. c. Display
details of all the students whose name starts with ‘S’.

TABLE: STUDENT
NO NAME AGE SEX

1 PANKAJ 24 M

2 SHALINI 21 F

3 SANJAY 22 M

4 SUDHA 25 F

5 RAKESH 22 M

import mysql.connector as con


mycon=con.connect(host='localhost', user='root',
passwd='admin') c=mycon.cursor()
c.execute('create database if not exists PRACTICAL')
c.execute('use PRACTICAL')

#table creation and record insertion:


c.execute('''create table STUDENT(NO int not null primary
key, NAME varchar(10),
AGE int,
SEX char(1)) ''')
i='''insert into student values
(1,'PANKAJ',24,'M'),
(2,'SHALINI',21,'F'),
(3,'SANJAY',22,'M'),
(4,'SUDHA',25,'F'),
(5,'RAKESH',22,'M') '''
c.execute(i)
mycon.commit()
print(c.rowcount,'Records Inserted')

#(a)query:
c.execute("select * from STUDENT where SEX='F'")
for i in c:
print(i)

#(b)query:
c.execute("select NAME,AGE from STUDENT where AGE>=22")
for i in c:
print(i)

#(c)query:
c.execute("select * from STUDENT where NAME like 'S%'")
for i in c:
print(i)

2.(I). Write a function in python to count number of lines in a text file


“XYZ.txt” which is starting with alphabet ‘A’ or ‘E’.

def countlines():
file=open("XYZ.txt")
count=0
lines=file.readlines()
for i in lines:
if i[0]=='A' or i[0]=='E':
count+=1
print("Number of lines starting with A or E
are",count)
file.close()
countlines()

2.(II). Write a Python & MySQL connectivity program to solve the queries.
a. To search records from the following STUDENT table based on non medical
students of class 12.
b. List the details of those students who are in class 12 sorted by
name.
c. To list name of female students who are in commerce stream.
TABLE: STUDENT
Rollno Name Stream Class Gender

1 Karan Medical 12B M

2 Divakar Commerce 11C M

3 Divya Nonmedical 12B F

4 Arun Humanities 12C M

5 Sabina Nonmedical 12A F

6 Rubina Medical 12C F

7 Harsha Commerce 11B F

import mysql.connector as con


mycon=con.connect(host='localhost', user='root',
passwd='admin') c=mycon.cursor()
c.execute('create database if not exists PRACTICAL')
c.execute('use PRACTICAL')

#table creation and record insertion:


c.execute('''create table STUDENT(RollNo int not null primary
key, Name varchar(10),
Stream varchar(15),
Class char(3),
Gender char(1))""")
i='''insert into STUDENT values
(1,'Karan','Medical','12B','M'),
(2,'Divakar','Commerce','11C','M'),
(3,'Divya','Nonmedical','12B','F'),
(4,'Arun','Humanities','12C','M'),
(5,'Sabina','Nonmedical','12A','F'),
(6,'Rubina','Medical','12C','F'),
(7,'Harsha','Commerce','11B','F')'''
c.execute(i)
mycon.commit()
print(c.rowcount,'Records Inserted')

#(a)query:
c.execute("select * from STUDENT where Stream='Nonmedical' and Class
like '12%'")
for i in c:
print(i)
#(b)query:
c.execute("select * from STUDENT where Class like '12%' order by
name")
for i in c:
print(i)

#(c)query:
c.execute("select Name from STUDENT where Stream='Commerce' and
Gender='F'")
for i in c:
print(i)
3.(I). Write a python program to get student data (Roll number, Name,
mark) from the user and store those details in a CSV file called
“marks.csv” and also display the file contents.

import csv
def write():
file=open('marks.csv','w')
write=csv.writer(file)
n=int(input('Enter no. of records: '))
for i in range(n):
roll=int(input('Enter Roll No: '))
name=input('Enter Name: ')
marks=float(input('Enter Marks: '))
rec=[roll,name,marks]
write.writerow(rec)
file.close()
def read():
file=open('marks.csv','r',newline='\r\n')
read=csv.reader(file)
for i in read:
print(i)
file.close()
write()
read()

3.(II). Write a Python & MySQL connectivity program to generate queries.

a. List the name of students who are in class 12 sorted by average marks.

b. Search minimum of average mark from the above table where average
marks<75.

c. Search following STUDENT table to delete all the records whose Avgmark is
less than 80.
TABLE: STUDENT
Name AvgMark Grade Class

Karan 78.5 B 12B


Divakar 89.2 A 11C

Divya 68.6 C 12C

Arun 73.1 B 12C

Sabina 90.6 A 11A

import mysql.connector as con


mycon=con.connect(host='localhost', user='root',
passwd='admin') c=mycon.cursor()
c.execute('create database if not exists PRACTICAL')
c.execute('use PRACTICAL')

#table creation and record insertion:


c.execute('''create table STUDENT(Name varchar(10),
AvgMark decimal(3,1),
Grade char(1),
Class char(3)) ''')

i='''insert into STUDENT values


('Karan',78.5,'B','12B'),
('Divakar',89.2,'A','11C'),
('Divya',68.6,'C','12C'),
('Arun',73.1,'B','12C'),
('Sabina',90.6,'A','11A') '''
c.execute(i)
mycon.commit()
print(c.rowcount,'Records Inserted')

#(a)query:
c.execute("select Name from STUDENT where Class like '12%' order by
AvgMark")
for i in c:
print(i)

#(b)query:
c.execute("select min(AvgMark) from STUDENT where AvgMark<75
")
for i in c:
print(i)

#(c)query:
c.execute("delete from STUDENT where AvgMark<80")
mycon.commit()
c.execute("select * from STUDENT")
for i in c:
print(i)

4.(I). A binary file “emp.dat” has structure [EID,Ename, designation, salary].


a. Write a user defined function CreateEmp() to input data for a record and
create a file emp.dat.
b. Write a function display() in Python to display the detail of all employees
whose salary is more than 50000.

import pickle
def CreateEmp():
file=open('emp.dat','wb')
n=int(input('Enter no. of records: '))
for i in range(n):
eid=input('Enter E.Id: ')
ename=input('Enter Name: ')
designation=input('Enter Designation: ')
salary=int(input('Enter Salary: '))
l=[eid,ename,designation,salary]
pickle.dump(l,file)
file.close()
def display():
file=open("emp.dat","rb")
try:
while True:
rec=pickle.load(file)
if rec[3]>50000:
print(rec[0],rec[1],rec[2],rec[3])
except:
file.close()
CreateEmp()
display()

4.(II). Write a Python & MySQL connectivity program to generate following


queries.
a. To increase the price of all books of EPB publishers by 50.
b. To display all the books sorted by name in descending order.
c. Display a list of books with price more than 400 and sorted by
publishers. TABLE : BOOKS
BOOK_NAME PUBLISHERS PRICE

FAST COOK EPB 355

THE TEARS FIRST PUBL. 650

MY FIRST C++ EPB 350

C++ BRAINWORKS TDH 350

THUNDERBOLTS FIRST PUBL. 750

import mysql.connector as con


mycon=con.connect(host='localhost', user='root',
passwd='admin') c=mycon.cursor()
c.execute('create database if not exists PRACTICAL')
c.execute('use PRACTICAL')

#table creation and record insertion:


c.execute('''create table BOOKS(BOOK_NAME varchar(15),
PUBLISHERS varchar(150),
PRICE int)''')
i='''insert into BOOKS values
('FAST COOK','EPB',355),
('THE TEARS','FIRST PUBL.',650),
('MY FIRST C++','EPB',350),
('C++ BRAINWORKS','TDH',350),
('THUNDERBOLTS','FIRST PUBL.',750) '''
c.execute(i)
mycon.commit()
print(c.rowcount,'Records Inserted')

#(a)query:
c.execute("update BOOKS set PRICE=PRICE+50 where
PUBLISHERS='EPB'") mycon.commit()
c.execute("select * from BOOKS")
for i in c:
print(i)

#(b)query:
c.execute("select BOOK_NAME from BOOKS order by BOOK_NAME
desc")
for i in c:
print(i)
#(c)query:
c.execute("select * from BOOKS where PRICE>400 order by
PUBLISHERS")
for i in c:
print(i)

5.(I). Vikram has a list containing 10 integers. You need to help him create
a program with separate user defined functions to perform the following
operations based on this list.
a. Traverse the content of the list and push the ODD numbers into a
stack.
b. Pop and display the content of the stack.
For Example: N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
The sample output should be 79,21,13

N=[12,13,34,56,21,79,98,22,35,38]
stk=[]
def push():
for i in N:
if i %2!=0:
stk.append(i)
def pop():
if stk==[]:
print("Stack Empty!Underflow!")
else:
print('Popped out item:',stk.pop())
def display():
for i in range(len(stk)- 1,-1,-1):
print(stk[i],end=",")
push()
pop()
display()

5.(II). Write a Python & MySQL connectivity program to search


following details from WORKER table.
a. To display the Wno and Name of those workers from the
table WORKER who are born between ‘1987-01-01’ and ‘1991-12-
01’.
b. List the details of those employees who have 5 lettered names.
c. List the details of all employees whose annual salary crosses
50000. TABLE: WORKER
WNO NAME DOB JOB SALARY

1001 JITHIN 1991-09-01 CLERK 2000

1002 ANANYA 1990-12-15 SALESMAN 4500

1003 MOHIT 1987-09-04 MANAGER 7000

1009 ANOOP 1988-10-02 MANAGER 3000

1007 AMIR 1984-10-19 CLERK 2000

1004 KULDEEP 1986-11-14 ANALYST 5000

import mysql.connector as con


mycon=con.connect(host='localhost', user='root',
passwd='admin') c=mycon.cursor()
c.execute('create database if not exists PRACTICAL')
c.execute('use PRACTICAL')

#table creation and record insertion:


c.execute('''create table WORKER (WNO int not null primary
key, NAME VARCHAR(10),
DOB DATE,
JOB VARCHAR(15),
SALARY INT) ''')
i='''insert into WORKER values
(1001,'JITHIN','1991-09-01','CLERK',2000),
(1002,'ANANYA','1990-12-15','SALESMAN',4500),
(1003,'MOHIT','1987-09-04','MANAGER',7000),
(1009,'ANOOP','1988-10-02','MANAGER',3000),
(1007,'AMIR','1984-10-19','CLERK',2000),
(1004,'KULDEEP','1986-11-14','ANALYST',5000)'''
c.execute(i)
mycon.commit()
print(c.rowcount,'Records Inserted')

#(a)query:
c.execute("select WNO, NAME from WORKER where DOB between '1987-01-01' and
'1991-12-01'")
for i in c:
print(i)

#(b)query:
c.execute("select * from WORKER where NAME like '_____'")
for i in c:
print(i)

#(c)query:
c.execute("select *,SALARY*12 from WORKER where
(SALARY*12)>50000")
for i in c:
print(i)

-----ALL THE BEST-----

PREPARED BY:
R.ANIRUTH SREENIVASAN

You might also like