《每日论文》Problem Solving with Algorithms and Data Structures using Python.

Problem Solving with Algorithms and Data Structures using Python.使用Python解决算法和数据结构的问题。

在这里插入图片描述
By Brad Miller and David Ranum, Luther College

Assignments

分配

There is a wonderful collection of YouTube videos recorded by Gerry Jenkins to support all of the chapters in this text.
Gerry Jenkins在YouTube上录制了一组精彩的视频来支持本文的所有章节。

1.Introduction

1、简介

1.1Objectives

1.1目的

To review the ideas of computer science, programming, and problem-solving.
复习计算机科学、编程和解决问题的思想。
To understand abstraction and the role it plays in the problem-solving process.
理解抽象和它在解决问题的过程中所扮演的角色。
To understand and implement the notion of an abstract data type.
理解和实现抽象数据类型的概念。
To review the Python programming language.
回顾Python编程语言。

1.2. Getting Started

1.2入门指南

The way we think about programming has undergone many changes in the years since the first electronic computers required patch cables and switches to convey instructions from human to machine.
自从第一代电子计算机需要接线和开关来将人的指令传送到机器中,我们思考编程的方式在这些年里经历了许多变化。
As is the case with many aspects of society, changes in computing technology provide computer scientists with a growing number of tools and platforms on which to practice their craft.
正如社会的许多方面一样,计算机技术的变化为计算机科学家提供了越来越多的工具和平台,通过使用这些工具和在这些平台上他们可以实践自己的技术。
Advances such as faster processors, high-speed networks, and large memory capacities have created a spiral of complexity through which computer scientists must navigate.
更快的处理器、高速的网络和更大的存储容量等技术进步创造了一个复杂的螺旋,计算机科学家必须在其中穿行。
Throughout all of this rapid evolution, a number of basic principles have remained constant.
在计算机的整个快速发展过程中,一些基本原则始终不变。
The science of computing is concerned with using computers to solve problems.
计算科学与使用计算机解决有关问题。
You have no doubt spent considerable time learning the basics of problem-solving and hopefully feel confident in your ability to take a problem statement and develop a solution.
毫无疑问,你已经花了相当多的时间学习解决问题的基础知识,并希望对自己的问题陈述和开发解决方案的能力有信心。
You have also learned that writing computer programs is often hard.
你也知道编写计算机程序通常是很困难的。
The complexity of large problems and the corresponding complexity of the solutions can tend to overshadow the fundamental ideas related to the problem-solving process.
大问题的复杂性和解决办法的相应复杂性可能会掩盖与解决问题过程有关的基本思想。
This chapter emphasizes two important areas for the rest of the text.
这一章为正文的其余部分强调了两个重要方面。
First, it reviews the framework within which computer science and the study of algorithms and data structures must fit, in particular, the reasons why we need to study these topics and how understanding these topics helps us to become better problem solvers.
首先,它回顾了计算机科学和算法和数据结构的研究必须适应的框架,特别是,为什么我们需要学习这些主题,以及理解这些主题如何帮助我们来成为更好的问题解决者。
Second, we review the Python programming language.
其次,回顾Python编程语言。
Although we cannot provide a detailed, exhaustive reference, we will give examples and explanations for the basic constructs and ideas that will occur throughout the remaining chapters.
虽然我们不能提供一个详细的,详尽的参考,我们将给出例子和解释的基本结构和思想,将出现在其余章节。

1.3. What Is Computer Science?

1.3.什么是计算机科学?

Computer science is difficult to define.
计算机科学很难定义。
This is probably due to the unfortunate use of the word “computer” in the name.
这可能是由于名字中不恰当地使用了“computer”这个词。
As you are perhaps aware, computer science is not simply the study of computers.
正如你可能意识到的,计算机科学不仅仅是对计算机的研究。
Although computers play an important supporting role as a tool in the discipline, they are just that–tools.
虽然计算机作为一种工具在学科中起着重要的辅助作用,但它们只是工具而已。

