Board Practical Program 24-25
Board Practical Program 24-25
def readline():
x=open('story.txt','r')
for i in x:
li=i.split(' ')
for j in li:
print(j,'#',end='')
readline()
def vowelcon():
x = open('story.txt', 'r')
v=c=u=l=0
y = x.read()
for i in y:
if i.lower() in 'aeiou':
v += 1
elif i.isalpha() and i.lower() not in 'aeiou':
c += 1
if i.isupper():
u += 1
elif i.islower():
l += 1
x.close()
print('No of vowels:', v)
print('No of consonants:', c)
print('No of uppercase letters:', u)
print('No of lowercase letters:', l)
vowelcon()
Remove all the lines that contain the character 'a' in a file and write it
to another file.
def readline():
x = open('story.txt', 'r')
y=open('shan.txt','w+')
for i in x:
for j in i:
if j.lower()=='a':
y.write(i)
x.close()
y.flush()
y.seek(0)
Read and displays all the words longer than 5 characters from a text
file.
def display_long_words():
x=open("notes.txt", 'r')
data=x.read()
words=data.split()
for word in words:
if len(word)>5:
print(word,end=' ')
display_long_words()
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.
import pickle
def read():
d={}
x=open('student.dat','wb')
choice ='y'
while choice in 'yY':
rno=int(input("Enter rollno"))
name=input("Enter name")
d['Rollno']=rno
d['Name']=name
pickle.dump(d,x)
choice=input("Press Y to continue")
x.close()
def search():
flag=False
rno=int(input('Enter Roll no to search'))
x=open('student.dat','rb')
try:
while True:
z=pickle.load(x)
if z['Rollno']==rno:
print(z['Name'])
flag=True
break
except EOFError:
x.close()
if flag==False:
print("Rollno is not present in the file")
read()
search()
Create a binary file with roll number, name and marks. Input a roll
number and update the marks.
import pickle
def read():
x=open('student.dat','wb')
choice ='y'
while choice in 'yY':
rno=int(input("Enter rollno"))
name=input("Enter name")
marks=int(input("Enter marks"))
li=[rno,name,marks]
pickle.dump(li,x)
choice=input("Press Y to continue")
x.close()
def search():
flag=False
rno=int(input('Enter Roll no to search'))
marks=int(input("Enter the marks to update"))
x=open('student.dat','rb+')
try:
while True:
loc=x.tell()
z=pickle.load(x)
if z[0]==rno:
z[2]=marks
x.seek(loc)
pickle.dump(z,x)
flag=True
break
except EOFError:
x.close()
if flag==False:
print("Rollno is not present in the file")
def display():
x=open('student.dat','rb')
try:
while True:
z=pickle.load(x)
print(z)
except EOFError:
x.close()
read()
search()
display()
Create a CSV file by entering user-id and password, read and search
the password for given userid.
import csv
def create():
x=open('users.csv', 'w', newline='')
writer = csv.writer(x)
writer.writerow(["user-id", "password"])
choice = 'y'
while choice in 'yY':
user = input("Enter user-id: ")
password = input("Enter password: ")
writer.writerow([user, password])
choice = input("Press 'Y' to add another user or any other key to stop: ")
def search():
user = input("Enter user-id to search for: ")
flag = False
x=open('users.csv', 'r')
reader = csv.reader(x)
next(reader)
for row in reader:
if row[0] == user:
print("Password for user-id is:",row[1])
flag = True
break
if flag==False:
print(f"No password found for user-id")
create()
search()
Write a Python program to implement a stack using list.
stack = []
def push():
item = int(input("Enter the element to push: "))
stack.append(item)
print(f"{item} pushed onto stack.")
def pop():
if not is_empty():
item = stack.pop()
print(f"{item} popped from stack.")
else:
print("Stack is empty. Cannot pop.")
def peek():
if not is_empty():
print(f"Top element is: {stack[-1]}")
else:
print("Stack is empty. Nothing to peek.")
def is_empty():
return len(stack) == 0
def display():
if not is_empty():
print("Stack elements:", stack)
else:
print("Stack is empty.")
def menu():
while True:
print("\nStack Operations Menu:")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display")
print("5. Exit")
if choice == '1':
push()
elif choice == '2':
pop()
elif choice == '3':
peek()
elif choice == '4':
display()
elif choice == '5':
print("Exiting...")
break
else:
print("Invalid choice! Please select a valid option.")
menu()