The use of functions of python notes

'''
function keyword
def define function
return return value
pass filter
exit(1) exit directly

function arguments
*args tuple parameter, corresponding to assignment
**kwargs dict parameter, corresponding to assignment
fun(*args,**kwargs)
fun(1,2,3,4,5,a=10,b=40)

args = (1,2,3,4,5)
kwargs = dict(a=10,b=40)

anonymous function
add = lambda x,y:x+y

'''

# add1 prints the result directly
def add1(x, y):
    print(x + y)

# add2 returns the result
def add2(x, y):
    return x + y

# The pass in hello1 is to ignore the current line and enter the next line
def hello1():
    pass
    print("hell1")

# exit in hello2 will exit the function that is not executed
def hello2():
    exit(1)
    print("hell2")


def test(m, *args, **kwargs):
    print("m = {0}".format(m))
    print("args = {0}".format(args)) # a list of lists
    print("kwargs = {0}".format(kwargs)) # a dict dictionary

# function call
test(10, args=(1,11,12))
test(10, (1,11,12))
test(10, 1,11,12)

add1(1, 3)
re = add2(4, 5)
print(re)
hello1()
hello2()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324483257&siteId=291194637