首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python•CHAPTER THREE

【导言】《Python Tutorial, Release 3.6.4》是适合Python3.6.4语言版本的教程,该教程英文原版来自Python官网,内容权威,版本最新,注重基础,示例代码准确无误,学习高效快捷,非常适合Python编程初学者入门。

该教程英文原版版权属于Python官网 https://www.python.org/ 所有,从英文原版译出的中文版权归译者华哥所有。

该教程发布形式:前部分为中文译文,后部分为对应的英文原文。中文译文中主要示例直接用Python3.6.4解释器演示。

该教程英文原版包括16章和附录共150多页,将分期连续发布,敬请期待!

该教程《第三章 Python简要导言》的译文分成三部分,将分三次连续发布,这是第一部分(1 / 3)。

第三章 Python简要导言

在下列示例中,输入输出是由提示符(>>> and …)的存在与不存在来区分的:要重复这个示例,你必须在提示符后面键入所有的东西,当提示符出现的时候,不从提示符开始的行是来自解释器的输出。注意:在示例中,行上次提示符本身意味着你必须键入一个空白行,这是用于结束多行命令。

这本手册中的许多例子,即使这些在交互提示符上输入的例子,也包括注释。在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's inside quotes."(“这不是一行注释,因为它在引号里”)

3.1将Python用作计算器

让我们试试一些简单的Python命令。启动解释器并等待主提示符,>>>(这应该不会花费太长时间)。

3.1.1数字

解释器充当一个简单计算器:你可以在它上面键入一个表达式而它将输出这个数值。表达式语法是简单的:运算符+,-,*和/,就像在许多其它语言里一样运行(例如:Pascal 或 C);圆括号(())可用于组合。例如:

整数数字(如:2,4,20)拥有int类型,而带有小数部分的数字(如:5.0,1.6)拥有float类型。后面,我们将在教程中看到更多关于数字的类型。

除法(/)总是返回一个浮点型数字。作向下取整除法得到一个整数结果(放弃任何小数结果),你可以用//运算符;计算余数,你可以用%运算符:

Python使用**运算符计算幂:

>>> 5 ** 2 # 5 squared(5的乘方)

25

>>> 2 ** 7 # 2 to the power of 7(2的7次幂)

128

等号=用于赋值给变量。此后,在下一个交互提示符之前,没有结果被显示出来:

>>> width = 20

>>> height = 5 * 9

>>> width * height

如果变量不是“被定义”(赋值),试着使用它,将会给出错误:

>>>n # try to access an undefined variable(试图获取未定义变量)

Traceback(most recent call last):(回溯(最近一次调用))

File"", line 1, in (文件"",行1,在里)

NameError:name 'n' is not defined(变量名错误:变量名‘n’未被定义)

对于浮点有充分的支持,带有混合类型操作数的运算符把整数操作数转换成浮点:

>>>4 * 3.75 - 1

14.0

在交互模式中,最后一个被打印出来的表达式被赋给变量_。这意味着当你正在把Python作为一个桌面计时器来使用时,持续计算更容易点,例如:

这个变量应该被用户当作只读的。不要明确地赋值给它——你会创建一个带有覆盖內建变量同一名称的、具有不可思议行为的独立的局部变量。

除整型和浮点型之外,Python支持其它类型的数字,诸如十进制数和小数。Python也有支持复数的內建类型,使用j或J后缀显示虚数部分(例如:3+5j)。

CHAPTER THREE AN INFORMAL INTRODUCTION TO PYTHON

In the following examples, input and output are distinguished by the presence or absence of prompts (>>> and …):to repeat the example, you must type everything after the prompt, when the prompt appears; lines that do not begin with a prompt are output from the interpreter. Note that a secondary prompt on a line by itself in an example means you must type a blank line; this is used to end a multi-line command.

Many of the examples in this manual, even those entered at the interactive prompt, include comments. Comments in Python start with the hash character, #, and extend to the end of the physical line. A comment may appear at the start of a line or following whitespace or code, but not within a string literal. A hash character within a string literal is just a hash character.Since comments are to clarify code and are not interpreted by Python, they maybe omitted when typing in examples.

Some examples:

# 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's inside quotes."

3.1 Using Python as a Calculator

Let’s try some simple Python commands. Start the interpreter and wait for the primary prompt, >>>. (It shouldn’t take long.)

3.1.1 Numbers

The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward: the operators +, -, * and / work just like in most other languages (for example,Pascal or C); parentheses (()) can be used for grouping. For example:

4

>>> 50 - 5*6

20

>>> (50 - 5*6) / 4

5.0

>>> 8 / 5 # division always returns a floating point number

1.6

The integer numbers (e.g. 2, 4, 20) have type int, the ones with afractional part (e.g. 5.0, 1.6) have type float. We will see more about numeric types later in the tutorial.

Division (/) always returns a float. To do floor division and get an integer result (discarding any fractional result) you can use the // operator; to calculate the remainder you can use %:

>>> 17 / 3 # classic division returns a float

>>> 17 // 3 # floor division discards the fractional part

5

>>> 17 % 3 # the % operator returns the remainder of the division

2

>>> 5 * 3 + 2 # result * divisor + remainder

17

With Python, it is possible to use the ** operator to calculate powers:

>>> 5 ** 2 # 5 squared

25

>>> 2 ** 7 # 2 to the power of 7

128

The equal sign (=) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt:

>>> width = 20

>>> height = 5 * 9

>>> width * height

If a variable is not “defined” (assigned a value), trying to use it will give you an error:

>>> n # try to access an undefined variable

Traceback (most recent call last):

File "", line 1, in

NameError: name 'n' is not defined

There is full support for floating point; operators with mixed type operands convert the integer operand to floating point:

>>> 4 * 3.75 - 1

14.0

In interactive mode, the last printed expression is assigned to the variable _. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example:

>>> tax = 12.5 / 100

>>> price = 100.50

>>> price * tax

12.5625

>>> price + _

113.0625

>>> round(_, 2)

113.06

This variable should be treated as read-only by the user. Don’t explicitly assign a value to it — you would create an independent local variable with the same name masking the built-in variable with its magic behavior.

In addition to int and float, Python supports other types of numbers, such as Decimal and Fraction.Python also has built-in support for complex numbers, and uses the j or J suffix to indicate the imaginary part (e.g. 3+5j).

未完待续

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180206G0ITPL00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券