码迷,mamicode.com
首页 > 编程语言 > 详细

python函数

时间:2016-03-17 00:21:49      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:

一、函数:

创建函数:使用def语句

举例:定义一个返回斐波那楔数列列表的函数

def fibs(num):
    result = [0,1]
    for i in range(num-2):
        result.append(result[-2]+result[-1])
    print(result)
fibs(10)

>>>
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>>

 文档化函数:对函数进行说明,写在函数开头的字符串,它会作为函数的一部分进行存储,称为文档字符串。

举例:给square()函数进行注释

def square(x):
    ‘‘‘calculates the square of the nmber x‘‘‘
    return x*x

#使用此方法访问:
>>> square.__doc__ calculates the square of the nmber x >>>

 return语句:函数的返回值,起到结束函数的作用,不需要返回时,返回为None

举例:

def test():
    print(pass)
    return
    print(nothing)
x = test()
print(x)

>>> 
pass
None
>>> 

 参数:

形参:函数名后面的变量通常叫做函数的形参;

实参:调用函数时提供的值称为实参;

说明:在函数内为参数赋值新值不会改变外部任何变量的值;

举例:

#不可变参数:字符串、数字和元组
def change(n):
    n = zyj
    print(n)
    
name = sl
change(name)
print(name)
>>> 
zyj
sl
>>>

#可变参数如列表
def change(n):
n[0] = zyj
print(n[0])

name = [sl,xm]
change(name)
print(name)

>>> 
zyj
[zyj, xm]
>>>

# 保留原始变量的办法:
def change(n):
n[0] = zyj
print(n[0])

name = [sl,xm]
change(name[:])
print(name)

zyj
[sl, xm]
>>>

使用函数改变数据结构的实现举例

要求:编写一个存储名字,并且能根据条件查找对应的名字

实现:定义数据结构类型为字典,通过键值(first,second)查找对应的name。

def init(data):
    ‘‘‘初始化数据结构的函数‘‘‘
    data[first] = {}
    data[second] = {}
def lookup(data,lable,name):
    ‘‘‘查找人名相同函数‘‘‘
    return data[lable].get(name)
def store(data,full_name):
    ‘‘‘将输入名字存储在数据库中函数‘‘‘
    names = full_name.split()
    if len(names) == 1:
        names.insert(1, ‘‘)
    labels = first,second
    for label,name in zip(labels,names):
        peole = lookup(data,label,name)
        if peole:
            peole.append(full_name)
        else:
            data[label][name]=[full_name]

>>> storage = {}
>>> init(storage)
>>> storage
{first: {}, second: {}}
>>> store(storage, zhao yujiao)
>>> lookup(storage,second,yujiao)
[zhao yujiao]
>>> store(storage, zhao qiaojiao)
>>> lookup(storage,second,yujiao)
[zhao yujiao]
>>> storage
{first: {zhao: [zhao yujiao, zhao qiaojiao]}, second: {yujiao: [zhao yujiao], qiaojiao: [zhao qiaojiao]}}
>>> lookup(storage,first,yujiao)
>>> lookup(storage,first,zhao)
[zhao yujiao, zhao qiaojiao]
>>> store(storage, song qiaojiao)
>>> storage
{first: {zhao: [zhao yujiao, zhao qiaojiao], song: [song qiaojiao]}, second: {yujiao: [zhao yujiao], qiaojiao: [zhao qiaojiao, song qiaojiao]}}
>>> lookup(storage,second,qiaojiao)
[zhao qiaojiao, song qiaojiao]
>>> store(storage, qiaojiao)
>>> storage
{first: {zhao: [zhao yujiao, zhao qiaojiao], qiaojiao: [qiaojiao], song: [song qiaojiao]}, second: {‘‘: [qiaojiao], yujiao: [zhao yujiao], qiaojiao: [zhao qiaojiao, song qiaojiao]}}

 关键字参数:可以明确每个参数的作用,不需要在乎参数的位置。

def hello_1(greeting,name):
    print("%s,%s!" % (greeting,name))
def hello_2(name,greeting):
    print("%s,%s!" % (name,greeting))
hello_1("Hello","World")
hello_1(greeting="Hello",name="World")
>>> 
Hello,World!
Hello,World!
>>>

关键字参数可以在函数中给参数提供默认值,此时调用的时候可以不用提供参数,提供一些或提供所有参数

def hello_3(greeting="Hello",name="World"):
    print("%s,%s!" % (greeting,name))
hello_3()
hello_3("greetings")
hello_3("greetings",zyj)
hello_3(name=zyj)
>>> 
Hello,World!
greetings,World!
greetings,zyj!
Hello,zyj!
>>> 

位置参数和关键字参数结合使用

def hello_3(name,greeting="Hello",p=!):
    print("%s,%s%s" % (greeting,name,p))
hello_3("zyj")
hello_3("zyj",p=!!!)
hello_3(name=zyj)
>>> 
Hello,zyj!
Hello,zyj!!!
Hello,zyj!
>>> 

收集参数:参数前加星号将所有值放置在同一个元组中。一个参数里面传递多个值。

def print_params(*params):
    print(params)
print_params(zyj)
print_params(1,2,3)
print_params([1],[2],[3])
print_params((1,2),(2,3))
def print_params(title,*params):
    print(title)
    print(params)
print_params(name:,zyj,sl,sb)
print_params(name:)
>>> 
(zyj,)
(1, 2, 3)
([1], [2], [3])
((1, 2), (2, 3))
name:
(zyj, sl, sb)
name:
()
>>> 

收集关键字参数:使用**,返回的是字典

def print_params(**params):
    print(params)
print_params(x=1,y=2,z=3)
print_params(x=[1],y=[2],z=[3])
print_params(x=(1,2),y=(2,3))
def print_params(x,y,z=9,*params,**key):
    print(x,y,z)
    print(params)
    print(key)
print_params(1,2,3,4,5,6,foo=1,bar=2)
print_params(1,2)
>>> 
{x: 1, y: 2, z: 3}
{x: [1], y: [2], z: [3]}
{x: (1, 2), y: (2, 3)}
1 2 3
(4, 5, 6)
{foo: 1, bar: 2}
1 2 9
()
{}
>>> 

 

  

python函数

标签:

原文地址:http://www.cnblogs.com/zhaoyujiao/p/5252056.html

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