For Loops confusion on output

Screen Link: <!https://app.dataquest.io/m/312/lists-and-for-loops/9/for-loops->

My Code: <a_list = [1, 3, 5]

a_sum = 0
for value in a_list:
a_sum = a_sum + value
print(a_sum)>

This code was copied/pasted from the lesson on For Loops.

Replace this line with your code
a_list = [1, 3, 5]

a_sum = 0
for value in a_list:
a_sum = a_sum + value
print(a_sum)

What I expected to happen:
So this might be a stupid question (I really struggle with Loops), but this output makes no sense to me. Can anyone explain if the code iterates 3 times, why there are 4 lines of output code in the example? Also, I thought the code would add 0 to to each value in a_list. So why would the output compute to 1, 4, 9, 9? All of this just makes absolutely no sense to me. How are these values being computed, if we are essentially adding 0 to each value in a_list? Wouldn’t the output be the same in the a_list?

What actually happened:
Output was 1,4, 9, but this is not clear to me.

Replace this line with the output/error
No output error, just looking for clarification on how this line of code actually works.

1 Like

Hi @shanni, welcome to the community. Yes, the output of the code should be 1, 4, 9 and the explanation is given below.
image

The for loop iterates over the values in a_list. At the first iteration, value in the for loop is 1 since 1 is the first value in a_list. since a_sum is initialized as 0 and value is 1, a_sum = 0 + 1 which equals 1. The print statement will then print 1 as the output.

With the second iteration of the for loop, value in the for loop is now 3 which is the second value in a_list. Note the value of a_sum after the first iteration is 1 so when you add 3 to 1 a_sum equals 4. 4 will thus be printed as output.

For the third iteration, value in the for loop is 5 and since a_sum has the value of 4 after the second running of the for loop, a_sum = 4+5 which gives 9. 9 is then printed as output.

The for loop will check a_list again but since there is no more value, it ends meaning the code a_sum=a_sum + value and the print statement are not executed. So, hopefully, you see why the output is 1,4,9.

Let me know if this helps.

3 Likes

Hey @shanni welcome
Here’s a simple explanation (i hope that you understand this)

  1. The code doesn’t add 0 to each value in a_list. What you did was create a variable called a_sum that has a value of 0
  2. Then the loop does this:
    2.1 takes the first value in a_list (1) and does: a_sum = 0 + 1, then a_sum = 1
    2.2 Then it prints that value, so you get 1 as an output
    2.2 then takes the second value (3) so a_sum = 1 + 3 , so a_sum = 4
    2.3 then it prints that value so now the output is 1, 4
    2.4 then it takes the third value in the list (5), so a_sum = 4 + 5, so a_sum = 9
    2.4 then it prints that value, so now the output is 1, 4, 9
    2.5 the loop checks if there’s another value in the list, and as there isn’t any more values the loop ends

If you have any more questions let me know

1 Like

Thank you both for the helpful responses! The math was driving me crazy and I was beating myself up because I couldn’t figure out something so simple. This makes complete sense to me know. Thank you again!