//abstest.py def my_abs(x): if x >= 0: return x else: return -x
在该文件的当前目录下启动python解释器,用 from abstest import my_abs 来导入my_abs()函数。
定义可变参数:
def calc(*numbers): sum = 0 for n in numbers: sum = sum + n*n return sum >>calc(1,2) 5 >>calc() 0 >>nums = [1,2,3] >>calc(*nums) 14
关键字参数:
def person(name , age , **kw): print(‘name:‘,name,‘age:‘,age,‘other:‘,kw) >>person(‘mico‘,30) name : mico age : 30 other : {} >>person(‘adm‘,45,city=‘beijing‘) name : adm age : 45 other : {‘city‘ : ‘Beijing‘} >>extra = {‘city‘ : ‘beijing‘ , ‘job‘ : ‘Engineer‘} >>person(‘jack‘,45,**extra) name : jack age : 45 other : {‘city‘ : ‘beijing‘ , ‘job‘ : ‘Engineer‘}
递归函数:
def fact(n): if n = 1: return 1 return n*fact(n-1)
原文:http://www.cnblogs.com/helloluo/p/7518065.html