class: center, middle, inverse, title-slide # 函数与模块 ## 🌱 ### 吴燕丰 ### 江西财大,金融学院 ### 2020/09/23 --- ### Python For Loops ```python fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) ``` ``` ## apple ## banana ## cherry ``` ```python for x in "banana": print(x) ``` ``` ## b ## a ## n ## a ## n ## a ``` --- ### The range() Function ```python for x in range(6): print(x) ``` ``` ## 0 ## 1 ## 2 ## 3 ## 4 ## 5 ``` ```python for x in range(2, 6): print(x) ``` ``` ## 2 ## 3 ## 4 ## 5 ``` --- ### The range() Function ```python for x in range(2, 20, 3): print(x) ``` ``` ## 2 ## 5 ## 8 ## 11 ## 14 ## 17 ``` ```python for x in range(6): print(x) else: print("Finally finished!") ``` ``` ## 0 ## 1 ## 2 ## 3 ## 4 ## 5 ## Finally finished! ``` --- ### Nested Loops ```python adj = ["red", "big", "tasty"] fruits = ["apple", "banana", "cherry"] for x in adj: for y in fruits: print(x, y) ``` ``` ## red apple ## red banana ## red cherry ## big apple ## big banana ## big cherry ## tasty apple ## tasty banana ## tasty cherry ``` --- ### 列表生成式 😷 列表生成式**替代**简单的循环 ```python a = [x**2 for x in range(10)] print(a) ``` ``` ## [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] ``` ```python b = [] for x in range(10): b.append(x**2) # 注意缩进! print(b) ``` ``` ## [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] ``` 类似其他: - 生成器 generator - 迭代器 .right[🌱] --- ### 判断 😷 ```python a = 8 if a > 10: print('a > 10') elif a == 9: print('a = 9') else: print('a既不大于10,又不等于9.') ``` ``` ## a既不大于10,又不等于9. ``` |判断|运算符号| |----|:------:| |等于|==| |不等于|!=| |小于| < | |小于等于| <= | |大于| > | |大于等于| >= | --- ### Short Hand If ### 🌰 One line if statement: ```python if a > b: print("a is greater than b") ``` -- ### Short Hand If ... Else ### 🌰 One line if else statement: ```python a = 2 b = 330 print("A") if a > b else print("B") ``` --- ### logical operator **and** ```python a = 200 b = 33 c = 500 if a > b and c > a: print("Both conditions are True") ``` ``` ## Both conditions are True ``` **or** ```python a = 200 b = 33 c = 500 if a > b or a > c: print("At least one of the conditions is True") ``` ``` ## At least one of the conditions is True ``` --- ### Nested If ```python x = 41 if x > 10: print("Above ten,") if x > 20: print("and also above 20!") else: print("but not above 20.") ``` ``` ## Above ten, ## and also above 20! ``` --- ### Python While Loops ```python i = 1 while i < 6: print(i) i += 1 ``` ``` ## 1 ## 2 ## 3 ## 4 ## 5 ``` --- ### The break Statement ```python i = 1 while i < 6: print(i) if i == 3: break i += 1 ``` ``` ## 1 ## 2 ## 3 ``` --- ### The continue Statement ```python i = 0 while i < 6: i += 1 if i == 3: continue print(i) ``` ``` ## 1 ## 2 ## 4 ## 5 ## 6 ``` --- ### The else Statement ```python i = 1 while i < 6: print(i) i += 1 else: print("i is no longer less than 6") ``` ``` ## 1 ## 2 ## 3 ## 4 ## 5 ## i is no longer less than 6 ``` --- ### 函数 ```python def fun(arg1, key='value'): print(arg1) print(key) return fun(1) ``` ``` ## 1 ## value ``` ```python fun(1, 'value2') ``` ``` ## 1 ## value2 ``` --- ### Python Lambda Syntax ```python lambda arguments : expression ``` ```python x = lambda a : a + 10 print(x(5)) ``` ``` ## 15 ``` ```python x = lambda a, b : a * b print(x(5, 6)) ``` ``` ## 30 ``` --- ### Python Scope A variable is only available from inside the region it is created. This is called **scope**. --- ### 模块 Python Modules Consider a module to be the same as a code library. A file containing a set of functions you want to include in your application. .pull-left[ ![](./images/PDF_data_collect.JPG) ] .pull-right[ ![](./images/PDF_processing.JPG) ] --- ### 常见模块 [Python常用模块大全](https://blog.csdn.net/qq_40674583/article/details/81940974) .pull-left[ ![](./images/OS_module.JPG) ] .pull-right[ ![](./images/RE_module.JPG) ![](./images/Random_module.JPG) ] [Python 标准库](https://docs.python.org/zh-cn/3/library/index.html) --- ### 安装第三方模块 如果是通过Anaconda安装的Python,那么,通过操作系统搜索功能,搜索并打开Anaconda Prompt,其界面入下图所示: .center[ ![](./images/AnacondaPrompt.JPG) ] --- ### 安装第三方模块(续) - 在打开的Anaconda Prompt窗口里,输入如下命令后,按`Enter`键,即可安装第三方模块 name_of_module: ```bash pip install name_of_module ``` **注意:** 没有真的模块叫做“name_of_module”,真的模块例如有: + pandas + PyMuPDF - 国内用户,请使用清华大学镜像源<sup>[*]</sup>: [https://pypi.tuna.tsinghua.edu.cn/simple](https://pypi.tuna.tsinghua.edu.cn/simple) ```bash pip install name_of_module -i https://pypi.tuna.tsinghua.edu.cn/simple ``` .footnote[ \[*\]:由于国际网络速度及国家对涉外网络的干预,常常致使直接安装第三方模块变得困难。但是,可以通过国内镜像进行安装,这样网速比较正常。 ] --- ### 测试安装pandas模块 ```bash pip install pandas ``` 如安装截面出现红字,意味着没安装成功,可能的解决方案: [解决方案](../../application/errors_explained/errors_explained.html#pip-install-error)