[Python入门学习记录(小甲鱼)]第6章 函数

函数就是把代码整理打包的东西

6.1 Python的函数基操

函数的基本操作

6.1.1 创建和调用函数

def myfunc():
    print(1)
    print(2)
    print(3)
myfunc()  # 输出 1 2 3 带换行  调用时会自动找函数定义

6.1.2 函数的参数

def add(num1, num2):
    print(num1 + num2)
add(1, 2)  # 输出 3

6.1.3 函数的返回值

def add(num1, num2):
    return num1 + num2
print(add(1, 2))  # 输出 3

def sub(num1, num2):
    print(num1 - num2)
print(sub(2, 1))  # 输出 1 和 None 没有返回值默认None

def test():
    return 1, 2, 3
print(test())  # 输出 (1, 2, 3) 多个返回值默认按元组返回

def test2():
    return [1, 2, 3]
print(test2())  # 输出 [1, 2, 3] 多个返回值可以设置按列表返回

6.2 Python函数的灵活参数

6.2.1 形参和实参

def say(name):
    print("i am %s" % name)
say('lin')  # 输出 i am lin   name 形参  'lin'实参

6.2.2 函数文档

def exchangeRate(dollar):
    """
    功能:汇率转换,美元->人民币
    汇率:6.54
    日期:2018-06-24
    """
    return dollar * 6.54
print(exchangeRate(10))  # 输出 65.4  文档字符串正常不打印

print(exchangeRate.__doc__)  # 查看文档字符串

6.2.3 关键字参数

def eat(somebody, something):
    print(somebody + '+' + something)
eat('wo', 'ni')  # 输出 wo+ni
eat('ni', 'wo')  # 输出 ni+wo
eat(something='wo', somebody='ni')  # 输出 ni+wo
eat('wo', somebody='ni')  # 输出 ni+wo

6.2.4 默认参数

def test(name="123", word="456"):
    print(name + '+' + word)
test()  # 输出 123+456
test('lin', 'wei')  # 输出 lin+wei

6.2.5 收集参数

def test(*params):
    print("有 %d 个参数" % len(params))
    print("第二个参数是", params[1])
test(1, 2, 3, 4, 5) 

6.3 Python变量的作用域

6.3.1 局部变量

函数的形参和函数内部定义的变量都是局部变量,只能在函数内调用

6.3.2 全局变量

定义在外部的变量能作用于任何地方,但如果要在函数内修改全局变量的值,需要使用 global 关键字

6.3.3 global关键字

def test():
    count = 10
    print(count)
test()
count = 5
print(count)  # 输出 10 和 5 无法改变全局变量的值

count = 5
def test():
    global count
    count = 10
    print(count)
test()

print(count)  # 输出 10 和 10 可以改变全局变量的值

6.3.4 内嵌函数

Python允许在函数内部定义另一个函数,内嵌函数只能在包含它的函数内部调用

6.3.5 LEGB原则

变量查找顺序:Local -> Enclosing function locals -> Global -> Builtin

6.3.6 闭包

在内部函数中对一个在外部作用域但不在全局作用域的变量引用,这个内部函数就认为是闭包

6.3.7 装饰器

将函数作为参数传递进另一个函数加工后再返回,这个过程就是装饰

def log(func):
    def func2():
        print("lin")
        func()
        print("wei")
    return func2

@log
def test():
    print("jun")
test()  # 输出 lin jun wei

6.4 函数式编程

6.4.1 lambda

lambda关键字用来创建匿名函数

g = lambda x, y: x + y
print(g(23, 3))  # 输出 26

6.4.2 filter

过滤器,filter函数用来过滤可迭代对象中符合条件的元素

print(list(filter(lambda x: x % 2, range(10))) # 过滤得到奇数

6.4.3 map

映射,map函数对每个元素进行操作

print(list(map(lambda x: x * 2, range(10)))  # 输出 [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

6.5 递归

6.5.1 写一个阶乘函数

def func(n):
    if n == 1:
        return 1
    else:
        return n * func(n - 1)

print(func(5))  # 输出 120

6.5.2 斐波那契数列

def func(n):
    if n == 1 or n == 2:
        return 1
    else:
        return func(n - 1) + func(n - 2)

print(func(12))  # 输出 144

6.5.3 汉诺塔

def hanoi(n, x, y, z):
    if n == 1:
        print(x + '->' + z)
    else:
        hanoi(n - 1, x, z, y)
        print(x + '->' + z)
        hanoi(n - 1, y, x, z)

n = int(input('输出层数:'))
hanoi(n, 'X', 'Y', 'Z')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值