As a student of python x = x +1 makes more since looking at it, but I know that x += x is the same. Since I’m learning I would like to know which is normal practice? No since learning something that is depreciated.
Thanks again.
What you called “in-place operators” are better called augmented assignment, since they are not always in-place.
They are standard and have not been deprecated.
x += x
is equivalent to x = x + x
. I think you meant to type x += 1
, which is equivalent to x = x + 1
.
How to tell the difference between an in-place operation and one which is not in-place:
# Not in-place.
x = y = 100 # x and y are two names for the same integer.
x += 1
print(x) # 101
print(y) # 100
# In-place.
x = y = [100, 200] # x and y are two names for the same list.
x += [300] # Adds the new list in-place.
print(x) # [100, 200, 300]
print(y) # [100, 200, 300]
For ints, addition is never in-place, it always creates a new int. So adding 1 to x does not change the value of y.
For lists, augmented assignment is in-place, and does not create a new list. It just changes the existing list. Since x and y are two names for the same list, changing x naturally changes y as well, since they remain the same list.