1.格式:

def 函数名(形参):

  函数体

 

2.过程:就是没有返回值的函数

3.参数

def test(x, y, z):
    print(x)
    print(y)
    print(z)

test(1, 2, 3) # 位置参数,必须一一对应,缺一不行多一也不行

test(y=1,x=3,z=4)  # 关键字参数,无须一一对应,缺一不行多一也不行

test(1,y=2,3) # 位置参数必须在关键字参数左边,报错
test(z=2,1,3)#报错
输出:SyntaxError: positional argument follows keyword argument

test(1,3,y=2) # 重复传值,报错
test(1,3,z=2,y=4) # 报错
输出:TypeError: test() got multiple values for argument 'y'

test(1,3,z=2) # 正确

4.参数值:**字典 *列表

def test(x,*args):
    print(x)
    print(args)

test(1)
test(1,2,3,4,5)
test(1,{'name':'alex'})
test(1,['x','y','z'])
test(1,*['x','y','z'])
test(1,*('x','y','z'))

输出:
1
()

1
(2, 3, 4, 5)

1
({'name': 'alex'},)

1
(['x', 'y', 'z'],)

1
('x', 'y', 'z')

1
('x', 'y', 'z')
*args示例
def test(x,**kwargs):
    print(x)
    print(kwargs)

test(1,y=2,z=3)
输出:
1
{'y': 2, 'z': 3}

test(1,y=2,z=3,z=4) # 一个参数不能传两个值
输出:
SyntaxError: keyword argument repeated
**kwargs示例
def test(x,*args,**kwargs):
    print(x)
    print(args,args[-1])
    print(kwargs,kwargs.get('y'))

test(1,1,2,1,1,11,1,x=1,y=2,z=3) #报错
输出:
TypeError: test() got multiple values for argument 'x'

test(1,1,2,1,1,11,1,y=2,z=3)
输出:
1
(1, 2, 1, 1, 11, 1) 1
{'y': 2, 'z': 3} 2

test(1,*[1,2,3],**{'y':1})
输出:
1
(1, 2, 3) 3
{'y': 1} 1
*args,**kwargs示例

5.前向引用:函数需于调用前已经定义才能调用

def foo():
    print('from foo')
    bar()

foo()

def bar():
    print('from bar')

输出:
NameError: name 'bar' is not defined
函数前向调用

6.函数递归:在一个函数体中重复调用该函数

def calc(n):
    print(n)
    if int(n / 2) == 0:
        return n
    res = calc(int(n / 2))
    print(res)
    return res

res = calc(5)
print(res)

输出:
5
2
1
1
1
1
函数递归示例

7.函数作用域:函数的作用域只跟函数声明时定义的作用域有关,跟函数的调用位置无任何关系

name = 'allen'
def foo():
    name = 'tsui'
    def bar():
        print(name)
    return bar

a = foo()
print(a)
a()

输出:
<function foo.<locals>.bar at 0x000000000345C488>
tsui
函数作用域示例

8.匿名函数:lambda

a = lambda x:x**2
print(a(2))

a = lambda x, y, z:(x+1, y+1, z+1)
print(a(1, 2, 3))

输出:
4
(2, 3, 4)
匿名函数示例

9.函数式编程之高阶函数:(1)函数接收的参数是一个函数名;(2)返回值中包含函数

# 把函数当作参数传给另外一个函数
def foo(n):
    print(n)

def bar(name):
    print('my name is %s' %name)

foo(bar('allen'))

输出:
my name is allen
None

#返回值中包含函数
def bar():
    print('from bar')
def foo():
    print('from foo')
    return bar
n=foo()
n()

输出:
from foo
from bar
高阶函数示例

10.map()函数:对输入序列元素进行处理,map(func, *iterables)

num_l=[1,2,10,5,3,7]
res=map(lambda x:x+1,num_l)
print(res)
print(list(res))

输出:<map object at 0x0000000003483CC0>
[2, 3, 11, 6, 4, 8]
map函数示例

11.filter()函数:对输入序列元素进行过滤处理,filter(function or None, iterable),function为None时返回True

people=['allen','tsui']
print(filter(lambda n:not n.endswith('en'),people))
res=filter(lambda n:not n.endswith('en'),people)
print(list(res))

输出:
<filter object at 0x000000000346B9E8>
['tsui']
filter函数示例

12.reduce()函数:对输入序列元素进行处理输出一个值,reduce(function, sequence, initial=None)

from functools import reduce   #python3中需要导入reduce模块
num_l=[1,2,3,100]
print(reduce(lambda x,y:x+y,num_l,1)) #赋予初始值
print(reduce(lambda x,y:x+y,num_l))

输出:
107
106
reduce函数示例

 13.zip()函数:把多个可迭代对象的对应位置元素输出成元组

print(list(zip(('a','n','c'),(1,2,3))))
print(list(zip(('a','n','c'),(1,2,3,4))))
print(list(zip(('a','n','c','d'),(1,2,3))))
print(zip(('a','n','c'),(1,2,3)))

输出:
[('a', 1), ('n', 2), ('c', 3)]
[('a', 1), ('n', 2), ('c', 3)]
[('a', 1), ('n', 2), ('c', 3)]
<zip object at 0x00000000034713C8>
zip函数示例

14.max()和min():比较序列中最大和最小值,不同类型之间不能进行比较;如果是字典默认比较的是字典的key;

people=[
    {'name':'a','age':10},
    {'name':'b','age':100},
    {'name':'c','age':9},
    {'name':'d','age':18},
]

print(max(people,key=lambda dic:dic['age']))

输出:
{'name': 'b', 'age': 100}
max() min()示例

15.sorted()函数:对序列元素大小进行排序;

people=[
    {'name':'a','age':10},
    {'name':'b','age':100},
    {'name':'c','age':9},
    {'name':'d','age':18},
]

print(sorted(people,key=lambda dic:dic['age']))

输出:
[{'name': 'c', 'age': 9}, {'name': 'a', 'age': 10}, {'name': 'd', 'age': 18}, {'name': 'b', 'age': 100}]
sorted()函数示例

16.内置函数

参考:

http://www.cnblogs.com/regit/p/8482197.html

http://www.runoob.com/python/python-built-in-functions.html