python教程19-递归的基本使用、递归的练习、匿名函数、sort方法的使用

    科技2022-07-13  125

    python教程——小白入门2020/10/04

    学习目标

    文章目录

    python教程——小白入门2020/10/04P121 递归的基本使用P122 递归的练习P123 匿名函数的使用介绍P124 sort方法的使用

    P121 递归的基本使用

    # 递归函数的基本使用 # author by Zhang_jiongjiong # 简单来说,就是函数内部自己调用自己 # def test(): # print('test') # test() # 递归最重要的就是找到出口(停止的条件) count = 0 def tell_story(): global count # 函数内部想要使用外部函数,需要使用global count += 1 print('从前有座山') print('山上有座庙') print('庙里有个老和尚') print('还有一个小和尚') print('老和尚在给小和尚讲故事') print('故事的内容是:') if count <= 3: tell_story() tell_story() # 求1-n的和 x = int(input("请输入想要求的数值范围:")) summ = 0 def get_sum(): global summ global x while x >= 1: summ += x x -= 1 return summ y = get_sum() print(y) # 请输入想要求的数值范围:5 # 15 def get_sum(n): if n == 0: return 0 return n + get_sum(n - 1) print(get_sum(5)) # 15

    P122 递归的练习

    # 递归的练习 # author by Zhang_jiongjiong # 使用递归求 n! def factorial(n): if n == 0: return 1 return n * factorial(n - 1) print(factorial(6)) # 720 # 使用递归求解斐波那契数列的第n个数字 def fibonacci(n): if n == 1 or n == 2 : return 1 return fibonacci(n-2) + fibonacci(n-1) print(fibonacci(6)) # 8

    P123 匿名函数的使用介绍

    # 匿名函数的使用介绍 # author by Zhang_jiongjiong def add(a, b): return a + b x = add(4, 5) # 9 # 函数名(实参)作用是调用函数,获取到函数的执行结果 # 并赋值给变量x print(x) print('0x%X' % id(add)) # 0x26BE6EB5CA8 fn = add # 相当于是给函数add起了一个艺名 print('0x%X' % id(fn)) # 0x26BE6EB5CA8 print(fn(3, 7)) # 10 # ------------------------------------------------------------------------ # 除了使用def关键字定义一个函数以外,含可以使用lambda 表达式定义一个函数 #  匿名函数:用来表达一个简单的函数,函数调用的次数很少,编程中很少使用 lambda a, b: a + b # 与上方add函数功能相同 # 调用匿名函数的两种方式: # 1.给它定义一个名字 mul = lambda a, b: a * b print(mul(4, 5)) # 20 # 2.把这个函数当作参数,传给另一个函数使用,使用场景比较多 def calc(a, b,fn): c = fn(a, b) return c def add(x, y): return x + y def jian(x, y): return x - y def cheng(x, y): return x * y def chu(x, y): return x / y #-------两种形式的练习--------------- print(calc(20, 4,add)) print(calc(20,4,jian)) print(calc(20,4,cheng)) print(calc(20,4,chu)) x5 = calc(20,4,lambda x,y:x//y) print(x5) # 24 # 16 # 80 # 5.0 # 5

    P124 sort方法的使用

    # sort方法的使用 # author by Zhang_jiulong # 有几个内置函数和内置类,用到了匿名函数 nums = [1, 2, 4, 6, 7, 8] ints = (5, 9, 2, 1, 3, 8, 7, 4) # 列表的sort方法,会直接对列表进行排序 # nums.sort() # [1, 2, 4, 6, 7, 8] #  sorted()内置函数,不会改变原有的数据,而是生成一个新的-列表- x = sorted(ints) # [1, 2, 4, 6, 7, 8] print(x) students = [ {'name': 'zhangsan', 'age': 22, 'score': 98, 'height': 180}, {'name': 'lisi', 'age': 21, 'score': 97, 'height': 185}, {'name': 'jack', 'age': 22, 'score': 100, 'height': 175}, {'name': 'tony', 'age': 23, 'score': 90, 'height': 176}, {'name': 'herry', 'age': 20, 'score': 95, 'height': 172} ] # students.sort() # print(students) # 会报错。因为在进行比较时没有标准,用哪个value进行比较,字典没有可比性,缺少比较规则 # TypeError: '<' not supported between instances of 'dict' and 'dict' def foo(x): # print('x = {}'.format(x)) # x = {'name': 'zhangsan', 'age': 22, 'score': 98, 'height': 180} # x = {'name': 'lisi', 'age': 21, 'score': 97, 'height': 185} # x = {'name': 'jack', 'age': 22, 'score': 100, 'height': 175} # x = {'name': 'tony', 'age': 23, 'score': 90, 'height': 176} # x = {'name': 'herry', 'age': 20, 'score': 95, 'height': 172} return x['age'] # 通过返回值告诉sort方法,按照元素的哪一个属性进行排序 # [{'name': 'zhangsan', 'age': 22, 'score': 98, 'height': 180}, {'name': 'lisi', 'age': 21, 'score': 97, 'height': 185}, {'name': 'jack', 'age': 22, 'score': 100, 'height': 175}, {'name': 'tony', 'age': 23, 'score': 90, 'height': 176}, {'name': 'herry', 'age': 20, 'score': 95, 'height': 172}] # 需要有比较规则key,需要的是一个函数 # TypeError: foo() takes 0 positional arguments but 1 was given # foo这个函数需要0个位置参数,但是在调用的时候传递了一个参数 # students.sort(key=foo) # print(students) # [{'name': 'herry', 'age': 20, 'score': 95, 'height': 172}, {'name': 'lisi', 'age': 21, 'score': 97, 'height': 185}, {'name': 'zhangsan', 'age': 22, 'score': 98, 'height': 180}, {'name': 'jack', 'age': 22, 'score': 100, 'height': 175}, {'name': 'tony', 'age': 23, 'score': 90, 'height': 176}] # 改进后的版本 students.sort(key=lambda y:y['height']) print(students) # [{'name': 'herry', 'age': 20, 'score': 95, 'height': 172}, {'name': 'jack', 'age': 22, 'score': 100, 'height': 175}, {'name': 'tony', 'age': 23, 'score': 90, 'height': 176}, {'name': 'zhangsan', 'age': 22, 'score': 98, 'height': 180}, {'name': 'lisi', 'age': 21, 'score': 97, 'height': 185}]
    Processed: 0.015, SQL: 8