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:
解释器的行为就像是一个计算器。你可以向它输入一个表达式,它会返回结果。表达式的语法简明易懂:+
,-
,*
,/
和大多数语言中的用法一样(比如C或Pascal),括号用于分组。例如:
>>> 2+2 4 >>> # This is a comment ... 2+2 4 >>> 2+2 # and a comment on the same line as code 4 >>> (50-5*6)/4 5 >>> # Integer division returns the floor: ... 7/3 2 >>> 7/-3 -3
Like in C, the equal sign ("=") is used to assign a value to a variable. The value of an assignment is not written:
像c一样,等号("=")用于给变量赋值。被分配的值是只读的。
>>> width = 20 >>> height = 5*9 >>> width * height 900
A value can be assigned to several variables simultaneously:
同一个值可以同时赋给几个变量:
>>> x = y = z = 0 # Zero x, y and z >>> x 0 >>> y 0 >>> z 0
There is full support for floating point; operators with mixed type operands convert the integer operand to floating point:
Python完全支持浮点数,不同类型的操作数混在一起时,操作符会把整型转化为浮点数。
>>> 3 * 3.75 / 1.5 7.5 >>> 7.0 / 2 3.5
Complex numbers are also supported; imaginary numbers are written with a suffix of "j" or "J". Complex numbers with a nonzero real component are written as "(real+imagj)", or can be created with the "complex(real, imag)" function.
复数也同样得到了支持,虚部由一个后缀"j"或者"J"来表示。带有非零实部的复数记为"real+imagj)",或者也可以通过"complex(real, img)"函数创建。
>>> 1j * 1J (-1+0j) >>> 1j * complex(0,1) (-1+0j) >>> 3+1j*3 (3+3j) >>> (3+1j)*3 (9+3j) >>> (1+2j)/(1+1j) (1.5+0.5j)
Complex numbers are always represented as two floating point numbers,
the real and imaginary part. To extract these parts from a complex
number z, use z.real
and z.imag
.
复数总是由实部和虚部两部分浮点数来表示。可以从 z.real
和 z.imag
得到复数z的实部和虚部。
>>> a=1.5+0.5j >>> a.real 1.5 >>> a.imag 0.5
The conversion functions to floating point and integer
(float(), int() and long()) don't
work for complex numbers -- there is no one correct way to convert a
complex number to a real number. Use abs(z)
to get its
magnitude (as a float) or z.real
to get its real part.
用于向浮点数和整型转化的函数(float(), int() 和 long())不能对复数起作用--没有什么方法可以将复数转化为实数。可以使用abs(z)取得它的模,也可以通过z.real得到它的实部。
>>> a=3.0+4.0j >>> float(a) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: can't convert complex to float; use abs(z) >>> a.real 3.0 >>> a.imag 4.0 >>> abs(a) # sqrt(a.real**2 + a.imag**2) 5.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:
交互模式下,最近一次表达式输出保存在 _ 变量中。这意味着把 Python 当做桌面计算器使用时,它可以更方便的进行连续计算,例如:
>>> 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.
这个变量对于用户来说是只读的。不要试图去给它赋值--限于 Python 的语法规则,你只会创建一个同名的局部变量覆盖它。