Lowest price ever! Learn Generative AI for 48% less!
Get my discount0
Someone please explain the output of the following code?
total = 0for i in 1, 3, 7: total = total + i print (total )
4 Answers
0
1,3,7 is a tuple. you could also write:
for i in (1,3,7 ):
0
you start with total = 0 and i = 1. Then you defined the new value for total = 0 + 1 and print this value. After this you jump to the beginning of the loop. i = 3. The new value for total = 1+3 = 4 you print it and jump to the beginning. now is i = 7. The new value of total is total = 4 + 7=11. The loop is finished because in the tuple is no futher value. programm finished.
0
output will be
1
4
11
0
Thank you Amarie, could you kindly explain why variable total takes the value of the second total (1+3) for the next iteration?