How to create a list containing an arithmetic progression?
Question:
Here’s an example of what I’m trying to achieve:
What I’m tring to do is make the sum of a starting number X, and sum it by Y, and with each sum, add the numbers to a previously empty list:
lst = []
i = -0.5
tot = 0.025
while i <= 100:
tot = tot + i
i = i + 1
a = tot
print("value: ",tot)
print(a)
lst.append(a)
print(lst)
Though I’m unable to keep them as individual numbers, and they just get clumped together.
Answers:
This is really about applying an incrementing multiplier to Y, so it is more suitably implemented by iterating over a range
of multipliers.
To produce 4 items, for example:
i = -0.5
tot = 0.025
lst = [i + tot * m for m in range(4)]
Here’s an example of what I’m trying to achieve:
What I’m tring to do is make the sum of a starting number X, and sum it by Y, and with each sum, add the numbers to a previously empty list:
lst = []
i = -0.5
tot = 0.025
while i <= 100:
tot = tot + i
i = i + 1
a = tot
print("value: ",tot)
print(a)
lst.append(a)
print(lst)
Though I’m unable to keep them as individual numbers, and they just get clumped together.
This is really about applying an incrementing multiplier to Y, so it is more suitably implemented by iterating over a range
of multipliers.
To produce 4 items, for example:
i = -0.5
tot = 0.025
lst = [i + tot * m for m in range(4)]