-3
A=[2,4,8,16]
for i in A:
    B = i/2
    print(B)

How to sum values ​​coming from a structure " FOR "? how could i to sum 1+2+4+8 ?

  • You can have a variable outside the for loop and then you can keep adding `i` to that variable and you can get the sum of the list `A` – Sanjay SS Jun 17 '22 at 06:32
  • hii @SanjaySS, how are you?, I don't understand, I'm new to programming, how would I do that? – Priscila Helthuis Jun 17 '22 at 06:36
  • You can see my answer https://stackoverflow.com/a/72655206/4129813 – Sanjay SS Jun 17 '22 at 06:37
  • @SanjaySS what I want is the sum of "B = i/2" – Priscila Helthuis Jun 17 '22 at 06:38
  • I have edited the answer accordingly. Please check now @Priscila Helthuis – Sanjay SS Jun 17 '22 at 06:42
  • Hi @PriscilaHelthuis, welcome to StackOverflow! Please note that these kind of questions have been likely already asked numerous times, and with little research effort (e.g. searching for your title in Google), you could have come to a decent answer yourself. Additionally, unless someone tries to read between the lines, it is not clear what you are really after. That is probably why you received some down-votes. Next time you may want to read [how-to-ask](https://stackoverflow.com/help/how-to-ask) to help yourself in writing a better question if needed :-) – norok2 Jun 17 '22 at 07:21
  • You need some sort of `total` variable. It can start with `total=0`, and then in the loop, do `total += i/2` (or `total = total + i/2` it that is clearer). – hpaulj Jun 17 '22 at 18:15

2 Answers2

0

You can have a variable outside and loop and keep adding the values in the list A

A=[2,4,8,16]
B = 0
for i in A:
    B = B+ i/2
print(B)
Sanjay SS
  • 568
  • 4
  • 14
0

I assume that here you are interested in understanding what is/should be happenning.

Let's start from your input:

A = [2, 4, 8, 16]

You have already found out that to access the elements of A, you can use a for loop, and then do whatever you want with each individual element.

To compute the sum of n numbers, you can use of the associative property of addition (a + b) + c = a + (b + c) and the fact that it has a neutral element (0), to accumulate iteratively the result in a new single variable:

result = 0  # the neutral element
for x in A:
    B = x / i
    result = result + B

The value of result is updated at each iteration with the value of result from the previous iteration summed with B.

This is equivalent to:

(((0 + B) + B) + B) + ... + B

(as many times as there are elements in A).

Now, you can notice that B is:

  • used only once
  • reassigned at every iteration
  • not used outside the for loop

For these reason you might replace its only usage with its value:

result = 0  # the neutral element
for x in A:
    result = result + x / i

Also, the construct x = x + y can be written more concisely and efficiently as x += y (using += operator):

result = 0  # the neutral element
for x in A:
    result += x / i

Finally, the sum of multiple elements is sufficiently common that in Python the above has been optimized and provided with a separate sum() function:

sum(x / i for x in A)

which combines a for generator expression with sum().

norok2
  • 25,683
  • 4
  • 73
  • 99