标签:
函数语法:
语法:
def functionname(para):
"函数_文档字符串"
function_suite
return[expression]
sample1:print str by def
def printstr(str):
"print input str"
print str
return
printstr("welcome to python!")
实参:调用函数时的变量名称
形参:定义函数时的变量名称
缺省参数:调用函数时,缺省参数的值没有传入,则被认为是默认值
sample2
def printstr(str="hell0,python"):
print str
printstr()
多类型传参:sample3
def f(x):
print x
f(10)
f("hello")
f([1,2,3])
f((1,2,3))
f({1,2,3})
Sample 4:
def f(x,y):
print x,y
T={5,"janice"}
f(*T)
为什么打印结果为 janice 5 呢?
sample5:一一对应的字典传值
def f(x,y):
print x,y
T={‘x‘:‘janice‘, ‘y‘:‘18‘}
f(**T)
结果输出:janice 18
sample 6:处理不定长参数
def f(x,*args,**kw):
print x
print args,
print kw
f(1,2,3,y=20,z=30)
标签:
原文地址:http://www.cnblogs.com/janicce-zhong/p/5110534.html