CLASS XII – COMPUTER SCIENCE
Python Practical File (Any 15 Programs)
1. Read a text file line by line and display each word separated by #.
f = open("[Link]","r") for line in f: words = [Link]() for w in words: print(w, end="#") print()
[Link]()
2. Count vowels, consonants, uppercase and lowercase letters in a file.
f = open("[Link]","r") v = c = u = l = 0 for ch in [Link](): if [Link](): if ch in "AEIOUaeiou": v += 1
else: c += 1 if [Link](): u += 1 if [Link](): l += 1 print(v, c, u, l) [Link]()
3. Remove lines containing 'a' and write remaining to another file.
f1 = open("[Link]","r") f2 = open("[Link]","w") for line in f1: if 'a' not in line: [Link](line) [Link]()
[Link]()
4. Count lines, words and characters in a file.
f = open("[Link]","r") lines = words = chars = 0 for line in f: lines += 1 words += len([Link]())
chars += len(line) print(lines, words, chars) [Link]()
5. Replace all spaces with # in a file.
f = open("[Link]","r") data = [Link]() [Link]() f = open("[Link]","w") [Link]([Link](" ","#"))
[Link]()
6. Binary file: store name & roll, search by roll.
import pickle f = open("[Link]","ab") [Link]([1,"Aman"], f) [Link]() f = open("[Link]","rb")
try: while True: r = [Link](f) if r[0] == 1: print(r) except: pass [Link]()
7. Binary file: update marks of student.
import pickle data = [] f = open("[Link]","rb") try: while True: [Link]([Link](f)) except:
pass [Link]() for i in data: if i[0] == 1: i[2] = 95 f = open("[Link]","wb") for i in data: [Link](i,f)
[Link]()
8. Append records to existing binary file.
import pickle f = open("[Link]","ab") [Link]([2,"Riya",88],f) [Link]()
9. Display all records from binary file.
import pickle f = open("[Link]","rb") try: while True: print([Link](f)) except: pass [Link]()
10. Delete record using roll number.
import pickle data=[] f=open("[Link]","rb") try: while True: [Link]([Link](f)) except: pass
[Link]() r=int(input("Roll: ")) data=[i for i in data if i[0]!=r] f=open("[Link]","wb") for i in data:
[Link](i,f) [Link]()
11. Create CSV file for user-id and password.
import csv f=open("[Link]","w",newline="") w=[Link](f) [Link](["user","pass"])
[Link](["admin","1234"]) [Link]()
12. Search password for given user-id from CSV.
import csv u=input("User: ") f=open("[Link]","r") r=[Link](f) for row in r: if row[0]==u:
print(row[1]) [Link]()
13. Read and display all records from CSV.
import csv f=open("[Link]","r") for row in [Link](f): print(row) [Link]()
14. Count total records in CSV file.
import csv f=open("[Link]","r") c=0 for row in [Link](f): c+=1 print(c-1) [Link]()
15. Stack using list with push and pop.
stack=[] [Link](10) [Link](20) print([Link]())