Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Python Final Assignment Imp

The document contains 14 programming problems related to Python. The problems cover topics like string manipulation, file handling, dictionaries, lists, tuples, classes and object-oriented programming concepts. Some key problems include writing a program to count vowels, consonants and other characters in a string; storing student records in a dictionary; converting a list of tuples to a list of lists; and generating a bill by calculating total amount from user input quantities.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Python Final Assignment Imp

The document contains 14 programming problems related to Python. The problems cover topics like string manipulation, file handling, dictionaries, lists, tuples, classes and object-oriented programming concepts. Some key problems include writing a program to count vowels, consonants and other characters in a string; storing student records in a dictionary; converting a list of tuples to a list of lists; and generating a bill by calculating total amount from user input quantities.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Python Assignment

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

5.Write a function display_words() in python to read lines from a text file


“article.txt”, and display those words, which are less than 6 characters.
def display_words():
c=0
file=open(“article.txt”,”r”)
line=file.read()
word=line.split()
for w in word:
if len(w)<6:
print(w)
file.close()
display_words()
6.Write a function in python to count the number of lines from a text file
"article.txt" which is not starting with an alphabet "T".

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))

Print(“Original tuple”, T1)


L1=list(T1)
L2=[]
for I in L1:
Result=sorted(i)
L2.append(tuple(result))
T2=tuple(L2)
Print(“Resultant tuple”, T2)

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)

x=int(input(“enter the number”))


print(“factor of”,x,”is:”)
b=[]
for I in range(1,x+1):
if x%i==0:
b.append(i)
print(tuple(b))

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

12.Write a Python program to scramble the letters of string in a given list.


Original list: [‘Python’, ‘list’, ‘exercises’, ‘practice’, ‘solution’]
After scrambling the letters of the strings of the said list:
[‘tnPhyo’, ‘tlis’, ‘ecrsseiex’, ‘ccpitear’, ‘noiltuos’]
from random import shuffle
def shuffle_word(text_list):
text_list = list(text_list)
shuffle(text_list)
return ‘’.join(text_list)
text_list = [‘Python’, ‘list’, ‘exercises’, ‘practice’, ‘solution’]
print(“Original list:”)
print(text_list)
print(“\nAfter scrambling the letters of the strings of the said list:”)
result = [shuffle_word(word) for word in text_list]
print(result)
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

You might also like