Let’s try some simple Python commands. Start the interpreter and wait for the primary prompt, >>>. (It shouldn’t take long.)让我们尝试一些简单的Python命令。启动解释器并等待主提示符> > >。(这不需要很长时间。)
1、Numbers 用Python做算术运算
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: 解释器充当一个简单的计算器:您可以在其上键入算术表达式,python计算出相应的结果。表达式语法很简单:运算符+、-、*和/大多数其他语言(例如,Pascal或C)一样;括号()可以用于分组。例如:
>>> 2 + 2
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 a fractional part (e.g. 5.0, 1.6) have type float. We will see more about numeric types later in the tutorial.像2,4,20这些整数是整型int
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结果是浮点数
5.666666666666667
>>>
>>> 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 [1]:用python进行次方运算即运算幂
>>> 5 ** 2 # 5 squared 5的平方
25
>>> 2 ** 7 # 2 to the power of 7 2的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 # 20 assign to width 20赋值给width
>>> height = 5 * 9
>>> width * height
900
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:
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:
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 + _ #相对最后一个表达式的值12.5625给变量,这里翻译过来即是100.50+12.5625
113.0625
>>> round(_, 2) #相对最后一个表达式的值113.0625给变量,这里翻译过来即是小数点右边只要2位四舍五入,使用round函数。
113.06
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).
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).除了整型和浮点型,Python支持其他类型的数字类型,如小数和分数。Python还具有对复数的内置支持,并且使用J或J后缀表示虚部(例如3 +5J)。