0% found this document useful (0 votes)
1 views

XII Practical File (python)

The document is a lab manual for Computer Science practicals at Velammal New Gen School for the academic year 2024-2025. It includes various Python programming exercises such as arithmetic operations, multiplication tables, list manipulations, and file handling, along with source code and outputs for each task. Each section outlines the aim, algorithm, source code, output, and result of the executed programs.

Uploaded by

A.S.Vishwaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

XII Practical File (python)

The document is a lab manual for Computer Science practicals at Velammal New Gen School for the academic year 2024-2025. It includes various Python programming exercises such as arithmetic operations, multiplication tables, list manipulations, and file handling, along with source code and outputs for each task. Each section outlines the aim, algorithm, source code, output, and result of the executed programs.

Uploaded by

A.S.Vishwaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

VELAMMAL NEW GEN SCHOOL – SHOLINGANALLUR.

COMPUTER SCIENCE
2024-2025
COMPUTER SCIENCE PRACTICAL (083) – LAB MANUAL

Submitted to: Submitted by:

1
1. ARITHMETIC OPERATIONS
Aim:-
To write a menu driven Python Program to perform Arithmetic
operations (+, -, *, /).
Algorithm:-
Step 1:- Get Inputs.
Step 2:- Print the menu.
Step 3:- Get input for the choice from menu.
Step 4:- Using Selective statements calculate and print the result.
Source Code:-
ch=int(input("Enter your choice:"))
a=int(input("Enter the First Number:"))
b=int(input("Enter the Second Number:"))
if ch==1:
print("The Addition value is:", a+b)
elif ch==2:
print("The Subtraction value is:", a-b)
elif ch==3:
print("The Multiplication value is:", a*b)
elif ch==4:
print("The Division value is:", a/b)
else:
print("Invalid option")

2
Output:-
Enter your choice:1
Enter the First Number:5
Enter the Second Number:3
The Addition value is: 8

Result:-
Thus the above program has been successfully executed.

3
2. MULTIPLICATION TABLE
Aim:-
To write a Python Program to display the multiplication table of the given
number.
Algorithm:-
Step 1:- Get Input.
Step 2:- Using Looping statements calculate and print the result.
Source Code:-
num = int(input("Display multiplication table of:"))
for i in range(1, 11):
print(num, 'x', i, '=', num*i)
Output:-
Display multiplication table of: 2
2x1=2
2x2=4
2x3=6
2x4=8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Result:-
Thus the above program has been successfully executed.

