Why with the *= operator is the order of operations altered?
The *= is not an operator, it's delimiter. Check the '2. Lexical analysis' of 'The Python Language Reference':
The following tokens are operators:
+ - * ** / // % @
<< >> & | ^ ~ :=
< > <= >= == !=
The following tokens serve as delimiters in the grammar:
( ) [ ] { }
, : . ; @ = ->
+= -= *= /= //= %= @=
&= |= ^= >>= <<= **=
The period can also occur in floating-point and imaginary literals. A
sequence of three periods has a special meaning as an ellipsis
literal. The second half of the list, the augmented assignment
operators, serve lexically as delimiters, but also perform an
operation.
Moreover, in your code you have the, augmented assignment statement, as per '7.2.1. Augmented assignment statements':
Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement:
[...]
An augmented assignment evaluates the target (which, unlike normal
assignment statements, cannot be an unpacking) and the expression
list, performs the binary operation specific to the type of assignment
on the two operands, and assigns the result to the original target.
The target is only evaluated once.
An augmented assignment expression like x += 1 can be rewritten as x =
x + 1 to achieve a similar, but not exactly equal effect. In the
augmented version, x is only evaluated once. Also, when possible, the
actual operation is performed in-place, meaning that rather than
creating a new object and assigning that to the target, the old object
is modified instead.
Unlike normal assignments, augmented assignments evaluate the
left-hand side before evaluating the right-hand side. For example,
a[i] += f(x) first looks-up a[i], then it evaluates f(x) and performs
the addition, and lastly, it writes the result back to a[i].
The '6.16. Evaluation order' says:
Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.
You can also check the '6.17. Operator precedence' chapter, but it's not about the statements.
result *= nim - 3is equivalent toresult = result * (num - 3)-last, then first you'd doresult *= num, and then you'd subtract 3 from that but not assign it to anything (because there's no assignment implied by the-operator). That would obviously be nonsensical, which is why it doesn't work like that. Instead the entire RHS of the*=gets evaluated together, as if you'd put parens around it, before the multiplication and assignment happens.