What is *args and **kwargs in Python?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Python basics — a prerequisite for any developer role.
Answer
*args allows a function to accept any number of positional arguments, collecting them into a tuple: def add(*args): return sum(args) — call as add(1, 2, 3, 4). **kwargs allows any number of keyword arguments, collecting them into a dict: def info(**kwargs): print(kwargs) — call as info(name="Alice", age=25). Both can be used together: def func(*args, **kwargs). Use * to unpack a sequence as positional arguments: func(*[1, 2, 3]). Use ** to unpack a dict as keyword arguments: func(**{"name": "Alice"}). These patterns are essential for writing flexible, forward-compatible APIs and when creating wrapper functions that pass arguments through to another function.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Python codebase.