0% found this document useful (0 votes)
23 views6 pages

9 PYTHON PROGRAMS (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

9 PYTHON PROGRAMS (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Date: 25/01/2023 SET-1 Max.

Marks: 30
Python Programming

Write a python program to remove all the lines that contain the
character “a‟ in a text file and write it to another file.
ANSWER:
file1=open("file1.txt","r")
l1=file1.readlines()
file1.close()
file2=open("filetwo.txt","w")
Q1
file1=open("file1.txt","w")
for line in l1: 08
if "a" in line:
file2.write(line)
else:
file1.write(line)
file1.close()
file2.close()
print("lines containing character 'a' are removed from file1 and added to
file2 successfully")

Q2
SET-2
Write a Python program to create binary file and store the details of a
student.
ANSWER:
import pickle
stu={}
myfile=open('student.dat','wb')
choice='y'
while choice=='y':
rno=int(input("enter roll number:"))
name=input("enter name:")
Class=int(input("enter class(11/12):"))
total=int(input("enter total marks:"))
stu['rollno']=rno
stu['name']=name
stu['class']=Class
stu['marks']=total
pickle.dump(stu,myfile)
choice=input("do you want to add more records?(y/n)...")
myfile.close()
stu={}
fin=open('student.dat','rb')
try:
print("file contents:")
while True:
stu=pickle.load(fin)
print(stu)
except EOFError:
fin.close()
Date: 25/01/2023 SET-3 Max. Marks: 30
Python Programming
Write a Python program to find the occurrence of a given word in a
string which is passed as an argument to function.
ANSWER:
def countWord(str1,word):
s=str1.split()
count=0
for w in s:
if w==word:
Q3 count+=1 08
return count
str1=input("Enter a sentence:")
word=input("Enter word to search:")
count=countWord(str1,word)
if count==0:
print("Word,",word,"not found")
else:
print("No. of occurences of word'",word,"'in the string: ",count)

Date: 25/01/2023 SET-4 Max. Marks: 30


Python Programming
Write a Python program to create CSV file to store information about
commercial products.
ANSWER:
import csv
myfile=open('product.csv','w',newline='')
prod_writer=csv.writer(myfile)
prod_writer.writerow(['code','name','price'])
for i in range(2):
print("product",i+1)
code=int(input("enter product code:"))
name=input("enter product name:")
Q4
price=float(input("enter price::")) 08
prod=[code,name,price]
prod_writer.writerow(prod)
print("file created sucessfully!")
print("file contents:")
print("------------------")
myfile.close()
with open("product.csv","r",newline='') as fh:
prod_reader=csv.reader(fh)
for rec in prod_reader:
print(rec)
Date: 25/01/2023 SET-5 Max. Marks: 30
Python Programming
Write a program to search an element in a list and display the
frequency of element present in list and their location using Linear
search
ANSWER:
def fun(l,x):
count=0
for i in range (0,len(l)):
if l[i]==x:
Q5
print("Found at INDEX NO: ",i) 08
count+=1
print("Frequency of element: ",count)

a=eval(input("Enter a list: "))


b=eval(input("Enter an element to search: "))
fun(a,b)

Date: 25/01/2023 SET-6 Max. Marks: 30


Python Programming
Q6 Write a Python Program to read a text file line by line and display the
count of vowels, consonants, uppercase, lowercase and other
characters.
ANSWER:
filein=open("myfile.txt","r")
line=filein.read()
count_vow=0
count_con=0
count_low=0
count_up=0
count_digit=0
08
count_other=0
print(line)
for ch in line:
if ch.isupper():
count_up+=1
if ch.islower():
count_low+=1
if ch in "aeiouAEIOU":
count_vow+=1
if ch.isalpha():
count_con+=1
if ch.isdigit():
count_digit+=1
if not ch.isalnum() and ch!=" " and ch!="\n":
count_other+=1
print("digits:",count_digit)
print("vowels:",count_vow)
print("consonants:",count_con- count_vow)
print("uppercase:",count_up)
print("lowercase:",count_low)
print("other than leters and digits:",count_other)
filein.close()

Date: 25/01/2023 SET-7 Max. Marks: 30


Python Programming
Write a program to function with key and value, and update value at
that key in dictionary entered by user
ANSWER:
def fun(d,k):
v=eval(input("Enter the new value:"))
d[k]=v
print("Value changed")
print("Updated dictionary:",d)
Q7
x=int(input("No. of pairs in dictionary:")) 08
dic={}
for i in range(x):
key=eval(input("Enter key:"))
value=eval(input("Enter value:"))
dic[key]=value
print("Original dictionary:",dic)
a=eval(input("Enter the key whose value you want to change:"))
fun(dic,a)
Date: 25/01/2023 SET-8 Max. Marks: 30
Q8 Write a Python program to implement Stack using a list data-
structure.
ANSWER:
def Push():
ele=int(input("Enter the element which you want push:"))
stack.append(ele)
def Pop():
if stack==[]:
print("stack is empty/underflow")
else:
print("The deleted item is:",stack.pop())
def Peek():
if stack==[]:
print("stack is empty/underflow")
else:
top=len(stack)-1
print("The Top most element of Stack is:",stack[top])
def Disp():
top=len(stack)-1
print("The stack elements are:")
for i in range(top,-1,-1):
print(stack[i])
stack=[] 08
opt='y'
while opt=='y' or opt=='Y':
print("Stack operations")
print("***********")
print("1.Push")
print("2.Pop")
print("3.Peek")
print("4.Display")
print("*************")
opt=int(input("Enter your choice:"))

if opt==1:
Push()
elif opt==2:
Pop()
elif opt==3:
Peek()
elif opt==4:
Disp()
else:
print("Invalid Output")
opt=input("Do you want to perform another stack operation (y/n)?:")
Date: 25/01/2023 SET-9 Max. Marks: 30

Q9 To write a Python program input n numbers in tuple and pass it to


function to count how many even and odd numbers are entered.
def count(Tuple):
counteven=0
countodd=0
for i in Tuple:
if i%2 == 0:
counteven+=1 08
else:
countodd+=1
print("The number of even elements in the list is: ",counteven)
print("The number of odd elements in the list is: ",countodd)
T=eval(input("Enter a tuple: "))
count(T)

You might also like