Xii CS Lab
Xii CS Lab
Xii CS Lab
NO:1 READ A TEXT FILE LINE BY LINE AND DISPLAY EACH WORD SEPARATED BY ‘#’
AIM
To write a python program to read a text file line by line and display each word separated by a #.
PROGRAM
file=open("PRACTICAL1.TXT","r")
lines=file.readlines()
words=line.split()
print(word+"#",end="")
print("")
file.close()
OUTPUT
PROGRAM OUTPUT
EX.NO:2 READ A TEXT FILE AND DISPLAY THE NUMBER OF
VOWELS/CONSONANTS/UPPERCASE/LOWERCASE CHARACTERS IN THE FILE
AIM
To write a python program to read a text file and display the number of vowels/ consonants/
uppercase/ lowercase characters in the file.
PROGRAM
file=open("PRACTICAL1.TXT","r")
content=file.read()
vowels=0
consonants=0
lower_case_letters=0
upper_case_letters=0
for ch in content:
if(ch.islower()):
lower_case_letters+=1
elif(ch.isupper()):
upper_case_letters+=1
ch=ch.lower()
if (ch in ['a','e','i','o','u']):
vowels+=1
elif(ch in ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']):
consonants+=1
file.close()
print("Consonants :",consonants)
print("Lower_case_letters :",lower_case_letters)
print("Upper_case_letters :",upper_case_letters)
OUTPUT
PROGRAM OUTPUT
EX.NO:3 SEARCHING DATA IN A BINARY FILE
AIM
To write a python program to create a binary file with name and roll number and to search for a given
roll number and display the name.
PROGRAM
“Create a binary file with name and roll number
import pickle
stud_data={}
list_of_students=[]
no_of_students=int(input("Enter no of Students:"))
for i in range(no_of_students):
stud_data["roll_no"]=int(input("Enter roll no:"))
stud_data["name"]=input("Enter name: ")
list_of_students.append(stud_data)
stud_data={}
file=open("C:\\Users\\dasam\\Desktop\\stud_data.dat","wb")
pickle.dump(list_of_students,file)
print("Data added successfully")
file.close()
“Search for a given roll number and display the name, if not found display appropriate message.”
import pickle
file=open("C:\\Users\\dasam\\Desktop\\stud_data.dat","rb")
list_of_students=pickle.load(file)
roll_no=int(input("Enter roll no.of student to search"))
found=False
for stud_data in list_of_students:
if(stud_data["roll_no"]==roll_no):
found=True
print(stud_data["name"],"found in file.")
if (found==False):
print("No student data found. please try again")
file.close()
OUTPUT
EX.NO:4 UPDATING DATA IN A BINARY FILE
AIM
To write a python program to create a binary file with roll number, name and marks and input a roll
number and update the marks.
PROGRAM
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.
PROGRAM
file=open("C:\\Users\\dasam\\Desktop\\format.txt","r")
lines=file.readlines()
file.close()
file=open("C:\\Users\\dasam\\Desktop\\format.txt","w")
file1=open("C:\\Users\\dasam\\Desktop\\second.txt","w")
for line in lines:
if 'a' in line or 'A' in line:
file1.write(line)
else:
file.write(line)
print("All lines that contains 'a' character has been removed in format.txt file")
print("All lines that contains 'a' character has been saved in second.txt file")
file.close()
file1.close()
OUTPUT
EX.NO:6 RANDOM NUMBER GENERATOR
AIM:
To write a random number generator that generates random numbers between 1 and 6.
PROGRAM:
import random
while(True):
choice=input("Enter (r) for roll dice or press any other key to quit")
if(choice!="r"):
break
n=random.randint(1,6)
print(n)
OUTPUT
EX.NO:7 IMPLEMENTATION OF STACK USING LIST
AIM:
PROGRAM:
def push():
stack.append(a)
return a
def pop():
if stack==[]:
else:
def display():
if stack==[]:
else:
for i in range(len(stack)-1,-1,-1):
print(stack[i])
stack=[ ]
print("STACK OPERATIONS")
print("***********************")
choice="y"
while choice=="y":
print("1.PUSH")
print("2.POP")
print("************************************")
if c==1:
push()
elif c==2:
pop()
elif c==3:
display()
else:
print("wrong input:")