How to pass a variable number of arguments to a function?

Practicing Coding Interview Questions in Python

Kirill Smirnov

Data Science Consultant, Altran

Argument types

There are two types of arguments:

 

Practicing Coding Interview Questions in Python

Argument types

There are two types of arguments:

  • positional arguments

 

Practicing Coding Interview Questions in Python

Argument types

There are two types of arguments:

  • positional arguments
  • keyword arguments

 

Practicing Coding Interview Questions in Python

Argument types

There are two types of arguments:

  • positional arguments
  • keyword arguments

 

Practicing Coding Interview Questions in Python

Positional arguments

def func_with_pos_args(arg1, arg2):

    pass
def multiply(x, y):
    return x * y
multiply(2, 3)
6
Practicing Coding Interview Questions in Python

*args

def func_with_var_pos_args(*args):

    pass
func_with_var_pos_args(1, 2, 'hello')
Practicing Coding Interview Questions in Python

*args

def func_with_var_pos_args(*args):

    print(args)
func_with_var_pos_args(1, 2, 'hello')
(1, 2, 'hello')
Practicing Coding Interview Questions in Python

*args

def func_with_var_pos_args(*args):
    for arg in args:
        print(arg)
func_with_var_pos_args(1, 2, 'hello')
1
2
'hello'
Practicing Coding Interview Questions in Python

Redefining multiply()

def multiply(*args):
    result = 1
    for arg in args:
        result = result * arg
    return result
multiply(1, 2, 3):
6
multiply(1, 2, 3, 4)
24
Practicing Coding Interview Questions in Python

Redefining multiply()

def multiply(*nums):
    result = 1
    for num in nums:
        result = result * num
    return result
multiply(1, 2, 3):
6
multiply(1, 2, 3, 4)
24
Practicing Coding Interview Questions in Python

Another use of single asterisk *

def multiply(num1, num2, num3):
    return num1 * num2 * num3
multiply(1, 2, 3)
6
Practicing Coding Interview Questions in Python

Another use of single asterisk *

def multiply(num1, num2, num3):
    return num1 * num2 * num3
nums = (2, 3, 4)
multiply(*nums)
24
nums = [2, 3]
multiply(*nums, 4)
24
Practicing Coding Interview Questions in Python

Another use of single asterisk *

def multiply(*args):
    result = 1
    for arg in args:
        result = result * arg
    return result
nums = (2, 3, 4, 5)
multiply(*nums)
120
Practicing Coding Interview Questions in Python

Argument types

There are two types of arguments:

  • positional arguments
  • keyword arguments

 

Practicing Coding Interview Questions in Python

Keyword arguments

def func_with_kwargs(arg1=1, arg2=2):
def multiply(x=1, y=2):
    print(str(x) + ' : ' + str(y))
multiply(2, 3)
2 : 3
multiply()
1 : 2
Practicing Coding Interview Questions in Python

Keyword arguments

def func_with_kwargs(arg1=1, arg2=2):
def multiply(x=1, y=2):
    print(str(x) + " : " + str(y))
multiply(y=5, x=3)
3 : 5
Practicing Coding Interview Questions in Python

**kwargs

kwargs - keyword arguments

def func_with_var_kwargs(**kwargs):
    print(kwargs)
func_with_var_kwargs(arg1=1, arg2=2, arg3=3)
{arg1: 1, arg2: 2, arg3: 3}
func_with_var_kwargs(1, arg2=2, arg3=3)
TypeError
Practicing Coding Interview Questions in Python

Redefining multiply()

def multiply_kwargs(**kwargs):
    result = 1
    for (key, value) in kwargs.items():
        print(key + ' = ' + str(value))
        result = result * value
    return result
def multiply(*args):
    result = 1
    for arg in args:
        result = result * arg
    return result
Practicing Coding Interview Questions in Python

Calling multiply_kwargs()

multiply_kwargs(num1=1, num2=2, num3=3, num4=4)
num1 = 1
num2 = 2
num3 = 3
num4 = 4
24
Practicing Coding Interview Questions in Python

Another use of double asterisk **

def multiply(num1=1, num2=2, num3=3):
    print('num1 = ' + str(num1))
    print('num2 = ' + str(num2))
    print('num3 = ' + str(num3))
    return num1 * num2 * num3
multiply()
num1 = 1
num2 = 2
num3 = 3
6
Practicing Coding Interview Questions in Python

Another use of double asterisk **

def multiply(num1=1, num2=2, num3=3):
    print('num1 = ' + str(num1))
    print('num2 = ' + str(num2))
    print('num3 = ' + str(num3))
    return num1 * num2 * num3
nums = {'num1': 10, 'num2': 20, 'num3': 30}
multiply(**nums)
num1 = 10
num2 = 20
num3 = 30
6000
Practicing Coding Interview Questions in Python

Another use of double asterisk **

def multiply(num1=1, num2=2, num3=3):
    print('num1 = ' + str(num1))
    print('num2 = ' + str(num2))
    print('num3 = ' + str(num3))
    return num1 * num2 * num3
nums = {'num1': 10, 'num3': 30}
multiply(**nums)
num1 = 10
num2 = 2
num3 = 30
600
Practicing Coding Interview Questions in Python

Another use of double asterisk **

def multiply(num1=1, num2=2, num3=3):
    print('num1 = ' + str(num1))
    print('num2 = ' + str(num2))
    print('num3 = ' + str(num3))
    return num1 * num2 * num3
nums = {'NUM10': 1, 'num2': 2, 'num3': 3}
multiply(**nums)
TypeError
Practicing Coding Interview Questions in Python

Another use of double asterisk **

def multiply_kwargs(**kwargs):
    result = 1
    for (key, value) in kwargs.items():
        print(key + ' = ' + str(value))
        result = result * value
    return result
nums = {
    'num1': 2, 'num2': 3,
    'num3': 4, 'num4': 5
}
multiply_kwargs(**nums)
num1 = 2
num2 = 3
num3 = 4
num4 = 5
120
Practicing Coding Interview Questions in Python

Argument order

def func(                                           ):
Practicing Coding Interview Questions in Python

Argument order

def func(arg1, arg2,                                ):
  • arg1, arg2 - positional arguments
Practicing Coding Interview Questions in Python

Argument order

def func(arg1, arg2, *args,                         ):
  • arg1, arg2 - positional arguments
  • *args - positional arguments of variable size
Practicing Coding Interview Questions in Python

Argument order

def func(arg1, arg2, *args, kwarg1, kwarg2,         ):
  • arg1, arg2 - positional arguments
  • *args - positional arguments of variable size
  • kwarg1, kwarg2 - keyword arguments
Practicing Coding Interview Questions in Python

Argument order

def func(arg1, arg2, *args, kwarg1, kwarg2, **kwargs):
  • arg1, arg2 - positional arguments
  • *args - positional arguments of variable size
  • kwarg1, kwarg2 - keyword arguments
  • **kwargs - keyword arguments of variable size
def func(arg1, arg2, *args):

def func(arg1, arg2, **kwargs):
def func(*args, **kwargs):
Practicing Coding Interview Questions in Python

Let's practice!

Practicing Coding Interview Questions in Python

Preparing Video For Download...