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)
语法:
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)
相关文章推荐
- Diag:Diagonal matrices and diagonals of a matrix
- 【知识】int的取值范围
- Android版本兼容
- web.xml中<web-app>出现有错误
- Play FrameWork 如何在一个项目中链接多个数据库
- Java内存泄露原因详解
- Eclipse 插件 Open Explorer
- Neural NILM: Deep Neural Networks Applied to Energy Disaggregation
- grep命令使用详解
- JVM入门
- JVM入门
- 最长公共子序列(一)
- Session
- 蓝牙OPP协议文件传输格式修改
- MySQL 性能调优的10个方法
- android shape
- JS date常用代码积累
- IEnumerable和IQueryable的区别
- PHP截断函数mb_substr()
- iOS 开发笔记