0% found this document useful (0 votes)
2 views

Programs (1)

The document contains multiple Python programs demonstrating various functionalities including a binary file management system for student records, a stack implementation, list manipulation for odd and even numbers, text file word filtering, and CSV file handling for student records. Additionally, it includes SQL commands for managing customer and book records in a database, as well as vehicle data manipulation. Each program and SQL command is structured to provide a menu-driven interface or specific data operations.

Uploaded by

kingadi5c
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programs (1)

The document contains multiple Python programs demonstrating various functionalities including a binary file management system for student records, a stack implementation, list manipulation for odd and even numbers, text file word filtering, and CSV file handling for student records. Additionally, it includes SQL commands for managing customer and book records in a database, as well as vehicle data manipulation. Each program and SQL command is structured to provide a menu-driven interface or specific data operations.

Uploaded by

kingadi5c
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Program-1 Menu driven program for binary file student.dat.

1. Add record 2. Display record 3. Exit


Structure of file content is [GrNo, name, Marks]

import pickle
L=[]
found=0
def add_rec():
f=open("student.dat","ab")
gr=int(input("Enter GrNo:"))
nm=input("Enter Name:")
mark=int(input("Enter Marks:"))
L=[gr,nm,mark]
pickle.dump(L,f)
print("Record Added.")
f.close()

def dis_rec():
f=open("student.dat","rb")
while True:
try:
L=pickle.load(f)
print(L)
except EOFError:
f.close()
break

while True:
print(“1.Add Record\n 2.Display Record\n 3.Exit”)
ch=int(input("Enter your choice:"))
if ch==1:
add_rec()
elif ch==2:
dis_rec()
elif ch==3:
break
else:
print("Invalid Choice")
Program -2
Menu driven program to implement stack using list and perform following:
1. Push 2. Pop 3. Display 4. Exit

stk=[]

def PUSH(stk,n):
stk.append(n)

def POP(stk):
if len(stk)==0:
print( "Underflow. Empty stack.")
else:
n=stk.pop()
print(“Deleted item:”,n)

def display(stk):
if len(stk)==0:
print( "Underflow. Empty stack." )
else:
for i in range(len(stk)-1,-1,-1):
print(stk[i])

while True:
print(“1. Push\n 2. Pop\n 3. Display\n 4. Exit”)
ch=int(input("Enter your choice:"))
if ch==1:
e=int(input("Enter the value to push:"))
PUSH(stk,e)
elif ch==2:
POP(stk)
elif ch==3:
display(stk)
elif ch==4:
break
else:
print("Invalid Choice")
Program-3 Write a python program to create a list and
multiply the odd numbers and divide the even numbers by
two.
def change(L):
for I in range(len(L)):
if L[I] % 2 == 0:
L[I] = L[I] / 2
else:
L[I] = L[I] * 2
print(L)
N=[10,2,11,4,13,17]
change(N)

Program-4 Write a python program to read a text file “sample.txt” and display
the four-letter words from it.
def dispaly():
f=open("sample.txt","r")
words =f.read().split()
for i in words:
if len(i)==4:
print(i)

dispaly()

Program-5 Write a program for working with records of students stored in


student.csv file. Write following functions:
(i) Newrecord() - To accept and add data of a student to a CSV file 'student.csv'. Each
record consists of a list with field elements as StudId, Studname and Stream to store
Student id, Student name and stream of the student respectively.
(ii) Displayrecords () - To display the records present in the CSV file named 'student.csv'.
import csv
def Newrecord():
stud_id = input("Enter Student ID: ")
stud_name = input("Enter Student Name: ")
stream = input("Enter Stream : ")
new_record = [stud_id, stud_name, stream]
file=open('student.csv', 'w', newline='')
obj = csv.writer(file)
obj.writerow(new_record)
print("Record added successfully.")

