Create a normal function

If a function doesn’t return any value, it returns None.

# without arguments
def func_name():
  pass

# with arguments
def func_name(<args>):
  pass

# return
def func_name(<args>):
  return <some_thing>

# call a function
func_name(<args>)

Unpacking a function

def sum_and_div(num1, num2):
  sum_nums = num1 + num2
  div_nums = num1 / num2
  return sum_nums, div_nums # multiple returns
  
sum, div = sum_and_div(18, 9)
print(sum, div)
27 2.0

Functions with stars (*args and **kwargs)

[ref]The *args will give you all function parameters as a tuple:

def foo(*args):
  print(args)
  for a in args:
    print(a)

foo(1)
foo(2, 3, 4)
(1,)
1
(2, 3, 4)
2
3
4
def foo(rarg1, rarg2):
  print(rarg1, rarg2)

lst = [1, 2]
foo(*lst)
tpl = (3, 4)
foo(*tpl)
1 2
3 4

If you wanna use “keywords arguments”, use **args:

def kwfunc(**kwargs):
  print(type(kwargs))
  print(kwargs)

kwfunc()
kwfunc(kw1="thi", kw2="dinh")
<class 'dict'>
{}
<class 'dict'>
{'kw1': 'thi', 'kw2': 'dinh'}

Use a dictionary as an input,

def kwfunc(**kwargs): # must have **
  print(kwargs)

kwargs = {'kw1': "thi", 'kw2': "dinh"}
kwfunc(**kwargs) # must have **
{'kw1': 'thi', 'kw2': 'dinh'}
def kwfunc(kw1="john", kw2="doe"):
  print(kw1, kw2)

kwargs = {'kw1': "thi", 'kw2': "dinh"}

kwfunc()
kwfunc(kwargs) # goes to kw1
kwfunc(**kwargs) # goes to both kw1 & kw2
john doe
{'kw1': 'thi', 'kw2': 'dinh'} doe
thi dinh

Coupling rargs, *args and **kwargs:

  • Required positional arguments: rarg1, rarg2, …
  • Optional positional arguments: *args.
  • Optional key-values arguments: **kwargs.
def kwfunc(rarg1=0, rarg2=0, *args, **kwargs):
  print("required args: ", rarg1, rarg2)
  if args:
    print("*args: ", args)
  if kwargs:
    print("**kwargs: ", kwargs)
  print("\n")


kwfunc()
kwfunc(1, 2)
kwfunc(3, 4, 5, 6)
kwfunc(kw1="thi", kw2="dinh")
required args:  0 0

required args:  1 2

required args:  3 4
*args:  (5, 6)

required args:  0 0
**kwargs:  {'kw1': 'thi', 'kw2': 'dinh'}

All arguments after * must be key-value arguments,

def func(rarg1, rarg2, *, kwarg1, kwarg2):
  print("required args: ", rarg1, rarg2)
  print("kwarg*: ", kwarg1, kwarg2)

# func(1, 2, 3, 4) # error!
func(1, 2, kwarg1=3, kwarg2=4)
required args:  1 2
kwarg*:  3 4

Lambda function

It’s convenient but don’t use it regularly, use def (in 1 line) instead.

x = lambda a : a + 10
print(x(5))

# you can use this
def x(a): return a + 10
15

Notice an error?

Everything on this site is published on Github. Just summit a suggested change or email me directly (don't forget to include the URL containing the bug), I will fix it.