# Im unable to figure what += refers to in vectorization

Hi, I have gone through the week 1 and week 2 courses and have seen on more than one occasion the terms ‘+=’ pop up when removing a loop in vectorization- it is used after initialization I guess? And I havent heard professor NG say what that is explicitly and It’s hounding me,
Could someone please throw light on this for me? It’s probably something he did mention or very common knowledge but I’m unaware

Pasting the snippet of what I’m referring to from the transcription:

db was initialized as 0 and db plus equals dz1.
"db plus equals dz2 down to you know
dz(m) and db divide equals M. So that’s what we had in the previous implementation.
We’d already got rid of one for loop."

That is a standard python operator, which is why Prof Ng did not explicitly explain it. This is not a beginning python course, so he assumes you already know this type of thing. If that is not true, then you might want to put this course on “pause” and have a look at some tutorials or even take a python course.

In terms of the output values, the following two statements achieve the same thing:

``````a += b
a = a + b
``````

It turns out there are subtle ways in which the two statements are different. The “+=” version of the operator is called an “in place” operator. If the values are scalars, then there is no difference. But if the operands in question are python “objects” like numpy arrays, then the way memory is managed in the two cases is different. If you say:

`a += b`

Then the object a after that line of code points to the same location in memory that it did before the statement. But if you say this:

`a = a + b`

Then the RHS expression is stored in a freshly allocated chunk of memory and then the object reference a on the LHS is set to point to the new piece of memory. The memory that used to be pointed to by a before that statement is now deallocated and can be “garbage collected” assuming there are no other active object references to it.

There are many other such “in place” operators, including “-=”, “*=”, “/=” and so forth. Here’s a video tutorial about that. You can find plenty more by googling “python in place operator”.

This is a great explanation- thanks so much , appreciate it! I will look at the video you linked to understand better

The differences that I described in how the in place operators manage memory can have some important side effects once you start using functions and calling them with object references as parameters (arguments). Here’s a thread from a while back that discusses the implications and some of the “gotchas” that you need to watch out for there.