Function with Default Args
Problem Statement
Create functions with default arguments and keyword arguments.
Background
Python functions support:
- Default arguments: def f(a, b=10)
- Keyword arguments: f(a=1, b=2)
- *args: Variable positional arguments
- **kwargs: Variable keyword arguments
Your Task
Write a function **create_profile(name, age=18, city="Unknown", extra) that creates a user profile.
Output Format
Return a dictionary with all provided information:
- "name": The name
- "age": The age (default 18)
- "city": The city (default "Unknown")
- Plus any extra keyword arguments
Example:
create_profile("Alice", age=25, city="NYC", job="Engineer"){'name': 'Alice', 'age': 25, 'city': 'NYC', 'job': 'Engineer'}Named args override defaults, **kwargs captures extra args
Constraints:
- name is required
- age defaults to 18
- city defaults to "Unknown"
- Include any extra keyword arguments
Background Knowledge
Python functions are flexible in how they accept arguments, allowing you to write more intuitive and maintainable code. Default arguments let you specify fallback values for parameters that aren't always provided by the caller—when a function is called without passing a value for a parameter with a default, Python uses the default instead. This reduces the need for multiple function overloads or conditional logic inside the function.
Keyword arguments allow callers to pass arguments by name rather than position, making function calls more readable and less error-prone. The **kwargs syntax (double asterisk) is a special parameter that captures any additional keyword arguments passed to the function as a dictionary. This is powerful for creating flexible APIs where you don't know all possible parameters in advance. When you use **kwargs, any extra keyword arguments are collected into a dictionary that you can iterate over, merge with other data, or pass along to other functions.
Understanding these features together enables you to write functions that are both simple to use (with sensible defaults) and extensible (accepting arbitrary additional parameters). This pattern is common in real-world Python libraries and frameworks where APIs need to balance ease of use with flexibility.
Algorithm/Approach
The general approach is straightforward:
- Define the function signature with explicit parameters (name, age, city) that have appropriate defaults, followed by **extra to capture any additional keyword arguments
- Collect all the data into a single dictionary that includes both the explicit parameters and the extra keyword arguments
- Return the combined dictionary containing all provided information
The key insight is that **kwargs automatically gives you a dictionary of the extra arguments, so you just need to combine it with your explicit parameters.
Step-by-Step Strategy
-
Create the function definition with the signature create_profile(name, age=18, city="Unknown", **extra). Note that name has no default (it's required), while age and city have defaults.
-
Build a dictionary starting with the explicit parameters. You can do this by creating a dictionary literal with the three main keys and their values.
-
Merge the extra arguments into your dictionary. Since extra is already a dictionary (containing all the **kwargs), you can use dictionary update methods or unpacking to combine them.
-
Return the complete dictionary containing all keys and values.
Common Pitfalls
- Parameter order matters: Required parameters (without defaults) must come before parameters with defaults. **kwargs must always come last.
- **Confusing *args with kwargs: Single asterisk (*args) captures positional arguments as a tuple; double asterisk (**kwargs) captures keyword arguments as a dictionary. This problem only uses **kwargs.
- **Forgetting that kwargs is a dictionary: Remember that extra is already a dictionary, so you don't need to convert it—just merge it with your other data.
- Mutating the wrong dictionary: If you're not careful with how you combine dictionaries, you might accidentally modify the extra dictionary or lose data.
Time & Space Complexity
- Time Complexity: O(n) where n is the total number of keyword arguments (explicit parameters plus extras). Dictionary creation and merging are linear in the number of key-value pairs.
- Space Complexity: O(n) for storing the output dictionary, which contains all the provided information.