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

COMPUTER SOFTWARE APPLICATION - CITS




                         **additional_info: Additional information (packed into a dictionary).

                     “””
                     print(“Name:”, name)
                     print(“Age:”, age)
                     print(“Additional Information:”, additional_info)
                 # Example usage:

                 print_person_info(“Alice”, 30, occupation=”Software Engineer”, city=”Wonderland”)
           Explanations :
           •  The function print_person_info is defined to accept two required positional arguments (name and age) and any
              number of additional keyword arguments. The double-asterisk ** before additional_info allows the function to
              receive these additional keyword arguments and pack them into a dictionary called additional_info.
           •  Inside the function, it prints the name and age of the person using the provided positional arguments (name
              and age).
           •  It then prints the additional information provided as keyword arguments, which are packed into the additional_
              info dictionary.

           •  The print_person_info function can handle different sets of additional information for a person, and it will print
              all the provided details.
           •  In the example usage, the function is called with the name “Alice”, age 30, and additional information about her
              occupation and city.
           Output:













           Related Exercises:
           1  Program 1: Tuple as Argument
              •  Write a Python program that defines a function to calculate the average of numbers passed as a tuple.
           2  Program 2: Dictionary as Argument
              •  Create a program that defines a function to display the details of a person. The function should take a
                 dictionary with keys like ‘name’, ‘age’, and ‘city’ as arguments.
           3  Program 3: Multiple Arguments and Default Values
              •  Write a program with a function that accepts multiple arguments, including a tuple and a dictionary. Set
                 default values for some parameters.


















                                                           269
                              CITS : IT & ITES - Computer Software Application - Exercise 129
   279   280   281   282   283   284   285   286   287   288   289