1.在Python中,表达式 2 *(3+4)的值是什么?
答:值为14, 2* 7的结果,因为小括号强制在乘法之前做加法

 

2.在Python中,表达式 2 * 3 + 4 的值是什么?
答:值为10,6+4的结果。Python操作符优先规则在没有小括号时生效,而且乘法优先于加法(在加法之前生效)。

 

3.在Python中,表达式 2 + 3 * 4 的值是什么?
答:值为14, 2+12的结果,理由同上。

 

4.为了计算一个数字的平方根,你可以使用什么工具,那它的平方呢?
答:在导入的math模块中,求平方根的函数,同 π,正切和更多别的函数,可以得到。为了求一个数字的平方根,import math 然后 调用 math.sqrt(N)。为了求一个数字的平方,要么使用指数表达式 X ** 2,要么使用内建函数 pow(X, 2)。当幂为0.5,后面两个任意一个可以计算平方根(e.g. X ** .5)

 

5.表达式 1 + 2.0 + 3 的值是什么类型?
答:结果将是一个浮点数:整数被转换为浮点(表达式中最复杂的类型),浮点数学用于计算浮点数。

 

6.你如何截断(truncate)或者 精确浮点数?
答:函数 int(N) 和 math.trunc(N) 截断,然后函数 round(N, digits)精确。我们也可以用math.floor(N)向下取整而使用字符串格式化操作显示精确结果。

 

7.你如何把整型转化为浮点数?
函数float(I)把整型转换为浮点数;在一个表达式中混合整型和浮点数也会导致转化。在某种意义上说,Python 3.X / 除法也这样转化——即时操作数都是整型,这个操作会返回包括了余数的浮点数结果。

 

8.你如何用八进制、十六进制或二进制展示整型?
答:内建函数oct(I)和hex(I)返回整数的八进制和十六进制字符串形式。在Python 2.6 ,3.0 和之后的版本,调用bin(I)也会返回数字的二进制位字符串。字符串格式化表达式 % 和 字符串方法 format 也提供了这样转化的目标。

 

9.你如何把八进制、十六进制或二进制转换为简单的整型?
答:函数int(S, base[进制])可以被用来把八进制和十六进制得到字符串转化为正常整型(把8,16或者2传入参数base)。函数eval(S)也可以用来这个目的,但是用来运行显得不值而且有安全问题。注意到整型在计算机内存中经常被存为二进制形式;这些只是显示字符串格式的转换。

 

标注:转载《Learning Python 5th Edition》[奥莱理]

1. What is the value of the expression 2 * (3 + 4) in Python?
2. What is the value of the expression 2 * 3 + 4 in Python?
3. What is the value of the expression 2 + 3 * 4 in Python?
4. What tools can you use to find a number’s square root, as well as its square?
5. What is the type of the result of the expression 1 + 2.0 + 3?
6. How can you truncate and round a floating-point number?
7. How can you convert an integer to a floating-point number?
8. How would you display an integer in octal, hexadecimal, or binary notation?
9. How might you convert an octal, hexadecimal, or binary string to a plain integer?

1. The value will be 14, the result of 2 * 7, because the parentheses force the addition to happen before the multiplication.
2. The value will be 10, the result of 6 + 4. Python’s operator precedence rules are applied in the absence of parentheses, and multiplication has higher precedence than (i.e., happens before) addition, per Table 5-2.
3. This expression yields 14, the result of 2 + 12, for the same precedence reasons as in the prior question.
4. Functions for obtaining the square root, as well as pi, tangents, and more, are available in the imported math module. To find a number’s square root, import math and call math.sqrt(N). To get a number’s square, use either the exponent expression X ** 2 or the built-in function pow(X, 2). Either of these last two can also compute the square root when given a power of 0.5 (e.g., X ** .5).
5. The result will be a floating-point number: the integers are converted up to floating point, the most complex type in the expression, and floating-point math is used to evaluate it.
6. The int(N) and math.trunc(N) functions truncate, and the round(N, digits) function rounds. We can also compute the floor with math.floor(N) and round for display with string formatting operations.
7. The float(I) function converts an integer to a floating point; mixing an integer with a floating point within an expression will result in a conversion as well. In some sense, Python 3.X / division converts too—it always returns a floating-point result that includes the remainder, even if both operands are integers.
8. The oct(I) and hex(I) built-in functions return the octal and hexadecimal string forms for an integer. The bin(I) call also returns a number’s binary digits string in Pythons 2.6, 3.0, and later. The % string formatting expression and format string method also provide targets for some such conversions.
9. The int(S, base) function can be used to convert from octal and hexadecimal strings to normal integers (pass in 8, 16, or 2 for the base). The eval(S) function can be used for this purpose too, but it’s more expensive to run and can have security issues. Note that integers are always stored in binary form in computer memory; these are just display string format conversions.