Skip to content

Commit cd529ff

Browse files
committed
更新了文档和代码
1 parent c2895d7 commit cd529ff

21 files changed

+418
-62
lines changed

Day01-15/01.初识Python.md

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#### Python的历史
66

77
1. 1989年圣诞节:Guido von Rossum开始写Python语言的编译器。
8-
2. 1991年2月:第一个Python编译器(同时也是解释器)诞生,它是用C语言实现的(后面又出现了Java和C#实现的版本Jython和IronPython,以及PyPy、Brython、Pyston等其他实现),可以调用C语言的库函数。在最早的版本中,Python已经提供了对“类”,“函数”,“异常处理”等构造块的支持,同时提供了“列表”和“字典”等核心数据类型,同时支持以模块为基础的拓展系统
8+
2. 1991年2月:第一个Python编译器(同时也是解释器)诞生,它是用C语言实现的(后面又出现了Java和C#实现的版本Jython和IronPython,以及PyPy、Brython、Pyston等其他实现),可以调用C语言的库函数。在最早的版本中,Python已经提供了对“类”,“函数”,“异常处理”等构造块的支持,同时提供了“列表”和“字典”等核心数据类型,同时支持以模块为基础来构造应用程序
99
3. 1994年1月:Python 1.0正式发布。
1010
4. 2000年10月16日:Python 2.0发布,增加了实现完整的[垃圾回收](https://zh.wikipedia.org/wiki/%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6_(%E8%A8%88%E7%AE%97%E6%A9%9F%E7%A7%91%E5%AD%B8)),提供了对[Unicode](https://zh.wikipedia.org/wiki/Unicode)的支持。与此同时,Python的整个开发过程更加透明,社区对开发进度的影响逐渐扩大,生态圈开始慢慢形成。
1111
5. 2008年12月3日:Python 3.0发布,它并不完全兼容之前的Python代码,不过因为目前还有不少公司在项目和运维中使用Python 2.x版本,所以Python 3.x的很多新特性后来也被移植到Python 2.6/2.7版本中。
@@ -27,18 +27,18 @@ Python的优点很多,简单的可以总结为以下几点。
2727
Python的缺点主要集中在以下几点。
2828

2929
1. 执行效率稍低,因此计算密集型任务可以由C/C++编写。
30-
2. 代码无法加密,但是现在的公司很多都不是卖软件而是卖服务,这个问题会被淡化。
30+
2. 代码无法加密,但是现在很多公司都不销售卖软件而是销售服务,这个问题会被淡化。
3131
3. 在开发时可以选择的框架太多(如Web框架就有100多个),有选择的地方就有错误。
3232

3333
#### Python的应用领域
3434

35-
目前Python在云基础设施、DevOps、网络爬虫开发、数据分析挖掘、机器学习等领域都有着广泛的应用,因此也产生了Web后端开发、数据接口开发、自动化运维、自动化测试、科学计算和可视化、数据分析、量化交易、机器人开发、图像识别和处理等一系列的职位。
35+
目前Python在Web应用开发、云基础设施、DevOps、网络爬虫开发、数据分析挖掘、机器学习等领域都有着广泛的应用,因此也产生了Web后端开发、数据接口开发、自动化运维、自动化测试、科学计算和可视化、数据分析、量化交易、机器人开发、图像识别和处理等一系列的职位。
3636

3737
### 搭建编程环境
3838

3939
#### Windows环境
4040

41-
可以在[Python官方网站](https://www.python.org)下载到Python的Windows安装程序(exe文件),需要注意的是如果在Windows 7环境下安装需要先安装Service Pack 1补丁包(可以通过一些工具软件自动安装系统补丁的功能来安装),安装过程建议勾选“Add Python 3.6 to PATH”(将Python 3.6添加到PATH环境变量)并选择自定义安装,在设置“Optional Features”界面最好将“pip”、“tcl/tk”、“Python test suite”等项全部勾选上。强烈建议使用自定义的安装路径并保证路径中没有中文。安装完成会看到“Setup was successful”的提示,但是在启动Python环境时可能会因为缺失一些动态链接库文件而导致Python解释器无法运行,常见的问题主要是api-ms-win-crt\*.dll缺失以及更新DirectX之后导致某些动态链接库文件缺失,前者可以参照[《api-ms-win-crt\*.dll缺失原因分析和解决方法》]()一文讲解的方法进行处理或者直接在[微软官网](https://www.microsoft.com/zh-cn/download/details.aspx?id=48145)下载Visual C++ Redistributable for Visual Studio 2015文件进行修复,后者可以下载一个DirectX修复工具进行修复
41+
可以在[Python官方网站](https://www.python.org)下载到Python的Windows安装程序(exe文件),需要注意的是如果在Windows 7环境下安装Python 3.x,需要先安装Service Pack 1补丁包(可以通过一些工具软件自动安装系统补丁的功能来安装),安装过程建议勾选“Add Python 3.6 to PATH”(将Python 3.6添加到PATH环境变量)并选择自定义安装,在设置“Optional Features”界面最好将“pip”、“tcl/tk”、“Python test suite”等项全部勾选上。强烈建议使用自定义的安装路径并保证路径中没有中文。安装完成会看到“Setup was successful”的提示。如果稍后运行Python程序时,出现因为缺失一些动态链接库文件而导致Python解释器无法工作的问题,可以按照后面说的方法加以解决。如果系统显示api-ms-win-crt\*.dll文件缺失,可以参照[《api-ms-win-crt\*.dll缺失原因分析和解决方法》]()一文讲解的方法进行处理或者直接在[微软官网](https://www.microsoft.com/zh-cn/download/details.aspx?id=48145)下载Visual C++ Redistributable for Visual Studio 2015文件进行修复;如果是因为更新Windows的DirectX之后导致某些动态链接库文件缺失问题,可以下载一个DirectX修复工具进行修复
4242

4343
#### Linux环境
4444

@@ -85,20 +85,26 @@ export PATH=$PATH:/usr/local/python37/bin
8585
source .bash_profile
8686
```
8787

88-
#### MacOS环境
88+
#### macOS环境
8989

90-
MacOS也是自带了Python 2.x版本的,可以通过[Python的官方网站](https://www.python.org)提供的安装文件(pkg文件)安装3.x的版本。默认安装完成后,可以通过在终端执行python命令来启动2.x版本的Python解释器,可以通过执行python3命令来启动3.x版本的Python解释器。
90+
macOS也自带了Python 2.x版本,可以通过[Python的官方网站](https://www.python.org)提供的安装文件(pkg文件)安装Python 3.x的版本。默认安装完成后,可以通过在终端执行python命令来启动2.x版本的Python解释器,可以通过执行python3命令来启动3.x版本的Python解释器。
9191

9292
### 从终端运行Python程序
9393

9494
#### 确认Python的版本
9595

96-
在终端或命令行提示符中键入下面的命令
96+
可以Windows的命令行提示符中键入下面的命令
9797

9898
```Shell
9999
python --version
100100
```
101-
当然也可以先输入python进入交互式环境,再执行以下的代码检查Python的版本。
101+
或者是在Linux或macOS系统的终端中键入下面的命令。
102+
103+
```Shell
104+
python3 --version
105+
```
106+
107+
当然也可以先输入python或python3进入交互式环境,再执行以下的代码检查Python的版本。
102108

103109
```Python
104110
import sys
@@ -109,7 +115,7 @@ print(sys.version)
109115

110116
#### 编写Python源代码
111117

112-
可以用文本编辑工具(推荐使用Sublime、Atom、TextMate、VSCode等高级文本编辑工具)编写Python源代码并将其命名为hello.py保存起来,代码内容如下所示。
118+
可以用文本编辑工具(推荐使用Sublime、TextMate、Visual Studio Code等高级文本编辑工具)编写Python源代码并用py作为后缀名保存该文件,代码内容如下所示。
113119

114120
```Python
115121
print('hello, world!')
@@ -123,6 +129,12 @@ print('hello, world!')
123129
python hello.py
124130
```
125131

132+
133+
134+
```Shell
135+
python3 hello.py
136+
```
137+
126138
### 代码中的注释
127139

128140
注释是编程语言的一个重要组成部分,用于在源代码中解释代码的作用从而增强程序的可读性和可维护性,当然也可以将源代码中不需要参与运行的代码段通过注释来去掉,这一点在调试程序的时候经常用到。注释在随源代码进入预处理器或编译时会被移除,不会在目标代码中保留也不会影响程序的执行结果。
@@ -156,29 +168,42 @@ IDLE是安装Python环境时自带的集成开发工具,如下图所示。但
156168

157169
#### IPython - 更好的交互式编程工具
158170

159-
IPython是一种基于Python的交互式解释器。相较于原生的Python Shell,IPython提供了更为强大的编辑和交互功能。可以通过Python的包管理工具pip安装IPython和Jupyter,具体的操作如下所示。
171+
IPython是一种基于Python的交互式解释器。相较于原生的Python交互式环境,IPython提供了更为强大的编辑和交互功能。可以通过Python的包管理工具pip安装IPython和Jupyter,具体的操作如下所示。
160172

161173
```Shell
162174
pip install ipython
163175
```
164176

165-
或者
177+
166178

167179
```Shell
168-
python -m pip install ipython
180+
pip3 install ipython
169181
```
170182

171183
安装成功后,可以通过下面的ipython命令启动IPython,如下图所示。
172184

173185
![](./res/python-ipython.png)
174186

175-
当然我们也可以通过安装Jupyter并运行名为notebook的程序在浏览器窗口中进行交互式代码编写操作
187+
当然,我们也可以通过安装Jupyter工具并运行名为notebook的程序在浏览器窗口中进行交互式代码编写操作
176188

177189
```Shell
178190
pip install jupyter
191+
```
192+
193+
194+
195+
```Shell
196+
pip3 intall jupyter
197+
```
198+
199+
然后执行下面的命令:
200+
201+
```Shell
179202
jupyter notebook
180203
```
181204

205+
206+
182207
![](./res/python-jupyter-2.png)
183208

184209
#### Sublime - 文本编辑神器
@@ -213,6 +238,8 @@ jupyter notebook
213238
- Python PEP8 Autoformat - PEP8规范自动格式化插件。
214239
- ConvertToUTF8 - 将本地编码转换为UTF-8。
215240

241+
> 说明:事实上Visual Studio Code可能是更好的选择,它不用花钱并提供了更为完整和强大的功能。
242+
216243
#### PyCharm - Python开发神器
217244

218245
PyCharm的安装、配置和使用在[《玩转PyCharm》](../玩转PyCharm.md)进行了介绍,有兴趣的读者可以选择阅读。
@@ -221,34 +248,38 @@ PyCharm的安装、配置和使用在[《玩转PyCharm》](../玩转PyCharm.md)
221248

222249
### 练习
223250

224-
1. 在Python交互环境中查看下面的代码结果,并将内容翻译成中文
251+
1. 在Python交互环境中查看下面的代码结果,并试着将这些内容翻译成中文
225252

226253
```Python
227254
import this
228-
229-
Beautiful is better than ugly.
230-
Explicit is better than implicit.
231-
Simple is better than complex.
232-
Complex is better than complicated.
233-
Flat is better than nested.
234-
Sparse is better than dense.
235-
Readability counts.
236-
Special cases aren't special enough to break the rules.
237-
Although practicality beats purity.
238-
Errors should never pass silently.
239-
Unless explicitly silenced.
240-
In the face of ambiguity, refuse the temptation to guess.
241-
There should be one-- and preferably only one --obvious way to do it.
242-
Although that way may not be obvious at first unless you're Dutch.
243-
Now is better than never.
244-
Although never is often better than *right* now.
245-
If the implementation is hard to explain, it's a bad idea.
246-
If the implementation is easy to explain, it may be a good idea.
247-
Namespaces are one honking great idea -- let's do more of those!
248255
```
249256

257+
> 说明:当前键入上面的命令后会在交互式环境中看到如下所示的输出,这段内容被称为“Python之禅”,里面讲述的道理不仅仅适用于Python,也适用于其他编程语言。
258+
>
259+
> Beautiful is better than ugly.
260+
> Explicit is better than implicit.
261+
> Simple is better than complex.
262+
> Complex is better than complicated.
263+
> Flat is better than nested.
264+
> Sparse is better than dense.
265+
> Readability counts.
266+
> Special cases aren't special enough to break the rules.
267+
> Although practicality beats purity.
268+
> Errors should never pass silently.
269+
> Unless explicitly silenced.
270+
> In the face of ambiguity, refuse the temptation to guess.
271+
> There should be one-- and preferably only one --obvious way to do it.
272+
> Although that way may not be obvious at first unless you're Dutch.
273+
> Now is better than never.
274+
> Although never is often better than \*right\* now.
275+
> If the implementation is hard to explain, it's a bad idea.
276+
> If the implementation is easy to explain, it may be a good idea.
277+
> Namespaces are one honking great idea -- let's do more of those!
278+
250279
2. 学习使用turtle在屏幕上绘制图形。
251280

281+
> 说明:turtle是Python内置的一个非常有趣的模块,特别适用于让小朋友体会什么是编程,它最早是Logo语言的一部分,Logo语言是Wally Feurzig和Seymour Papert在1966发明的编程语言.
282+
252283
```Python
253284
import turtle
254285

Day01-15/02.语言元素.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ print(a / b)
5050
print(a // b)
5151
print(a % b)
5252
print(a ** b)
53-
5453
```
5554

5655
```Python
@@ -72,7 +71,6 @@ print('%d / %d = %f' % (a, b, a / b))
7271
print('%d // %d = %d' % (a, b, a // b))
7372
print('%d %% %d = %d' % (a, b, a % b))
7473
print('%d ** %d = %d' % (a, b, a ** b))
75-
7674
```
7775

7876
```Python
@@ -94,7 +92,6 @@ print(type(b))
9492
print(type(c))
9593
print(type(d))
9694
print(type(e))
97-
9895
```
9996

10097
在对变量类型进行转换时可以使用Python的内置函数(准确的说下面列出的并不是真正意义上的函数,而是后面我们要讲到的创建对象的构造方法)。
@@ -126,7 +123,7 @@ Python支持多种运算符,下表大致按照优先级从高到低的顺序
126123
| `not` `or` `and` | 逻辑运算符 |
127124
| `=` `+=` `-=` `*=` `/=` `%=` `//=` `**=` `&=` `\|=` `^=` `>>=` `<<=` | (复合)赋值运算符 |
128125

129-
>**说明:** 在实际开发中,如果搞不清楚优先级可以使用括号来确保运算的执行顺序
126+
>**说明:** 在实际开发中,如果搞不清楚运算符的优先级,可以使用括号来确保运算的执行顺序
130127
131128
下面的例子演示了运算符的使用。
132129

@@ -161,7 +158,6 @@ print("flag4 = ", flag4)
161158
print("flag5 = ", flag5)
162159
print(flag1 is True)
163160
print(flag2 is not False)
164-
165161
```
166162

167163
### 练习
@@ -180,7 +176,6 @@ Author: 骆昊
180176
f = float(input('请输入华氏温度: '))
181177
c = (f - 32) / 1.8
182178
print('%.1f华氏度 = %.1f摄氏度' % (f, c))
183-
184179
```
185180

186181
#### 练习2:输入圆的半径计算计算周长和面积。
@@ -200,7 +195,6 @@ perimeter = 2 * math.pi * radius
200195
area = math.pi * radius * radius
201196
print('周长: %.2f' % perimeter)
202197
print('面积: %.2f' % area)
203-
204198
```
205199

206200
#### 练习3:输入年份判断是不是闰年。

Day01-15/code/.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "/Library/Frameworks/Python.framework/Versions/3.7/bin/python3"
3+
}

Day01-15/code/Day01/hello.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
Author: 骆昊
77
Date: 2018-02-26
88
9-
请将该文件命名为hello.py并在终端中通过下面的命令运行它
9+
请将该文件命名为hello.py
10+
11+
使用Windows的小伙伴可以在命令行提示下通过下面的命令运行该程序
1012
python hello.py
13+
14+
对于使用Linux或macOS的小伙伴可以打开终端并键入下面的命令来运行程序
15+
python3 hello.py
1116
"""
1217

1318
print('hello, world!')

Day01-15/code/Day02/circle.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
Author: 骆昊
66
Date: 2018-02-27
77
"""
8-
98
import math
109

1110
radius = float(input('请输入圆的半径: '))

Day01-15/code/Day03/grade.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
22
百分制成绩转等级制成绩
3-
90分以上 --> A
4-
80分~89分 --> B
5-
70分~79分 --> C
6-
60分~69分 --> D
7-
60分以下 --> E
3+
90分以上,输出A
4+
80分~89分,输出B
5+
70分~79分,输出C
6+
60分~69分,输出D
7+
60分以下,输出E
88
99
Version: 0.1
1010
Author: 骆昊

Day01-15/code/Day03/rolldice.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
Author: 骆昊
66
Date: 2018-02-28
77
"""
8-
98
from random import randint
109

1110
face = randint(1, 6)

Day01-15/code/Day03/triangle.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
Author: 骆昊
77
Date: 2018-02-28
88
"""
9-
109
import math
1110

1211
a = float(input('a = '))

Day01-15/code/Day03/verify.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
Author: 骆昊
66
Date: 2018-02-28
77
"""
8-
98
# import getpass
109
# from getpass import getpass
1110
# from getpass import *

Day01-15/code/Day04/for4.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
Author: 骆昊
66
Date: 2018-03-01
77
"""
8-
98
from math import sqrt
109

1110
num = int(input('请输入一个正整数: '))

Day01-15/code/Day04/while2.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
Date: 2018-03-01
77
"""
88

9-
sum = 0
10-
num = 2
9+
sum, num = 0, 2
1110
while num <= 100:
1211
sum += num
1312
num += 2

Day01-15/code/Day05/chicken.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,3 @@
1313
z = 100 - x - y
1414
if 5 * x + 3 * y + z / 3 == 100:
1515
print('公鸡: %d只, 母鸡: %d只, 小鸡: %d只' % (x, y, z))
16-
17-
# 要理解程序背后的算法 - 穷举法

Day01-15/code/Day05/craps.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
Author: 骆昊
1212
Date: 2018-03-02
1313
"""
14-
1514
from random import randint
1615

1716
money = 1000

Day01-15/code/Day05/fibonacci.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
a = 0
1111
b = 1
1212
for _ in range(20):
13-
(a, b) = (b, a + b)
13+
a, b = b, a + b
1414
print(a, end=' ')

Day01-15/code/Day05/guess.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
Author: 骆昊
88
Date: 2018-03-02
99
"""
10-
1110
import random
1211

1312
answer = random.randint(1, 100)

0 commit comments

Comments
 (0)