0% found this document useful (0 votes)
8 views

Python Coding

Fbcbb

Uploaded by

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

Python Coding

Fbcbb

Uploaded by

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

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=line.split()
for i in words:
item.append(i)
print("#".join(item))
Input:

Output:
Q.Find 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:
output.append(i)
print(output)
Input:

Output:
Q.create 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:
Q.program to create binary file to store Rollno, Name
and marks and update marks of entered Rollno.
import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll=int(input("enter roll number:"))
name=input("enter the name:")
marks=int(input("enter marks:"))
student.append([roll,name,marks])
ans=input("add more?(y)")
pickle.dump(student,f)
f.close()
f=open('student.dat','rb+')
student=[]
while True:
try:
student=pickle.load(f)
except Error:
break
ans='y'
while ans.lower()=='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):")
f.close()
input:

Output:
Q.write 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:
Q.Program 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=random.randint(1,6)
print(n,end='')
time.sleep(.00001)
except KeyboardInterrupt:
print("your number is:",n)
ans=input("play More?(y):")
if ans.lower()!='y':
play='n'
break
INPUT:

OUTPUT:
Q.Program 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('student.dat','wb')
ans='y/n'
while ans.lower()=='y/n':
roll=int(input("enter roll number:"))
name=input("enter name:")
student.append([roll,name])
ans=input("add more?(y/n)")
pickle.dump(student,f)
f.close()
f=open('student.dat','rb')
student=[]
while True:
try:
student=pickle.load(f)
except EOFError:
break
ans='y/n'
while ans.lower()=='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):")
f.close()
Input:

Output:
Q.python 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:
Q.read a text file and display the number of
vowels/consonents/uppercase/lowercase characters in
the file.
f=open("welcome.py")
v=0
c=0
u=0
l=0
o=0
data=f.read()
vowels=['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
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)
f.close()
Input:

Output:
Q.push and pop operation on stack.
list=[]
cho="Yy"
while cho in "Yy":
print("1.PUSH")
print("2.POP")
print("3.DISPLAY")
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)
list.append(detail)
elif choice==2:
if list==[]:
print("no record exist")
else:
pin_code,city_name=list.pop()
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:

You might also like