Computer science is the study of problems, problem-solving, and the solutions that come out of the problem-solving process.
计算机科学是一门研究问题、解决问题以及解决问题过程的解决方法的科学。
Given a problem, a computer scientist’s goal is to develop an algorithm, a step-by-step list of instructions for solving any instance of the problem that might arise.
给定一个问题,计算机科学家的目标是开发一种算法,一种解决任何可能出现的问题实例的一步一步的指令列表。
Algorithms are finite processes that if followed will solve the problem.
算法是有限的过程,如果遵循它就能解决问题。
Algorithms are solutions.
算法就是解决方案。

Computer science can be thought of as the study of algorithms.
计算机科学可以被认为是对算法的研究。
However, we must be careful to include the fact that some problems may not have a solution.
然而,我们必须谨慎地考虑到这样一个事实,即有些问题可能没有解决办法。
Although proving this statement is beyond the scope of this text, the fact that some problems cannot be solved is important for those who study computer science.
虽然证明这一说法超出了本文的范围,但对于学习计算机科学的人来说,有些问题无法解决这一事实是重要的。
We can fully define computer science, then, by including both types of problems and stating that computer science is the study of solutions to problems as well as the study of problems with no solutions.
我们可以完全定义计算机科学,通过包括这两种类型的问题,并指出计算机科学是对问题的解决方案的研究,以及对没有解决方案的问题的研究。

It is also very common to include the word computable when describing problems and solutions.
在描述问题和解决方案时,包含可计算的这个词也是很常见的。
We say that a problem is computable if an algorithm exists for solving it.
我们说一个问题是可计算的,如果存在解决它的算法。
An alternative definition for computer science, then, is to say that computer science is the study of problems that are and that are not computable, the study of the existence and the nonexistence of algorithms.
那么,计算机科学的另一种定义是,计算机科学是对可计算和不可计算问题的研究,即对算法存在和不存在的研究。
In any case, you will note that the word “computer” did not come up at all.
无论如何,你会注意到“computer”这个词根本就没有出现。
Solutions are considered independent from the machine.
解决方案是独立于机器的。

Computer science, as it pertains to the problem-solving process itself, is also the study of abstraction.
计算机科学,由于它与解决问题的过程本身有关,也是一门抽象学。
Abstraction allows us to view the problem and solution in such a way as to separate the so-called logical and physical perspectives.
抽象允许我们以一种分离所谓的逻辑和物理视角的方式来看待问题和解决方案。
The basic idea is familiar to us in a common example.
在一个常见的例子中,基本思想是我们所熟悉的。

Consider the automobile that you may have driven to school or work today.
考虑一下你今天开去学校或工作的汽车。
As a driver, a user of the car, you have certain interactions that take place in order to utilize the car for its intended purpose.
作为一名司机,一名汽车的使用者,为了使用汽车达到预定的目的,你需要进行一定的交互。
You get in, insert the key, start the car, shift, brake, accelerate, and steer in order to drive.
你坐进车里,插入钥匙,发动汽车,换挡,刹车,加速,转向,以便驾驶。
From an abstraction point of view, we can say that you are seeing the logical perspective of the automobile.
从抽象的角度来看,我们可以说你看到的是汽车的逻辑视角。
You are using the functions provided by the car designers for the purpose of transporting you from one location to another.
你正在使用汽车设计师提供的功能,以便将你从一个地点运送到另一个地点。
These functions are sometimes also referred to as the interface.
这些函数有时也称为接口。

On the other hand, the mechanic who must repair your automobile takes a very different point of view.
另一方面,要修理你汽车的机修工却有着很是不同的看法。
She not only knows how to drive but must know all of the details necessary to carry out all the functions that we take for granted.
她不仅知道如何驾驶,而且必须知道执行我们认为理所当然的所有功能所必需的所有细节。
She needs to understand how the engine works, how the transmission shifts gears, how temperature is controlled, and so on.
她需要了解发动机是如何工作的,变速器是如何换挡的,温度是如何控制的,等等。
This is known as the physical perspective, the details that take place “under the hood.”
这就是所谓的物理视角,即发生在“引擎盖下”的细节。

