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)
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:
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.
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.
@@ -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:
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.
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:
Let’s create a thread program. In this program we will start 10 threads which will each output their id.
让我们创建一个线程程序。 在此程序中,我们将启动 10 个线程,每个线程将输出其 ID。
```py
importthreading
...
...
@@ -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.
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:
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.
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)
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)
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.
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.
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.
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,右边的圆是输出:
@@ -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 均为零时,输出才为零。
@@ -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)
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.