Published Feb. 10, 2022, 12:57 p.m.
How do you like the new intro? Nevermind. On to python!
Welcome to this one-off tutorial on the wonders of the python += operator. We're gonna cover its uses with scalar numbers, lists, and strings as well as its use with numpy arrays (spoiler alert: get ready for linear algebra!).
I am going to do this work in a simple windows terminal. However, you can follow along in an IDE like spyder or wherever you can get an interactive python session going.
We will start off with some simple imports:
import sys, os
import numpy as np
That is all the imports we will need for this tutorial.
First I want to show you what I think is the most common and basic use of the += operator. It is most often used for scalar (integer or floating point number) addition. Consider that you have two variables a and b:
a = 14.34
b = 1
We could add those numbers together and re-cast the variable 'a' by writing:
a = a + b
In python this has been truncated by the += operator so that the expression above is the same as
a += b
If you run that a should now equal 15.34. If you run it a couple of times, then you should continue to add '1' to the value of 'a' each time you do it.
The += operator has application for strings as well. Consider now two strings:
a = "The kitten was"
b = " dying slowly."
Python lets you 'concatinate' these strings with the += operator. (Get it? conCATinate). Running a += b converts a into:
>> a += b
>> a
>> "The kitten was dying slowly."
Which is a very sad and tragic sentence. But let's move on.
For lists the += operator works in a similar fashion to how it works for strings. Consider the following lists:
a = [1,2,3,4,5]
b = [1,1,1,1,10]
Running the += operator as we have been doing renders the following:
>> a += b
>> a
>> [1,2,3,4,5,1,1,1,1,10]
You can see that for lists, the += operator has concatinated the two lists together (The lists didn't have the be the same length). This is a natural and straightforward use of the + and += operator for lists (in my opinion). But what if you wanted instead to take two lists like our original a and b and do some element-wise addition? Is there an easy way in python to do that? Well it turns out that there is! You guessed it: the += operator -- but for a special class of python variables: the numpy array.
We can easily reset our a and b variables as the original a and b lists and then re-cast them as numpy arrays as follows:
a = [1,2,3,4,5]
b = [1,1,1,1,10]
a = np.array(a)
b = np.array(b)
Now what happens if we try to use the += operator?:
>> a += b
>> a
>> array([2,3,4,5,15])
If we run it again we'd get:
>> a += b
>> a
>> array([3,4,5,6,25])
Another thing that numpy arrays can accept is scalar division or multiplication. What I mean by that is that if you try to divide a by a single scalar number it will assume that you want to divide every element of a by that number. However, there is a quirk that I discovered in python if you attemp to use the /= operator for this:
>>> a/=25
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: No loop matching the specified signature and casting was found for ufunc true_divide
Now, I don't know why this happens because if you try the following instead:
>>> a = a/25
>>> a
array([0.12, 0.16, 0.2 , 0.24, 1. ])
>>>
Python accepts it! And even stranger than that... if you NOW try to use *= or /= python will NOW accept it...:
>>> a *=25
>>> a /=25
>>> a
array([0.12, 0.16, 0.2 , 0.24, 1. ])
>>>
So for some reason, you must try scalar division (and possibly multiplaction) apart from the *= or /= architeture the first time you do it (maybe because the numbers in the array were integers?) But once you have done it use the more formal: a = a/25, then python is ok with the *= or /= operators.
Well I hope this has been a useful tutorial on the += operator in python. I think that once you get a handle on it, you will gain the intuition necessary for the other operators -=, *=, /= .
Thanks for watching!