python 06函数

1.格式:

python中函数定义方法:

def test(x):
"The function definitions"
x+=1
return x

def:定义函数的关键字
test:函数名
():内可定义形参
"":文档描述(非必要,但是强烈建议为你的函数添加描述信息)
x+=1:泛指代码块或程序处理逻辑
return:定义返回值

调用运行:可以带参数也可以不带
函数名()

补充:数学函数和编程函数的不同: 不同的是数学意义的函数,传入值相同,得到的结果必然相同且没有任何变量的修改(不修改状态),而编程语言中的函数传入的参数相同返回值可不一定相同

函数式编程就是:先定义一个数学函数(数学建模),然后按照这个数学模型用编程语言去实现它。

2.使用函数的好处:

1.代码重用

2.保持一致性,易维护

3.可扩展性

特点:同名函数会覆盖,不会重载

3.返回值

返回值数=0:返回None

返回值数=1:返回object

返回值数>1:返回tuple

4.函数参数

原则:

4.1位置参数,必须一一对应,缺一多一都不行

4.2关键字参数,无须一一对应,缺一多一都不行

4.3混合使用,位置参数必须在关键字参数左边,, 必须一一对应

4.4混合使用时,不能对同一个参数,重复传值

4.5设置默认参数,在方法名上,   def   handle(x,type=‘123‘)

4.6参数组:*列表,**字典    手动声明列表或字典时,需要在数组前面加*

原文地址:https://www.cnblogs.com/MrYangjlPython/p/9998340.html

时间: 2025-01-10 07:54:36

python 06函数的相关文章

python之函数用法capitalize()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法capitalize() #capitalize() #说明:将字符串的第一个字母变成大写,其他字母变小写. ''' capitalize(...) S.capitalize() -> string Return a copy of the string S with only its first character capitalized. ''' #案例 str='xiaoden

关于python的函数的*和**参数:

1.将足够的参数解包以后传递给函数:>def f(p1, p2, p3, p4):>     print p1+p2+p3+p4>>li = [1, 2, 3, 4]>f(*li)10>>tu = (1, 2, 3, 4)>f(*tu)10>>di = {'p1':1, 'p2':2, 'p3':3, 'p4':4}>f(**di)>10 2.使用封包的方法访问多余的参数>>> def funct(*para, **

python之函数用法setdefault()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法setdefault() #D.get(k,d) #说明:k在D中,则返回 D[K],如果k不在D中,则返回d值 #D.get(k,d), also set D[k]=d if k not in D ''' >>> help(dict.setdefault) Help on built-in function setdefault: setdefault(...) D.set

python之函数用法islower()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法islower() #http://www.runoob.com/python/att-string-islower.html #islower() #说明:检测字符串是否都由小写字母组成 str = "THIS is string example....wow!!!" print str.islower()#False str = "this is string

python之函数用法xrange()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法xrange() #xrange() #说明:返回一个生成器 #xrange做循环的性能比range好,尤其是返回很大的时候.除非要返回一个列表,则用range. ''' class xrange(object) | xrange(stop) -> xrange object | xrange(start, stop[, step]) -> xrange object | | Li

python之函数用法startswith()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法startswith() #http://www.runoob.com/python/att-string-startswith.html #startswith() #说明:返回布尔值,用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False. ''' startswith(...) S.startswith(prefix[, start[, end]]

python之函数用法globals()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法globals() #globals() #说明:在当前作用域下,查看全局变量 ''' globals(...) globals() -> dictionary Return the dictionary containing the current scope's global variables. ''' #案例 b='xiaodeng' print globals#<buil

python之函数用法__setattr__

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法__setattr__ #http://www.cnblogs.com/hongfei/p/3858256.html #用__setattr__函数重构方法 class Fruit(): def __init__(self,color,price): self.__color = color self.__price = price def __setattr__(self,name

Python 3 函数自由变量的大坑

Python中函数是一个对象, 和整数,字符串等对象有很多相似之处,例如可以作为其他函数的参数或返回对象, Python中的函数还可以携带自由变量, 两者无疑极大增进了Python的表达力. 但是Python函数自由变量的内部机制和列表解析或for循环结合使用时却暗藏杀机: #---CASE 1 fs = map(lambda i:(lambda j: i*j),range(6)) print([f(2) for f in fs]) #---CASE 2 fs = [lambda j:i*j f