Search code examples
pythonoperators

What is the difference between "=" and "+=" in Python?


What is the difference between = and +=?

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


Solution

  • 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