- Description: Formal arguments are the parameters listed in the function definition that define how inputs are passed into the function.
- Positional arguments: It refers to how actual arguments are matched with formal arguments in a function based on their order of appearance.
- Keyword arguments: It passing arguments to a function using their names (keywords) instead of relying solely on their position in the function call.
- Variable-length arguments: It allows functions to accept an arbitrary number of arguments, which can be either positional or keyword arguments.
- Arbitrary Positional Arguments(*args): Allows a function to accept any number of positional arguments.These arguments are collected into a tuple.
- Arbitrary Keyword Arguments(**kwargs): Allows a function to accept any number of keyword arguments.These arguments are collected into a dictionary.
- #Positional arguments
print("Output for positional arguments")
def sub(x,y):
return x-y
result = sub(10,5)
print(result)
print()
#Keyword arguments
print("Output for keyword arguments")
def greet(name, greeting="Hello"):
print(f"{greeting}, {name}!")
result2 = greet(name="Praveen",greeting="Hi")
print(result2)
print()
#Variable-length arguments
# Using *args
print("Output for *args")
def sum_numbers(*args):
print(args)
return sum(args)
result3 = sum_numbers(1, 2, 3, 4, 5)
print(result3)
print()
#Using **Kwargs
print("Output for **kwargs")
def display_details(**kwargs):
print(kwargs)
for key, value in kwargs.items():
print(f"{key}: {value}")
display_details(name="Kumar", age=25, city="Mumbai")