Python之初(一)
在交换环境里 输入 第一次输入
import this
>>>import this The Zen of Python, by Tim Peters #Python之道 Tim Peters Beautiful is better than ugly. #美好胜于丑陋 Explicit is better than implicit. #清楚胜于含混 Simple is better than complex. #简明胜于复杂 Complex is better than complicated. #复杂胜于晦涩 Flat is better than nested. #平直胜于曲折 Sparse is better than dense. #疏散胜于密集 Readability counts. #重在易读 Special cases aren't special enough to break the rules. #遵守规则,概莫能外 Although practicality beats purity. #纵然实践会击败真理 Errors should never pass silently. #错误却万不容姑息 Unless explicitly silenced. #除非心意已冷 In the face of ambiguity, refuse the temptation to guess.2 #疑窦丛生,拒绝猜测 There should be one-- and preferably only one --obvious way to do it. #宁折不弯,探寻正道 Although that way may not be obvious at first unless you're Dutch. #路漫漫其修远兮,吾将上下而求索 Now is better than never. #悟以往之不谏,知来者之可追 Although never is often better than *right* now. #前路不明,似应放弃 If the implementation is hard to explain, it's a bad idea. #晦涩难懂,弃之可也 If the implementation is easy to explain, it may be a good idea. #简明易懂,方可存留 Namespaces are one honking great idea -- let's do more of those! #命名空间,多多益善
- 编程规范与代码优化建议
- 严格使用缩进来体现代码的逻辑从属关系。
- 每个import语句只导入一个模块,好按照标准库、扩展库、自定义库的顺序进行导入。
- 最好在每个类、函数定义和一段完整的功能代码之后增加一个空行,在运算符两侧增加一个空格,逗号后面增加一个空格。
- 尽量不要写过长的语句。
- 复杂的表达式建议在适当的位置使用括号使得各种运算的隶属关系和顺序更加明确。
- 对关键代码和重要的业务逻辑代码进行必要的注释。
- 在开发速度和运行速度之间尽量取得佳平衡。
- 内置对象快,标准库次之,之后是扩展库。
- 根据运算特点选择合适的数据类型来提高程序的运行效率。
- 充分利用关系运算符以及逻辑运算符and和or的惰性求值的特点,合理组织条件表达式中多个条 件的先后顺序,减少不必要的计算。
- 减少内循环中的无关计算,尽量往外层提取
- pip工具的使用:
pip命令示例 | 说明 |
---|---|
pip download SomePackage[==version] | 下载扩展库的指定版本,不安装 |
pip freeze [> requirements.txt] | 以requirements的格式列出已安装模块 |
pip list | 列出当前已安装的所有模块 |
pip install SomePackage[==version] | 在线安装SomePackage模块的指定版本 |
pip install SomePackage.whl | 通过whl文件离线安装扩展库 |
pip install package1 package2 … | 依次(在线)安装package1、package2等扩展模块 |
pip install -r requirements.txt | 安装requirements.txt文件中指定的扩展库 |
pip install --upgrade SomePackage | 升级SomePackage模块 |
pip uninstall SomePackage[==version] | 卸载SomePackage模块的指定版本 |
使用工具
Anaconda3:Python的Anaconda发行版
预装了150个以上的常用Packages,包括NumPy,pandas,Scipy, Matplotlib, scikit-learn和Ipython库。
包括:Spyder,与jupyter工具,安装后可自行选择使用。
下载地址:https://www.anaconda.com/download/
安装教程:https://blog.csdn.net/u012318074/article/details/77075209/
-
Spyder:https://blog.csdn.net/fu6543210/article/details/79772093
-
jupyter:交互式笔记本,支持运行40多种编程语言。
- Ipython : 安装后cmd命令行 输入ipython即可使用