Python手册外文翻译资料

 2022-05-15 10:05

The Python Tutorial

Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Pythonrsquo;s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.

The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python Web site, https://www.python.org/, and may be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation.

The Python interpreter is easily extended with new functions and data types implemented in C or C (or other languages callable from C). Python is also suitable as an extension language for customizable applications.

This tutorial introduces the reader informally to the basic concepts and features of the Python language and system. It helps to have a Python interpreter handy for hands-on experience, but all examples are self-contained, so the tutorial can be read off-line as well.

For a description of standard objects and modules, see The Python Standard Library. The Python Language Reference gives a more formal definition of the language. To write extensions in C or C , read Extending and Embedding the Python Interpreter and Python/C API Reference Manual. There are also several books covering Python in depth.

This tutorial does not attempt to be comprehensive and cover every single feature, or even every commonly used feature. Instead, it introduces many of Pythonrsquo;s most noteworthy features, and will give you a good idea of the languagersquo;s flavor and style. After reading it, you will be able to read and write Python modules and programs, and you will be ready to learn more about the various Python library modules described in The Python Standard Library.

The Glossary is also worth going through.

1 Using the Python Interpreter

1.1 Invoking the Interpreter

The Python interpreter is usually installed as /usr/local/bin/python3.6 on those machines where it is available; putting /usr/local/bin in your Unix shellrsquo;s search path makes it possible to start it by typing the command:

gt;gt;gt; python3.6

The interpreterrsquo;s line-editing features include interactive editing, history substitution and code completion on systems that support readline. Perhaps the quickest check to see whether command line editing is supported is typing Control-P to the first Python prompt you get. If it beeps, you have command line editing; see Appendix Interactive Input Editing and History Substitution for an introduction to the keys. If nothing appears to happen, or if ^P is echoed, command line editing isnrsquo;t available; yoursquo;ll only be able to use backspace to remove characters from the current line.

The interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, it reads and executes commands interactively; when called with a file name argument or with a file as standard input, it reads and executes a script from that file.

A second way of starting the interpreter is python -c command [arg] ..., which executes the statement(s) in command, analogous to the shellrsquo;s -c option. Since Python statements often contain spaces or other characters that are special to the shell, it is usually advised to quote command in its entirety with single quotes.

Some Python modules are also useful as scripts. These can be invoked using python -m module [arg] ..., which executes the source file for module as if you had spelled out its full name on the command line.

When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. This can be done by passing -i before the script.

All command line options are described in Command line and environment.

Argument Passing

When known to the interpreter, the script name and additional arguments thereafter are turned into a list of strings and assigned to the argv variable in the sys module. You can access this list by executing import sys. The length of the list is at least one; when no script and no arguments are given, sys.argv[0] is an empty string. When the script name is given as #39;-#39;(meaning standard input), sys.argv[0] is set to #39;-#39;. When -c command is used, sys.argv[0] is set to #39;-c#39;. When -m module is used, sys.argv[0] is set to the full name of the located module. Options found after -c command or

Python手册

Python解释器和广泛的标准库可以从Python网站https://www.python.org/以源代码或二进制形式免费获得,并且可以自由散布。站点还包含许多免费的第三方Python模块,程序和工具以及其他文档的发行版。

本教程通俗的地向读者介绍了Python语言和系统的基本概念和功能。 如果有python解释器来示范的话会更好,但所有示例都是独立的,因此该教程也可以脱机阅读。

本教程不会尝试全面,涵盖每个功能,甚至每个常用功能。 相反,它引入了许多Python最值得注意的特性,并且会给你一个关于该语言的风格和特征的介绍。 阅读完它后,您将能够读写Python小程序,并且有助于你去了解更多关于Python标准库中描述的各种Python库模块的信息。

