CS-Practical Programs-2023-24
CS-Practical Programs-2023-24
COMPUTER SCIENCE
2023-2024
CLASS - XII
#Calling the relevant method based on users choice using if-else loop
if choice == 1:
print("\nAddition of two numbers")
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
addition(a,b)
elif choice == 2:
print("\nSubtraction of two numbers")
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
subtraction(a,b)
elif choice == 3:
print("\nMultiplication of two numbers")
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
multiplication(a,b)
VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024
elif choice == 4:
print("\nDivision of two numbers")
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
divide(a,b)
Result:
DATE:
LIST SWAP
A List contains the following elements N1=[3,25,13,6,35,8,14,45]
Write a user defined function swap ( ) that takes in lst as a parameter
and swaps the content with the next value divisible by 5 so that the
resultant list will look like:
[25, 3, 13, 35, 6, 8, 45, 14]
AIM:
To write a user defined function swap ( ) that takes in lst as a
parameter and swaps the content with the next value divisible by 5
SOURCE CODE:
def swap(lst):
for i in range(len(lst)-1):
if lst[i+1]%5==0:
lst[i],lst[i+1]=lst[i+1],lst[i]
for i in range(len(lst)):
print(lst[i],end=' ')
lst=[]
while True:
print("\nChoose the operation to perform:")
print("1.list creation")
print("2.swap value")
print("3.Exit")
ch=int(input("Enter your choice"))
if ch==1:
lst=eval(input("Enter a list"))
elif ch==2:
swap(lst)
elif ch==3:
VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024
break
else:
print("Invalid Choice")
SAMPLE OUTPUT:
Result:
DATE:
String Operations
AIM:
To write a menu driven program in python using user defined
functions to take string as input and
(i) To check whether the given string is palindrome or not.
(ii) To count number of occurrences of a given character.
SOURCE CODE:
def palindrome(s):
rev=s[::-1]
if rev==s:
print("The String is palindrome")
else:
print("The string is not a palindrome")
def Count(st,choice):
c=st.count(ch)
return c
#Main program
while True:
print("Menu")
print("1.palidrome")
print("2.To count number of occurrences of a given character")
print("3. Exit")
opt=int(input("Enter your choice:"))
if opt==1:
s=input("enter the string:")
palindrome(s)
VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024
elif opt==2:
st=input("Enter the string:")
ch=input("Enter a character")
cnt=Count(st,ch)
print("The character",ch,"is present in",st,"is",cnt,"times")
elif opt==3:
break
else:
print("Invalid Choice")
SAMPLE OUTPUT:
Menu
1. palidrome
2. To count number of occurrences of a given character
3. Exit
Enter your choice:1
enter the string:race
The string is not a palindrome
Menu
1. palidrome
2. To count number of occurrences of a given character
3. Exit
Enter your choice:2
Enter the string:race
Enter a charactera
The character a is present in race is 1 times
Menu
1. palidrome
2. To count number of occurrences of a given character
3. Exit
Enter your choice: 3
VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024
Result:
The above output has been verified in the terminal.
DATE:
List Search
AIM:
To write a python program that uses a function search () which
takes a list and element as a parameter and searches an element in a
list and displays the position of the element present in the list and its
frequency
SOURCE CODE:
def search(lst,ele):
found=False
for i in range (len(lst)):
if ele==lst[i]:
found=True
print("Position",i,end=' ')
if found:
print("Frequency",lst.count(ele))
else:
print("Element not found")
lst=[]
while True:
print("1.List creation")
print("2.Search value")
print("3.Exit")
ch=int(input("Enter your choice"))
if ch==1:
lst=eval(input("Enter list elements"))
elif ch==2:
VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024
x=int(input("Enter the element you want to search"))
search(lst,x)
elif ch==3:
break
else:
print("Invalid choice")
Sample Output:
1. List creation
2. Search value
3. Exit
Enter your choice1
Enter list elements [1, 2, 3, 4, 1, 2]
1. List creation
2. Search value
3. Exit
Enter your choice2
Enter the element you want to search2
Position 1 Position 5 Frequency 2
1. List creation
2. Search value
3. Exit
Enter your choice3
>>>
Result:
DATE:
Dictionary –Create and Update
AIM:
To write a python script to print a dictionary where the keys are
numbers between 1 and 15(both included) and the values are square of
keys. Also write a function search () that takes in the dictionary d as
parameter and searches for a particular key, if found returns the
corresponding value, if not found, an error message will displayed.
Sample dictionary
{1:1,2:4,3:9,4:16,…15:225}
SOURCE CODE:
def search(d):
x=int(input("Enter a key-value to search"))
if x in d.keys():
print("key",x,"is found, the value is",d.get(x))
else:
print('key not found')
d={}
for i in range(1,16):
d[i]=i*i
print(d)
while True:
print("1.Search for a value")
print("2.Exit")
ch=int(input("Enter your choice:"))
if ch==1:
search(d)
elif ch==2:
VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024
break
else:
print("invalid choice")
Sample Output:
Run1
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121,
12: 144, 13: 169, 14: 196, 15: 225}
1. Search for a value
2. Exit
Enter your choice: 1
Enter a key-value to search5
key 5 is found, the value is 25
1. Search for a value
2. Exit
Enter your choice: 2
>>>
Run2:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121,
12: 144, 13: 169, 14: 196, 15: 225}
1.Search for a value
2.Exit
Enter your choice:1
Enter a key-value to search16
key not found
1.Search for a value
2.Exit
Enter your choice:2
>>>
Result:
The above output has been verified in the terminal.
VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024
EX.NO:6
DATE:
AIM:
To write a Python Program to Read a text file "Story.txt" line by line and display
each word separated by '#'.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
Story.txt:
***************************************************************************************
EX.NO:7
DATE:
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
Story.txt:
***************************************************************************************
EX.NO:8
DATE:
AIM:
To write a python program to read lines from a text file "Sample.txt" and copy those lines
into another file which are starting with an alphabet 'a' or 'A'.
SOURCE CODE:
Result:
Thus the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
Python Executed Program output:
Sample.txt:
New.txt:
*****************************************************************************************
EX.NO:09
DATE:
AIM:
To write a Python Program to Create a binary file with roll number and name. Search
for a given roll number and display the name, if not found display appropriate
message.
SOURCE CODE:
Result:
Thus , the above Python program is executed successfully and the output is verified.
SAMPLE OUPUT:
************************************************************************************
EX.NO:10
DATE:
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
PYTHON PROGRAM EXECUTED OUTPUT:
**********************************************************************************
EX.NO: 11
DATE:
AIM:
To write a Python program Create a CSV file to store Empno, Name, Salary and search
any Empno and display Name, Salary and if not found display appropriate message.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
PYTHON PROGRAM EXECUTED OUTPUT:
******************************************************************************
EX.NO: 12
DATE:
CREATING A PYTHON PROGRAM TO PERFORM READ AND WRITE
OPERATION WITH CSV FILE
AIM:
To write a Python program Create a CSV file to store Rollno, Name, marks and read
the content from csv file.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
PYTHON PROGRAM EXECUTED OUTPUT:
********************************************************************************************
Ex.no.13
Write a python program to maintain book details like book code, book title and price using
stacks data structures? (Implement push (), pop () ,peek () and traverse () functions)
AIM: To write a python program to maintain book details like book code, book title and price using stacks
data structures
book=[]
def push():
bcode=input("Enter bcode ")
btitle=input("Enter btitle ")
price=input("Enter price ")
bk=(bcode,btitle,price)
book.append(bk)
def pop():
if(book==[]):
print("Underflow / Book Stack in empty")
else:
bcode,btitle,price=book.pop()
print("poped element is ")
print("bcode ",bcode," btitle ",btitle," price ",price)
def peek():
if book==[]:
print("Book stack is empty")
else:
top=len(book)-1
print("the top of the book stack is :",book[top])
def traverse():
if book==[]:
print("Book stack is empty")
else:
top=len(book)-1
for i in range(top,-1,-1):
print(book[i])
while True:
print("1. Push")
print("2. Pop")
print("3.peek")
print("4. Traversal")
print("5. Exit")
ch=int(input("Enter your choice "))
if(ch==1):
push()
elif(ch==2):
pop()
elif(ch==3):
peek()
elif(ch==4):
traverse()
elif(ch==5):
print("End")
break
else:
print("Invalid choice")
Ex.no.14
Write a python program using function PUSH (Arr), where Arr is a list of numbers. From this
list push all numbers divisible by 5 into a stack implemented by using a list. Display the stack if
it has at least one element, otherwise display appropriate error message.
AIM:
To write a python program using function PUSH (Arr), where Arr is a list of numbers. From this
list push all numbers divisible by 5 into a stack implemented by using a list. Display the stack if
it has at least one element, otherwise display appropriate error message.
SOURCE CODE:
def push(Arr,item):
if item%5==0:
Arr.append(item)
def pop(Arr):
if Arr==[]:
print("No item found")
else:
print("The deleted element:")
return Arr.pop()
def show():
if Arr==[]:
print('No item found')
else:
top=len(Arr)-1
print("The stack elements are:")
for i in range(top,-1,-1):
print(Arr[i])
#Main program
Arr=[]
top=None
while True:
print('****** STACK IMPLEMENTATION USING LIST ******')
print('1: PUSH')
print('2: pop')
print('3: Show')
print('0: Exit')
ch=int(input('Enter choice:'))
if ch==1:
val=int(input('Enter no to push:'))
push(Arr,val)
elif ch==2:
res=pop(Arr)
print(res)
elif ch==3:
show()
elif ch==0:
print('Bye')
break
Output is:
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: pop
3: Show
0: Exit
Enter choice:1
Enter no to push:55
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: pop
3: Show
0: Exit
Enter choice:1
Enter no to push:33
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: pop
3: Show
0: Exit
Enter choice:2
The deleted element:
55
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: pop
3: Show
0: Exit
Enter choice:3
No item found
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: pop
3: Show
0: Exit
Enter choice:0
Bye
Ex.no.15
Write a python program to create a dictionary containing names and marks as key-value pairs of 5 students.
With separate user-defined functions to perform the following operations. Push the keys (name of the
student) of the dictionary into a stack, where the corresponding value (marks) is greater than 70.
Display the content of the stack.
SOURCE CODE:
def push(stk,d):
for i in d:
if d[i]>70:
stk.append(i)
def disp():
if stk==[]:
print("stack is empty")
else:
top=len(stk)-1
print(“The stack elements are;”)
for i in range(top,-1,-1):
print(stk[i])
stk=[]
d={}
print("performing stack operation using Dictionary")
while True:
print()
print("1.push")
print("2.disp")
print("0.exit")
opt=int(input("Enter your choice:"))
if opt==1:
d["Ramesh"]=int(input("Enter the mark of ramesh:"))
d["Umesh"]=int(input("Enter the mark of Umesh:"))
d["Vishal"]=int(input("Enter the mark of Vishal:"))
d["Khushi"]=int(input("Enter the mark of Khushi:"))
d["Ishika"]=int(input("Enter the mark of Ishika:"))
push(stk,d)
elif opt==2:
disp()
elif opt==0:
print('Bye')
break
OUTPUT:
1. push
2. disp
0. exit
Enter your choice:1
Enter the mark of ramesh: 55
Enter the mark of Umesh: 66
Enter the mark of Vishal: 77
Enter the mark of Khushi: 44
Enter the mark of Ishika: 88
1.push
2.disp
0.exit
Enter your choice:2
Ishika
Vishal
1.push
2.disp
0.exit
Enter your choice:0
Bye
>>>
EX.NO:16
DATE:
AIM:
To create a python program to integrate mysql with python to display the avgmarks
greater than 75
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is verified.
SQL Table
Output:
EX.NO: 17 DATE:
AIM:
To write a Python Program to integrate MYSQL with Python by inserting records to Emp
table and display the records.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
SQL OUTPUT:
EX.NO: 18
DATE:
DISPLAYING RECORDS)
AIM:
To write a Python Program to integrate MYSQL with Python to search an Employee using
EMPID and display the record if present in already existing table EMP, if not display the
appropriate message.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
Run – 2:
SQL OUTPUT:
OUTPUT -1:
OUTPUT -2:
EX.NO: 19
DATE:
AIM:
To write a Python Program to integrate MYSQL with Python to search an Employee using
EMPID and update the Salary of an employee if present in already existing table EMP, if not
display the appropriate message.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
Run -2:
SQL OUTPUT:
EX.NO: 20
DATE:
AIM:
To write a Python Program to integrate MYSQL with Python to search an Employee using
EMPID and delete the record of an employee if present in already existing table EMP, if not
display the appropriate message.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
RUN – 2:
SQL OUTPUT:
Sql exercise -1
a. To show all information about the students of the history department
b. To list the names of female students who are in hindi department.
c. To list the names of all the students with their date of admission in descending order.
d. To display the students name, age, fee, for male students only.
e. To count the number of students with age less than 20.
TABLE STUDENT
TABLE DRESS
DCOD DESCRIPTIO PRIC MCOD LAUNCH
DATE
E N E E
10001 FORMAL 1250 M007 06-JUN-08
SHIRT
10020 FROCK 750 M004 06-SEPT-
07
10012 INFORMAL 1150 M002 06-JUN-08
SHIRT
10090 TULIP SHIRT 850 M003 31-MAR-
07
10019 EVENING 850 M003 20-OCT-08
GOWN
10023 PENCIL 1250 M003 20-OCT-08
SHIRT
10081 SLACHS 850 M003 09-MAR-
08
10007 FORMAL 1450 M001 20-OCT-08
PANT
10008 INFORMAL 1400 M002 07-APR-09
PANT
10024 BABY TOP 850 M003 06-JUN-08
TABLE MATERIAL
MCODE TYPE
M001 TERELENE
M002 COTTON
M003 POLYSTER
M004 SILK
ANSWERS.
A. SELECT DCODE, DESCRIPTION FROM DRESS ORDER BY DCODE;
B. SELECT * FROM DRESS WHERE LAUNCH DATE BETWEEN ’05-DEC-07’ AND ’20-JUN-08’
TABLE: STATIONARY
S_ID STATIONARY COMPANY PRICE
NAME
DP01 DET PEN ABC 10
PL02 PENCIL XYZ 6
ER05 ERASER XYZ 7
PL01 PENCIL CAN 5
GP02 GELPEN ABC 15
TABLE: CONSUMER
C_ID CONSUMER NAME ADDRESS S_ID
01 GOOD EARNER DELHI PL01
06 WHITE WELL MUMBAI GP02
12 TOPPER DELHI DP01
15 WRITE 4 DRAW DELHI PL02
16 MOTIVATION BANGALORE PL01
ANSWERS.
a.) SELECT * FROM CONSUMER WHERE ADDRESS=”DELHI”;
b.) SELECT * FROM STATIONARY WHERE PRICE BETWEEN 8 AND 15;
c.) SELECT CONSUMER NAME, ADDRESS ,COMPANY, PRICE FROM CONSUMER A,
STATIONARY B WHERE A.S_ID=B.S_ID;
d.) UPDATE STATIONARY SET PRICE=PRICE+2;
e.) INSERT INTO CONSUMER VALUES(17,”FAST WRITER”,”MUMBAI’,”GP01”);