⊗pyPmFnPC 14 of 128 menu

Combining Parameters in Python

You can pass both named and positional parameters to the same function. This is done using the symbol *, which means that the parameters specified to the right of it should be considered named if their default values ​​are not to be used.

Let's create a function that will output all parameters to the console. The variable data will denote a regular positional parameter. Then, after the symbol *, two variables start and end are set with default values. However, when calling the function, we will make these variables named:

def func(data, *, start=0, end=100): print(data, start, end) func(1, start=2, end=3)

Result of code execution:

1 2 3

If you remove the default value of the variable start and at the same time remove it as a named parameter, an error will be displayed. This happens because the symbol * has already defined start as a named variable:

def func(data, *, start, end=100): print(data, start, end) func(1, 2, end=3) # will display an error

What will be the result of running the following code:

def func(num1, num2, *, num3): return (num1 + num2) * num3 print(func(2, 4, num3=3))

What will be the result of running the following code:

def func(num1, *, num2, num3): return (num1 - num2) / num3 print(func(12, 4, num3=5))

What will be the result of running the following code:

def func(*, name='user1', age='18'): return 'Username is ' + name + ' age is ' + age print(func(name='john'))
enru