函数定义与调用
定义:
- 函数代码块以
def
关键词开头,后接函数标识符名称和圆括号()。 - 任何传入参数和自变量必须放在圆括号中间。圆括号之间可以用于定义参数。
- 函数的第一行语句可以选择性地使用文档字符串,用于存放函数说明。
- 函数内容以冒号起始,并且缩进。
- return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的return相当于返回 None。
def say(name, word):
'My first function'
print(name, ':', word)
a = 'hesor'
b = 'write the code ,change the world !'
say(a, b)
print(say.__doc__)
'''
Output:
hesor : write the code ,change the world !
My first function
'''
函数参数
形参与实参
形参;函数定义时括号中的内容,例如示例1中的name和word
实参:函数调用时传入函数的内容,例如示例1中的字符串a和字符串b
关键字参数
防止参数调用的时候顺序混淆,具体使用如下;
def say(name, word):
'My first function'
print(name, ':', word)
a = 'hesor'
b = 'write the code ,change the world !'
say(a, b)
say(name=a, word=b) # 关键字参数
say(word=b, name=a) # 关键字参数
print(say.__doc__)
'''
Output:
hesor : write the code ,change the world !
hesor : write the code ,change the world !
hesor : write the code ,change the world !
My first function
'''
默认参数
和C++中的默认参数使用方法相似,如果不传入参数,那么使用默认参数值
def say(name='zhangsan', word='hello'):
'My first function'
print(name, ':', word)
a = 'hesor'
b = 'write the code ,change the world !'
say()
say(word=b)
say(name=a)
say(a, b)
'''
Output:
zhangsan : hello
zhangsan : write the code ,change the world !
hesor : hello
hesor : write the code ,change the world !
'''
收集参数
不确定函数会使用多少个参数,在参数名前面加上*
表示默认参数
默认参数可以看成是元组
最好结合关键字参数和默认参数使用
def say(*word, name='zhangsan'):
'My first function'
print(len(word))
print(name, ':', word)
a = 'hesor'
b = 'write the code ,change the world !'
c = 'hello world !'
say(b, c, name=a)
'''
Output:
2
hesor : ('write the code ,change the world !', 'hello world !')
'''
函数与过程
过程可以理解为没有返回值的函数
函数的一大特点就是可以有返回值
python中的函数没有像C语言中的函数类型,但是可以返回任何类型的值,甚至可以返回多个类型、多个值(打包成元组、列表返回)
局部变量与全局变量
不要试图在函数中改变全局变量的值,那样只会新创建一个局部变量,如果真的要这么做,现在函数体内使用global关键字
a = 100
def f():
global a # 声明a为全局变量
a = 10
f()
print(a)
'''
Output:
10
'''
内嵌函数
即函数嵌套函数
def f1():
x = 3
def f2():
x *= x
return x
f2()
return x
print(f1())
与全局变量和局部变量相类似的,可以得到上述示例中的f1() f2()中的x作用域,为了能在f2()中调用x的值,我们可以使用nonlocal
关键字,方法同global
def f1():
x = 3
def f2():
nonlocal x
x *= x
return x
f2()
return x
print(f1())
区分global
与nonlocal
:
1、局部作用域改变全局变量用global
2、内层函数改变外层函数变量用nonlocal
, nonlocal
不能定义新的外层函数变量,只能改变已有的外层函数变量
经过测试,nonlocal
可以调用多层嵌套的外部函数
lambda表达式
格式:
lambda 参数a,参数b : 表达式
等价于一个 有两个参数a
、b
的,return表达式
的函数
def f1(x):
return x * 2 + 3
f2 = lambda x : x * 2 + 3
print(f1(5))
print(f2(5))
'''
f1 f2 等价
'''
filter()与map()
filter()
:意即过滤器,顾名思义过滤掉不需要的东西
格式:
filter(function,可迭代对象)
其中function为lambda表达式或者是函数,也就是过滤条件
第二个参数为可迭代对象
filter函数返回的是对象
def is_odd(x):
return x % 2
list1 = range(10)
print(list(filter(is_odd, list1)))
print(list(filter(lambda x: x % 2, list1)))
'''
Output:
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
'''
map()
:映射,对迭代对象进行处理
map(function,可迭代对象)
其中function为lambda表达式或者是函数,作为处理方式
第二个参数为可迭代对象
map函数返回的是对象
def f(x):
return x * 2+3
list1 = range(10)
print(list(map(f, list1)))
print(list(map(lambda x: x * 2+3, list1)))
'''
Output:
[3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
[3, 5, 7, 9, 11, 13, 15, 17, 19, 21]
'''
以及前几天我遇到过的一行多个整数的输入问题:
a = list(map(int, list(input().split())))
print(a)