Write A Program That Reads A Line and Prints Its Statistics Like
Write A Program That Reads A Line and Prints Its Statistics Like
Write a program that reads a line and prints its statistics like:
Number of uppercase
Number of lowercase
Number of alphabets
Number of digits
line=input("Enter a line:")
lowercount=uppercount=0
digitcount=alphacount=0
for a in line:
if a.islower():
lowercount+=1
elif a.isupper():
uppercount+=1
elif a.isdigit():
digitcount+=1
if a.isalpha():
alphacount+=1
Output:
Number of alphabets: 18
Number of digits: 3
2. Write a Program to create a dictionary containing names of competition winner students
as key and number of their wins as value.
CompWinners={}
for a in range(n):
CompWinners[key]=value
print(CompWinners)
Output:
How many students?5
list2=['h','i','t']
list3=['0','1', '2']
print("Originally :")
print("List1=", list1)
print("List2=", list2)
print("List3=", list3)
list3.extend(list1)
list3.extend(list2)
print(list3)
Output:
Originally :
if char in tuple1:
count = 0
for a in tuple1:
if a != char:
count += 1
else:
break
else:
Output:
si1=interest(prin)
print("Rs.", si1)
print("Simple interest with your provided ROI and time values is : ")
print("Rs.", si2)
Output:
Rs. 1340.0
Rs. 1608.0
6. Displaying the size of a file after removing EOL characters, leading and trailing white
spaces and blank lines
size = 0
tsize = 0
while str1:
str1= myfile.readline()
print("Size of file after removing all EOL characters & blank lines:", size)
Output:
Size of file after removing all EOL characters & blank lines: 360
for i in range(count) :
rollno = int(input("Rollno:"))
name=input("Name :")
marks=float(input ("Marks:"))
rec=str(rollno)+","+name+","+str(marks)+'\n'
fileout.write(rec)
fileout.close()
Output:
How many students are there in the class?3
Enter details for student 1 below:
Rollno:12
Name :Hazel
Marks:67.75
Enter details for student 2 below:
Rollno:15
Name :Jiya
Marks:78.5
Enter details for student 3 below:
Rollno:16
Name :Noor
Marks:68.9
8. Write a program to read a text file line by line and display each word separated by a ‘#’.
line=''
while line:
line = myfile.readline()
print()
myfile.close()
Output:
Letter#'a'#is#a#wonderful#letter.#
It#is#impossible#to#think#of #a#sentence#without#it.#
We#know#this#will #never#occur.#
How#mediocre#our#world#would#be#without#this#single#most#powerful#letter.#
9. Write a program to read a text file and display the count of vowels and consonants in the
file.
ch=''
vcount=0
ccount=0
while ch:
ch = myfile.read(1)
if ch in ['a', 'A', 'e', 'E', 'i', 'I', 'o', '0', 'u', 'U']:
vcount = vcount + 1
else:
ccount = ccount + 1
myfile.close()
10. Write a program to get student data (roll no., name and marks) from user and write onto a
binary file. The program should be able to get data from the user and write onto the file as
long as the user wants.
import pickle
stu = {}
ans = 'y'
stu['Rollno']=rno
stu['Name'] = name
stu['Marks']=marks
pickle.dump(stu, stufile)
stufile.close()
Output:
Enter marks : 81
import pickle
stu={}
found=False
fin=open('Stu.dat', 'rb')
searchkeys=[12, 14]
try:
stu-pickle.load(fin)
if stu['Rollno'] in searchkeys:
print (stu)
found=True
except EOFError:
if found==False:
else:
print("Search successful.")
fin.close()
Output:
Search successful.
12. Read file stu.dat created in earlier programs and display records having marks > 81.
import pickle
stu = {}
found=False
stu pickle.load(fin)
print (stu)
found= True
if found== False
else:
print("Search successful.")
Output:
Search successful.
13. Consider the binary file Stu.dat storing student details, which you created in earlier
programs. Write a program to update the records of the file Stu.dat so that those who
have scored more than 81.0, get additional bonus marks of 2.
import pickle
stu= {}
found = False
try:
while True:
rpos = fin.tell()
stu= pickle.load(fin)
stu[ 'Marks'] += 2
fin.seek(rpos)
pickle.dump(stu, fin)
found True
except EOFError
if found == False:
else:
fin.close()
Output:
import pickle
stu = {}
try:
while True :
stu= pickle.load(fin)
print (stu)
except EOFError:
fin.close()
Output:
import pickle
stu = {}
found = False
try:
while True :
rpos = fin.tell()
stu=pickle.load(fin)
if stu['Rollno'] == 12:
stu['Name'] = 'Gurnam'
fin.seek (rpos)
pickle.dump(stu, fin)
found = True
except EOFError:
if found == False:
else:
fin.close()
Output:
import csv
fh = open("Student.csv", "w")
stuwriter.writerow(sturec)
fh.close()
Output:
Student record 1
Enter rollno:11
Enter name:Nistha
Enter marks:79
Student record 2
Enter rollno:12
Enter name: Rudy
Enter marks:89
Student record 3
Enter rollno:13
Enter name: Rustom
Enter marks:75
Student record 4
Enter rollno:14
Enter name: Gurjot
Enter marks: 89
Student record 5
Enter rollno:15
Enter name:Sadaf
Enter marks:85
17. Write a program to create a csv file by suppressing the EOL translation.
import csv
ewriter=csv.writer (fh)
empdata=[
['Empno', 'Name','Designation','Salary'],
[1002, 'Raziya','Manager',55900],
[1003, 'Simran','Analyst',35000],
ewriter.writerows(empdata)
fh.close()
Output:
18. Write a program that copies a text file "source.txt" onto "target.txt" barring the lines
starting with a "@" sign.
while True :
text = fin.readline()
if len(text) == 0:
break
continue
fout.write(text)
fin.close()
fout.close()
filter("source.txt", "target.txt")