Q.
Read a text file line by line and display each word
separated by #.
file=input("enter the files:")
f=open(file,'r')
item=[]
for line in f:
words=[Link]()
for i in words:
[Link](i)
print("#".join(item))
Input:
Output:
[Link] the tuples containing the given element from a
list of tuples.
input=[(11,22),(33,55),(55,77),(11,44),(3,22,100,11),
(9,11)]
x=11
output=[]
for i in input:
if x in i:
[Link](i)
print(output)
Input:
Output:
[Link] a dictionary with the roll no,name, and marks
of n students in a class and display the names of
students who have marks above 75.
no_of_std=int(input("enter number of student:"))
result={}
for i in range(no_of_std):
print("enter details of student no.",i+1)
roll_no=int(input("roll no:"))
std_name=input("student name:")
marks=int(input("marks:"))
result[roll_no]=[std_name,marks]
print(result)
for student in result:
if result[student][1]>75:
print("student name who get more then 75 marks is/are",
(result[student][0]))
Input:
Output:
[Link] to create binary file to store Rollno, Name
and marks and update marks of entered Rollno.
import pickle
student=[]
f=open('[Link]','wb')
ans='y'
while [Link]()=='y':
roll=int(input("enter roll number:"))
name=input("enter the name:")
marks=int(input("enter marks:"))
[Link]([roll,name,marks])
ans=input("add more?(y)")
[Link](student,f)
[Link]()
f=open('[Link]','rb+')
student=[]
while True:
try:
student=[Link](f)
except Error:
break
ans='y'
while [Link]()=='y':
found=False
r=int(input("enter roll number to update:"))
for s in student:
if s[0]==r:
print("## name is:",s[1],"##")
print("## current marks is:",s[2],"##")
m=int(input("enter new marks:"))
s[2]=m
print("## record updated ##")
found=True
break
if not found:
print("### sorry!roll number not found ###")
ans=input("updated more?(y):")
[Link]()
input:
Output:
[Link] a program to find whether an inputted number
is perfect or not.
num=int(input("enter the number:"))
sum_v=0
for i in range(1,num):
if (num%i==0):
sum_v=sum_v+i
if(sum_v==num):
print("the entered number is a perfect number")
else:
print("the entered number is a not a perfect
number")
Input:
Output:
[Link] to generate random number 1-6, simulating
a dice.
import random
import time
print("press CTRL+C to stop the dice")
play='y'
while play =='y':
try:
while True:
for i in range(10):
print()
n=[Link](1,6)
print(n,end='')
[Link](.00001)
except KeyboardInterrupt:
print("your number is:",n)
ans=input("play More?(y):")
if [Link]()!='y':
play='n'
break
INPUT:
OUTPUT:
[Link] to create binary file to store Rollno and
name,Search any Rollno and display name if Rollno
found otherwise “Rollno not found”.
import pickle
student=[]
f=open('[Link]','wb')
ans='y/n'
while [Link]()=='y/n':
roll=int(input("enter roll number:"))
name=input("enter name:")
[Link]([roll,name])
ans=input("add more?(y/n)")
[Link](student,f)
[Link]()
f=open('[Link]','rb')
student=[]
while True:
try:
student=[Link](f)
except EOFError:
break
ans='y/n'
while [Link]()=='y/n':
found=False
r=int(input("enter roll number to search:"))
for s in student:
if s[0]==r:
print("### name is:",s[1],"##")
found=True
break
if not found:
print("###sorry!roll number not found###")
ans=input("search more?(y/n):")
[Link]()
Input:
Output:
[Link] program to check if the number is an
Armstrong number or not.
num=int(input("enter a number:"))
sum=0
temp=num
while temp>0:
digit=temp%10
sum+=digit**3
temp//=10
if num==sum:
print(num,"is an armstrong number")
else:
print(num,"is not an armstrong number")
Input:
Output:
[Link] a text file and display the number of
vowels/consonents/uppercase/lowercase characters in
the file.
f=open("[Link]")
v=0
c=0
u=0
l=0
o=0
data=[Link]()
vowels=['a','e','i','o','u']
for ch in data:
if [Link]():
if [Link]() in vowels:
v+=1
else:
c+=1
if [Link]():
u+=1
elif [Link]():
l+=1
elif ch!='' and ch!='\n':
o+=1
print("total vowels in file:",v)
print("total consonents in file:",c)
print("total capital letters in file:",u)
print("total small letters in file:",l)
print("total other than letters:",o)
[Link]()
Input:
Output:
[Link] and pop operation on stack.
list=[]
cho="Yy"
while cho in "Yy":
print("[Link]")
print("[Link]")
print("[Link]")
choice=int(input("enter your choice:"))
if choice==1:
pin_code=int(input("enter the pincode of the
city:"))
city_name=input("enter name of the city:")
detail=(pin_code,city_name)
[Link](detail)
elif choice==2:
if list==[]:
print("no record exist")
else:
pin_code,city_name=[Link]()
print("deleted element is:",pin_code,city_name)
elif choice==3:
print(list[len(list)-1],"<--- is TOP element")
for i in range(len(list)-2,-1,-1):
print("wrong input")
else:
print("wrong input")
cho=input("do you want to continue=(y/n)?")
Input:
Output: