[Type A] Working with Functions – Chapter 3 Sumita Arora
Table of Contents
6. Explain with a code example the usage of default arguments and keyword arguments.
Solution: Though, explicit examples are illustrated in Q5 above, a code example is given for this question as stated.
Python
def greet(name="Guest", age=30):
print("Hello,", name, "You are", age, "years old.")
greet() # Output: Hello, Guest You are 30 years old.
greet("Alice") # Output: Hello, Alice You are 30 years old.
greet(age=25, name="Bob") # Output: Hello, Bob You are 25 years old.
The code demonstrates the usage of default arguments and keyword arguments in Python functions.