Practical File XII CS Python 2021
Practical File XII CS Python 2021
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()
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)
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)
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
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