提交 cd529ffa 编写于 作者: J jackfrued

更新了文档和代码

上级 c2895d7c
......@@ -5,7 +5,7 @@
#### Python的历史
1. 1989年圣诞节:Guido von Rossum开始写Python语言的编译器。
2. 1991年2月:第一个Python编译器(同时也是解释器)诞生,它是用C语言实现的(后面又出现了Java和C#实现的版本Jython和IronPython,以及PyPy、Brython、Pyston等其他实现),可以调用C语言的库函数。在最早的版本中,Python已经提供了对“类”,“函数”,“异常处理”等构造块的支持,同时提供了“列表”和“字典”等核心数据类型,同时支持以模块为基础的拓展系统
2. 1991年2月:第一个Python编译器(同时也是解释器)诞生,它是用C语言实现的(后面又出现了Java和C#实现的版本Jython和IronPython,以及PyPy、Brython、Pyston等其他实现),可以调用C语言的库函数。在最早的版本中,Python已经提供了对“类”,“函数”,“异常处理”等构造块的支持,同时提供了“列表”和“字典”等核心数据类型,同时支持以模块为基础来构造应用程序
3. 1994年1月:Python 1.0正式发布。
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的整个开发过程更加透明,社区对开发进度的影响逐渐扩大,生态圈开始慢慢形成。
5. 2008年12月3日:Python 3.0发布,它并不完全兼容之前的Python代码,不过因为目前还有不少公司在项目和运维中使用Python 2.x版本,所以Python 3.x的很多新特性后来也被移植到Python 2.6/2.7版本中。
......@@ -27,18 +27,18 @@ Python的优点很多,简单的可以总结为以下几点。
Python的缺点主要集中在以下几点。
1. 执行效率稍低,因此计算密集型任务可以由C/C++编写。
2. 代码无法加密,但是现在的公司很多都不是卖软件而是卖服务,这个问题会被淡化。
2. 代码无法加密,但是现在很多公司都不销售卖软件而是销售服务,这个问题会被淡化。
3. 在开发时可以选择的框架太多(如Web框架就有100多个),有选择的地方就有错误。
#### Python的应用领域
目前Python在云基础设施、DevOps、网络爬虫开发、数据分析挖掘、机器学习等领域都有着广泛的应用,因此也产生了Web后端开发、数据接口开发、自动化运维、自动化测试、科学计算和可视化、数据分析、量化交易、机器人开发、图像识别和处理等一系列的职位。
目前Python在Web应用开发、云基础设施、DevOps、网络爬虫开发、数据分析挖掘、机器学习等领域都有着广泛的应用,因此也产生了Web后端开发、数据接口开发、自动化运维、自动化测试、科学计算和可视化、数据分析、量化交易、机器人开发、图像识别和处理等一系列的职位。
### 搭建编程环境
#### Windows环境
可以在[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修复工具进行修复。
可以在[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修复工具进行修复。
#### Linux环境
......@@ -85,20 +85,26 @@ export PATH=$PATH:/usr/local/python37/bin
source .bash_profile
```
#### MacOS环境
#### macOS环境
MacOS也是自带了Python 2.x版本的,可以通过[Python的官方网站](https://www.python.org)提供的安装文件(pkg文件)安装3.x的版本。默认安装完成后,可以通过在终端执行python命令来启动2.x版本的Python解释器,可以通过执行python3命令来启动3.x版本的Python解释器。
macOS也自带了Python 2.x版本,可以通过[Python的官方网站](https://www.python.org)提供的安装文件(pkg文件)安装Python 3.x的版本。默认安装完成后,可以通过在终端执行python命令来启动2.x版本的Python解释器,可以通过执行python3命令来启动3.x版本的Python解释器。
### 从终端运行Python程序
#### 确认Python的版本
在终端或命令行提示符中键入下面的命令。
可以Windows的命令行提示符中键入下面的命令。
```Shell
python --version
```
当然也可以先输入python进入交互式环境,再执行以下的代码检查Python的版本。
或者是在Linux或macOS系统的终端中键入下面的命令。
```Shell
python3 --version
```
当然也可以先输入python或python3进入交互式环境,再执行以下的代码检查Python的版本。
```Python
import sys
......@@ -109,7 +115,7 @@ print(sys.version)
#### 编写Python源代码
可以用文本编辑工具(推荐使用Sublime、Atom、TextMate、VSCode等高级文本编辑工具)编写Python源代码并将其命名为hello.py保存起来,代码内容如下所示。
可以用文本编辑工具(推荐使用Sublime、TextMate、Visual Studio Code等高级文本编辑工具)编写Python源代码并用py作为后缀名保存该文件,代码内容如下所示。
```Python
print('hello, world!')
......@@ -123,6 +129,12 @@ print('hello, world!')
python hello.py
```
```Shell
python3 hello.py
```
### 代码中的注释
注释是编程语言的一个重要组成部分,用于在源代码中解释代码的作用从而增强程序的可读性和可维护性,当然也可以将源代码中不需要参与运行的代码段通过注释来去掉,这一点在调试程序的时候经常用到。注释在随源代码进入预处理器或编译时会被移除,不会在目标代码中保留也不会影响程序的执行结果。
......@@ -156,29 +168,42 @@ IDLE是安装Python环境时自带的集成开发工具,如下图所示。但
#### IPython - 更好的交互式编程工具
IPython是一种基于Python的交互式解释器。相较于原生的Python Shell,IPython提供了更为强大的编辑和交互功能。可以通过Python的包管理工具pip安装IPython和Jupyter,具体的操作如下所示。
IPython是一种基于Python的交互式解释器。相较于原生的Python交互式环境,IPython提供了更为强大的编辑和交互功能。可以通过Python的包管理工具pip安装IPython和Jupyter,具体的操作如下所示。
```Shell
pip install ipython
```
```Shell
python -m pip install ipython
pip3 install ipython
```
安装成功后,可以通过下面的ipython命令启动IPython,如下图所示。
![](./res/python-ipython.png)
当然我们也可以通过安装Jupyter并运行名为notebook的程序在浏览器窗口中进行交互式代码编写操作。
当然,我们也可以通过安装Jupyter工具并运行名为notebook的程序在浏览器窗口中进行交互式代码编写操作。
```Shell
pip install jupyter
```
```Shell
pip3 intall jupyter
```
然后执行下面的命令:
```Shell
jupyter notebook
```
![](./res/python-jupyter-2.png)
#### Sublime - 文本编辑神器
......@@ -213,6 +238,8 @@ jupyter notebook
- Python PEP8 Autoformat - PEP8规范自动格式化插件。
- ConvertToUTF8 - 将本地编码转换为UTF-8。
> 说明:事实上Visual Studio Code可能是更好的选择,它不用花钱并提供了更为完整和强大的功能。
#### PyCharm - Python开发神器
PyCharm的安装、配置和使用在[《玩转PyCharm》](../玩转PyCharm.md)进行了介绍,有兴趣的读者可以选择阅读。
......@@ -221,34 +248,38 @@ PyCharm的安装、配置和使用在[《玩转PyCharm》](../玩转PyCharm.md)
### 练习
1. 在Python交互环境中查看下面的代码结果,并内容翻译成中文。
1. 在Python交互环境中查看下面的代码结果,并试着将这些内容翻译成中文。
```Python
import this
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.
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!
```
> 说明:当前键入上面的命令后会在交互式环境中看到如下所示的输出,这段内容被称为“Python之禅”,里面讲述的道理不仅仅适用于Python,也适用于其他编程语言。
>
> 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.
> 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!
2. 学习使用turtle在屏幕上绘制图形。
> 说明:turtle是Python内置的一个非常有趣的模块,特别适用于让小朋友体会什么是编程,它最早是Logo语言的一部分,Logo语言是Wally Feurzig和Seymour Papert在1966发明的编程语言.
```Python
import turtle
......
......@@ -50,7 +50,6 @@ print(a / b)
print(a // b)
print(a % b)
print(a ** b)
```
```Python
......@@ -72,7 +71,6 @@ print('%d / %d = %f' % (a, b, a / b))
print('%d // %d = %d' % (a, b, a // b))
print('%d %% %d = %d' % (a, b, a % b))
print('%d ** %d = %d' % (a, b, a ** b))
```
```Python
......@@ -94,7 +92,6 @@ print(type(b))
print(type(c))
print(type(d))
print(type(e))
```
在对变量类型进行转换时可以使用Python的内置函数(准确的说下面列出的并不是真正意义上的函数,而是后面我们要讲到的创建对象的构造方法)。
......@@ -126,7 +123,7 @@ Python支持多种运算符,下表大致按照优先级从高到低的顺序
| `not` `or` `and` | 逻辑运算符 |
| `=` `+=` `-=` `*=` `/=` `%=` `//=` `**=` `&=` `\|=` `^=` `>>=` `<<=` | (复合)赋值运算符 |
>**说明:** 在实际开发中,如果搞不清楚优先级可以使用括号来确保运算的执行顺序。
>**说明:** 在实际开发中,如果搞不清楚运算符的优先级,可以使用括号来确保运算的执行顺序。
下面的例子演示了运算符的使用。
......@@ -161,7 +158,6 @@ print("flag4 = ", flag4)
print("flag5 = ", flag5)
print(flag1 is True)
print(flag2 is not False)
```
### 练习
......@@ -180,7 +176,6 @@ Author: 骆昊
f = float(input('请输入华氏温度: '))
c = (f - 32) / 1.8
print('%.1f华氏度 = %.1f摄氏度' % (f, c))
```
#### 练习2:输入圆的半径计算计算周长和面积。
......@@ -200,7 +195,6 @@ perimeter = 2 * math.pi * radius
area = math.pi * radius * radius
print('周长: %.2f' % perimeter)
print('面积: %.2f' % area)
```
#### 练习3:输入年份判断是不是闰年。
......
{
"python.pythonPath": "/Library/Frameworks/Python.framework/Versions/3.7/bin/python3"
}
\ No newline at end of file
......@@ -6,8 +6,13 @@ Version: 0.1
Author: 骆昊
Date: 2018-02-26
请将该文件命名为hello.py并在终端中通过下面的命令运行它
请将该文件命名为hello.py
使用Windows的小伙伴可以在命令行提示下通过下面的命令运行该程序
python hello.py
对于使用Linux或macOS的小伙伴可以打开终端并键入下面的命令来运行程序
python3 hello.py
"""
print('hello, world!')
......
......@@ -5,7 +5,6 @@ Version: 0.1
Author: 骆昊
Date: 2018-02-27
"""
import math
radius = float(input('请输入圆的半径: '))
......
"""
百分制成绩转等级制成绩
90分以上 --> A
80分~89分 --> B
70分~79分 --> C
60分~69分 --> D
60分以下 --> E
90分以上,输出A
80分~89分,输出B
70分~79分,输出C
60分~69分,输出D
60分以下,输出E
Version: 0.1
Author: 骆昊
......
......@@ -5,7 +5,6 @@ Version: 0.1
Author: 骆昊
Date: 2018-02-28
"""
from random import randint
face = randint(1, 6)
......
......@@ -6,7 +6,6 @@ Version: 0.1
Author: 骆昊
Date: 2018-02-28
"""
import math
a = float(input('a = '))
......
......@@ -5,7 +5,6 @@ Version: 0.1
Author: 骆昊
Date: 2018-02-28
"""
# import getpass
# from getpass import getpass
# from getpass import *
......
......@@ -5,7 +5,6 @@ Version: 0.1
Author: 骆昊
Date: 2018-03-01
"""
from math import sqrt
num = int(input('请输入一个正整数: '))
......
......@@ -6,8 +6,7 @@ Author: 骆昊
Date: 2018-03-01
"""
sum = 0
num = 2
sum, num = 0, 2
while num <= 100:
sum += num
num += 2
......
......@@ -13,5 +13,3 @@ for x in range(0, 20):
z = 100 - x - y
if 5 * x + 3 * y + z / 3 == 100:
print('公鸡: %d只, 母鸡: %d只, 小鸡: %d只' % (x, y, z))
# 要理解程序背后的算法 - 穷举法
......@@ -11,7 +11,6 @@ Version: 0.1
Author: 骆昊
Date: 2018-03-02
"""
from random import randint
money = 1000
......
......@@ -10,5 +10,5 @@ Date: 2018-03-02
a = 0
b = 1
for _ in range(20):
(a, b) = (b, a + b)
a, b = b, a + b
print(a, end=' ')
......@@ -7,7 +7,6 @@ Version: 0.1
Author: 骆昊
Date: 2018-03-02
"""
import random
answer = random.randint(1, 100)
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态列表</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
background-color: #000;
color: #fff;
}
#app {
width: 40%;
margin: 20px auto;
}
#fruits>li {
width: 90%;
height: 50px;
background-color: #6ca;
margin: 4px 0;
text-align: center;
font-size: 20px;
list-style-type: none;
line-height: 50px;
}
#fruits>li>a {
float: right;
color: #fff;
text-decoration: none;
margin-right: 10px;
}
#fruits+div {
margin-top: 20px;
}
#fname {
width: 70%;
height: 40px;
color: #fff;
border-radius: 8px;
border: none;
outline: none;
font-size: 20px;
text-align: center;
vertical-align: middle;
background-color: #999;
}
#ok {
width: 19%;
height: 40px;
color: #fff;
background-color: #a45;
border: none;
outline: none;
font-size: 16px;
vertical-align: middle;
}
</style>
</head>
<body>
<div id="app">
<ul id="fruits">
<li>苹果<a href="">×</a></li>
<li>香蕉<a href="">×</a></li>
<li>榴莲<a href="">×</a></li>
<li>火龙果<a href="">×</a></li>
</ul>
<div>
<input type="text" id="fname">
<button id="ok">确定</button>
</div>
</div>
<script>
const ul = document.querySelector('#fruits')
const fnameInput = document.querySelector('#fname')
const okBtn = document.querySelector('#ok')
const anchors = document.querySelectorAll('#fruits a')
function removeItem(evt) {
evt.preventDefault()
let li = evt.target.parentNode
li.parentNode.removeChild(li)
}
function addItem(evt) {
let fname = fnameInput.value.trim()
if (fname.length > 0) {
let li = document.createElement('li')
li.textContent = fname
let a = document.createElement('a')
a.setAttribute('href', '')
a.textContent = '×'
a.addEventListener('click', removeItem)
li.appendChild(a)
ul.insertBefore(li, ul.firstElementChild)
}
fnameInput.value = ''
fnameInput.focus()
}
window.addEventListener('load', (evt) => {
for (let i = 0; i < anchors.length; i += 1) {
anchors[i].addEventListener('click', removeItem)
}
fnameInput.addEventListener('keydown', (evt) => {
let code = evt.keyCode || evt.which
if (code == 13) {
addItem()
}
})
okBtn.addEventListener('click', addItem)
})
</script>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态列表</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
background-color: #000;
color: #fff;
}
#app {
width: 40%;
margin: 20px auto;
}
#fruits>li {
width: 90%;
height: 50px;
background-color: #6ca;
margin: 4px 0;
text-align: center;
font-size: 20px;
list-style-type: none;
line-height: 50px;
}
#fruits>li>a {
float: right;
color: #fff;
text-decoration: none;
margin-right: 10px;
}
#fruits+div {
margin-top: 20px;
}
#fname {
width: 70%;
height: 40px;
color: #fff;
border-radius: 8px;
border: none;
outline: none;
font-size: 20px;
text-align: center;
vertical-align: middle;
background-color: #999;
}
#ok {
width: 19%;
height: 40px;
color: #fff;
background-color: #a45;
border: none;
outline: none;
font-size: 16px;
vertical-align: middle;
}
</style>
</head>
<body>
<div id="app">
<ul id="fruits">
<li>苹果<a href="">×</a></li>
<li>香蕉<a href="">×</a></li>
<li>榴莲<a href="">×</a></li>
<li>火龙果<a href="">×</a></li>
</ul>
<div>
<input type="text" id="fname">
<button id="ok">确定</button>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
// 1. $函数的参数是一个函数,该函数是页面加载完成后执行的回调函数
$(() => {
function removeItem(evt) {
evt.preventDefault()
// 4. $函数的参数是原生JavaScript对象,返回该原生JavaScript对象对应的jQuery对象
$(evt.target).parent().remove()
}
function addItem(evt) {
let fname = $('#fname').val().trim()
if (fname.length > 0) {
$('#fruits').append(
// 3. $函数的参数是标签字符串,创建对应的标签元素并返回jQuery对象
$('<li>').text(fname).append(
$('<a>').attr('href', '').text('×')
.on('click', removeItem)
)
)
}
$('#fname').val('')
// jQuery对象通过下标运算或get方法可以获得与之对应的原生JavaScript对象
// input.get(0).focus()
$('#fname')[0].focus()
}
// 2. $函数的参数是选择器字符串,返回对应元素的jQuery对象
$('#fruits a').on('click', removeItem)
$('#ok').on('click', addItem)
$('#fname').on('keydown', (evt) => {
let code = evt.keyCode || evt.which
if (code == 13) {
addItem(evt)
}
})
})
</script>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态列表</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
background-color: #000;
color: #fff;
}
#app {
width: 40%;
margin: 20px auto;
}
#fruits>li {
width: 90%;
height: 50px;
background-color: #6ca;
margin: 4px 0;
text-align: center;
font-size: 20px;
list-style-type: none;
line-height: 50px;
}
#fruits>li>a {
float: right;
color: #fff;
text-decoration: none;
margin-right: 10px;
}
#fruits+div {
margin-top: 20px;
}
#fname {
width: 70%;
height: 40px;
color: #fff;
border-radius: 8px;
border: none;
outline: none;
font-size: 20px;
text-align: center;
vertical-align: middle;
background-color: #999;
}
#ok {
width: 19%;
height: 40px;
color: #fff;
background-color: #a45;
border: none;
outline: none;
font-size: 16px;
vertical-align: middle;
}
</style>
</head>
<body>
<div id="app">
<ul id="fruits">
<li v-for="fruit in fruits">
{{ fruit }}
<a href="" @click.prevent="removeItem(fruit)">×</a>
</li>
</ul>
<div>
<input @keydown.enter="addItem()" type="text" id="fname" v-model="fname">
<button id="ok" @click="addItem()">确定</button>
</div>
</div>
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.min.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
fruits: ['苹果', '香蕉', '榴莲', '火龙果'],
fname: ''
},
methods: {
addItem() {
if (this.fname.trim().length > 0) {
this.fruits.push(this.fname.trim())
}
this.fname = ''
},
removeItem(fruit) {
let index = this.fruits.indexOf(fruit)
if (index >= 0) {
this.fruits.splice(index, 1)
}
}
}
})
</script>
</body>
</html>
\ No newline at end of file
## Django知识点概述
## 使用Django开发项目
> 说明:本文的部分插图来自于《Python项目开发实战》和《精通Django》,这两本书中都包含了对Django框架精彩的讲解,有兴趣的读者可以自行购买阅读。
......
## 关于测试
## 软件测试和自动化测试
### 软件测试概述
......
......@@ -812,7 +812,7 @@ HAYSTACK_CONNECTIONS = {
# 引擎配置
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
# 搜索引擎服务的URL
'URL': 'http://1.2.3.49200',
'URL': 'http://1.2.3.4:9200',
# 索引库的名称
'INDEX_NAME': 'goods',
},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册