Professional Documents
Culture Documents
S PUBLIC
SCHOOL
SESSION: 2022-23
CBSE (CENTRAL BOARD OF SECONDARY
EDUCATION)
COMPUTER SCIENCE
“Programming File + SQL”
OUTPUT:
1
# WAP to sort a list using Insertion
Sort:
CODE:
l = []
n = int(input("Enter the length of list: "))
for i in range(n):
e = int(input("Enter the element: "))
l.append(e)
print("List entered is: ",l)
for i in range(1, n):
key = l[i]
j = i-1
while j >= 0 and key < l[j]:
l[j+1] = l[j]
j = j-1
else:
l[j+1] = key
print("List after using insertion sort: ", l)
OUTPUT:
2
# WAP to use Linear search in a list:
CODE:
def l_search(l, ele):
n = len(l)
found = False
for i in range(n):
if l[i] == ele:
print("Element found at index: ",i)
print("Element found at position: ", i + 1)
found = True
if not found:
print("Element not found")
L = []
n=int(input("Enter number of elements in the list: "))
for i in range(n):
ele = int(input("Enter element: "))
L.append(ele)
print("List created: ",L)
s=int(input("Enter element you want to search: "))
l_search(L, s)
OUTPUT:
OUTPUT:
4
# WAP to show Implementation of a 2-
D List (Sum of all elements in a matrix
of order m X n):
CODE:
s=0
a = []
m = int(input("Enter no. of rows: "))
n = int(input("Enter no. of columns: "))
for i in range(m):
r = []
for j in range(n):
x=int(input(f"Enter Element in {i} row and {j} column: "))
r.append(x)
a.append(r)
print("Entered list: ")
for i in range(m):
print(" ")
for j in range(n):
s += a[i][j]
print(a[i][j], end=" ")
print(" ")
print("Sum of all elements:",s)
OUTPUT:
5
# WAP to create a text file:
CODE:
f = open("text_file.txt",'w')
ch = 'y'
while ch=='y'.casefold():
d = input("Enter data to write on file:\n")
s = d + "\n"
f.write(s)
ch = input("Do you want to enter more data?\nEnter 'y' to continue.")
f.close()
f = open("text_file.txt",'r')
r = f.read()
print("The text file contains: ",r)
f.close()
OUTPUT:
6
# WAP to count number of lines in the
text file:
CODE:
f = open("text_file.txt",'r')
x = f.readlines()
s = len(x)
print("No. of lines present in file: ", s)
f.close()
OUTPUT:
7
# WAP to count number of lines
starting with uppercase in text file:
CODE:
c=0
f = open("text_file.txt",'r')
ch = f.readline()
while ch:
if ch[0].isupper():
c += 1
ch = f.readline()
print("No. of lines starting with uppercase letters are: ", c)
f.close()
OUTPUT:
8
OUTPUT:
OUTPUT:
9
# WAP to create a binary file by
writing product no., name and price:
CODE:
import pickle
f = open("Record.dat",'wb')
ch = 'y'
while ch == 'y' or ch == 'Y':
d = {}
d['P_no.'] = int(input("Enter product no.: "))
d['P_name'] = input("Enter Product Name: ")
d['Price'] = float(input("Enter Price: "))
pickle.dump(d,f)
ch = input("Enter Y to enter more data\nEnter N to exit:")
f.close()
f = open("Record.dat",'rb')
try:
while True:
x = pickle.load(f)
print(x)
except EOFError:
f.close()
OUTPUT:
10
# WAP to search any product in binary
file using product no.:
CODE:
import pickle
f = open("Record.dat",'rb')
y = int(input("Enter product no. for searching: "))
try:
while True:
l=pickle.load(f)
if l['P_no.'] == y:
print(l)
except EOFError:
f.close()
OUTPUT:
11
# WAP to count no. of products with
price more than 100:
CODE:
import pickle
c=0
f = open("Record.dat",'rb')
try:
while True:
x=pickle.load(f)
if x['Price'] > 100:
c += 1
except EOFError:
print("No. of products more than 100 are: ", c)
f.close()
OUTPUT:
12
# WAP to update price of the product
whose number is entered by user:
CODE:
import pickle
f = open("Record.dat",'rb')
w = int(input("Enter pr no. for the product to be updated: "))
y = float(input("Enter new price: "))
try:
print("Product details after updating: ")
while True:
x=pickle.load(f)
if x['P_no.'] == w:
x['Price'] = y
print(x)
except EOFError:
f.close()
OUTPUT:
13
# WAP to copy details of all the
products having price more than 100:
CODE:
import pickle
f=open("Record.dat", 'rb')
f1=open("temp.dat", 'wb')
try:
while True:
x=pickle.load(f)
if x['Price'] > 100:
pickle.dump(x, f1)
except EOFError:
f.close()
f1.close()
f1 = open("temp.dat", 'rb')
try:
print("File having details of products with price more than 100: ")
while True:
x=pickle.load(f1)
print(x)
except EOFError:
f1.close()
OUTPUT:
14
# WAP to create a CSV File with roll
no., name and marks of ‘n’ students:
CODE:
import csv
f = open("Student.csv",'w')
w = csv.writer(f)
w.writerow(['Roll_no.','Name','Marks'])
n=int(input("Enter no. of records to be added in file: "))
for i in range(n):
r = int(input("Enter Roll no.: "))
n = input("Enter name: ")
m = float(input("Enter marks: "))
s = [r,n,m]
w.writerow(s)
f.close()
with open("Student.csv", 'r', newline = '\r\n') as f:
r=csv.reader(f)
for x in r:
print(x)
OUTPUT:
15
# WAP to count no. of records having
marks more than 90:
CODE:
import csv
f = open("Student.csv",'w')
w = csv.writer(f)
w.writerow(['Roll_no.','Name','Marks'])
n=int(input("Enter no. of records to be added in file: "))
for i in range(n):
r = int(input("Enter Roll no.: "))
n = input("Enter name: ")
m = float(input("Enter marks: "))
s = [r,n,m]
w.writerow(s)
f.close()
with open("Student.csv", 'r', newline = '\r\n') as f:
r=csv.reader(f)
for x in r:
print(x)
OUTPUT:
16
# WAP to show implementation of
stack:
CODE:
def isempty(stk):
if stk == []:
return True
else:
return False
def push(stk, item):
stk.append(item)
top=len(stk)-1
def pop(stk):
if isempty(stk):
return "underflow"
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item
def peek(stk):
if isempty(stk):
return "underflow"
else:
top=len(stk)-1
return stk[top]
def display(stk):
if isempty(stk):
print("stack empty")
else:
top=len(stk)-1
print(":Elements in stack are:")
for a in range(top,-1,-1):
print(stk[a])
17
stack=[]
top=None
while True:
print("stack operations")
print("1. Push \t 2. pop \t 3. peek \t 4. display stack \t 5. exit")
ch=int(input("enter your choice(1-5)"))
if ch==1:
item=int(input("enter item:"))
push(stack,item)
elif ch==2:
item=pop(stack)
if item=="underflow":
print("stack is empty")
else:
print("popped item is",item)
elif ch==3:
item=peek(stack)
if item=="underflow":
print("stack is empty")
else:
print("topmost item is",item)
elif ch==4:
display(stack)
elif ch==5:
break
else:
print("invalid choice")
18
OUTPUT:
19