What Does The += Signify In Python 3?
For example, when adding lists together: list = [1,2,3,4,5]  list_sum = 0 for x in list:    list_sum += x
Solution 1:
list_sum += x means add the contents of list_sum variable with the contents of variable x and again store the result to list_sum variable.
Code explanation:
list_sum = 0     # At first, 0 is assigned to the `list_sum` variable .
for x in list:   # iterating over the contents which are present inside the variable `list`
list_sum += x    # list_sum = 0+1 . After the first iteration, value 1 is stored to list_sum variable. Likewise it sums up the values present in  the list and then assign it back to list_sum variable. Atlast `list_sum` contains the sum of all the values present inside the given list.Solution 2:
It's shorthand for list_sum = list_sum + x
for x in list: will loop once through every element in list, assigning the value to a temporary variable x
Check out these duplicates:
Duplicate 1and not exactly a duplicate but another example of how it works
Solution 3:
It is shortened operation used in any language list_sum += x => list_sum = list_sum + x
There can also be "-=", "*=" and "/=" respectively.
Post a Comment for "What Does The += Signify In Python 3?"