4
3. DOUBLE THE ODD VALUES AND HALF EVEN VALUES OF A LIST
Aim: - To write a python program to pass list to a function and double the
odd values and half even values of a list and display list element after
changing.
Algorithm:-
Step 1:- Create List.
Step 2:- Create Function.
Step 3:- Call the Function.
Step 4:- Using Selective statements calculate and print the result.
Source Code:-
def f(x):
newlist=[ ]
for i in x:
if i%2==0:
newlist.append(i//2)
else:
newlist.append(i*2)
print(“Modified list elements are:”,newlist)
mylist=eval(input(“Enter the list elements:”))
print(“Original list elements are:”,mylist)
f(mylist)
Output:-
Original list elements are: [1, 2, 3, 4, 5]
Modified list elements are: [2, 1, 6, 2, 10]
Result:-
Thus the above program has been successfully executed.

5
4. ODD AND EVEN NUMBERS COUNT IN TUPLE.
Aim:-
To write a Python program input n numbers in tuple and pass it to
function to count how many even and odd numbers are entered.
Algorithm:-
Step 1:- Create tuple.
Step 2:- Print the data.
Step 3:- Create and call the Function.
Step 4:- Using Selective statements calculate and print the result.
Source Code:-
def EOcount(x):
e=0
o=0
for i in x:
if i%2==0:
e=e+1
else:
o=o+1
print(“Even count is:”,e)
print(“Odd count is:”,o)
t=eval(input("Enter the tuple elements"))
print("Tuple elements are = ", t)
EOcount(t)

6
Output:-
Enter the tuple 2, 3, 4, 5, 6, 7, 8, 9
Tuple Items are = (2, 3, 4, 5, 6, 7, 8, 9)
The Count of Even Numbers in t = 4
The Count of Odd Numbers in t = 4
Result:-
Thus the above program has been successfully executed.

7
5. UPDATE VALUE AT THAT KEY IN DICTIONARY
Aim: - To write a Python program to function with key and value, and
update value at that key in dictionary entered by user.
Algorithm:-
Step 1:- Get Inputs.
Step 2:- Create empty dictionary.
Step 3:- Update the value.
Step 4:- Print the result.
Source Code:-
key = input("Please enter the Key : ")
value = input("Please enter the Value : ")
d = {}
d [key] = value
print("Updated Dictionary = ", d)
Output:-
Please enter the Key: 1
Please enter the Value: January

Updated Dictionary = {'1': 'January'}


Result:-
Thus the above program has been successfully executed.

8
6. VOWELS COUNTING
Aim:-
To write a Python program to pass a string to a function and count how
many vowels present in the string.
Algorithm:-
Step 1:- Get Input.
Step 2:- Print the data.
Step 3:- Create and call the function.
Step 4:- Using Selective statements calculate and print the result.
Source Code:-
def vowels(x):
c=0
for i in x:
if i in "aeiouAEIOU":
c=c+1
return c
s=input("Enter String:")
print("Number of vowels is string is:", vowels(s))

Output:-
Enter String: Hello India
Number of vowels is string is: 5
Result:-
Thus the above program has been successfully executed.

9
7. RANDOM NUMBER GENERATOR
Aim: - To write a Python program to generator (Random Number) that
generates random numbers between 1 and 6 (simulates a dice) using user
defined function.
Algorithm:-
Step 1:- Import module.
Step 2:- Get input for the choice.
Step 3:- Using Selective statements calculate and print the result.
Source Code:-
import random
while True:
ch=input("Enter (r) for roll dice or press any other key to quit")
if ch=='r':
print(random.randint(1,6))
else:
break
Output:-
Enter (r) for roll dice or press any other key to quit r
4
Enter (r) for roll dice or press any other key to quit r
1
Enter (r) for roll dice or press any other key to quit r
2
Enter (r) for roll dice or press any other key to quit d
Result:-
Thus the above program has been successfully executed.

10
8. MATHEMATICAL FUNCTIONS
Aim: - To write a python program to implement python mathematical
functions.
Algorithm:-
Step 1:- Import module.
Step 2:- Using math functions Calculate and print the result.
Source Code:-
import math
print("The sin value of 90 is:",math.sin(90))
print("The cos value of 45 is:",math.cos(45))
print("The tan value of 90 is:",math.tan(90))
print("The square root value of 81 is:",math.sqrt(81))
print("The power value of 2 power 3 is:",math.pow(2,3))
print("The ceil value of 1.3 is:",math.ceil(1.3))
print("The floor value of 1.5 is:",math.floor(1.5))
print("The absolute value of -5 is:",math.fabs(-5))
print("The factorial value of 4 is:",math.factorial(4))
Output:-
The sin value of 90 is: 0.8939966636005579
The cos value of 45 is: 0.5253219888177297
The tan value of 90 is: -1.995200412208242
The square root value of 81 is: 9.0
The power value of 2 power 3 is: 8.0
The ceil value of 1.3 is: 2
The floor value of 1.5 is: 1
The absolute value of -5 is: 5.0
11
The factorial value of 4 is: 24
Result:-
Thus the above program has been successfully executed.

12
9. STRING FUNCTIONS
Aim: - To write a python program to implement python string functions.
Algorithm:-
Step 1:- Get Inputs.
Step 2:- Print the data.
Step 3:- Using String functions print the result.
Source Code:-
s1="Hello India"
s2="Welcome India"
print("The Capitalize functions is:",s1.capitalize())
print("The title function is:",s2.title())
print("The lower function is:",s1.lower())
print("The Upper function is:",s2.upper())
print("The Swapcase function is:",s1.swapcase())
print("The Count function is:",s2.count('e'))
print("The find function is:",s2.find('e'))
print("The isalpha function is:",s1.isalpha())
print("The isdigit function is:",s2.isdigit())
print("The replace function is:",s2.replace("Welcome","My"))
Output:-
The Capitalize functions is: Hello india
The title function is: Welcome India
The lower function is: hello india
The Upper function is: WELCOME INDIA
The Swapcase function is: hELLO iNDIA
The Count function is: 2
13
The find function is: 1
The isalpha function is: False
The isdigit function is: False
The replace function is: My India
Result:-
Thus the above program has been successfully executed.

14
10. DISPLAY EACH WORD SEPARATED BY #
Aim: - To write a python program to read and display file content line by
line with each word separated by #.
Algorithm:-
Step 1:- Open a file.
Step 2:- Read the data.
Step 3:- Using Looping statements calculate and print the result.
Source Code:-
sample.txt

Hello Students

Welcome to All

textfilepro.py

f=open(“sample.txt”,’r’)

for text in f.readlines():

for word in text.split():

print(word+ “#” , end=” “)

print(“ “)

f.close()

Output:-

Hello# Students#

Welcome# to# All#

Result:-

Thus the above program has been successfully executed.

15
11. REMOVE THE LINES THAT CONTAIN ‘A’ IN A FILE.
Aim:- To write a python program to remove all the lines that contain the
character ‘a’ in a file and write it to another file.
Algorithm:-
Step 1:- Open a file.
Step 2:- Read the data.
Step 3:- Using Selective statements calculate and print the result.
Source Code:-
sample.txt
I am a python
hello world
textfilepro.py
f=open("sample.txt",'r')
x=f.readlines()
f.close()
f1=open("x.txt",'w')
f2=open("y.txt",'w')
for i in x:
if 'a' in i:
f2.write(line)
else:
f1.write(line)
print("All line that contains a character has been removed from x.txt
file")
print("All line that contains a character has been saved from y.txt file")
f1.close()

16
f2.close()

Output:-
x.txt
hello world

y.txt
I am a python
Result:-
Thus the above program has been successfully executed.

17
12. CASE COUNTING
Aim:- To write a python program to read characters from keyboard one
by one, all lower case letters gets stored inside a file “LOWER”, all
uppercase letters gets stored inside a file “UPPER”, and all other
characters get stored inside “OTHERS”.
Algorithm:-
Step 1:- Open a file.
Step 2:- Read the data.
Step 3:- Using Selective statements calculate and print the result.
Source Code:-
f1=open("e:\\lower.txt","w")
f2=open("e:\\upper.txt","w")
f3=open("e:\\others.txt","w")
while True:
c=input ("Enter a character to write or False to terminate the
program:")
if c=="exit":
break
elif c.islower():
f1.write(c)
elif c.isupper():
f2.write(c)
else:
f3.write(c)
f1.close()
f2.close()
f3.close()

18
Output:-
Enter a character to write or False to terminate the program: h
Enter a character to write or False to terminate the program: H
Enter a character to write or False to terminate the program: a
Enter a character to write or False to terminate the program: A
Enter a character to write or False to terminate the program: i
Enter a character to write or False to terminate the program: I
Enter a character to write or False to terminate the program: @
Enter a character to write or False to terminate the program: 1
Enter a character to write or False to terminate the program:
lower.txt
hai
upper.txt
HAI
others.txt
@1
Result:-
Thus the above program has been successfully executed.

19
13. SEARCH RECORD IN A BINARY FILE
Aim: - To write a Python program to create a binary file with name and
roll number. Search for a given roll number and display the name, if not
found display appropriate message.
Algorithm:-
Step 1:- Import module.
Step 2:- Open a file.
Step 3:- Get Inputs.
Step 4:- Read the data.
Step 5:- Get input for the choice.
Step 6:- Using Looping statements Search and print the result.
Source Code:-
binaryprofile1.py
import pickle
f=open("e:\\stud.dat","wb")
n=int(input("Enter the number of students:"))
for i in range(n):
sno=int(input("Enter the student roll number:"))
sname=input("Enter student name")
x=[sno,sname]
pickle.dump(x,f)
f.close()
binaryprofile2.py
import pickle
f=open("e:\\stud.dat","rb")
n=int(input("Enter the student Number to search"))

20
try:
while True:
x=pickle.load(f)
if x[0]==n:
print("The name of the student is",x[1])
break
except:
print("Data not found in the file.")
f.close()
Output:-
Enter the number of students: 2
Enter the student roll number: 101
Enter the student name: Ajay
Enter the student roll number: 102
Enter the student name: Siva
Enter the student number to search: 101
The name of the student is Ajay
Enter the student number to search: 111
Data not found in the file.
Result:-
Thus the above program has been successfully executed.

21
14. UPDATE RECORD OF A BINARY FILE.
Aim: - To write a Python program to create a binary file with roll number,
name and marks. Input a roll number and update the marks.
Algorithm:-
Step 1:- Import module.
Step 2:- Open a file.
Step 3:- Get inputs.
Step 4:- Read the data.
Step 5:- Get input for the choice.
Step 6:- Using Looping statements update and print the result.
Source Code:-
binaryprofile1.py
import pickle
f=open("e:\\stud.dat","wb")
n=int(input("Enter the number of students:"))
for i in range(n):
sno=int(input("Enter the student roll number:"))
sname=input("Enter student name")
mark=int(input(“Enter Student mark”))
x=[sno,sname,mark]
pickle.dump(x,f)
f.close()
binaryprofile2.py
import pickle
f=open("e:\\stud.dat","rb")
n=int(input("Enter the student Number to update marks"))
22
try:
while True:
x=pickle.load(f)
if x[0]==n:
mark=int(input("Enter the student new marks="))
x[2]=mark
print(x)
break
except:
print("Data not found in the file.")
f.close()
Output:-
Enter the number of students: 2

Enter the student roll number: 111


Enter the student name: Arjun

Enter the student mark: 70


Enter the student roll number: 112
Enter the student name: Ram
Enter the student mark: 90

Enter the student number to update marks: 111

Enter the student new marks: 80


[111,”Arjun”,80]

Enter the student number to update marks: 110


Data not found in the file.

Result: - Thus the above program has been successfully executed.

23
15. EMPLOYEE DETAILS IN CSV FILE.
Aim: - To write a Python program to create a CSV file to store Empno,
Name and Salary. Search and display Name and Salary of a given Empno,
if not found display appropriate message.
Algorithm:-
Step 1:- Import module.
Step 2:- Open a file.
Step 3:- Get Inputs.
Step 4:- Read the data.
Step 3:- Get input for the choice.
Step 4:- Using Selective statements search and print the result.
Source Code:-
csvfile1.py
import csv
f=open("e:\\myfile.csv","a”, newline="")
x=csv.writer(f)
n=int(input("Enter the number of records:"))
for i in range(n):
eno=int(input("Enter Employee Number:"))
ename=input("Enter Employee Name:")
salary=int(input("Enter Employee Salary:"))
y=[eno,ename,salary]
x.writerow(y)
f.close()

24
csvfile2.py
import csv
f=open("e:\\myfile.csv","r")
x=csv.reader(f)
while True:
n=int(input("Enter Employee Number to Search:"))
for i in x:
if int(i[0])==n:
print("NAME :",i[1])
print("SALARY :",i[2])
break
else:
print(" Data not found")
break
f.close()
Output:-
Enter the number of records:2
Enter Employee Number:111
Enter Employee Name:Ajay
Enter Employee Salary:50000
Enter Employee Number:112
Enter Employee Name:Siva
Enter Employee Salary:40000
Enter Employee Number to Search:111
NAME: Ajay
SALARY: 50000
25
Enter Employee Number to Search:113
Data not found
Result:-
Thus the above program has been successfully executed.

26
16. STUDENTS DETAILS IN CSV FILE.
Aim:- To write a Python program create a CSV file to store Rollno, Name
and Marks. Also read the content from CSV file.
Algorithm:-
Step 1:- Import module.
Step 2:- Open a file.
Step 2:- Get Inputs.
Step 3:- Read the data.
Step 4:- Using Looping statements print the result.
Source Code:-
csvfile1.py
import csv
f=open("e:\\myfile1.csv","a", newline="")
x=csv.writer(f)
n=int(input("Enter the number of records:"))
for i in range(n):
Rollno=int(input("Enter Student Roll Number:"))
Name=input("Enter Student Name:")
Marks=int(input("Enter Student Marks:"))
y=[Rollno,Name,Marks]
x.writerow(y)
f.close()
csvfile2.py
import csv
f=open("e:\\myfile1.csv","r")
x=csv.reader(f)
27
for i in x:
print("Student roll number is:",i[0])
print("Student name is:",i[1])
print("Student mark is:",i[2])
f.close()
Output:-
Enter the number of records:2
Enter Student Roll Number:111
Enter Student Name: Ajay
Enter Student Marks:100
Enter Student Roll Number:112
Enter Student Name: Siva
Enter Student Marks:80
Student roll number is: 111
Student name is: Ajay
Student mark is: 100
Student roll number is: 112
Student name is: Siva
Student mark is: 80
Result:-
Thus the above program has been successfully executed.

28
17. READ THE PASSWORD IN A CSV FILE
Aim: - To create a CSV file by entering user-id and password, read and
search the password for given userid.
Algorithm:-
Step 1:- Import module.
Step 2:- Open a file.
Step 3:- Get Inputs.
Step 4:- Read the data.
Step 5:- Get input for the choice.
Step 6:- Using Selective statements search and print the result.
Source Code: -
csvfile1.py
import csv
f=open("e:\\mycsvfile.csv","a", newline="")
x=csv.writer(f)
n=int(input("Enter the number of records:"))
for i in range(n):
uid=input("Enter User:")
pd=input("Enter Password:")
y=[uid,pd]
x.writerow(y)
f.close()
csvfile2.py
import csv
f=open("e:\\mycsvfile.csv",'r')
n=input("Enter uid to search pwd:")
29
x=csv.reader(f)
for i in x:
if i[0]==n:
print("Your Password is:",i[1])
break
else:
print("Sorry data not found")
f.close()
Output:-
Enter the number of records:2
Enter Userid: admin1
Enter Password:111
Enter Userid: admin2
Enter Password:222
Enter uid to search pwd: admin1
Your Password is: 111
Enter uid to search pwd: admin3
Sorry data not found
Result:-
Thus the above program has been successfully executed.

30
18. Write a Python Program demonstrate the stack operation to push the
Book details – b_ID,b_NAME,b_PRICE, delete using pop() function and
display the stack elements.
Aim:-
To write a python program demonstrate the stack operations to
push the book details, delete and display the stack elements.
Code: -
book=[ ]
n=int(input("Enter stack Limit"))
while True:
print("1.Push, 2.Pop, 3. Display, Press Any other key to Exit.")
ch=int(input("Enter your choice"))
if ch==1:
if len(book)==n:
print("Stack Overflow...!!")
else:
bid=int(input("Enter bookid="))
bname=input("Enter book name=")
bprice=int(input("Enter book price="))
x=[bid,bname,bprice]
book.append(x)
elif ch==2:
if len(book)==0:
print("Stack Underflow...!!")
else:
print(book.pop())
elif ch==3:
31
print(book)
else:
break
Output:-
Enter stack Limit2
1.Push, 2.Pop, 3. Display, Press Any other key to Exit.
Enter your choice 1
Enter book id=1
Enter book name=cs
Enter book price=500
1.Push, 2.Pop, 3. Display, Press Any other key to Exit.
Enter your choice 1
Enter bookid=2
Enter book name=ip
Enter book price=450
1.Push, 2.Pop, 3. Display, Press Any other key to Exit.
Enter your choice 3
[[1, 'cs', 500], [2, 'ip', 450]]
1.Push, 2.Pop, 3. Display, Press Any other key to Exit.
Enter your choice 4
>>>

Result:-
Thus the above program has been successfully executed.

32

You might also like