Logarithms: a Practical Use in Python

This is a simple Python project demonstrating a useful application of logarithms. The logarithm of a number to a specified base is the power to which the base must be raised to get the number. Since their invention by John Napier in the 17th century until a few decades ago slide rules and books of log tables were used to simplify multiplication by turning it into a process of addition. Modern science, technology and engineering all relied on that simple idea.
The following example demonstrates logarithms. If…
...then the base 10 logarithm of 100 is 2:
Any number can be used as the base but the usual bases are 2, 10 and e which is the base of natural logarithms. e is an irrational number and is 2.718281828 to 9dp. Python’s math module provides functions for all three of these, and scientific calculators usually provide at least base e and base 10 logarithm keys.
The three Python functions are:
math.log - base e, ie. natural logarithm
math.log10 - base 10
math.log2 - base 2
The best known and “classic” use of logarithms is for simplifying multiplication as I mentioned above. In this article I will demonstrate another purpose: calculating how long it will take a sum of money earning a specific rate of interest to increase to a certain amount.
Coding
This project consist of a single file called logarithms.py containing two functions plus a main function to call them. The first function is ridiculously simple and just calculates compound interest over a period of several years, firstly just calculating the final amount and secondly calculating all interim yearly amounts.
The second function will carry out the process in reverse: starting with the opening balance and an interest rate we will calculate how long it will take for our money to grow to a specified amount. This is probably one of those little bits of mathematics that makes you think “hmm, we did that in school but I’d forgotten all about it”.
I have used interest as an example but the principle can be applied to any form of exponential growth or decay - reproduction of bacteria, radioactive decay and so on.
The logarithms.py file can be downloaded or cloned from the Github repository.
calculateamounts
This function takes as arguments a start amount, an interest rate (which is expressed as a decimal fraction, eg. 10% would be 1.1) and a number of years. It calculates how much you will have after earning the specified interest for the specified number of years.
It actually does this twice, firstly in one hit by multiplying start amount by interest to the power of years - note the comment regarding operator precedence. It then does it year by year within a for loop.
timetoamount
Now lets get to the main purpose of this little program: given a certain amount of money earning a certain amount of interest, how long will it take for the balance to grow to a certain amount? This is calculated by taking the logarithm of the new amount as a proportion of the original amount, and dividing it by the logarithm of the growth rate. That sentence probably doesn’t make sense, even if you understood how to do it beforehand, so here it is as a formula.
Using the values hardcoded into main we already know that in 12 years £1,000 will grow to £3,138 at 10% pa. (If you know where I can get 10% interest please let me know immediately!) Plugging those numbers into the formula we get:
The rounding error is less than 3 minutes so probably not worth worrying about, but basically we have turned the process round and calculated what we already knew, that it takes 12 years to get to the final amount from our starting point at the specified interest rate.
I have used math.log, the natural logarithm (base e) but it doesn’t matter which you use as we are calculating ratios rather than actual amounts, as long as you use the same one both times. If you have a few minutes to spare try out all three, and then try breaking the code by mixing up different log functions.
Now run the program with this command:
python3 logarithms.py
This gives you the following output:
The first chunk of output tells you that after 12 years at 10% £1000 will have grown to £3138.43. We then have the same information again but this time calculated year by year.
Lastly we reverse the process by calculating how long it will take to get to £3138.43 from £1000 at 10% pa.
For updates and random ramblings please follow me on Bluesky.
No AI was used in creating the code, text or images in this article.









Nice walkthrough of logarithms applied to compund interest. The time-to-amount formula is especially useful since most folks encounter the problem in reverse order compared to how its typically taught. I've used similar logic when modeling bacterial growth, and the ratio principle you mentioned holds nicely since base choice cancels out eitherway.