Professional Documents
Culture Documents
CS Practical File-2024
CS Practical File-2024
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