KANHA MAKHAN MILLENIUM
SCHOOL, MATHURA
AISSCE
‘2023-24’
COMPUTER SCIENCE
PRACTICAL FILE
Submitted By:- Submitted to:-
Aakarsh Sharma Mr. Ashish Agrawal
XII-Science (HOD & PGT-
Roll No :- Computer Science)
Q1) Write a program in python to find the
factorial of a natural number.
CODE :-
num = int(input("Enter a Natural number : "))
factorial = 1
if num < 0:
print("Sorry, factorials do not exist for
negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of", num, "is", factorial)
OUTPUT :-
Q2) Write a program in python to read a
text file line by line and display each word
separated by ‘#’.
CODE :-
f = open([Link], "r")
data= [Link]()
for i in data:
print([Link]("",'#'))
INPUT :-
OUTPUT :-
Q3) Write a program in python to read a text
file and display the number of
vowels/consonants/uppercase/lowercase
characters.
CODE :-
def cnt ():
f=open(“[Link]”,”r”)
cont=[Link]()
print(cnt)
v=0
cons=0
l_c_l=0
u_c_l=0
for ch in cont:
if ([Link]()):
l_c_l+=1
elif([Link]()):
u_c_l+=1
ch=[Link]()
if( ch in ['a','e','i','o','u']):
v+=1
elif (ch in ['b','c','d','f','g',
'h','j','k','l','m',
'n','p','q','r','s',
't','v','w','x','y','z']):
cons+=1
[Link]()
print("Vowels are : ",v)
print("consonants are : ",cons)
print("Lower case letters are : ",l_c_l)
print("Upper case letters are : ",u_c_l)
cnt()
INPUT :-
OUTPUT :-
Q4) Write a program in python to remove
all the lines that contain the character ‘a’
and write into another file.
CODE :-
fin=open(“[Link]”,”r”)
fout=open(“[Link]”,”r”)
s=[Link]()
for j in s:
if ‘a’ in j:
[Link](j)
[Link]()
[Link]()
INPUT :-
OUTPUT :-
Q5) Write a program in python to create a
binary file with name, roll no., marks. Input
a roll no. and update the marks.
CODE :-
Import pickle
S={}
f=open(“[Link]”,”wb”)
c=’y’
while c==’y’ or c==’Y’:
rno=int(input(“Enter the roll no. of the student : ”))
name=input(“Enter the name of the student : ”)
marks=int(input(“Enter the marks of the student :
”))
S[‘RollNo’]=rno
S[‘Name’]=name
S[‘Marks’]=marks
[Link](S,f)
c=input(‘Do You Want to add more students (y/n):’)
[Link]()
f=open(‘[Link].,’rb+’)
rno=int(input(“Enter the roll no. of the student to be
updated : ”))
marks=int(input(“Enter the updated marks of the
student : ”))
[Link](0,0)
m=0
try:
while True:
pos=[Link]()
S=[Link](f)
if S[“RollNo”] == rno:
[Link](pos)
S[“Marks”]=marks
[Link](S,f)
m=m+1
except EOFError:
[Link]()
if m==0:
print(“Student not Found”)
else:
f=open(‘[Link]’,’rb’)
try:
while True:
S=[Link](f)
print(S)
except EOFError:
[Link]()
OUTPUT :-
Q6) Write a program in python to
implement a stack using a list.
CODE :-
def isEmpty(S):
if len(S)==0:
return True
else:
return False
def Push(S,item):
[Link](item)
top=len(S)-1
def Pop(S):
if isEmpty(S):
return "Underflow"
else:
val = [Link]()
if len(S)==0:
top=None
else:
top=len(S)-1
return val
def Peek(S):
if isEmpty(S):
return "Underflow"
else:
top=len(S)-1
return S[top]
def Show(S):
if isEmpty(S):
print("Sorry No items in Stack ")
else:
t = len(S)-1
print("(Top)",end=' ')
while(t>=0):
print(S[t],"<==",end=' ')
t- =1
print()
# main begins here
S=[] #Stack
top=None
while True:
print("**** STACK DEMONSTRATION ******")
print("1. PUSH ")
print("2. POP")
print("3. PEEK")
print("4. SHOW STACK ")
print("0. EXIT")
ch = int(input("Enter your choice :"))
if ch==1:
val = int(input("Enter Item to Push :"))
Push(S,val)
elif ch==2:
val = Pop(S)
if val=="Underflow":
print("Stack is Empty")
else:
print("\nDeleted Item was :",val)
elif ch==3:
val = Peek(S)
if val=="Underflow":
print("Stack Empty")
else:
print(“Top Item : ”,Peek(S))
elif ch==4:
print("Top Item :",val)
Show(S)
elif ch==0:
print("Bye")
break
OUTPUT :-
Q7) Write a program in python to create a
CSV file by entering User ID and Password,
read and search the password for given
User ID.
CODE :-
import csv
with open("[Link]", "w") as obj:
fileobj = [Link](obj)
[Link](["User Id", "password"])
while(True):
user_id = input("enter id: ")
password = input("enter password: ")
record = [user_id, password]
[Link](record)
x = input("press Y/y to continue and N/n to
terminate the program\n")
if x in "Nn":
break
elif x in "Yy":
continue
with open("[Link]", "r") as obj2:
fileobj2 = [Link](obj2)
given = input("enter the user id to be
searched\n")
for i in fileobj2:
next(fileobj2)
# print(i,given)
if i[0] == given:
print(i[1])
break
OUTPUT :-
Q8) Write a program in python to binary
search in the list.
CODE :-
def binary_search(arr, low, high, x):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
else:
return binary_search(arr, mid + 1, high, x)
else:
return -1
# Test LIST
L = [ 2, 3, 4, 10, 40 ]
x = 10
# Function call
result = binary_search(L, 0, len(L)-1, x)
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in list")
OUTPUT :-
Q9) Write a program in python for insertion
sort.
CODE :-
def insertionSort(arr):
n = len(arr)
if n <= 1:
return
for i in range(1, n):
key = arr[i]
j = i-1
while j >= 0 and key < arr[j]
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
L = [12, 11, 13, 5, 6]
insertionSort(L)
print(L)
OUTPUT :-
Q10) Create a student table and insert data.
Implement the following SQL commands on
the student table.
(i) Alter table to add new attribute and
modify data type and drop attribute.
(ii) Update table to modify data.
(iii) To display data in ascending or
descending order.
(iv) Remove the tuple or tuples
(v) Find the min, max, sum, count,
average.
CODE + OUTPUT :-