Professional Documents
Culture Documents
D TYTLER SCHOOL
BATCH 2022-2023
COMPUTER SCIENCE
PRACTICAL
NAME – KAUSHI NEGI
CLASS – XII – C
BOARD ROLL NO – 26601821
INDEX
S.NO PARTICULARS
1 IMPLEMENTING A STACK CONTAINING NUMBER
2 IMPLEMENTING A STACK CONTAINING NAME OF PERSON AND
SALARY
PROGRAM THAT WILL STORE NAME AND MARKS OF N STUDENTS IN
A DICTIONARYAND THEN PUSH THOSE STUDENTS IN A STACK
WHOSE MARKS ARE GREATER THAN 75.PROGRAM THEN POP THE
3 NAME FROM STACK AND DISPLAY
4 STORE NAME ,CLASS AND MARKS OF DIFFERENT STUDENTS IN A
CSV FILE AND THEN DISPLAY ALL THE RECORDS
PROGRAM TO STORE ROLL NUMBER,NAME,AND MARKS OF STUDENT
IN A BINARY FILE AND DISPLAY THE RECORDS.PROGRAM ALSO
5 SEARCH A PARTICULAR RECORD
MENU DRIVEN PROGRAM TO STORE ROLL NUMBER ,NAME AND
MARKS OF STUDENTS IN A BINARY FILE,DISPLAY ALL RECORDS AND
6 DELETE THE RECORD AND DELETE THE RECORD OF A STUDENT
MENU DRIVEN PROGRAM TO STORE ROLLNO, NAME & MARKS OF
DIFFERENT STUDENTS IN A BINARY FILE , DISPLAY ALL RECORDS
7 AND MODIFY RECORD OF A STUDENT
PYTHON CODE FOR CREATING AND DISPLAY A TEXT FILE.PROGRAM
ALSO COUNT THE NUMBER OF VOWELS AS PER USER'S
8 CHOICE
PYTHON CODE FOR CREATING AND DISPLAY A TEXT FILE.PROGRAM
ALSO COUNT THE NUMBER OF TIMES A WORD 'MY' APPEAR
9 AS PER USER'S CHOICE
PYTHON CODE FOR CREATING AND DISPLAY A TEXT FILE.PROGRAM
ALSO COUNT THE NUMBER OF LINES STARTING WITH ALPHABETS 'A'
10 OR 'a' AS PER USER'S CHOICE
11 PYTHON CODE TO ADD, DISPLAY AND DELETE RECORD OF A
CUSTOMER USING MYSQL AS BACKEND
12 PYTHON CODE TO ADD, DISPLAY AND SEARCH RECORD OF A
CUSTOMER USING MYSQL AS BACKEND
13 SQL
PROGRAM NO. : 1
# implementing a stack containing number
#-------------------------------------------------------------------------
def Push(stk):
num = int(input("Enter a number to be pushed : "))
stk.append(num)
#-------------------------------------------------------------------------
def Pop(stk):
# check for empty stack
if stk == [] :
print("Stack is empty")
else :
num = stk.pop()
print(num , " is popped from stack ")
#-------------------------------------------------------------------
def displaystk(stk):
print("\nContents of stack are : \n")
L = len(stk)
for i in range(L-1, -1, -1) :
print(stk[i], end = ",")
#---------------------------------------------------------------------
def menu():
print("\t\t\t Main Menu \n")
print("Press 1 : To Push an element")
print("Press 2 : To Pop an element")
print("Press 3 : To Display the stack")
print("Press 4 : To Exit")
#--------------------------------------------------------------------
def main(stk) :
while True :
menu()
ch = int(input("\t\tEnter your choice : "))
if ch == 1:
Push(stk)
elif ch == 2 :
Pop(stk)
elif ch == 3 :
displaystk(stk)
elif ch == 4 :
print("\n Good Bye !!!!")
break
else :
print("Invalid Choice !!! ")
#--------------------------------------OUTPUT -----------------------------------------------
Main Menu
#---------------------------------------------------------------------------------------
def Pop(stk):
# check for empty stack
if stk == [] :
print("Stack is empty")
else :
rec = stk.pop()
print(rec[0] , " is popped from stack ")
#-----------------------------------------------------------------------------------------
def displaystk(stk):
print("\nContents of stack are : \n")
L = len(stk)
for i in range(L-1, -1, -1) :
print(stk[i][0], stk[i][1], sep = '\t')
#-------------------------------------------------------------------------------------
def menu():
print("\t\t\t Main Menu \n")
print("Press 1 : To Push an element")
print("Press 2 : To Pop an element")
print("Press 3 : To Display the stack")
print("Press 4 : To Exit")
#---------------------------------------------------------------------------------
def main(stk) :
while True :
menu()
ch = int(input("Enter your choice : "))
if ch == 1:
Push(stk)
elif ch == 2 :
Pop(stk)
elif ch == 3 :
displaystk(stk)
elif ch == 4 :
print("\n Good Bye !!!!")
break
else :
print("Invalid Choice !!! ")
#----------------------------------------------OUTUT--------------------------------------------------------------------------
Main Menu
Jugal 7000.0
Manish 4000.0
Main Menu
Manish 4000.0
Main Menu
#-------------------------------------------------
def menu():
print("\t\t\t Main Menu \n")
print("Press 1 : To add records in a dictionary")
print("Press 2 : To Push the name into stack")
print("Press 3 : To Display the stack")
print("Press 4 : To Exit")
#-------------------------------------------------------
def main(stk) :
while True :
menu()
ch = int(input("Enter yout choice : "))
if ch == 1:
records = create_dictionary()
elif ch == 2 :
push(records,stk)
elif ch == 3 :
display_stack(stk)
elif ch == 4 :
print("\n Good Bye !!!!")
break
else :
print("Invalid Choice !!! ")
#------------------------------------OUTPUT------------------------------------------------------------
Main Menu
Main Menu
Press 1 : To add records in a dictionary
Press 2 : To Push the name into stack
Press 3 : To Display the stack
Press 4 : To Exit
Enter your choice : 2
Main Menu
uma
manish
Main Menu
#---------------------------------------------------------
def readcsv():
f = open("marks.csv", "r", newline ='')
records = csv.reader(f)
for r in records :
print(','.join(r))
f.close()
#------ main----------------
createcsv()
readcsv()
#----------------------------------OUTPUT -----------------------------------------------------
Enter Name : Yogesh
Enter Class : 12
Enter Marks : 89
Wish to write more records (Y/N) : y
Enter Name : Tarun
Enter Class : 12
Enter Marks : 90
Wish to write more records (Y/N) : n
File Created
Name,class,Marks
Yogesh,12,89.0
Tarun,12,90.0
“””
PROGRAM NO. : 5
Program to store Roll Number, Name and Marks of Students in a binary file
and display the records. Program Also search a particular record.
“””
import pickle
#-------------------------------------------------------------
def creatbinary():
file = open("Marks.dat", "wb")
Records = []
while True :
R = int(input("Enter The Roll Numbers :"))
Nm = input("Enter Name :")
M = float(input("Enter Marks : "))
rec = [R,Nm, M]
Records.append(rec)
ch = input("Wish to enter More Records (Y/N): ")
if ch == 'N' or ch == 'n' :
break
pickle.dump(Records,file)
file.close()
#-------------------------------------------------------------
def readbinaryfile():
file = open("Marks.dat", 'rb')
try :
while True :
Records = pickle.load(file)
for rec in Records :
print(rec[0], rec[1], rec[2], sep = '\t')
except EOFError :
pass
file.close()
#-------------------------------------------------------------
def Searchrec():
rn = int(input("\n\n\t\tEnter the roll number : "))
file = open("Marks.dat", 'rb')
try :
while True :
Records = pickle.load(file)
found = False
for rec in Records :
if rec[0] == rn :
found = True
print("Name : ", rec[1])
print("Marks :" , rec[2])
break
if found == False:
print("Record Does not Exist")
except EOFError :
pass
file.close()
#-------------main -------------------------------------------
def main():
while True :
menu()
opt = int(input("Enter Your Option : "))
if opt == 1 :
creatbinary()
elif opt == 2 :
readbinaryfile()
elif opt == 3 :
Searchrec()
elif opt == 4 :
break
else :
print("\n\n\t\t Invalid Choice")
#---------------------------------------------------------------
main()
#-------------------------OUTPUT-----------------------------------------------------------------
MAIN MENU
MAIN MENU
MAIN MENU
“””
import pickle
#-------------------------------------------------------------
def creatbinary():
file = open("Marks.dat", "wb")
Records = []
while True :
R = int(input("Enter The Roll Numbers :"))
Nm = input("Enter Name :")
M = float(input("Enter Marks : "))
rec = [R,Nm, M]
Records.append(rec)
ch = input("Wish to enter More Records (Y/N): ")
if ch == 'N' or ch == 'n' :
break
pickle.dump(Records,file)
file.close()
#-------------------------------------------------------------
def readbinaryfile():
file = open("Marks.dat", 'rb')
try :
while True :
Records = pickle.load(file)
for rec in Records :
print(rec[0], rec[1], rec[2], sep = '\t')
except EOFError :
pass
file.close()
#-------------------------------------------------------------
def Deleterec():
rn = int(input("\n\n\t\tEnter the roll number : "))
file = open("Marks.dat", 'rb')
Newrecords = []
try :
while True :
Records = pickle.load(file)
found = False
for rec in Records :
if rec[0] == rn :
found = True
else:
Newrecords.append(rec)
except EOFError :
file.close()
if found == False:
print("Record Does not Exist")
else:
file = open("Marks.dat", 'wb')
pickle.dump(Newrecords, file)
print("Record is deleted successfully")
file.close()
#-------------main -------------------------------------------
def main():
while True :
menu()
opt = int(input("Enter Your Option : "))
if opt == 1 :
creatbinary()
elif opt == 2 :
readbinaryfile()
elif opt == 3 :
Deleterec()
elif opt == 4 :
break
else :
print("\n\n\t\t Invalid Choice")
#---------------------------------------------------------------
main()
#-----------------------------OUTPUT -------------------------------------------------------
MAIN MENU
MAIN MENU
MAIN MENU
#-------------main -------------------------------------------
def main():
while True :
menu()
opt = int(input("Enter Your Option : "))
if opt == 1 :
creatbinary()
elif opt == 2 :
readbinaryfile()
elif opt == 3 :
Modifyrec()
elif opt == 4 :
break
else :
print("\n\n\t\t Invalid Choice")
#---------------------------------------------------------------
main()
#---------------------------------OUTPUT------------------------------------------------------------------
MAIN MENU
MAIN MENU
Number of vowels : 29
MAIN MENU
#-------------------------------------OUTPUT -------------------------------------------------------------------
MAIN MENU
MAIN MENU
"""
PROGRAM NO. 10
PYTHON CODE FOR CREATING AND DISPLAY A TEXT FILE.
PROGRAM ALSO COUNT THE NUMBER OF LINES STARTING WITH
ALPHABETS 'A' OR 'a' AS PER USER'S CHOICE
"""
def create_text() :
file = open("DIARY.txt", "w")
print("Enter the text (# to stop ) : \n")
while True :
S = input()
if S == '#' :
break
file.write(S)
file.write('\n')
file.close()
#---------------------------------------------
def disp_text():
file = open("DIARY.txt", "r")
text = file.read()
print("\n\nContents of text file : \n")
print(text)
file.close()
#----------------------------------------------
def countlines():
file = open("DIARY.txt", "r")
Lines = file.readlines()
cnt = 0
for L in Lines :
if L[0] == 'A' or L[0] == 'a' :
cnt = cnt +1
print("Number of lines starting with 'A' or 'a' = ", cnt)
file.close()
#--------------------------------------------------OUTPUT---------------------------------------------------------
MAIN MENU
MAIN MENU
Invalid Choice
MAIN MENU
#-------------------------------------------------------------
# function : To delete record of a Customer
#-------------------------------------------------------------
def delete_customer():
#-------------------------------------------------------------------------
# function to display the Customers Detail
#-------------------------------------------------------------------------
def disp_customer():
system("cls")
print("\n\n")
print('{:^100s}'.format("List of Customers"))
print("\n\n")
sql = "Select * from Customer"
Dbcur.execute(sql)
records = Dbcur.fetchall()
draw_line("=",100)
print('{:^10s} {:^20s} {:^25s} {:^25s}'
.format("CUST_ID" ,"NAME","ADDRESS", "PHONE NUMBER"))
draw_line("=",100)
for rec in records :
print('{:^10s} {:<25s} {:<25s} {:<10d}'
.format(rec[0] ,rec[1], rec[2],rec[3]))
draw_line('_',100)
print("\n\n\t\t\t Press any key to continue.... ", end = '')
input()
#----------------------------------------------------------
def database_setup():
query = "Drop database BOOKSHOP1"
query1 = "Create Database BOOKSHOP1"
query2 = "USE BOOKSHOP1"
query3 = "Create table Customer ( cust_id char(4) Primary Key, CName
varchar(25),\ Caddress varchar(30), phone bigint(10))"
Dbcur.execute(query)
Dbcur.execute(query1)
Dbcur.execute(query2)
Dbcur.execute(query3)
#-----------------------------------------------------------------------------------------------------------------
def Customer_app():
database_setup()
while True :
system("cls")
Customer_menu()
ch = int(input("\n\n\t\t\tEnter your choice : "))
if ch == 1 :
add_customer()
elif ch == 2 :
delete_customer()
elif ch == 3 :
disp_customer()
elif ch == 4:
Dbcur.close()
Dbconn.close()
break
else :
print("Invalid Choice !!!! ")
print("\n\n\t\t\t Press any key to continue.... ", end = '')
input()
#--------------------------------------------------------
Customer_app()
#------------------------------------------------OUTPUT--------------------------------------------------------------------
Customer Management
Customer id : C104
===========================================================================
Customer Name : JUGAL NATH
===========================================================================
Customer Address : 56 NAI WALA KAROL BAGH
===========================================================================
Phone Number : 6278288289
===========================================================================
1 record is inserted
Press any key to continue........
Customer Management
List of Customers
====================================================================================
CUST_ID NAME ADDRESS PHONE NUMBER
====================================================================================
C104 JUGAL NATH 56 NAI WALA KAROL BAGH 6278288289
_____________________________________________________________________________________
C105 AMIT KUMAR 56 OLD RAJNDER NAGAR 7828289728
_____________________________________________________________________________________
from os import *
import mysql.connector as conn
Dbconn = conn.connect(host = 'localhost', user = 'root',passwd = 'mysql')
Dbcur = Dbconn.cursor()
#----------------------------------------------------------------
def draw_line(ch = '=',n = 100):
for i in range(n):
print(ch, end = '')
print()
#----------------------------------------------------------------
def Customer_menu():
print("\t\t\t\t\tCustomer Management \n\n")
print("\t\t\t Press 1 : To Add new Customer \n")
print("\t\t\t Press 2 : To Search a Customer \n")
print("\t\t\t Press 3 : To Display the detail of Customer \n ")
print("\t\t\t Press 4 : To Exit ")
#----------------------------------------------------------------
# function : To add a new Customer
#----------------------------------------------------------------
def add_customer():
system("cls")
print("\n\n\t\t\t\tCustomer id : ", end = '')
cid = input()
draw_line()
print("\n\n\t\t\t\tCustomer Name : ", end = '')
cname = input()
draw_line()
print("\n\n\t\t\t\tCustomer Address : ", end = '')
cadd = input()
draw_line()
print("\n\n\t\t\t\tPhone Number : ", end = '')
cphone = int(input())
draw_line()
Customer id : C101
=====================================================================================
Customer Name : HIMANSHU
=====================================================================================
Customer Address : 7/10 ROOP NAGAR DELHI
Phone Number : 6726772922
=====================================================================================
1 record is inserted
Press any key to continue........
Customer Management
List of Customers
=====================================================================================
CUST_ID NAME ADDRESS PHONE NUMBER
=====================================================================================
C101 HIMANSHU 7/10 ROOP NAGAR DELHI 6726772922
_____________________________________________________________________________________
C105 AMIT KUMAR 56 OLD RAJNDER NAGAR 7828289728
_________________________________________________________________________________
C106 MANISH KHURANA 16A R-BLOCK NEW RAJINDER NGR 9810086865
____________________________________________________________________________________
Press any key to continue....
Customer Management
Detail of Customer
Customer id : C101
Customer's Name : HIMANSHU
Customer's Address : 7/10 ROOP NAGAR DELHI
Phone Number : 6726772922
from os import *
import mysql.connector as conn
Dbconn = conn.connect(host = 'localhost', user = 'root',passwd = 'mysql')
Dbcur = Dbconn.cursor()
#----------------------------------------------------------------
def draw_line(ch = '=',n = 100):
for i in range(n):
print(ch, end = '')
print()
#----------------------------------------------------------------
def Customer_menu():
print("\t\t\t\t\tCustomer Management \n\n")
print("\t\t\t Press 1 : To Add new Customer \n")
print("\t\t\t Press 2 : To Modify a Customer \n")
print("\t\t\t Press 3 : To Display the detail of Customer \n ")
print("\t\t\t Press 4 : To Exit ")
#----------------------------------------------------------------
# function : To add a new Customer
#----------------------------------------------------------------
def add_customer():
system("cls")
print("\n\n\t\t\t\tCustomer id : ", end = '')
cid = input()
draw_line()
print("\n\n\t\t\t\tCustomer Name : ", end = '')
cname = input()
draw_line()
print("\n\n\t\t\t\tCustomer Address : ", end = '')
cadd = input()
draw_line()
print("\n\n\t\t\t\tPhone Number : ", end = '')
cphone = int(input())
draw_line()
#---------------------------------------------------------------
# function : To search a customer
#---------------------------------------------------------------
def modify_customer():
bn = input("Enter Cust_Id : ")
query = "Select * from Customer where Cust_id = %s"
data = (bn,)
Dbcur.execute(query,data)
r = Dbcur.fetchone()
if r == []:
print("Customer does not Exist")
else :
system("cls")
print("\n\n")
print('{:^100s}'.format("Detail of Customer"))
print("\n\n")
print("\t\t\tCustomer id : ", r[0], end = '\n')
print("\t\t\tCustomer's Name : ", r[1], end = '\n')
print("\t\t\tCustomer's Address : ", r[2], end = '\n')
print("\t\t\tPhone Number : ", r[3], end = '\n')
opt = input("\n\n\t\t\t Want to modify(Y/N) :")
if opt in 'Yy' :
system("cls")
draw_line()
print("\n\n\t\t\t\tCustomer Name : ", end = '')
cname = input()
draw_line()
print("\n\n\t\t\t\tCustomer Address : ", end = '')
cadd = input()
draw_line()
print("\n\n\t\t\t\tPhone Number : ", end = '')
cphone = int(input())
draw_line()
sql = "UPDATE Customer SET Cname = '{}', Caddress = '{}',\
phone = {} where cust_id = '{}'".format(cname,cadd,cphone,bn)
Dbcur.execute(sql)
Dbconn.commit()
print(Dbcur.rowcount, " record is updated")
def Customer_app():
database_setup()
while True :
system("cls")
Customer_menu()
ch = int(input("\n\n\t\t\tEnter your choice : "))
if ch == 1 :
add_customer()
elif ch == 2 :
modify_customer()
elif ch == 3 :
disp_customer()
elif ch == 4:
Dbcur.close()
Dbconn.close()
break
else :
print("Invalid Choice !!!! ")
print("\n\n\t\t\t Press any key to continue.... ", end = '')
input()
#--------------------------------------------------------
Customer_app()
#------------------------------------------------------OUTPUT -------------------------------------------------------------
Customer Management
Customer Management
List of Customers
=====================================================================================
CUST_ID NAME ADDRESS PHONE NUMBER
====================================================================================
C101 HIMANSHU 7/10 ROOP NAGAR DELHI 6726772922
____________________________________________________________________________________
C105 AMIT KUMAR 56 OLD RAJNDER NAGAR 7828289728
____________________________________________________________________________________
C106 JDH DHHJDJ 2788273822
____________________________________________________________________________________
Press any key to continue....
Customer Management
Want to modify(Y/N) :Y
Detail of Customer
================================================================================
Customer Name : MANISH KHURANA
================================================================================
Customer Address : 16 R BLOCK NEW RAJINDER NGR
================================================================================
Phone Number : 9810030678
================================================================================
1 record is updated
Press any key to continue....
Customer Management
TABLE : RECIPIENT
Recipient.
8. Delete the records of Recipient whose name start with letter ‘S’.
Ans.:
(1) CREATE DATABASE STORE
TABLE : PRODUCT
TABLE : CLIENT
3. Display the ClientName, City from table Client and Productname, Price from the
TABLE : ITEM
TABLE : CUSTOMER
2. Display the details of ITEMS whose price is in the range of 35000 to 55000.
3. Display the Customer Name, City from table Customer and Item name, Price from
(2) SELECT * FROM ITEMS WHERE PRICE BETWEEN 35000 AND 55000;
TABLE : CONSIGNEE
2. Display the CneeID, CnorName, CnorAddress, CneeName, CneeAddress for every Consignee.
5. Delete the records of Consignee whose name end with letter ‘e’.
CUSTOMER. COMPANY
CUSTOMER
CUSTID NAME PRICE QTY CID
101 Rohan Sharma 70000 20 222
102 Deepak Kumar 50000 10 666
103 Mohan Kumar 30000 5 111
104 Sahil Bansal 35000 3 333
105 Neha Soni 25000 7 444
106 Sonal Aggarwal 20000 5 333
107 Arjun Singh 50000 15 666
(i) To display those company name which are having prize less than 30000.
(iii) To increase the prize by 1000 for those customer whose name starts with
„S‟
(v)
COUNT(*) CITY
3 DELHI
2 MUMBAI
1 MADRAS
(vi)
MIN(PRICE) MAX(PRICE)
50000 70000
(vii)
AVG(QTY)
37500
(viii)
NAME CITY PRICE
NOKIA MUMBAI 70000
SONY MUMBAI 25000