函数参数

#-*- coding:UTF-8 -*-
from __future__ import division


def append(args=[]):   #定义函数,接受一个列表
    if len(args)<=0:
        args=[]
    args.append(0)
    print args
append()
append([1])
append()
print


print'函数的缺省参数'
def arithmetic(x=1,y=1,operator="+"):
    result={
        "+":x + y,
        "-":x - y,
        "*":x * y,
        "/":x / y
        }
    return result.get(operator) #返回计算结果


print arithmetic(1,2)
print arithmetic(1,2,"-")
print arithmetic(y=3,operator="-")
print arithmetic(x=4,operator="-")
print arithmetic(y=3,x=4,operator="-")
print
print'列表作为参数传递'
def arithmetic(args=[],operator="+"):
    x=args[0]
    y=args[1]
    result={
        "+":x + y,
        "-":x - y,
        "*":x * y,
        "/":x / y
    }
    return result.get(operator) #返回计算结果


print arithmetic([1,2])
print


print'传递可变参数'
def func(*args):
    print args
    
func(1,2,3)
print


print'传递可变参数'
def search(*t,**d):   #*t查找列表 **d查找参数,接收键值对组合为字典
    keys=d.keys()
    values=d.values()
    print keys
    print values
    for arg in t:
        for key in keys:
            if arg==key:
                print"find:",d[key]


search("one","three",one="1",two="2",three="3")

        

====================== RESTART: C:/Python27/argument.py ======================
[0]
[1, 0]
[0]
函数的缺省参数
3
-1
-2
3
1


列表作为参数传递
3


传递可变参数
(1, 2, 3)


传递可变参数
['three', 'two', 'one']
['3', '2', '1']
find: 1
find: 3


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值