Q. Write the code as per following specifications. You may use for loops.
(a) Write a loop that prints each inner tuple from tuple pets on a separate line.
(b) Write a loop that prints the second element of each inner tuple in tuple pets on a separate line.
(c) Write a loop that examines tuple pets and computes the number of dogs in the tuple.
(d) Write a loop that examines tuple pets and computes the sum of the ages of the animals in the tuple. Ages are the third element of the inner tuples.
(e) Write a program that inputs a tuple T and prints a tuple of the lengths subtuples.
((1, 2), (2, 4, 6), (4,), (5, 0, 5))
Then it should prints (2, 3, 1, 3)
(f) Write a program that inputs a tuple with each element of it as another tuple. It then prints the elements of the tuple in increasing order of their lengths.
Answer :-
(a)
tup = eval(input("Enter a tuple pets :- "))
for i in tup :
print (i)
(b)
tup = eval(input("Enter a tuple pets :- "))
for i in tup :
print (i[1])
(c)
tup = eval(input("Enter a tuple pets :- "))
count = 0
for i in tup :
if i[1] == "Dog" or i[1] == "dog" :
count += 1
print ("Number of Dogs :- ", count )
(d)
tup = eval(input("Enter a tuple pets :- "))
sum = 0
for i in tup :
sum += i[2]
print ("Sum of ages :- ", sum)
(e)
tup = eval(input("Enter a tuple :- "))
newtup = ()
for i in tup :
newtup += (len(i),)
print (newtup)
(f)
tup = eval(input("Enter a nested tuple :-"))
newtup = [ ]
for i in tup :
newtup += [ i ]
for i in range(len(newtup)):
for j in range( len( newtup ) - 1 ):
if len(newtup [ j ] ) > len(newtup [ j + 1 ]) :
newtup [ j ] , newtup [ j + 1 ] = newtup [ j + 1 ] , newtup [ j ]
print(newtup)
Output :-
(a)
(b)
(c)
(d)
(e)
(f)
bhai can i get the video for this sum
ReplyDeleteOk . I will make a video.
Deleteok bhai������
ReplyDelete: )
DeleteThanks for this coding
ReplyDeleteWelcome : )
DeleteCan you hepl me ?I am not getting the correct answer of e)part.
ReplyDeleteCopy the above code carefully and run it .
Deletesame here...... part e program is incorrect.....
DeleteTypeError: object of type 'int' has no len()
this is what it is showing when i run it
i just changed tup by t1 and newtup by t2 . please correct .
thanks
Please check again and run this code -
Deletetup = eval(input("Enter a tuple :- "))
newtup = ()
for i in tup :
newtup += (len(i),)
print (newtup)
pls give code of (f) question
ReplyDeletePlease check again.
DeleteOne of Best 🤩 website of Python..
ReplyDeleteI have Got..
Thank you so much
Path walla
Welcome : )
Deletepath walla where can i contact you for contributing to this website
ReplyDeleteYou can contact me on telegram.
Deleteplease explain the codes by giving examples
ReplyDeleteplease upload it as soon as possible
DeleteOk, I have uploaded it.
Deleteprogram f is giving error that int object has no length please give a solution
ReplyDeletePlease write int as ( integer ,)
DeleteExample :- ( (5,) , (3 ,) )
it's working thank you
ReplyDeleteWelcome : )
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )