提交 19fe3c5a 编写于 作者: W wizardforcel

2019-11-18 16:24:12

上级 a1f7e8a0
......@@ -69,7 +69,7 @@ print(z)
调用 afunction()之后,将更改整个程序的全局变量。
## 行使
## 练习
Local and global variables can be used together in the same program.
Try to determine the output of this program:
......
# 范围
# 作用域
> 原文: [https://pythonspot.com/scope](https://pythonspot.com/scope)
## 范围
## 作用域
变量只能到达定义它们的区域,这称为 _ 范围 _。 将其视为可以使用变量的代码区域。 Python 支持全局变量(可在整个程序中使用)和局部变量。
变量只能到达定义它们的区域,这称为 _ 作用域 _。 将其视为可以使用变量的代码区域。 Python 支持全局变量(可在整个程序中使用)和局部变量。
默认情况下,函数中声明的所有变量都是局部变量。 要访问函数内部的全局变量,必须明确定义“全局变量”。
## 示例
下面我们将研究用法 局部变量和范围。 此 _**不起作用**_ :
下面我们将研究用法 局部变量和作用域。 此 _**不起作用**_ :
```py
#!/usr/bin/python
......
# 循环:For 循环,while 循环
# 循环:`For`循环,`while`循环
> 原文: [https://pythonspot.com/loops/](https://pythonspot.com/loops/)
......@@ -6,7 +6,7 @@
Python 有 3 种循环类型:for 循环,while 循环和嵌套循环。
## 对于循环
## `for`循环
We can iterate a list using a for loop
......@@ -33,7 +33,7 @@ for i in range(1,10):
```
## While 循环
## `While`循环
If you are unsure how many times a code should be repeated, use a while loop.
For example,
......
......@@ -28,7 +28,7 @@ The python range function in the interpreter
* Python 3.x: _range()函数生成一个序列。_
## range in python 2.7
## python 2.7 中的范围
A call to range(5) will return: 0,1,2,3,4.
......
......@@ -29,7 +29,7 @@ personInfo = ("Diana", 32, "New York")
元组中的数据可以是一种或多种数据类型,例如文本和数字。
## 资料存取
## 数据存取
To access the data we can simply use an index. As usual, an index is a number between brackets:
......
......@@ -6,7 +6,7 @@
一对大括号创建一个空字典: `{}` 。 每个元素都可以映射到某个值。 整数或字符串可用于索引。 字典没有命令。
## 字典
## 字典
Let us make a simple dictionary:
......@@ -59,7 +59,7 @@ This sentence is stored here.
```
## 处理字典
## 使用字典
We can manipulate the data stored in a dictionairy after declaration.  This is shown in the example below:
......
......@@ -46,7 +46,7 @@ print(c)
在上面的示例中,我们将两个具有数据类型字符串的变量强制转换为数据类型 float。
## 转换功能
## 转换函数
To convert between datatypes you can use:
......
......@@ -49,7 +49,7 @@ print(uniform(1, 10))
从列表中选择一个随机项目
## 有趣的列表
## 打乱列表
我们可以使用以下代码对列表进行随机排列:
......
# 读取档案
# 读取文件
> 原文: [https://pythonspot.com/read-file/](https://pythonspot.com/read-file/)
您之前已经看过各种类型的数据持有者:整数,字符串,列表。 但是到目前为止,我们还没有讨论如何读取或写入文件。
## 读取档案
## 读取文件
You can read a file with the code below.
......
# 写文件
# 写文件
> 原文: [https://pythonspot.com/write-file/](https://pythonspot.com/write-file/)
......@@ -51,7 +51,7 @@ myfile.close()
```
## 参
## 参
A summary of parameters:
......
......@@ -17,10 +17,6 @@ Technology always evolves. What are classes and where do they come from?
让我们一起潜水吧!
## Python 课程:
[Python 编程训练营:从零到英雄](https://gum.co/dcsp)
## Python 类
We can create virtual objects in Python. A virtual object can contain variables and methods.  A program may have many different types and are created from a class. Example:
......
# 封装形式
# 封装
> 原文: [https://pythonspot.com/encapsulation](https://pythonspot.com/encapsulation)
在面向对象的 python 程序中,您可以 _ 限制对方法和变量的访问 _。 这可以防止意外修改数据,这就是 _ 封装 _。 让我们从一个例子开始。
## 私方法
## 私方法
![encapsulation](img/f9a64d9604d279e122057d7ac9086909.jpg)
......@@ -48,7 +48,7 @@ driving
实际上可以使用 redcar._Car__updateSoftware()调用该方法。
## 私变量
## 私变量
![encapsulation-example](img/5c9510eede5c19eda3992c95d2bcb565.jpg)
......@@ -123,6 +123,6 @@ Other programming languages have protected class methods too, but Python does no
| 类型 | 描述 |
| --- | --- |
| 公开方法 | 可从任何地方访问 |
| 私方法 | 仅在自己的课程中可访问。 以两个下划线开头 |
| 私方法 | 仅在自己的课程中可访问。 以两个下划线开头 |
| 公共变量 | 可从任何地方访问 |
| 私人变量 | 仅在自己的类或方法(如果已定义)中可访问。 以两个下划线开头 |
\ No newline at end of file
| 私有变量 | 仅在自己的类或方法(如果已定义)中可访问。 以两个下划线开头 |
\ No newline at end of file
# 遗产
# 继承
> 原文: [https://pythonspot.com/inheritance/](https://pythonspot.com/inheritance/)
......@@ -6,7 +6,7 @@
与其他流行的编程语言不同,Python 支持从多个类继承。
## 归纳法
## 介绍
We define a basic class named User:
......
# 多态
# 多态
> 原文: [https://pythonspot.com/polymorphism/](https://pythonspot.com/polymorphism/)
有时,对象有多种类型或形式。 如果我们有一个按钮,则有许多不同的绘制输出(圆形按钮,复选按钮,方形按钮,带有图像的按钮),但它们确实共享相同的逻辑:onClick()。 我们使用相同的方法访问它们。 这个想法称为 _ 多态 _。
有时,对象有多种类型或形式。 如果我们有一个按钮,则有许多不同的绘制输出(圆形按钮,复选按钮,方形按钮,带有图像的按钮),但它们确实共享相同的逻辑:onClick()。 我们使用相同的方法访问它们。 这个想法称为 _ 多态 _。
多态是基于希腊语 Poly(许多)和 morphism(形式)。 我们将创建一个可以采用或使用多种形式的对象的结构。
## 具有功能的多态性
## 具有函数的多态
We create two classes:  Bear and Dog, both  can make a distinct sound.  We then make two instances and call their action using the same method.
......@@ -93,7 +93,7 @@ Document3: Show word contents!
我们有一个抽象访问点(文档),用于访问遵循相同结构的多种类型的对象(pdf,word)。
## 多态示例
## 多态示例
![polymorphism-example](img/744304fa9c5a5b805fd3179eb9881d70.jpg)
......
# 内部
# 内部
> 原文: [https://pythonspot.com/inner-classes](https://pythonspot.com/inner-classes)
......
......@@ -13,10 +13,6 @@
* 如果用户启动 Web 浏览器,则浏览器不会事先知道将打开多少个选项卡(每个选项卡都是一个对象)。
## Related Course:
[Python Programming Bootcamp: Go from zero to hero](https://gum.co/dcsp)
## 工厂方法模式
To deal with this we can use the _factory method_ pattern.
......
# 记录中
# 日志
> 原文: [https://pythonspot.com/logging/](https://pythonspot.com/logging/)
......@@ -8,7 +8,7 @@ We can track events in a software application, this is known as **logging**. Let
与仅打印错误相反,可以将日志记录配置为禁用输出或保存到文件。 这是简单打印错误的一大优势。
## 记录示例
## 日志示例
```py
import logging
......@@ -59,8 +59,6 @@ logging.basicConfig(level=logging.DEBUG)
| 错误 | 该软件将不再能够运行 |
| 危急 | 非常严重的错误 |
|
```py
import logging
......@@ -69,8 +67,6 @@ logging.debug('Debug message')
```
|
## 记录时间
You can enable time for logging using this line of code:
......
# Python 子进程
# Python `subprocess`
> 原文: [https://pythonspot.com/python-subprocess/](https://pythonspot.com/python-subprocess/)
子流程模块使您可以从 Python 程序启动新应用程序。 多么酷啊?
`subprocess`模块使您可以从 Python 程序启动新应用程序。 多么酷啊?
## 在 Python 中启动一个程:
## 在 Python 中启动一个程:
You can start a process in Python using the Popen function call. The program below starts the unix program ‘cat’ and the second parameter is the argument. This is equivalent to ‘cat test.py’.  You can start any program with any parameter.
......@@ -19,9 +19,9 @@ print stdout
```
process.communicate()调用从程中读取输入和输出。 stdout 是进程输出。 仅当发生错误时,stderr 才会被写入。 如果要等待程序完成,可以调用 Popen.wait()。
process.communicate()调用从程中读取输入和输出。 stdout 是进程输出。 仅当发生错误时,stderr 才会被写入。 如果要等待程序完成,可以调用 Popen.wait()。
## 子流程 call()
## `subprocess.call()`
Subprocess has a method call() which can be used to start a program. The parameter is a list of which the first argument must be the program name. The full definition is:
......
# 线程和线程
# `threading`和`_thread`
> 原文: [https://pythonspot.com/threading](https://pythonspot.com/threading)
在 Python 中,您可以使用 Python 2.x 中的线程模块或 Python 3 中的 _thread 模块创建线程。我们将使用线程模块与之交互。
在 Python 中,您可以使用 Python 2.x 中的`threading`模块或 Python 3 中的 _thread 模块创建线程。我们将使用`threading`模块与之交互。
线程是一种操作系统进程,具有与正常进程不同的功能:
* 线程作为进程的子集存在
......@@ -46,7 +46,7 @@ for x in xrange(10):
如果运行一次,线程不必停止。 线程可以是定时的,每 x 秒重复执行一次线程功能。
## 定时线程
## 线程定时
In Python, the Timer class is a subclass of the Thread class. This means it behaves similar. We can use the timer class to create timed threads. Timers are started with the .start() method call, just like regular threads. The program below creates a thread that starts after 5 seconds.
......
......@@ -4,11 +4,6 @@
我们可以创建匿名函数,称为 lambda 函数。 Lambda 函数与常规 Python 函数不同,它们源自 Lambda 微积分。 它允许您编写非常简短的函数。
## Related Courses:
* [Python 编程训练营:从零变成英雄](https://gum.co/dcsp)
## Lambda 函数示例
此代码显示 lambda 函数的用法:
......@@ -33,7 +28,7 @@ print(f(12))
```
## 地图功能
## `map`函数
The definition of map is map(function,iterable). It applies a function to every item in the iteratable. We can use map() to on a lambda function with a list:
......@@ -48,7 +43,7 @@ print(squaredList)
在使用 lambda 函数的任何地方,都可以改用普通函数。 Lambda 函数不是语句,而是表达式。 Lambda 函数不支持语句块。
## 过滤功能
## `filter`函数
filter(function,iterable) creates a new list from the elmeents for which the function returns True. Example:
......@@ -63,7 +58,7 @@ print(newList)
返回列表只包含 lambda 表达式“ lamba x:x%2 == 0”为真的元素。
## 减少功能
## `reduce`函数
The reduce function, reduce(function, iterable) applies two arguments cumulatively to the items of iterable, from left to right. Example:
......
# Python 集
# Python 集
> 原文: [https://pythonspot.com/python-set/](https://pythonspot.com/python-set/)
## 用 Python 设置
## Python 集合
A set in Python is a collection of objects. Sets are available in Python 2.4 and newer versions. They are different from lists or tuples in that they are modeled after sets in mathematics.
## 设置示例
## 集合示例
要创建一个集合,我们使用 set()函数。
......@@ -43,9 +43,9 @@ print(y)
```
## 设定方法
## 集合方法
## Clear elements from set
## 从集合清除元素
To remove all elements from sets:
......@@ -71,9 +71,9 @@ print(x)
```
## 将元素删除到集合中
## 将元素从集合中删除
要将元素删除到集合中
要将元素从集合中删除
```py
!/usr/bin/env python
......@@ -84,9 +84,9 @@ print(x)
```
## 两之间的差异
## 两个集合之间的差异
要查找两之间的差异,请使用:
要查找两个集合之间的差异,请使用:
```py
#!/usr/bin/env python
......@@ -113,9 +113,9 @@ print( x.issubset(y) )<b>
```
## 超
## 超集
要测试集合是否是超集,请执行以下操作:
要测试集合是否是超集,请执行以下操作:
```py
#!/usr/bin/env python
......@@ -126,9 +126,9 @@ print( x.issuperset(y) )
```
## 交叉点
## 交
要测试交叉点,请使用:
要测试交,请使用:
```py
#!/usr/bin/env python
......
......@@ -10,11 +10,6 @@ A tree with eight nodes. The root of the tree (5) is on top.
Python 没有对树的内置支持。
## Related Course:
* [Python 编程训练营:从零变成英雄](https://gum.co/dcsp)
## 二叉树
**A binary** **tree** is a data structure where every node has at most two children (left and right child). The **root** of a tree is on top. Every node below has a node above known as the parent node.We define a class thee which has a left and right attribute. From this binary tree we define the root (top of the three) and a left and right node.
......
......@@ -18,7 +18,7 @@ This tiny amount of information, the smallest amount of information that you can
To represent higher numbers than 1, the idea was born to use a sequence of bits. A sequence of eight bits could store much larger numbers, this is called a _byte_. A sequence consisting of ones and zeroes is known as _binary_. Our traditional counting system with ten digits is known as decimal.
<caption id=”attachment_824” align=”alignnone” width=”300”]![binary](img/74c0efc42f7bf310489f953f2b5cbd0e.jpg)
![binary](img/74c0efc42f7bf310489f953f2b5cbd0e.jpg)
Binary numbers and their decimal representation.
......@@ -82,7 +82,7 @@ After shifting in decimal: 10
```
## AND 运算
## AND 运算
Given two inputs, the computer can do several logic operations with those bits. Let’s take the AND operator. If input A and input B are positive, the output will be positive. We will demonstrate the AND operator graphically, the two left ones are input A and input B, the right circle is the output:<caption id=”attachment_843” align=”alignnone” width=”640”]![Bitwise AND](img/a783141f02a6302a2f1e80908156dc5d.jpg)
......@@ -126,7 +126,7 @@ print bin(inputA & inputB) # logical AND on inputA and inputB and output in bi
```
## OR 运算
## OR 运算
Now that you have learned the AND operator, let’s have a look at the OR operator. Given two inputs, the output will be zero only if A and B are both zero.<caption id=”attachment_847” align=”alignnone” width=”640”]![binary bitwise OR](img/b726bf5bcea4b7fc5dd8a2908ec4d963.jpg)
......
# 读取档案
# 读取文件
> 原文: [https://pythonspot.com/read-file/](https://pythonspot.com/read-file/)
您之前已经看过各种类型的数据持有者:整数,字符串,列表。 但是到目前为止,我们还没有讨论如何读取或写入文件。
## 读取档案
## 读取文件
You can read a file with the code below.
......
......@@ -51,7 +51,7 @@ myfile.close()
```
## 参
## 参
A summary of parameters:
......
......@@ -10,7 +10,7 @@
* [flaskr](http://flask.pocoo.org/docs/0.10/tutorial/introduction/) -微博
* [minitwit](https://github.com/mitsuhiko/flask/tree/master/examples/minitwit/) -一个推特克隆
* [flask 网站](https://github.com/mitsuhiko/flask-website)-静态页面+邮件列表档案
* [flask 网站](https://github.com/mitsuhiko/flask-website)-静态页面+邮件列表文件
## 安装烧瓶
......
# 弦片(第 2 部分)
# 字符串(第 2 部分)
> 原文: [https://pythonspot.com/string-slices-part-2/](https://pythonspot.com/string-slices-part-2/)
......
......@@ -11,7 +11,7 @@ Jinja2 模板只是一个文本文件,不需要特定的扩展名,例如.htm
| 定界符 | 用法 |
| --- | --- |
| &lt;raw&gt;&lt;…%&gt;&lt;endraw&gt;&lt;/endraw&gt;&lt;/raw&gt; | 陈述 |
| &lt;raw&gt;&lt;…%&gt;&lt;endraw&gt;&lt;/endraw&gt;&lt;/raw&gt; | 语句 |
| &lt;raw&gt;&lt;&gt; &gt;&lt;endraw&gt;&lt;/endraw&gt;&lt;/raw&gt; | 要打印到模板输出的表达式 |
| &lt;raw&gt;&lt;…#&gt;&lt;endraw&gt;&lt;/endraw&gt;&lt;/raw&gt; | 注释未包含在模板输出中 |
| &lt;raw&gt;#…##&lt;endraw&gt;&lt;/endraw&gt;&lt;/raw&gt; | 行语句 |
......
......@@ -18,10 +18,6 @@
* 布尔值 _(对或错)_
## Related Course:
[Python Programming Bootcamp: Go from zero to hero](https://gum.co/dcsp)
## 数值变量示例
Example of numeric variables:
......
......@@ -115,7 +115,7 @@ print json.dumps(d, ensure_ascii=False)
| JSON 格式 | 蟒蛇 |
| --- | --- |
| 宾语 | 字典 |
| 数组 | 清单 |
| 数组 | 列表 |
| 串 | 力量 |
| 数字(整数) | 整型 |
| 数字(实数) | 浮动 |
......
# Python 清单
# Python 列表
> 原文: [https://pythonspot.com/python-lists/](https://pythonspot.com/python-lists/)
列表是一个序列和一个基本的数据结构。 列表可以包含字符串(文本)和数字。 列表类似于其他编程语言中的数组,但具有其他功能。
## Python 清单
## Python 列表
We define lists with brackets []. To access the data, these same brackets are used.
Example list usage:
......@@ -38,7 +38,7 @@ print(l) # print all elements.
```
## 排序清单
## 排序列表
We can sort the list using the sort() function.
......
# 如果陈述
# `if`语句
> 原文: [https://pythonspot.com/if-statements/](https://pythonspot.com/if-statements/)
在 Python 中,您可以定义条件语句,称为 if 语句。
如果满足某些条件,则将执行代码块。
## 如果陈述
## `if`语句
Consider this application, it executes either the first or second code depending on the value of x.
......@@ -46,7 +46,7 @@ A word on conditional operators
Do not confuse the assignment operator (=) with the equals operator (==).
## 套料
## 嵌套
The most straightforward way to do multiple conditions is nesting:
......
......@@ -17,7 +17,7 @@ re.match(pattern, string)
参数为:
| 参 | 描述 |
| 参 | 描述 |
| --- | --- |
| 图案 | 正则表达式 |
| 串 | 输入字符串 |
......
# 职能
# 函数
> 原文: [https://pythonspot.com/functions/](https://pythonspot.com/functions/)
函数是可重用的代码,可以在程序中的任何位置调用。 函数提高了代码的可读性:使用函数而不是冗长的指令列表,使人们更容易理解代码。
最重要的是,可以重复使用或修改功能,这也提高了可测试性和可扩展性。
最重要的是,可以重复使用或修改函数,这也提高了可测试性和可扩展性。
## 功能定义
## 函数定义
We use this syntax to define as function:
......@@ -17,7 +17,7 @@ def function(parameters):
```
**def 关键字**告诉 Python 我们有一段可重用的代码(一个函数)。 一个程序可以具有许多功能
**def 关键字**告诉 Python 我们有一段可重用的代码(一个函数)。 一个程序可以具有许多函数
## 实际例子
......@@ -42,7 +42,7 @@ print(f(3))
该函数具有一个参数 x。 返回值是函数返回的值。 并非所有函数都必须返回某些内容。
## 参
## 参
 We can pass multiple variables:
......
......@@ -28,7 +28,7 @@ python -m http.server
## 有趣的文字
尝试以下陈述来显示蒂姆·彼得斯的诗。
尝试以下语句来显示蒂姆·彼得斯的诗。
```py
import this
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册