You are on page 1of 16

VELAMMAL VIDHYASHRAM SURAPET

COMPUTER SCIENCE

2023-2024

CLASS - XII

VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024


EX.NO.1
DATE:
Creating a Menu driven program to perform Arithmetic
operations.
AIM:
To write a menu driven python program to perform Arithmetic
operations based on the user’s choice.
SOURCE CODE:
#function for addition of two numbers
def addition(a, b):
sum = a + b
print(a, "+", b, "=", sum)

#function for subtraction of two numbers


def subtraction(a, b):
difference = a - b
print(a, "-", b, "=", difference)
#function for multiplication of two numbers
def multiplication(a, b):
product = a * b
print(a, "*", b, "=", product)

#function for division of two numbers


def divide(a, b):
quotient = a / b
remainder = a % b
print("Quotient of", a, "/", b, "=", quotient)
print("Remainder of", a, "%", b, "=", remainder)

#Heading of menu-driven approach


VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024
print("WELCOME TO CALCULATOR")

#using the while loop to print menu list


while True:
print("\nChoose the operation to perform:")
print("1. Addition of two numbers")
print("2. Subtraction of two numbers")
print("3. Multiplication of two numbers")
print("4. Division of two numbers")
print("5. Exit")

choice = int(input("\nEnter your Choice: "))

#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)

#exit condition to get out of the while loop


elif choice == 5:
print("Thank You! Bye.")
break
else:
print("Invalid Input! Please, try again.")

VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024


SAMPLE OUTPUT
WELCOME TO CALCULATOR

Choose the operation to perform:


1. Addition of two numbers
2. Subtraction of two numbers
3. Multiplication of two numbers
4. Division of two numbers
5. Exit

Enter your Choice: 1

Addition of two numbers


Enter the first number: 5
Enter the second number: 5
5 + 5 = 10

Choose the operation to perform:


1. Addition of two numbers
2. Subtraction of two numbers
3. Multiplication of two numbers
4. Division of two numbers
5. Exit

Enter your Choice: 2

Subtraction of two numbers


Enter the first number: 5
Enter the second number: 5
5-5=0
VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024
Choose the operation to perform:
1. Addition of two numbers
2. Subtraction of two numbers
3. Multiplication of two numbers
4. Division of two numbers
5. Exit

Enter your Choice: 3

Multiplication of two numbers


Enter the first number: 5
Enter the second number: 5
5 * 5 = 25

Choose the operation to perform:


1. Addition of two numbers
2. Subtraction of two numbers
3. Multiplication of two numbers
4. Division of two numbers
5. Exit

Enter your Choice: 4

Division of two numbers


Enter the first number: 25
Enter the second number: 5
Quotient of 25 / 5 = 5.0
Remainder of 25 % 5 = 0

Choose the operation to perform:


VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024
1. Addition of two numbers
2. Subtraction of two numbers
3. Multiplication of two numbers
4. Division of two numbers
5. Exit

Enter your Choice: 5


Thank You! Bye.

Result:

The above output has been verified in the terminal.

VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024


EX.NO.2

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:

choose the operation to perform:


1. list creation
2. swap value
3. Exit
Enter your choice1
Enter a list[3,25,13,6,35,8,14,45]

Choose the operation to perform:


1.list creation
2.swap value
3.Exit
Enter your choice2
25 3 13 35 6 8 45 14
Choose the operation to perform:
1.list creation
2.swap value
3.Exit
Enter your choice3

Result:

The above output has been verified in the terminal.

VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024


EX.NO.3

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.

VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024


EX.NO.4

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:

The above output has been verified in the terminal.

VVS/COMPUTER SCIENCE PRACATICAL/ 2023-2024


EX.NO.5

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

You might also like