首页 > 编程语言 > 详细

Python函数笔记

时间:2016-10-10 23:26:49      阅读:284      评论:0      收藏:0      [点我收藏+]

函数返回值
#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函数笔记

原文:http://www.cnblogs.com/ZHANG576433951/p/5947459.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!