The same thing happens when we use computers.
同样的事情也发生在我们使用电脑的时候。
Most people use computers to write documents, send and receive email, surf the web, play music, store images, and play games without any knowledge of the details that take place to allow those types of applications to work.
大多数人使用计算机编写文档、发送和接收电子邮件、浏览网页、播放音乐、存储图像和玩游戏,而不知道这些类型的应用程序工作时所发生的细节。
They view computers from a logical or user perspective.
他们从逻辑或用户的角度来看待计算机。
Computer scientists, programmers, technology support staff, and system administrators take a very different view of the computer.
计算机科学家、程序员、技术支持人员和系统管理员对计算机有不同的看法。
They must know the details of how operating systems work, how network protocols are configured, and how to code various scripts that control function.
他们必须知道操作系统是如何工作的细节,网络协议是如何配置的,以及如何编写控制功能的各种脚本。
They must be able to control the low-level details that a user simply assumes.
它们必须能够控制用户简单假设的底层细节。

The common point for both of these examples is that the user of the abstraction, sometimes also called the client, does not need to know the details as long as the user is aware of the way the interface works.
这两个例子的共同点是,抽象的用户,有时也称为客户端,只要用户知道接口的工作方式,就不需要知道细节。
This interface is the way we as users communicate with the underlying complexities of the implementation.
这个接口是我们作为用户与实现的底层复杂性进行通信的方式。
As another example of abstraction, consider the Python math module.
作为抽象的另一个例子,考虑Python数学模块。
Once we import the module, we can perform computations such as
导入模块后,就可以执行诸如此类的计算

>>> import math
>>> math.sqrt(16)
4.0
>>>

This is an example of procedural abstraction.
这是程序性抽象的一个例子。
We do not necessarily know how the square root is being calculated, but we know what the function is called and how to use it.
我们不一定知道如何计算平方根,但我们知道这个函数名以及如何使用它。
If we perform the import correctly, we can assume that the function will provide us with the correct results.
如果我们正确地执行导入,我们可以假定函数将为我们提供正确的结果。
We know that someone implemented a solution to the square root problem but we only need to know how to use it.
我们知道有人解决了平方根问题但我们只需要知道如何使用它。
This is sometimes referred to as a “black box” view of a process.
这有时被称为流程的“黑盒”视图。
We simply describe the interface: the name of the function, what is needed (the parameters), and what will be returned.
我们只是简单地描述接口:函数的名称、需要什么(参数)以及将返回什么。
The details are hidden inside (see Figure 1).
详细信息隐藏在内部(参见图1)。
Figure1
Figure 1: Procedural Abstraction
图1:过程抽象

1.4. What Is Programming?

1.4.编程是什么?

Programming is the process of taking an algorithm and encoding it into a notation, a programming language, so that it can be executed by a computer.
编程是将一种算法编码成一种符号,一种编程语言,这样它就可以被计算机执行的过程。
Although many programming languages and many different types of computers exist, the important first step is the need to have the solution.
尽管存在许多编程语言和许多不同类型的计算机,但重要的第一步是需要有解决方案。
Without an algorithm there can be no program.
没有算法就没有程序。

Computer science is not the study of programming.
计算机科学不是关于编程的研究。
Programming, however, is an important part of what a computer scientist does.
然而,编程是计算机科学家工作的一个重要部分。
Programming is often the way that we create a representation for our solutions.
编程通常是我们为解决方案创建表示的方式。
Therefore, this language representation and the process of creating it becomes a fundamental part of the discipline.
因此,这种语言表征及其创造过程成为该学科的一个基本组成部分。

Algorithms describe the solution to a problem in terms of the data needed to represent the problem instance and the set of steps necessary to produce the intended result.
算法根据表示问题实例所需的数据以及产生预期结果所需的步骤集来描述问题的解决方案。
Programming languages must provide a notational way to represent both the process and the data.
编程语言必须提供一种符号方式来表示过程和数据。
To this end, languages provide control constructs and data types.
为此,语言提供了控制结构和数据类型。

Control constructs allow algorithmic steps to be represented in a convenient yet unambiguous way.
控制结构允许以一种方便而明确的方式表示算法步骤。
At a minimum, algorithms require constructs that perform sequential processing, selection for decision-making, and iteration for repetitive control.
至少,算法需要执行顺序处理、决策选择和重复控制迭代的构造。
As long as the language provides these basic statements, it can be used for algorithm representation.
只要语言提供了这些基本语句,它就可以用于算法表示。

