#-*- 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")
from __future__ import division
def append(args=[]): #定义函数,接受一个列表
if len(args)<=0:
args=[]
args.append(0)
print args
append()
append([1])
append()
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'列表作为参数传递'
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'传递可变参数'
def func(*args):
print args
func(1,2,3)
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