Ssce Computer Science Lab Manual - 2023-2024

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

COMPUTER SCIENCE

TABLE OF CONTENT
QUESTION
PAPER TABLE OF CONTENT
SET NO
TEXT FILE – 2 PROGRAMS
1
SQL

BINARY FILE
2
SQL

CSV FILE – 2 PROGRAMS


3
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:

Write a Python program using user-defined function TextFile(), which takes


file name as argument, to create a text file named text1.txt and write some
text into it, and then read the same text file and display the following
information:
Note: Open and Close the file only once.

• 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:

Thus the program was executed successfully.


BINARY FILE QP - SET 2
PROBLEM STATEMENT:
Write a Python program with the following specification: Create a binary file to store the
details of the customer as follows. Dictionary Name: Customer
Key: Phone number ,Value: Name The program must have the following options.
• Enter data into the file
• Search name by giving phone number
• Display the records.

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

Enter Customer Name:NILA


Enter Phone number:123456

1.Enter Customer Details


2.Search Customer Details
3.Display Customer Details
4.Exit
Enter Ur Choice: 1

Enter Customer Name:USHA


Enter Phone number:987654

1.Enter Customer Details


2.Search Customer Details
3.Display Customer Details
4.Exit
Enter Ur Choice: 3
Customer Name: NILA
Phone Number: 123456
Customer Name: USHA
Phone Number: 987654

1.Enter Customer Details


2.Search Customer Details
3.Display Customer Details
4.Exit
Enter Ur Choice: 2

Enter Phone number to be Searched: 123456


Customer Name: NILA
Phone Number: 123456

1.Enter Customer Details


2.Search Customer Details
3.Display Customer Details
4.Exit
Enter Ur Choice: 4
Thank You !!!
Result
The program was executed successfully
CSV FILE QP - SET 3a.
PROBLEM STATEMENT:

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

Do you want to continue(y/n)


Y
Enter Empno:2
Enter Ename:NILA
Enter salary:9000

Do you want to continue(y/n)


Y
Enter Empno:3
Enter Ename:KALA
Enter salary:7500

Do you want to continue(y/n)


N

DATA WRITTEN SUCCESSFULLY

Reading data from a CSV file..

['EmpNo', 'Empname', 'Salary']


['1', 'MALA', '2500']
['2', 'NILA', '9000']
['3', 'KALA', '7500']

sum of salary of all employees:


19000
['2', 'NILA', '9000']
['3', 'KALA', '7500']

Number of employees salary above 7000:


2

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

Enter Course ID: 3


Enter Course Name: PYTHON
Enter Faculty Name: KALA
Enter Course Fees: 4500
1.Append
2.Delete
3.Exit
Enter Ur Choice: 2

Enter Course ID to be Deleted: 2


Data Deleted Successfully
The records after deletion
['1', 'C++', 'NILA', '2500.0']
['3', 'PYTHON', 'KALA', '4500.0']
1.Append
2.Delete
3.Exit
Enter Ur Choice: 3

Thank You !!!


RESULT:

Thus the program was executed successfully.


STACK QP - SET 4a.
PROBLEM STATEMENT:

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.

STACK QP - SET 5a.


PROBLEM STATEMENT
Write an interactive menu driven program to perform Push, Pop, Peek, Search,
and Display operations implementing Stack technique using a List of integers.

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]

def Display (stk) :


if isEmpty (stk) :
print ('stack empty')
else:
top = len (stk) -1
print (stk[top], '<- top' )
for a in range (top- 1, -1, -1 ) :
print (stk [a])
stack = [ ]
top = None
while True:
print ('STACK OPERATIONS')
print ('1. Push')
print ('2. Pop')
print ('3. Peek')
print ('4. Display stack')
print ( '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 ('underflow: stack is empty !')
else:
print ('popped item is', item)
elif ch == 3:
item = peek (stack)
if item =='underflow':
print ('underflow! Stack is empty!')
else:
print ('Topmost item is', item)
elif ch == 4:
Display (stack)
elif ch == 5:
break
else:
print ('Invalid choice!')

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.

FUNCTIONS – USER DEFINED QP - SET 5b.


PROBLEM STATEMENT:
Write a Python program to display different patterns using user defined
functions.
AIM:
To write a Python program to display different patterns using user defined
functions.

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.

SQL QP – SET .1,3 & 5


Study the following tables CLIENT and BILL and write SQL queries for the
questions (i) to (iv) .
TABLE: CLIENT

TABLE: BILL

(i) Display a report containing cust_id, cust_name, Item, qty, price and bill
amount. Bill amount is calculated as qty*price.

SELECT A.CUST_ID, A.CUST_NAME, B.ITEM, B.QTY, B.PRICE,


QTY*PRICE AS "BILL AMOUNT” FROM CLIENT A, BILL B WHERE
A.CUST_ID = B.CUST_ID;

(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.

SELECT CUST_NAME , CITY FROM CLIENT ORDER BY CITY;

Result:
Thus the queries were executed successfully.

SQL QP – SET 2 & 4


Study the following tables FLIGHTS and FARES and write SQL queries for
the questions (i) to (iv).

TABLE: FLIGHTS

TABLE: FARES

Give Query:

(i) Display FL_NO and NO_FLIGHTS from “KANPUR” TO “BANGALORE” from


the table FLIGHTS.

SELECT FL_NO, NO_FLIGHTS FROM FLIGHTS WHERE SOURCE=”KANPUR” AND


DESTINATION=”BANGALORE”;

(ii) Display the minimum fare “Indian Airlines” is offering.


SELECT MIN(FARE) FROM FARES WHERE AIRLINES='INDIAN AIRLINES';
(iii) Display airlines from FARES in ascending order of airlines and descending
order of fare.
SELECT AIRLINES FROM FARES ORDER BY AIRLINES ASC, FARE DESC;

(iv) Display the total no.of flights from FLIGHTS where SOURCE starts with
letter ‘M’ and the no.of stops is more than 2.

SELECT COUNT(*) AS "RESULT" FROM FLIGHTS WHERE SOURCE LIKE


'M%' AND NO_STOPS>2;

Result:
Thus the queries were executed successfully.

You might also like