All data items in the computer are represented as strings of binary digits.
计算机中的所有数据项都用二进制数字串表示。
In order to give these strings meaning, we need to have data types.
为了赋予这些字符串意义,我们需要数据类型。
Data types provide an interpretation for this binary data so that we can think about the data in terms that make sense with respect to the problem being solved.
数据类型为这个二进制数据提供了一种解释,这样我们就可以根据要解决的问题来考虑数据。
These low-level, built-in data types (sometimes called the primitive data types) provide the building blocks for algorithm development.
这些低级的内置数据类型(有时称为原始数据类型)为算法开发提供了构建块。

For example, most programming languages provide a data type for integers.
例如,大多数编程语言都提供整数的数据类型。
Strings of binary digits in the computer’s memory can be interpreted as integers and given the typical meanings that we commonly associate with integers (e.g. 23, 654, and -19).
计算机内存中的二进制数字串可以被解释为整数,并给出我们通常与整数相联系的典型含义(例如23,654和-19)。
In addition, a data type also provides a description of the operations that the data items can participate in.
此外,数据类型还提供了数据项可以参与的操作的描述。
With integers, operations such as addition, subtraction, and multiplication are common.
对于整数,加法、减法和乘法等操作是常见的。
We have come to expect that numeric types of data can participate in these arithmetic operations.
我们期望数字类型的数据可以参与这些算术运算。

The difficulty that often arises for us is the fact that problems and their solutions are very complex.
我们经常遇到的困难是,问题及其解决办法是非常复杂的。
These simple, language-provided constructs and data types, although certainly sufficient to represent complex solutions, are typically at a disadvantage as we work through the problem-solving process.
这些简单的、由语言提供的结构和数据类型,虽然对于表示复杂的解决方案来说当然足够了,但在我们解决问题的过程中,它们通常处于不利地位。
We need ways to control this complexity and assist with the creation of solutions.
我们需要控制这种复杂性的方法,并协助创造解决方案。

在这里插入图片描述
2020.12.22今天暂且就到这吧…

1.5. Why Study Data Structures and Abstract Data Types?

1.4.为什么要学习数据结构和抽象数据类型?

To manage the complexity of problems and the problem-solving process, computer scientists use abstractions to allow them to focus on the “big picture” without getting lost in the details.
为了管理问题的复杂性和解决问题的过程,计算机科学家使用抽象使他们能够专注于“大局”而不迷失在细节中。
By creating models of the problem domain, we are able to utilize a better and more efficient problem-solving process.
通过创建问题域的模型,我们能够利用一个更好、更有效的问题解决过程。
These models allow us to describe the data that our algorithms will manipulate in a much more consistent way with respect to the problem itself.
这些模型允许我们描述我们的算法将以一种更一致的方式处理问题本身的数据。

Earlier, we referred to procedural abstraction as a process that hides the details of a particular function to allow the user or client to view it at a very high level.
在前面,我们将过程抽象称为一种过程,它隐藏了特定功能的细节,以允许用户或客户端在非常高的层次上查看它。
We now turn our attention to a similar idea, that of data abstraction.
现在我们将注意力转向一个类似的概念,即数据抽象。
An abstract data type, sometimes abbreviated ADT, is a logical description of how we view the data and the operations that are allowed without regard to how they will be implemented.
抽象数据类型,有时缩写为ADT,是一种逻辑描述,描述我们如何查看数据和允许的操作,而不考虑它们将如何实现。
This means that we are concerned only with what the data is representing and not with how it will eventually be constructed.
这意味着我们只关心数据表示的是什么,而不关心它最终将如何构造。
By providing this level of abstraction, we are creating an encapsulation around the data.
通过提供这种级别的抽象,我们正在围绕数据创建一个封装。
The idea is that by encapsulating the details of the implementation, we are hiding them from the user’s view.
其思想是,通过封装实现的细节,我们将它们隐藏在用户的视图之外。
This is called information hiding.
这被称为信息隐藏。

