Home>
a = 1
for i in range (1,4):
  a = a + i
  print (a) # 1
a # 2


It is a program that calculates the addition of the above arithmetic progression with the for syntax.
I would like to retrieve the list [(i, a) = (1,2), (2,4), (3,7)].
in the for syntax

print (a) # 1


Then
2
Four
7
Is displayed, but outside the for syntax

a # 2


Then
7
Is just output.

How can I retrieve the list [(i, a)]?

  • Answer # 1

    ks = [] # difference list
    a = 1
    for i in range (1, 4):
        a = a + i
        ks.append ((i, a))
    print (ks)
    # [(1, 2), (2, 4), (3, 7)]