Junior — Middle
What methods can be used to pass parameters to functions and which ones do you know?
sobes.tech AI
Answer from AI
In Python, function parameters can be passed in several ways:
- Positional parameters — passed in the order they are declared.
- Named parameters (keyword arguments) — passed with the parameter name, order does not matter.
- Default parameters — parameters with default values, used if no argument is provided.
- *Variable-length arguments (args) — allow passing an arbitrary number of positional arguments.
- **Variable-length named arguments (kwargs) — allow passing an arbitrary number of named arguments.
Example:
def example(a, b=2, *args, **kwargs):
print(f'a={a}, b={b}')
print(f'args={args}')
print(f'kwargs={kwargs}')
example(1, 3, 4, 5, x=10, y=20)
Output:
a=1, b=3
args=(4, 5)
kwargs={'x': 10, 'y': 20}