Figure 2 shows a picture of what an abstract data type is and how it operates.
图2显示了什么是抽象数据类型以及它是如何操作的。
The user interacts with the interface, using the operations that have been specified by the abstract data type.
用户使用抽象数据类型指定的操作与接口进行交互。
The abstract data type is the shell that the user interacts with.
抽象数据类型是用户与之交互的shell。
The implementation is hidden one level deeper.
实现隐藏在更深一层。
The user is not concerned with the details of the implementation.
用户并不关心实现的细节。
在这里插入图片描述
Figure 2: Abstract Data Type
图2:抽象数据类型

The implementation of an abstract data type, often referred to as a data structure, will require that we provide a physical view of the data using some collection of programming constructs and primitive data types.
抽象数据类型(通常称为数据结构)的实现需要我们使用一些编程结构和原始数据类型集合提供数据的物理视图。
As we discussed earlier, the separation of these two perspectives will allow us to define the complex data models for our problems without giving any indication as to the details of how the model will actually be built.
正如我们前面所讨论的,这两个透视图的分离将允许我们为我们的问题定义复杂的数据模型,而不提供任何关于模型将如何实际构建的细节的指示。
This provides an implementation-independent view of the data.
这提供了与实现无关的数据视图。
Since there will usually be many different ways to implement an abstract data type, this implementation independence allows the programmer to switch the details of the implementation without changing the way the user of the data interacts with it.
由于通常有许多不同的方法来实现抽象数据类型,这种实现独立性允许程序员在不改变数据用户与数据交互的方式的情况下切换实现的细节。
The user can remain focused on the problem-solving process.
用户可以专注于解决问题的过程。

1.6. Why Study Algorithms?

1.6. 为什么学习算法?

Computer scientists learn by experience.
计算机科学家从经验中学习。
We learn by seeing others solve problems and by solving problems by ourselves.
我们通过看到别人解决问题和自己解决问题来学习。
Being exposed to different problem-solving techniques and seeing how different algorithms are designed helps us to take on the next challenging problem that we are given.
接触到不同的解决问题的技术,看看不同的算法是如何设计的,有助于我们处理下一个遇到的挑战问题。
By considering a number of different algorithms, we can begin to develop pattern recognition so that the next time a similar problem arises, we are better able to solve it.
通过考虑许多不同的算法,我们可以开始发展模式识别,以便下次出现类似的问题时,我们能够更好地解决它。

Algorithms are often quite different from one another.
算法之间往往有很大的不同。
Consider the example of sqrt seen earlier.
考虑前面看到的sqrt示例。
It is entirely possible that there are many different ways to implement the details to compute the square root function.
完全有可能有许多不同的方法来实现计算平方根函数的细节。
One algorithm may use many fewer resources than another.
一种算法可能比另一种算法使用更少的资源。
One algorithm might take 10 times as long to return the result as the other.
一种算法返回结果的时间可能是另一种算法的10倍。
We would like to have some way to compare these two solutions.
我们想用某种方法来比较这两个解决方案。
Even though they both work, one is perhaps “better” than the other.
即使这两种方法都有效,但其中一种可能比另一种“更好”。
We might suggest that one is more efficient or that one simply works faster or uses less memory.
我们可能会建议一种更高效的方式,或者一种工作更快或使用更少的内存的方式。
As we study algorithms, we can learn analysis techniques that allow us to compare and contrast solutions based solely on their own characteristics, not the characteristics of the program or computer used to implement them.
当我们学习算法时,我们可以学习分析技术,使我们能够仅根据解决方案本身的特征来进行比较和对比,而不是根据执行它们的程序或计算机的特征。

In the worst case scenario, we may have a problem that is intractable, meaning that there is no algorithm that can solve the problem in a realistic amount of time.
在最坏的情况下,我们可能会遇到一个棘手的问题,这意味着没有算法可以在现实的时间内解决这个问题。
It is important to be able to distinguish between those problems that have solutions, those that do not, and those where solutions exist but require too much time or other resources to work reasonably.
重要的是要能够区分那些有解决方案的问题,那些没有解决方案的问题,以及那些有解决方案但需要太多时间或其他资源才能合理工作的问题。

