Is += a Shortcut for x = x + y?

Is the += command a shortcut to saying: annual_rainfall = annual_rainfall + november_rainfall?


Hi @oadin,

That’s correct. The += operator is only used because it’s more convenient to type out.


Values can implement += differently if they wish. If the value is mutable then the in-place version likely modifies the value rather than creating a new one.

a = b = []
b += range(3)
print(a)  # [0, 1, 2]
a = b = []
b = b + range(3)
print(a)  # []
>>> b = b + range(3)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    b = b + range(3)
TypeError: can only concatenate list (not "range") to list
>>> b = b + list(range(3))
>>> b
[0, 1, 2, 0, 1, 2]
>>> a
[0, 1, 2]

Right. That’s another difference. list’s + doesn’t iterate, but += does

I do wonder why + doesn’t also iterate over the other value

Sorry, I did precurse the above these two lines…

>>> a = b = []
>>> b += range(3)

Which lends itself to your quesiton of the difference. Hmmm?

+ is plus, and concatenate, and nothing more. += (as with any assignment operator) is a method. That enlarges the playing field (my take on it).


+= is in-place add, it stands to reason it does the same thing as + aside from being in-place

I think it (list’s +=) might have been implemented using list.extend’s code and therefore got an extra feature which then couldn’t be removed.
But that’s purely a guess, and I don’t really need a reason

Take for example set’s &=
It doesn’t accept an iterable, neither does |= or ^=
And not str’s +=

list’s += is the odd one out

1 Like

No, in Python everything is an object, in case of operators what happens under the hood is that the interpreter is using magic methods.

x = x + y

equals to calling

x = x.__add__(y)
x += y

equals to

x = x.__iadd__(y)

You can check this pseudo integer implementation to see the difference

class MyInteger:
    def __init__(self, value=0):
        self.value = value

    def __add__(self, other):
        return MyInteger(self.value + other)

    def __iadd__(self, other):
        return "Not exactly the same."

    def __repr__(self):
        return f"{self.value}"

x = MyInteger(10)
x = x + 10
x += 10

You would have to check documentation for how they’re implemented for built-in types, but my guess is that they work differently for modifiable types, x = x + y probably creates new objects while x += y modifies existing ones.


i don’t understand this.please help me.

What the code demonstrates is that at first a and b reference the same list object. Changes to b will reflect in a since they point to the same thing.

The second example reassigns b with a new object, b + [0,1,2] (that’s what range(3) gives us). It no longer points to the same object as a and is now independent of it. They are two different pointers referencing two different objects.


Why can’t I use numbers in the variable name?

Hello @swapnilchowdhury!

Yes, you can use numbers in variable names in Python, but not at the beginning of the name. For example, num1 would be valid but 1num would be invalid.

For more Python naming conventions, check out this link.


Is 1.5e2 = 1.5 * 10^2? (while I am defining a float using scientific notation)

Have you tried printing out the value. It’s a quick test. You could even do an equality check directly if you use ** for the exponent instead of ^ which is binary operator.
1.5e2 == 1.5 * 10**2

1 Like

Is =+ the same as +=?

Hello, @cloud9488943976, and welcome to the forums.


=+ is two operators: = followed by +.
+= is a single operator
You could easily try something like this yourself:

a = 10
a += 5
print(a) # prints 15
a =+ 5
print(a) #prints 5 (why?)
#we used two operators, so we assigned (using =) nothing + 5 which evaluates to 5

For someone learning to code, does one memorize a set up codes for solving problems? Forgive me if not stated clearly. I barely know technology…trying to figure technology I can learn without a lot of previous computer skills.

Short answer, no. We practice and learn as a process. This gives us concepts and applications. We’re not memorizing, but training.

Does one memorize the times tables? Or learn how to compute any one of the entries with computer speed in their head? On the other hand, we were all children once, and had to depend upon our memory while our computation skills improved. For my own part, I knew them by heart. With so many times having to write them on the board in detention, it wasn’t hard. Repetition grows memory.

There’s a good reason that actors rehearse for several weeks before staging a play. Reps. Any athlete will say the same. It’s not so much about memorization as it is about internalizing. Once we make something a part of ourselves, it can never leave.

We’re given a problem. How will we approach it? That is the question in front of everyone who has ever faced a problem. One speculates, because I’m not sure that particular question is really the first one to come up when faced with a problem. But, in a purist world, let’s say it does. In a programming world, it certainly does.

What is the shortest distance between two points? A straight line. We know this from reason and logic, not maths, but we can prove it with maths. And if you’re like me, we can find it on Wikipedia.

It is not the one off problems we store in our memory, but the way we solved them. There are many approaches, some more elegant and refined, some more naive. In programming, what works, works. Don’t put yourself in memorize mode or you will surely miss the ride.

One must confess, as an intuitive I don’t solve much from memory and start from scratch each time. Thankfully concepts and applications do come to mind.

I’m new to this all. I’m not sure i posted first post in right place. How does one know what variable stands for?

A variable is represented by a specific characteristic, like a number, or quantity that can be measured/counted. A variable may also be called a data item. Age, country of birth, class grades, eye colour, vehicle type all of them are examples of variables.
Next time make sure you post a new question because here we are talking about an operator :smiley: