函数:代码的重复利用

格式:

def 函数名 (参数列表)
    函数体

函数定义与调用:

def fun():    #定义函数
    print('hello world !')

fun() #函数调用

输出结果为:

hello world !

按值传递参数和按引用传递参数

a,b = 5,8
c = a * b
print(c)
print('我是分割线'.center(30,'*'))

def fun(x,y):
    t = x*y
    return t

c = fun(a,b)
print(c)

输出结果:

40
************我是分割线*************
40

函数参数:

  • 形参:是虚拟的,不占用内存空间
  • 实参:调用函数时传给函数的参数

(1)必备参数:必备参数须以正确的顺序传入函数。调用时的数量必须和声明时的一样

def fun(str):   #预定义例一个参数
    print(str)
    return
fun()   #调用到时候没有传入任何参数

输出结果:

TypeError: fun() missing 1 required positional argument: 'str'   #这时候会报错,需要给函数传入一给参数。

(2)指定参数:传入的参数顺序与声明时不一致时

例1:

def fun(name,age):
    print('Name:',name)
    print('Age:',age)
    return
fun(age=30,name='pingy')   #传入顺序故意改变

输出结果:

Name: pingy
Age: 30

例2:

def fun(x,y,z):
    print('x is %d,y is %d,z is %d' %(x,y,z))

fun(3,y=2,z=4)   #指定y和z的值,而x的值就是默认传入位置的参数
print('我是分割线'.center(30,'*'))
fun(3,z=2,y=4)   #指定y和z的值,但是位置对调

 

输出结果:

x is 3,y is 2,z is 4
************我是分割线*************
x is 3,y is 4,z is 2

如果指定x的参数,而y和z不指定会如何呢?答案是会报错!

(3)位置参数:

def fun(x,y):   #x,y是形参
    print('x is %d,y is %d' %(x,y))

fun(3,6)  #3在前,6在后,在传给函数的形参就是按位置x=3,y=6
print('我是分割线'.center(30,'*'))
fun(6,3)  #6在前,3在后,在传给函数的形参就是按位置x=6,y=3

输出结果:

x is 3,y is 6
************我是分割线*************
x is 6,y is 3

(4)默认参数:调用函数时,缺省参数的值如果没有传入,则被认为是默认值。

例1:

def fun(name,age=29):
    print('Name:',name)
    print('Age:',age)
    return
fun(age=35,name='echo')
fun(name='pingy')

输出结果:

Name: echo
Age: 35
Name: pingy
Age: 29

例2:

def fun(x,y=3,z):
    print('x is %d,y is %d,z is %d' %(x,y,z))

fun(1,3)   #只传入两个参数,正常逻辑是x=1,y=3(默认),z=3

输出结果:

    def fun(x,y=3,z):
           ^
SyntaxError: non-default argument follows default argument

例3:

def fun(x,y,z=3):
    print('x is %d,y is %d,z is %d' %(x,y,z))

fun(1,6)   #只传入两个参数,正常逻辑是x=1,y=6,z=3(默认)

输出结果:

x is 1,y is 6,z is 3

例4:

def fun(x,z,y=3):
    print('x is %d,y is %d,z is %d' %(x,y,z))

fun(1,6)   #只传入两个参数,正常逻辑是x=1,z=6,y=3(默认)

输出结果:

x is 1,y is 3,z is 6

(5)动态参数:当传入当参数比当初声明的参数多时,可用来接收多余参数

例1:

def fun(x,y,*args):
    print('x is %s,y is %s,z is %s' %(x,y,args))

fun(1,2,3,4,5,6)

输出结果:

x is 1,y is 2,z is (3, 4, 5, 6)

例2:*args:接收到多余的值放入元组中

def fun(x,y,*args):
    print('x is %s,y is %s,z is %s' %(x,y,args))

fun(1,2)  #不传入args的值
fun(1,2,(3,4,5))  #传入一个元组
fun(1,2,[3,4,5])  #传入一个列表
fun(1,2,{3,4,5})  #传入一个集合
fun(1,2,{'k1':3,'k2':4})   #传入一个字典

输出结果:

