提交 d45ee5d4 编写于 作者: W wizardforcel

2019-11-19 00:06:30

上级 277148ca
......@@ -8,7 +8,7 @@ Python 有 3 种循环类型:for 循环,while 循环和嵌套循环。
## `for`循环
We can iterate a list using a for loop
我们可以使用`for`循环迭代列表
```py
#!/usr/bin/python
......@@ -21,7 +21,8 @@ for item in items:
```
for 循环的可视化:
[<picture><source srcset="/wp-content/uploads/2016/03/for-loop-animation.gif.webp" type="image/webp"> <source srcset="/wp-content/uploads/2016/03/for-loop-animation.gif" type="image/jpeg"> ![for loop](img/82f73ddadae94db3c7fa209585564d41.jpg)</picture> ](/wp-content/uploads/2016/03/for-loop-animation.gif)
![for loop](img/82f73ddadae94db3c7fa209585564d41.jpg)
for 循环也可以重复 N 次:
......@@ -35,8 +36,7 @@ for i in range(1,10):
## `While`循环
If you are unsure how many times a code should be repeated, use a while loop.
For example,
如果不确定代码应重复多少次,请使用`while`循环。例如,
```py
......@@ -55,7 +55,7 @@ print('You guessed the correct number')
## 嵌套循环
We can combine for loops using nesting. If we want to iterate over an (x,y) field we could use:
我们可以使用嵌套组合`for`循环。 如果我们要遍历`(x, y)`字段,可以使用:
```py
#!/usr/bin/python
......
......@@ -16,9 +16,11 @@ range(lower_bound, upper_bound, step_size)
* step_bound:_ 步长,列表中每个数字之间的差。_
The lower_bound and step_size parameters are optional. By default the lower bound is set to zero, the incremental step is set to one. The parameters must be of the type integers, but may be negative.![python range](img/e67c0c741c049ebf8163523792e9e8d0.jpg)
`lower_bound``step_size`参数是可选的。默认情况下,下限设置为零,增量步长设置为一。参数必须为整数类型,但可以为负。
The python range function in the interpreter
[python range](img/e67c0c741c049ebf8163523793792e9e8d0.jpg)
解释器中的 python `range`函数
## 范围实现差异
......@@ -30,11 +32,11 @@ The python range function in the interpreter
## python 2.7 中的范围
A call to range(5) will return: 0,1,2,3,4.
调用`range(5)`将返回:`0, 1, 2, 3, 4`
```py
>>> range(5)
>>> range(5)
```
......@@ -42,7 +44,7 @@ A call to range(5) will return: 0,1,2,3,4.
```py
>>> range(1,10)
>>> range(1,10)
```
......@@ -50,7 +52,7 @@ A call to range(5) will return: 0,1,2,3,4.
```py
>>> range(0,10,2)
>>> range(0,10,2)
```
......@@ -59,7 +61,7 @@ python 3 中的**范围**
```py
>>> list(range(5))
>>> list(range(5))
```
......@@ -67,7 +69,7 @@ python 3 中的**范围**
```py
>>> list(range(0,10,2))
>>> list(range(0,10,2))
[0, 2, 4, 6, 8]
```
......
......@@ -6,7 +6,7 @@
## Python 元组
An empty tuple in Python would be defined as:
Python 中的空元组将定义为:
```py
tuple = ()
......@@ -31,7 +31,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:
要访问数据,我们可以简单地使用索引。 通常,索引是括号之间的数字:
```py
#!/usr/bin/env python
......@@ -56,7 +56,7 @@ print(country)
## 附加到 Python 中的元组
If you have an existing tuple, you can append to it with the + operator.  You can only append a tuple to an existing tuple.
如果您有一个现有的元组,则可以使用`+`运算符将其追加。 您只能将元组追加到现有元组。
```py
#!/usr/bin/env python
......
......@@ -8,7 +8,7 @@
## 字典示例
Let us make a simple dictionary:
让我们生成一个简单的字典:
```py
#!/usr/bin/python
......@@ -61,7 +61,7 @@ This sentence is stored here.
## 使用字典
We can manipulate the data stored in a dictionairy after declaration.  This is shown in the example below:
声明后,我们可以操作存储在字典中的数据。 在下面的示例中显示:
```py
#!/usr/bin/python
......
......@@ -17,7 +17,7 @@ y = "text"
## 数据类型转换
If you want to print numbers you will often need casting.
如果要打印数字,则经常需要强制转换。
在下面的示例中,我们要打印两个数字,一个整数(整数)和一个浮点数。
......@@ -48,7 +48,7 @@ print(c)
## 转换函数
To convert between datatypes you can use:
要在数据类型之间转换,可以使用:
[下载 Python 练习](https://pythonspot.com/download-python-exercises/)
......
......@@ -6,7 +6,7 @@
## 读取文件
You can read a file with the code below.
您可以使用以下代码读取文件。
该文件必须与程序位于同一目录中,如果不是,则需要指定路径。
......
......@@ -8,7 +8,7 @@ Python 默认支持写文件,不需要特殊模块。 您可以使用.write(
## 创建要写入的文件
The code below creates a new file (or overwrites) with the data.
下面的代码使用数据创建一个新文件(或覆盖)。
```py
#!/usr/bin/env python
......@@ -31,7 +31,7 @@ myfile.close()
## 附加到文件
If you simply want to add content to the file you can use the ‘a’ parameter.
如果您只想向文件添加内容,则可以使用`"a"`参数。
```py
#!/usr/bin/env python
......@@ -53,7 +53,7 @@ myfile.close()
## 参数
A summary of parameters:
参数总结:
[下载 Python 练习](https://pythonspot.com/download-python-exercises/)
......
......@@ -4,7 +4,7 @@
## 介绍
Technology always evolves. What are classes and where do they come from?
技术总是在发展。什么是类,它们从何而来?
**1.语句**
在计算的早期,程序员仅编写命令。
......@@ -19,7 +19,7 @@ Technology always evolves. What are classes and where do they come from?
## 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:
我们可以在 Python 中创建虚拟对象。 虚拟对象可以包含变量和方法。 程序可能具有许多不同的类型,并且是从类创建的。 例:
```py
class User:
......@@ -46,7 +46,7 @@ david.sayHello()
![python class: creation of objects](img/4915adfc6d1a53dddb74cfc74d45644b.jpg)
Python Class: create objects
Python 类:创建对象
在此类中,我们定义了 sayHello()方法,这就是为什么我们可以为每个对象调用它的原因。 **初始化**()方法被称为**构造函数**,并且在创建对象时始终被调用。 该类拥有的变量在这种情况下为“名称”。 这些变量有时称为类属性。
......@@ -54,7 +54,7 @@ Python Class: create objects
## 类变量
We define a class CoffeeMachine of which the virtual objects hold the amount of beans and amount of water. Both are defined as a number (integer). We may then define methods that add or remove beans.
我们定义了一个`CoffeeMachine`类,其中的虚拟对象包含大量的咖啡豆和大量的水。 两者都定义为数字(整数)。然后我们可以定义添加或删除豆子的方法。
```py
def addBean(self):
......@@ -65,7 +65,7 @@ def removeBean(self):
```
对于可变水,我们也是如此。 如下所示:
对于水,我们也是如此。如下所示:
```py
class CoffeeMachine:
......
......@@ -26,7 +26,7 @@ duck.walk()
![python class](img/cb722e238db4c81d1854b65097d597a9.jpg)
Python class and object drawn using the modeling language UML
使用建模语言 UML 绘制的 Python 类和对象
创建对象后,我们可以调用其方法并无限期使用其变量。 同一类的每个对象都具有相同的方法,但是其变量内容可能有所不同。
......
......@@ -8,7 +8,7 @@
![encapsulation](img/f9a64d9604d279e122057d7ac9086909.jpg)
encapsulation. Restricted accesss to methods or variables
封装。限制访问方法或变量
我们创建了一个 Car 类,它具有两种方法:drive()和 updateSoftware()。 创建汽车对象时,它将调用私有方法 __updateSoftware()。
......@@ -52,7 +52,7 @@ driving
![encapsulation-example](img/5c9510eede5c19eda3992c95d2bcb565.jpg)
Class with private variables
具有私有变量的类
变量可以是私有的,这在许多情况下都可以使用。 私有变量只能在类方法内更改,而不能在类外部更改。
......@@ -112,9 +112,9 @@ redcar.drive()
## Python 封装
To summarize, in Python there are:
总而言之,在 Python 中有:
Other programming languages have protected class methods too, but Python does not.
其他编程语言也具有受保护的类方法,而 Python 没有。
封装使您可以更好地控制代码中的耦合程度,它允许类在不影响代码其他部分的情况下更改其实现。
......@@ -122,7 +122,7 @@ Other programming languages have protected class methods too, but Python does no
| 类型 | 描述 |
| --- | --- |
| 公方法 | 可从任何地方访问 |
| 公方法 | 可从任何地方访问 |
| 私有方法 | 仅在自己的课程中可访问。 以两个下划线开头 |
| 公共变量 | 可从任何地方访问 |
| 私有变量 | 仅在自己的类或方法(如果已定义)中可访问。 以两个下划线开头 |
\ No newline at end of file
......@@ -4,7 +4,7 @@
![method overloading](img/fac89ca5c417cab74c1eae70c7d3c4b4.jpg)
Several ways to call a method (method overloading)
几种调用方法的方法(方法重载)
在 Python 中,您可以以一种多种方式来调用它来定义方法。
......@@ -16,7 +16,7 @@ Several ways to call a method (method overloading)
## 方法重载示例
We create a class with one method sayHello(). The first parameter of this method is set to None, this gives us the option to call it with or without a parameter.
我们使用一个方法`sayHello()`创建一个类。此方法的第一个参数设置为`None`,这使我们可以选择是否使用参数来调用它。
一个基于该类创建的对象,我们使用零和一个参数调用其方法。
......
......@@ -8,7 +8,7 @@
## 介绍
We define a basic class named User:
我们定义一个名为`User`的基本类:
```py
class User:
......
......@@ -8,7 +8,7 @@
## 具有函数的多态:
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.
我们创建了两个类别:`Bear``Dog`,两者都能发出不同的声音。 然后,我们创建两个实例,并使用相同的方法调用它们的动作。
```py
class Bear(object):
......@@ -42,8 +42,9 @@ Woof woof!
![polymorphism example](img/2c012acbbf068f97276301d4bf125959.jpg)
Polymorphism visual.
Abstract structure is defined in Document class.
视觉多态。
抽象结构在`Document`类中定义。
如果您创建编辑器,则可能事先不知道用户将打开哪种类型的文档(pdf 格式还是 word 格式?)。
......@@ -97,7 +98,7 @@ Document3: Show word contents!
![polymorphism-example](img/744304fa9c5a5b805fd3179eb9881d70.jpg)
Structure in abstract class, implementation in other classes
抽象类中的结构,其他类中的实现
另一个例子是拥有一个抽象类 Car,其中包含了 drive()和 stop()结构。
......
......@@ -6,8 +6,7 @@
## 内部类的例子
We create a class (Human) with one inner class (Head).
An instance is created that calls a method in the inner class:
我们创建一个具有一个内部类(`Head`)的类(`Human`)。创建一个实例,该实例调用内部类中的方法:
```py
#!/usr/bin/env python
......@@ -41,7 +40,7 @@ talking...
## 多个内部类
You are by no means limited to the number of inner classes, for example this code will work too:
您绝不限于内部类的数量,例如,以下代码也可以使用:
```py
#!/usr/bin/env python
......
......@@ -15,8 +15,7 @@
## 工厂方法模式
To deal with this we can use the _factory method_ pattern.
The idea is to have one function, the factory, that takes an input string and outputs an object.
为了解决这个问题,我们可以使用工厂方法模式。这个想法是要有一个函数,即工厂,该函数接受一个输入字符串并输出一个对象。
```py
obj = Car.factory("Racecar")
......@@ -30,7 +29,7 @@ obj.drive()
## 工厂方法示例
The example below demonstrates a factory method. The factory method (named factory) returns a new object of either type depending on the input.
下面的示例演示了工厂方法。工厂方法(命名为`factory`)将根据输入返回这两种类型的新对象。
```py
......
......@@ -7,7 +7,7 @@
* “要了解递归,您必须先了解递归”,
* “人就是人的母亲”。
You might wonder, what does this have to do with programming?
您可能想知道,这与编程有什么关系?
您可能需要将一个复杂的问题分解为几个较小的问题。 您已经熟悉[循环或迭代](https://pythonspot.com/loops/)。 在某些情况下,递归可能是更好的解决方案。
......@@ -15,9 +15,9 @@ You might wonder, what does this have to do with programming?
## 递归示例
## Recursion in with a list
## 列表中的递归
Let’s start with a very basic example: adding all numbers in a list.  Without recursion, this could be:
让我们从一个非常基本的示例开始:将所有数字添加到列表中。 如果没有递归,则可能是:
```py
#!/usr/bin/env python
......@@ -74,7 +74,7 @@ print(factorial(3))
## 递归限制
Everytime a function calls itself and stores some memory. Thus, a recursive function could hold much more memory than a traditional function. Python stops the function calls after a depth of 1000 calls. If you run this example:
每次函数调用自身并存储一些内存。 因此,与传统函数相比,递归函数可以容纳更多的内存。 Python 在 1000 次调用的深度后停止函数调用。 如果运行此示例:
```py
#!/usr/bin/env python
......
......@@ -4,7 +4,7 @@
## Python 日志记录
We can track events in a software application, this is known as **logging**. Let’s start with a simple example, we will log a warning message.
我们可以在软件应用程序中跟踪事件,这称为“日志”。让我们从一个简单的示例开始,我们将记录一条警告消息。
与仅打印错误相反,可以将日志记录配置为禁用输出或保存到文件。 这是简单打印错误的一大优势。
......@@ -40,7 +40,7 @@ logging.warning('Another message')
## 严重程度
The logger module has several levels of severity. We set the level of severity using this line of code:
日志模块具有多个严重级别。我们使用以下代码行设置严重性级别:
```py
logging.basicConfig(level=logging.DEBUG)
......@@ -69,7 +69,7 @@ logging.debug('Debug message')
## 记录时间
You can enable time for logging using this line of code:
您可以使用以下代码行启用日志记录时间:
```py
logging.basicConfig(format='%(asctime)s %(message)s')
......
......@@ -6,7 +6,7 @@
## 在 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.
您可以使用`Popen`函数调用在 Python 中启动进程。 下面的程序启动 Unix 程序`cat`,第二个参数是参数。 这相当于`cat test.py`。您可以使用任何参数启动任何程序。
```py
#!/usr/bin/env python
......@@ -23,7 +23,7 @@ process.communicate()调用从进程中读取输入和输出。 stdout 是
## `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:
`subprocess`具有方法`call()`,可用于启动程序。该参数是一个列表,其第一个参数必须是程序名称。完整的定义是:
```py
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
......@@ -44,7 +44,7 @@ subprocess.call(["ls", "-l"])
## 保存进程输出(标准输出)
We can get the output of a program and store it in a string directly using check_output. The method is defined as:
我们可以获取程序的输出,并使用`check_output`直接将其存储在字符串中。该方法定义为:
```py
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)
......
......@@ -13,7 +13,7 @@
## Python 线程
Let’s create a thread program. In this program we will start 10 threads which will each output their id.
让我们创建一个线程程序。 在此程序中,我们将启动 10 个线程,每个线程将输出其 ID。
```py
import threading
......@@ -46,9 +46,9 @@ 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.
在 Python 中,`Timer`类是`Thread`类的子类。 这意味着它的行为类似。我们可以使用计时器类来创建定时线程。计时器是通过`.start()`方法调用启动的,就像常规线程一样。 下面的程序创建一个线程,该线程在 5 秒钟后启动。
```py
#!/usr/bin/env python
......
......@@ -30,7 +30,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:
`map`的定义是`map(function, iterable)`。它将函数应用于可迭代对象的每个项目。我们可以对列表和 lambda 函数使用`map()`
```py
#!/usr/bin/env python
......@@ -45,7 +45,7 @@ print(squaredList)
## `filter`函数
filter(function,iterable) creates a new list from the elmeents for which the function returns True. Example:
`filter(function, iterable)`从元素中创建一个新列表,该函数为其返回`True`。例:
```py
#!/usr/bin/env python
......@@ -60,7 +60,7 @@ print(newList)
## `reduce`函数
The reduce function, reduce(function, iterable) applies two arguments cumulatively to the items of iterable, from left to right. Example:
函数`reduce(function, iterable)`从左到右将函数应用在`iterable`的各个项目上。 例:
```py
#!/usr/bin/env python
......
......@@ -4,7 +4,7 @@
## 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.
Python 中的集合是对象的集合。 集合在 Python 2.4 和更高版本中可用。 它们与列表或元组的不同之处在于它们是根据数学集合建模的。
## 集合示例
......@@ -30,7 +30,7 @@ print(x)
## 简单记法
If you use Python version 2.6 or a later version, you can use a simplified notation:
如果您使用 Python 2.6 版或更高版本,则可以使用简化的符号:
```py
#!/usr/bin/env python
......@@ -47,7 +47,7 @@ print(y)
## 从集合清除元素
To remove all elements from sets:
要从集合中删除所有元素:
```py
#!/usr/bin/env python
......@@ -108,8 +108,7 @@ print( y.difference(x) )
x = set(["a","b","c","d"])
y = set(["c","d"])
print( x.issubset(y) )<b>
</b>
print( x.issubset(y) )
```
......
......@@ -4,11 +4,11 @@
## 模块化编程
As you are programming, the software can quickly scale into a large code base. To manage complexity we can use classes, functions and modules.
在进行编程时,该软件可以迅速扩展为大型代码库。 为了管理复杂性,我们可以使用类,函数和模块。
## 模块内容
To show the accessible functions (and included variables) in a module, you can use this code:
要显示模块中的可访问函数(和包含的变量),可以使用以下代码:
```py
#!/usr/bin/env python
......@@ -36,7 +36,7 @@ print(dir(sys))
## 创建一个模块
You can create your own module in these steps:
可以按照以下步骤创建自己的模块:
创建一个名为 test.py 的文件(您的模块)
......
......@@ -4,9 +4,11 @@
## 介绍
A graph in mathematics and computer science consists of “nodes” which may or may not be connected with one another. Connections between nodes are called edges. A graph can be directed (arrows) or undirected. The edges could represent distance or weight.![graph mathematics](img/eabd6215fba57ca6e4a5ab91823da3c2.jpg)
数学和计算机科学中的图由节点组成,这些节点可以相互连接,也可以不相互连接。 节点之间的连接称为边。图可以是有向的(箭头)或无向的。 边可以表示距离或重量。
default graph (left), directed graph (right)
![graph mathematics](img/eabd6215fba57ca6e4a5ab91823da3c2.jpg)
默认图(左),有向图(右)
Python 没有图数据类型。 要使用图,我们可以使用模块,也可以自己实现:
......@@ -16,7 +18,7 @@ Python 没有图数据类型。 要使用图,我们可以使用模块,也可
## Python 中的图
A directed graph can be defined as:
有向图可以定义为:
```py
#!/usr/bin/env python
......@@ -32,7 +34,7 @@ print(graph)
## 使用 networkx 的图
The networkx software module has support for creating, manipulating graphs.
networkx 软件模块支持创建,处理图。
```py
#!/usr/bin/env python
......
......@@ -10,15 +10,17 @@
## 有限状态机示例
First install the Fysom module:
首先安装 Fysom 模块:
```py
sudo pip install fysom
```
We can define a Finite State Machine (FSM) with two states: sleeping and awake. To move between the states we will define the transitions wakeup() and sleep().![finite state machine](img/4feca70727af7a6746c9d69566f08f53.jpg)
我们可以用两个状态定义一个有限状态机(FSM):睡眠状态和唤醒状态。 要在状态之间移动,我们将定义转换`wakeup()``sleep()`
Finite state machine. States: awake, sleeping. Transitions: sleep, wake up
![finite state machine](img/4feca70727af7a6746c9d69566f08f53.jpg)
有限状态机。状态:醒着,睡觉。转换:睡眠,醒来
例:
......@@ -51,8 +53,8 @@ awake
## 有限状态机
There are several implementations of Finite State Machines in Python:
Python 中有几种有限状态机的实现:
* [Fysom](https://github.com/oxplot/fysom)
* [Python fsm](https://github.com/smontanaro/python-bits/blob/master/fsm.py)
* [PythonBits fsm](https://github.com/smontanaro/python-bits/blob/master/fsm.py)
* [Fsme](http://fsme.sourceforge.net/)
\ No newline at end of file
......@@ -4,15 +4,17 @@
## 介绍
In computer science, a **tree** is a **data** **structure** that is modeled after nature. Unlike trees in nature, the tree data structure is upside down: the root of the tree is on top. A tree consists of nodes and its connections are called edges. The bottom nodes are also named leaf nodes. A tree may not have a cycle.![tree](img/883cefe5eed8bb91f8afda3e8466c83b.jpg)
在计算机科学中,树是模仿自然的数据结构。与自然界中的树不同,树数据结构是上下颠倒的:树的根在顶部。一棵树由节点组成,其连接称为边。 底部节点也称为叶节点。 一棵树可能没有循环。
A tree with eight nodes. The root of the tree (5) is on top.
![tree](img/883cefe5eed8bb91f8afda3e8466c83b.jpg)
一棵有八个节点的树。 树的根(5)在顶部。
Python 没有对树的内置支持。
## 二叉树
**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.
二叉树是一种数据结构,其中每个节点最多具有两个子代(左子代和右子代)。 一棵树的根在最上面。下面的每个节点都有一个以上的节点称为父节点。我们定义一个具有`left``right`属性的类`Tree`。 从这个二叉树中,我们定义根(三个中的顶部)和一个左右节点。
```py
#!/usr/bin/env python
......
......@@ -12,17 +12,17 @@
## 二进制数
At the lowest level, the computer has no notion whatsoever of numbers except ‘there is a signal’ or ‘these is not a signal’. You can think of this as a light switch: Either the switch is on or it is off.
在最低层次上,计算机没有任何数字概念,除了“有信号”或“无信号”。您可以将其视为电灯开关:开关打开或关闭。
This tiny amount of information, the smallest amount of information that you can store in a computer, is known as a _bit._ We represent a bit as either low (0) or high (1).
这些微小的信息,是您可以在计算机中存储的最小信息,称为位。我们将位表示为低(0)或高(1)。
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.
为了表示大于1的数字,使用一系列位的主意诞生了。 一个八位的序列可以存储更大的数字,这称为字节。 由一和零组成的序列称为二进制。我们传统的十位数计数系统称为十进制。
![binary](img/74c0efc42f7bf310489f953f2b5cbd0e.jpg)
Binary numbers and their decimal representation.
二进制数及其十进制表示形式。
Lets see that in practice:
让我们在实践中看看:
```py
# Prints out a few binary numbers.
......@@ -33,7 +33,7 @@ print int('11', 2)
```
The second parameter 2, tells Python we have a number based on 2 elements (1 and 0). To convert a byte (8 bits) to decimal, simple write a combination of eight bits in the first parameter.
第二个参数 2 告诉 Python 我们有一个基于 2 个元素(1 和 0)的数字。 要将字节(8 位)转换为十进制,只需在第一个参数中写入 8 位的组合即可。
```py
# Prints out a few binary numbers.
......@@ -44,13 +44,13 @@ print int('11111111', 2) # outputs 255
```
How does the computer do this? Every digit (from right to left) is multiplied by the power of two.
电脑如何做到这一点? 每个数字(从右到左)乘以 2 的幂。
The number ‘000**1**000**1**‘ is (**1** x 2^0) + (0 x 2^1) + (0 x 2^2) + (0 x 2^3) + (**1** x 2^4) + (0 x 2^5) + (0 x 2^6) + (0 x 2^7) = 16 + 1 = 17\. Remember, read from right to left.
数字`00010001``(1 x 2^0) + (0 x 2^1) + (0 x 2^2) + (0 x 2^3) + (1 x 2^4) + (0 x 2^5) + (0 x 2^6) + (0 x 2^7) = 16 + 1 = 17`。记住,从右到左阅读。
The number ‘00110010’ would be (0 x 2^0) + (1 x 2^1) + (0 x 2^2) + (0 x 2^3) + (**1** x 2^4) + (**1** x 2^5) + (0 x 2^6) + (0 x 2^7) = 32+16+2 = 50.
数字`00110010``(0 x 2^0) + (1 x 2^1) + (0 x 2^2) + (0 x 2^3) + (**1** x 2^4) + (**1** x 2^5) + (0 x 2^6) + (0 x 2^7) = 32+16+2 = 50`
Try the sequence ‘00101010’ yourself to see if you understand and verify with a Python program.
自己尝试使用序列`00101010`,以了解您是否了解并使用 Python 程序进行验证。
## 二进制数的逻辑运算
......@@ -84,9 +84,11 @@ After shifting in decimal: 10
## 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)
给定两个输入,计算机可以使用这些位进行若干逻辑运算。让我们以 AND 运算符为例。 如果输入 A 和输入 B 为正,则输出将为正。 我们将以图形方式演示 AND 运算符,左边的两个是输入 A 和输入 B,右边的圆是输出:
Bitwise AND
![Bitwise AND](img/a783141f02a6302a2f1e80908156dc5d.jpg)
按位 AND
在代码中,这就像使用&符号一样简单,该符号代表逻辑 AND 运算符。
......@@ -128,11 +130,13 @@ print bin(inputA & inputB) # logical AND on inputA and inputB and output in bi
## 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)
现在您已经了解了 AND 运算符,让我们看一下 OR 运算符。 给定两个输入,仅当 A 和 B 均为零时,输出才为零。
![binary bitwise OR](img/b726bf5bcea4b7fc5dd8a2908ec4d963.jpg)
binary bitwise OR
二进制按位 OR
要执行它,我们使用| 操作员。 可以像这样简单地执行一系列位:
要执行它,我们使用`|`运算符。 可以像这样简单地执行一系列位:
```py
inputA = int('00100011',2) # define binary number
......@@ -155,9 +159,11 @@ print bin(inputA | inputB) # Execute bitwise logical OR and print result in bi
## XOR 运算符
This is an interesting operator: The Exclusive OR or shortly XOR.<caption id=”attachment_849” align=”alignnone” width=”640”]![bitwise XOR](img/461628f4ea72f485b16a902f65db838a.jpg)
这是一个有趣的运算符:异或或简称 XOR。
![bitwise XOR](img/461628f4ea72f485b16a902f65db838a.jpg)
bitwise XOR
按位 XOR
为了执行它,我们使用^运算符。 可以像这样简单地执行一系列位:
......
......@@ -56,7 +56,7 @@ Breakpoint in Python program
## PDB-Python 调试器
The module pdb supports setting breakpoints. A breakpoint is an intentional pause of the program. where you can get more information about the programs state.
`pdb`模块支持设置断点。 断点是程序的有意暂停。 您可以在其中获取有关程序状态的更多信息。
要设置断点,请插入行
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册