diff --git a/PythonLanguage/12. 函数与Lambda表达式.md b/PythonLanguage/12. 函数与Lambda表达式.md index 88ad37c..20d222e 100644 --- a/PythonLanguage/12. 函数与Lambda表达式.md +++ b/PythonLanguage/12. 函数与Lambda表达式.md @@ -36,16 +36,6 @@ temp = printme('hello') # hello print(temp) # None ``` -【例子】 -```python -def add(a, b): - print(a + b) - - -add(1, 2) # 3 -add([1, 2, 3], [4, 5, 6]) # [1, 2, 3, 4, 5, 6] -``` - ### 函数文档 ```python @@ -401,18 +391,16 @@ outer() 【例子】`n! = 1 x 2 x 3 x ... x n` -++循环++ + ```python +# 利用循环 n = 5 for k in range(1, 5): n = n * k print(n) # 120 -``` - -++递归++ -```python +# 利用递归 def factorial(n): if n == 1: return 1 @@ -424,9 +412,10 @@ print(factorial(5)) # 120 【例子】斐波那契数列 `f(n)=f(n-1)+f(n-2), f(0)=0 f(1)=1` -++循环++ + ```python +# 利用循环 i = 0 j = 1 lst = list([i, j]) @@ -437,11 +426,8 @@ for k in range(2, 11): j = k print(lst) # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] -``` -++递归++ - -```python +# 利用递归 def recur_fibo(n): if n <= 1: return n @@ -463,6 +449,9 @@ import sys sys.setrecursionlimit(1000) ``` +有关递归的详细介绍参见: +- [利用python解决汉诺塔问题?](https://mp.weixin.qq.com/s?__biz=MzIyNDA1NjA1NQ==&mid=2651010966&idx=1&sn=57c9c6a05cfeaafc6bb88eaee54550a3&chksm=f3e3500ec494d918edc37c3e23997d0fc330152029e7140b82437f453ec9e7bf8d0c500a500d&token=523711417&lang=zh_CN#rd) + --- ## 2. Lambda 表达式 @@ -598,12 +587,17 @@ print(apply_to_list(lambda x: sum(x) / len(x), lst)) # 3.0 ``` + --- **参考文献**: - https://www.runoob.com/python3/python3-tutorial.html - https://www.bilibili.com/video/av4050443 - https://mp.weixin.qq.com/s/gKhXS8JVU8dZBHJF7sIFsw + + + + --- **练习题**: