1. Multi-type pass-through values and redundancy parameters
Multi-type pass-through values:
def fun (x, y):
return x +y
Print Fun(3,5)
8
Print Fun(*t)
3
def fun(x, Y, z):
return x + y + Z
T1 = (a)
Fun(*t1)
6
Fun(* (2,4,5))
11
Fun(1,*t)
4
Print T
(1, 2)
Fun (x=1,y=3,z=5)
9
>>> dic = {' x ': 1, ' Y ': 3, ' Z ': 6}
>>> Fun(**dic)
Ten
Redundant parameters:
>>> def fun (x,*args,**kwargs):
... print x
.. print args
... print Kwargs
...
>>> Fun (1)
1
()
{}
>>> Fun (+)
1
(2,)
{}
>>> Fun ( all in all)
1
(2, 3)
{}
>>> T
(1, 2)
>>> Fun (' A ', [1,2],*t,a=3,**{' t ': one, ' P ':')
1
(2, 3, ' a ', [1, 2], 1, 2)
{' A ': 3, ' P ': $, ' t ': one}
2. Recursive invocation of functions
Considerations for recursion:
You must have the final default result:
if n = = 0
Recursive parameters must converge to the default result:
Factorial (n-1)
Factorial script:
#!/usr/bin/env python
#-*-Coding:utf-8-*-
# @Time: 2018/1/4 11:57
# @Author: Feng xiaoqing
# @File: jiecheng.py
# ======================
def factorial (n):
sum = 0
For i in range (1,n+1):
sum + = i
return sum
print factorial (+)
Another method:
def factorial (n):
if n = = 0:
return 1
Else:
return n * factorial (n-1)
print factorial (5)
For the sum of 1-100 additions:
def factorial (n):
if n = = 0:
return 0
Else:
return n + factorial (n-1)
print factorial (+)
Recursive invocation of multiple types of values and redundant parameters and functions in Python functions