NIRMALA PUBLIC SCHOOL
MUVATTUPUZHA
AFFILIATION NO. : 930332
COMPUTER SCIENCE
Code - 083
PRACTICAL FILE
2024 – 2025
Certified that this is a bonafide record of
practical work done by ………………………..
………….…….. Roll. No. …………….
Ad.No. ………………… of Class & Div
…………….. during the year 2024 -2025.
Instructor in Charge PRINCIPAL Examiner
INDEX
No. Date Description Page No.
2
3
DATE : …………………………
# PROGRAM 1
# Program to input three numbers and swap them as this: 1st number becomes the
# 2nd number,2nd number becomes the 3rd number and the 3rd number becomes
#the first number.
x=int(input("Enter number 1:"))
y=int(input("Enter number 2:"))
z=int(input("Enter number 3:"))
print("Original numbers:",x,y,z)
x,y,z=y,z,x
print("After swapping:",x,y,z)
OUTPUT
Enter number 1:25
Enter number 2:50
Enter number 3:100
Original numbers: 25 50 100
After swapping: 50 100 25
4
DATE : …………………………
# PROGRAM 2
#Program to input a value in miles and convert it into kilometres.
miles=int(input("Enter miles:"))
km=miles/0.621371
print("Miles:",miles)
print("Kilometres:",km)
OUTPUT
Enter miles:50
Miles: 50
Kilometres: 80.46722489462817
5
DATE : …………………………
# PROGRAM 3
#Program to calculate the area and volume of a sphere.
import math
r=float(input("Enter the radius:"))
area=4*math.pi*r*r
volume=4/3*math.pi*math.pow(r,3)
print("Radius of the sphere:",r,"metres")
print("Area of the sphere:",area,"units square")
print("Volume of the sphere:",volume,"units cube")
OUTPUT
Enter the radius:8
Radius of the sphere: 8.0 metres
Area of the sphere: 804.247719318987 units square
Volume of the sphere: 2144.660584850632 units cube
6
DATE : …………………………
# PROGRAM 4
#Program to display a menu for calculating area of a circle or perimeter of a circle.
radius=float(input("Enter radius of the circle:"))
print("1.Calculate Area")
print("2.Calculate Perimeter")
choice=int(input("Enter your choice (1 or 2):"))
if choice==1:
area=3.14159*radius*radius
print("Area of circle with radius",radius,'is',area)
else:
perm=2*3.14159*radius
print("Perimeter of circle with radius",radius,'is',perm)
OUTPUT 1
Enter radius of the circle:5
1.Calculate Area
2.Calculate Perimeter
Enter your choice (1 or 2):1
Area of circle with radius 5.0 is 78.53975
OUTPUT 2
Enter radius of the circle:5
1.Calculate Area
2.Calculate Perimeter
Enter your choice (1 or 2):2
Perimeter of circle with radius 5.0 is 31.4159
7
DATE : …………………………
# PROGRAM 5
#Program to calculate and print roots of a quadratic equation:ax^2+bx+c=0(a!=0).
import math
print("For quadratic equation,ax**2+bx+c=0,enter coefficient below")
a=int(input("Enter a :"))
b=int(input("Enter b :"))
c=int(input("Enter c :"))
if a==0:
print("Value of",a,'should not be zero')
print("\n Aborting!!!!!!")
else:
delta=b*b-4*a*c
if delta>0:
root1=(-b+math.sqrt(delta))/(2*a)
root2=(-b-math.sqrt(delta))/(2*a)
print("Roots are REAL and UNEQUAL")
print("Root1=",root1,", Root2=",root2)
elif delta==0:
root1=-b/(2*a)
print("Roots are REAL and EQUAL")
print("Root1=",root1,", Root2=",root1)
else:
print("Roots are COMPLEX and IMAGINARY")
OUTPUT 1
For quadratic equation,ax**2+bx+c=0,enter coefficient below
Enter a :3
Enter b :4
Enter c :1
Roots are REAL and UNEQUAL
Root1= -0.3333333333333333 , Root2= -1.0
OUTPUT 2
For quadratic equation,ax**2+bx+c=0,enter coefficient below
Enter a :1
Enter b :2
8
Enter c :3
Roots are COMPLEX and IMAGINARY
OUTPUT 3
For quadratic equation,ax**2+bx+c=0,enter coefficient below
Enter a :1
Enter b :-4
Enter c :4
Roots are REAL and EQUAL
Root1= 2.0 , Root2= 2.0
9
DATE : …………………………
# PROGRAM 6
#Program to print Fibonacci series.
t=int(input("How many terms?(enter 2+ value):"))
first=0
second=1
print("\nFibonacci series is:")
print(first,",",second,end=" , ")
for i in range(2,t):
next=first+second
print(next,end=" , ")
first=second
second=next
How many terms?(enter 2+ value):12
Fibonacci series is:
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 ,
10
DATE : …………………………
# PROGRAM 7
#Program to read an integer > 1000 and reverse the number.
num=int(input("Enter an integer(>1000):"))
tnum=num
reverse=0
while tnum:
digit=tnum%10
tnum=int(tnum/10)
reverse=reverse*10+digit
print("Reverse of",num,"is",reverse)
OUTPUT
Enter an integer(>1000):1357
Reverse of 1357 is 7531
11
DATE : …………………………
# PROGRAM 8
#Program to input the value of x and n and print the sum of the series:
#1-x+x^2-x^3+x^4-....x^n
x=int(input("Enter value of x:"))
n=int(input("Enter the power(n):"))
s=0
sign=+1
for a in range(n+1):
term=(x**a)*sign
s+=term
sign*=-1
print("Sum of first",n,"terms:",s)
OUTPUT
Enter value of x:7
Enter the power(n):8
Sum of first 8 terms: 5044201
12
DATE : …………………………
# PROGRAM 9
#Program to input the value of x and n and print the sum of the series:
#x+x^2/2!-x^3/3!+x^4/4!-.....x^n/n!
x=int(input("Enter value of x:"))
n=int(input("Enter the power(n):"))
s=x
sign=+1
for a in range(2,n+1):
f=1
for i in range(1,a+1):
f*=i
term=((x**a)*sign)/f
s+=term
sign*=-1
print("Sum of first",n,"terms:",s)
OUTPUT
Enter value of x:6
Enter the power(n):7
Sum of first 7 terms: -13.542857142857144
13
DATE : …………………………
# PROGRAM 10
#Program to implement a simple calculator for two input numbers.
#Offer choices through a menu.
print("Enter 2 numbers below")
a=int(input("Enter number 1:"))
b=int(input("Enter number 2:"))
ch=0
while ch<5:
print("Calculator Menu")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("5.Exit")
ch=int(input("Enter Choice(1-5):"))
if ch==1:
c=a+b
print("Sum=",c)
elif ch==2:
c=a-b
print("Difference=",c)
elif ch==3:
c=a*b
print("Product=",c)
elif ch==4:
c=a/b
print("quotient=",c)
elif ch==5:
break
else:
print("Invalid Character")
OUTPUT
Enter 2 numbers below
Enter number 1:6
14
Enter number 2:8
Calculator Menu
1.Add
2.Subtract
3.Multiply
4.Divide
5.Exit
Enter Choice(1-5):1
Sum= 14
Calculator Menu
1.Add
2.Subtract
3.Multiply
4.Divide
5.Exit
Enter Choice(1-5):2
Difference= -2
Calculator Menu
1.Add
2.Subtract
3.Multiply
4.Divide
5.Exit
Enter Choice(1-5):3
Product= 48
Calculator Menu
1.Add
2.Subtract
3.Multiply
4.Divide
5.Exit
Enter Choice(1-5):4
quotient= 0.75
Calculator Menu
1.Add
2.Subtract
3.Multiply
4.Divide
5.Exit
Enter Choice(1-5):5
15
DATE : …………………………
# PROGRAM 11
#Program to input a number and check it is a palindrome or not.
num=int(input("Enter number:"))
rev=0
nnum=num
while(nnum != 0):
digit=nnum%10;
nnum=nnum//10
rev=rev*10+digit
if rev==num:
print(num,"is a palindrome number.")
else:
print(num,"is not a palindrome number.")
OUTPUT 1
Enter number:2345432
2345432 is a palindrome number.
OUTPUT 2
Enter number:3478
3478 is not a palindrome number.
16
DATE : …………………………
# PROGRAM 12
#Program to print the pattern:
#A
#A B
#A B C
#A B C D
#A B C D E
for i in range(1,6):
for j in range(65,65+i):
print(chr(j),end=" ")
print()
OUTPUT
A
AB
ABC
ABCD
ABCDE
17
DATE : …………………………
# PROGRAM 13
#Program to print the pattern:
#1
#2 2
#3 3 3
#4 4 4 4
#5 5 5 5 5
for i in range(1,6):
for j in range(1,i+1):
print(i,end=" ")
print()
OUTPUT
1
22
333
4444
55555
18
DATE : …………………………
# PROGRAM 14
#Program to input a string and print number of upper and lower case letters in it.
str1=input("Enter a string:")
ucase,lcase=0,0
for ch in str1:
if ch>='A' and ch<='Z':
ucase+=1
if ch>='a' and ch<='z':
lcase+=1
print("No.of uppercase letters:",ucase)
print("No.of lowercase letters:",lcase)
OUTPUT
Enter a string:ComPuTEr sciEnCE
No.of uppercase letters: 7
No.of lowercase letters: 8
19
DATE : …………………………
# PROGRAM 15
#Program to input a list and do operations according to the user’s input.
#1.Search an element.
#2.Add an element to the last.
#3.Add an element to a particular position.
#4.Delete an element.
#5.Delete the list.
#6.Exit.
print("Input a list below")
lst1=eval(input("Enter a list: "))
ch=0
c="y"
while c=="y":
print("List Operations Menu: ")
print("1.Search an element")
print("2.Add an element at the end")
print("3.Add an element to a particular position")
print("4.Delete an element")
print("5.Delete the list")
print("6.Exit")
ch=int(input("Enter choice: "))
if ch==1:
ele=int(input("Enter element to be searched: "))
if ele in lst1:
print("Element is present in list")
else:
print("Element is not present in list")
c=input("Do you want to continue?(y/n): ")
elif ch==2:
ele=int(input("Enter element to be added to end of list: "))
lst1.append(ele)
print("After addition of element, list is", lst1)
c=input("Do you want to continue?(y/n): ")
elif ch==3:
ele=int(input("Enter element to be added to the list: "))
i=int(input("Enter position to which element is to be added: "))
lst1.insert(i,ele)
print("After addition of element, list is", lst1)
c=input("Do you want to continue?(y/n): ")
20
elif ch==4:
ele=int(input("Enter element to be deleted: "))
while True:
if ele in lst1:
lst1.remove(ele)
else:
print("After deletion of element, list is", lst1)
break
c=input("Do you want to continue?(y/n): ")
elif ch==5:
del lst1
print("List has been deleted.Further operations are not possible on list")
break
elif ch==6:
print("Exiting!!!")
break
else:
print("Invalid Input!!!")
OUTPUT
Input a list below
Enter a list: [1,2,3,4,5,6]
List Operations Menu:
1.Search an element
2.Add an element at the end
3.Add an element to a particular position
4.Delete an element
5.Delete the list
6.Exit
Enter choice: 1
Enter element to be searched: 4
Element is present in list
Do you want to continue?(y/n): y
List Operations Menu:
1.Search an element
2.Add an element at the end
3.Add an element to a particular position
4.Delete an element
5.Delete the list
6.Exit
Enter choice: 2
Enter element to be added to end of list: 57
21
After addition of element, list is [1, 2, 3, 4, 5, 6, 57]
Do you want to continue?(y/n): y
List Operations Menu:
1.Search an element
2.Add an element at the end
3.Add an element to a particular position
4.Delete an element
5.Delete the list
6.Exit
Enter choice: 3
Enter element to be added to the list: 46
Enter position to which element is to be added: 3
After addition of element, list is [1, 2, 3, 46, 4, 5, 6, 57]
Do you want to continue?(y/n): y
List Operations Menu:
1.Search an element
2.Add an element at the end
3.Add an element to a particular position
4.Delete an element
5.Delete the list
6.Exit
Enter choice: 4
Enter element to be deleted: 6
After deletion of element, list is [1, 2, 3, 46, 4, 5, 57]
Do you want to continue?(y/n): y
List Operations Menu:
1.Search an element
2.Add an element at the end
3.Add an element to a particular position
4.Delete an element
5.Delete the list
6.Exit
Enter choice: 5
List has been deleted.Further operations are not possible on list
List Operations Menu:
1.Search an element
2.Add an element at the end
3.Add an element to a particular position
4.Delete an element
5.Delete the list
6.Exit
Enter choice: 6
Exiting!!!
22
DATE : …………………………
# PROGRAM 16
#Program to create a nested tuple to store roll no.,name,mark of 5 subjects for 5
#students.
lst1=[]
for i in range(1,6):
lst2=[]
rollno=int(input("Enter the roll number of the student: "))
name=input("Enter the name of the student: ")
lst2.append(rollno)
lst2.append(name)
lst3=[]
for j in range(1,6):
mrk=int(input("Enter the mark of the subject: "))
lst3.append(mrk)
lst2.append(tuple(lst3))
lst1.append(tuple(lst2))
tup1=tuple(lst1)
print("The student details are",tup1)
OUTPUT
Enter the roll number of the student: 1
Enter the name of the student: Student 1
Enter the mark of the subject: 67
Enter the mark of the subject: 80
Enter the mark of the subject: 87
Enter the mark of the subject: 58
Enter the mark of the subject: 90
Enter the roll number of the student: 2
Enter the name of the student: Student 2
Enter the mark of the subject: 97
Enter the mark of the subject: 89
Enter the mark of the subject: 83
Enter the mark of the subject: 65
Enter the mark of the subject: 72
Enter the roll number of the student: 3
Enter the name of the student: Student 3
23
Enter the mark of the subject: 68
Enter the mark of the subject: 45
Enter the mark of the subject: 49
Enter the mark of the subject: 80
Enter the mark of the subject: 81
Enter the roll number of the student: 4
Enter the name of the student: Student 4
Enter the mark of the subject: 95
Enter the mark of the subject: 85
Enter the mark of the subject: 68
Enter the mark of the subject: 92
Enter the mark of the subject: 99
Enter the roll number of the student: 5
Enter the name of the student: Student 5
Enter the mark of the subject: 48
Enter the mark of the subject: 78
Enter the mark of the subject: 63
Enter the mark of the subject: 74
Enter the mark of the subject: 76
The student details are: ((1, 'Student 1', (67, 80, 87, 58, 90)),
(2, 'Student 2', (97, 89, 83, 65, 72)), (3, 'Student 3', (68, 45, 49, 80, 81)),
(4, 'Student 4', (95, 85, 68, 92, 99)), (5, 'Student 5', (48, 78, 63, 74, 76)))
24
DATE : …………………………
# PROGRAM 17
#Program to input a list of numbers and test if a number is equal to the sum of the
# cubes of its digits to find the smallest and largest such number from the given
#list of numbers.
val=eval(input("Enter a list"))
alist=[]
s=len(val)
for i in range(s):
num=val[i]
csum=0
while num:
dig=num%10
csum+=(dig*dig*dig)
num=num//10
if csum==val[i]:
alist.append(val[i])
print("Largest number(=sum of its digits' cubes):",max(alist))
print("Smallest number(=sum of its digits' cubes):",min(alist))
OUTPUT
Enter a list[456,786,56,45,345,370,567,34,56,407]
Largest number(=sum of its digits' cubes): 407
Smallest number(=sum of its digits' cubes): 370
25
DATE : …………………………
# PROGRAM 18
#Program to read a sentence and then create a dictionary that contains the
#frequency of letters and digits in the sentence.
sen=input("Enter a sentence: ")
sen=sen.lower()
alphabet_digits='abcdefghijklmnopqrstuvwxyz0123456789'
char_count={}
print("Total characters in the sentence are: ",len(sen))
for char in sen:
if char in alphabet_digits:
if char in char_count:
char_count[char]=char_count[char]+1
else:
char_count[char]=1
print(char_count)
OUTPUT
Enter a sentence: There are 28 states in India
Total characters in the sentence are: 28
{'t': 3, 'h': 1, 'e': 4, 'r': 2, 'a': 3, '2': 1, '8': 1, 's': 2, 'i': 3, 'n': 2, 'd': 1}
26
DATE : …………………………
# PROGRAM 19
#Program to create a dictionary with the roll number,name and marks of n
#students #in a class and display the names of students who have marks above
# 75.
n=int(input("How many Students? "))
stu={}
for i in range(1,n+1):
print("Enter details of Student",(i))
rollno=int(input("Roll number:"))
name=input("Name:")
marks=float(input("Marks:"))
d={"Roll_no":rollno,"Name":name,"Marks":marks}
stu[i]=d
print("The dictionary is ",stu)
print("Students with marks > 75 are:")
for k in stu:
if stu[k]["Marks"] > 75:
print(stu[k])
OUTPUT
How many Students? 5
Enter details of Student 1
Roll number:1
Name:Rohit
Marks:89
Enter details of Student 2
Roll number:2
Name:Ravi
27
Marks:67
Enter details of Student 3
Roll number:3
Name:Peter
Marks:79
Enter details of Student 4
Roll number:4
Name:Octavius
Marks:78
Enter details of Student 5
Roll number:5
Name:Tobey
Marks:71
Students with marks > 75 are:
{'Roll_no': 1, 'Name': 'Rohit', 'Marks': 89.0}
{'Roll_no': 3, 'Name': 'Peter', 'Marks': 79.0}
{'Roll_no': 4, 'Name': 'Octavius', 'Marks': 78.0}
28
DATE : …………………………
# PROGRAM 20
# Program to input your friends name and phone numbers and store then in the
#dictionary as the key-value pair. Perform the following operations on the
dictionary
'''
a) Display the name and phone number of all your friends
b) Add a new key-value pair in this dictionary and display the modified
dictionary
c) Delete a particular friend from the dictionary
d) Modify the phone number of an existing friend
e) Check if a friend is present in the dictionary or not
f) Display dictionary in sorted order of names
'''
n=int(input("How many friends :"))
fd={}
for i in range(n):
print("Enter details of friend")
name=input("Name : ")
name=name.title()
ph=int(input("Phone : "))
fd[name]=ph
print ("The frinds dictionary is ", fd)
ch=0
while ch!=7:
print("\t MENU ")
print("1. Display all friends")
print("2. Add a new friend")
print("3. Delete a friend")
print("4. Modify Phone number")
print("5. Search for a friend")
print("6. Sort by name")
print("7. Exit")
ch=int(input("Enter your choice (1 - 7) : "))
if ch==1:
print(fd)
29
elif ch==2:
print("Enter details of new friend")
name=input("Name : ")
name=name.title()
ph=int(input("Phone : "))
fd[name]=ph
elif ch==3:
name=input("Enter friend Name to be deleted : ")
name=name.title()
res=fd.pop(name,-1)
if res!=-1:
print(res," is deleted from friends list")
else:
print("No Such friend")
elif ch==4:
name=input("Friends Name to update phone number : ")
name=name.title()
ph=int(input("Phone : "))
fd[name]=ph
elif ch==5:
name=input("Friends Name to search : ")
name=name.title()
if name in fd:
print(name, " exists in the dictionary")
else:
print(name, " does not exists in the dictionary")
elif ch==6:
lst=sorted(fd)
print("{",end= " ")
for a in lst:
print(a," : ", fd[a], end=" ")
print("}")
elif ch==7:
break
else:
print("Invalid Choice, Enter 1-7 only")
30
OUTPUT
How many friends : 2
Enter details of friend
Name : Basil Binu
Phone : 9876546345
Enter details of friend
Name : Kiran Kumar
Phone : 6789456790
The frinds dictionary is {'Basil Binu': 9876546345, 'Kiran Kumar': 6789456790}
MENU
1. Display all friends
2. Add a new friend
3. Delete a friend
4. Modify Phone number
5. Search for a friend
6. Sort by name
7. Exit
Enter your choice (1 - 7) : 1
{'Basil Binu': 9876546345, 'Kiran Kumar': 6789456790}
MENU
1. Display all friends
2. Add a new friend
3. Delete a friend
4. Modify Phone number
5. Search for a friend
6. Sort by name
7. Exit
Enter your choice (1 - 7) : 2
Enter details of new friend
Name : Abel Anil
Phone : 9876545690
MENU
1. Display all friends
2. Add a new friend
3. Delete a friend
4. Modify Phone number
5. Search for a friend
6. Sort by name
7. Exit
Enter your choice (1 - 7) : 1
{'Basil Binu': 9876546345, 'Kiran Kumar': 6789456790, 'Abel Anil': 9876545690}
31
MENU
1. Display all friends
2. Add a new friend
3. Delete a friend
4. Modify Phone number
5. Search for a friend
6. Sort by name
7. Exit
Enter your choice (1 - 7) : 4
Friends Name to update phone number : Basil Binu
Phone : 6789452345
MENU
1. Display all friends
2. Add a new friend
3. Delete a friend
4. Modify Phone number
5. Search for a friend
6. Sort by name
7. Exit
Enter your choice (1 - 7) : 1
{'Basil Binu': 6789452345, 'Kiran Kumar': 6789456790, 'Abel Anil': 9876545690}
Enter your choice (1 - 7) : 3
Enter friend Name to be deleted : kiran kumar
Kiran Kumar is deleted from friends list
MENU
1. Display all friends
2. Add a new friend
3. Delete a friend
4. Modify Phone number
5. Search for a friend
6. Sort by name
7. Exit
Enter your choice (1 - 7) : 1
{'Basil Binu': 9876546345, 'Abel Anil': 9876545690}
MENU
1. Display all friends
2. Add a new friend
3. Delete a friend
4. Modify Phone number
5. Search for a friend
6. Sort by name
7. Exit
Enter your choice (1 - 7) : 5
32
Friends Name to search : Basil Binu
Basil Binu exists in the dictionary
MENU
1. Display all friends
2. Add a new friend
3. Delete a friend
4. Modify Phone number
5. Search for a friend
6. Sort by name
7. Exit
Enter your choice (1 - 7) : 6
{ Abel Anil : 9876545690 Basil Binu : 9876546345 }
MENU
1. Display all friends
2. Add a new friend
3. Delete a friend
4. Modify Phone number
5. Search for a friend
6. Sort by name
7. Exit
Enter your choice (1 - 7) : 7
33
1. Program to input three numbers and swap them as this: 1st number becomes
the #2nd number,2nd number becomes the 3rd number and the 3rd number
becomes #the first number.
2. #Program to input a value in miles and convert it into kilometres.
3. #Program to calculate the area and volume of a sphere.
4. #Program to display a menu for calculating area of a circle or perimeter of a
circle.
5. #Program to calculate and print roots of a quadratic equation:
ax^2+bx+c=0(a!=0).
6. #Program to print Fibonacci series.
7. #Program to read an integer > 1000 and reverse the number.
8. #Program to input the value of x and n and print the sum of the series: #1-
x+x^2-x^3+x^4-....x^n
9. #Program to input the value of x and n and print the sum of the series:
#x+x^2/2!-x^3/3!+x^4/4!-.....x^n/n!
10. #Program to implement a simple calculator for two input numbers.#Offer
choices through a menu.
11. #Program to input a number and check it is a palindrome or not.
12. #Program to print the pattern:
#A
#A B
#A B C
#A B C D
#A B C D E
13. #Program to print the pattern:
#1
#2 2
#3 3 3
#4 4 4 4
#5 5 5 5 5
14. #Program to input a string and print number of upper and lower case letters
in it.
15. #Program to input a list and do operations according to the user’s input.
#1.Search an element.
#2.Add an element to the last.
#3.Add an element to a particular position.
#4.Delete an element.
#5.Delete the list.
#6.Exit.
34
16. #Program to create a nested tuple to store roll no.,name,mark of 5 subjects
for 5 students.
17. #Program to input a list of numbers and test if a number is equal to the sum
of the #cubes of its digits to find the smallest and largest such number from
the given list of numbers.
18. #Program to read a sentence and then create a dictionary that contains the
frequency of letters and digits in the sentence.
19. #Program to create a dictionary with the roll number, name and marks of n
students in a class and display the names of students who have marks above
75.
20.# Program to input your friends name and phone numbers and store then in
the #dictionary as the key-value pair. Perform the following operations on
the dictionary
'''
a) Display the name and phone number of all your friends
b) Add a new key-value pair in this dictionary and display the modified
dictionary
c) Delete a particular friend from the dictionary
d) Modify the phone number of an existing friend
e) Check if a friend is present in the dictionary or not
f) Display dictionary in sorted order of names
'''
35