1

What is the difference between = and +=?

I've been experimenting, and I haven't found the difference.

4
  • += is a short notation for adding a value to the variable. Lets say if x=1, then x += 1 will add 1 to x, so when you print x, it should say 2 Commented Jun 17, 2016 at 18:49
  • If you always start with an empty / zero value then there is effectively no difference but I imagine if you started with anything else you would see the difference. Commented Jun 17, 2016 at 18:52
  • x += 5 is the same as x = x + 5 Commented Jun 17, 2016 at 18:52
  • 2
    I'm voting to close this question as off-topic because you haven't provided any examples that demonstrate your confusion. Without introducing non-standard classes (which you don't mention), the only case where = and += would appear to be doing the same thing is if the variable on the left is already assigned to either 0, [], {}, or ''. Commented Jun 17, 2016 at 18:56

8 Answers 8

9

Similar to many other languages, += is a "shortcut".

x = y

Assigns a reference to the object on the right-hand-side to the name on the left.

x += y

Conceptually adds the object or the right-hand-size to the object referred to on the left. Conceptually the same as:

x = x + y

I say "conceptually" because the += operator can do different things depending on the class of the object on the left. For example with an integer it simply does an add, with a string (str) it appends to the string, with a list it adds a new element to the right side of the list.

A class can implement the __iadd__() special function which will carry to the required operation. += is a member of augmented assignments, see http://legacy.python.org/dev/peps/pep-0203/

Sign up to request clarification or add additional context in comments.

Comments

5

In python, the phrase x=4 will assign the value of 4 to x. However, the phrase x+=4 will increment 4 to the current value of x. For example:

x = 3
print x #will print 3
x += 2
print x #will print 5

1 Comment

Thanks; it makes much more sense now!
0

foo = bar sets foo to the same value as bar- if foo is 5 and bar is 3, foo is now 3.

foo += bar is shorthand for foo = foo + bar. So if foo is 5, and bar is 3, foo is now 8

This does in fact use whatever + means in context, so if foo is "A String" and bar is "bar", foo += bar makes foo == "A Stringbar"

Comments

0

= is used to assign a value to a variable. e.g.: c=1+c (which assigns the value of c+1 to c, so it increments c by 1 )

+= is used to increment a variable by a specific value. e.g.: c+=1 (which

Comments

0

x = 4 initiates the variable x to equal 4

x += 4 is actually x = x + 4. If you try to do x += 4 without initializing x you will get a name error

y = 5  # 5
y += 2  # 7

x += 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

Comments

0

x = 2 assigns a value 2 to the variable x. x += 2 is the same as x = x + 2. In other words += adds a value to a already existing value. Example:

>>> x += 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'y' is not defined

This is because x has never been assigned to a value. To understand what happens, look at the following code:

>>> x = 2
>>> print(x)
2
>>> x += 2
>>> print(x)
4

Comments

0

In python

c = a + b assigns value of a + b into c

c = 1 + 3 #returns 4

c += a is equivalent to c = c + a

c = 1 a = 2

c += 1 #returns 3

Check out: http://www.tutorialspoint.com/python/python_basic_operators.htm

Comments

0

Quick Notes:

a=b : assign the value b to the variable a

a+=b : sum b to the current value of a

a+=b is a short notation for a = a + b

You can do a-=b, it'll reproduces a = a - b

you can either to *= , /= and %=


Example:

i = 2

i += 3
print i 
# output = 5 (2+3)

i -= 3
print i 
# output = 2 (5-3)

i *= 3
print i 
# output = 6 (2*3)

i /= 3
print i 
# output = 2 (6/3)

i %= 3
print i 
# output = 2 (modulo of 2/3 = 2)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.