It's just like in algebra. You evaluate exponents before the - which,
after all, is just another way to write -1, or times-negative-one.
However, a variable with a negative value is not the same as a value
that is being multiplied by a negative.<br>
<br>-3 ** 2 = (-1)(3)^(2) in algebraic terms. Exponents first, then multiplication. <br><br>However,<br><br>x ** 2 = (x)^(2) = (-3)^(2)<br><br>regardless of the value of x which, in this case, is -3. When you multiply a negative by itself, you get a positive.<br>
<br>In short, the ** operator appears to have a higher precedence than the - operator, based on your results.<br><br><br><div class="gmail_quote">On Sun, Nov 29, 2009 at 6:39 PM, Esmail <span dir="ltr"><<a href="mailto:ebonak@hotmail.com">ebonak@hotmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Ok, this is somewhat unexpected:<br>
<br>
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)<br>
[GCC 4.3.3] on linux2<br>
Type "help", "copyright", "credits" or "license" for more information.<br>
<br>
<br>
>>> -3**2<br>
-9<br>
<br>
>>> x = -3<br>
<br>
>>> x**2<br>
9<br>
>>><br>
<br>
I would have expected the same result in both cases.<br>
<br>
Initially I would have expected -3**2 to yield 9, but I can accept<br>
that ** binds tighter than the unary -, but shouldn't the results<br>
be consistent regardless if I use a literal or a variable?<br><font color="#888888">
<br>
<br>
<br>
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br>