I would use a Python generator if I were you since it gives you extra expressiveness with the ability to express an "infinitely" large sequence.
def succession(initial, delta):
while True:
yield initial
# value, then, the next time you call
initial += delta
values_generator = succession(6.08, 0.052)
# later we decide how many values we want to consume from the generator
for i in range(20):
print(next(values_generator))
Update:
Adding more explanation on generators. Generators are a mechanism provided by Python that allows to build efficient data processing pipelines.
Say you have a list of numbers from 0 to 100_000, and you want to apply some transformations to it like filtering, then mapping, and so on, if you use regular Python2 map
and filter
your solution won't scale well because in each step (filtering, mapping, etc) an entire new list of intermediate results with potentially many elements that might not even be used in the final result will be computed. The key idea of building these functional pipelines is that we want them to be combinators that is, small functions that we can combine to create yet new functions that later can be combined, but we don't want to pay a price in performance. Generators basically come to solve this in Python (in Clojure you have transducers and in Haskell lazy evaluation). With generators Python won't build the intermediate lists but to push one value on one end of the pipeline, transform it, and pop it out the other end, this for each element in the initial data set you wish to transform.
A simple example of a generator would be:
def trivial_generator():
print "running until first yield..."
yield 1
print "running until second yield..."
yield 2
print "exiting..."
generator = trivial_generator()
result = generator.next()
print "got result ", result
result = generator.next()
print "got result ", result
generator.next()
if you run that you'll get:
running until first yield...
got result 1
running until second yield...
got result 2
exiting...
Traceback (most recent call last):
File "foo.py", line 14, in <module>
generator.next()
StopIteration
which illustrates the behavior of generators which is:
- You create one by calling a function that contains a
yield
- Every time you call the
.next()
method on it, or pass it to the next(generator)
function, the body of the generator will be executed until a yield
is found
- When a
yield
is found, the value specified in the yield will be returned and the generator will stay paused until you call its .next()
method again, at which point it will resume from the last point it was paused (notice that all the internal state of the generator is kept between calls)
- If you call
.next()
on a generator and no other yield
is found (which means is the end of it) an StopIteration
is raised (which is very convenient since is the exception a for
loops expects to happen at the en d of an iterator)
In the example solution I used, I took advantage of the pause/resume capabilities of the generators, by putting the yield
inside an infinite loop, I got a generator that will give me as many values I need efficiently.
a[i]
doesn't exist past 0, try doinga.append()
a[0]
is the only value that exists in the list, thank you...