标签:定义函数 默认参数 类型 class 字符 tar callable append document
def function_name(param1, param2):
‘this is document‘
pass # fuction body
return value # 可选
>>> def print_params(*params):
... print params
...
>>> print_params(1,2,3,4)
(1, 2, 3, 4)
>>>
>>> a=(1, 2, 3, 4)
>>> print_params(*a)
(1, 2, 3, 4)
>>> print_params(a)
((1, 2, 3, 4),)
def print_params_3(**params):
print params
>>>
>>> print_params_3(a=1, b=2, c=3, d=4)
{‘a‘: 1, ‘c‘: 3, ‘b‘: 2, ‘d‘: 4}
>>>
>>> print_params_3(**a)
{‘a‘: 1, ‘c‘: 3, ‘b‘: 2, ‘d‘: 4}
>>>
>>> def interval(start, stop=None, step=1):
... ‘imitates range for step > 0‘
... if stop == None:
... start,stop=0,start
... result=[]
... i = start
... while i < stop:
... result.append(i)
... i+=step
... return result
...
>>>
>>> interval(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> interval(2,5)
[2, 3, 4]
>>>
标签:定义函数 默认参数 类型 class 字符 tar callable append document
原文地址:http://www.cnblogs.com/wansui/p/7277263.html