# - Must parameters, keyword parameters, default parameters - - -
1 def user_info(name,age,sex="male"):
2 print("Nmae: %s"% name)
3 print("Age: %d" % age)
4 print("Sex: %s" % sex)
5
6 #Necessary parameters('dream',18)
7 user_info('dream',18) #Nmae: dream Age: 18 Sex: male
8 #Keyword parameters(age=10,name='rise')
9 user_info(age=10,name='rise') #Nmae: rise Age: 10 Sex: male
10 #Default parameters(sex="male")
11 user_info("xiaohong",12,"madam") #Nmae: xiaohong Age: 12 Sex: madam
# The sum() method is used to sum the series.
Syntax sum(iterable[, start])
parameter
1. iterable -- iteratable objects, such as lists, tuples, and collections.
2. start -- Specifies the added parameter. If this value is not set, the default is 0.
# The use of indefinite length parameters (* args,**kwargs) - -
# --- adder
1 def add(*args):
2 print(args,type(args)) #((1, 2, 3, 4, 5) <class 'tuple'> #args is an iterative object (tuple)
3 sum1 = sum(args)
4 print(sum1)
5 add(1,2,3,4,5)
6
7 def print_info(name,age,**kwargs):
8 print("Nmae: %s"% name)
9 print("Age: %d" % age)
10 for i in kwargs:
11 print("%s:%s"% (i,kwargs[i]))
12
13 print_info("xiaohong",12,sex="madam",job="IT")
14
15 #CONCLUSION: Regarding the location of indefinite length parameters:*args Put it on the left.**kargs Put the parameters on the right
16
17 def print_info(name,age,sex = "male",*args,**kwargs):
18 print("Nmae: %s"% name)
19 print("Age: %d" % age)
20 print("Sex: %s" % sex)
21 for i in args:
22 print(i)
23 for j in kwargs:
24 print("%s:%s"% (j,kwargs[j]))
25
26 print_info("xiaohong",12,"madam",1,2,job="IT",learn="python")
#-----return-----
Role of return
1. End function
2. Return an object
# - Attention:
1. If there is no return in the function, a None will be returned by default.
2. If multiple objects are returned, python will help us encapsulate multiple objects into a tuple (returning an object)
1 def f():
2 return 1
3 print(f()) #return Return to 1 ( return Return to caller)
4
5 def add(*args):
6 print(args,type(args)) #((1, 2, 3, 4, 5) <class 'tuple'> #args is an iterative object (tuple)
7 return sum(args)
8 print(add(1,2,3,4,5))
# Scope
# Scope:
1. L: local, local scope, i.e. variables defined in functions
2. E: enclosing, the local scope of the nested parent function, that is, the local scope of the upper function containing the function, but not the global scope.
3. G: globa global variables, variables defined at the module level.
4. Variables in Fixed Module of B: Buil-in System
Scope priority: local scope > external scope > global scope in current module > Python built-in scope
1 count = 10
2 def outer():
3
4 # print(count) #local variable 'count' referenced before assignment
5 global count #Global variable usage global Keyword
6 print(count) #10
7 count = 5
8 print(count) #5
9 outer()
10
11 def outer():
12 count = 10
13 def inner():
14 nonlocal count #Use of nested scoped variables nonlocal Keyword
15 print(count) #10
16 count = 20
17 print(count) #20
18 inner()
19 outer()
# Higher order functions
# 1. Function names can be assigned
# 2. Function names can be used as parameters, and function names can also be used as return values of functions.
1 def f(n):
2 return n*n
3 def foo(a,b,f):
4 return f(a)+f(b)
5 print(foo(2,3,f)) #13
6
7 def a():
8 def b():
9 return 6
10 return b
11 ret = a()
12 print(ret()) # 6
# Recursive function
# Definition: Within a function, other functions can be called. If a function calls itself (itself) internally, the function is recursive.
# --- factorial examples
1 def num1(n):
2 ret = 1
3 for i in range(1,n+1):
4 ret = ret * i
5 return ret
6 print(num1(4))
7
8 def num2(n):
9 if n == 1:
10 return 1
11 return n*num2(n-1)
12 print(num2(4))
# - Fibonacci sequence 1 12 3 5 8 13
1 def fibo(n):
2 if n <= 1:
3 return n
4 return fibo(n-1)+fibo(n-2)
5
6 print(fibo(8))
7
8 def fi(n):
9 before = 0
10 after = 1
11 for i in range(n-1):
12 before,after = after,before+after
13 return after
14 print(fi(6))
# Built-in functions
# - filter filter filter
1 str = ['a','b','c']
2 def fun1(s):
3 if s != 'a':
4 return s
5
6 ret = filter(fun1,str) #filter Iterator objects
7 print(list(ret)) #['b', 'c']
# map processing sequence (stitching, pruning, deleting)
1 str = ['a','b','c']
2 def fun1(s):
3 if s != 'a':
4 return s+'alvin'
5 ret = map(fun1,str) # Iterator objects
6 print(list(ret)) #[None, 'balvin', 'calvin']
# The result of reduce is a value.
1 from functools import reduce
2 def add1(x,y):
3 return x + y
4
5 print(reduce(add1,range(1,101))) #36 (1+2+3+4+5....+8+9)
# --lambda anonymous function
1 add = lambda a,b : a+b
2 print(add(2,3)) #5