First understanding of functions of python notes

'''
function definition (declaration)
Functions start with the keyword def, function name, parameters: carriage return indentation
def function(arg1,arg2,...):
    pass

A function is an idea of ​​encapsulation, combining small functions or
The function that can be minified is encapsulated into a function

function call:
The function name can be directly passed in the parameters
function(1,2,3)
arg1,arg2, formal parameters
1,2,3 Arguments

def fun(*args, **kwargs):
    pass
*args stands for list
**kwargs stands for dict

fun(1,2,3,4,5,a=1,b=2)
a =
args = [2,3,4,5] list one-to-one correspondence
kwargs = {"a":1,"b":2} dict one-to-one correspondence

anonymous function
lambda

'''

def jc(n): # define the function
    if n == 0:
        return 1
    else:
        result = 1
        for i in range(1,n+1):
            result *= i
        return result

def main():
    n = 3
    count = 0
    for i in range(0,n+1):
        count += jc(i) # call other functions
    print("count = {0}".format(count))

def add(x,y):
    return x+y
#######   <<<<<<<====等价===>>>>>>>>   #####
add = lambda x,y:x+y


if __name__ == '__main__':
    main()

Guess you like

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