Python Augmented Assignment Operators, Operator Associativity/Precedence & How Taking PEMDAS Literally Might Lead You Astray

In this brief video tutorial I go over the use of Python’s augmented assignment operators, operator associativity/precedence and why taking PEMDAS literally might lead you to the wrong answer. This subject caused some confusion in the course material when the evaluation of an expression using non-augmented assignment operators (a = a / 2 * b where a = 6 and b = 3) gave us an answer of 9.0 and the “equivalent” operation using augmented assignment operators (a /= 2 * b where a = 6 and b = 3) returned an answer of 1.0 leading to some confusion. I go over why we receive two different answers and how PEMDAS should actually be thought of as:

P E [MD] [AS]

to stress that multiplication and division share the same precedence…in other words, they are ‘associated’. When two operators with the same precedence are in an expression, it is operator associativity (which for multiplication/division/addition/subtraction is LEFT TO RIGHT) kicks in and might give you an answer very different from the one you were anticipating were you to interpret PEMDAS literally from an order of operations perspective. Enjoy!