Member-only story

Use Python’s itertools built-in module to create a decaying sine wave

Crystal X
2 min readMar 24, 2022

--

Itertools is a built-in module of Python that implements a number of iterator building blocks. The built-in module standardises a core set of fast memory efficient tools that are useful by themselves or in combination with other methods. This library makes it possible to construct specialised tools succinctly and efficiently in pure Python.

One itertools method that I have recently used to execute an animation of a decaying sine wave is itertools.count(). The Python documentation states that itertools.count, returns evenly spaced values with the value start and the sequence of iteration by the step. The code for this procedure can be seen in the screenshot below:-

I obtained the below script from the matplotlib library documentation and ran the accompanying program up on Replit, which is a free online interpreter. The exact code that was used in a function to create the decaying sine wave can be found in the screenshot below. Please note that in the example below, itertools.count did not accept any arguments as input:-

The above code uses itertools to iterate through the for loop infinitely. Each iteration is…

--

--

Crystal X
Crystal X

Written by Crystal X

I have over five decades experience in the world of work, being in fast food, the military, business, non-profits, and the healthcare sector.

No responses yet