python函数

函数定义与调用

定义:

  • 函数代码块以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

函数参数

形参与实参

形参;函数定义时括号中的内容,例如示例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
'''
示例2
默认参数

和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 !
'''
示例3
收集参数

不确定函数会使用多少个参数,在参数名前面加上*表示默认参数
默认参数可以看成是元组
最好结合关键字参数和默认参数使用

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
'''
示例4

内嵌函数

即函数嵌套函数

def f1():
    x = 3

    def f2():
        x *= x
        return x
    f2()
    return x


print(f1())
示例5

与全局变量和局部变量相类似的,可以得到上述示例中的f1() f2()中的x作用域,为了能在f2()中调用x的值,我们可以使用nonlocal关键字,方法同global

def f1():
    x = 3

    def f2():
        nonlocal x
        x *= x
        return x
    f2()
    return x


print(f1())
示例6

区分globalnonlocal
1、局部作用域改变全局变量用global
2、内层函数改变外层函数变量用nonlocalnonlocal不能定义新的外层函数变量,只能改变已有的外层函数变量
经过测试,nonlocal可以调用多层嵌套的外部函数

lambda表达式

格式:

lambda 参数a,参数b : 表达式

等价于一个 有两个参数ab的,return表达式的函数

def f1(x):
    return x * 2 + 3
    
f2 = lambda x : x * 2 + 3

print(f1(5))
print(f2(5))
'''
f1 f2 等价
'''
示例7

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]
'''
示例8

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]
'''
示例9

以及前几天我遇到过的一行多个整数的输入问题:

a = list(map(int, list(input().split())))
print(a)
示例10
以上差不多就是Python函数基础的全部内容了^_^
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hesorchen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值