COMPUTER SCIENCE
PRACTICAL PROGRAMS
FOR GRADE -XII-A
[2024-2025]
PREPARED BY
JEET GAUTAM
ACKNOWLEDGMENT
We would like to take this opportunity to express our sincere
Gratitude to all who contributed to the successful completion
Of our computer practical.Firstly we extend our heartfelt
Thanks to our teacher. Mr.Vaibhav Gupta For their constant
Support patience and guidance throughout the practical session
Your expertise and teaching methods have enriched our
understanding of computer concepts and practical applications.
We would also like to acknowledge our peers, who collaborated
and shared their insights, making our practical work more engaging
and productive. Additionally, our thanks go to the school administration
for providing the necessary resources and a conducive environment for
our studies.
Lastly, we appreciate the opportunities to apply theoretical knowledge i
n practical settings, as this has enhanced our learning experience
immensely.
Thank you all for your invaluable contributions!
CERTIFICATE
This to certify that this practical is submitted by
Jeet Gautam to the computer department S.B.V
Anandvas was carried out by her under the
guidance and supervision of MR.VAIBHAV GUPTA
during academic session 2024-2025.
External examiner:
Internal examiner:
EX.NO: 1
CREATING A MENU DRIVEN PROGRAM TOPERFORM
ARITHMETIC OPERATIONS
AIM:
To write a menu driven Python Program to perform Arithmetic operations (+,-*,/)
B. based on the user’s choice.
Source code:
print("1. Adddition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
opt-int(input("Enter your choice:"))
awint(input("Enter the First Number:"))
beint (input("Enter the Second Number:"))
If opt=1:
c=a+b print("The Addition of two number is:",c)
elif opt2:
cma-b print("The Subtraction of two number is:",c)
lif opt=3:
cab print("The Multiplication of two number is:",c)
elif opt=4:
elae: 10: print("Enter any other number other than 0%) elmet cma/b
print("The Division of two number is:",c)
print("Invalid Option")
Sampleoutput
1. Adddition
2. Subtraction
3. Multiplication
4. Division
Enteryourchoice:1
EntertheFirstNumber:10
Enter the Second Number:23
The Addition of two number is: 33
EX.NO: 2
CREATING A PYTHON PROGRAM TO DISPLAY FIBONACCI SERIES
AIM: To write a Python Program to display Fibonacci Series up to ‘n’
numbers.
SOURCE CODE:
def fibonacci_series(n):
fib_sequence = []
a, b = 0, 1
for _ in range(n):
fib_sequence.append(a)
a, b = b, a + b
return fib_sequence
try:
n = int(input("Enter the number of terms for the Fibonacci series: "))
if n <= 0:
print("Please enter a positive integer.")
else:
print(f"The first {n} terms of the Fibonacci series are:")
print(fibonacci_series(n))
except ValueError:
print("Invalid input! Please enter an integer.")
Example Output:
Enter the number of terms for the Fibonacci series: 7
The first 7 terms of the Fibonacci series are:
[0, 1, 1, 2, 3, 5, 8]
Ex.no:3
CREATING A MENU DRIVEN PROGRAM TO FIND FACTORIAL AND SUM OF LIST OF
NUMBERS USING FUNCTION
AIM:To write a menu driven Python Program to find Factorial and sum of list of numbers
using function.
Source code:
print("1. To Find Factorial") print("2. To Find sum of List elements") opt-int(input("E
L=[] n=int(input("Enter how many elements you want to store in List?:")) for i in ran
Ex.no:4
Aim:To write a python program to define the function check(no1,no2)
that takes two numbers and returns that has minimum one digit.
Source code:
def Check(no1,no2):
if (no1%10) < (no2%10):
return nol
else:
return no2
a=int(input("Enter the First number:"))
b=int(input("Enter the Second number:"))
r=Check(a,b)
print("The Number that has minimum one's digit is:",r)
Output:
Enter the First number:245
Enter the Second number:342
The Number that has minimum one's digit is: 342
Ex.no:5
CREATING A PYTHON PROGRAM TO IMPLEMENT MATHEMATICAL
FUNCTIONS
Aim: To write a python program to implement python mathematics
Function to find:
(i) To find Square of a Number.
(ii) To find Log of a Number(i.e. Log10)
(iii) To find Quad of a Number
Source code:
import math
def Square (num):
Smath.pow(num, 2)
return S
def Log (num):
Smath.log10 (num)
return S
def Quad (X,Y):
S-math.sqrt (X**2 + Y**2)
return S
print("The Square of a Number is:", Square (5))
print("The Log of a Number is:", Log (10))
print("The Quad of a Number is:", Quad (5,2))
Output:
The Square of a Number is: 25.0
The Log of a Number is: 1.0
The Quad of a Number is: 5.385164807134504
Ex.no:6
CREATING A PYTHON PROGRAM TO GENERATE RANDOM NUMBER
BETWEEN 1 TO 6
Aim:To write a python program to generate random number between
1 to 6 to simulate the dice
Source code:
import random
while True:
Choice-input("\nDo you want to roll the dice (y/n):")
no-random.randint(1,6)
if Choice'y':
print("\nYour Number is:",no)
else:
break
Output:
Do you want to roll the dice(y/n):y
Your Number is: 5
Do you want to roll the dice(y/n):y
Your Number is: 2
Do you want to roll the dice (y/n):y
Your Number is: 1
Do you want to roll the dice (y/n):n
Ex.no:7
CREATING A PYTHON PROGRAM TO READ A TEXT FILE LINE BY LINE AND
DISPLAY EACH WORD SEPARATED BY '#'
Aim:To write a Python Program to Read a text file "Story.txt" line by line and display
each word separated by '#'.
Source code:
f=open("Story.txt", 'r')
Contents=f.readlines ()
for line in Contents:
words=line.split()
for i in words:
print(i+'#',end='')
print("")
f.close()
Sample output:
Like a# Joy# on# the# heart# of# a# sorrow.
# The# sunset# hangs# on a cloud.#
Ex.no:8
CREATING A PYTHON PROGRAM TO READ A TEXT FILE AND DISPLAY THE
NUMBER OF VOWELS/CONSONANTS/LOWER CASE/ UPPER CASE CHARACTERS.
Aim:To write a Python Program to read a text file "Story.txt" and
displays the number of Vowels/ Consonants/ Lowercase / Uppercase/hai
characters in the file.
Source code:
f-open ("Story.txt", 'r')
Contents-f.read()
Vowels-0
Consonants=0
Lower_case=0
Upper case=0
for ch in Contents
if ch in aeiouΛΕIOU':
Vowels Vowels+1
if ch in 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ":
Consonants Consonants+1
if ch.islower():
Lower_case-Lower_case+1
if ch.isupper():
Upper_case-Upper_case+1
f.close()
print("The total numbers of vowels in the file:", Vowels)
print("The total numbers of consonants in the file:", Consonants)
print("The total numbers of uppercase in the file:", Upper case)
print("The total numbers of lowercase in the file:", Lower case)
Ex.no:9
CREATING PYTHON PROGRAM TO DISPLAY SHORT WORDS
FROM A TEXT FILE
Aim:To Write a method Disp() in Python, to read the lines from poem.txt and display
those words which are less than 5 characters.
Source code:
def Disp():
F=open("Poem.txt")
S=F.read()
W=S.split()
print("The follwoing words are less than 5 characters")
for i in W:
if len(i)<5:
print(i,end=' ')
F.close()
Disp()
Python executed program
The follwoing words are less than 5 characters
rays of on the sky,
Ex.no 10
CREATING A PYTHON PROGRAM TO COPY PARTICULAR LINES OF A TEXT FILE
INTO AN ANOTHER TEXT FILE
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:
F1=open("Story.txt",'r')
F2=open("New.txt",'w')
S=F1.readlines()
for i in S:
if i[0]=='A' or i[0]=='a':
F2.write(i)
print("Written Successfully")
F1.close()
F2.close()
Ex.no:11
CREATING A PYTHON PROGRAM TO CREATE AND SEARCH RECORDS IN
BINARY FILE
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
messag
Source code:
import pickle
def Create():
F=open ("Students.dat", 'ab')
opt='y
while opt='y':
Roll No-int(input('Enter roll number:'))
Name input("Enter Name:")
L=[Roll No, Name]
pickle.dump (L,F)
opt-input("Do you want to add another student detail(y/n):")
F.close()
def Search():
F-open ("Students.dat", 'rb')
no-int(input("Enter Roll. No of student to search:"))
found 0
try:
while True:
S-pickle.load (F)
if S[0]=no:
print("The searched Roll. No is found and Details are:",S)
found-1
break
except:
F.close()
If found0:
print("The Searched Roll. No is not found")
#Main Program
Create()
Search ()
Output:
Enter roll number: 1
Enter Name: Arun
Do you want to add another student detail (y/n):y
Enter roll number: 2
Enter Name: Bala
Do you want to add another student detail (y/n):y
Enter roll number: 3
Enter Name: Charan
Do you want to add another student detail (y/n):y
Enter roll number: 4
Enter Name: Dinesh
Do you want to add another student detail (y/n):y
Enter roll number: 5
Enter Name: Divya
Do you want to add another student detail (y/n):n
Enter Roll. No of student to search: 3
The searched Roll. No is found and Details are: [3, 'Charan']
Ex.no:12
Ex.no:13
Ex.no:14
Ex.no:15
Ex.no:16
Ex.no:17
Ex.no:18
Ex.no:19