码迷,mamicode.com
首页 > 其他好文 > 详细

lesson4: 函数简单应用

时间:2016-01-07 18:06:11      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

函数语法:

语法:

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)

技术分享

 

lesson4: 函数简单应用

标签:

原文地址:http://www.cnblogs.com/janicce-zhong/p/5110534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!