Programs (1)
Programs (1)
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()
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
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;