Page 305 - CTS - CSA TP - Volume 2
P. 305
COMPUTER SOFTWARE APPLICATION - CITS
EXERCISE 134 : Write a python program depicting
argument passing and using tuples,
dictionaries as arguments
Objectives
At the end of this exercise you shall be able to
• develop python programs depicting argument passing and using tuples, dictionaries as arguments.
Procedure
TASK 1: Argument Passing with Tuples
Code:
def display_info(*args):
for arg in args:
print(arg)
# Example usage
display_info(“John”, 25, “USA”, “Software Engineer”)
Explanation:
• The function display_info accepts variable-length positional arguments using *args.
• It prints each argument passed to the function.
Output:
TASK 2: Argument Passing with Dictionaries
Code:
def display_user_info(**kwargs):
for key, value in kwargs.items():
print(f”{key}: {value}”)
# Example usage
display_user_info(name=”Alice”, age=30, occupation=”Data Scientist”, country=”Canada”)
Explanation:
• The function display_user_info accepts variable-length keyword arguments using **kwargs.
• It prints each key-value pair passed to the function.
Output:
290