Python Function Arguments

1. Positional Arguments

def display_info(name, age):
    print(f"Name: {name}, Age: {age}")

display_info("Alice", 25)

Arguments are passed in the order defined by the function.


2. Keyword Arguments

def display_info(name, age):
    print(f"Name: {name}, Age: {age}")

display_info(age=25, name="Alice")

Arguments are passed using parameter names, improving readability and flexibility.


3. Default Arguments

def greet(name="Guest"):
    print(f"Hello, {name}")

greet()
greet("Bob")

Default values are used when arguments are not supplied.


4. Required Arguments

Arguments without defaults must be provided.


5. *Variable-Length Positional Arguments (args)

*args collects extra positional arguments into a tuple.


6. **Variable-Length Keyword Arguments (kwargs)

**kwargs collects extra keyword arguments into a dictionary.


7. **Combining Normal, *args, and kwargs

Order of parameters must follow: **normal → *args → kwargs


8. Positional-Only Arguments (Python 3.8+)

Parameters before / must be positional-only.


9. Keyword-Only Arguments

Parameters after * must be specified as keyword arguments.


10. Argument Unpacking

* and ** can unpack tuples/lists and dictionaries directly into function arguments.


Last updated