def Displayrecords():
file=open('student.csv','r')
obj = csv.reader(file)
records = list(obj)
for r in records:
print("Student ID:”,r[0],"Name:",r[1],"Stream: ",r[2])

while True:
print("\nMenu:")
print("1. Add New Record")
print("2. Display All Records")
print("3. Exit")
choice = int(input("Enter your choice (1/2/3): "))
if choice == 1:
Newrecord()
elif choice == 2:
Displayrecords()
elif choice == 3:
print("Exiting the program.")
break
else:
print("Invalid choice. Please try again.")
SQL

Consider the table customer write SQL command for given statements:
CustomerID CustomerName City MobileNo
C111 Abhishek Ahmedabad 9999999999
C132 Bhavik Anand 7799779977
C135 Chandani Baroda 8856895485
C145 Dhara Ahmedabad 7456879652
C121 Divya Anand 9015123569
1. Display all details of customers belongs to Ahmedabad.
2. Display CustomerId, customername and mobileno of customers whose name
starts with ‘D’.
3. Delete record whose name is “Abhishek”.
4. Modify city to “Nadiad” whose CustomerID is “C132”.
Ans.
1. SELECT * FROM CUSTOMER WHERE CITY=’Ahmedabad’;
2. SELECT CUSTOMERID, CUSTOMERNAME, MOBILENO FROM
CUSTOMER WHERE NAME LIKE ‘D%’;
3. DELETE FROM CUSTOMER WHERE NAME = ‘Abhishek’;
4. UPDATE CUSTOMER SET CITY=’Nadiad’ WHERE
CUSTOMERID=’C132’;

Consider the table BOOKS. Write SQL command for given statements:

1. Display the book id, book name and quantity in order of Book name.
2. Display the book id, book name, author name, quantity issued for all books
which price is less than 300.
3. Insert a new record in above table.
BID – COMP13, BNAME – Head First Python, AUNAME – Luciano Ramalho,
PRICE – 500, TYPE- COMPUTER, QTY-15
4. Modify the quantity of book “LET US C” to 25.
ANS
1. Select BID,BNAME,QTY FROM BOOKS ORDER BY BNAME;
2. SELECT BID, BNAME ,AUNAME,QTY FROM BOOKS WHERE PRICE
< 300;
3. INSERT INTO BOOKS VALUES(‘COMP13’,’Head First
Python’, ‘Luciano Ramalho’, 500, ‘COMPUTER’,15);
4. UPDATE BOOKS SET QTY=25 WHERE BNAME = ‘LET US C’;
Consider the table CABHUB. Write SQL command for given
statements:
Vcode VehicleName Make Color Capacity Charges
100 Innova Suzuki WHITE 15 102
104 Mercedes Toyota BLUE 14 300
103 SX4 C Class RED 3 200
105 A-Star Tata WHITE 14 108
102 Indigo Suzuki SILVER 12 250

(a) To display the names of all the white colored vehicles.


(b) To display name of vehicle and capacity of vehicles in ascending order of their sitting
capacity.
(c) Delete the records of vehicles of white color.
(d) Add a column Pnum in the table CABHUB with datatype as integer.

Ans.
(a) Select * from CABHUB where Color=’WHITE’;
(b) Select VehicleName, Capacity from CABHUB order by
Capacity;
(c) Delete from CABHUB where Color=’WHITE’;
(d) Alter table CABHUB ADD Pnum int;

Consider the tables CARDEN and CUSTOMER given below:

Based on the tables write SQL queries for the following:


(i) To display the names of all the silver coloured cars.
(ii) To display names of car, make and capacity of cars in descending order of their
sitting capacity.
(iii) To modify the Colour to ‘BLACK’ whose Ccode is 501.
(iv) To display the customer name and the corresponding name of the cars hired by
them.
ANS
1. Select * from CARDEN where Colour=’SILVER’;
2. Select Carname, make, capacity from CARDEN order
by capacity desc;
3. Update CARDEN set Colour=’BLACK’ where Ccode=501;
4. Select B.Cname, A.Carname from CARDEN A, CUSTOMER
B where A.Ccode=B.Ccode;

You might also like