li = 1,42,3,14,58,6
print fun(*li) # 关键字参数 def enroll(name, age=22, **kwargs): print 'name:' + name print 'age:%d' % age for k, w in kwargs.items(): print '%s : %s' % (k, w) dic = {'myclass':'运维班',"gender":'male'} enroll('fentiao', **dic) # 参数定义的顺序:必选参数 > 默认参数 > 可变参数 > 关键字参数 # *args,可变参数,args接收的是tuple. # **kwargs,关键字参数,kwargs接收的是字典. def enroll(name, age=22, **kwargs): print 'name:' + name print 'age:%d' % age for k, w in kwargs.items(): print '%s : %s' % (k, w) dic = {'myclass':'运维班',"gender":'male'} enroll('fentiao', **dic) ''' b = 2 # 全局变量,在整个代码中生效 def a(): global b b=1 # 局部变量,只在函数内生效 print b a()print b
#**************************** 1. 递归函数 ******************************** # # n! = 1x2x3x4x.......(n-1)xn= (n-1)!xn = (n-2)!x(n-1)xn # # 1! = 1 # 2! = (2-1)! x 2 = fact(2-1) x 2 # 3! = (3-1)! x 3 = (2-1)! x 2 x 3 = fact(3-1) x 3 = fact(1)x 2 x3 def fact(n): if not isinstance(n,int): print 'input int num' if n == 1: return 1 return fact(n-1)*n print fact(3) # 1. 在写递归函数时要有一个明确的结束条件; # 2. 在递归时,问题规模要越来越小; # 3. 递归函数效率不高,函数调用是通过栈的数据结构实现的, # 每调用依次,栈就会多一层,最多999层,否则会出现栈溢出。 # 实现二分的效果 # n = 10 # 返回值为: # 5 # 2 # 1 def fun(n): if not isinstance(n,int): print 'Error Type' print n b = int(n/2) if b > 0: return fun(b) fun(18) #**************************** 2. 函数式编程 ************************ # 面向对象编程语言:java,c++, python # 面向过程编程语言:c # 面向函数编程语言(lisp,erlang): f = y(x,y) (13+10)*2-8 面向过程: a = 13 + 10 b = a * 2 c = b -8 面向函数: result = jian(multipy(add(13,10),2),8) #**************************** 3. 高阶函数 ************************ # 函数可以当作参数去传递 def fun1(x,y,fun): return fun(1,2) print fun1(1,2,max)
