
Python has several built-in iteration patterns such as those provided by the ‘range()’ and ‘reversed()’ method. The ‘range()’ method takes ‘start’ and ‘stop’ arguments and returns an immutable sequence that we can iterate over. The ‘reverse()’ method takes an input sequence and returns the reverse iterator. Suppose we wanted to implement an iteration pattern that is customized and behaves differently from these built-in functions. In this post, we will discuss how to implement custom iteration patterns in python.
Let’s get started!
To start, let’s create an iteration pattern that produces an increasing range of floats. The function will take ‘start’, ‘stop’ and ‘increment’ arguments:
def increment_pattern(start, stop, increment):
Next, we will initialize the variable we will increment, x:
def increment_pattern(start, stop, increment):
x = start
Now, let’s write a control flow condition, such that whatever process that follows will continue as long a x is less than or equal to the ‘stop’ input value:
def increment_pattern(start, stop, increment):
x = start
while x < stop:
Since we want to write a custom iteration pattern, similar to ‘range()’, we want to return a generator. We do this using the ‘yield’ keyword:
def increment_pattern(start, stop, increment):
x = start
while x < stop:
yield x
Finally, we increment the value, x:
def increment_pattern(start, stop, increment):
x = start
while x <= stop:
yield x
x += increment
We can now call our generator in a ‘for-loop’ and iterate over values. Let’s pass in ‘start’ = 0, ‘stop’ = 3 and let’s increment by 0.5:
for value in increment_pattern(0, 3, 0.5):
print(value)

We can also define a decrement pattern. Let’s define a new function and change the while loop condition such that x is greater than or equal to the ‘stop’ value. Let’s also subtract by the decrement value iteratively:
def decrement_pattern(start, stop, decrement):
x = start
while x >= stop:
yield x
x -= decrement
In our function call within our ‘for-loop’, let’s swap the values for start and stop and decrement by 0.5:
for value in decrement_pattern(3, 0, 0.5):
print(value)

Finally, let’s look at a more practical example. Let’s use a custom iteration pattern to generate a list of values corresponding to an exponential growth function. Specifically, let’s consider the example of compound interest:

The terms in the equation are as follows:
- A represents the amount of money after a time period as passed
- P represents the principle, which is the amount of money you started with
- r represents the interest rate
- n is the number of times interest is compounded
- t represents the amount of time in years
Suppose you had $1000 in a savings account created when you were born and you want to calculate the amount of money you would have each year up until you turn 18 at a 8% interest rate compounded monthly. Let’s define our compound interest function. Here, we initialize the principle to be the $1000 we start off with. We also initialize the year, t, to be the ‘start’ value. We also write the condition that we will increment while the year, t, is less than ‘stop’:
def compound_interest_monthly(start, stop, increment):
t= start
principle = 1000
while t < stop:
Next, we define the interest portion of our equation:
def compound_interest_monthly(start, stop, increment):
...
interest = (1+ 0.08/12)**(12*t)
We then return the generator and increment our year:
def compound_interest_monthly(start, stop, increment):
...
interest = (1+ 0.08/12)**(12*t)
yield principle*interest
t+= increment
The total function is as follows:
def compound_interest_monthly(start, stop, increment):
t= start
principle = 1000
while t< stop:
interest = (1+ 0.08/12)**(12*t)
yield principle*interest
t+= increment
Let’s call our generator in a ‘for-loop’ with ‘start’ = 0 years and ‘stop’ = 19 years and increment yearly:
for value in compound_interest_monthly(0, 19, 1):
print(value)

Let’s also look at the compound interest starting with the same principle and over the same time period, compounded quarterly at a 4% interest rate:
def compound_interest_quarterly(start, stop, increment):
t= start
principle = 1000
while t< stop:
interest = (1+ 0.04/4)**(4*t)
yield principle*interest
t+= increment
Let’s iterate over our generator:
for value in compound_interest_quarterly(0, 19, 1):
print(value)

I’ll stop here but feel free to play around with the code yourself.
CONCLUSIONS
To summarize, in this post we discussed how to define custom iteration patterns with generators in python. We created custom increment and decrement iteration patterns that allowed us to generate a sequence of floats. We then developed a more practical custom iteration pattern for calculating compound interest over time. I hope you found this post interesting/useful. The code in this post is available on GitHub. Thank you for reading!