You are on page 1of 16

R.S.B.V WEST NINOD NAGAR DELHI-110092 PAGE NO.

QUES.3

Write a python program to read a text file line by line and display each word separated by #.

SOLUTION.

To write a python program to read a text file line by line and display each word separated by #.

SOURCE CODE:

f=open("karan.txt","r")

for line in f:

word=line.split()

for w in word:

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

print()

f.close()

OUTPUT:
R.S.B.V WEST VINOD NAGAR DELHI-110092 PAGE NO.4

QUES:4
Write a python program to read a text file and display the number of vowels /consonants/uppercase/lowercase characters in the file.

SOLUTION:
Write a python program to read a text file and display the number of vowels /consonants/uppercase/lowercase characters in the file.

SOURCE CODE:
def cnt():
f=open(“karan.txt”,”r”)
cont=f.read()
print(cnt)
v=0
cons=0
lc=0
uc=0
for ch in count:
if(ch.islower()):
lc+=1
elif(ch.isupper()):
uc+=1
ch=ch.lower()
if(ch in[‘a’,’e’,’i’,’o’,’u’]):
v+=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’]):
cons+=1
f.close()
print(“vowels are:”,v)
print(“consonants are:”,cons)
print(“lower case are:”,lc)
print(“upper case are:”,uc)
Cnt()

OUTPUT:
R.S.B.V WEST VINOD NAGAR DELHI-110092 PAGE NO. 5

QUES 5:
Write a python program to remove all the lines that contain the character ‘a’ in a file and write it to another file.

SOLUTION:
To write a python program to remove all the lines that contain the character ‘a’ in a file and write it to another file.

SOURCE CODE :
file= open("karan.txt","r")
lines=file.readlines()
file.close()
file=open("karan.txt","w")
file1=open("hp.txt","w")
for line in lines:
if 'a' in line:
file1.write(line)
else:
file.write(line)
print("lines that contain a character are removed from karan")
print("lines that contain a character are added in hp")
file.close()
file1.close()

OUTPUT:
R.S.B.V WEST VINOD NAGAR DELHI-110092 PAGE.NO 6

QUES6:
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 .

SOLUTION:
To write a python program to 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.

SOURCE CODE:
import pickle
sdata={}
slist=[]
totals=int(input("enter no. of student:"))
for i in range(totals):
sdata["roll no."]=int(input("enter roll no.:"))
sdata["name"]=input("enter name:")
slist.append(sdata)
sdata={}
a=open("karan.dat","wb")
pickle.dump(slist,a)
a.close()
x=open("karan.dat","rb")
slist=pickle.load(x)
b=int(input("enter the roll number of student to be searched:"))
y=False
for text in slist:
if(text["roll no."]==b):
y=True
print(text["name"],"found in file")
if(y==false):
print("data of student not found ")

OUTPUT:
R.S.B.V WEST VINOD NAGAR DELHI-110092 PAGE NO.7

QUES7:
Write a python program to create a binary file with emp no., name, salary, then the read and display their details on screen.

SOLUTION:
To write a python program to create a binary file with emp no., name, salary, then the read and display their details on screen.

SOURCE CODE:
import pickle
def write():
f=open("karan.dat","ab")
d={}
n=int(input("enter how many employee details you want to store:"))
for i in range(n):
d['emp no']=int(input("enter the employee number:"))
d['emp name']=input("enter the employee name:")
d['salary']=int(input("enter the employee salary:"))
pickle.dump(d,f)
f.close()

def read():
f=open("karan.dat","rb")
s=pickle.load(f)
print(s)
f.close()
write()
read()

OUTPUT:
R.S.B.V WEST VINOD NAGAR DELHI –110092 PAGE NO.8

QUES8:
Take a sample text file and find the most commonly occurring word. Also, list the frequencies of words in the text file.

SOLUTION:
To take a sample text file and find the most commonly occurring word. Also, list the frequencies of words in the text file.

SOURCE CODE:
with open("karan.txt","r")as fh:
contents=fh.read()
wordlist=contents.split()
wordfreq=[]
high=0
word=""
existing=[]
for w in wordlist:
wcount=wordlist.count(w)
if w not in existing:
wordfreq.append([w,wcount])
existing.append(w)
if wcount>high:
high=wcount
word=w
print("the word'''+word''' occurs maximum number f times,",high,"times.")
print("\n other word have these frequencies:")
print(wordfreq)

OUTPUT:
R.S.B.V WEST VINOD NAGAR DELHI –110092 PAGE NO.9

QUES:9
Create a binary file with roll number, name and marks. Input a roll number and update the marks.

SOLUTION:
To write a python program to create a binary file with roll number, name and marks. Input a roll number and update the marks.

SOURCE CODE:
import pickle
sdata={}
totals=int(input("enter the no. of student"))
a=open("karan.dat","wb")
for i in range(totals):
sdata["roll no."]=int(input("enter roll no:"))
sdata["name"]=input("enter the name:")
sdata["marks"]=float(input("enter the marks:"))
pickle.dump(sdata,a)
sdata={}
a.close()
found=False
rollno=int(input("enter roll no to update:"))
a=open("karan.dat","rb+")
try:
while True:
pos=a.tell()
sdata=pickle.load(a)
if(sdata["roll no"]==rollno):
sdata["marks"]=float(input("enter new marks:"))
a.seek(pos)
pickle.dump(sdata,a)
found=True
except EOFError:
if found==False:
print("roll number not found")
else:
print("student marks updated succesfully")
a.close()

OUTPUT:
R.S.B.V WEST VINOD NAGAR DELHI –110092 PAGE NO.10

QUES10:
Write a python program to create a random number generator that generates random numbers between 1 and 6 (simulates a
dice).

SOLUTION:
To create a random number generator that generates random numbers between 1 and 6 (simulates a dice).

SOURCE CODE:
import random
rand_list = []
for i in range(0,1):
n = random.randint(1,6)
rand_list.append(n)
print(rand_list)

OUTPUT:
R.S.B.V WEST VINOD NAGAR DELHI-110092 PAGE NO.11

QUES11:
Program to take 10 sample phishing email, and find the most common word occurring.

SOLUTION:
To write a Program to take 10 sample phishing email, and find the most common word occurring.

SOURCE CODE:

OUTPUT:
R.S.B.V WEST VINOD NAGAR DELHI-110092 PAGE NO.12

QUES12:
Create a CSV file by entering user id and password, read and search the password for given user id.

SOLUTION:
To write a python program to create a CSV file by entering user id and password, read and search the password for given
user id.

SOURCE CODE:
import csv
with open("7.csv","w")as obj:
fileobj=csv.writer(obj)
fileobj.writerow(["user id","password"])
while(True):
user_id=input("enter id:")
password=input("enter password:")
record=[user_id,password]
fileobj.writerow(record)
x=input("press Y/y to continue and N/n to terminate the program\n")
if x in "Nn" :
break
elif x in "Yy":
continue
with open("7.csv","r")as obj2:
fileobj2=csv.reader(obj2)
given=input("enter the user id to be searched\n")

for i in fileobj2:
next(fileobj2)
if i[0]==given:
print(i[1])
break

OUTPUT:
R.S.B.V WEST VINOD NAGAR DELHI –110092 PAGE NO.13

QUES13:
Create a python program to create and search student’ record in csv file.

SOLUTION:
To write a python program create a csv file to store roll no., name, age and search any roll no and display name and, age and if
not found display appropriate message.

SOURCE CODE:
import csv
def create():
f=open("stu.csv","a")
w=csv.writer(f)
opt='y'
while opt=='y':
no=int(input("enter the student roll no:"))
name=input("enter the student name:")
age=int(input("enter the student age:"))
l=[no,name,age]
w.writerow(l)
opt=input("do you want to add another details?(y/n):")
f.close()
def read():
f=open("stu.csv","r",newline='\r\n')
no=int(input("enter student roll no to search"))
found=0
row=csv.reader(f)
for data in row:
if data[0]==str(no):
print("student details are:")
print("============================")
print("name:",data[1])
print("age:",data[2])
print("============================")
found=1
break
if found==0:
print("the student roll no is not found")
f.close()
create()
read()
OUTPUT :
R.S.B.V WEST VINOD NAGAR DELHI-110092 PAGE NO.14

