Xii CS Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

EX.

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()

for line in lines:

words=line.split()

for word in words:

print(word+"#",end="")

print("")

file.close()
OUTPUT

SAMPLE TEXT FILE

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("Vowels are :",vowels)

print("Consonants :",consonants)
print("Lower_case_letters :",lower_case_letters)

print("Upper_case_letters :",upper_case_letters)
OUTPUT

SAMPLE TEXT FILE

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

#Create a binary file with roll number, name and marks.


import pickle
student_data={}
no_of_students=int(input("Enter no..of Students to insert in file : "))
file=open("C:\\Users\\dasam\\Desktop\\student_data","wb")
for i in range(no_of_students):
student_data["RollNo"]=int(input("Enter roll no :"))
student_data["Name"]=input("Enter Student Name :")
student_data["Marks"]=float(input("Enter Students Marks :"))
pickle.dump(student_data,file)
student_data={}
file.close()
print("data inserted Successfully")

#Input a roll number and update the marks.


import pickle
student_data={}
found=False
roll_no=int(input("Enter the roll no to search :"))
file=open("C:\\Users\\dasam\\Desktop\\student_data","rb+")
try:
while True:
pos=file.tell()
student_data=pickle.load(file)
if(student_data["RollNo"]==roll_no):
student_data["Marks"]=float(input("Enter marks to update"))
file.seek(pos)
pickle.dump(student_data,file)
found=True
except EOFError:
if(found==False):
print("Roll no not found.please try again")
else:
print("Students marks updated Successfully")
file.close()

# print the updated data


import pickle
student_data={}
file=open("C:\\Users\\dasam\\Desktop\\student_data","rb")
try:
while True:
student_data=pickle.load(file)
print(student_data)
except EOFError:
file.close()
OUTPUT
EX.NO:5 FILE OPERATIONS

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:

To write a Python program to implement a stack using a list data-structure.

PROGRAM:

def push():

a=int(input("Enter the element which you want to push:"))

stack.append(a)

return a

def pop():

if stack==[]:

print("Stack empty....Underflow case....can not delete the element...")

else:

print("deleted element is :",stack.pop())

def display():

if stack==[]:

print("Stack empty....no elements in 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("3.DISPLAY ELEMENTS OF STACK")

print("************************************")

c=int(input("Enter your choice : "))

if c==1:

push()

elif c==2:

pop()

elif c==3:

display()

else:

print("wrong input:")

choice=input("Do you want to continue or not?(y/n) : ")


OUTPUT

You might also like