x is 1,y is 2,z is ()
x is 1,y is 2,z is ((3, 4, 5),)
x is 1,y is 2,z is ([3, 4, 5],)
x is 1,y is 2,z is ({3, 4, 5},)
x is 1,y is 2,z is ({'k1': 3, 'k2': 4},)

例3:**kwargs:接收到多余的值放入字典中

def fun(x,y,**kwargs):
    print('x is %s,y is %s,z is %s' %(x,y,kwargs))

fun(1,2)  #不传入args的值
fun(1,2,3,4,5)

输出结果:

x is 1,y is 2,z is {}

Traceback (most recent call last):
    fun(1,2,3,4,5) 
TypeError: fun() takes 2 positional arguments but 5 were given    #第二条函数调用出错:只需要2个值,而传入了5个值

def fun(x,y,**kwargs):
    print('x is %s,y is %s,z is %s' %(x,y,kwargs))

fun(1,2,name='pingy',age=18,job='IT')

输出结果:

x is 1,y is 2,z is {'job': 'IT', 'age': 18, 'name': 'pingy'}

list1 = [8,9,10,11]
dict1 = {'name':'pingy','age':18,'job':'IT'}

def fun(x,y,*args,**kwargs):
    print('x is %s,y is %s,args is %s,kwargs is %s' %(x,y,args,kwargs))

fun(1,2,*list1,**dict1)

输出结果:

x is 1,y is 2,args is (8, 9, 10, 11),kwargs is {'age': 18, 'name': 'pingy', 'job': 'IT'}

lambda函数:

def f(x,y):
        print(x+y)
f(2,3)
#或者:
#s
= lambda x,y:x+y #s(2,3)

输出结果:

5

return语句:return 表达式用于退出函数


def
sum(arg1,arg2): t = arg1 + arg2 print('函数内:',t) return t t = sum(10,20) print('函数外:',t)

输出结果:

函数内:30
函数外:30

变量作用域:

t = 0    # 全局变量
def
sum (arg1,arg2): t = arg1 + arg2 # 局部变量 print('函数内部局部变量:',t) return t sum(10,20) # 调用函数 print('函数外部全局变量:',t)

输出结果:

函数内部局部变量:30
函数外部全局变量:0

序列处理函数:

filter():过滤器

def fun(x):
    return x % 2

list1 = range(20)
t = list(filter(fun,list1))
print(t)
或者:
>>> list(filter(lambda x : x % 2,range(20)))

输出结果:

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

zip():列表并行遍历

name = ['pingy','echo','show']
age = [20,30,40]
tel = ['133','159','189']

t = zip(name,age,tel)
print(list(t))

输出结果:

[('pingy', 20, '133'), ('echo', 30, '159'), ('show', 40, '189')] 

map()

例1:

name = ['pingy','echo','show']
age = [20,30,40]
tel = ['133','159','189']

def fun(x,y,z):
    return x,y,z

t = map(fun,name,age,tel)
print(list(t))

输出结果:

[('pingy', 20, '133'), ('echo', 30, '159'), ('show', 40, '189')]

#如果上题中再新增一个列表,而且列表中元素少于name,age,tel中元素个数,并行的最后一组不会打印出来,如下例题:

name = ['pingy','echo','show']
age = [20,30,40]
tel = ['133','159','189']
class1 = ['one','two']

def fun(x,y,z,m):
    return x,y,z,m
t = map(fun,name,age,tel,class1)
print(list(t))

输出结果:

[('pingy', 20, '133', 'one'), ('echo', 30, '159', 'two')]

例2:

a = [1,2,3]
b = [4,5,6]

def fun(x,y):
    return x*y

t = map(fun,a,b)
print(list(t))

输出结果为:

[4, 10, 18]

reduce():递归  #注意:在python3中,reduce函数已经从全局名字空间中移除了,现在放在functools模块里。

from functools import reduce   #先从模块中调用reduce函数

l = range(101)
from functools import reduce
def fun(x,y):
    return x+y

t = reduce(fun,l)
print(t)

#或者
>>> sum = reduce(lambda x,y:x+y,l)
>>> sum

输出结果:

5050


posted on 2016-08-31 15:18  PingY  阅读(562)  评论(0编辑  收藏  举报