例如,在一起添加列表时:
list = [1,2,3,4,5]
list_sum = 0
for x in list:
list_sum += x
答案 0 :(得分:1)
list_sum += x
表示将list_sum
变量的内容添加到变量x
的内容中,然后再将结果存储到list_sum
变量。
代码说明:
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.
答案 1 :(得分:-1)
答案 2 :(得分:-1)
缩短了用于任何语言list_sum += x => list_sum = list_sum + x
的操作
也可以分别是“ - =”,“* =”和“/ =”。