Skip to content

Commit 4194b11

Browse files
committed
content from day 1 to day 5
0 parents  commit 4194b11

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2206
-0
lines changed

Day01/hello.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
3+
第一个Python程序 - hello, world!
4+
向伟大的Dennis M. Ritchie先生致敬
5+
6+
Version: 0.1
7+
Author: 骆昊
8+
Date: 2018-02-26
9+
10+
请将该文件命名为hello.py并在终端中通过下面的命令运行它
11+
python hello.py
12+
13+
"""
14+
15+
print('hello, world!')
16+
# print("你好,世界!")
17+
print('你好', '世界')
18+
print('hello', 'world', sep=', ', end='!')
19+
print('goodbye, world', end='!\n')

Day01/res/python-idle.png

90.8 KB
Loading

Day01/res/python-ipython.png

203 KB
Loading

Day01/res/python-jupyter-1.png

95.1 KB
Loading

Day01/res/python-jupyter-2.png

164 KB
Loading

Day01/res/python-pycharm.png

53.2 KB
Loading

Day01/res/python-sublime.png

96 KB
Loading

Day01/初识Python.md

+253
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
## Day01 - 初识Python
2+
3+
### Python简介
4+
5+
#### Python的历史
6+
7+
1. 1989年圣诞节:Guido von Rossum开始写Python语言的编译器。
8+
2. 1991年2月:第一个Python编译器(同时也是解释器)诞生,它是用C语言实现的(后面又出现了Java和C#实现的版本Jython和IronPython,以及PyPy、Brython、Pyston等其他实现),可以调用C语言的库函数。在最早的版本中,Python已经提供了对“类”,“函数”,“异常处理”等构造块的支持,同时提供了“列表”和“字典”等核心数据类型,同时支持以模块为基础的拓展系统。
9+
3. 1994年1月:Python 1.0正式发布。
10+
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的整个开发过程更加透明,社区对开发进度的影响逐渐扩大,生态圈开始慢慢形成。
11+
5. 2008年12月3日:Python 3.0发布,此版不完全兼容之前的Python代码,不过很多新特性后来也被移植到旧的Python 2.6/2.7版本,因为目前还有公司在项目和运维中使用Python 2.x版本的代码。
12+
13+
目前我们使用的Python 3.6.x的版本是在2016年的12月23日发布的,Python的版本号分为三段,形如A.B.C。其中A表示大版本号,一般当整体重写,或出现不向后兼容的改变时,增加A;B表示功能更新,出现新功能时增加B;C表示小的改动(如修复了某个Bug),只要有修改就增加C。如果对Python的历史感兴趣,可以查看一篇名为[《Python简史》](http://www.cnblogs.com/vamei/archive/2013/02/06/2892628.html)的博文。
14+
15+
#### Python的优缺点
16+
17+
Python的优点很多,简单的可以总结为以下几点。
18+
19+
1. 简单和明确,做一件事只有一种方法。
20+
2. 学习曲线低,与其他很多语言比上手更容易。
21+
3. 开放源代码,拥有强大的社区和生态圈。
22+
4. 解释型语言,完美的平台可移植性。
23+
5. 支持两种主流的编程范式,可以使用面向对象和函数式编程。
24+
6. 可扩展性和可嵌入性,可以调用C/C++代码也可以在C/C++中调用。
25+
7. 代码规范程度高,可读性强,适合有代码洁癖和强迫症的人群。
26+
27+
Python的缺点主要集中在以下几点。
28+
29+
1. 执行效率低下,因此计算密集型任务可以由C/C++编写。
30+
2. 代码无法加密,但是现在的公司很多都不是卖软件而是卖服务,这个问题慢慢会淡化。
31+
3. 在开发时可以选择的框架太多,有选择的地方就有错误。
32+
33+
#### Python的应用领域
34+
35+
目前Python在云基础设施、DevOps、网络爬虫开发、数据分析挖掘、机器学习等领域都有着广泛的应用,因此也产生了服务器开发、数据接口开发、自动化运维、科学计算和数据可视化、聊天机器人开发、图像识别和处理等一系列的职位。
36+
37+
### 搭建编程环境
38+
39+
#### Windows环境
40+
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修复工具进行修复。
42+
43+
#### Linux环境
44+
45+
Linux环境自带了Python 2.x版本,但是如果要更新到3.x的版本,可以在[Python的官方网站](https://www.python.org)下载Python的源代码并通过源代码构建安装的方式进行安装,具体的步骤如下所示。
46+
47+
安装依赖库(因为没有这些依赖库可能在源代码构件安装时因为缺失底层依赖库而失败)。
48+
49+
```Shell
50+
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
51+
```
52+
53+
下载Python源代码并解压缩到指定目录。
54+
55+
```Shell
56+
wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
57+
xz -d Python-3.6.1.tar.xz
58+
tar -xvf Python-3.6.1.tar
59+
```
60+
61+
切换至Python源代码目录并执行下面的命令进行配置和安装。
62+
63+
```Shell
64+
cd Python-3.6.1
65+
./configure --prefix=/usr/local/python3.6 --enable-optimizations
66+
make && make install
67+
```
68+
69+
创建软链接,这样就可以直接通过python3直接启动Python解释器。
70+
71+
```Shell
72+
ln -s /usr/local/python3.6/bin/python3 /usr/bin/python3
73+
```
74+
75+
76+
#### MacOS环境
77+
78+
MacOS也是自带了Python 2.x版本的,可以通过[Python的官方网站](https://www.python.org)提供的安装文件(pkg文件)安装3.x的版本。默认安装完成后,可以通过在终端执行python命令来启动2.x版本的Python解释器,可以通过执行python3命令来启动3.x版本的Python解释器,当然也可以通过重新设置软链接来修改启动Python解释器的命令。
79+
80+
### 从终端运行Python程序
81+
82+
#### 确认Python的版本
83+
84+
在终端或命令行提示符中键入下面的命令。
85+
86+
```Shell
87+
python --version
88+
```
89+
当然也可以先输入python进入交互式环境,再执行以下的代码检查Python的版本。
90+
91+
```Python
92+
import sys
93+
94+
print(sys.version_info)
95+
print(sys.version)
96+
```
97+
98+
#### 编写Python源代码
99+
100+
可以用文本编辑工具(推荐使用Sublime、Atom、TextMate、VSCode等高级文本编辑工具)编写Python源代码并将其命名为hello.py保存起来,代码内容如下所示。
101+
102+
```Python
103+
print('hello, world!')
104+
```
105+
106+
#### 运行程序
107+
108+
切换到源代码所在的目录并执行下面的命令,看看屏幕上是否输出了"hello, world!"。
109+
110+
```Shell
111+
python hello.py
112+
```
113+
114+
### 代码中的注释
115+
116+
注释是编程语言的一个重要组成部分,用于在源代码中解释代码的作用从而增强程序的可读性和可维护性,当然也可以将源代码中不需要参与运行的代码段通过注释来去掉,这一点在调试程序的时候经常用到。注释在随源代码进入预处理器或编译时会被移除,不会在目标代码中保留也不会影响程序的执行结果。
117+
118+
1. 单行注释 - 以#和空格开头的部分
119+
2. 多行注释 - 三个引号开头,三个引号结尾
120+
121+
```Python
122+
"""
123+
124+
第一个Python程序 - hello, world!
125+
向伟大的Dennis M. Ritchie先生致敬
126+
127+
Version: 0.1
128+
Author: 骆昊
129+
Date: 2018-02-26
130+
131+
"""
132+
133+
print('hello, world!')
134+
# print("你好,世界!")
135+
print('你好', '世界')
136+
print('hello', 'world', sep=', ', end='!')
137+
print('goodbye, world', end='!\n')
138+
```
139+
140+
### 其他工具介绍
141+
142+
#### IDLE - 自带的集成开发工具
143+
144+
IDLE是安装Python环境时自带的集成开发工具,如下图所示。但是由于IDLE的用户体验并不是那么好所以很少在实际开发中被采用。
145+
146+
![](./res/python-idle.png)
147+
148+
#### IPython - 更好的交互式编程工具
149+
150+
IPython是一种基于Python的交互式解释器。相较于原生的Python Shell,IPython提供了更为强大的编辑和交互功能。可以通过Python的包管理工具pip安装IPython和Jupyter,具体的操作如下所示。
151+
152+
```Shell
153+
pip install ipython jupyter
154+
```
155+
156+
或者
157+
158+
```Shell
159+
python -m pip install ipython jupyter
160+
```
161+
162+
安装成功后,可以通过下面的ipython命令启动IPython,如下图所示。
163+
164+
![](./res/python-ipython.png)
165+
166+
当然我们也可以通过Jupyter运行名为notebook的项目在浏览器窗口中进行交互式操作。
167+
168+
```Shell
169+
jupyter notebook
170+
```
171+
172+
![](./res/python-jupyter-1.png)
173+
174+
![](./res/python-jupyter-2.png)
175+
176+
#### Sublime - 文本编辑神器
177+
178+
![](./res/python-sublime.png)
179+
180+
- 首先可以通过[官方网站](https://www.sublimetext.com/)下载安装程序安装Sublime 3或Sublime 2。
181+
182+
- 安装包管理工具。通过快捷键Ctrl+`或者在View菜单中选择Show Console打开控制台,输入下面的代码。
183+
184+
- Sublime 3
185+
186+
```Python
187+
import urllib.request,os;pf='Package Control.sublime-package';ipp=sublime.installed_packages_path();urllib.request.install_opener(urllib.request.build_opener(urllib.request.ProxyHandler()));open(os.path.join(ipp,pf),'wb').write(urllib.request.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read())
188+
```
189+
190+
- Sublime 2
191+
192+
```Python
193+
import urllib2,os;pf='Package Control.sublime-package';ipp=sublime.installed_packages_path();os.makedirs(ipp)ifnotos.path.exists(ipp)elseNone;urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler()));open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read());print('Please restart Sublime Text to finish installation')
194+
```
195+
196+
- 安装插件。通过Preference菜单的Package Control或快捷键Ctrl+Shift+P打开命令面板,在面板中输入Install Package就可以找到安装插件的工具,然后再查找需要的插件。我们推荐大家安装以下几个插件。
197+
198+
- SublimeCodeIntel - 代码自动补全工具插件
199+
- Emmet - 前端开发代码模板插件
200+
- Git - 版本控制工具插件
201+
- Python PEP8 Autoformat - PEP8规范自动格式化插件
202+
- ConvertToUTF8 - 将本地编码转换为UTF-8
203+
204+
#### PyCharm - Python开发神器
205+
206+
PyCharm的安装、配置和使用我们在后面会进行介绍。
207+
208+
![](./res/python-pycharm.png)
209+
210+
### 练习
211+
212+
1. 在Python交互环境中下面的代码查看结果并将内容翻译成中文。
213+
214+
```Python
215+
import this
216+
217+
Beautiful is better than ugly.
218+
Explicit is better than implicit.
219+
Simple is better than complex.
220+
Complex is better than complicated.
221+
Flat is better than nested.
222+
Sparse is better than dense.
223+
Readability counts.
224+
Special cases aren't special enough to break the rules.
225+
Although practicality beats purity.
226+
Errors should never pass silently.
227+
Unless explicitly silenced.
228+
In the face of ambiguity, refuse the temptation to guess.
229+
There should be one-- and preferably only one --obvious way to do it.
230+
Although that way may not be obvious at first unless you're Dutch.
231+
Now is better than never.
232+
Although never is often better than *right* now.
233+
If the implementation is hard to explain, it's a bad idea.
234+
If the implementation is easy to explain, it may be a good idea.
235+
Namespaces are one honking great idea -- let's do more of those!
236+
```
237+
238+
2. 学习使用turtle在屏幕上绘制图形。
239+
240+
```Python
241+
import turtle
242+
243+
turtle.pensize(4)
244+
turtle.pencolor('red')
245+
turtle.forward(100)
246+
turtle.right(90)
247+
turtle.forward(100)
248+
turtle.right(90)
249+
turtle.forward(100)
250+
turtle.right(90)
251+
turtle.forward(100)
252+
turtle.mainloop()
253+
```

Day02/centigrade.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
3+
将华氏温度转换为摄氏温度
4+
F = 1.8C + 32
5+
6+
Version: 0.1
7+
Author: 骆昊
8+
Date: 2018-02-27
9+
10+
"""
11+
12+
f = float(input('请输入华氏温度: '))
13+
c = (f - 32) / 1.8
14+
print('%.1f华氏度 = %.1f摄氏度' % (f, c))

