Page 306 - CTS - CSA TP - Volume 2
P. 306

COMPUTER SOFTWARE APPLICATION - CITS



















           TASK 3: Combined Use of Tuples and Dictionaries
           Code:

           def display_person_info(name, age, **additional_info):
           print(“Name:”, name)
           print(“Age:”, age)
           for key, value in additional_info.items():
           print(f”{key}: {value}”)

           # Example usage
           display_person_info(“Bob”, 28, occupation=”Software Developer”, city=”New York”)
           Explanation:
           •  The function display_person_info has both positional and keyword arguments.

           •  It prints the name and age as positional arguments and any additional information as keyword arguments.
              These examples showcase different ways to use tuples and dictionaries for argument passing in Python
              functions.
           Output:













           Related Exercises:
           Exercise 1: Tuples
           1  Write a Python function that takes a tuple of numbers as input and returns their sum.

           2  Create a program that prompts the user to enter values and stores them in a tuple. Display the tuple.
           Exercise 2: Dictionaries
           1  Implement a function that accepts a dictionary of student names and their corresponding grades. Print each
              student’s name and grade.
           2  Write a program to input key-value pairs from the user and store them in a dictionary. Display the dictionary.
           Exercise 3: Combined Use
           1  Define a function that takes a person’s name and age as positional arguments and any additional information
              as keyword arguments. Display the name, age, and additional information.
           2  Create a program that reads information about books (title, author, year) from the user and stores it in a
              dictionary. Display the dictionary.



                                                           291
                              CITS : IT & ITES - Computer Software Application - Exercise 134
   301   302   303   304   305   306   307   308   309   310   311