There will often be trade-offs that we will need to identify and decide upon.
经常会有我们需要识别和决定的权衡。
As computer scientists, in addition to our ability to solve problems, we will also need to know and understand solution evaluation techniques.
作为计算机科学家,除了我们解决问题的能力,我们还需要知道和理解解决方案评估技术。
In the end, there are often many ways to solve a problem.
最后,往往有许多方法可以解决一个问题。
Finding a solution and then deciding whether it is a good one are tasks that we will do over and over again.
找到一个解决方案,然后决定它是否是一个好方案,是我们将一遍又一遍地做的任务。

1.7. Review of Basic Python

1.7.Python基础回顾

In this section, we will review the programming language Python and also provide some more detailed examples of the ideas from the previous section.
在本节中,我们将回顾编程语言Python,并提供一些更详细的例子来说明上一节的思想。
If you are new to Python or find that you need more information about any of the topics presented, we recommend that you consult a resource such as the Python Language Reference or a Python Tutorial.
如果您是Python新手,或者发现您需要更多关于本文主题的信息,我们建议您参考Python语言参考或Python教程等资源。
Our goal here is to reacquaint you with the language and also reinforce some of the concepts that will be central to later chapters.
我们在这里的目标是让你重新熟悉这门语言,并加强一些概念,这些概念将在后面的章节中成为核心。

Python is a modern, easy-to-learn, object-oriented programming language.
Python是一种现代的、易于学习的、面向对象的编程语言。
It has a powerful set of built-in data types and easy-to-use control constructs.
它有一组强大的内置数据类型和易于使用的控制结构。
Since Python is an interpreted language, it is most easily reviewed by simply looking at and describing interactive sessions.
由于Python是一种解释性语言,它最容易通过简单地查看和描述交互式会话来检查。
You should recall that the interpreter displays the familiar >>> prompt and then evaluates the Python construct that you provide.
您应该记得,解释器显示熟悉的>>>提示符,然后计算您提供的Python结构。
For example,
例如,

>>> print("Algorithms and Data Structures")
Algorithms and Data Structures
>>>

shows the prompt, the print function, the result, and the next prompt.
显示提示符、打印函数、结果和下一个提示符。

1.8. Getting Started with Data

1.8.从数据开始

We stated above that Python supports the object-oriented programming paradigm.
我们在上面说过Python支持面向对象编程范式。
This means that Python considers data to be the focal point of the problem-solving process.
这意味着Python将数据视为解决问题过程的焦点。
In Python, as well as in any other object-oriented programming language, we define a class to be a description of what the data look like (the state) and what the data can do (the behavior).
在Python中,以及在任何其他面向对象编程语言中,我们定义类来描述数据的样子(状态)和数据可以做什么(行为)。
Classes are analogous to abstract data types because a user of a class only sees the state and behavior of a data item.
类类似于抽象数据类型,因为类的用户只能看到数据项的状态和行为。
Data items are called objects in the object-oriented paradigm.
数据项在面向对象范式中称为对象。
An object is an instance of a class.
对象是类的一个实例。

1.8.1. Built-in Atomic Data Types

1.8.1. 内置原子数据类型

We will begin our review by considering the atomic data types.
我们将从考虑原子数据类型开始回顾。
Python has two main built-in numeric classes that implement the integer and floating point data types.
Python有两个主要的内置数值类,它们实现了整数和浮点数据类型。
These Python classes are called int and float.
这些Python类被称为int和float。
The standard arithmetic operations, +, -, *, /, and ** (exponentiation), can be used with parentheses forcing the order of operations away from normal operator precedence.

标准的算术运算,+、-、*、/和**(求幂),可以与括号一起使用,从而使操作的顺序偏离正常操作符的优先级。
Other very useful operations are the remainder (modulo) operator, %, and integer division, //.
其他非常有用的操作是余数(模)运算符%和整型除法//。
Note that when two integers are divided, the result is a floating point.
注意,当两个整数被除时,结果是一个浮点数。
The integer division operator returns the integer portion of the quotient by truncating any fractional part.
整型除法运算符通过截断任何小数部分返回商的整型部分。
示例如下:

