+= 在 python 3 中表示什么? [英] What does the += signify in python 3?

查看:68
本文介绍了+= 在 python 3 中表示什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,将列表添加到一起时:

For example, when adding lists together:

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

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

推荐答案

list_sum += x 表示将list_sum变量的内容与变量的内容相加x 并再次将结果存储到 list_sum 变量中.

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.

代码说明:

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.

这篇关于+= 在 python 3 中表示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