1使用Python解释器

    1. Python解释器通常以目录/usr/local/bin/python3.6安装在可用的机器上; 把/ usr / local / bin放在你的Unix shell的环境变量后可以通过输入以下命令启动它:

      解释器的行编辑功能支持逐行解释式的系统交互式编辑,历史替换和代码完成。 也许,查看命令行编辑是否受支持的最快检测方式是在您获得的第一个Python提示符处输入Control-P。 如果它发出嘟嘟声,您将可以进行命令行编辑; 请参阅附录“交互式输入编辑”和“历史替换”以了解对密钥的介绍。 如果没有任何事情发生,或者^ P被回显,则命令行编辑不可用; 您只能使用退格删除当前行中的字符。

      第二种启动解释器的方法是python -c command [arg] ...,它执行命令中的语句,类似于shell的-c选项。 由于Python语句通常包含空格或其他特殊的字符,因此通常建议使用单引号包含整个命令。

      当使用脚本文件时,有时可以运行脚本并在之后进入交互模式。 这可以通过在执行脚本时传递-i参数来完成。

      参数传递

      解释模式

      解释器及其环境

      lt;a href='https://docs.py

      默认情况下,Python源文件以UTF-8编码。 世界上大多数语言支持该编码,在文字,标识符和注释中使用 - 尽管标准库只使用ASCII字符作为标识符,这是任何可移植代码应遵循的约定。 要正确显示这些字符,编辑器必须识别该文件是UTF-8,并且它必须使用支持文件中所有字符的字体。

      2 Python的正式介绍

      在以下示例中,通过是否存在提示(gt;gt;gt;和...)来区分输入和输出:要重复该示例,必须在出现提示后键入所有内容; 从解释器输出不以提示开头的行。 请注意,在一个例子中,一行中的次要提示意味着你必须输入一个空白行; 这用于结束多行命令。

      本手册中的许多示例(包括在交互式提示中输入的示例)都包含注释。 Python中的注释以字符#开始,并延伸到行的末尾。 注释可能出现在行的开头、空白处或代码之后,但不会出现在字符串文字中。 字符串文字中的哈希字符只是一个哈希字符。 由于注释是为了阐明代码,而不是由Python解释,因此在键入示例时可忽略它们。

      下面是一些例子:

      # this is the first comment

      spam = 1 # and this is the second comment

      # ... and now a third!

      text = ' # This is not a comment because it#39;s inside quotes.'

      2.1用python作为计算器

      我们来试一下简单的Python命令。 启动解释器并等待主提示符gt;gt;gt;(它花的时间应该不长)。

      2.1.1数字

      解释器的作用就像一个简单的计算器:你可以在它上面输入一个表达式,它会计算出这个值。 表达式语法很简单:运算符 , - ,*和/的工作方式与大多数其他语言(例如Pascal或C)类似; 圆括号(())可用于分组。 例如:

      gt;gt;gt; 2 2

      4

      gt;gt;gt; 50 - 5*6

      20

      gt;gt;gt; (50 - 5*6) / 4

      5.0

      gt;gt;gt; 8 / 5 # division always returns a floating point number

      1.6

      整数(例如2,4,20)类型为int,具有小数部分(例如5.0,1.6)的数字类型为float。本教程稍后会介绍更多关于数字类型的内容。

      除(/)总是返回一个浮点数。 要进行划分运算并获得整数结果(放弃任何小数结果),可以使用//运算符;计算余数时你可以使用%。

      在Python中,可以使用**运算符来进行平方运算。

      等号(=)用于为变量赋值。 之后,在下一个交互式提示之前不会显示任何结果。

      如果一个变量没有被“定义”(赋值),试图使用它将会出现错误:

      gt;gt;gt; n # try to access an undefined variable

      Traceback (most recent call last):

      File 'lt;stdingt;', line 1, in lt;modulegt;

      NameError: name #39;n#39; is not defined

      2.1.2字符串

      除了数字之外,Python还可以处理字符串,可以用几种方式来表示。 它们可以用单引号(#39;...#39;)或双引号(“...”)括起来,两者等价。 \可以用来作为转义字符。

      在交互式解释器中,输出字符串用引号括起来,特殊字符用反斜线转义。 虽然这有时看起来不同于输入(封闭引号可能会改变),但这两个字符串是等价的。 如果字符串包含单引号且不带双引号,则该字符串将用双引号括起来,否则将用单引号括起来。 print()函数通过省略封闭引号并打印转义字符和特殊字符来产生更易读的输出。

      如果您不希望以\开头的字符被解释为特殊字符,则可以在第一个引号之前添加一个r来输出原始字符串。

      字符串文字可以跨越多行。 一种方法是使用三引号:“”“...”“”或#39;#39;#39;...#39;#39;#39;。 行尾自动包含在字符串中,但可以通过在行尾添加\来防止这种情况。 以下为示例:

      print('''\

      Usage: thingy [OPTIONS]

      -h Display this usage message

      -H hostname Hostname to connect to

      ''')

      产生以下输出(请注意,不包括初始换行符):

      Usage: thingy [OPTIONS]

      -h Display this usage message

      -H hostname Hostname to connect to

      字符串可以与 运算符串接(连接在一起),并用*表示重复:

      gt;gt;gt; # 3 times #39;un#39;, followed by #39;ium#39;

      gt;gt;gt; 3 * #39;un#39; #39;ium#39;

      #39;unununium#39;

      字符串可以被索引(用下标),第一个字符的索引为0。没有单独的字符类型; 一个字符只是一个大小为1的字符串。

      2.1.3列表

      Python拥有许多复合数据类型,用于将其他值组合在一起。 最通用的是列表,其可以用方括号表示并用逗号分隔值(条目)。 列表可能包含不同类型的条目,但通常这些条目都具有相同的类型。

      gt;gt;gt; squares = [1, 4, 9, 16, 25]

      gt;gt;gt; squares

      [1, 4, 9, 16, 25]

      像字符串(以及所有其他内置序列类型)一样,列表可以被索引和划分。所有划分操作都将返回一个包含所请求元素的新列表。 这意味着下面的划分将返回列表的新(浅)副本:

      gt;gt;gt; squares[:]

      [1, 4, 9, 16, 25]

      与不可变的字符串不同,列表是一种可变类型,即可以更改它们的内容。您还可以使用append()方法在列表末尾添加新项目(我们将在后面看到关于该方法的更多信息):

      gt;gt;gt; cubes.append(216) # add the cube of 6

      gt;gt;gt; cubes.append(7 ** 3) # and the cube of 7

      gt;gt;gt; cubes[1, 8, 27, 64, 125, 216, 343]

      2.2编程第一步

      当然,我们还可以将Python用于更复杂的任务,而不是仅仅做两两相加运算。 例如,我们可以按如下方式编写斐波那契数列的初始子序列:

      gt;gt;gt; # Fibonacci series:

      ... # the sum of two elements defines the next

      ... a, b = 0, 1

      gt;gt;gt; while b lt; 10:

      ... print(b)

      ... a, b = b, a b

      ...

      1

      1

      2

      3

      5

      8

      这个例子用来介绍几个新功能:

      第一行包含一个多重赋值:变量a和b同时得到新的值0和1。在最后一行,再次使用它,表明右边的表达式也可以进行赋值,右侧表达式从左到右进行赋值。

      只要条件(此处:b lt;10)保持为真,while循环就会执行。 在Python中,与C一样,任何非零整数值都是true; 零是false值。 条件也可以是字符串或列表值,实际上是任何序列; 任何长度不为零的序列都是true值,空序列是false值。 本例中使用的测试是一个简单的比较。 标准比较运算符的写法与C中相同:lt;(小于),gt;(大于),==(等于),lt;=(小于或等于),gt; =(大于或等于) 和!=(不等于)。

      循环的主体是缩进的:缩进是Python对语句进行分组的方式。 在交互式提示中,您必须为每个缩进行输入一个或多个空格。 在实践中,您将使用文本编辑器为Python准备更复杂的输入; 所有正规的文本编辑器都有自动缩进功能。 当交互式输入复合语句时,必须在后面跟一个空行来表

      全文共8271字,剩余内容已隐藏,支付完成后下载完整资料


      资料编号:[12363],资料为PDF文档或Word文档,PDF文档可免费转换为Word

原文和译文剩余内容已隐藏,您需要先支付 30元 才能查看原文和译文全部内容!立即支付

以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。