Sobes.tech
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:

  1. Positional parameters — passed in the order they are declared.
  2. Named parameters (keyword arguments) — passed with the parameter name, order does not matter.
  3. Default parameters — parameters with default values, used if no argument is provided.
  4. *Variable-length arguments (args) — allow passing an arbitrary number of positional arguments.
  5. **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}