On 5/6/2013 11:31 AM, Roy Smith wrote:
In article <mailman.1361.1367847484.3114.python-l...@python.org>,
Chris Angelico  <ros...@gmail.com> wrote:
On Mon, May 6, 2013 at 11:08 PM, Roy Smith <r...@panix.com> wrote:
On the other hand, I've long since given up trying to remember operator
precedence in various languages.  If I ever have even the slightest
doubt, I just go ahead and put in the extra parens.

If I ever have even the slightest doubt, I just go ahead and type
"<language> operator precedence" into a web search and check it :)

Well, that solves the problem once, and it solves it for me.  I figure
if I'm not 100% sure, then maybe other people aren't 100% sure either,
and my adding the extra parens helps them too.

If you keep the Python docs handy, on or off line, the Language manual Expressions chapter ends with this single page (but better formatted as a table than here). But I sometimes add parens for quickness or readability.

6.15. Operator precedence
The following table summarizes the operator precedences in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right β€” see section Comparisons β€” and exponentiation, which groups from right to left).

Operator Description
lambda Lambda expression
if – else Conditional expression
or Boolean OR
and Boolean AND
not x Boolean NOT
in, not in, is, is not, <, <=, >, >=, !=, == Comparisons, including membership tests and identity tests,
| Bitwise OR
^ Bitwise XOR
& Bitwise AND
<<, >> Shifts
+, - Addition and subtraction
*, /, //, % Multiplication, division, remainder [5]
+x, -x, ~x Positive, negative, bitwise NOT
** Exponentiation [6]
x[index], x[index:index], x(arguments...), x.attribute Subscription, slicing, call, attribute reference (expressions...), [expressions...], {key: value...}, {expressions...} Binding or tuple display, list display, dictionary display, set display

Footnotes

[1] While abs(x%y) < abs(y) is true mathematically, for floats it may not be true numerically due to roundoff. For example, and assuming a platform on which a Python float is an IEEE 754 double-precision number, in order that -1e-100 % 1e100 have the same sign as 1e100, the computed result is -1e-100 + 1e100, which is numerically exactly equal to 1e100. The function math.fmod() returns a result whose sign matches the sign of the first argument instead, and so returns -1e-100 in this case. Which approach is more appropriate depends on the application. [2] If x is very close to an exact integer multiple of y, it’s possible for x//y to be one larger than (x-x%y)//y due to rounding. In such cases, Python returns the latter result, in order to preserve that divmod(x,y)[0] * y + x % y be very close to x. [3] While comparisons between strings make sense at the byte level, they may be counter-intuitive to users. For example, the strings "\u00C7" and "\u0327\u0043" compare differently, even though they both represent the same unicode character (LATIN CAPITAL LETTER C WITH CEDILLA). To compare strings in a human recognizable way, compare using unicodedata.normalize(). [4] Due to automatic garbage-collection, free lists, and the dynamic nature of descriptors, you may notice seemingly unusual behaviour in certain uses of the is operator, like those involving comparisons between instance methods, or constants. Check their documentation for more info. [5] The % operator is also used for string formatting; the same precedence applies. [6] The power operator ** binds less tightly than an arithmetic or bitwise unary operator on its right, that is, 2**-1 is 0.5.



--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to