QUES14:
Write a python program to create a CSV file with empid, name and mobile no. and search empid ,update the record and display
the records.

SOLUTION:
To write a python program to create a CSV file with empid, name and mobile no. and search empid ,update the record and
display the records.

SOURCE CODE:
import csv
with open("karan.txt",'a')as csvfile:
mywritwr=csv.writer(csvfile,delimiter=",")
ans='y'
while ans.lower()=='y':
eno=int(input("enter employee id:"))
name=input("enter employee name:")
mobno=int(input("enter employee mobile no:"))
mywritwr.writerow([eno,name,mobno])
ans=input("do you want to enter more data?(y/n):")
ans='y'
with open("karan.txt",'r')as csvfile:
myreader=csv.reader(csvfile,delimiter=',')
ans='y'
while ans.lower()=='y':
found=False
e=int(input("enter employee id to search:"))
for row in myreader:
if len(row)!=0:
if int(row[0])==e:
print("name:",row[1])
print("mobile no.:",row[2])
found=True
break
if not found:
print("employee id not found")
ans=input("dou want to search more?(y/n):")
ans='y'
with open("karan.txt",'a')as csvfile:
x=csv.writer(csvfile)
x.writerow([eno,name,mobno])
OUTPUT:
R.S.B.V WEST VINOD NAGAR DELHI -110092 PAGE NO.15

QUES15:
Write a python program to implement a stack using a list (push & pop operation on stack ).

SOLUTION :
To write a python program to implement a stack using a list (push & pop operation on stack ).

SORCE CODE :
def isempty(s):
if len(s)==0:
return True
else:
return False
def push(s,item):
s.append(item)
top=len(s)-1
def pop(s):
if isempty(s):
return'underflow occurs'
else:
val=s.pop()

if len(s)==0:
top=None
else:
top =len(s)-1
return val
def peek(s):
if isempty(s):
return'underflow occurs'
else:
top=len(s)-1
return s[top]
def show(s):
if isempty(s):
print('no item found')
else:
t=len(s)-1
print('(top)',end="")
while(t>=0):
print(s[t],'<==',end="")
t=t-1
print()
s=[]
top=None
while True:
print('***STACK IMPLEMENTATION USING LIST****')
print('1:PUSH')
print('2:POP')
print('3:PEEK')
print('4:SHOW')
print('0:EXIT')
ch=int(input("enter choice"))
if ch==1:
val=int(input("enter no to push;")
push(s,val)
elif ch==2:
val=pop(s)
if val=="underflow":
print("stack is empty")
else:
print("\nTop item is:",val)
elif ch==3:
val=peek(s)
if val=='underflow':
print('stack is empty')
else:
print('\nTop item is:',val)
elif ch==4:
show(s)
elif ch==0:
print('bye')
break

OUTPUT:
R.S.B.V WEST VINOD NAGAR DELHI-110092 PAGE NO.1

QUES1:
Write a python program to input three numbers and display the larger or smaller number.

SOLUTION:
To write a python program to input three numbers and display the larger or smaller number.

SOURCE CODE:
n1 = float(input("ENTER FIRST NO:"))
n2 = float(input("ENTER SECOND NO:"))
n3 = float(input("ENTER THIRD NO:"))
if (n1 > n2) and (n1 > n3):
large = n1
elif (n2 > n1) and (n2 > n3):
large = n2
else:
large = n3
if (n1 < n2) and (n1 < n3):
small = n1
elif (n2 < n1) and (n2 < n3):
small = n2
else:
small = n3
print("The large number is", large, "\n")
print("The small number is", small, "\n")

OUTPUT:
R.S.B.V WEST VINOD NAGAR DELHI-110092 PAGE NO.2

QUES2:
Write a python program to input a number and check if the number is a prime number or composite number.

SOLUTION:
To write a python program to input a number and check if the number is a prime number or composite number.

SOURCE CODE:
number = int(input("Enter any number: "))
if number > 1:
for i in range(2, number):
if (number % i) == 0:
print(number, "is COMPOSITE number")
break
else:
print(number, "is a PRIME number")
else:
print(number, "is COMPOSITE number")

OUTPUT:

You might also like