Ssce Computer Science Lab Manual - 2023-2024
Ssce Computer Science Lab Manual - 2023-2024
Ssce Computer Science Lab Manual - 2023-2024
TABLE OF CONTENT
QUESTION
PAPER TABLE OF CONTENT
SET NO
TEXT FILE – 2 PROGRAMS
1
SQL
BINARY FILE
2
SQL
STACK – 2 PROGRAMS
4
SQL
STACK
5
USER DEFINED FUNCTION
QP - SET 1 a.
TEXT FILE
PROBLEM STATEMENT:
Write a Python program to accept the details of a book like book number, book
name, author and price as input from the user and write it into a text file as
a single record.
AIM:
To write a Python program to accept the details of a book and write it into a
text file as a single record.
SOURCE CODE:
myfile=open("Book.txt","a")
ch='y'
while ch=='y' or ch=='Y':
bno=int(input("Enter a book number:"))
bname=input("Enter the book name:")
author=input("Enter the author name:")
price=int(input("Enter book price:"))
bookrec=str(bno)+","+bname+","+author+","+str(price)+"\n"
myfile.write(bookrec)
print("Data written successfully:")
ch=input("Do you want to continue: y/n")
myfile.close()
OUTPUT:
Enter a book number:1
Enter the book name:Harrypotter
Enter the author name:J.K.Rowling
Enter book price:569
Data written successfully:
Do you want to continue: y/nY
Enter a book number:2
Enter the book name:Ponniyin selvan
Enter the author name:kalki
Enter book price:1500
Data written successfully:
Do you want to continue: y/nn
RESULT:
Thus the program was executed successfully.
QP - SET 1 b.
TEXT FILE
PROBLEM STATEMENT:
• No. Of Vowels
• No. Of Digits
• No. Of Special Characters
• No. Of Consonants
AIM:
To write a Python program to write and read the text file text1.txt and to count
the no. of Vowels, Digits, Special Characters and Consonants.
SOURCE CODE:
def TextFile(FileName):
file1 = open(FileName,"w+")
ch ="y"
while ch=="y":
str1= input("Enter the string: ")
file1.write(str1)
file1.write("\n")
ch= input("Do you wish to continue? y/n: ")
file1.seek(0)
str1= file1.read()
vowels = 0
consonant = 0
specialChar = 0
digit = 0
for i in range(len(str1)):
ch = str1[i]
if (ch.isalpha()):
ch = ch.lower()
if (ch in "aeiou"):vowels += 1
else:consonant += 1
elif (ch.isdigit()):digit += 1
else:
specialChar += 1
print("A. Vowels: ", vowels)
print("B. Digits: ", digit)
print("C. Special Characters: ", specialChar-1)
print("D. Consonants: ", consonant)
file1.close()
FileName = input("Enter the file name :")
FileName +='.txt'
TextFile(FileName)
OUTPUT:
Enter the file name :poem
Enter the string: We are learning Python 123 $2%%
Do you wish to continue? y/n: y
Enter the string: Easy Learning 87890 !@&*
Do you wish to continue? y/n: n
A. Vowels: 12
B. Digits: 9
C. Special Characters: 16
D. Consonants: 19
RESULT:
AIM:
To write a Python program to create a binary file to store the details of the customer in a
dictionary.
SOURCE CODE:
import pickle
def EnterCustomerDetails():
Customer = dict()
CName = input('Enter Customer Name:')
PhNo = input('Enter Phone number:')
Customer[PhNo] = CName
f = open("Customer.dat",'ab')
pickle.dump(Customer,f)
f.close()
def SearchCustomerDetails(phno):
f = open("Customer.dat",'rb')
flag = False
while True:
try:
rec = pickle.load(f)
for pno in rec:
if pno == phno:
print('Customer Name: ',rec[phno])
print('Phone Number: ',phno)
flag = True
except EOFError:
break
if flag == False:
print('No Records found')
f.close()
def DisplayCustomerDetails():
with open("Customer.dat",'rb') as f :
while True:
try:
record = pickle.load(f)
for phno in record:
print('Customer Name: ',record[phno])
print('Phone Number: ',phno)
except EOFError:
break
ch=0
while(ch!=4):
print("\n1.Enter Customer Details \n2.Search Customer Details \n3.Display Customer
Details \n4.Exit")
ch=int(input("Enter Ur Choice: "))
print()
if(ch==1):
EnterCustomerDetails()
elif(ch==2):
phno=input("Enter Phone number to be Searched: ")
SearchCustomerDetails(phno)
elif(ch==3):
DisplayCustomerDetails()
elif(ch==4):
print("Thank You !!!")
else:
print("Enter the valid choice between 1 and 4")
OUTPUT
1.Enter Customer Details
2.Search Customer Details
3.Display Customer Details
4.Exit
Enter Ur Choice: 1
Write a Python program to find the sum of salary of all the employees and
count the number of employees who are getting salary more than 7000 using
functions in a CSV file. The file contains the details of Employee (EmpNo,
EmpName and salary).
AIM:
To write a Python program to find the sum of salary of all the employees and
count the number of employees who are getting salary more than 7000.
SOURCE CODE:
import csv
def writeemp():
f=open("employee.csv","w",newline='')
swriter=csv.writer(f)
swriter.writerow(['EmpNo','Empname','Salary'])
rec=[]
while True:
eno=int(input("Enter Empno:"))
ename=input("Enter Ename:")
salary=int(input("Enter salary:"))
data=[eno,ename,salary]
rec.append(data)
ch=input("\nDo you want to continue(y/n)\n")
if ch in "nN":
break
swriter.writerows(rec)
print("\nDATA WRITTEN SUCCESSFULLY\n")
f.close()
def read():
f=open("employee.csv","r")
print("\nReading data from a CSV file..\n")
sreader=csv.reader(f)
for i in sreader:
print(i)
f.close()
def sumsalary():
f=open("employee.csv","r")
sreader=csv.reader(f)
next(sreader)
sum1=0
for i in sreader:
sum1=sum1+int(i[2])
print("\nsum of salary of all employees:\n",sum1)
f.close()
def salagr():
f=open("employee.csv","r")
sreader=csv.reader(f)
count1=0
next(sreader)
for i in sreader:
if int(i[2])>7000:
print(i)
count1+=1
print("\nNumber of employees salary above 7000:\n",count1)
f.close()
writeemp()
read()
sumsalary()
salagr()
OUTPUT
Enter Empno:1
Enter Ename:MALA
Enter salary:2500
RESULT:
Thus the program was executed successfully.
CSV FILE QP - SET 3b.
PROBLEM STATEMENT:
Write a menu driven Python program to append and delete from a CSV file
containing the details of Course (CourseId, CourseName, Faculty, Fees).
AIM:
To write a menu driven Python program to append and delete from a CSV file
containing the details of Course (CourseId, CourseName, Faculty, Fees).
SOURCE CODE:
import csv
def Append():
csvf = open('CourseDetails.csv', mode = 'a',newline='')
mywr = csv.writer(csvf,delimiter=',')
CourseId = input("Enter Course ID: ")
CourseName = input("Enter Course Name: ")
Faculty = input("Enter Faculty Name: ")
Fees = float(input("Enter Course Fees: "))
mywr.writerow([CourseId, CourseName, Faculty, Fees])
csvf.close()
def Delete(crd):
found = False
csvf = open('CourseDetails.csv', mode = 'r')
temp=[]
myrd = csv.reader(csvf,delimiter=',')
for r in myrd:
if(r[0] != crd):
temp.append(r)
else:
found = True
csvf.close()
csvf = open('CourseDetails.csv', mode = 'w',newline='')
mywr = csv.writer(csvf,delimiter=',')
mywr.writerows(temp)
csvf.close()
if found == False:
print("No such Course in the File")
else:
print("Data Deleted Successfully")
print("The records after deletion")
for i in temp:
print(i)
ch=0
while(ch!=3):
print("\n1.Append \n2.Delete \n3.Exit")
ch=int(input("Enter Ur Choice: "))
print()
if(ch==1):
Append()
elif (ch==2):
crd=input("Enter Course ID to be Deleted: ")
Delete(crd)
elif (ch==3):
print("Thank You !!!")
else:
print("Enter the valid choice between 1 and 3")
OUTPUT
1.Append
2.Delete
3.Exit
Enter Ur Choice: 1
Enter Course ID: 1
Enter Course Name: C++
Enter Faculty Name: NILA
Enter Course Fees: 2500
1.Append
2.Delete
3.Exit
Enter Ur Choice: 1
Enter Course ID: 2
Enter Course Name: C
Enter Faculty Name: MALA
Enter Course Fees: 3000
1.Append
2.Delete
3.Exit
Enter Ur Choice: 1
Write a Python program to Push, Pop, and Display from a STACK containing
the details of CS marks as a list. The details are stored as an integer.
AIM:
To write an interactive menu driven Python program that will implement the
STACK created with the given details.
SOURCE CODE:
s=[]
c="y"
while (c=="y"):
print ("1. PUSH")
print ("2. POP ")
print ("3. Display")
print ("4. Exit")
choice=int(input("Enter your choice: "))
if (choice==1):
a=input("Enter any number :")
s.append(a)
elif (choice==2):
if (s==[]):
print ("Stack Empty")
else:
print ("Deleted element is : ",s.pop())
elif (choice==3):
l=len(s)
for i in range(l-1,-1,-1):
print (s[i])
elif (choice==4):
print('Thank you')
else:
print("Wrong Input")
c=input("Do you want to continue or not? ")
OUTPUT
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 1
Enter any number :32
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 1
Enter any number :95
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 2
Deleted element is : 95
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 3
32
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 4
Thank you
Do you want to continue or not? n
RESULT
Thus the program was executed successfully.
STACK QP - SET 4b.
PROBLEM STATEMENT:
Write a Python Program to perform stack operations,
a) Push the elements that are divisible by 3
b) Pop the elements from the stack
Aim :
To write an interactive menu driven Python program that will add the
elements in to the stack that are divisible by 3 and the pop the elements from
the stack.
Source Code:
s=[]
c="y"
while (c=="y"):
print ("1. PUSH")
print ("2. POP ")
print ("3. Display")
print ("4. Exit")
choice=int(input("Enter your choice: "))
if (choice==1):
a=int(input("Enter any number :"))
if (a%3 == 0):
s.append(a)
else:
print('The number is not dicisible by 3')
elif (choice==2):
if (s==[]):
print ("Stack Empty")
else:
print ("Deleted element is : ",s.pop())
elif (choice==3):
l=len(s)
for i in range(l-1,-1,-1):
print (s[i])
elif (choice==4):
print('Exit')
else:
print("Wrong Input")
c=input("Do you want to continue or not? ")
Output
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 1
Enter any number :12
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 1
Enter any number :30
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 1
Enter any number :11
The number is not divisible by 3
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 2
Deleted element is : 30
Do you want to continue or not? y
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 3
12
Do you want to continue or not? n
Result:
The program were executed successfully.
Aim:
To write an interactive menu driven program to perform Push, Pop, Peek,
Search, and Display operations implementing Stack technique using a List of
integers.
Source 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]
OUTPUT
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5) :1
Enter item:12
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5) :1
Enter item:45
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5) :1
Enter item:32
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5) :4
32 <- top
45
12
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5) :3
Topmost item is 32
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5) :2
popped item is 32
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5) :4
45 <- top
12
STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5) :5
Result:
The program was executed successfully.
SOURCE CODE:
def Horizontal():
n=int(input("Enter the no of Steps: "))
for i in range(1,n+1):
for j in range(1,i+1):
print(i, end=" ")
print()
def Vertical():
n=int(input("Enter the no of Steps: "))
for i in range(1,n+1):
for j in range(1,i+1):
print(j,end=" ")
print()
def Continous():
k=1
n=int(input("Enter the no of Steps: "))
for i in range(1,n+1):
for j in range(1,i+1):
print(k, end=" ")
k=k+1
print()
ch=0
while(ch!=4):
print("\n1.Horizontal \n2.Vertical \n3.Continous \n4.Exit")
ch=int(input("Enter Ur Choice: "))
if(ch==1):
Horizontal()
elif(ch==2):
Vertical()
elif(ch==3):
Continous()
elif(ch==4):
print("Thank you")
else:
print("Enter the valid choice between 1 and 4")
OUTPUT:
1.Horizontal
2.Vertical
3.Continous
4.Exit
Enter Ur Choice: 1
Enter the no of Steps: 5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
1.Horizontal
2.Vertical
3.Continous
4.Exit
Enter Ur Choice: 2
Enter the no of Steps: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1.Horizontal
2.Vertical
3.Continous
4.Exit
Enter Ur Choice: 3
Enter the no of Steps: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
1.Horizontal
2.Vertical
3.Continous
4.Exit
Enter Ur Choice: 4
Thank you
RESULT:
Thus the program was executed successfully.
TABLE: BILL
(i) Display a report containing cust_id, cust_name, Item, qty, price and bill
amount. Bill amount is calculated as qty*price.
(ii) Display how many customers have ordered Pizza in the month of
March.
SELECT COUNT(*) AS "CUSTOMER ORDERING PIZZA" FROM BILL
WHERE ITEM = 'Pizza' AND MONTH(ORD_DATE) = 3 ;
(iii) Count the number of customer who have ordered item worth more than
1700. Total amount = qty* price.
SELECT COUNT(*) AS "ABOVE 1700 CUSTOMER " FROM BILL WHERE QTY * PRICE > 1700;
(iv) Display the name of customer along with city in alphabetical order of
city.
Result:
Thus the queries were executed successfully.
TABLE: FLIGHTS
TABLE: FARES
Give Query:
(iv) Display the total no.of flights from FLIGHTS where SOURCE starts with
letter ‘M’ and the no.of stops is more than 2.
Result:
Thus the queries were executed successfully.