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.

For example, if passed tuple T is
((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)

Enter a tuple pets :- ( ( "Tony", "Dog", 6 ), ( "Capy", "Cat", 4),( "MAX", "Dog",7) )
('Tony', 'Dog', 6)
('Capy', 'Cat', 4)
('MAX', 'Dog', 7)
>>>

(b)

Enter a tuple pets :- ( ( "Tony", "Dog", 6 ), ( "Capy", "Cat", 4),( "MAX", "Dog",7) )
Dog
Cat
Dog
>>>

(c)

Enter a tuple pets :- ( ( "Tony", "Dog", 6 ), ( "Capy", "Cat", 4),( "MAX", "Dog",7) )
Number of Dogs :-  2
>>>

(d)

Enter a tuple pets :- ( ( "Tony", "Dog", 6 ), ( "Capy", "Cat", 4),( "MAX", "Dog",7) )
Sum of ages :-  17
>>>

(e)

Enter a tuple :- ((1, 2), (2, 4, 6), (4,), (5, 0, 5))
(2, 3, 1, 3)
>>>

Enter a tuple :- ( (2,6,8,7),(9,7,5),(4,5,9,7,2),(5,) )
(4, 3, 5, 1)
>>>

(f)

Enter a nested tuple :-((1, 2), (2, 4, 6), (4,), (5, 0, 5))
[(4,), (1, 2), (2, 4, 6), (5, 0, 5)]
>>>

Enter a nested tuple :-( (2,6,8,7),(9,7,5),(4,5,9,7,2),(5,) )
[(5,), (9, 7, 5), (2, 6, 8, 7), (4, 5, 9, 7, 2)]
>>>


23 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. bhai can i get the video for this sum

    ReplyDelete
  2. ok bhai������

    ReplyDelete
  3. Can you hepl me ?I am not getting the correct answer of e)part.

    ReplyDelete
    Replies
    1. Copy the above code carefully and run it .

      Delete
    2. same here...... part e program is incorrect.....
      TypeError: 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

      Delete
    3. Please check again and run this code -

      tup = eval(input("Enter a tuple :- "))
      newtup = ()
      for i in tup :
      newtup += (len(i),)
      print (newtup)

      Delete
  4. One of Best 🤩 website of Python..
    I have Got..
    Thank you so much
    Path walla

    ReplyDelete
  5. path walla where can i contact you for contributing to this website

    ReplyDelete
  6. please explain the codes by giving examples

    ReplyDelete
  7. program f is giving error that int object has no length please give a solution

    ReplyDelete
    Replies
    1. Please write int as ( integer ,)
      Example :- ( (5,) , (3 ,) )

      Delete
  8. it's working thank you

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post