Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
141 views

Practical File XII CS Python 2021

The document contains instructions for 15 Python programming practical assignments. Practical 8 involves creating a binary file to store student roll numbers, names, and marks, and updating the marks for a given roll number. Practical 9 involves creating a binary file to store employee codes, names, salaries and searching for a given employee code. Practical 15 involves creating a binary file called student.dat to store student records including roll number, name, and address, and writing functions to write, read, and print the data.

Uploaded by

Harsh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views

Practical File XII CS Python 2021

The document contains instructions for 15 Python programming practical assignments. Practical 8 involves creating a binary file to store student roll numbers, names, and marks, and updating the marks for a given roll number. Practical 9 involves creating a binary file to store employee codes, names, salaries and searching for a given employee code. Practical 15 involves creating a binary file called student.dat to store student records including roll number, name, and address, and writing functions to write, read, and print the data.

Uploaded by

Harsh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

KENDRIYA VIDYALAYA MALANJKHAND

CBSE Term – 1 Practical List Class XII Computer Science 2021-22


Teachers
S.No. Practical Date
Sign
Programming in Python
1 Write a program to read a text file line by line and display each word separated
by a #
2 Write a program to read a text file and display the
a) Number of vowels in the file
b) Number of consonants in the file
c) Number of uppercase and lowercase characters in the file
3 Write a program to remove all the lines that contain the character ‘a’ in a file
and write it to another file.
4 Write a program in Python to copy the contents of one file to another.
5 Write a program to read a text file line by line and show only those lines which
starts with the letter ‘T’.
6 Write a program in python to search and display number of occurrence of a
particular word.
7 Write a 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.
8 Write a program to create a binary file with roll number, name and marks. Input
a roll number and update the marks.
9 Write a program to create a binary file to store empcode, empname and empsal.
Search record for a given employee code.
10 Write a program to create a binary file to store empcode, empname and empsal.
Update salary for a given employee code.
11 Write a program to create a csv file by entering user-id and password, read and
search the password for given user id.
12 Write a program to create a csv file with details such as bookid, title, author.
Read the csv file and show contents on the screen.
13 Write a program to read a csv file containing marks in five subjects. Show total
and average of each row in a csv file.
14 Write a random number generator that generates random numbers between 1 and
6 (simulates a dice)
15 Create a binary file student.dat to hold students’ records like Rollno., Students
name, and Address using the list. Write functions to write data, read them, and
print on the screen.
Practical No. 8
Write a program to create a binary file with roll number, name and marks. Input a roll number and update the
marks.

import pickle
stud1 = {"roll":1201, 'name':'Vikas', 'Marks':98}
stud2 = {"roll":1202, 'name':'Mayank', 'Marks':67}
stud3 = {"roll":1203, 'name':'Shruti', 'Marks':54}
stud4 = {"roll":1204, 'name':'Mayuri', 'Marks':81}
stud5 = {"roll":1205, 'name':'Vaibhav', 'Marks':62}

myfile = open(r"D:/Practical2021/student.dat","wb")
pickle.dump(stud1,myfile)
pickle.dump(stud2,myfile)
pickle.dump(stud3,myfile)
pickle.dump(stud4,myfile)
pickle.dump(stud5,myfile)

myfile.close()

rno = int(input('Enter rollno to update marks'))


m = int(input('Enter new marks '))
try:
myfile = open(r"D:/Practical2021/student.dat","rb+")
while True:
pointer = myfile.tell()
d = pickle.load(myfile)
if d['roll'] == rno:
myfile.seek(pointer)
d['Marks'] = m
pickle.dump(d,myfile)
print(d)
except FileNotFoundError:
print('Please check the file name ')
except EOFError:
myfile.close()

OUTPUT
Enter rollno to update marks 1204
Enter new marks 100
{'roll': 1201, 'name': 'Vikas', 'Marks': 98}
{'roll': 1202, 'name': 'Mayank', 'Marks': 67}
{'roll': 1203, 'name': 'Shruti', 'Marks': 54}
{'roll': 1204, 'name': 'Mayuri', 'Marks': 100}
{'roll': 1205, 'name': 'Vaibhav', 'Marks': 62}
Practical No. 9
Write a program to create a binary file to store empcode, empname and empsal. Search record for a
given employee code.

import pickle
emp1 = {"ecode":201051, 'name':'Santosh', 'Salary':15000}
emp2 = {"ecode":201052, 'name':'Deepak', 'Salary':25000}
emp3 = {"ecode":201053, 'name':'Kuldeep', 'Salary':34000}
emp4 = {"ecode":201054, 'name':'Ajay', 'Salary':20000}
emp5 = {"ecode":201055, 'name':'Ravita', 'Salary':55000}

myfile = open(r"D:/Practical2021/employee.dat","wb+")
pickle.dump(emp1,myfile)
pickle.dump(emp2,myfile)
pickle.dump(emp3,myfile)
pickle.dump(emp4,myfile)
pickle.dump(emp5,myfile)

found=False
myfile.seek(0)

code = int(input('Enter employee code to search '))


try:
myfile = open(r"D:/Practical2021/employee.dat","rb+")
while True:
d = pickle.load(myfile)
if d['ecode'] == code:
print('Record found\n', d)
found=True
except FileNotFoundError:
print('Please check the file name ')
except EOFError:
if found:
myfile.close()
else:
print('Employee details not found ')
myfile.close()

OUTPUT 1
Enter employee code to search 201054
Record found
{'ecode': 201054, 'name': 'Ajay', 'Salary': 20000}

OUTPUT 2
Enter employee code to search 12345
Employee details not found
Practical No. 10
Write a program to create a binary file to store empcode, empname and empsal. Update salary for a
given employee code.

import pickle
emp1 = {"ecode":201051, 'name':'Santosh', 'Salary':15000}
emp2 = {"ecode":201052, 'name':'Deepak', 'Salary':25000}
emp3 = {"ecode":201053, 'name':'Kuldeep', 'Salary':34000}
emp4 = {"ecode":201054, 'name':'Ajay', 'Salary':20000}
emp5 = {"ecode":201055, 'name':'Ravita', 'Salary':55000}

myfile = open(r"D:/Practical2021/employee.dat","wb+")
pickle.dump(emp1,myfile)
pickle.dump(emp2,myfile)
pickle.dump(emp3,myfile)
pickle.dump(emp4,myfile)
pickle.dump(emp5,myfile)

found=False
myfile.seek(0)

code = int(input('Enter employee code to update salary '))


try:
myfile = open(r"D:/Practical2021/employee.dat","rb+")
while True:
pointer = myfile.tell()
d = pickle.load(myfile)
if d['ecode']==code:
myfile.seek(pointer)
found=True
print('Record found\n',d)
newsalary = int(input('Enter new salary '))
d['Salary'] = newsalary
pickle.dump(d,myfile)
print(d)
except FileNotFoundError:
print('Please check the file name ')
except EOFError:
if found:
myfile.close()
else:
print('Employee details not found ')
myfile.close()

OUTPUT
Enter employee code to update salary 201052
{'ecode': 201051, 'name': 'Santosh', 'Salary': 15000}
Record found
{'ecode': 201052, 'name': 'Deepak', 'Salary': 25000}
Enter new salary 26750
{'ecode': 201052, 'name': 'Deepak', 'Salary': 26750}
{'ecode': 201053, 'name': 'Kuldeep', 'Salary': 34000}
{'ecode': 201054, 'name': 'Ajay', 'Salary': 20000}
{'ecode': 201055, 'name': 'Ravita', 'Salary': 55000}
Practical No.11
Write a program to create a csv file by entering user-id and password, read and search the password for given
user id.

import csv
list1 = ['userid','password']
list2 = [ ['divya','XYZ@1234'],
['shreya','KVM@2021'],
['kavita','abcd@4567'],
['lalit','PQRS@9999'],
['mayank','freedom@1947']
]

myfile = open(r"D:\Practical2021\users.csv","w+",newline='\r\n')
writer = csv.writer(myfile)
writer.writerow(list1)
writer.writerows(list2)

found = False
myfile.seek(0)
user = input('Enter user id name ')
reader = csv.reader(myfile)

for x in reader:
if x[0]==user:
print('Password is ',x[1])
found = True
myfile.close()
if found:
pass
else:
print('User id not found')

OUTPUT
Enter user id name mayank
Password is freedom@1947

Enter user id name mohan


User id not found
Practical No. 12
Write a program to create a csv file with details such as bookid, title, author. Read the csv file and show
contents on the screen.

SOURCE CODE
import csv
myfile = open(r"D:\Practical2021\library.csv","w+",newline='\r\n')
list1 = [ ]
n = int(input('How many records you want to enter? '))
writer = csv.writer(myfile)
for i in range(n):
bookid = input('Enter book id ')
title = input('Enter title of book ')
author = input('Enter author of the book ')
list1=[bookid, title, author]
writer.writerow(list1)

myfile.seek(0)
reader = csv.reader(myfile)
print('ALL RECORDS')
for record in reader:
print(record)

myfile.close()

OUTPUT
How many records you want to enter? 5
Enter book id 1111
Enter title of book Python
Enter author of the book John
Enter book id 2222
Enter title of book Java
Enter author of the book Pankaj
Enter book id 3333
Enter title of book SQL
Enter author of the book Uday
Enter book id 4444
Enter title of book Networks
Enter author of the book Shiv Prasad
Enter book id 5555
Enter title of book C++
Enter author of the book Yashwant
ALL RECORDS
['1111', 'Python', 'John']
['2222', 'Java', 'Pankaj']
['3333', 'SQL', 'Uday']
['4444', 'Networks', 'Shiv Prasad']
['5555', 'C++', 'Yashwant']
Practical No. 14
Write a random number generator that generates random numbers between 1 and 6 (simulates a
dice)
import random
print("I am throwing a dice....")
print('Dice thrown!')
n = random.randint(1,6)
print('Guess the number, You have three chances')
for i in range(3):
print('Guess',i+1,end=':')
guess = int(input())
if guess==n:
print("You won!Congratulations")
break
else:
print("NO ")
else:
print("Wrong Guess, The correct number is",n)

I am throwing a dice....
Dice thrown!
Guess the number, You have three chances
Guess 1:1
NO
Guess 2:2
NO
Guess 3:3
NO
Wrong Guess, The correct number is 5

I am throwing a dice....
Dice thrown!
Guess the number, You have three chances
Guess 1:4
NO
Guess 2:5
You won!Congratulations
Practical No. 15
Create a binary file student.dat to hold students’ records like Rollno., Students name, and Address using the
list. Write functions to write data, read them, and print on the screen.

SOURCE CODE
import pickle
rec = [ ]
def file_create():
f = open('student.dat','wb')
rno = int(input('Enter student roll number '))
sname = input('Enter student name ')
address = input("Enter student's address ")
rec = [rno, sname, address]
pickle.dump(rec,f)
f.close()

def file_read():
f = open('student.dat','rb')
print('#'*15)
print('Data stored in file ')
rec = pickle.load(f)
for i in rec:
print(i)
f.close()

file_create()
file_read()

OUTPUT
Enter student roll number 1201
Enter student name Aman Verma
Enter student's address Malanjkhand,Balaghat
###############
Data stored in file
1201
Aman Verma
Malanjkhand,Balaghat

You might also like