Python:函数

来源:互联网 发布:我的世界为什么用java 编辑:程序博客网 时间:2024/06/14 02:34

函数的定义和调用:

格式:def  函数名(参数列表):#可以没有参数

函数体


接受中文几种方式:

#coding:utf8

#coding=utf8

#encoding=utf8

#encoding:utf8

#_*_ coding:utf8 _*_


默认值设定:自右想左

def  function(a = 0,b = 0):

def  function (a,b = 0): 自右想左

返回值的时候根据缩进使用return

def function():

return 


在函数内强制声明为全局变量:global x

在函数调用时才能使用 x,如果不调用函数,则不能使用x


向函数传元组和字典:function(* t)  传入字典(**t)

处理多余实参: def f(x,*args)(元组方式) def f(x,**t)字典方式

print "%s : %s " % 'name','milo'






0 0
原创粉丝点击