Python入门6_抽象

来源:互联网 发布:苹果数据线线序 编辑:程序博客网 时间:2024/06/12 18:46

1,创建函数:

>>> def hello(name)        return 'hello'+name>>> print hello('jack')'hello jack'# 使用help()函数能了解函数的信息>>> help(abs)>>> abs(...)    abs(number) -> number    Return the absolute value of the argument.

2,关键值参数和默认值

>>> def hello(name,age):        print name ,'is',age>>> hello(name = 'Nacy',age = 19)#避免参数位置错误,定义的时候也可以这样>>> def hello(name = 'Nacy',age = 19)

4,收集参数:

>>> def printf(*data):#在参数前面加上*,将所有传入的参数转化为元组        print data>>> printf(1,2,3):(1,2,3)>>> def ggg(**x):#加两个×号返回字典        print x>>> ggg(x = 2,y = 3){x:2,y:3}#还有它的可逆过程>>> def gg(x,y):return x+y>>> pa = (1,4)>>> print gg(*pa)5

5,先实战一下:

>>> def tell(**kid):        return '%(name)s,%(age)s',%kid>>> tell(name = 'kalina',age = 19)>>> dic = {'name':'ka','age':19}>>> tell(**dic)  #这个和上面调用的效果一样>>> c = (2,3)>>> pow(*c)8

接下来继续学习 Python入门7_抽象进阶

原创粉丝点击