Python Final Assignment Imp
Python Final Assignment Imp
1.) Write a program using dictionary that accepts a string and change its
lowercase vowels to special character as given below:
a#e
@i$
o %
u!
print("Enter the String: ")
str = input().lower()
newstr = ""
for i in range(len(str)):
i str[i]=='a':
newstr = newstr + '#'
elif str[i]=='e':
newstr = newstr + '@'
elif str[i]=='i':
newstr = newstr + '$'
elif str[i]=='o':
newstr = newstr + '%'
elif str[i]=='u':
newstr = newstr + '!'
else:
newstr = newstr + str[i] print("\
nOriginal String:", str)
print("New String:", newstr)
OUTPUT
2.) Write a program that reads two strings from keyboard and prints the
common words. Your program should convert input string to lower case.
class Solution:
def solve(self, s0, s1):
s0 = s0.lower()
s1 = s1.lower()
s0List = s0.split(" ")
s1List = s1.split(" ")
return set(s0List)&set(s1List)
ob = Solution()
S = input("enter the first string:")
T= input("enter the second string:")
print("Common words from 2 strings:", ob.solve(S,T))
OUTPUT
3.) Write a program that keeps student's name and his marks in a dictionary
as key-value pairs. The program should store records of 10 students and
display students name and marks of five students in decreasing order of
marks obtained.
d={}
def arrange(m1,m2,m3,m4,m5):
l=[m1,m2,m3,m4,m5]
l.sort(reverse=True)
m1,m2,m3,m4,m5=l
d[‘marks1’]=m1
d[‘marks2’]=m2
d[‘marks3’]=m3
d[‘marks4’]=m4
d[‘marks5’]=m5
print(d)
for I in range(10):
print(“Enter the name of students”,i+1)
n=input()
d[‘name’]=n
m1=int(input(“marks of subject 1:”))
m2=int(input(“marks of subject 2:”))
m3=int(input(“marks of subject 3:”))
m4=int(input(“marks of subject 4:”))
m5=int(input(“marks of subject 5:”))
arrange(m1,m2,m3,m4,m5)
OUTPUT
4.In a text editing software , after saving the article as WORDS.TXT, it is
realised that alphabet I is wrongly typed as alphabet J everywhere in the
article. write a function definition for convert() in Python that would display the
corrected version of entire content of the file WORDS.TXT with all the
alphabets "J" to be displayed as an alphabet "I" on screen.
def JTOI():
file = open("word.txt","r")
data = file.read()
for letter in data:
if letter == 'j':
print("i",end="")
else:
print(letter,end="")
file.close()
JTOI()
OUTPUT
def line_count():
file=open(“article.txt”,”r”)
count=0
for line in file:
if line[0] not in “T”:
count+=1
file .close()
print(“number of lines starting with T are:”,count)
line_count()
OUTPUT
7. Write a program to arrange all the sub tuples of the tuples in increasing order
and store
them in another tuple.
T1=((1,2,3),(94,23),(67,95,13)) # original tuple
T2=((1,2,3),(23,94),(13,67,95)) #new tuple
T1 =((7,1,2), (9,5,10), (15,8,7,8))
OUTPUT
8.Write a program to accept a number from the user and create a tuple
containing all
factors of a number.
Eg: Enter a number: 9
T1=(1,3,9)
OUTPUT
9. Write a Python program to compute the sum of all the elements of each tuple
stored
inside a list of tuples.
Original list of tuples:
[(1, 2), (2, 3), (3, 4)]
def test(lst):
result=map(sum,lst)
return list(result)
nums=[(1,2),(2,3),(3,4)]
print(“original list of tuples is:”)
print(nums)
print(“sum of all elements of each tuple is”)
print(test(nums))
OUTPUT
10. Write a Python program to convert a given list of tuples to a list of lists.
Original list of tuples: [(1, 2), (2, 3, 5), (3, 4), (2, 3, 4, 2)]
Convert the said list of tuples to a list of lists: [[1, 2], [2, 3, 5], [3, 4], [2, 3, 4, 2]]
def test(lst_tuples):
result=[list(i) for I in lst_tuples]
return result
lst_tuples=[(1,2),(2,3,5),(3,4),(2,3,4,2)]
print(“original list of tuples:”)
print(lst_tuples)
print(“converted to list of lists:”)
print(test(lst_tuples))
OUTPUT
11. Write a Python program to replace the last element in a list with another list.
Sample data : [1, 3, 5, 7, 9, 10], [2, 4, 6, 8]
Expected Output: [1, 3, 5, 7, 9, 2, 4, 6, 8]
num1 = [1, 3, 5, 7, 9, 10]
num2 = [2, 4, 6, 8]
num1[-1:] = num2
print(nunum
OUTPUT
13 . Write a program that has class store which keeps a record of code and
price of each product. Display a menu of all products to the user and prompt
him to enter the quantity of each item required. Generate a bill and display the
total amount.
Class store:
Item_code = [ ]
price= [ ]
def get_data(self):
for I in range(5):
self.item_code.append(int(input(“Enter the code of item”)))
self.price.append(int(input(“Enter the price”)))
def display_data(self):
print(“item_code \t price”)
for i in range(5):
print(self.item_code[i],”\t\t”,self.price[i])
def calculate_bill(self,quant):
total_amount=0
for i in range(5):
total_amount = total_amount+self.price[i]*quant[i]
print(“*********generate bill*********”)
print(“item \t price \t quantity \t subtotal”)
for i in range(5):
print(self.item_code[i],”\t” ,self.price[i],”\t”,quant[i],”\t”,quant[i]*self.price[i])
print(“*****”)
print(“total = :”, total_amount)
s=store()
s.get_data()
s.display_data()
q=[]
print(“enter the quantity of each item:”)
for i in range(5):
q.append(int(input()))
s.calculate_bill(q)
OUTPUT
14. Write a class that stores a string and all its status details such as number of
uppercase , characters, vowels, consonants, spaces, etc.
class string:
def __init__(self):
self.vowels=0
self.spaces=0
self.consonants=0
self.uppercase=0
self.lowercase=0
self.string=str(input(“enter the string”))
def count_uppercase(self):
for letter in self.string:
if(letter.isupper()):
self.uppercase+=1
def count_lowercase(self):
for letter in self.string:
if(letter.islower()):
self.lowercase+=1
def count_vowels(self):
for letter in self.string:
if(letter in (‘a’,’e’,'i',’o’,’u’)):
self.vowels+=1
elif(letter in (‘A’,’E’,’I’,’O’,’U’)):
self.vowels+=1
def count_spaces(self):
for letter in self.string:
if(letter == ' ’):
self.space+=1
def count_consonants(self):
for letter in self.string:
if( letter not in (‘a’,’e’,'i',’o’,’u’,’A’,’E’,’I’,’O’,’U’, ‘’)):
self.consonants+=1
def compute_stat(self):
self.count_uppercase()
self.count_lowercase()
self.count_vowels()
self.count_spaces()
self.count_consonants()
def show_stat(self):
print(“vowels:%d”%self.vowels)
print(“consonants:%d”%self.consonants)
print(“spaces:%d”%self.spaces)
print(“uppercase:%d”%self.uppercase)
print(“lowercase:%d”%self.lowercase)
s=String()
s.compute_stat()
s.show_stat()
OUTPUT