print(2+3*4)
print((2+3)*4)
print(2**10)
print(6/3)
print(7/3)
print(7//3)
print(7%3)
print(3/6)
print(3//6)
print(3%6)
print(2**100)
#输出结果如下:
14
20
1024
2.0
2.333333333333333
2
1
0.5
0
3
1267650600228229401496703205376

The boolean data type, implemented as the Python bool class, will be quite useful for representing truth values.
作为Python bool类实现的布尔数据类型对于表示真值非常有用。
The possible state values for a boolean object are True and False with the standard boolean operators, and, or, and not.
对于标准的布尔操作符and、or和not,布尔对象的可能状态值分别为True和False。

>>> True
True
>>> False
False
>>> False or True
True
>>> not (False or True)
False
>>> True and True
True

Boolean data objects are also used as results for comparison operators such as equality (==) and greater than (>).

布尔数据对象也可用作比较运算符的结果,比如equal(==)和greater than(>)。
In addition, relational operators and logical operators can be combined together to form complex logical questions.
此外,关系运算符和逻辑运算符可以组合在一起形成复杂的逻辑问题。
Table 1 shows the relational and logical operators with examples shown in the session that follows.
表1展示了关系操作符和逻辑操作符,并在下面的会话中给出了示例。
Table 1: Relational and Logical Operators
表1:关系操作符和逻辑操作符
在这里插入图片描述

print(5==10)
print(10 > 5)
print((5 >= 1) and (5 <= 10))
#输出结果如下
False
True
True

Identifiers are used in programming languages as names.
标识符在编程语言中用作名称。
In Python, identifiers start with a letter or an underscore (_), are case sensitive, and can be of any length.

在Python中,标识符以字母或下划线(_)开头,区分大小写,长度可以任意。
Remember that it is always a good idea to use names that convey meaning so that your program code is easier to read and understand.
请记住,使用具有含义的名称总是一个好主意,这样您的程序代码更容易阅读和理解。

A Python variable is created when a name is used for the first time on the left-hand side of an assignment statement.
当名称第一次在赋值语句的左侧被使用时,将创建Python变量。
Assignment statements provide a way to associate a name with a value.
赋值语句提供了一种将名称与值相关联的方法。
The variable will hold a reference to a piece of data and not the data itself.
变量将保存对一段数据的引用,而不是数据本身。
Consider the following session:

>>> theSum = 0
>>> theSum
0
>>> theSum = theSum + 1
>>> theSum
1
>>> theSum = True
>>> theSum
True

The assignment statement theSum = 0 creates a variable called theSum and lets it hold the reference to the data object 0 (see Figure 3).
赋值语句theSum = 0创建了一个名为theSum的变量,并让它保存对数据对象0的引用(参见图3)。
In general, the right-hand side of the assignment statement is evaluated and a reference to the resulting data object is “assigned” to the name on the left-hand side.
通常,赋值语句的右手边被求值,对结果数据对象的引用被“赋值”给左手边的名称。
At this point in our example, the type of the variable is integer as that is the type of the data currently being referred to by theSum.
在我们的示例中,此时变量的类型是integer,因为它是当前由群集引用的数据的类型。
If the type of the data changes (see Figure 4), as shown above with the boolean value True, so does the type of the variable (theSum is now of the type boolean).
如果数据的类型发生了变化(参见图4),如上图所示,布尔值为True,那么变量的类型也会发生变化(theSum现在是布尔类型)。
The assignment statement changes the reference being held by the variable.
赋值语句改变变量持有的引用。
This is a dynamic characteristic of Python.
这是Python的一个动态特性。
The same variable can refer to many different types of data.
同一个变量可以引用许多不同类型的数据。
在这里插入图片描述
Figure 3: Variables Hold References to Data Objects
图3:变量包含对数据对象的引用
在这里插入图片描述
Figure 4: Assignment Changes the Reference
图4:赋值改变了引用
在这里插入图片描述
2020.12.23今天暂且就到这吧…

猜你喜欢

转载自blog.csdn.net/qq_51771374/article/details/111552790