Day02/circle.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
3+
输入半径计算圆的周长和面积
4+
5+
Version: 0.1
6+
Author: 骆昊
7+
Date: 2018-02-27
8+
9+
"""
10+
11+
import math
12+
13+
radius = float(input('请输入圆的半径: '))
14+
perimeter = 2 * math.pi * radius
15+
area = math.pi * radius * radius
16+
print('周长: %.2f' % perimeter)
17+
print('面积: %.2f' % area)

Day02/leap.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
3+
输入年份 如果是闰年输出True 否则输出False
4+
5+
Version: 0.1
6+
Author: 骆昊
7+
Date: 2018-02-27
8+
9+
"""
10+
11+
year = int(input('请输入年份: '))
12+
# 如果代码太长写成一行不便于阅读 可以使用\或()折行
13+
is_leap = (year % 4 == 0 and year % 100 != 0 or
14+
year % 400 == 0)
15+
print(is_leap)

Day02/operator.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
3+
运算符的使用
4+
5+
Version: 0.1
6+
Author: 骆昊
7+
Date: 2018-02-27
8+
9+
"""
10+
11+
a = 5
12+
b = 10
13+
c = 3
14+
d = 4
15+
e = 5
16+
a += b
17+
a -= c
18+
a *= d
19+
a /= e
20+
print("a = ", a)
21+
22+
flag1 = 3 > 2
23+
flag2 = 2 < 1
24+
flag3 = flag1 and flag2
25+
flag4 = flag1 or flag2
26+
flag5 = not flag1
27+
print("flag1 = ", flag1)
28+
print("flag2 = ", flag2)
29+
print("flag3 = ", flag3)
30+
print("flag4 = ", flag4)
31+
print("flag5 = ", flag5)
32+
print(flag1 is True)
33+
print(flag2 is not False)

Day02/string.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
3+
字符串常用操作
4+
5+
Version: 0.1
6+
Author: 骆昊
7+
Date: 2018-02-27
8+
9+
"""
10+
11+
str1 = 'hello, world!'
12+
print('字符串的长度是:', len(str1))
13+
print('单词首字母大写: ', str1.title())
14+
print('字符串变大写: ', str1.upper())
15+
# str1 = str1.upper()
16+
print('字符串是不是大写: ', str1.isupper())
17+
print('字符串是不是以hello开头: ', str1.startswith('hello'))
18+
print('字符串是不是以hello结尾: ', str1.endswith('hello'))
19+
print('字符串是不是以感叹号开头: ', str1.startswith('!'))
20+
print('字符串是不是一感叹号结尾: ', str1.endswith('!'))
21+
str2 = '- \u9a86\u660a'
22+
str3 = str1.title() + ' ' + str2.lower()
23+
print(str3)

0 commit comments

Comments
 (0)