您的位置:首页 > 其它

lesson4: 函数简单应用

2016-01-07 17:12 357 查看
函数语法:

语法:

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)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: