Integers are immutable data types, so they cannot be changed.
The line
a +=1
is a shortcut for
a=a+1
What is happening is that inside the scope of the function, the variable label 'a' is being assigned to a new different integer, while the original integer as an object in memory remains unchanged.
Outside of the function, the label 'a' still points to the original unchanged integer object.
The id() function can be used to show you if the underlying memory object of two variables is the same
>>> a =1
>>> def f(a):
... print('before',id(a))
... a+=1
... print('after',id(a))
...
>>> print('outside',id(a));f(a);print('outside',id(a))
outside 504120010720
before 504120010720
after 504120010752
outside 504120010720
>>>
The after object is a different object than the other three.
In contrast, something mutable like a list can be used to send changes out of a function.
>>> b=[]
>>> def g(b):
... print('before',id(b))
... b.append(5)
... print('after',id(b))
...
>>> print('outside',id(b));g(b);print('outside',id(b))
outside 504083879680
before 504083879680
after 504083879680
outside 504083879680
>>> print(b)
[5]
The after object is the same object as the other three.
The bottom line is that append() modifies the same object in memory, modifications are only allowed for mutable objects.
Doin a += does not modify the original object but produces an entirely new object which is assigned to the variable name.