+ =在python 3中表示什么?

时间:2014-10-31 01:10:32

标签: python python-3.x syntax

例如,在一起添加列表时:

list = [1,2,3,4,5]

list_sum = 0
for x in list:
   list_sum += x

3 个答案:

答案 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)

list_sum = list_sum + x

shorthand

for x in list:将遍历list中的每个元素,将值分配给临时变量x

查看以下重复项:

Duplicate 1并且不完全重复 another example of how it works

答案 2 :(得分:-1)

缩短了用于任何语言list_sum += x => list_sum = list_sum + x的操作 也可以分别是“ - =”,“* =”和“/ =”。