CBSE Class 12 Computer Science Question 23 of 26

Data Structures in Python — Question 24

Back to all questions
24
Question

Question 23

Two lists Lname and Lage contain name of person and age of person respectively. A list named Lnameage is empty. Write functions as per details given below:

  1. Push_na(): It will push the tuple containing pair of name and age from Lname and Lage whose age is above 50.
  2. Pop_na(): It will remove the last pair of name and age and also print name and age of the removed person. It should also print "underflow" if there is nothing to remove.

For example, the two lists have the following data:

Lname=['Narender', 'Jaya', 'Raju', 'Ramesh', 'Amit', 'Piyush' ]
Lage= [45, 23, 59, 34, 51, 43]

After Push_na() the Lnameage Stack contains:

[('Raju', 59), ('Amit', 51)]

The output of first execution of Pop_na() is:

The name removed is Amit
The age of person is 51

Solution
def Push_na(Lname, Lage, Lnameage):
    for i in range(len(Lname)):
        if Lage[i] > 50:
            Lnameage.append((Lname[i], Lage[i]))

def Pop_na(Lnameage):
    if Lnameage:
        removed_name, removed_age = Lnameage.pop()
        print("The name removed is", removed_name)
        print("The age of person is", removed_age)
    else:
        print("Underflow - Nothing to remove")

Lname = ['Narender', 'Jaya', 'Raju', 'Ramesh', 'Amit', 'Piyush']
Lage = [45, 23, 59, 34, 51, 43]
Lnameage = []

Push_na(Lname, Lage, Lnameage)
print("After Push_na() the Lnameage Stack contains:")
print(Lnameage)

print("\nThe output of first execution of Pop_na() is:")
Pop_na(Lnameage)
Output
After Push_na() the Lnameage Stack contains:
[('Raju', 59), ('Amit', 51)]

The output of first execution of Pop_na() is:
The name removed is Amit
The age of person is 51
Answer

Answer: Push_na():, Pop_na():


Source: This question is from Data Structures in Python, Computer Science — Class 12, CBSE Board.

Key Concepts Covered

This question tests your understanding of the following concepts from the chapter Data Structures in Python: Question, Lists, Lname, Lage, Contain, Person. These are fundamental topics in Computer Science that students are expected to master as part of the CBSE Class 12 curriculum.

A thorough understanding of these concepts will help you answer similar questions confidently in your CBSE examinations. These topics are frequently tested in both objective and subjective sections of Computer Science papers. We recommend revising the relevant section of your textbook alongside practising these solved examples to build a strong foundation.

How to Approach This Question

Read the question carefully and identify what is being asked. Break down complex questions into smaller parts. Use the terminology and concepts discussed in this chapter. Structure your answer logically — begin with a definition or key statement, then provide supporting details. Review your answer to ensure it addresses all parts of the question completely.

Key Points to Remember

  • Write programs with proper indentation and comments.
  • Trace through your code with sample inputs to verify correctness.
  • Explain the logic behind each step of your solution.
  • Familiarise yourself with common library functions and methods.

Practice more questions from Data Structures in Python — Computer Science, Class 12 CBSE