Bug or intended behavior?

Skip Montanaro skip.montanaro at gmail.com
Mon Jun 5 16:35:17 EDT 2017


On Mon, Jun 5, 2017 at 2:26 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Interestingly, however, Python hasn't extended that principle to the
> expression syntax. You could have:
>
>    >>> 1 + 2*3
>    7
>    >>> 1+2 * 3
>    9

In a later post, you referenced a Wikipedia page on order of operations
<https://en.wikipedia.org/wiki/Order_of_operations>, in which it states:

*The order of operations used throughout mathematics, science, technology
and many computer programming languages is expressed here...*


Python's syntax is indentation-based because Guido and the people he worked
with on the ABC language (I think) at CWI observed that it was easier for
new programmers to understand the program's logic if it was forced to
behave the way it looks ("Python, programming the way Guido indented").
So-called high-level programming languages having been available for only
about 30-40 years at that point, there was still valuable research to be
done on the "best" way to structure your code, especially for people new to
programming. OTOH, changing the way order of operations is specified from
the way settled on in mathematics over the past few hundred years (and
which every algebra student learns) hardly makes sense. In this particular
case, the OP simply made a mistake, one, which when explained, probably
made perfect sense to him. Like many other computer languages, Python has
overloaded arithmetic operators a bit, and even though '%' isn't the most
common of the basic arithmetic operators, it still has the same precedence
it would have if it occurred in a purely arithmetic expression:

  1 + 372 % 19

Skip



More information about the Python-list mailing list