Mathematical operators (such as + and -) provided our first example of a method of combination, but we have yet to define an evaluation procedure for expressions that contain these operators.

    数学运算符(如+和-)提供了我们第一个组合方法的例子,但是我们还没有为包含这些运算符的表达式定义一个求值过程。

    Python expressions with infix operators each have their own evaluation procedures, but you can often think of them as short-hand for call expressions. When you see

    带有Infix操作符的 python 表达式各有自己的计算过程,但是您通常可以将它们看作是调用表达式的简便操作。当你看到

    >>> 2 + 3
    5
    simply consider it to be short-hand for

    简单地认为它是

    >>> add(2, 3)
    5
    Infix notation can be nested, just like call expressions. Python applies the normal mathematical rules of operator precedence, which dictate how to interpret a compound expression with multiple operators.

    Infix符号可以被嵌套,就像调用表达式一样。Python 应用了正常的数学运算符优先规则,它决定了如何解释一个有多个运算符的复合表达式。

    >>> 2 + 3 * 4 + 5
    19
    evaluates to the same result as

    计算出的结果与下面的一样

    >>> add(add(2, mul(3, 4)), 5)
    19

    The nesting in the call expression is more explicit than the operator version, but also harder to read. Python also allows subexpression grouping with parentheses, to override the normal precedence rules or make the nested structure of an expression more explicit.

    调用表达式中的嵌套比操作符版本更明确,但也更难阅读。Python还允许带括号的子表达式分组,以覆盖正常的优先规则或使表达式的嵌套结构更加明确。

    >>> (2 + 3) * (4 + 5)
    45
    evaluates to the same result as

    计算出的结果与下面的一样

    >>> mul(add(2, 3), add(4, 5))
    45

    When it comes to division, Python provides two infix operators: / and //. The former is normal division, so that it results in a floating point, or decimal value, even if the divisor evenly divides the dividend:

    当涉及到除法时,python 提供了两个中缀操作符:///。前者是正规除法,即使除数均匀地除以被除数,也会得到浮点数或小数值:

    >>> 5 / 4
    1.25
    >>> 8 / 4
    2.0
    The // operator, on the other hand, rounds the result down to an integer:

    另一方面,//操作符将结果四舍五入为整数:

    >>> 5 // 4
    1
    >>> -5 // 4
    -2
    These two operators are shorthand for the truediv and floordiv functions.

    这两个操作符是 truediv 和 floordiv 函数的简写。

    >>> from operator import truediv, floordiv
    >>> truediv(5, 4)
    1.25
    >>> floordiv(5, 4)
    1

    You should feel free to use infix operators and parentheses in your programs. Idiomatic Python prefers operators over call expressions for simple mathematical operations.

    你可以在程序中使用infix操作符和括号。对于简单的数学运算,习惯使用的 python 的偏爱运算符,而不喜欢调用表达式。