首页 > 代码库 > Python函数笔记
Python函数笔记
函数返回值
#coding:utf-8
def f(x,y):
print x+y/*x+y即计算x+y若为"x+y"则直接输出x+y*/
f(97,98)
D:\Python安装程序\python.exe C:/Users/欢/PycharmProjects/untitled/ll.py
195
Process finished with exit code 0
#coding:utf-8
def f(x,y):
print "welcome"
print x+y
f(2,3)
z=f(3,4)
print z
D:\Python安装程序\python.exe C:/Users/欢/PycharmProjects/untitled/ll.py
welcome
5
welcome
7
None
Process finished with exit code 0
#coding:utf-8
def f():
return "hello"
z= f()/*函数被调用后必须返回一个指定的值,否则无法输出*/
print z
D:\Python安装程序\python.exe C:/Users/欢/PycharmProjects/untitled/ll.py
hello
Process finished with exit code 0
#coding:utf-8
def f():
return "hello"/*同时输入两个return,只有第一个执行即return执行后函数终止*/
return "rr"
z= f()
print z
D:\Python安装程序\python.exe C:/Users/欢/PycharmProjects/untitled/ll.py
hello
Process finished with exit code 0
向函数传元组和字典
#coding:utf-8
t=(‘name‘,‘milo‘)
def f(x,y):
print " % s: % s" % ( x,y)/*注意这种写法*/
f(*t)/*输出t元组对应的两个值*/
D:\Python安装程序\python.exe C:/Users/欢/PycharmProjects/untitled/ll.py
name: milo
Process finished with exit code 0
#coding:utf-8
def f(name=‘name‘,age=0):
print "name:%s"% name
print "age: %d"% age
f("milo",30)
d={"name":‘milo‘,"age":20}/*字典的无序性*/
f(**d)/*定义字典调用的方法*/
d={‘a‘:9,‘n‘:‘milo‘}
f(d[‘n‘],d[‘a‘])/*两个值要一一对应*/
D:\Python安装程序\python.exe C:/Users/欢/PycharmProjects/untitled/ll.py
name:milo
age: 30
name:milo
age: 20
name:milo
age: 9
处理多余的实数
#coding:utf-8
def f(x,*args):
print x
print args
f(1,2,3)
def f(x,*args,**kwargs):/*一个*表示元组,两个**表示字典*/
print x
print args
print kwargs
f(x=1,y=3,z=4)/*y=3,z=4根元组形式一样*/
D:\Python安装程序\python.exe C:/Users/欢/PycharmProjects/untitled/ll.py
1
(2, 3)
1
()
{‘y‘: 3, ‘z‘: 4}
Process finished with exit code 0
Process finished with exit code 0
Python函数笔记