1. The function

Def print_name(): print()'My name is Lao Li'Print_name () def add(x, y, z=4Print (x, y, z) # args,**kwargs def demo(x, y, z=2, *args, **kwargs):
    isum = 0
    isum = x + y + z
    for i in args:  # (3.4.5.6.7)
        isum += i
    for j in kwargs.values():  # {'a': 1.'b': 2} isum += j print(isum) # print(x) # print(y) # print(*args) #3 4 5 6 7# print(args)3.4.5.6.7Print (kwargs) # {'a': 1.'b': 2The demo (} dictionary1.2.3.4.5.6.7, a=1, b=2Def demo2(x, y, z=2Def test1(): print(*args, **kwargs): print(x)'test1')
    test2()
    print('Test2 called complete')


def test2():
    print('test2')
    test3()


def test3():
    print('test3')


if __name__ == '__main__': test1() #'Lao wang'Global variable age =12
sex = 'male'


def show():
    global age, sex  # globalDeclare age, the sex argument is the modified global variable age =100# global sex ='woman'# global variable name ='3'Print (name, age, sex)if __name__ == '__main__': show() print(name, age, sex) def fn(x, y): z = x + yreturnThe z # function endsif __name__ == '__main__':
    result = fn(1.2)
    print(result + 1Def fn1(name, age): name ="My name is % s" % name
    age = "I am %s years old." % age
    return name, age


result1 = fn1('Lao wang'.12Print (result1) #'My name is Lao Wang.'.'I'm 12 years old')
name1, age1 = fn1('Lao wang'.12Print (name1, age1) #12Recursive function #6!6*5*4*3*2*! = 720

def six(num):
    if num == 1:
        return num
    return num * six(num - 1)


result2 = six(6Print (result2) #bDef fun(a, b) def fun(a, b):return a + b


result3 = fun(1.2) print(result3) # high order function"""The map() function takes two arguments, a function and an Iterable. Map applies the passed function to each element of the sequence and returns the result as a new Iterator. (can be used with next ())"""

def f(x):
    return x * x


r = map(f, [1.2.3.4.5.6.7.8.9])
print(list(r))  # [1.4.9.16.25.36.49.64.81]


lmap = list(map(str, [1.2.3.4.5.6.7.8.9]))
print(lmap)  # ['1'.'2'.'3'.'4'.'5'.'6'.'7'.'8'.'9']


"""Reduce applies a function to a sequence [x1, x2, x3... Reduce (f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)"""
from functools import reduce

def fn_reduce(x, y):
    return x*10 + y


print(reduce(fn_reduce, [1.3.5.7.9#]))13579Def prod(L) def prod(L) def prod(L)return reduce(v_fn, L)


def v_fn(x, y):
    return x*y


print('3 * 5 * 7 * 9 =', prod([3.5.7.9]))
if prod([3.5.7.9= =])945:
    print('Test successful! ')
else:
    print('Test failed! ')

Copy the code