semantics of ** (unexpected/inconsistent?)

Brian J Mingus Brian.Mingus at Colorado.EDU
Sun Nov 29 19:48:52 EST 2009


On Sun, Nov 29, 2009 at 5:39 PM, Esmail <ebonak at hotmail.com> wrote:

> Ok, this is somewhat unexpected:
>
> Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
>
> >>> -3**2
> -9
>
> >>> x = -3
>
> >>> x**2
> 9
> >>>
>
> I would have expected the same result in both cases.
>
> Initially I would have expected -3**2 to yield 9, but I can accept
> that ** binds tighter than the unary -, but shouldn't the results
> be consistent regardless if I use a literal or a variable?
>

I think you answered your own question. 3**2 comes first in the order of
operations, followed by the negation.

>>> (-3)**2
9
>>> 3**2
9
>>> -3**2
-9
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091129/0e5c80b9/attachment-0001.html>


More information about the Python-list mailing list