<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[CodeDrome]]></title><description><![CDATA[Programming projects in Python focused on STEM: science, technology, mathematics and engineering.]]></description><link>https://codedrome.substack.com</link><image><url>https://substackcdn.com/image/fetch/$s_!Z9Mo!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda95de05-f465-4a63-b0bd-00b4e1c5ba00_256x256.png</url><title>CodeDrome</title><link>https://codedrome.substack.com</link></image><generator>Substack</generator><lastBuildDate>Mon, 20 Jul 2026 11:54:03 GMT</lastBuildDate><atom:link href="https://codedrome.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Chris Webb]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[codedrome@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[codedrome@substack.com]]></itunes:email><itunes:name><![CDATA[Chris Webb]]></itunes:name></itunes:owner><itunes:author><![CDATA[Chris Webb]]></itunes:author><googleplay:owner><![CDATA[codedrome@substack.com]]></googleplay:owner><googleplay:email><![CDATA[codedrome@substack.com]]></googleplay:email><googleplay:author><![CDATA[Chris Webb]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Statistics With Python Lists and Functions]]></title><description><![CDATA[This is the first in a series of articles on calculating statistics from datasets using Python.]]></description><link>https://codedrome.substack.com/p/statistics-with-python-lists-and-functions</link><guid isPermaLink="false">https://codedrome.substack.com/p/statistics-with-python-lists-and-functions</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 14 Jul 2026 09:08:37 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!EhV6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d6c891b-7463-4f71-a938-42bddaee8d7c_600x400.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EhV6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d6c891b-7463-4f71-a938-42bddaee8d7c_600x400.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EhV6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d6c891b-7463-4f71-a938-42bddaee8d7c_600x400.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EhV6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d6c891b-7463-4f71-a938-42bddaee8d7c_600x400.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EhV6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d6c891b-7463-4f71-a938-42bddaee8d7c_600x400.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EhV6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d6c891b-7463-4f71-a938-42bddaee8d7c_600x400.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!EhV6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d6c891b-7463-4f71-a938-42bddaee8d7c_600x400.jpeg" width="600" height="400" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5d6c891b-7463-4f71-a938-42bddaee8d7c_600x400.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:400,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:63118,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/205606070?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d6c891b-7463-4f71-a938-42bddaee8d7c_600x400.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EhV6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d6c891b-7463-4f71-a938-42bddaee8d7c_600x400.jpeg 424w, https://substackcdn.com/image/fetch/$s_!EhV6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d6c891b-7463-4f71-a938-42bddaee8d7c_600x400.jpeg 848w, https://substackcdn.com/image/fetch/$s_!EhV6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d6c891b-7463-4f71-a938-42bddaee8d7c_600x400.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!EhV6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d6c891b-7463-4f71-a938-42bddaee8d7c_600x400.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">image: <a href="https://pixabay.com/photos/calculator-calculation-assurance-723917/">Pixabay</a></figcaption></figure></div><p>This is the first in a series of articles on calculating statistics from datasets using Python. This article&#8217;s code will create a simple Python list of random data and use built-in functions and the statistics module.</p><h2>The Project</h2><p>This article has a single Python file, <em>pyliststats.py</em>, which you can clone or downloaded from the <a href="https://github.com/CodeDrome/python-statistics">GitHub repository</a>. Code from future articles will be uploaded to the same repository.</p><h2>The Code</h2><p>This is <em>pyliststats.py</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yx1q!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f06d93b-80b5-46f9-910f-c548ce8d2ff5_1154x1288.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yx1q!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f06d93b-80b5-46f9-910f-c548ce8d2ff5_1154x1288.png 424w, https://substackcdn.com/image/fetch/$s_!yx1q!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f06d93b-80b5-46f9-910f-c548ce8d2ff5_1154x1288.png 848w, https://substackcdn.com/image/fetch/$s_!yx1q!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f06d93b-80b5-46f9-910f-c548ce8d2ff5_1154x1288.png 1272w, https://substackcdn.com/image/fetch/$s_!yx1q!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f06d93b-80b5-46f9-910f-c548ce8d2ff5_1154x1288.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yx1q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f06d93b-80b5-46f9-910f-c548ce8d2ff5_1154x1288.png" width="1154" height="1288" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4f06d93b-80b5-46f9-910f-c548ce8d2ff5_1154x1288.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1288,&quot;width&quot;:1154,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:288191,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/205606070?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f06d93b-80b5-46f9-910f-c548ce8d2ff5_1154x1288.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yx1q!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f06d93b-80b5-46f9-910f-c548ce8d2ff5_1154x1288.png 424w, https://substackcdn.com/image/fetch/$s_!yx1q!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f06d93b-80b5-46f9-910f-c548ce8d2ff5_1154x1288.png 848w, https://substackcdn.com/image/fetch/$s_!yx1q!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f06d93b-80b5-46f9-910f-c548ce8d2ff5_1154x1288.png 1272w, https://substackcdn.com/image/fetch/$s_!yx1q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f06d93b-80b5-46f9-910f-c548ce8d2ff5_1154x1288.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>At the top of the file <strong>random</strong> and <strong>statistics</strong> are imported. In <strong>main</strong> we firstly create and populate a list of random numbers.</p><p>In lines 17 to 20 four of Python&#8217;s built-in functions are called. The function names are self-explanatory and calculate the length, minimum, maximum and sum (total) of the values in the iterable argument. I have omitted exception handling but an empty iterable will raise a <strong>ValueError</strong> so add exception handling to production code.</p><p>The methods in the statistics module are more sophisticated and require some elaboration. These raise a <strong>StatisticsError</strong> with empty iterables.</p><h3>Means</h3><p>The two means used here are the arithmetic means, what most people mean (!) by &#8220;average&#8221;, ie. add up all the numbers and divide by the count. The <strong>fmean</strong> method converts all values to floats (although our data is all floats anyway) and is more efficient than <strong>mean</strong>. There may be minor differences in the return values of the two functions.</p><h3>Mode</h3><p>The most frequent value, or the first of these if there is a &#8220;draw&#8221;.</p><h3>Standard Deviation</h3><p>The mean is just a single value which gives no indication of the range of the data; we could calculate the &#8220;mean deviation from the mean&#8221; or how much, on average, values are above or below the mean but the standard deviation is an ultimately more useful indicator. We know that our data ranges from 1 to 1000 and if you run the code a few times the <strong>pstdev</strong> is typically in the high 200s which gives a reasonable idea of the spread. Note that I have used <strong>pstdev</strong> or population standard deviation. This is slightly different from the <strong>stdev</strong> method which should be used for a sample of data.</p><h3>Medians</h3><p>The median is the middle value of the sorted data and is somewhat more meaningful that the arithmetic mean as it is less affected by skewed data, ie. when values are more heavily concentrated at the bottom or top ends. The <strong>statistics</strong> module spoils us with no less that four median methods to choose from although I have only used three so far.</p><ul><li><p><strong>median</strong>: the middle value of an odd number of values or the mean of the two middle values if the count is even</p></li><li><p><strong>median_low</strong>: if the count is even the lower value is returned</p></li><li><p><strong>median_high</strong>: if the count is even the higher value is returned</p></li></ul><h3>Quantiles</h3><p>If you sort the data and divide it into chunks with the same number of values in each then the quantiles are the values at the dividing lines of these chunks. The most common are quartiles which divide the data into four groups and deciles which divide it into ten. These give a reasonable indication of the spread of data and the upper and lower quantiles may be more meaningful that the minimum and maximum values if there are outliers, ie. a few values a long way from the typical range. (For this article I have used <strong>random.uniform</strong> so the data is far better behaved than you would typically find in the wild so there aren&#8217;t any values you could consider to be outliers.)</p><p>The <strong>quantiles</strong> method returns a list of the border values, the default number of quantiles being 4 for which we get three values. I have also called the method with <strong>n=10 </strong>for deciles in which case we get nine values. In each case the middle value is of course the same as the median. In a future article I will show how quantiles can be represented graphically.</p><h2>Running the Program</h2><p>Run the program like this:</p><p><strong>python3 pyliststats.py</strong></p><p>Here we have the output. Don&#8217;t bother reading it: this article is more about the process than the end results.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qMsG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42fb6f42-5a72-4cf6-8b25-9dd055709dcd_612x326.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qMsG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42fb6f42-5a72-4cf6-8b25-9dd055709dcd_612x326.png 424w, https://substackcdn.com/image/fetch/$s_!qMsG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42fb6f42-5a72-4cf6-8b25-9dd055709dcd_612x326.png 848w, https://substackcdn.com/image/fetch/$s_!qMsG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42fb6f42-5a72-4cf6-8b25-9dd055709dcd_612x326.png 1272w, https://substackcdn.com/image/fetch/$s_!qMsG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42fb6f42-5a72-4cf6-8b25-9dd055709dcd_612x326.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qMsG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42fb6f42-5a72-4cf6-8b25-9dd055709dcd_612x326.png" width="612" height="326" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/42fb6f42-5a72-4cf6-8b25-9dd055709dcd_612x326.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:326,&quot;width&quot;:612,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:58439,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/205606070?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42fb6f42-5a72-4cf6-8b25-9dd055709dcd_612x326.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qMsG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42fb6f42-5a72-4cf6-8b25-9dd055709dcd_612x326.png 424w, https://substackcdn.com/image/fetch/$s_!qMsG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42fb6f42-5a72-4cf6-8b25-9dd055709dcd_612x326.png 848w, https://substackcdn.com/image/fetch/$s_!qMsG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42fb6f42-5a72-4cf6-8b25-9dd055709dcd_612x326.png 1272w, https://substackcdn.com/image/fetch/$s_!qMsG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42fb6f42-5a72-4cf6-8b25-9dd055709dcd_612x326.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/statistics-with-python-lists-and-functions/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/statistics-with-python-lists-and-functions/comments"><span>Leave a comment</span></a></p><h2>Conclusion</h2><p>The various functions demonstrated in this article&#8217;s code might be all you need especially for small datasets, but they are rather rudimentary and, if you are learning statistics, completely opaque. You throw a list at a function and it gives you back a number or another list with no indication of their meaning or how they are calculated.</p><p>For the next article in this series I will write a class that calculates the stats we have looked at so far, primarily to raise the veil of mystery behind them but also because it might be a faster way of doing things. Any possible performance increases will be investigated in Part 3.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p>]]></content:encoded></item><item><title><![CDATA[Saving EXIF Data with the Python Pillow Library]]></title><description><![CDATA[I have already written a couple of introductory articles on the Pillow image library and another on using the library to read EXIF data.]]></description><link>https://codedrome.substack.com/p/saving-exif-data-with-the-python-pillow-library</link><guid isPermaLink="false">https://codedrome.substack.com/p/saving-exif-data-with-the-python-pillow-library</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 07 Jul 2026 10:52:08 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!v-KU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92e929d6-c1d0-4db9-aeae-86ff4e4d698e_600x600.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!v-KU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92e929d6-c1d0-4db9-aeae-86ff4e4d698e_600x600.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!v-KU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92e929d6-c1d0-4db9-aeae-86ff4e4d698e_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!v-KU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92e929d6-c1d0-4db9-aeae-86ff4e4d698e_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!v-KU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92e929d6-c1d0-4db9-aeae-86ff4e4d698e_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!v-KU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92e929d6-c1d0-4db9-aeae-86ff4e4d698e_600x600.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!v-KU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92e929d6-c1d0-4db9-aeae-86ff4e4d698e_600x600.jpeg" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/92e929d6-c1d0-4db9-aeae-86ff4e4d698e_600x600.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:208824,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/203411997?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92e929d6-c1d0-4db9-aeae-86ff4e4d698e_600x600.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!v-KU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92e929d6-c1d0-4db9-aeae-86ff4e4d698e_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!v-KU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92e929d6-c1d0-4db9-aeae-86ff4e4d698e_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!v-KU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92e929d6-c1d0-4db9-aeae-86ff4e4d698e_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!v-KU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92e929d6-c1d0-4db9-aeae-86ff4e4d698e_600x600.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I have already written a couple of introductory articles on the Pillow image library and another on using the library to read EXIF data. These articles are linked below.</p><p><a href="https://codedrome.substack.com/p/an-introduction-to-the-python-pillow-image-library">An Introduction to the Python Pillow Image Library</a><br><a href="https://codedrome.substack.com/p/the-python-pillow-image-library-part-2">The Python Pillow Image Library part 2</a><br><a href="https://codedrome.substack.com/p/reading-exif-data-with-python-and-pillow">Reading EXIF Data with Python and Pillow</a></p><p>The first article includes a demonstration of how to save an image opened with Pillow, either to create a copy or save changes made by Pillow. This is done by passing a filename to the <strong>save</strong> method but saving any EXIF data which may have existed in the original file needs a bit more work.</p><h2>The Code</h2><p>The code for this article consists of a single file called <em>pillowsaveexif.py</em> which lives in the <a href="https://github.com/CodeDrome/python-pillow">Github repository</a>. This repository also contains the code for my other Pillow articles and <em>photo.jpg</em>, the big orange flower.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!v-5H!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39d88f69-4562-4d81-adbf-cd515b69ec67_1506x1000.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!v-5H!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39d88f69-4562-4d81-adbf-cd515b69ec67_1506x1000.jpeg 424w, https://substackcdn.com/image/fetch/$s_!v-5H!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39d88f69-4562-4d81-adbf-cd515b69ec67_1506x1000.jpeg 848w, https://substackcdn.com/image/fetch/$s_!v-5H!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39d88f69-4562-4d81-adbf-cd515b69ec67_1506x1000.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!v-5H!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39d88f69-4562-4d81-adbf-cd515b69ec67_1506x1000.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!v-5H!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39d88f69-4562-4d81-adbf-cd515b69ec67_1506x1000.jpeg" width="1456" height="967" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39d88f69-4562-4d81-adbf-cd515b69ec67_1506x1000.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:967,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:626330,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/203411997?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39d88f69-4562-4d81-adbf-cd515b69ec67_1506x1000.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!v-5H!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39d88f69-4562-4d81-adbf-cd515b69ec67_1506x1000.jpeg 424w, https://substackcdn.com/image/fetch/$s_!v-5H!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39d88f69-4562-4d81-adbf-cd515b69ec67_1506x1000.jpeg 848w, https://substackcdn.com/image/fetch/$s_!v-5H!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39d88f69-4562-4d81-adbf-cd515b69ec67_1506x1000.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!v-5H!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39d88f69-4562-4d81-adbf-cd515b69ec67_1506x1000.jpeg 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This is the entire code in <em>pillowsaveexif.py</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!I9vj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7aae9877-59ec-4b99-a54f-9bfc7e7f556b_1110x1704.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!I9vj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7aae9877-59ec-4b99-a54f-9bfc7e7f556b_1110x1704.png 424w, https://substackcdn.com/image/fetch/$s_!I9vj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7aae9877-59ec-4b99-a54f-9bfc7e7f556b_1110x1704.png 848w, https://substackcdn.com/image/fetch/$s_!I9vj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7aae9877-59ec-4b99-a54f-9bfc7e7f556b_1110x1704.png 1272w, https://substackcdn.com/image/fetch/$s_!I9vj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7aae9877-59ec-4b99-a54f-9bfc7e7f556b_1110x1704.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!I9vj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7aae9877-59ec-4b99-a54f-9bfc7e7f556b_1110x1704.png" width="1110" height="1704" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7aae9877-59ec-4b99-a54f-9bfc7e7f556b_1110x1704.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1704,&quot;width&quot;:1110,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:285047,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/203411997?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7aae9877-59ec-4b99-a54f-9bfc7e7f556b_1110x1704.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!I9vj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7aae9877-59ec-4b99-a54f-9bfc7e7f556b_1110x1704.png 424w, https://substackcdn.com/image/fetch/$s_!I9vj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7aae9877-59ec-4b99-a54f-9bfc7e7f556b_1110x1704.png 848w, https://substackcdn.com/image/fetch/$s_!I9vj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7aae9877-59ec-4b99-a54f-9bfc7e7f556b_1110x1704.png 1272w, https://substackcdn.com/image/fetch/$s_!I9vj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7aae9877-59ec-4b99-a54f-9bfc7e7f556b_1110x1704.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>Imports</h2><p>Here we just import <strong>Image</strong> from the <strong>PIL</strong> library. (As I mentioned in an earlier article PIL was inherited from Pillow&#8217;s ancestor.)</p><h2>The main function</h2><p>The <strong>openfilepath</strong> variable is a file with EXIF data. This is passed to two functions which will save copies without and with the EXIF - note the filenames.</p><h2>save_NO_exif and save_WITH_exif</h2><p>The code in <strong>save_NO_exif</strong> is essentially the same as in the first article of the series in that any EXIF data is lost.</p><p>In <strong>save_WITH_exif</strong> we first check the image&#8217;s <strong>info</strong> property, a dictionary, to see whether there is a key called &#8220;exif&#8221;. If so then all we need to do is pass it to save as an optional <strong>exif</strong> argument. Simple!</p><p>If the <strong>info</strong> dictionary does not contain any EXIF then we just call <strong>save</strong> with a filename.</p><h2>The Path to Success is Paved With Failure</h2><p>As an alternative to the <strong>if/else</strong> I tried this:</p><p><strong>exif = image.info.get(&#8217;exif&#8217;) or None</strong></p><p>And then passed the <strong>exif</strong> variable to save whatever its value. This of course worked fine for images that have EXIF data but passing <strong>None</strong> as the <strong>exif</strong> argument caused an exception and, worse, the file to be corrupted. I thought I would mention this just in case you had the same idea, and also as a reminder that neat, tidy and working code is often the result of much trial and error.</p><h2>Trying It Out</h2><p>Run the program like this:</p><p><strong>python3 pillowsaveexif.py</strong></p><p>There is no terminal output (unless you get an exception for some reason) but you&#8217;ll find a couple more image files have been created, <em>photo_no_exif.jpg</em> and <em>photo_with_exif.jpg</em>. You might like to verify their respective absence and presence of EXIF data.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/saving-exif-data-with-the-python-pillow-library/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/saving-exif-data-with-the-python-pillow-library/comments"><span>Leave a comment</span></a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p>]]></content:encoded></item><item><title><![CDATA[Tableizator: A Python Terminal Table Generator part 2]]></title><description><![CDATA[In Part 1 of this series I wrote a short Python program that printed sample tables with Unicode border characters.]]></description><link>https://codedrome.substack.com/p/tableizator-a-python-terminal-table-2</link><guid isPermaLink="false">https://codedrome.substack.com/p/tableizator-a-python-terminal-table-2</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 30 Jun 2026 12:11:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!VjFZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4b233d01-1dba-4aa7-b7ca-d90b16d79f0f_600x400.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VjFZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4b233d01-1dba-4aa7-b7ca-d90b16d79f0f_600x400.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VjFZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4b233d01-1dba-4aa7-b7ca-d90b16d79f0f_600x400.png 424w, https://substackcdn.com/image/fetch/$s_!VjFZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4b233d01-1dba-4aa7-b7ca-d90b16d79f0f_600x400.png 848w, https://substackcdn.com/image/fetch/$s_!VjFZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4b233d01-1dba-4aa7-b7ca-d90b16d79f0f_600x400.png 1272w, https://substackcdn.com/image/fetch/$s_!VjFZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4b233d01-1dba-4aa7-b7ca-d90b16d79f0f_600x400.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VjFZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4b233d01-1dba-4aa7-b7ca-d90b16d79f0f_600x400.png" width="600" height="400" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4b233d01-1dba-4aa7-b7ca-d90b16d79f0f_600x400.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:400,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:395746,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/203235464?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4b233d01-1dba-4aa7-b7ca-d90b16d79f0f_600x400.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VjFZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4b233d01-1dba-4aa7-b7ca-d90b16d79f0f_600x400.png 424w, https://substackcdn.com/image/fetch/$s_!VjFZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4b233d01-1dba-4aa7-b7ca-d90b16d79f0f_600x400.png 848w, https://substackcdn.com/image/fetch/$s_!VjFZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4b233d01-1dba-4aa7-b7ca-d90b16d79f0f_600x400.png 1272w, https://substackcdn.com/image/fetch/$s_!VjFZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4b233d01-1dba-4aa7-b7ca-d90b16d79f0f_600x400.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Image: <a href="https://pixabay.com/illustrations/ai-generated-linked-data-data-center-7975718/">Pixabay</a></figcaption></figure></div><p>In <a href="https://codedrome.substack.com/p/tableizator-a-python-terminal-table-generator-1">Part 1</a> of this series I wrote a short Python program that printed sample tables with Unicode border characters. In Part 2 I get to the central purpose of this series: writing a function to print a table with actual headings and data.</p><p>The files for this series live in their own <a href="https://github.com/CodeDrome/tableizator-python">GitHub repository</a>. In Part 2 we add a couple more files:</p><ul><li><p>tableizator.py</p></li><li><p>tabledemo.py</p></li></ul><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h2>The Code</h2><p>The code to generate a table is in the <em>tableizator.py</em> file. The process consists of four main steps, each of which is carried out by a function call in <strong>tableize</strong>. The steps are:</p><ul><li><p>Calculate the column widths from the longest item in each heading/column</p></li><li><p>Create four types of horizontal border string</p></li><li><p>Print the headings</p></li><li><p>Print the data</p></li></ul><p>As <em>tableizator.py</em> is fairly long I&#8217;ll look at it piece by piece.</p><h3>The tableize Function</h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WDC2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1aa06430-c4ba-4eb0-a7e2-582097210eda_1602x648.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WDC2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1aa06430-c4ba-4eb0-a7e2-582097210eda_1602x648.png 424w, https://substackcdn.com/image/fetch/$s_!WDC2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1aa06430-c4ba-4eb0-a7e2-582097210eda_1602x648.png 848w, https://substackcdn.com/image/fetch/$s_!WDC2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1aa06430-c4ba-4eb0-a7e2-582097210eda_1602x648.png 1272w, https://substackcdn.com/image/fetch/$s_!WDC2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1aa06430-c4ba-4eb0-a7e2-582097210eda_1602x648.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WDC2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1aa06430-c4ba-4eb0-a7e2-582097210eda_1602x648.png" width="1456" height="589" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1aa06430-c4ba-4eb0-a7e2-582097210eda_1602x648.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:589,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:92924,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/203235464?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1aa06430-c4ba-4eb0-a7e2-582097210eda_1602x648.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WDC2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1aa06430-c4ba-4eb0-a7e2-582097210eda_1602x648.png 424w, https://substackcdn.com/image/fetch/$s_!WDC2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1aa06430-c4ba-4eb0-a7e2-582097210eda_1602x648.png 848w, https://substackcdn.com/image/fetch/$s_!WDC2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1aa06430-c4ba-4eb0-a7e2-582097210eda_1602x648.png 1272w, https://substackcdn.com/image/fetch/$s_!WDC2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1aa06430-c4ba-4eb0-a7e2-582097210eda_1602x648.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>After importing a few members of the typing module four functions are called in <strong>tableize</strong>, as described above.</p><h3>The __calc_col_widths Function</h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!U4US!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a780101-10de-432a-94b1-9fbc8105a427_1602x552.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!U4US!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a780101-10de-432a-94b1-9fbc8105a427_1602x552.png 424w, https://substackcdn.com/image/fetch/$s_!U4US!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a780101-10de-432a-94b1-9fbc8105a427_1602x552.png 848w, https://substackcdn.com/image/fetch/$s_!U4US!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a780101-10de-432a-94b1-9fbc8105a427_1602x552.png 1272w, https://substackcdn.com/image/fetch/$s_!U4US!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a780101-10de-432a-94b1-9fbc8105a427_1602x552.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!U4US!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a780101-10de-432a-94b1-9fbc8105a427_1602x552.png" width="1456" height="502" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2a780101-10de-432a-94b1-9fbc8105a427_1602x552.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:502,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:81344,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/203235464?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a780101-10de-432a-94b1-9fbc8105a427_1602x552.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!U4US!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a780101-10de-432a-94b1-9fbc8105a427_1602x552.png 424w, https://substackcdn.com/image/fetch/$s_!U4US!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a780101-10de-432a-94b1-9fbc8105a427_1602x552.png 848w, https://substackcdn.com/image/fetch/$s_!U4US!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a780101-10de-432a-94b1-9fbc8105a427_1602x552.png 1272w, https://substackcdn.com/image/fetch/$s_!U4US!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a780101-10de-432a-94b1-9fbc8105a427_1602x552.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Firstly the heading widths are obtained with a list comprehension and then within a nested loop these are overwritten if any data is longer than its heading. This means that for each column we know the widest value. The list is returned as a tuple.</p><h3>The __create_border_strings Function</h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rmF4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61d943a-9d93-44b6-8b90-fe8414747878_1602x1160.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rmF4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61d943a-9d93-44b6-8b90-fe8414747878_1602x1160.png 424w, https://substackcdn.com/image/fetch/$s_!rmF4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61d943a-9d93-44b6-8b90-fe8414747878_1602x1160.png 848w, https://substackcdn.com/image/fetch/$s_!rmF4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61d943a-9d93-44b6-8b90-fe8414747878_1602x1160.png 1272w, https://substackcdn.com/image/fetch/$s_!rmF4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61d943a-9d93-44b6-8b90-fe8414747878_1602x1160.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rmF4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61d943a-9d93-44b6-8b90-fe8414747878_1602x1160.png" width="1456" height="1054" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a61d943a-9d93-44b6-8b90-fe8414747878_1602x1160.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1054,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:246054,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/203235464?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61d943a-9d93-44b6-8b90-fe8414747878_1602x1160.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rmF4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61d943a-9d93-44b6-8b90-fe8414747878_1602x1160.png 424w, https://substackcdn.com/image/fetch/$s_!rmF4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61d943a-9d93-44b6-8b90-fe8414747878_1602x1160.png 848w, https://substackcdn.com/image/fetch/$s_!rmF4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61d943a-9d93-44b6-8b90-fe8414747878_1602x1160.png 1272w, https://substackcdn.com/image/fetch/$s_!rmF4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa61d943a-9d93-44b6-8b90-fe8414747878_1602x1160.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This function is a lot simpler than it looks because it has to do everything four times. I&#8217;m not entirely satisfied with this and may revisit the code one day and try to streamline it.</p><p>For each of the four types of horizontal border we need to carry out four tasks:</p><ul><li><p>Add the leftmost character</p></li><li><p>Iterate the columns and append the appropriate number of characters which go above or below each item</p></li><li><p>Append an appropriate intersection character</p></li><li><p>Append the appropriate end-of-row character</p></li></ul><p>Having done that the strings are returned as a dictionary.</p><h3>The __format_heading and __format_data Functions</h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!H0pq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b93a18a-b24d-4065-915f-64dbfc3d2e4a_1602x808.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!H0pq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b93a18a-b24d-4065-915f-64dbfc3d2e4a_1602x808.png 424w, https://substackcdn.com/image/fetch/$s_!H0pq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b93a18a-b24d-4065-915f-64dbfc3d2e4a_1602x808.png 848w, https://substackcdn.com/image/fetch/$s_!H0pq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b93a18a-b24d-4065-915f-64dbfc3d2e4a_1602x808.png 1272w, https://substackcdn.com/image/fetch/$s_!H0pq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b93a18a-b24d-4065-915f-64dbfc3d2e4a_1602x808.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!H0pq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b93a18a-b24d-4065-915f-64dbfc3d2e4a_1602x808.png" width="1456" height="734" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2b93a18a-b24d-4065-915f-64dbfc3d2e4a_1602x808.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:734,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:98563,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/203235464?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b93a18a-b24d-4065-915f-64dbfc3d2e4a_1602x808.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!H0pq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b93a18a-b24d-4065-915f-64dbfc3d2e4a_1602x808.png 424w, https://substackcdn.com/image/fetch/$s_!H0pq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b93a18a-b24d-4065-915f-64dbfc3d2e4a_1602x808.png 848w, https://substackcdn.com/image/fetch/$s_!H0pq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b93a18a-b24d-4065-915f-64dbfc3d2e4a_1602x808.png 1272w, https://substackcdn.com/image/fetch/$s_!H0pq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b93a18a-b24d-4065-915f-64dbfc3d2e4a_1602x808.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The <strong>__format_heading</strong> function is just a single line but I felt it was worth putting in a separate function to allow for more complex enhancements without cluttering up the <strong>__print_headings</strong> function.</p><p>The <strong>__format_data</strong> function is a bit more involved as the alignment depends on the data type, and again this could expand to something more sophisticated.</p><h3>The __print_headings and __print_data Functions</h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1RMI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83775035-5919-428c-89d4-394c59360f6d_1602x1128.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1RMI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83775035-5919-428c-89d4-394c59360f6d_1602x1128.png 424w, https://substackcdn.com/image/fetch/$s_!1RMI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83775035-5919-428c-89d4-394c59360f6d_1602x1128.png 848w, https://substackcdn.com/image/fetch/$s_!1RMI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83775035-5919-428c-89d4-394c59360f6d_1602x1128.png 1272w, https://substackcdn.com/image/fetch/$s_!1RMI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83775035-5919-428c-89d4-394c59360f6d_1602x1128.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1RMI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83775035-5919-428c-89d4-394c59360f6d_1602x1128.png" width="1456" height="1025" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/83775035-5919-428c-89d4-394c59360f6d_1602x1128.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1025,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:253101,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/203235464?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83775035-5919-428c-89d4-394c59360f6d_1602x1128.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1RMI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83775035-5919-428c-89d4-394c59360f6d_1602x1128.png 424w, https://substackcdn.com/image/fetch/$s_!1RMI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83775035-5919-428c-89d4-394c59360f6d_1602x1128.png 848w, https://substackcdn.com/image/fetch/$s_!1RMI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83775035-5919-428c-89d4-394c59360f6d_1602x1128.png 1272w, https://substackcdn.com/image/fetch/$s_!1RMI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83775035-5919-428c-89d4-394c59360f6d_1602x1128.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The <strong>__print_headings</strong> function assembles a single string for the headings and their border characters and then prints it sandwiched between <strong>heading_top_border</strong> and <strong>heading_bottom_border</strong>, these retrieved from the dictionary created by <strong>__create_border_strings</strong></p><p>The <strong>__print_data</strong> function is basically the same but of course has to iterate rows as well as columns. The border string printed in each iteration depends on whether the last row has been reached.</p><h2>A Demo Program</h2><p>This is a simple bit of code to try out our Tableizator. It just creates list of headings and data and throws it to tableize to do its stuff.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wmQ5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54d0eb9d-1958-4f7b-979c-e88b05552546_1790x648.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wmQ5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54d0eb9d-1958-4f7b-979c-e88b05552546_1790x648.png 424w, https://substackcdn.com/image/fetch/$s_!wmQ5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54d0eb9d-1958-4f7b-979c-e88b05552546_1790x648.png 848w, https://substackcdn.com/image/fetch/$s_!wmQ5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54d0eb9d-1958-4f7b-979c-e88b05552546_1790x648.png 1272w, https://substackcdn.com/image/fetch/$s_!wmQ5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54d0eb9d-1958-4f7b-979c-e88b05552546_1790x648.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wmQ5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54d0eb9d-1958-4f7b-979c-e88b05552546_1790x648.png" width="1456" height="527" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/54d0eb9d-1958-4f7b-979c-e88b05552546_1790x648.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:527,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:129477,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/203235464?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54d0eb9d-1958-4f7b-979c-e88b05552546_1790x648.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wmQ5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54d0eb9d-1958-4f7b-979c-e88b05552546_1790x648.png 424w, https://substackcdn.com/image/fetch/$s_!wmQ5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54d0eb9d-1958-4f7b-979c-e88b05552546_1790x648.png 848w, https://substackcdn.com/image/fetch/$s_!wmQ5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54d0eb9d-1958-4f7b-979c-e88b05552546_1790x648.png 1272w, https://substackcdn.com/image/fetch/$s_!wmQ5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54d0eb9d-1958-4f7b-979c-e88b05552546_1790x648.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Run the program with this command:</p><p><strong>python3 tabledemo.py</strong></p><p>This is the result.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8Qup!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47028398-749c-4f88-9eb1-181bad0b170d_801x180.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8Qup!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47028398-749c-4f88-9eb1-181bad0b170d_801x180.png 424w, https://substackcdn.com/image/fetch/$s_!8Qup!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47028398-749c-4f88-9eb1-181bad0b170d_801x180.png 848w, https://substackcdn.com/image/fetch/$s_!8Qup!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47028398-749c-4f88-9eb1-181bad0b170d_801x180.png 1272w, https://substackcdn.com/image/fetch/$s_!8Qup!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47028398-749c-4f88-9eb1-181bad0b170d_801x180.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8Qup!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47028398-749c-4f88-9eb1-181bad0b170d_801x180.png" width="801" height="180" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/47028398-749c-4f88-9eb1-181bad0b170d_801x180.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:180,&quot;width&quot;:801,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:26925,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/203235464?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47028398-749c-4f88-9eb1-181bad0b170d_801x180.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8Qup!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47028398-749c-4f88-9eb1-181bad0b170d_801x180.png 424w, https://substackcdn.com/image/fetch/$s_!8Qup!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47028398-749c-4f88-9eb1-181bad0b170d_801x180.png 848w, https://substackcdn.com/image/fetch/$s_!8Qup!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47028398-749c-4f88-9eb1-181bad0b170d_801x180.png 1272w, https://substackcdn.com/image/fetch/$s_!8Qup!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47028398-749c-4f88-9eb1-181bad0b170d_801x180.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h2>Conclusion</h2><p>I am aware that this little project is very minimalist and that there is a potentially huge number of enhancements. A few that spring to mind are:</p><ul><li><p>More sophisticated formatting</p></li><li><p>Specifying actual or maximum column widths</p></li><li><p>Specifying which columns to include from a larger data set</p></li><li><p>Alternative border characters (various options were shown in Part 1)</p></li><li><p>A curses version with colours and other formatting</p></li></ul><p>If anyone wishes to fork the project and enhance it then please go ahead. I&#8217;d be very interested to see what you come up with.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/tableizator-a-python-terminal-table-2/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/tableizator-a-python-terminal-table-2/comments"><span>Leave a comment</span></a></p><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p>]]></content:encoded></item><item><title><![CDATA[A NumPy Cheat Sheet]]></title><description><![CDATA[NumPy is a large and complex library and most users will only need a small subset of its functionality.]]></description><link>https://codedrome.substack.com/p/a-numpy-cheat-sheet</link><guid isPermaLink="false">https://codedrome.substack.com/p/a-numpy-cheat-sheet</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 23 Jun 2026 10:14:51 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!BWZR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2c65e0f-9a7e-4fc2-8e7b-901e3b527a37_600x400.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BWZR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2c65e0f-9a7e-4fc2-8e7b-901e3b527a37_600x400.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BWZR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2c65e0f-9a7e-4fc2-8e7b-901e3b527a37_600x400.jpeg 424w, https://substackcdn.com/image/fetch/$s_!BWZR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2c65e0f-9a7e-4fc2-8e7b-901e3b527a37_600x400.jpeg 848w, https://substackcdn.com/image/fetch/$s_!BWZR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2c65e0f-9a7e-4fc2-8e7b-901e3b527a37_600x400.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!BWZR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2c65e0f-9a7e-4fc2-8e7b-901e3b527a37_600x400.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BWZR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2c65e0f-9a7e-4fc2-8e7b-901e3b527a37_600x400.jpeg" width="600" height="400" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b2c65e0f-9a7e-4fc2-8e7b-901e3b527a37_600x400.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:400,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:78149,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/202723152?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2c65e0f-9a7e-4fc2-8e7b-901e3b527a37_600x400.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BWZR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2c65e0f-9a7e-4fc2-8e7b-901e3b527a37_600x400.jpeg 424w, https://substackcdn.com/image/fetch/$s_!BWZR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2c65e0f-9a7e-4fc2-8e7b-901e3b527a37_600x400.jpeg 848w, https://substackcdn.com/image/fetch/$s_!BWZR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2c65e0f-9a7e-4fc2-8e7b-901e3b527a37_600x400.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!BWZR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2c65e0f-9a7e-4fc2-8e7b-901e3b527a37_600x400.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>NumPy is a large and complex library and most users will only need a small subset of its functionality. It is very well documented <a href="https://numpy.org/doc/stable/">here</a> but I often find it useful to keep snippets or bits of sample code, especially for features I need occasionally but not frequently enough to commit to memory. The code for this article consists of a Python file containing a number of such snippets which can be copied, pasted and edited for specific situations. You may or may not find them useful but that is not the point - the purpose of this article is to encourage and inspire you to create and maintain your own cheatsheet.</p><p><em>If you are not already a NumPy user you might like to read my introductory <a href="https://codedrome.substack.com/p/a-numpy-reference">NumPy Reference</a>.</em></p><p>This project consists of a single file called <em>numpycheatsheet.py</em> which you can find in the <a href="https://github.com/CodeDrome/numpy_cheatsheet">Github repository</a>. Please feel free to edit and adapt it to your own needs. Each NumPy feature covered by this article has its own function in the file; just uncomment the one you want to run in the <strong>main</strong> function. I have chosen a selection of common tasks which I feel will cover the majority of what a typical NumPy programmer will need on a day to day basis.</p><p><em>The code snippets all assume NumPy has been aliased as np, the usual convention.</em></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h2>The Code Snippets</h2><h3>Version</h3><p>You might occasionally need to check the version of NumPy you have installed. To do this simply grab the <strong>np.__version__</strong> property.</p><h3>Creating Arrays</h3><p>There are several ways to create a NumPy array including from a Python list or with the <strong>zeros</strong> or <strong>ones</strong> methods which create an array filled with 0s or 1s respectively.</p><h3>Statistics</h3><p>NumPy contains methods to calculate a variety of statistics including arithmetic mean, sum, minimum and maximum etc. These can be called on NumPy (np) like this <strong>np.sum(data)</strong> or on a NumPy array like this <strong>data.sum()</strong>.</p><h3>Array Information</h3><p>There are a few methods to get information about an array including <strong>ndim</strong> (number of dimensions), <strong>size</strong> (total number of elements) and <strong>dtype</strong> (data type).</p><h3>unique</h3><p>The <strong>unique</strong> method returns a sorted array of unique values, ie. it &#8220;de-dupes&#8221; the values.</p><h3>csv_files</h3><p>The <strong>savetxt</strong> and <strong>loadtxt</strong> methods respectively save an array to a CSV file and create an array from a CSV file. As with any code that attempts to interact with the outside world, even the local file system, calls to these two methods should be in <strong>try/except</strong> blocks.</p><h3>Data Types</h3><p>A NumPy array has a data type depending on its contents. In general you can let NumPy figure out the type for itself but you can specify the type when creating an array and, as we saw in the info function, you can read the <strong>dtype</strong> property.</p><h3>Iterating an Array</h3><p>It&#8217;s sometimes useful to explicitly iterate the values in an array, for example when printing the values in a format other than the default. You can use a normal <strong>for/in</strong> loop or obtain an iterator with the <strong>nditer</strong> method. Note that operations on an array&#8217;s elements rarely if ever require an explicit loop and can be performed on the array as a single entity.</p><h3>where</h3><p>The <strong>where</strong> method returns an array of indices of the values in the input array satisfying the where clause; this can then be used to retrieve the actual values you want. I have included an alternative syntax with these two operations combined into a single line.</p><h3>Random Numbers</h3><p>NumPy&#8217;s pseudo-random methods are useful for generating test or demo values, and include a handy Gaussian or normal distribution generator.</p><h3>Arithmetic</h3><p>Possibly NumPy&#8217;s most useful feature is the ability to carry out operations on all elements of an array in a single line with no explicit looping. The code in the arithmetic function demonstrated adding, subtracting, multiplying and dividing two arrays as well as one array and a scalar. NumPy has methods called <strong>add</strong>, <strong>subtract</strong>, <strong>multiply</strong> and <strong>divide</strong> but I much prefer the good old fashioned +, -, * and / symbols.</p><h3>linspace</h3><p>This is another way of creating an array which I thought deserved its own function. The <strong>linspace </strong>(linear space) method creates an equally spaced range of values between a minimum and maximum.</p><h3>reshape</h3><p>The shape of an array is the number of values in each dimension, and the <strong>reshape</strong> method rearranges the existing values into the specified number of rows, columns or deeper dimensions.</p><h3>all and any</h3><p>These methods return <strong>True</strong> or <strong>False</strong> depending on whether all or any of the values in an array satisfy the condition.</p><h3>Powers and Roots</h3><p>Numpy provides us with <strong>sqrt</strong> and <strong>pow</strong> methods for square roots and powers but you can also use the standard Python <strong>**</strong> syntax which I prefer.</p><p>The n<sup>th</sup> root of a number is the number to the power of 1/n, so for example the square root of a number is the number to the power of 1/2 or 0.5. I have included a demonstration of this in the sample code. NumPy also has a <strong>cbrt</strong> (cube root) method but if you are unlucky enough to need and other roots you will have to use the power of reciprocal technique.</p><h3>stacking</h3><p>The ...stack methods combine two or more arrays vertically (<strong>vstack</strong>) or horizontally (<strong>column_stack</strong>).</p><h3>Appending</h3><p>The <strong>append</strong> method creates a new array with the specified iterable or scalar appended. This is not an in-place operation so the return value must be assigned to a variable. In the code I have overwritten the original arrays with the return values of <strong>append</strong> but if you do not do this the original array is unchanged.</p><h3>Reversing with flip</h3><p>The <strong>flip</strong> method reverses an array but works across all dimensions, so for example a 2-dimensional array is reversed both horizontally and vertically.</p><h2>Conclusion</h2><p>I hope you found something useful in the sample code in numpycheatsheet.py and more importantly were inspired to create your own NumPy cheatsheet.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/a-numpy-cheat-sheet/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/a-numpy-cheat-sheet/comments"><span>Leave a comment</span></a></p><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p>]]></content:encoded></item><item><title><![CDATA[Exploring Bezier Curves with Python: part 1]]></title><description><![CDATA[If you have ever attempted to draw a Bezier curve in a graphics application such as Inkscape you may have found it baffling and frustrating.]]></description><link>https://codedrome.substack.com/p/exploring-bezier-curves-with-python-part-1</link><guid isPermaLink="false">https://codedrome.substack.com/p/exploring-bezier-curves-with-python-part-1</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 16 Jun 2026 12:14:56 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!abnr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f02a35-be88-40ef-ba67-90aa5689b460_600x400.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!abnr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f02a35-be88-40ef-ba67-90aa5689b460_600x400.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!abnr!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f02a35-be88-40ef-ba67-90aa5689b460_600x400.jpeg 424w, https://substackcdn.com/image/fetch/$s_!abnr!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f02a35-be88-40ef-ba67-90aa5689b460_600x400.jpeg 848w, https://substackcdn.com/image/fetch/$s_!abnr!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f02a35-be88-40ef-ba67-90aa5689b460_600x400.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!abnr!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f02a35-be88-40ef-ba67-90aa5689b460_600x400.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!abnr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f02a35-be88-40ef-ba67-90aa5689b460_600x400.jpeg" width="600" height="400" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d3f02a35-be88-40ef-ba67-90aa5689b460_600x400.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:400,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:100349,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/201477060?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f02a35-be88-40ef-ba67-90aa5689b460_600x400.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!abnr!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f02a35-be88-40ef-ba67-90aa5689b460_600x400.jpeg 424w, https://substackcdn.com/image/fetch/$s_!abnr!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f02a35-be88-40ef-ba67-90aa5689b460_600x400.jpeg 848w, https://substackcdn.com/image/fetch/$s_!abnr!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f02a35-be88-40ef-ba67-90aa5689b460_600x400.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!abnr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3f02a35-be88-40ef-ba67-90aa5689b460_600x400.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">image: <a href="https://pixabay.com/photos/abstract-rainbow-pattern-lines-6297317/">Pixabay</a></figcaption></figure></div><p>If you have ever attempted to draw a Bezier curve in a graphics application such as Inkscape you may have found it baffling and frustrating. Bezier curves are defined relative to control points but how these affect the curve isn&#8217;t always obvious. In this article I will write Python code to illustrate how the curve is derived from the points and I hope you find the output instructive.</p><p>As Confucius <em>didn&#8217;t</em> say &#8220;a picture is worth a thousand words&#8221; so to start I&#8217;ll show you a Bezier curve with its secrets exposed. I&#8217;m sure you will find it instantly enlightening.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!u8Q3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f4144da-a3b7-4538-9e7c-cc2fc0848270_444x572.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!u8Q3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f4144da-a3b7-4538-9e7c-cc2fc0848270_444x572.png 424w, https://substackcdn.com/image/fetch/$s_!u8Q3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f4144da-a3b7-4538-9e7c-cc2fc0848270_444x572.png 848w, https://substackcdn.com/image/fetch/$s_!u8Q3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f4144da-a3b7-4538-9e7c-cc2fc0848270_444x572.png 1272w, https://substackcdn.com/image/fetch/$s_!u8Q3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f4144da-a3b7-4538-9e7c-cc2fc0848270_444x572.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!u8Q3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f4144da-a3b7-4538-9e7c-cc2fc0848270_444x572.png" width="444" height="572" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7f4144da-a3b7-4538-9e7c-cc2fc0848270_444x572.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:572,&quot;width&quot;:444,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:52611,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/201477060?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f4144da-a3b7-4538-9e7c-cc2fc0848270_444x572.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!u8Q3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f4144da-a3b7-4538-9e7c-cc2fc0848270_444x572.png 424w, https://substackcdn.com/image/fetch/$s_!u8Q3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f4144da-a3b7-4538-9e7c-cc2fc0848270_444x572.png 848w, https://substackcdn.com/image/fetch/$s_!u8Q3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f4144da-a3b7-4538-9e7c-cc2fc0848270_444x572.png 1272w, https://substackcdn.com/image/fetch/$s_!u8Q3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f4144da-a3b7-4538-9e7c-cc2fc0848270_444x572.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This is an annotated version of the Matplotlib output created by this article&#8217;s code. The points <strong>P<sub>0</sub></strong>, <strong>P<sub>1</sub></strong> and <strong>P<sub>2</sub></strong> are key to the whole process of creating a Bezier curve. In this article I will look at curves with three control points; these are known as quadratic Bezier curves. These are the salient points to remember:</p><ul><li><p><strong>P<sub>0</sub></strong> and <strong>P<sub>2</sub></strong> are the ends of the curve</p></li><li><p><strong>P<sub>1</sub></strong> governs the behaviour of the curve between its end points</p></li><li><p>The start and end points of the intermediate lines are equally spaced along the lines between the control points <strong>L<sub>1</sub></strong> and <strong>L<sub>2</sub></strong></p></li><li><p>Given enough intermediate lines the outermost intersection points give an acceptable illusion of a curve</p></li></ul><p>The code to create this illustration contains data structures to represent the points and the inter-point lines which are then used to create a data structure of the intermediate lines. This last data structure is then plotted by Matplotlib.</p><h2>The Project</h2><p>This project consists of the following files which you can find in the <a href="https://github.com/CodeDrome/bezier-curves-python">GitHub repository</a>.</p><ul><li><p>namedtuples.py</p></li><li><p>bccalc.py</p></li><li><p>bcplot.py</p></li><li><p>bcdemo.py</p></li></ul><p>In Part 2 I will extend the code to calculate and plot just the points along the curve. For Part 3 I will develop an interactive Bezier curve interface using PyGame.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h2>The Code</h2><h3>namedtuples.py</h3><p>In this file three named tuples are defined to make the code that calculates points, lines and the distances of the intermediate lines clearer.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gb89!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b66a591-72ca-4ef4-ba0f-74a94091b11f_1500x296.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gb89!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b66a591-72ca-4ef4-ba0f-74a94091b11f_1500x296.png 424w, https://substackcdn.com/image/fetch/$s_!gb89!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b66a591-72ca-4ef4-ba0f-74a94091b11f_1500x296.png 848w, https://substackcdn.com/image/fetch/$s_!gb89!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b66a591-72ca-4ef4-ba0f-74a94091b11f_1500x296.png 1272w, https://substackcdn.com/image/fetch/$s_!gb89!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b66a591-72ca-4ef4-ba0f-74a94091b11f_1500x296.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gb89!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b66a591-72ca-4ef4-ba0f-74a94091b11f_1500x296.png" width="1456" height="287" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8b66a591-72ca-4ef4-ba0f-74a94091b11f_1500x296.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:287,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:58779,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/201477060?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b66a591-72ca-4ef4-ba0f-74a94091b11f_1500x296.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gb89!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b66a591-72ca-4ef4-ba0f-74a94091b11f_1500x296.png 424w, https://substackcdn.com/image/fetch/$s_!gb89!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b66a591-72ca-4ef4-ba0f-74a94091b11f_1500x296.png 848w, https://substackcdn.com/image/fetch/$s_!gb89!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b66a591-72ca-4ef4-ba0f-74a94091b11f_1500x296.png 1272w, https://substackcdn.com/image/fetch/$s_!gb89!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b66a591-72ca-4ef4-ba0f-74a94091b11f_1500x296.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3>bccalc.py</h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Kufy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F372d1529-7ce5-4d48-9006-51af30b216e0_1500x1288.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Kufy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F372d1529-7ce5-4d48-9006-51af30b216e0_1500x1288.png 424w, https://substackcdn.com/image/fetch/$s_!Kufy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F372d1529-7ce5-4d48-9006-51af30b216e0_1500x1288.png 848w, https://substackcdn.com/image/fetch/$s_!Kufy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F372d1529-7ce5-4d48-9006-51af30b216e0_1500x1288.png 1272w, https://substackcdn.com/image/fetch/$s_!Kufy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F372d1529-7ce5-4d48-9006-51af30b216e0_1500x1288.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Kufy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F372d1529-7ce5-4d48-9006-51af30b216e0_1500x1288.png" width="1456" height="1250" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/372d1529-7ce5-4d48-9006-51af30b216e0_1500x1288.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1250,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:169486,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/201477060?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F372d1529-7ce5-4d48-9006-51af30b216e0_1500x1288.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Kufy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F372d1529-7ce5-4d48-9006-51af30b216e0_1500x1288.png 424w, https://substackcdn.com/image/fetch/$s_!Kufy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F372d1529-7ce5-4d48-9006-51af30b216e0_1500x1288.png 848w, https://substackcdn.com/image/fetch/$s_!Kufy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F372d1529-7ce5-4d48-9006-51af30b216e0_1500x1288.png 1272w, https://substackcdn.com/image/fetch/$s_!Kufy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F372d1529-7ce5-4d48-9006-51af30b216e0_1500x1288.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This is where the hard work is done. The arguments to the create function are a tuple of points - specifically named tuples from the previous file - and the number of intermediate lines to draw. The points are split out into separate variables purely to shorten the code using them.</p><p>The variables <strong>l1</strong> and <strong>l2</strong> connect the control points, and lines is an empty list to which new lines will be appended as they are created. Next we calculate the horizontal and vertical distances between intermediate lines along both the existing lines connecting the control points. Finally a <strong>for</strong> loop iterates the specified number of intermediate lines, calculating the coordinates of their endpoints and appending each new line to our list.</p><h3>bcplot.py</h3><p>This is a very straightforward usage of the Matplotlib library. If you aren&#8217;t familiar with Matplotlib you might like to read my <a href="https://codedrome.substack.com/p/a-matplotlib-reference">Matplotlib Reference</a> article.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Exy7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62457145-cbe6-4080-aa3c-2b23642a7541_1500x840.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Exy7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62457145-cbe6-4080-aa3c-2b23642a7541_1500x840.png 424w, https://substackcdn.com/image/fetch/$s_!Exy7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62457145-cbe6-4080-aa3c-2b23642a7541_1500x840.png 848w, https://substackcdn.com/image/fetch/$s_!Exy7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62457145-cbe6-4080-aa3c-2b23642a7541_1500x840.png 1272w, https://substackcdn.com/image/fetch/$s_!Exy7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62457145-cbe6-4080-aa3c-2b23642a7541_1500x840.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Exy7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62457145-cbe6-4080-aa3c-2b23642a7541_1500x840.png" width="1456" height="815" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/62457145-cbe6-4080-aa3c-2b23642a7541_1500x840.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:815,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:89549,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/201477060?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62457145-cbe6-4080-aa3c-2b23642a7541_1500x840.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Exy7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62457145-cbe6-4080-aa3c-2b23642a7541_1500x840.png 424w, https://substackcdn.com/image/fetch/$s_!Exy7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62457145-cbe6-4080-aa3c-2b23642a7541_1500x840.png 848w, https://substackcdn.com/image/fetch/$s_!Exy7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62457145-cbe6-4080-aa3c-2b23642a7541_1500x840.png 1272w, https://substackcdn.com/image/fetch/$s_!Exy7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F62457145-cbe6-4080-aa3c-2b23642a7541_1500x840.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The <strong>plotlines</strong> function takes as arguments a list of lines created by the code in <em>bccalc.py</em> and a color. It then iterates the lines, calling Matplotlib&#8217;s <strong>plot</strong> method on each. Note that we need to set the aspect to equal as the units of each axis are of the same type; without this the plot would probably be distorted.</p><h3>bcdemo.py</h3><p>Now we just need to try the code out.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!u42P!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfb0381-3e52-4937-901d-b1e3123e26b1_1500x776.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!u42P!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfb0381-3e52-4937-901d-b1e3123e26b1_1500x776.png 424w, https://substackcdn.com/image/fetch/$s_!u42P!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfb0381-3e52-4937-901d-b1e3123e26b1_1500x776.png 848w, https://substackcdn.com/image/fetch/$s_!u42P!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfb0381-3e52-4937-901d-b1e3123e26b1_1500x776.png 1272w, https://substackcdn.com/image/fetch/$s_!u42P!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfb0381-3e52-4937-901d-b1e3123e26b1_1500x776.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!u42P!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfb0381-3e52-4937-901d-b1e3123e26b1_1500x776.png" width="1456" height="753" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7cfb0381-3e52-4937-901d-b1e3123e26b1_1500x776.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:753,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83154,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/201477060?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfb0381-3e52-4937-901d-b1e3123e26b1_1500x776.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!u42P!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfb0381-3e52-4937-901d-b1e3123e26b1_1500x776.png 424w, https://substackcdn.com/image/fetch/$s_!u42P!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfb0381-3e52-4937-901d-b1e3123e26b1_1500x776.png 848w, https://substackcdn.com/image/fetch/$s_!u42P!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfb0381-3e52-4937-901d-b1e3123e26b1_1500x776.png 1272w, https://substackcdn.com/image/fetch/$s_!u42P!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7cfb0381-3e52-4937-901d-b1e3123e26b1_1500x776.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>After importing the three previous files three points are created and passed to <strong>bccalc.create</strong>, the return value of which is of course a list of lines which are then passed to <strong>bcplot.plotlines</strong>.</p><p>Run the program with this command:</p><p><strong>python3 bcdemo.py</strong></p><p>This is the end result.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wRvK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844735b6-0c7b-42a3-8fe4-73d79cba30ab_700x615.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wRvK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844735b6-0c7b-42a3-8fe4-73d79cba30ab_700x615.png 424w, https://substackcdn.com/image/fetch/$s_!wRvK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844735b6-0c7b-42a3-8fe4-73d79cba30ab_700x615.png 848w, https://substackcdn.com/image/fetch/$s_!wRvK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844735b6-0c7b-42a3-8fe4-73d79cba30ab_700x615.png 1272w, https://substackcdn.com/image/fetch/$s_!wRvK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844735b6-0c7b-42a3-8fe4-73d79cba30ab_700x615.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wRvK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844735b6-0c7b-42a3-8fe4-73d79cba30ab_700x615.png" width="700" height="615" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/844735b6-0c7b-42a3-8fe4-73d79cba30ab_700x615.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:615,&quot;width&quot;:700,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:43910,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/201477060?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844735b6-0c7b-42a3-8fe4-73d79cba30ab_700x615.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wRvK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844735b6-0c7b-42a3-8fe4-73d79cba30ab_700x615.png 424w, https://substackcdn.com/image/fetch/$s_!wRvK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844735b6-0c7b-42a3-8fe4-73d79cba30ab_700x615.png 848w, https://substackcdn.com/image/fetch/$s_!wRvK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844735b6-0c7b-42a3-8fe4-73d79cba30ab_700x615.png 1272w, https://substackcdn.com/image/fetch/$s_!wRvK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F844735b6-0c7b-42a3-8fe4-73d79cba30ab_700x615.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You may want to try out a few other combinations of control points by editing <em>bcdemo.py</em> or uncommenting the three already in the <strong>main</strong> function.</p><h2>Conclusion</h2><p>The code above gives a clear and intuitive demonstration of how a bezier curve is put together and how the three control points do their job. For the next part of this series I&#8217;ll isolate the outermost intersection points so that we can plot just the curves without their &#8220;scaffolding&#8221;.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/exploring-bezier-curves-with-python-part-1/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/exploring-bezier-curves-with-python-part-1/comments"><span>Leave a comment</span></a></p><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p><p></p>]]></content:encoded></item><item><title><![CDATA[Tableizator: A Python Terminal Table Generator part 1]]></title><description><![CDATA[Amongst the 160,000 or so Unicode characters are a set of forty which can be used to form borders around data printed in a table format.]]></description><link>https://codedrome.substack.com/p/tableizator-a-python-terminal-table-generator-1</link><guid isPermaLink="false">https://codedrome.substack.com/p/tableizator-a-python-terminal-table-generator-1</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 09 Jun 2026 14:27:23 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!9wle!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F126d3111-d75b-4c06-841e-4da4f91511d6_600x400.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9wle!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F126d3111-d75b-4c06-841e-4da4f91511d6_600x400.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9wle!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F126d3111-d75b-4c06-841e-4da4f91511d6_600x400.png 424w, https://substackcdn.com/image/fetch/$s_!9wle!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F126d3111-d75b-4c06-841e-4da4f91511d6_600x400.png 848w, https://substackcdn.com/image/fetch/$s_!9wle!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F126d3111-d75b-4c06-841e-4da4f91511d6_600x400.png 1272w, https://substackcdn.com/image/fetch/$s_!9wle!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F126d3111-d75b-4c06-841e-4da4f91511d6_600x400.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9wle!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F126d3111-d75b-4c06-841e-4da4f91511d6_600x400.png" width="600" height="400" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/126d3111-d75b-4c06-841e-4da4f91511d6_600x400.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:400,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:395746,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/199466270?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F126d3111-d75b-4c06-841e-4da4f91511d6_600x400.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9wle!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F126d3111-d75b-4c06-841e-4da4f91511d6_600x400.png 424w, https://substackcdn.com/image/fetch/$s_!9wle!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F126d3111-d75b-4c06-841e-4da4f91511d6_600x400.png 848w, https://substackcdn.com/image/fetch/$s_!9wle!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F126d3111-d75b-4c06-841e-4da4f91511d6_600x400.png 1272w, https://substackcdn.com/image/fetch/$s_!9wle!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F126d3111-d75b-4c06-841e-4da4f91511d6_600x400.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">image: <a href="https://pixabay.com/illustrations/ai-generated-linked-data-data-center-7975718/">Pixabay</a></figcaption></figure></div><p>Amongst the 160,000 or so Unicode characters are a set of forty which can be used to form borders around data printed in a table format. The ultimate aim of this project is to create a function which will use these characters to generate a table from iterables but in Part 1 I will write code which prints the characters to provide a reference to what is available, and also print a few examples of how they can be used.</p><p>The project so far consists of a single file called <em>symbols.py</em> which you can find in the <a href="https://github.com/CodeDrome/tableizator-python">GitHub repository</a>. Further files from this project will be added to the same repository.</p><h2>The Code</h2><p>This is the <em>symbols.py</em> file.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4l1I!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabb8f0d8-a7a6-4031-9853-b7b98cf57405_1126x2408.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4l1I!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabb8f0d8-a7a6-4031-9853-b7b98cf57405_1126x2408.png 424w, https://substackcdn.com/image/fetch/$s_!4l1I!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabb8f0d8-a7a6-4031-9853-b7b98cf57405_1126x2408.png 848w, https://substackcdn.com/image/fetch/$s_!4l1I!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabb8f0d8-a7a6-4031-9853-b7b98cf57405_1126x2408.png 1272w, https://substackcdn.com/image/fetch/$s_!4l1I!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabb8f0d8-a7a6-4031-9853-b7b98cf57405_1126x2408.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4l1I!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabb8f0d8-a7a6-4031-9853-b7b98cf57405_1126x2408.png" width="1126" height="2408" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/abb8f0d8-a7a6-4031-9853-b7b98cf57405_1126x2408.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2408,&quot;width&quot;:1126,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:349135,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/199466270?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabb8f0d8-a7a6-4031-9853-b7b98cf57405_1126x2408.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4l1I!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabb8f0d8-a7a6-4031-9853-b7b98cf57405_1126x2408.png 424w, https://substackcdn.com/image/fetch/$s_!4l1I!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabb8f0d8-a7a6-4031-9853-b7b98cf57405_1126x2408.png 848w, https://substackcdn.com/image/fetch/$s_!4l1I!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabb8f0d8-a7a6-4031-9853-b7b98cf57405_1126x2408.png 1272w, https://substackcdn.com/image/fetch/$s_!4l1I!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabb8f0d8-a7a6-4031-9853-b7b98cf57405_1126x2408.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>the main Function</h3><p>Here we simply call the other two functions, uncommenting/commenting the calls as required.</p><h3>print_symbols</h3><p>The hexadecimal values of the border characters are hard-coded into a tuple. Unfortunately the values aren&#8217;t in a single sequence so we can&#8217;t iterate a range. After printing a heading the tuple is then used in a <strong>for/in</strong> loop to print the values in a variety of formats, and finally the bottom table border is printed. When this project is finished we will be able to do all the table border stuff automatically.</p><p>The code in this function is pretty straightforward but there are a few points to note:</p><ul><li><p>Hexadecimal values must be prefixed with <strong>0x</strong></p></li><li><p>Even though programmed as hexadecimal the values are of course fundamentally binary so can be printed as decimal if required. This is done here using the <strong>:0d</strong> formatting.</p></li><li><p>To print numbers as hexadecimal use <strong>:0x</strong> (lower case letters) or <strong>:0X</strong> (upper case letters). I prefer the latter as the letters are the same size as the decimal numbers.</p></li><li><p>The <strong>chr</strong> function gets the character for the specified numeric value.</p></li></ul><h3>sample_tables</h3><p>This function consists of just a pile of hard-coded print statements to show a variety of ways the table border characters can be used.</p><h2>Running the Program</h2><p>Run the program like this:</p><p><strong>python3 symbols.py</strong></p><p>This is the end result. The first eleven characters, which do not have a consecutive sequence of codes, are single-line. The rest, which do have consecutive numbers, are double-line or a mixture of single and double.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!YKVv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49b04244-14b4-464b-b8a9-5afd3e463b20_145x741.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!YKVv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49b04244-14b4-464b-b8a9-5afd3e463b20_145x741.png 424w, https://substackcdn.com/image/fetch/$s_!YKVv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49b04244-14b4-464b-b8a9-5afd3e463b20_145x741.png 848w, https://substackcdn.com/image/fetch/$s_!YKVv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49b04244-14b4-464b-b8a9-5afd3e463b20_145x741.png 1272w, https://substackcdn.com/image/fetch/$s_!YKVv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49b04244-14b4-464b-b8a9-5afd3e463b20_145x741.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!YKVv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49b04244-14b4-464b-b8a9-5afd3e463b20_145x741.png" width="145" height="741" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/49b04244-14b4-464b-b8a9-5afd3e463b20_145x741.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:741,&quot;width&quot;:145,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:16305,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/199466270?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49b04244-14b4-464b-b8a9-5afd3e463b20_145x741.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!YKVv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49b04244-14b4-464b-b8a9-5afd3e463b20_145x741.png 424w, https://substackcdn.com/image/fetch/$s_!YKVv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49b04244-14b4-464b-b8a9-5afd3e463b20_145x741.png 848w, https://substackcdn.com/image/fetch/$s_!YKVv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49b04244-14b4-464b-b8a9-5afd3e463b20_145x741.png 1272w, https://substackcdn.com/image/fetch/$s_!YKVv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49b04244-14b4-464b-b8a9-5afd3e463b20_145x741.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In <strong>main</strong> comment out <strong>print_symbols()</strong>, uncomment <strong>sample_tables()</strong> and run the program again. This shows that the characters form a cohesive set for any possible corner, junction or line.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qvyZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F187abd47-fc1e-4f5b-9515-f07ec68c5a55_72x586.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qvyZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F187abd47-fc1e-4f5b-9515-f07ec68c5a55_72x586.png 424w, https://substackcdn.com/image/fetch/$s_!qvyZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F187abd47-fc1e-4f5b-9515-f07ec68c5a55_72x586.png 848w, https://substackcdn.com/image/fetch/$s_!qvyZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F187abd47-fc1e-4f5b-9515-f07ec68c5a55_72x586.png 1272w, https://substackcdn.com/image/fetch/$s_!qvyZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F187abd47-fc1e-4f5b-9515-f07ec68c5a55_72x586.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qvyZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F187abd47-fc1e-4f5b-9515-f07ec68c5a55_72x586.png" width="72" height="586" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/187abd47-fc1e-4f5b-9515-f07ec68c5a55_72x586.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:586,&quot;width&quot;:72,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3849,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/199466270?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F187abd47-fc1e-4f5b-9515-f07ec68c5a55_72x586.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qvyZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F187abd47-fc1e-4f5b-9515-f07ec68c5a55_72x586.png 424w, https://substackcdn.com/image/fetch/$s_!qvyZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F187abd47-fc1e-4f5b-9515-f07ec68c5a55_72x586.png 848w, https://substackcdn.com/image/fetch/$s_!qvyZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F187abd47-fc1e-4f5b-9515-f07ec68c5a55_72x586.png 1272w, https://substackcdn.com/image/fetch/$s_!qvyZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F187abd47-fc1e-4f5b-9515-f07ec68c5a55_72x586.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>Conclusion</h2><p>As I mentioned this is just the first stage in a project to create a table-generating function. Stay tuned.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/tableizator-a-python-terminal-table-generator-1/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/tableizator-a-python-terminal-table-generator-1/comments"><span>Leave a comment</span></a></p><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p>]]></content:encoded></item><item><title><![CDATA[Exploring Binocular Lens Sizes with Python]]></title><description><![CDATA[I recently bought a new pair of binoculars (not the ones in the photo above!) which got me thinking about the way binocular magnifications and lens sizes are specified.]]></description><link>https://codedrome.substack.com/p/exploring-binocular-lens-sizes-with-python</link><guid isPermaLink="false">https://codedrome.substack.com/p/exploring-binocular-lens-sizes-with-python</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 02 Jun 2026 14:24:54 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!R6q7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99062996-2e5a-4288-bd71-a32e6450fabc_600x400.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!R6q7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99062996-2e5a-4288-bd71-a32e6450fabc_600x400.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!R6q7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99062996-2e5a-4288-bd71-a32e6450fabc_600x400.jpeg 424w, https://substackcdn.com/image/fetch/$s_!R6q7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99062996-2e5a-4288-bd71-a32e6450fabc_600x400.jpeg 848w, https://substackcdn.com/image/fetch/$s_!R6q7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99062996-2e5a-4288-bd71-a32e6450fabc_600x400.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!R6q7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99062996-2e5a-4288-bd71-a32e6450fabc_600x400.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!R6q7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99062996-2e5a-4288-bd71-a32e6450fabc_600x400.jpeg" width="600" height="400" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/99062996-2e5a-4288-bd71-a32e6450fabc_600x400.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:400,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:132594,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/199329194?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99062996-2e5a-4288-bd71-a32e6450fabc_600x400.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!R6q7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99062996-2e5a-4288-bd71-a32e6450fabc_600x400.jpeg 424w, https://substackcdn.com/image/fetch/$s_!R6q7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99062996-2e5a-4288-bd71-a32e6450fabc_600x400.jpeg 848w, https://substackcdn.com/image/fetch/$s_!R6q7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99062996-2e5a-4288-bd71-a32e6450fabc_600x400.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!R6q7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99062996-2e5a-4288-bd71-a32e6450fabc_600x400.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">image: <a href="https://pixabay.com/photos/binoculars-map-above-vintage-old-7723092/">Pixabay</a></figcaption></figure></div><p>I recently bought a new pair of binoculars (not the ones in the photo above!) which got me thinking about the way binocular magnifications and lens sizes are specified. These are the key characteristics of a pair of binoculars and are specified in the format [magnification]&#10005;[objective lens diameter in millimetres], for example 8&#10005;30 or 10&#10005;42.</p><p>The diameter and therefore area of the objective lenses (those at the very front) govern the amount of light the binoculars capture and therefore the brightness of the image, but the area does not increase in direct proportion to the diameter. This is illustrated by the diagram below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5Loy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bd984cb-a3dc-4943-93be-8164a66ca86c_335x271.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5Loy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bd984cb-a3dc-4943-93be-8164a66ca86c_335x271.png 424w, https://substackcdn.com/image/fetch/$s_!5Loy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bd984cb-a3dc-4943-93be-8164a66ca86c_335x271.png 848w, https://substackcdn.com/image/fetch/$s_!5Loy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bd984cb-a3dc-4943-93be-8164a66ca86c_335x271.png 1272w, https://substackcdn.com/image/fetch/$s_!5Loy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bd984cb-a3dc-4943-93be-8164a66ca86c_335x271.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5Loy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bd984cb-a3dc-4943-93be-8164a66ca86c_335x271.png" width="335" height="271" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1bd984cb-a3dc-4943-93be-8164a66ca86c_335x271.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:271,&quot;width&quot;:335,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:11216,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/199329194?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bd984cb-a3dc-4943-93be-8164a66ca86c_335x271.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5Loy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bd984cb-a3dc-4943-93be-8164a66ca86c_335x271.png 424w, https://substackcdn.com/image/fetch/$s_!5Loy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bd984cb-a3dc-4943-93be-8164a66ca86c_335x271.png 848w, https://substackcdn.com/image/fetch/$s_!5Loy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bd984cb-a3dc-4943-93be-8164a66ca86c_335x271.png 1272w, https://substackcdn.com/image/fetch/$s_!5Loy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bd984cb-a3dc-4943-93be-8164a66ca86c_335x271.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The two lenses shown have diameters of 25mm and 56mm, the smallest and largest sizes of widely available general purpose binoculars. Even though the larger lens has a diameter just over twice that of the smaller lens the 56mm has an area and therefore light-gathering power about five times larger.</p><p>In this article I will write some Python code to calculate the areas of a range of lens diameters, displaying them as a bar graph and a table.</p><h2>Some Simple Arithmetic</h2><p>You probably know that the area of a circle is calculated like this, where <em>&#960;</em> is the constant 3.141592653589793 (15dp) and <em>r</em> is the circle&#8217;s radius.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!nS1p!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F520bd9c9-09be-4d98-b204-1c60fd54ea49_75x48.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!nS1p!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F520bd9c9-09be-4d98-b204-1c60fd54ea49_75x48.png 424w, https://substackcdn.com/image/fetch/$s_!nS1p!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F520bd9c9-09be-4d98-b204-1c60fd54ea49_75x48.png 848w, https://substackcdn.com/image/fetch/$s_!nS1p!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F520bd9c9-09be-4d98-b204-1c60fd54ea49_75x48.png 1272w, https://substackcdn.com/image/fetch/$s_!nS1p!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F520bd9c9-09be-4d98-b204-1c60fd54ea49_75x48.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!nS1p!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F520bd9c9-09be-4d98-b204-1c60fd54ea49_75x48.png" width="75" height="48" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/520bd9c9-09be-4d98-b204-1c60fd54ea49_75x48.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:48,&quot;width&quot;:75,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:897,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/199329194?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F520bd9c9-09be-4d98-b204-1c60fd54ea49_75x48.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!nS1p!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F520bd9c9-09be-4d98-b204-1c60fd54ea49_75x48.png 424w, https://substackcdn.com/image/fetch/$s_!nS1p!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F520bd9c9-09be-4d98-b204-1c60fd54ea49_75x48.png 848w, https://substackcdn.com/image/fetch/$s_!nS1p!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F520bd9c9-09be-4d98-b204-1c60fd54ea49_75x48.png 1272w, https://substackcdn.com/image/fetch/$s_!nS1p!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F520bd9c9-09be-4d98-b204-1c60fd54ea49_75x48.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><p>Let's modify the formula slightly to allow for the fact that we are working with diameters so need to divide them by 2 to get the radius.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!O3-o!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb32a5f67-6c66-4603-b595-b62f27d66cb0_142x53.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!O3-o!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb32a5f67-6c66-4603-b595-b62f27d66cb0_142x53.png 424w, https://substackcdn.com/image/fetch/$s_!O3-o!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb32a5f67-6c66-4603-b595-b62f27d66cb0_142x53.png 848w, https://substackcdn.com/image/fetch/$s_!O3-o!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb32a5f67-6c66-4603-b595-b62f27d66cb0_142x53.png 1272w, https://substackcdn.com/image/fetch/$s_!O3-o!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb32a5f67-6c66-4603-b595-b62f27d66cb0_142x53.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!O3-o!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb32a5f67-6c66-4603-b595-b62f27d66cb0_142x53.png" width="142" height="53" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b32a5f67-6c66-4603-b595-b62f27d66cb0_142x53.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:53,&quot;width&quot;:142,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1904,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/199329194?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb32a5f67-6c66-4603-b595-b62f27d66cb0_142x53.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!O3-o!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb32a5f67-6c66-4603-b595-b62f27d66cb0_142x53.png 424w, https://substackcdn.com/image/fetch/$s_!O3-o!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb32a5f67-6c66-4603-b595-b62f27d66cb0_142x53.png 848w, https://substackcdn.com/image/fetch/$s_!O3-o!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb32a5f67-6c66-4603-b595-b62f27d66cb0_142x53.png 1272w, https://substackcdn.com/image/fetch/$s_!O3-o!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb32a5f67-6c66-4603-b595-b62f27d66cb0_142x53.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Now let's use the formula with a couple of actual values, 30mm and 40mm, which seem to be the most popular binocular sizes.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Grc7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f766f6d-b389-40ee-9eca-3be859e1523c_264x140.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Grc7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f766f6d-b389-40ee-9eca-3be859e1523c_264x140.png 424w, https://substackcdn.com/image/fetch/$s_!Grc7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f766f6d-b389-40ee-9eca-3be859e1523c_264x140.png 848w, https://substackcdn.com/image/fetch/$s_!Grc7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f766f6d-b389-40ee-9eca-3be859e1523c_264x140.png 1272w, https://substackcdn.com/image/fetch/$s_!Grc7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f766f6d-b389-40ee-9eca-3be859e1523c_264x140.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Grc7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f766f6d-b389-40ee-9eca-3be859e1523c_264x140.png" width="264" height="140" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0f766f6d-b389-40ee-9eca-3be859e1523c_264x140.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:140,&quot;width&quot;:264,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:7058,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/199329194?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f766f6d-b389-40ee-9eca-3be859e1523c_264x140.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Grc7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f766f6d-b389-40ee-9eca-3be859e1523c_264x140.png 424w, https://substackcdn.com/image/fetch/$s_!Grc7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f766f6d-b389-40ee-9eca-3be859e1523c_264x140.png 848w, https://substackcdn.com/image/fetch/$s_!Grc7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f766f6d-b389-40ee-9eca-3be859e1523c_264x140.png 1272w, https://substackcdn.com/image/fetch/$s_!Grc7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f766f6d-b389-40ee-9eca-3be859e1523c_264x140.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>As you can see a relatively modest increase in diameter of one third results in an increase in area approaching double.</p><p><em>This isn&#8217;t intended to be a binocular buying guide but basically there is always going to be a compromise between performance on the one hand and price/size/weight on the other. Also note that size and weight are not proportional to lens diameter - as a rule of thumb they are more closely proportional to the magnification.</em></p><p>As a final piece of arithmetic I&#8217;ll calculate the relative light gathering capabilities of the 30mm and 40mm lenses used in the examples above.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MzBi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4b5d537-847d-4fc9-9123-c9d0a63a2e36_291x134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MzBi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4b5d537-847d-4fc9-9123-c9d0a63a2e36_291x134.png 424w, https://substackcdn.com/image/fetch/$s_!MzBi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4b5d537-847d-4fc9-9123-c9d0a63a2e36_291x134.png 848w, https://substackcdn.com/image/fetch/$s_!MzBi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4b5d537-847d-4fc9-9123-c9d0a63a2e36_291x134.png 1272w, https://substackcdn.com/image/fetch/$s_!MzBi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4b5d537-847d-4fc9-9123-c9d0a63a2e36_291x134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MzBi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4b5d537-847d-4fc9-9123-c9d0a63a2e36_291x134.png" width="291" height="134" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f4b5d537-847d-4fc9-9123-c9d0a63a2e36_291x134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:134,&quot;width&quot;:291,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:7434,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/199329194?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4b5d537-847d-4fc9-9123-c9d0a63a2e36_291x134.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!MzBi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4b5d537-847d-4fc9-9123-c9d0a63a2e36_291x134.png 424w, https://substackcdn.com/image/fetch/$s_!MzBi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4b5d537-847d-4fc9-9123-c9d0a63a2e36_291x134.png 848w, https://substackcdn.com/image/fetch/$s_!MzBi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4b5d537-847d-4fc9-9123-c9d0a63a2e36_291x134.png 1272w, https://substackcdn.com/image/fetch/$s_!MzBi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4b5d537-847d-4fc9-9123-c9d0a63a2e36_291x134.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Here we see that the larger lens has 1.78 times the area of the smaller, and the smaller has 0.56 times the area of the larger. This is basically the same information looked at in two ways.</p><h2>The Project</h2><p>This project consists of a single file called <em>binocularlenses.py</em> which you can find in the <a href="https://github.com/CodeDrome/binocular-lens-areas-python">GitHub repository</a>.</p><p>The purpose of the code is to take a selection of common binocular objective lens diameters and calculate the actual areas as well as the areas relative to the smallest.</p><p>The code uses some simple functionality from the NumPy and Matplotlib libraries. If you are not familiar with these you might like to read my introductory articles.</p><p><a href="https://codedrome.substack.com/p/a-numpy-reference">A NumPy Reference</a></p><p><a href="https://codedrome.substack.com/p/a-matplotlib-reference">A Matplotlib Reference</a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h2>The Code</h2><p>This is the entire <em>binocularlenses.py</em> file.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ashq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32a16a84-c874-4271-a106-a5fb8e3056ca_1312x2216.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ashq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32a16a84-c874-4271-a106-a5fb8e3056ca_1312x2216.png 424w, https://substackcdn.com/image/fetch/$s_!ashq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32a16a84-c874-4271-a106-a5fb8e3056ca_1312x2216.png 848w, https://substackcdn.com/image/fetch/$s_!ashq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32a16a84-c874-4271-a106-a5fb8e3056ca_1312x2216.png 1272w, https://substackcdn.com/image/fetch/$s_!ashq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32a16a84-c874-4271-a106-a5fb8e3056ca_1312x2216.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ashq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32a16a84-c874-4271-a106-a5fb8e3056ca_1312x2216.png" width="1312" height="2216" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/32a16a84-c874-4271-a106-a5fb8e3056ca_1312x2216.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2216,&quot;width&quot;:1312,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:355590,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/199329194?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32a16a84-c874-4271-a106-a5fb8e3056ca_1312x2216.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ashq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32a16a84-c874-4271-a106-a5fb8e3056ca_1312x2216.png 424w, https://substackcdn.com/image/fetch/$s_!ashq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32a16a84-c874-4271-a106-a5fb8e3056ca_1312x2216.png 848w, https://substackcdn.com/image/fetch/$s_!ashq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32a16a84-c874-4271-a106-a5fb8e3056ca_1312x2216.png 1272w, https://substackcdn.com/image/fetch/$s_!ashq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32a16a84-c874-4271-a106-a5fb8e3056ca_1312x2216.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>After the imports the <strong>main</strong> function calls the two functions; you can comment/uncomment these to run the ones you want.</p><p>The <strong>barchart</strong> function creates a NumPy array of values representing a range of lens diameters. (This consists of all the integers in the range, most of which aren&#8217;t typically used for binoculars.) This is used to create a second NumPy array of areas; note that we can carry out a calculation on an array in one line of code without any explicit iteration.</p><p>These two array are used as arguments to a call to the Matplotlib <strong>bar</strong> method, a few cosmetic properties are set and the <strong>show</strong> method is called.</p><p>If you run the program with this command...</p><p><strong>python3 binocularlenses.py</strong></p><p>... you will see the following bar chart.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zOua!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9161cfc6-6c4a-4397-a9f0-c7bc6995825f_700x599.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zOua!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9161cfc6-6c4a-4397-a9f0-c7bc6995825f_700x599.png 424w, https://substackcdn.com/image/fetch/$s_!zOua!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9161cfc6-6c4a-4397-a9f0-c7bc6995825f_700x599.png 848w, https://substackcdn.com/image/fetch/$s_!zOua!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9161cfc6-6c4a-4397-a9f0-c7bc6995825f_700x599.png 1272w, https://substackcdn.com/image/fetch/$s_!zOua!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9161cfc6-6c4a-4397-a9f0-c7bc6995825f_700x599.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!zOua!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9161cfc6-6c4a-4397-a9f0-c7bc6995825f_700x599.png" width="700" height="599" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9161cfc6-6c4a-4397-a9f0-c7bc6995825f_700x599.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:599,&quot;width&quot;:700,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:26570,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/199329194?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9161cfc6-6c4a-4397-a9f0-c7bc6995825f_700x599.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zOua!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9161cfc6-6c4a-4397-a9f0-c7bc6995825f_700x599.png 424w, https://substackcdn.com/image/fetch/$s_!zOua!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9161cfc6-6c4a-4397-a9f0-c7bc6995825f_700x599.png 848w, https://substackcdn.com/image/fetch/$s_!zOua!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9161cfc6-6c4a-4397-a9f0-c7bc6995825f_700x599.png 1272w, https://substackcdn.com/image/fetch/$s_!zOua!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9161cfc6-6c4a-4397-a9f0-c7bc6995825f_700x599.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The important thing to note is that the lens diameter roughly doubles but the area increases about fivefold, exactly as shown in the diagram at the beginning of this article.</p><p>The <strong>table</strong> function creates a tuple of lens diameters actually used in binoculars, and then calculates the area of the first; you&#8217;ll see why in a moment.</p><p>The tuple is then iterated, calculating the area of each current lens. In the print statement we output the diameter and area, and then the area relative to the smallest lens which forms an index baseline value of 1. This forms a handy at-a-glance impression of the relative areas. (If you want to edit the values in the tuple note that they must be in ascending order for this code to work.)</p><p>In <strong>main</strong> comment out <strong>barchart()</strong>, uncomment <strong>table()</strong> and run the code again. This is the end result.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!z5rZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7419cbc1-f42d-438e-a73b-b061d2193953_445x187.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!z5rZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7419cbc1-f42d-438e-a73b-b061d2193953_445x187.png 424w, https://substackcdn.com/image/fetch/$s_!z5rZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7419cbc1-f42d-438e-a73b-b061d2193953_445x187.png 848w, https://substackcdn.com/image/fetch/$s_!z5rZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7419cbc1-f42d-438e-a73b-b061d2193953_445x187.png 1272w, https://substackcdn.com/image/fetch/$s_!z5rZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7419cbc1-f42d-438e-a73b-b061d2193953_445x187.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!z5rZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7419cbc1-f42d-438e-a73b-b061d2193953_445x187.png" width="445" height="187" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7419cbc1-f42d-438e-a73b-b061d2193953_445x187.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:187,&quot;width&quot;:445,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:10020,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/199329194?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7419cbc1-f42d-438e-a73b-b061d2193953_445x187.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!z5rZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7419cbc1-f42d-438e-a73b-b061d2193953_445x187.png 424w, https://substackcdn.com/image/fetch/$s_!z5rZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7419cbc1-f42d-438e-a73b-b061d2193953_445x187.png 848w, https://substackcdn.com/image/fetch/$s_!z5rZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7419cbc1-f42d-438e-a73b-b061d2193953_445x187.png 1272w, https://substackcdn.com/image/fetch/$s_!z5rZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7419cbc1-f42d-438e-a73b-b061d2193953_445x187.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/exploring-binocular-lens-sizes-with-python/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/exploring-binocular-lens-sizes-with-python/comments"><span>Leave a comment</span></a></p><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p><p></p>]]></content:encoded></item><item><title><![CDATA[First Steps in Isometrics with Python]]></title><description><![CDATA[The word isometric means &#8220;with equal measurements&#8221; and the best known use of the word is probably as a form of &#8220;pseudo-3d&#8221; graphics without perspective.]]></description><link>https://codedrome.substack.com/p/first-steps-in-isometrics-with-python</link><guid isPermaLink="false">https://codedrome.substack.com/p/first-steps-in-isometrics-with-python</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 26 May 2026 10:54:49 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!B7hj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d424c2-28dd-4cfb-802d-4df5d6d15013_600x400.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B7hj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d424c2-28dd-4cfb-802d-4df5d6d15013_600x400.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B7hj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d424c2-28dd-4cfb-802d-4df5d6d15013_600x400.png 424w, https://substackcdn.com/image/fetch/$s_!B7hj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d424c2-28dd-4cfb-802d-4df5d6d15013_600x400.png 848w, https://substackcdn.com/image/fetch/$s_!B7hj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d424c2-28dd-4cfb-802d-4df5d6d15013_600x400.png 1272w, https://substackcdn.com/image/fetch/$s_!B7hj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d424c2-28dd-4cfb-802d-4df5d6d15013_600x400.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B7hj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d424c2-28dd-4cfb-802d-4df5d6d15013_600x400.png" width="600" height="400" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/71d424c2-28dd-4cfb-802d-4df5d6d15013_600x400.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:400,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:33983,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197865104?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d424c2-28dd-4cfb-802d-4df5d6d15013_600x400.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!B7hj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d424c2-28dd-4cfb-802d-4df5d6d15013_600x400.png 424w, https://substackcdn.com/image/fetch/$s_!B7hj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d424c2-28dd-4cfb-802d-4df5d6d15013_600x400.png 848w, https://substackcdn.com/image/fetch/$s_!B7hj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d424c2-28dd-4cfb-802d-4df5d6d15013_600x400.png 1272w, https://substackcdn.com/image/fetch/$s_!B7hj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d424c2-28dd-4cfb-802d-4df5d6d15013_600x400.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The word isometric means &#8220;with equal measurements&#8221; and the best known use of the word is probably as a form of &#8220;pseudo-3d&#8221; graphics without perspective. This means that a particular distance will appear on screen the same size irrespective of its supposed distance from the viewer. You may already be familiar with isometric projections even if you didn&#8217;t know the term as they have been used for decades in video games, and are far simpler to calculate than true 3d coordinates.</p><p>In this article I&#8217;ll write a very minimalist introduction to the subject which will calculate the two-dimensional coordinates of a three-dimensional cube, and then plot them using Matplotlib. As the title of the article states this is very much a first step and I have a number of ideas for taking isometrics further.</p><p>The screenshot below illustrates just what we are going to achieve. The cube on the left is shown with perspective, the red lines being the near face and the green lines being the far face. On the right is the same cube but with the vertices (corners) and edges (lines between corners) calculated to give an isometric projection. The colours remain the same so you can see how the cube has been rotated right and down, although it&#8217;s probably better to think of the viewer moving left and up.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B99X!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F410b6405-0fa4-4e9c-b4e5-da6a3fb6fd49_835x508.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B99X!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F410b6405-0fa4-4e9c-b4e5-da6a3fb6fd49_835x508.png 424w, https://substackcdn.com/image/fetch/$s_!B99X!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F410b6405-0fa4-4e9c-b4e5-da6a3fb6fd49_835x508.png 848w, https://substackcdn.com/image/fetch/$s_!B99X!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F410b6405-0fa4-4e9c-b4e5-da6a3fb6fd49_835x508.png 1272w, https://substackcdn.com/image/fetch/$s_!B99X!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F410b6405-0fa4-4e9c-b4e5-da6a3fb6fd49_835x508.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B99X!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F410b6405-0fa4-4e9c-b4e5-da6a3fb6fd49_835x508.png" width="835" height="508" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/410b6405-0fa4-4e9c-b4e5-da6a3fb6fd49_835x508.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:508,&quot;width&quot;:835,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:32886,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197865104?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F410b6405-0fa4-4e9c-b4e5-da6a3fb6fd49_835x508.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!B99X!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F410b6405-0fa4-4e9c-b4e5-da6a3fb6fd49_835x508.png 424w, https://substackcdn.com/image/fetch/$s_!B99X!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F410b6405-0fa4-4e9c-b4e5-da6a3fb6fd49_835x508.png 848w, https://substackcdn.com/image/fetch/$s_!B99X!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F410b6405-0fa4-4e9c-b4e5-da6a3fb6fd49_835x508.png 1272w, https://substackcdn.com/image/fetch/$s_!B99X!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F410b6405-0fa4-4e9c-b4e5-da6a3fb6fd49_835x508.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The process of calculating the data we need to plot the cube on the right consists of the following steps:</p><ul><li><p>Create a set of 8 three-dimensional coordinates for the corners or vertices of the cube. These each consist of 3 values: <em>x</em> (horizontal), <em>y</em> (vertical) and <em>z</em> (front to back)</p></li><li><p>For each vertex calculate the two-dimensional <em>x</em> and <em>y</em> values. This is the core calculation for the whole field of isometrics.</p></li><li><p>From the 2d vertex coordinates create edges consisting of two coordinates each.</p></li><li><p>Also using the 2d vertex coordinates, calculate the top, bottom, left and right borders of the three visible faces of the cube.</p></li></ul><p>Having carried out the following steps we can pass the data to Matplotlib so it can do its thing and produce output like that shown above.</p><h2>The Dreaded Mathematics (Optional)</h2><p>Calculating two-dimensional <em>x,y</em> coordinates from three-dimensional <em>x,y,z</em> coordinates is an involved process using matrices, some of the matrices used having their values calculated using trigonometric functions. Untangling the trigonometry and matrix arithmetic is way beyond the scope of a programming article although I may write about it on my LaTeX Substack.</p><p>I&#8217;ll therefore just give a very brief overview of the process to give a flavour of what is going on. Don&#8217;t worry if you don&#8217;t understand this, and please feel free to skip to the next section if you aren&#8217;t interested in the mathematics.</p><p>There are three broad stages involved.</p><h3>Create Constants &#945; and &#946;</h3><p>These two constants are angles in radians.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6yx5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd40589b1-802c-4581-b2d5-41fc9ed5b86f_198x87.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6yx5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd40589b1-802c-4581-b2d5-41fc9ed5b86f_198x87.png 424w, https://substackcdn.com/image/fetch/$s_!6yx5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd40589b1-802c-4581-b2d5-41fc9ed5b86f_198x87.png 848w, https://substackcdn.com/image/fetch/$s_!6yx5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd40589b1-802c-4581-b2d5-41fc9ed5b86f_198x87.png 1272w, https://substackcdn.com/image/fetch/$s_!6yx5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd40589b1-802c-4581-b2d5-41fc9ed5b86f_198x87.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6yx5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd40589b1-802c-4581-b2d5-41fc9ed5b86f_198x87.png" width="198" height="87" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d40589b1-802c-4581-b2d5-41fc9ed5b86f_198x87.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:87,&quot;width&quot;:198,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:4594,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197865104?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd40589b1-802c-4581-b2d5-41fc9ed5b86f_198x87.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6yx5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd40589b1-802c-4581-b2d5-41fc9ed5b86f_198x87.png 424w, https://substackcdn.com/image/fetch/$s_!6yx5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd40589b1-802c-4581-b2d5-41fc9ed5b86f_198x87.png 848w, https://substackcdn.com/image/fetch/$s_!6yx5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd40589b1-802c-4581-b2d5-41fc9ed5b86f_198x87.png 1272w, https://substackcdn.com/image/fetch/$s_!6yx5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd40589b1-802c-4581-b2d5-41fc9ed5b86f_198x87.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3>Create Three Matrices</h3><p>These are effectively constant although many of the values are calculated using <em>&#945;</em>,<em> &#946;</em> and trigonometry.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!00-t!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76421de4-5be2-4a56-956e-960fb1a82792_419x202.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!00-t!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76421de4-5be2-4a56-956e-960fb1a82792_419x202.png 424w, https://substackcdn.com/image/fetch/$s_!00-t!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76421de4-5be2-4a56-956e-960fb1a82792_419x202.png 848w, https://substackcdn.com/image/fetch/$s_!00-t!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76421de4-5be2-4a56-956e-960fb1a82792_419x202.png 1272w, https://substackcdn.com/image/fetch/$s_!00-t!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76421de4-5be2-4a56-956e-960fb1a82792_419x202.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!00-t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76421de4-5be2-4a56-956e-960fb1a82792_419x202.png" width="419" height="202" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/76421de4-5be2-4a56-956e-960fb1a82792_419x202.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:202,&quot;width&quot;:419,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:15837,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197865104?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76421de4-5be2-4a56-956e-960fb1a82792_419x202.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!00-t!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76421de4-5be2-4a56-956e-960fb1a82792_419x202.png 424w, https://substackcdn.com/image/fetch/$s_!00-t!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76421de4-5be2-4a56-956e-960fb1a82792_419x202.png 848w, https://substackcdn.com/image/fetch/$s_!00-t!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76421de4-5be2-4a56-956e-960fb1a82792_419x202.png 1272w, https://substackcdn.com/image/fetch/$s_!00-t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76421de4-5be2-4a56-956e-960fb1a82792_419x202.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3>Calculate Two-Dimensional Coordinates</h3><p>The first matrix, <em>a</em>, contains the three-dimensional coordinates. The second, <em>c</em>, is the product of <em>m1</em> and <em>m2</em> from the second stage and <em>a</em>.</p><p>Finally, <em>m3</em> and <em>c</em> are multiplied to create a matrix containing the <em>x</em> and <em>y</em> coordinates. (The others are always 0 and superfluous.)</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VsQG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F713e031d-5e5f-4637-a143-187e66b11d25_181x159.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VsQG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F713e031d-5e5f-4637-a143-187e66b11d25_181x159.png 424w, https://substackcdn.com/image/fetch/$s_!VsQG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F713e031d-5e5f-4637-a143-187e66b11d25_181x159.png 848w, https://substackcdn.com/image/fetch/$s_!VsQG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F713e031d-5e5f-4637-a143-187e66b11d25_181x159.png 1272w, https://substackcdn.com/image/fetch/$s_!VsQG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F713e031d-5e5f-4637-a143-187e66b11d25_181x159.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VsQG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F713e031d-5e5f-4637-a143-187e66b11d25_181x159.png" width="181" height="159" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/713e031d-5e5f-4637-a143-187e66b11d25_181x159.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:159,&quot;width&quot;:181,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:4194,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197865104?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F713e031d-5e5f-4637-a143-187e66b11d25_181x159.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VsQG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F713e031d-5e5f-4637-a143-187e66b11d25_181x159.png 424w, https://substackcdn.com/image/fetch/$s_!VsQG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F713e031d-5e5f-4637-a143-187e66b11d25_181x159.png 848w, https://substackcdn.com/image/fetch/$s_!VsQG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F713e031d-5e5f-4637-a143-187e66b11d25_181x159.png 1272w, https://substackcdn.com/image/fetch/$s_!VsQG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F713e031d-5e5f-4637-a143-187e66b11d25_181x159.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h2>The Project (Back to Python!)</h2><p>This project consists of the following files which you can clone or download from the <a href="https://github.com/CodeDrome/isometrics-python">Github repository</a>.</p><ul><li><p>isometric.py</p></li><li><p>isometricplotter.py</p></li><li><p>isometricdemo.py</p></li><li><p>isometricillustrator.py</p></li></ul><p>The last of these, <em>isometricillustrator.py</em>, creates the side-by-side plots shown above. I won&#8217;t show the code here as it&#8217;s basically just a load of hardcoded coordinates.</p><p>The code uses NumPy and Matplotlib. If you are not familiar with these please read my introductory articles on them.</p><p><a href="https://codedrome.substack.com/p/a-numpy-reference">https://codedrome.substack.com/p/a-numpy-reference</a></p><p><a href="https://codedrome.substack.com/p/a-matplotlib-reference">https://codedrome.substack.com/p/a-matplotlib-reference</a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h2>isometric.py</h2><p>This is the code for <em>isometric.py</em> which implements the mathematics shown above, and also calculates the vertices, edges and faces of a cuboid.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!e5Wo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1dc1255-7e7e-4cca-88a5-fff5d5c958bd_1574x5608.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!e5Wo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1dc1255-7e7e-4cca-88a5-fff5d5c958bd_1574x5608.png 424w, https://substackcdn.com/image/fetch/$s_!e5Wo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1dc1255-7e7e-4cca-88a5-fff5d5c958bd_1574x5608.png 848w, https://substackcdn.com/image/fetch/$s_!e5Wo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1dc1255-7e7e-4cca-88a5-fff5d5c958bd_1574x5608.png 1272w, https://substackcdn.com/image/fetch/$s_!e5Wo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1dc1255-7e7e-4cca-88a5-fff5d5c958bd_1574x5608.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!e5Wo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1dc1255-7e7e-4cca-88a5-fff5d5c958bd_1574x5608.png" width="1456" height="5188" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a1dc1255-7e7e-4cca-88a5-fff5d5c958bd_1574x5608.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:5188,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:861983,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197865104?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1dc1255-7e7e-4cca-88a5-fff5d5c958bd_1574x5608.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!e5Wo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1dc1255-7e7e-4cca-88a5-fff5d5c958bd_1574x5608.png 424w, https://substackcdn.com/image/fetch/$s_!e5Wo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1dc1255-7e7e-4cca-88a5-fff5d5c958bd_1574x5608.png 848w, https://substackcdn.com/image/fetch/$s_!e5Wo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1dc1255-7e7e-4cca-88a5-fff5d5c958bd_1574x5608.png 1272w, https://substackcdn.com/image/fetch/$s_!e5Wo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1dc1255-7e7e-4cca-88a5-fff5d5c958bd_1574x5608.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Constant Values and Matrices</h3><p>These are the constants which form the first step of the calculations. The symbols are the Greek letters <em>alpha</em> and <em>beta</em> and although I rarely use characters in code that aren&#8217;t on my keyboard I decided to do so here to conform to the usual conventions for these calculations.</p><p>The matrices <em>m1</em>, <em>m2</em> and <em>m3</em> are then created, partly using sine functions and <em>&#945;</em> and <em>&#946;</em>, and partly hard-coded.</p><h3>xyz_to_xy</h3><p>This function is at the heart isometric rendering as it converts three-dimensional coordinates to two dimensions, specifically <em>x,y,z</em> to <em>x,y</em>. If you wish to follow the mathematics the code corresponds to the following, repeated from above.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!epHZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18abfb64-1e3a-48ec-8b82-a3427f8129b2_181x159.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!epHZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18abfb64-1e3a-48ec-8b82-a3427f8129b2_181x159.png 424w, https://substackcdn.com/image/fetch/$s_!epHZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18abfb64-1e3a-48ec-8b82-a3427f8129b2_181x159.png 848w, https://substackcdn.com/image/fetch/$s_!epHZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18abfb64-1e3a-48ec-8b82-a3427f8129b2_181x159.png 1272w, https://substackcdn.com/image/fetch/$s_!epHZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18abfb64-1e3a-48ec-8b82-a3427f8129b2_181x159.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!epHZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18abfb64-1e3a-48ec-8b82-a3427f8129b2_181x159.png" width="181" height="159" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/18abfb64-1e3a-48ec-8b82-a3427f8129b2_181x159.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:159,&quot;width&quot;:181,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:4194,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197865104?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18abfb64-1e3a-48ec-8b82-a3427f8129b2_181x159.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!epHZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18abfb64-1e3a-48ec-8b82-a3427f8129b2_181x159.png 424w, https://substackcdn.com/image/fetch/$s_!epHZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18abfb64-1e3a-48ec-8b82-a3427f8129b2_181x159.png 848w, https://substackcdn.com/image/fetch/$s_!epHZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18abfb64-1e3a-48ec-8b82-a3427f8129b2_181x159.png 1272w, https://substackcdn.com/image/fetch/$s_!epHZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18abfb64-1e3a-48ec-8b82-a3427f8129b2_181x159.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3>cuboid</h3><p>This function creates a dictionary of vertices, edges and faces which make up a cuboid with the specified position and size. The separate elements are generated by the various functions described below.</p><h3>_calculate_cuboid_vertices_3d</h3><p>This function&#8217;s parameters are the <strong>ltf</strong> (left, top, front) and <strong>whd</strong> (width, height, depth) arguments from the cuboid function, split out into separate variables to make them easier to use.</p><p>Firstly a NumPy array is created to represent a matrix with all values the same, specifically the left top front vertex. The second array contains values specifying how much each point has to be shifted in each axis. By adding these together we end up with a matrix of coordinates of the eight vertices.</p><h3>_calculate_cuboid_vertices_2d</h3><p>This function &#8220;flattens&#8221; the 3D coordinates down to 2D by calling <strong>xyz_to_xy</strong> on each one and adding the result to a list.</p><h3>_calculate_cuboid_edges_2d</h3><p>The twelve edges are created rather tediously by appending the necessary pairs of vertices to a list. I couldn&#8217;t discern a pattern to the indices that could be used to streamline the process - if you can then please let me know or, better still, write a shorter and neater version of this function. Thank you.</p><h3>_calculate_cuboid_faces_2d</h3><p>Here we create a list of the coordinates of the vertices of the front faces of the cuboid, the ones that can be seen and need filling. The same comment applies as with <strong>_calculate_cuboid_edges_2d</strong>: if you can make it more concise please do so.</p><h2>isometricplotter.py</h2><p>The <em>isometricplotter.py</em> file contains code to plot isometric objects.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!swE2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999b7728-7233-437f-8eaa-273d0cf2e501_1574x1512.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!swE2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999b7728-7233-437f-8eaa-273d0cf2e501_1574x1512.png 424w, https://substackcdn.com/image/fetch/$s_!swE2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999b7728-7233-437f-8eaa-273d0cf2e501_1574x1512.png 848w, https://substackcdn.com/image/fetch/$s_!swE2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999b7728-7233-437f-8eaa-273d0cf2e501_1574x1512.png 1272w, https://substackcdn.com/image/fetch/$s_!swE2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999b7728-7233-437f-8eaa-273d0cf2e501_1574x1512.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!swE2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999b7728-7233-437f-8eaa-273d0cf2e501_1574x1512.png" width="1456" height="1399" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/999b7728-7233-437f-8eaa-273d0cf2e501_1574x1512.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1399,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:178863,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197865104?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999b7728-7233-437f-8eaa-273d0cf2e501_1574x1512.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!swE2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999b7728-7233-437f-8eaa-273d0cf2e501_1574x1512.png 424w, https://substackcdn.com/image/fetch/$s_!swE2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999b7728-7233-437f-8eaa-273d0cf2e501_1574x1512.png 848w, https://substackcdn.com/image/fetch/$s_!swE2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999b7728-7233-437f-8eaa-273d0cf2e501_1574x1512.png 1272w, https://substackcdn.com/image/fetch/$s_!swE2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F999b7728-7233-437f-8eaa-273d0cf2e501_1574x1512.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The components list is iterated with a <strong>for/in</strong> loop which in turn iterates and plots the three types of element making up each shape. The vertices are plotted first; note the marker is set to &#8220;.&#8221; and that the first colour in the list is used. The edges have no markers, a linestyle of &#8220;-&#8221; and are drawn with the second colour.</p><p>Finally the faces are drawn with the <strong>fill_between</strong> method which, to quote <a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.fill_between.html">the documentation</a>, &#8220;fills the area between two horizontal curves&#8221;. Each coordinate argument, <strong>x</strong>, <strong>y1</strong> and <strong>y2</strong>, consists of two coordinates so together they make up a two-dimensional area to fill. The last of the colours is used, and I have set alpha or opacity to 0.25 so the bits of the cuboid hidden by faces are still visible.</p><h2>isometricdemo.py</h2><p>In <em>isometricdemo.py</em> we try out the code listed above.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kzTq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe57119a2-95cd-40e5-bcea-04bb13adfedd_1574x872.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kzTq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe57119a2-95cd-40e5-bcea-04bb13adfedd_1574x872.png 424w, https://substackcdn.com/image/fetch/$s_!kzTq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe57119a2-95cd-40e5-bcea-04bb13adfedd_1574x872.png 848w, https://substackcdn.com/image/fetch/$s_!kzTq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe57119a2-95cd-40e5-bcea-04bb13adfedd_1574x872.png 1272w, https://substackcdn.com/image/fetch/$s_!kzTq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe57119a2-95cd-40e5-bcea-04bb13adfedd_1574x872.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kzTq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe57119a2-95cd-40e5-bcea-04bb13adfedd_1574x872.png" width="1456" height="807" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e57119a2-95cd-40e5-bcea-04bb13adfedd_1574x872.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:807,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:141374,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197865104?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe57119a2-95cd-40e5-bcea-04bb13adfedd_1574x872.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!kzTq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe57119a2-95cd-40e5-bcea-04bb13adfedd_1574x872.png 424w, https://substackcdn.com/image/fetch/$s_!kzTq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe57119a2-95cd-40e5-bcea-04bb13adfedd_1574x872.png 848w, https://substackcdn.com/image/fetch/$s_!kzTq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe57119a2-95cd-40e5-bcea-04bb13adfedd_1574x872.png 1272w, https://substackcdn.com/image/fetch/$s_!kzTq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe57119a2-95cd-40e5-bcea-04bb13adfedd_1574x872.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The isometricplotter&#8217;s <strong>plot</strong> function takes a list of components to plot so first we create an empty list, then create a single cuboid which is appended to the list. Finally this list is passed to plot along with a few other arguments as described above.</p><p>The commented line calls a function to create the double plot showing a colour-coded illustration of the rotation process.</p><h2>Running the Program</h2><p>Run the program like this:</p><p><strong>python3 isometricdemo.py</strong></p><p>This is the end result.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZcWI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01fe5b2a-74a5-4abc-bf85-6b2d65b9001c_712x615.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZcWI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01fe5b2a-74a5-4abc-bf85-6b2d65b9001c_712x615.png 424w, https://substackcdn.com/image/fetch/$s_!ZcWI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01fe5b2a-74a5-4abc-bf85-6b2d65b9001c_712x615.png 848w, https://substackcdn.com/image/fetch/$s_!ZcWI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01fe5b2a-74a5-4abc-bf85-6b2d65b9001c_712x615.png 1272w, https://substackcdn.com/image/fetch/$s_!ZcWI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01fe5b2a-74a5-4abc-bf85-6b2d65b9001c_712x615.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZcWI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01fe5b2a-74a5-4abc-bf85-6b2d65b9001c_712x615.png" width="712" height="615" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/01fe5b2a-74a5-4abc-bf85-6b2d65b9001c_712x615.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:615,&quot;width&quot;:712,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:37784,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197865104?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01fe5b2a-74a5-4abc-bf85-6b2d65b9001c_712x615.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ZcWI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01fe5b2a-74a5-4abc-bf85-6b2d65b9001c_712x615.png 424w, https://substackcdn.com/image/fetch/$s_!ZcWI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01fe5b2a-74a5-4abc-bf85-6b2d65b9001c_712x615.png 848w, https://substackcdn.com/image/fetch/$s_!ZcWI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01fe5b2a-74a5-4abc-bf85-6b2d65b9001c_712x615.png 1272w, https://substackcdn.com/image/fetch/$s_!ZcWI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01fe5b2a-74a5-4abc-bf85-6b2d65b9001c_712x615.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>Conclusion</h2><p>The code which calculates two-dimensional coordinates from three-dimensional ones is at the core of isometrics so by coding this process we have laid a firm foundation, as well as put it to good use by drawing a &#8220;box&#8221;. However, this is only the first step and in future articles I will use the code in PyGame and add further shapes.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/first-steps-in-isometrics-with-python/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/first-steps-in-isometrics-with-python/comments"><span>Leave a comment</span></a></p><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p>]]></content:encoded></item><item><title><![CDATA[Exploring SI Units in Python Part 1: Base Units]]></title><description><![CDATA[The International System of Units, commonly known as the SI System, consists of seven base units for measuring quantities such as mass, time and electric current.]]></description><link>https://codedrome.substack.com/p/exploring-si-units-in-python-part-1</link><guid isPermaLink="false">https://codedrome.substack.com/p/exploring-si-units-in-python-part-1</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 19 May 2026 14:43:48 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!2Xzb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ad9228c-5d5b-4734-9c8f-171b43fe622e_600x400.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2Xzb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ad9228c-5d5b-4734-9c8f-171b43fe622e_600x400.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2Xzb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ad9228c-5d5b-4734-9c8f-171b43fe622e_600x400.jpeg 424w, https://substackcdn.com/image/fetch/$s_!2Xzb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ad9228c-5d5b-4734-9c8f-171b43fe622e_600x400.jpeg 848w, https://substackcdn.com/image/fetch/$s_!2Xzb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ad9228c-5d5b-4734-9c8f-171b43fe622e_600x400.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!2Xzb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ad9228c-5d5b-4734-9c8f-171b43fe622e_600x400.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2Xzb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ad9228c-5d5b-4734-9c8f-171b43fe622e_600x400.jpeg" width="600" height="400" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3ad9228c-5d5b-4734-9c8f-171b43fe622e_600x400.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:400,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:55937,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197357176?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ad9228c-5d5b-4734-9c8f-171b43fe622e_600x400.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!2Xzb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ad9228c-5d5b-4734-9c8f-171b43fe622e_600x400.jpeg 424w, https://substackcdn.com/image/fetch/$s_!2Xzb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ad9228c-5d5b-4734-9c8f-171b43fe622e_600x400.jpeg 848w, https://substackcdn.com/image/fetch/$s_!2Xzb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ad9228c-5d5b-4734-9c8f-171b43fe622e_600x400.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!2Xzb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ad9228c-5d5b-4734-9c8f-171b43fe622e_600x400.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Image: <a href="https://pixabay.com/photos/laboratory-apparatus-equipment-217041/">Pixabay</a></figcaption></figure></div><p>The <em>International System of Units</em>, commonly known as the <em>SI System</em>, consists of seven base units for measuring quantities such as mass, time and electric current. These base units can be combined into <em>derived units</em> to measure a wide range of other quantities.</p><p>In this article I will develop a Python class to represent base unit values and associated methods. In a future article I will extend the project to handle derived units.</p><h2>SI Units</h2><p>As I mentioned above there are seven base units, as listed below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!olIL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7129c108-82cf-47d8-8ae6-ac974b112d40_950x392.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!olIL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7129c108-82cf-47d8-8ae6-ac974b112d40_950x392.png 424w, https://substackcdn.com/image/fetch/$s_!olIL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7129c108-82cf-47d8-8ae6-ac974b112d40_950x392.png 848w, https://substackcdn.com/image/fetch/$s_!olIL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7129c108-82cf-47d8-8ae6-ac974b112d40_950x392.png 1272w, https://substackcdn.com/image/fetch/$s_!olIL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7129c108-82cf-47d8-8ae6-ac974b112d40_950x392.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!olIL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7129c108-82cf-47d8-8ae6-ac974b112d40_950x392.png" width="950" height="392" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7129c108-82cf-47d8-8ae6-ac974b112d40_950x392.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:392,&quot;width&quot;:950,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:95743,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197357176?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7129c108-82cf-47d8-8ae6-ac974b112d40_950x392.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!olIL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7129c108-82cf-47d8-8ae6-ac974b112d40_950x392.png 424w, https://substackcdn.com/image/fetch/$s_!olIL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7129c108-82cf-47d8-8ae6-ac974b112d40_950x392.png 848w, https://substackcdn.com/image/fetch/$s_!olIL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7129c108-82cf-47d8-8ae6-ac974b112d40_950x392.png 1272w, https://substackcdn.com/image/fetch/$s_!olIL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7129c108-82cf-47d8-8ae6-ac974b112d40_950x392.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>There is also a wide range of prefixes representing both positive and negative powers. These are used to create more conventiently sized units although many unit/prefix combinations are not widely used. (When did you last hear anybody say &#8220;yottamole&#8221;?!)</p><p>I won&#8217;t list the prefixes here because that would just be boring, but later we&#8217;ll see a full list hard-coded in Python.</p><h2>The Project</h2><p>This project consists of the following files which you can find in the <a href="https://github.com/CodeDrome/si-units-python">GitHub repository</a>.</p><ul><li><p>sibaseunit.py</p></li><li><p>siunits.py</p></li></ul><p>The purpose of this project is to create a class that will represent an amount of the required quantity along with any specified prefix. The class will also provide a number of arithmetic and comparison methods which can be safely used with units of the same quantity but different prefixes, for example adding metres and centimetres.</p><p>This class will also form the basis of a further project on derived units.</p><h2>The Code</h2><p>Let&#8217;s get stuck into the code, starting with the <strong>SIBaseUnit</strong> class in <em>sibaseunit.py</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sZW4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59930ce1-8355-46ae-9126-850c527b7e0e_1876x8456.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sZW4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59930ce1-8355-46ae-9126-850c527b7e0e_1876x8456.png 424w, https://substackcdn.com/image/fetch/$s_!sZW4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59930ce1-8355-46ae-9126-850c527b7e0e_1876x8456.png 848w, https://substackcdn.com/image/fetch/$s_!sZW4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59930ce1-8355-46ae-9126-850c527b7e0e_1876x8456.png 1272w, https://substackcdn.com/image/fetch/$s_!sZW4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59930ce1-8355-46ae-9126-850c527b7e0e_1876x8456.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sZW4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59930ce1-8355-46ae-9126-850c527b7e0e_1876x8456.png" width="1876" height="8456" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/59930ce1-8355-46ae-9126-850c527b7e0e_1876x8456.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:8456,&quot;width&quot;:1876,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2119050,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197357176?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70924bd0-db82-4f76-b248-e32d535c43f3_1876x8456.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!sZW4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59930ce1-8355-46ae-9126-850c527b7e0e_1876x8456.png 424w, https://substackcdn.com/image/fetch/$s_!sZW4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59930ce1-8355-46ae-9126-850c527b7e0e_1876x8456.png 848w, https://substackcdn.com/image/fetch/$s_!sZW4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59930ce1-8355-46ae-9126-850c527b7e0e_1876x8456.png 1272w, https://substackcdn.com/image/fetch/$s_!sZW4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F59930ce1-8355-46ae-9126-850c527b7e0e_1876x8456.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This isn&#8217;t a hugely long class but there&#8217;s a lot to discuss so I&#8217;ll work through each point one at a time.</p><h3>quantities</h3><p>This is a dictionary (wrapped in an immutable <strong>MappingProxyType</strong>) of the seven base units, each containing a &#8220;sub-dictionary&#8221; of details.</p><h3>prefixes</h3><p>Another <strong>MappingProxyType</strong> wrapped dictionary, this time for the prefixes including &#8220;none&#8221;.</p><h3>__init__</h3><p>The <strong>__init__</strong> method takes a value from the quantities and prefixes dictionaries, and also a value representing the amount of the quantity. This value is stored internally in the base unit, irrespective of any prefix. The conversion is done using the <strong>prefixed_value_to_base</strong> method which we&#8217;ll see later.</p><p>As I mentioned in the table above the SI unit of mass is the kilogram, not gram. For the purposes of this project I have assumed common sense prevailed so used the gram for base values of mass. No doubt the SI Police will be after me soon.</p><h2>Properties</h2><p>There are two properties, <strong>value</strong> and <strong>prefix</strong>. <strong>value</strong> is settable and converted to the base unit. The <strong>prefix</strong> property is gettable only.</p><h3>__repr__ and __str__</h3><p>The <strong>__repr__</strong> method combines the properties into a string representation of the object, and <strong>__str__</strong> simply calls <strong>string_short</strong> which we&#8217;ll see next.</p><h3>string_long and string_short</h3><p>The <strong>string_long</strong> method returns the value and the long version of the unit name, for example kilogram or millimetre. The <strong>string_short</strong> method is similar but uses the abbreviation such as <em>kg</em> or <em>mm</em>.</p><h2>Arithmetic and Comparison Operators</h2><p>If you are writing a class that represents objects which can have arithmetic or comparison operators carried out on them then you can easily implement these operations with <em>dunder</em> methods, ie methods with names that start and end with <em>d</em>ouble <em>under</em>scores.</p><p>The methods have meaningful names like <strong>__add__</strong> for addition and <strong>__lt__</strong> for less than. Within these methods you can do whatever you need to achieve the correct result, and here it is necessary in most cases to check that the two <strong>SIBaseUnit</strong> objects represent the same quantity so we don&#8217;t, for example, try to add a mass and a length. This is done by a separate function, <strong>__check_quantities</strong>. However, the prefixes can be different so values need to be converted to base values before operations are carried out, and converted back to prefixed values before being returned.</p><p>Once these dunder methods are implemented we can use symbols such as + and &lt; with instances of the class.</p><p>There are two dunder methods for multiplication: <strong>__mul__</strong> and <strong>__rmul__</strong>. The first is for when the object is on the left of the expression and multiplier on the right, and the second is for when they are reversed. This is an example snippet where length is an instance of SIBaseUnit.</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;acaea463-00c6-4f22-bbd8-aa6e1d3f5a7a&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python">length * 4 # __mul__ is called
6 * length # __rmul__ is called</code></pre></div><p>Another slightly mysteriously named dunder method is <strong>__truediv__</strong> which is normal rather than floor division. It is used when the / operator is used rather than the // operator. (If you need floor division implement <strong>__floordiv__</strong>. There was also a <strong>__div__</strong> in Python 2.x which is now obsolete.)</p><h3>__check_quantities</h3><p>As I mentioned most of the dunder methods work on two instances of <strong>SIBaseUnit</strong> so need to check the quantities are the same. To avoid repetition this check is split off into a separate function which raises a <strong>TypeError</strong> if they are different.</p><h3>output</h3><p>This method prints out all the information there is on the current instance. You probably wouldn&#8217;t use it in production code but it&#8217;s useful for demoing and testing.</p><h3>Converting to and from Prefixed Values</h3><p>Finally we have a couple of static methods for converting between base values and prefixed values.</p><h2>Let&#8217;s Try it Out</h2><p>That&#8217;s the <strong>SIBaseUnit</strong> class finished so let&#8217;s write a bit of code to show it in action. This is <em>siunits.py.</em></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!L5sj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f201833-7c30-4f70-ab4a-212a162094be_1876x7112.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!L5sj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f201833-7c30-4f70-ab4a-212a162094be_1876x7112.png 424w, https://substackcdn.com/image/fetch/$s_!L5sj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f201833-7c30-4f70-ab4a-212a162094be_1876x7112.png 848w, https://substackcdn.com/image/fetch/$s_!L5sj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f201833-7c30-4f70-ab4a-212a162094be_1876x7112.png 1272w, https://substackcdn.com/image/fetch/$s_!L5sj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f201833-7c30-4f70-ab4a-212a162094be_1876x7112.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!L5sj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f201833-7c30-4f70-ab4a-212a162094be_1876x7112.png" width="1876" height="7112" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7f201833-7c30-4f70-ab4a-212a162094be_1876x7112.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:7112,&quot;width&quot;:1876,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1655973,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197357176?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7f99a21-a0a1-42fc-8064-77131b292ee7_1876x7112.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!L5sj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f201833-7c30-4f70-ab4a-212a162094be_1876x7112.png 424w, https://substackcdn.com/image/fetch/$s_!L5sj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f201833-7c30-4f70-ab4a-212a162094be_1876x7112.png 848w, https://substackcdn.com/image/fetch/$s_!L5sj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f201833-7c30-4f70-ab4a-212a162094be_1876x7112.png 1272w, https://substackcdn.com/image/fetch/$s_!L5sj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f201833-7c30-4f70-ab4a-212a162094be_1876x7112.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>There are five functions here which are called in <strong>main</strong>. We can uncomment each one in turn and run it.</p><h3>quantities</h3><p>The first is <strong>quantities</strong> which creates 7 instances of <strong>SIBaseUnit</strong>, one for each quantity, before calling their <strong>string_long</strong>, <strong>string_short</strong> and <strong>output</strong> methods.</p><p>Now run the program:</p><p><strong>python3 siunits.py</strong></p><p>This is the output, showing only the first object, <strong>time</strong>.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wGAO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb31ec7ca-8020-4ca9-9710-118f470660b3_275x196.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wGAO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb31ec7ca-8020-4ca9-9710-118f470660b3_275x196.png 424w, https://substackcdn.com/image/fetch/$s_!wGAO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb31ec7ca-8020-4ca9-9710-118f470660b3_275x196.png 848w, https://substackcdn.com/image/fetch/$s_!wGAO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb31ec7ca-8020-4ca9-9710-118f470660b3_275x196.png 1272w, https://substackcdn.com/image/fetch/$s_!wGAO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb31ec7ca-8020-4ca9-9710-118f470660b3_275x196.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wGAO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb31ec7ca-8020-4ca9-9710-118f470660b3_275x196.png" width="275" height="196" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b31ec7ca-8020-4ca9-9710-118f470660b3_275x196.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:196,&quot;width&quot;:275,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:16532,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197357176?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb31ec7ca-8020-4ca9-9710-118f470660b3_275x196.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wGAO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb31ec7ca-8020-4ca9-9710-118f470660b3_275x196.png 424w, https://substackcdn.com/image/fetch/$s_!wGAO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb31ec7ca-8020-4ca9-9710-118f470660b3_275x196.png 848w, https://substackcdn.com/image/fetch/$s_!wGAO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb31ec7ca-8020-4ca9-9710-118f470660b3_275x196.png 1272w, https://substackcdn.com/image/fetch/$s_!wGAO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb31ec7ca-8020-4ca9-9710-118f470660b3_275x196.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3>add_and_subtract</h3><p>The next function creates two <strong>SIBaseUnit</strong> objects representing lengths, but with different prefixes: the first is centimetres and the second in millimetres. However, we can still add and subtract them and get the correct results which will have the suffix of the first instance, in this case centimetres.</p><p>Uncomment <strong>add_and_subtract</strong> and run the program again.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!OJ4I!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a890d21-1e2a-4a17-a0bb-c516c0142ae2_211x77.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!OJ4I!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a890d21-1e2a-4a17-a0bb-c516c0142ae2_211x77.png 424w, https://substackcdn.com/image/fetch/$s_!OJ4I!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a890d21-1e2a-4a17-a0bb-c516c0142ae2_211x77.png 848w, https://substackcdn.com/image/fetch/$s_!OJ4I!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a890d21-1e2a-4a17-a0bb-c516c0142ae2_211x77.png 1272w, https://substackcdn.com/image/fetch/$s_!OJ4I!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a890d21-1e2a-4a17-a0bb-c516c0142ae2_211x77.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!OJ4I!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a890d21-1e2a-4a17-a0bb-c516c0142ae2_211x77.png" width="211" height="77" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2a890d21-1e2a-4a17-a0bb-c516c0142ae2_211x77.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:77,&quot;width&quot;:211,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:7933,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197357176?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a890d21-1e2a-4a17-a0bb-c516c0142ae2_211x77.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!OJ4I!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a890d21-1e2a-4a17-a0bb-c516c0142ae2_211x77.png 424w, https://substackcdn.com/image/fetch/$s_!OJ4I!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a890d21-1e2a-4a17-a0bb-c516c0142ae2_211x77.png 848w, https://substackcdn.com/image/fetch/$s_!OJ4I!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a890d21-1e2a-4a17-a0bb-c516c0142ae2_211x77.png 1272w, https://substackcdn.com/image/fetch/$s_!OJ4I!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a890d21-1e2a-4a17-a0bb-c516c0142ae2_211x77.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3>add_error</h3><p>Just to demonstrate what happens if you try to carry out operations on units with different quantities I have attempted to add a length and a mass, within a <strong>try/except</strong> block which prints any error. As you can see if you run the function the error message is shown.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JDNp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef4fb549-5093-4ffe-b2e7-db8481f03614_291x59.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JDNp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef4fb549-5093-4ffe-b2e7-db8481f03614_291x59.png 424w, https://substackcdn.com/image/fetch/$s_!JDNp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef4fb549-5093-4ffe-b2e7-db8481f03614_291x59.png 848w, https://substackcdn.com/image/fetch/$s_!JDNp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef4fb549-5093-4ffe-b2e7-db8481f03614_291x59.png 1272w, https://substackcdn.com/image/fetch/$s_!JDNp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef4fb549-5093-4ffe-b2e7-db8481f03614_291x59.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JDNp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef4fb549-5093-4ffe-b2e7-db8481f03614_291x59.png" width="291" height="59" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ef4fb549-5093-4ffe-b2e7-db8481f03614_291x59.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:59,&quot;width&quot;:291,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:9281,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197357176?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef4fb549-5093-4ffe-b2e7-db8481f03614_291x59.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JDNp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef4fb549-5093-4ffe-b2e7-db8481f03614_291x59.png 424w, https://substackcdn.com/image/fetch/$s_!JDNp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef4fb549-5093-4ffe-b2e7-db8481f03614_291x59.png 848w, https://substackcdn.com/image/fetch/$s_!JDNp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef4fb549-5093-4ffe-b2e7-db8481f03614_291x59.png 1272w, https://substackcdn.com/image/fetch/$s_!JDNp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef4fb549-5093-4ffe-b2e7-db8481f03614_291x59.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3>multiply_and_divide</h3><p>These two operations work on an instance of <strong>SIBaseUnit</strong> and a numeric value, rather than two <strong>SIBaseUnit</strong> objects. This function shows the operations in action including the alternative &#8220;flavour&#8221; of multiplication where the multiplier comes first.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!19We!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76b838bd-b1ff-452e-8dca-d26981b2a301_155x76.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!19We!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76b838bd-b1ff-452e-8dca-d26981b2a301_155x76.png 424w, https://substackcdn.com/image/fetch/$s_!19We!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76b838bd-b1ff-452e-8dca-d26981b2a301_155x76.png 848w, https://substackcdn.com/image/fetch/$s_!19We!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76b838bd-b1ff-452e-8dca-d26981b2a301_155x76.png 1272w, https://substackcdn.com/image/fetch/$s_!19We!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76b838bd-b1ff-452e-8dca-d26981b2a301_155x76.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!19We!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76b838bd-b1ff-452e-8dca-d26981b2a301_155x76.png" width="155" height="76" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/76b838bd-b1ff-452e-8dca-d26981b2a301_155x76.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:76,&quot;width&quot;:155,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:7745,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197357176?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76b838bd-b1ff-452e-8dca-d26981b2a301_155x76.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!19We!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76b838bd-b1ff-452e-8dca-d26981b2a301_155x76.png 424w, https://substackcdn.com/image/fetch/$s_!19We!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76b838bd-b1ff-452e-8dca-d26981b2a301_155x76.png 848w, https://substackcdn.com/image/fetch/$s_!19We!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76b838bd-b1ff-452e-8dca-d26981b2a301_155x76.png 1272w, https://substackcdn.com/image/fetch/$s_!19We!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F76b838bd-b1ff-452e-8dca-d26981b2a301_155x76.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3>comparison_operators</h3><p>Finally let&#8217;s look at the six comparison operators. As with addition and subtraction they can safely be used when units have different prefixes.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-QTN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253ad37-a653-40aa-8b6c-148b7b8e9aff_170x109.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-QTN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253ad37-a653-40aa-8b6c-148b7b8e9aff_170x109.png 424w, https://substackcdn.com/image/fetch/$s_!-QTN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253ad37-a653-40aa-8b6c-148b7b8e9aff_170x109.png 848w, https://substackcdn.com/image/fetch/$s_!-QTN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253ad37-a653-40aa-8b6c-148b7b8e9aff_170x109.png 1272w, https://substackcdn.com/image/fetch/$s_!-QTN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253ad37-a653-40aa-8b6c-148b7b8e9aff_170x109.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-QTN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253ad37-a653-40aa-8b6c-148b7b8e9aff_170x109.png" width="170" height="109" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b253ad37-a653-40aa-8b6c-148b7b8e9aff_170x109.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:109,&quot;width&quot;:170,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:8245,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/197357176?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253ad37-a653-40aa-8b6c-148b7b8e9aff_170x109.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-QTN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253ad37-a653-40aa-8b6c-148b7b8e9aff_170x109.png 424w, https://substackcdn.com/image/fetch/$s_!-QTN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253ad37-a653-40aa-8b6c-148b7b8e9aff_170x109.png 848w, https://substackcdn.com/image/fetch/$s_!-QTN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253ad37-a653-40aa-8b6c-148b7b8e9aff_170x109.png 1272w, https://substackcdn.com/image/fetch/$s_!-QTN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb253ad37-a653-40aa-8b6c-148b7b8e9aff_170x109.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>So now we have a class which can handle the SI base units together with a wide selection of prefixes and operations. Next I&#8217;ll combine them into derived units such as metre per second.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/exploring-si-units-in-python-part-1/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/exploring-si-units-in-python-part-1/comments"><span>Leave a comment</span></a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p>]]></content:encoded></item><item><title><![CDATA[Reading EXIF Data with Python and Pillow]]></title><description><![CDATA[Pretty much any smartphone, tablet or digital camera embeds EXIF data into each photograph it takes, with details of the device itself as well as the individual photo.]]></description><link>https://codedrome.substack.com/p/reading-exif-data-with-python-and-pillow</link><guid isPermaLink="false">https://codedrome.substack.com/p/reading-exif-data-with-python-and-pillow</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 12 May 2026 09:52:25 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!qWMc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1ad59e0-b79a-418f-aa35-7e8543afb76f_600x399.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qWMc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1ad59e0-b79a-418f-aa35-7e8543afb76f_600x399.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qWMc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1ad59e0-b79a-418f-aa35-7e8543afb76f_600x399.jpeg 424w, https://substackcdn.com/image/fetch/$s_!qWMc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1ad59e0-b79a-418f-aa35-7e8543afb76f_600x399.jpeg 848w, https://substackcdn.com/image/fetch/$s_!qWMc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1ad59e0-b79a-418f-aa35-7e8543afb76f_600x399.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!qWMc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1ad59e0-b79a-418f-aa35-7e8543afb76f_600x399.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qWMc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1ad59e0-b79a-418f-aa35-7e8543afb76f_600x399.jpeg" width="600" height="399" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f1ad59e0-b79a-418f-aa35-7e8543afb76f_600x399.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:399,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:143495,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/195874828?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1ad59e0-b79a-418f-aa35-7e8543afb76f_600x399.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qWMc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1ad59e0-b79a-418f-aa35-7e8543afb76f_600x399.jpeg 424w, https://substackcdn.com/image/fetch/$s_!qWMc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1ad59e0-b79a-418f-aa35-7e8543afb76f_600x399.jpeg 848w, https://substackcdn.com/image/fetch/$s_!qWMc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1ad59e0-b79a-418f-aa35-7e8543afb76f_600x399.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!qWMc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1ad59e0-b79a-418f-aa35-7e8543afb76f_600x399.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Pretty much any smartphone, tablet or digital camera embeds EXIF data into each photograph it takes, with details of the device itself as well as the individual photo. And just about every piece of software which can display or edit digital images can read and display that data. Or at least attempt to...!</p><p>Unfortunately EXIF is not easy to deal with. It would be nice if the data consisted of a string of XML or JSON so that it could be handled using ubiquitous techniques and libraries, but the format itself is rather quirky, made worse by inconsistent implementations by different manufacturers and sometimes different devices from the same manufacturer.</p><p>You may have noticed that sometimes even popular mainstream software fails to show sensible interpretations of some EXIF data. Despite that I will have a bash at extracting the EXIF data from images using Python and the Pillow library.</p><h2>The Project</h2><p>The <strong>Image</strong> module of the Pillow library for Python includes a method called <strong>_getexif</strong>. This returns a dictionary of EXIF data with the keys being numeric codes and the values being in various formats which are not always human-readable.</p><p>To obtain something suitable for display to users we therefore need to do a bit of work on this data:</p><ul><li><p>Obtain the names corresponding to field codes. Pillow provides most of these in the dictionary <strong>PIL.ExifTags.TAGS</strong></p></li><li><p>Process the data items which are not human readable in some way to make them so</p></li></ul><p>In this project I will write a module with a method called <strong>generate_exif_dict</strong>. This takes a filename argument and returns a dictionary with textual fieldnames as keys and values which have been processed into some format you might want to let users of your software see.</p><p>The solution I have come up with is far from perfect; these are the main imperfections I am aware of:</p><ul><li><p>Not all tags in <strong>PIL.ExifTags.TAGS</strong> may be present in an image&#8217;s EXIF data (<strong>PIL.ExifTags.TAGS</strong> has 268 items but my image only has 50 of these)</p></li><li><p>Not all tags in an image&#8217;s EXIF data may be in <strong>PIL.ExifTags.TAGS</strong></p></li><li><p>Some data may be in proprietary formats not published by manufacturers and therefore of no use to anyone else</p></li><li><p>I have only attempted processing of the more common and useful tags - the more obscure ones have been left as they are</p></li><li><p>The module was developed around photos from cameras I have personally used: Nikon, Olympus, Lumix and Fujifilm. Photos taken on cameras or devices from other manufacturers might give inconsistent results</p></li></ul><h2>Starting to Code</h2><p>This project consists of the following Python files which you can clone/download from the <a href="https://github.com/CodeDrome/PythonPillowEXIF">Github repository</a>. You will also need an image that has EXIF data in it and the repository includes one of mine, <em>DSC_0502.jpg</em>, but by changing the filename in the code you can use one of your own photos.</p><ul><li><p>exif2.py</p></li><li><p>exifdemo.py</p></li></ul><p><em>I wrote an earlier version of this code back in 2020 and the then-current version of Pillow returned some numeric values as pairs of integers representing the numerator and denominator of a fraction. For example an aperture of f8.0 was returned as (80,10) requiring a bit of arithmetic to convert it to something more familiar. The latest version of Pillow does that for us so the code in this article is significantly shorter and simpler but the original module, exif.py, is still in the Github repository.</em></p><p>This is <em>exif2.py</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!I8bY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb10b2df-b486-41ee-a304-30eb24278b25_1660x5064.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!I8bY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb10b2df-b486-41ee-a304-30eb24278b25_1660x5064.png 424w, https://substackcdn.com/image/fetch/$s_!I8bY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb10b2df-b486-41ee-a304-30eb24278b25_1660x5064.png 848w, https://substackcdn.com/image/fetch/$s_!I8bY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb10b2df-b486-41ee-a304-30eb24278b25_1660x5064.png 1272w, https://substackcdn.com/image/fetch/$s_!I8bY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb10b2df-b486-41ee-a304-30eb24278b25_1660x5064.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!I8bY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb10b2df-b486-41ee-a304-30eb24278b25_1660x5064.png" width="1660" height="5064" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fb10b2df-b486-41ee-a304-30eb24278b25_1660x5064.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:5064,&quot;width&quot;:1660,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1097307,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/195874828?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec4a834d-f7be-4ef8-9a0e-d87bab86b411_1660x5064.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!I8bY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb10b2df-b486-41ee-a304-30eb24278b25_1660x5064.png 424w, https://substackcdn.com/image/fetch/$s_!I8bY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb10b2df-b486-41ee-a304-30eb24278b25_1660x5064.png 848w, https://substackcdn.com/image/fetch/$s_!I8bY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb10b2df-b486-41ee-a304-30eb24278b25_1660x5064.png 1272w, https://substackcdn.com/image/fetch/$s_!I8bY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb10b2df-b486-41ee-a304-30eb24278b25_1660x5064.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">caption...</figcaption></figure></div><h3>generate_exif_dict</h3><p>After opening the image with Pillow and grabbing its EXIF data with <strong>_getexif()</strong> we create an empty dictionary. We then iterate <strong>PIL.ExifTags.TAGS</strong> and if the key also exists in the image data we store it in the value variable, otherwise value is set to <strong>None</strong>.</p><p>Some EXIF data can be very long, for example the <strong>MakerNote</strong>, so any data longer than 64 characters - an arbitrary amount - is truncated and an ellipsis added.</p><p>An item is then added to the <strong>exif_data</strong> dictionary, the key being the value from <strong>PIL.ExifTags.TAGS</strong> (ie the name such as Make or Model) and the value being a dictionary containg the tag number, the raw value and the processed value. The processed value is set to the same as the raw value but some will be changed later.</p><p>Finally we pass the completed dictionary to <strong>_process_exif_dict</strong> to overwrite some of the processed values to a more human-friendly format.</p><h3>_create_lookups</h3><p>Some values are encoded as an index, for example MeteringMode can take a value between 0 and 6. The textual equivalents are added to a dictionary as tuples so that we can retrieve them using the encoded values as indexes.</p><p>The first two start at 0. Fine. The second two start at 1. Hmm . . . ! I have padded these with empty strings.</p><h3>_process_exif_dict</h3><p>After creating a date formatting string and a lookups dictionary the following processing is carried out:</p><ul><li><p>The 3 dates are strings so are converted to actual dates, although they look identical when printed</p></li><li><p>FNumber is prefixed with the letter &#8220;f&#8221;</p></li><li><p>MaxApertureValue is processed in a similar way. Additionally zoom lenses with variable apertures may give us many decimal places so the processed value is formatted to just 1dp</p></li><li><p>FocalLength is suffixed with &#8220;mm&#8221;</p></li><li><p>FocalLengthIn35mmFilm is also suffixed with &#8220;mm&#8221;</p></li><li><p>Orientation, ResolutionUnit, ExposureProgram and MeteringMode are all set from the lookup dictionary as described above.</p></li><li><p>ExposureTime (ie. shutter speed) converted to a Fraction object so is displayed as &#8220;[numerator]/[denominator]&#8221;, eg. 1/250.</p></li><li><p>ExposureBiasValue (better known as exposure compensation) is suffixed with &#8220;EV&#8221;, or what I would refer to as stops.</p></li></ul><p>That&#8217;s all the processing for now. At a later date I&#8217;ll revisit the code and add processing for a few more fields. Please feel free to fork the repository and apply your own processing, especially if you are not happy with the results from images taken on your own camera or phone.</p><p>Now let&#8217;s write a short program to use the above module and print the results.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3pWk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72fc0817-1694-427c-983d-8e6c92d66cdd_1660x1288.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3pWk!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72fc0817-1694-427c-983d-8e6c92d66cdd_1660x1288.png 424w, https://substackcdn.com/image/fetch/$s_!3pWk!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72fc0817-1694-427c-983d-8e6c92d66cdd_1660x1288.png 848w, https://substackcdn.com/image/fetch/$s_!3pWk!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72fc0817-1694-427c-983d-8e6c92d66cdd_1660x1288.png 1272w, https://substackcdn.com/image/fetch/$s_!3pWk!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72fc0817-1694-427c-983d-8e6c92d66cdd_1660x1288.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3pWk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72fc0817-1694-427c-983d-8e6c92d66cdd_1660x1288.png" width="1660" height="1288" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/72fc0817-1694-427c-983d-8e6c92d66cdd_1660x1288.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1288,&quot;width&quot;:1660,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:201473,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/195874828?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa94a0ad1-fdff-4182-ac04-9ac7f62e5189_1660x1288.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!3pWk!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72fc0817-1694-427c-983d-8e6c92d66cdd_1660x1288.png 424w, https://substackcdn.com/image/fetch/$s_!3pWk!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72fc0817-1694-427c-983d-8e6c92d66cdd_1660x1288.png 848w, https://substackcdn.com/image/fetch/$s_!3pWk!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72fc0817-1694-427c-983d-8e6c92d66cdd_1660x1288.png 1272w, https://substackcdn.com/image/fetch/$s_!3pWk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72fc0817-1694-427c-983d-8e6c92d66cdd_1660x1288.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This is all very straightforward - we just call <strong>exif2.generate_exif_dict</strong> and pass the result at the <strong>print_exif_dict</strong> which iterates the dictionary, printing each name as a heading and then the tag number, raw value and processed value.</p><h2><strong>Running the Program</strong></h2><p>Now we can run the program with the command below. I am using the full sized version photo at the top of the page but just change the filename if you want to use your own.</p><p><strong>python3 exifdemo.py</strong></p><p>This will give us the following output.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7zLl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827746ce-e89c-4efa-8bf2-76fce65f2174_272x610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7zLl!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827746ce-e89c-4efa-8bf2-76fce65f2174_272x610.png 424w, https://substackcdn.com/image/fetch/$s_!7zLl!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827746ce-e89c-4efa-8bf2-76fce65f2174_272x610.png 848w, https://substackcdn.com/image/fetch/$s_!7zLl!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827746ce-e89c-4efa-8bf2-76fce65f2174_272x610.png 1272w, https://substackcdn.com/image/fetch/$s_!7zLl!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827746ce-e89c-4efa-8bf2-76fce65f2174_272x610.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7zLl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827746ce-e89c-4efa-8bf2-76fce65f2174_272x610.png" width="272" height="610" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/827746ce-e89c-4efa-8bf2-76fce65f2174_272x610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:610,&quot;width&quot;:272,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:36757,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/195874828?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827746ce-e89c-4efa-8bf2-76fce65f2174_272x610.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7zLl!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827746ce-e89c-4efa-8bf2-76fce65f2174_272x610.png 424w, https://substackcdn.com/image/fetch/$s_!7zLl!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827746ce-e89c-4efa-8bf2-76fce65f2174_272x610.png 848w, https://substackcdn.com/image/fetch/$s_!7zLl!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827746ce-e89c-4efa-8bf2-76fce65f2174_272x610.png 1272w, https://substackcdn.com/image/fetch/$s_!7zLl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827746ce-e89c-4efa-8bf2-76fce65f2174_272x610.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This is a truncated version of the output. My photo has 50 pieces of EXIF data but the output shown here is only the top handful. If you use a photograph from another camera manufacturer you will probably see some differences in the fields displayed.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/reading-exif-data-with-python-and-pillow/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/reading-exif-data-with-python-and-pillow/comments"><span>Leave a comment</span></a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p>]]></content:encoded></item><item><title><![CDATA[Image Editing with NumPy Part 2: Histograms]]></title><description><![CDATA[In the first article of this series I showed how to load an image using the Pillow library and copy the pixel data to a NumPy array.]]></description><link>https://codedrome.substack.com/p/image-editing-with-numpy-part-2-histograms</link><guid isPermaLink="false">https://codedrome.substack.com/p/image-editing-with-numpy-part-2-histograms</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 05 May 2026 11:33:51 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!i6GX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36778fb4-0708-47ed-8475-74d6c3cb665b_600x600.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!i6GX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36778fb4-0708-47ed-8475-74d6c3cb665b_600x600.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!i6GX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36778fb4-0708-47ed-8475-74d6c3cb665b_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!i6GX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36778fb4-0708-47ed-8475-74d6c3cb665b_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!i6GX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36778fb4-0708-47ed-8475-74d6c3cb665b_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!i6GX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36778fb4-0708-47ed-8475-74d6c3cb665b_600x600.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!i6GX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36778fb4-0708-47ed-8475-74d6c3cb665b_600x600.jpeg" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/36778fb4-0708-47ed-8475-74d6c3cb665b_600x600.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:116430,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/195768188?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36778fb4-0708-47ed-8475-74d6c3cb665b_600x600.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!i6GX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36778fb4-0708-47ed-8475-74d6c3cb665b_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!i6GX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36778fb4-0708-47ed-8475-74d6c3cb665b_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!i6GX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36778fb4-0708-47ed-8475-74d6c3cb665b_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!i6GX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36778fb4-0708-47ed-8475-74d6c3cb665b_600x600.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In the <a href="https://codedrome.substack.com/p/image-editing-with-numpy-part-1-intro">first article of this series</a> I showed how to load an image using the Pillow library and copy the pixel data to a NumPy array. The data can then be used for any purpose you can think of including editing, in which case it can be copied back to a Pillow image and saved.</p><p>For this second article I won&#8217;t be doing any editing but will use the data to create a set of three histograms showing the distributions of red, green and blue values in an image. If you use any sort of photo editing software you may be familiar with such histograms which are useful for deciding how to adjust colour values. For the moment the histograms I&#8217;m creating here are stand-alone and just pop up in a Matplotlib window but in the long term I&#8217;m planning on including them in a photo editing application which is in the very early stages of development. (Don&#8217;t hold your breath, but I&#8217;ll get there one day!)</p><h2>Image Histograms</h2><p>If you&#8217;re not sure what image histograms represent then this is a brief overview, but if you already understand them just skip to the next section.</p><p>A histogram is a very widely used graphical representation of frequency data, using vertical bars to show the frequency of each value or range of values on the horizontal axis. Histograms are used for a wide range of data from people&#8217;s heights, exam scores and many others. Another common use is for the red, green and blue values of an image. Colour digital image pixels consists of three values specifying the amount of red, green and blue, each somewhere between 0 (none of that colour) and 255 (the maximum amount of that colour). Histograms showing the frequencies of each colour value look something like this.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5bsw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3c32935-2264-4d98-aa52-fc47e53910f7_678x593.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5bsw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3c32935-2264-4d98-aa52-fc47e53910f7_678x593.png 424w, https://substackcdn.com/image/fetch/$s_!5bsw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3c32935-2264-4d98-aa52-fc47e53910f7_678x593.png 848w, https://substackcdn.com/image/fetch/$s_!5bsw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3c32935-2264-4d98-aa52-fc47e53910f7_678x593.png 1272w, https://substackcdn.com/image/fetch/$s_!5bsw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3c32935-2264-4d98-aa52-fc47e53910f7_678x593.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5bsw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3c32935-2264-4d98-aa52-fc47e53910f7_678x593.png" width="678" height="593" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f3c32935-2264-4d98-aa52-fc47e53910f7_678x593.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:593,&quot;width&quot;:678,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:22591,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/195768188?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3c32935-2264-4d98-aa52-fc47e53910f7_678x593.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5bsw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3c32935-2264-4d98-aa52-fc47e53910f7_678x593.png 424w, https://substackcdn.com/image/fetch/$s_!5bsw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3c32935-2264-4d98-aa52-fc47e53910f7_678x593.png 848w, https://substackcdn.com/image/fetch/$s_!5bsw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3c32935-2264-4d98-aa52-fc47e53910f7_678x593.png 1272w, https://substackcdn.com/image/fetch/$s_!5bsw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3c32935-2264-4d98-aa52-fc47e53910f7_678x593.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Above are the actual Matplotlib histograms produced by the code below for the sample image. The horizontal or <em>x-axis</em> covers a range of 0 to 255, the vertical bars showing the relative numbers of pixels in the image for each value. Note that the frequencies have been normalised, ie reduced to a value between 0 and 1, rather than showing the actual frequencies.</p><p>These three histograms are fairly typical in that they tend to be highest across the middle areas while training off towards the ends. Also, the three are of comparable shape which again is typical.</p><p>If you have an image which is too light or too dark, or appears to have colour balance issues, then these histograms can help to diagnose the problem and indicate which of the channels (red, green and blue) need to be adjusted and by how much.</p><h2>The Project</h2><p>This project consists of two Python files:</p><ul><li><p>imagehistograms.py</p></li><li><p>imagehistogramsdemo.py</p></li></ul><p>All files for this series of articles are in the same <a href="https://github.com/CodeDrome/numpy-image-processing">Github repository</a>.</p><p>For demonstration I have used a photo with the prosaic name of <em>001.jpg</em> which is included in the Github repository, but if you are going to try out code you might like to use one of your own photos.</p><p>This is the <em>001.jpg</em> photo which I am using for histograms.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TYzo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdab4ff8a-44f2-47ee-a6e3-be8be8c62787_1080x1080.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TYzo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdab4ff8a-44f2-47ee-a6e3-be8be8c62787_1080x1080.jpeg 424w, https://substackcdn.com/image/fetch/$s_!TYzo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdab4ff8a-44f2-47ee-a6e3-be8be8c62787_1080x1080.jpeg 848w, https://substackcdn.com/image/fetch/$s_!TYzo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdab4ff8a-44f2-47ee-a6e3-be8be8c62787_1080x1080.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!TYzo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdab4ff8a-44f2-47ee-a6e3-be8be8c62787_1080x1080.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TYzo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdab4ff8a-44f2-47ee-a6e3-be8be8c62787_1080x1080.jpeg" width="1080" height="1080" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dab4ff8a-44f2-47ee-a6e3-be8be8c62787_1080x1080.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1080,&quot;width&quot;:1080,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:256717,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/195768188?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdab4ff8a-44f2-47ee-a6e3-be8be8c62787_1080x1080.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!TYzo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdab4ff8a-44f2-47ee-a6e3-be8be8c62787_1080x1080.jpeg 424w, https://substackcdn.com/image/fetch/$s_!TYzo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdab4ff8a-44f2-47ee-a6e3-be8be8c62787_1080x1080.jpeg 848w, https://substackcdn.com/image/fetch/$s_!TYzo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdab4ff8a-44f2-47ee-a6e3-be8be8c62787_1080x1080.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!TYzo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdab4ff8a-44f2-47ee-a6e3-be8be8c62787_1080x1080.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This article isn&#8217;t intended to be a tutorial on Pillow, NumPy or Matplotlib but the code using each of these is very straightforward. If you don&#8217;t have them installed they are all on PyPI and are a quick <em>pip</em> away; these are the links.</p><p><a href="https://pypi.org/project/pillow/">https://pypi.org/project/pillow/</a></p><p><a href="https://pypi.org/project/numpy/">https://pypi.org/project/numpy/</a></p><p><a href="https://pypi.org/project/matplotlib/">https://pypi.org/project/matplotlib/</a></p><h2>The Code</h2><p>This is the <em>imagehistograms.py</em> file.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!d6XB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7818f1d2-803c-43a5-96b3-f50e98b04435_1298x3880.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!d6XB!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7818f1d2-803c-43a5-96b3-f50e98b04435_1298x3880.png 424w, https://substackcdn.com/image/fetch/$s_!d6XB!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7818f1d2-803c-43a5-96b3-f50e98b04435_1298x3880.png 848w, https://substackcdn.com/image/fetch/$s_!d6XB!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7818f1d2-803c-43a5-96b3-f50e98b04435_1298x3880.png 1272w, https://substackcdn.com/image/fetch/$s_!d6XB!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7818f1d2-803c-43a5-96b3-f50e98b04435_1298x3880.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!d6XB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7818f1d2-803c-43a5-96b3-f50e98b04435_1298x3880.png" width="1298" height="3880" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7818f1d2-803c-43a5-96b3-f50e98b04435_1298x3880.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:3880,&quot;width&quot;:1298,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:870851,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/195768188?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff40dea43-e7e7-432c-b1be-dbed9b4bc5ec_1298x3880.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!d6XB!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7818f1d2-803c-43a5-96b3-f50e98b04435_1298x3880.png 424w, https://substackcdn.com/image/fetch/$s_!d6XB!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7818f1d2-803c-43a5-96b3-f50e98b04435_1298x3880.png 848w, https://substackcdn.com/image/fetch/$s_!d6XB!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7818f1d2-803c-43a5-96b3-f50e98b04435_1298x3880.png 1272w, https://substackcdn.com/image/fetch/$s_!d6XB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7818f1d2-803c-43a5-96b3-f50e98b04435_1298x3880.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Let&#8217;s look at the various bits of the file.</p><h2>Imports</h2><p>This project uses the Pillow library (known as PIL for historical reasons), NumPy and Matplotlib which are imported here.</p><h2>create_image_histograms</h2><p>This is the core &#8220;pseudo-public&#8221; function which calls &#8220;private&#8221; functions to create and show histograms for the specified image file. Any exceptions are raised to the calling code.</p><h2>_as_2d</h2><p>To calculate frequencies it&#8217;s far more convenient to have three separate 1-dimensional arrays, one for each of the colour channels. This function firstly splits the array into three separate ones, then transforms them from 2D to 1D. We now have three long arrays containing the colour values for the whole image, which are then returned in a tuple.</p><h2>_get_rgb_frequencies</h2><p>All good things come in threes, and most of what this function does is in triplicate. Firstly the frequencies for each colour channel are calculated using the NumPy bincount method. Next we get the highest value for each channel and then the highest overall frequency across all channels.</p><p>As I mentioned earlier the histograms show normalised frequencies between 0 and 1. These are calculated for each channel by dividing each set of frequencies by the overall highest. Note here that NumPy arrays can be treated as single entities, so doing something like this . . .</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;python&quot;,&quot;nodeId&quot;:&quot;7b11d3c6-5e11-473a-94d7-83d8e88a6f7a&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-python"> red_counts = red_counts / max_count </code></pre></div><p>. . . divides each value in the <strong>red_counts</strong> array by <strong>max_count</strong>, returning a new NumPy array with the calculated values. This abstracts away any iteration into single lines of code, while also being many times faster than a loop written in Python would be. Such is the power of NumPy.</p><h2>_histograms</h2><p>This function creates Matplotlib histograms from the data created by <strong>_get_rgb_frequencies</strong>. The second argument is the filename which is just used as a title.</p><p>The first couple of lines just create limit values used later to ensure all three histograms cover the same x and y ranges irrespective of the actual data. The <strong>possible_values</strong> is also used later to ensure the histogram includes all values between 0 and 255 even if some don&#8217;t appear in the data.</p><p>Next we create a set of 3 plots arranged vertically; note the 3 rows and 1 column. The title is set to the filename, and I have set <strong>tight_layout</strong> which I think looks better although of course this is only a cosmetic tweak.</p><p>The next 6 lines set the x and y limits of each of the three plots. After that the actual histograms are created using the <strong>hist</strong> method. The data in the counts arrays is used for the weights arguments. Finally we call <strong>plt.show()</strong> to display the histograms to the outside world.</p><h2>The Demo Program</h2><p>Trying out the above only requires a few lines of code. You&#8217;ll probably want to edit the filename to use one of your own photos.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!nxFQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f964911-4387-4e70-b010-b883971339f0_1298x776.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!nxFQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f964911-4387-4e70-b010-b883971339f0_1298x776.png 424w, https://substackcdn.com/image/fetch/$s_!nxFQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f964911-4387-4e70-b010-b883971339f0_1298x776.png 848w, https://substackcdn.com/image/fetch/$s_!nxFQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f964911-4387-4e70-b010-b883971339f0_1298x776.png 1272w, https://substackcdn.com/image/fetch/$s_!nxFQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f964911-4387-4e70-b010-b883971339f0_1298x776.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!nxFQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f964911-4387-4e70-b010-b883971339f0_1298x776.png" width="1298" height="776" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7f964911-4387-4e70-b010-b883971339f0_1298x776.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:776,&quot;width&quot;:1298,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:91558,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/195768188?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe75cc28e-2df1-409a-8ea2-6edd85166d8b_1298x776.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!nxFQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f964911-4387-4e70-b010-b883971339f0_1298x776.png 424w, https://substackcdn.com/image/fetch/$s_!nxFQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f964911-4387-4e70-b010-b883971339f0_1298x776.png 848w, https://substackcdn.com/image/fetch/$s_!nxFQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f964911-4387-4e70-b010-b883971339f0_1298x776.png 1272w, https://substackcdn.com/image/fetch/$s_!nxFQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f964911-4387-4e70-b010-b883971339f0_1298x776.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>Running the Program</h2><p>Now let&#8217;s run the program:</p><p>Running the Program</p><p><strong>python3 imagehistogramdemo.py</strong></p><p>This is the screenshot of the Matplot window again. If you&#8217;re not familiar with Matplotlib you might like to play with the buttons and resize the window in the usual way by dragging the edges with your mouse.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0RQM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabe7aace-2fe9-446e-aadc-cd3515366b13_678x593.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0RQM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabe7aace-2fe9-446e-aadc-cd3515366b13_678x593.png 424w, https://substackcdn.com/image/fetch/$s_!0RQM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabe7aace-2fe9-446e-aadc-cd3515366b13_678x593.png 848w, https://substackcdn.com/image/fetch/$s_!0RQM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabe7aace-2fe9-446e-aadc-cd3515366b13_678x593.png 1272w, https://substackcdn.com/image/fetch/$s_!0RQM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabe7aace-2fe9-446e-aadc-cd3515366b13_678x593.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0RQM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabe7aace-2fe9-446e-aadc-cd3515366b13_678x593.png" width="678" height="593" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/abe7aace-2fe9-446e-aadc-cd3515366b13_678x593.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:593,&quot;width&quot;:678,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:22591,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/195768188?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabe7aace-2fe9-446e-aadc-cd3515366b13_678x593.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!0RQM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabe7aace-2fe9-446e-aadc-cd3515366b13_678x593.png 424w, https://substackcdn.com/image/fetch/$s_!0RQM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabe7aace-2fe9-446e-aadc-cd3515366b13_678x593.png 848w, https://substackcdn.com/image/fetch/$s_!0RQM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabe7aace-2fe9-446e-aadc-cd3515366b13_678x593.png 1272w, https://substackcdn.com/image/fetch/$s_!0RQM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fabe7aace-2fe9-446e-aadc-cd3515366b13_678x593.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I have a number of other articles planned on using image data in NumPy so stay tuned. If you have any suggestions for future articles or have used NumPy with image data yourself please let me know.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/image-editing-with-numpy-part-2-histograms/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/image-editing-with-numpy-part-2-histograms/comments"><span>Leave a comment</span></a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p>]]></content:encoded></item><item><title><![CDATA[An Angle Class in Python]]></title><description><![CDATA[In this project I will write a Python class to represent angles in various units, and provide a selection of arithmetic and comparison operators.]]></description><link>https://codedrome.substack.com/p/an-angle-class-in-python</link><guid isPermaLink="false">https://codedrome.substack.com/p/an-angle-class-in-python</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 28 Apr 2026 13:26:49 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!5BRU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28390232-4e43-4e48-80f7-c3932df866ee_600x600.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5BRU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28390232-4e43-4e48-80f7-c3932df866ee_600x600.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5BRU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28390232-4e43-4e48-80f7-c3932df866ee_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!5BRU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28390232-4e43-4e48-80f7-c3932df866ee_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!5BRU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28390232-4e43-4e48-80f7-c3932df866ee_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!5BRU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28390232-4e43-4e48-80f7-c3932df866ee_600x600.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5BRU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28390232-4e43-4e48-80f7-c3932df866ee_600x600.jpeg" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/28390232-4e43-4e48-80f7-c3932df866ee_600x600.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:117380,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191584270?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28390232-4e43-4e48-80f7-c3932df866ee_600x600.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5BRU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28390232-4e43-4e48-80f7-c3932df866ee_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!5BRU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28390232-4e43-4e48-80f7-c3932df866ee_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!5BRU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28390232-4e43-4e48-80f7-c3932df866ee_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!5BRU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28390232-4e43-4e48-80f7-c3932df866ee_600x600.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Image: <a href="https://pixabay.com/photos/architecture-building-geometric-1868547/">Pixabay</a></figcaption></figure></div><p>In this project I will write a Python class to represent angles in various units, and provide a selection of arithmetic and comparison operators.</p><h2>Overview</h2><p>There is a surprising number of different units used to measure angles. Dealing with them in code can be fiddly for a number of reasons so I have written a class to make the process easier.</p><p>The class records not only the angle itself but also the unit you wish to use, for which there is a choice of no less than seven. Whichever unit you choose, the angle value itself is stored in seconds. A second (strictly second of arc or arcsecond) is one sixtieth of a minute (minute of arc or arcminute), which is one sixtieth of a degree. Yes, degrees are sliced up in the same way as hours. The reason for the value being stored in arcseconds is to simplify calculations and comparisons using different instances of the <strong>Angle</strong> class which may use different units.</p><p>In addition to a value and unit the class also provides a selection of arithmetic and comparison operators to simplify using instances in expressions. Due to roundings and slight inaccuracies caused by using <em>pi</em> to calculate radian values, different angles which are intended to be equal might be slightly different, messing up our comparison operators. I have therefore provided a function to test for approximate equality which uses the rather obscure <strong>isclose</strong> method provided by Python&#8217;s <strong>math</strong> module.</p><p>Finally, the <strong>__str__</strong> dunder method is implemented to provide a standard and human-friendly string representation of the value and unit.</p><p>Now let&#8217;s take a brief look at the seven units of angle provide by the Angle class.</p><ul><li><p>Degrees, minutes and seconds - 360 in a circle, each degree is divided into 60 arcminutes, which are divided into 60 arcseconds</p></li><li><p>Radians - see below</p></li><li><p>Gradians - 400 in a circle, therefore 100 in a right angle</p></li><li><p>Turn - 1 in a circle</p></li><li><p>Hour Angle - 24 in a circle</p></li><li><p>Point - one unit of the 32 points of a compass</p></li><li><p>Quadrant - 4 in a circle</p></li></ul><h2>Radians</h2><p>This article includes the use of radians to measure angles. If you are not familiar with radians you might like to read <a href="https://codedrome.substack.com/p/radians-an-exploration-in-python">my article</a> on the topic.</p><h2>The Project</h2><p>This project consists of the following files which you can find in the <a href="https://github.com/CodeDrome/angle_python_class">GitHub repository</a>.</p><ul><li><p>angle.py</p></li><li><p>angledemo.py</p></li></ul><p>The <em>angle.py</em> file contains the Angle class, and <em>angledemo.py</em> contains various functions to show the class in action.</p><h2>The Code</h2><p>This is the full code listing for the Angle class.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CHkX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F695dffae-7b5e-49cf-8238-89c6301859d7_1848x5832.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CHkX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F695dffae-7b5e-49cf-8238-89c6301859d7_1848x5832.png 424w, https://substackcdn.com/image/fetch/$s_!CHkX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F695dffae-7b5e-49cf-8238-89c6301859d7_1848x5832.png 848w, https://substackcdn.com/image/fetch/$s_!CHkX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F695dffae-7b5e-49cf-8238-89c6301859d7_1848x5832.png 1272w, https://substackcdn.com/image/fetch/$s_!CHkX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F695dffae-7b5e-49cf-8238-89c6301859d7_1848x5832.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CHkX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F695dffae-7b5e-49cf-8238-89c6301859d7_1848x5832.png" width="1848" height="5832" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/695dffae-7b5e-49cf-8238-89c6301859d7_1848x5832.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:5832,&quot;width&quot;:1848,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1157759,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191584270?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfe0fe19-c4c8-494b-98b5-601cfa361d28_1848x5832.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CHkX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F695dffae-7b5e-49cf-8238-89c6301859d7_1848x5832.png 424w, https://substackcdn.com/image/fetch/$s_!CHkX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F695dffae-7b5e-49cf-8238-89c6301859d7_1848x5832.png 848w, https://substackcdn.com/image/fetch/$s_!CHkX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F695dffae-7b5e-49cf-8238-89c6301859d7_1848x5832.png 1272w, https://substackcdn.com/image/fetch/$s_!CHkX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F695dffae-7b5e-49cf-8238-89c6301859d7_1848x5832.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>units</h3><p>This consists of a <strong>MappingProxyType</strong> (basically an immutable dictionary) with the unit names as keys and inner dictionaries as values. As well as a name the inner dictionaries contain three functions to convert the value to and from seconds, and provide a human readable representation.</p><p>The logic associated with each unit is therefore kept together in one neat package, and the whole is easily extensible for other units. You might consider this &#8220;design pattern&#8221; to be ingenious, or on the other hand you might find it freakish and bizarre!</p><h3>__init__</h3><p>This is pretty straightforward, just setting the variables for the value and unit. Note though that the value is converted to seconds using the method associated with the relevant unit.</p><h3>value Property</h3><p>As values are stored as seconds the value getter converts seconds to the actual unit before returning it. Similarly, the setter converts units to seconds.</p><h3>__str__</h3><p>Here we convert the value from seconds, and then call the unit&#8217;s tostring method.</p><h3>approx_equal</h3><p>As I mentioned above small inaccuracies can creep into the seconds value used to store all values. This method allows the values of two instances of the <strong>Angle</strong> class to be compared for equality even if they are slightly different. It uses the <strong>math.isclose</strong> function provided for us by Python.</p><p>The <strong>isclose</strong> function can take optional <strong>rel_tol</strong> (relative tolerance) or <strong>abs_tol</strong> (absolute tolerance) arguments although here I use the default, <strong>rel_tol=1e-09</strong>, which the Python documentation states <em>&#8220;assures that the two values are the same within about 9 decimal digits&#8221;</em>.</p><p>It&#8217;s worth taking a minute to read the <strong>math.isclose</strong> function&#8217;s documentation.</p><p><a href="https://docs.python.org/3/library/math.html#math.isclose">https://docs.python.org/3/library/math.html#math.isclose</a></p><h3>Arithmetic Operators</h3><p>These implement the various arithmetic dunder methods. The actual arithmetic is carried out on the seconds values so that we can carry out calculations with angles of different units. Then the value in the unit of the first angle is calculated, and a new <strong>Angle</strong> instance returned, again with the unit of the first angle.</p><h3>Comparison Operators</h3><p>These are all straightforward one-line implementations, again using seconds to enable angles of different units to be compared.</p><h2>Let&#8217;s Try It Out . . .</h2><p>The Angle class is now complete so we need some bits of code to try it out.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!DwkF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e6887b6-24f1-4ff1-a13b-cb3a2308b489_1848x5544.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DwkF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e6887b6-24f1-4ff1-a13b-cb3a2308b489_1848x5544.png 424w, https://substackcdn.com/image/fetch/$s_!DwkF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e6887b6-24f1-4ff1-a13b-cb3a2308b489_1848x5544.png 848w, https://substackcdn.com/image/fetch/$s_!DwkF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e6887b6-24f1-4ff1-a13b-cb3a2308b489_1848x5544.png 1272w, https://substackcdn.com/image/fetch/$s_!DwkF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e6887b6-24f1-4ff1-a13b-cb3a2308b489_1848x5544.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DwkF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e6887b6-24f1-4ff1-a13b-cb3a2308b489_1848x5544.png" width="1848" height="5544" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2e6887b6-24f1-4ff1-a13b-cb3a2308b489_1848x5544.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:5544,&quot;width&quot;:1848,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1191553,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191584270?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0fe4356-e7e2-4a47-bd05-4115eccb5b1d_1848x5544.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!DwkF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e6887b6-24f1-4ff1-a13b-cb3a2308b489_1848x5544.png 424w, https://substackcdn.com/image/fetch/$s_!DwkF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e6887b6-24f1-4ff1-a13b-cb3a2308b489_1848x5544.png 848w, https://substackcdn.com/image/fetch/$s_!DwkF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e6887b6-24f1-4ff1-a13b-cb3a2308b489_1848x5544.png 1272w, https://substackcdn.com/image/fetch/$s_!DwkF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e6887b6-24f1-4ff1-a13b-cb3a2308b489_1848x5544.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>The main Function</h3><p>This just has six calls to functions demoing the various aspects of the Angle class. We can comment/uncomment them and run one at a time.</p><h3>The create_and_output Function</h3><p>For each of the seven units an Angle instance is created, and the unit name, seconds value, unit value and string value are printed.</p><p>For the <strong>degreeminutesecond</strong> unit a tuple is passed to <strong>__init__</strong>, the other units just needing a single value.</p><p>Run the code like this:</p><p><strong>python3 angledemo.py</strong></p><p>This is the output of the <strong>create_and_output</strong> function.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!NRDf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0dab98a-300a-446c-8bc6-db86b43bc53a_283x588.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NRDf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0dab98a-300a-446c-8bc6-db86b43bc53a_283x588.png 424w, https://substackcdn.com/image/fetch/$s_!NRDf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0dab98a-300a-446c-8bc6-db86b43bc53a_283x588.png 848w, https://substackcdn.com/image/fetch/$s_!NRDf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0dab98a-300a-446c-8bc6-db86b43bc53a_283x588.png 1272w, https://substackcdn.com/image/fetch/$s_!NRDf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0dab98a-300a-446c-8bc6-db86b43bc53a_283x588.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!NRDf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0dab98a-300a-446c-8bc6-db86b43bc53a_283x588.png" width="283" height="588" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b0dab98a-300a-446c-8bc6-db86b43bc53a_283x588.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:588,&quot;width&quot;:283,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:40161,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191584270?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0dab98a-300a-446c-8bc6-db86b43bc53a_283x588.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!NRDf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0dab98a-300a-446c-8bc6-db86b43bc53a_283x588.png 424w, https://substackcdn.com/image/fetch/$s_!NRDf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0dab98a-300a-446c-8bc6-db86b43bc53a_283x588.png 848w, https://substackcdn.com/image/fetch/$s_!NRDf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0dab98a-300a-446c-8bc6-db86b43bc53a_283x588.png 1272w, https://substackcdn.com/image/fetch/$s_!NRDf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0dab98a-300a-446c-8bc6-db86b43bc53a_283x588.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>The set_value Function</h3><p>This is to demonstrate that value can be set, but the new value needs to be in the <strong>Angle</strong> instance&#8217;s unit, in this case a tuple of degrees, minutes and seconds.</p><p>Uncomment <strong>set_value()</strong> in <strong>main</strong> and run the program. This is the output.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!OO99!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60aa95b0-58a8-4b59-9363-e1cc198b04d4_97x38.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!OO99!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60aa95b0-58a8-4b59-9363-e1cc198b04d4_97x38.png 424w, https://substackcdn.com/image/fetch/$s_!OO99!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60aa95b0-58a8-4b59-9363-e1cc198b04d4_97x38.png 848w, https://substackcdn.com/image/fetch/$s_!OO99!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60aa95b0-58a8-4b59-9363-e1cc198b04d4_97x38.png 1272w, https://substackcdn.com/image/fetch/$s_!OO99!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60aa95b0-58a8-4b59-9363-e1cc198b04d4_97x38.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!OO99!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60aa95b0-58a8-4b59-9363-e1cc198b04d4_97x38.png" width="97" height="38" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/60aa95b0-58a8-4b59-9363-e1cc198b04d4_97x38.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:38,&quot;width&quot;:97,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:6147,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191584270?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60aa95b0-58a8-4b59-9363-e1cc198b04d4_97x38.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!OO99!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60aa95b0-58a8-4b59-9363-e1cc198b04d4_97x38.png 424w, https://substackcdn.com/image/fetch/$s_!OO99!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60aa95b0-58a8-4b59-9363-e1cc198b04d4_97x38.png 848w, https://substackcdn.com/image/fetch/$s_!OO99!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60aa95b0-58a8-4b59-9363-e1cc198b04d4_97x38.png 1272w, https://substackcdn.com/image/fetch/$s_!OO99!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60aa95b0-58a8-4b59-9363-e1cc198b04d4_97x38.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3>The arithmetic Function</h3><p>Firstly we create two <strong>Angle</strong> objects, one with a unit of <strong>degreeminutesecond</strong> and the other with <strong>quadrant</strong>. This is to show that it is possible to mix up units.</p><p>Next five new <strong>Angle</strong> objects are created, each the result of a calculation using the first two angles. There are two multiplications, <strong>dms * 2</strong> which uses <strong>__mul__</strong>, and <strong>3 * dms</strong> which uses <strong>__rmul__</strong>.</p><p>We then print the original two angles followed by the various results. Uncomment <strong>arithmetic()</strong> in <strong>main</strong> and run the program again.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!d91X!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c5051df-926f-4daf-baf4-a1f959c9b5fe_258x145.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!d91X!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c5051df-926f-4daf-baf4-a1f959c9b5fe_258x145.png 424w, https://substackcdn.com/image/fetch/$s_!d91X!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c5051df-926f-4daf-baf4-a1f959c9b5fe_258x145.png 848w, https://substackcdn.com/image/fetch/$s_!d91X!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c5051df-926f-4daf-baf4-a1f959c9b5fe_258x145.png 1272w, https://substackcdn.com/image/fetch/$s_!d91X!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c5051df-926f-4daf-baf4-a1f959c9b5fe_258x145.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!d91X!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c5051df-926f-4daf-baf4-a1f959c9b5fe_258x145.png" width="258" height="145" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9c5051df-926f-4daf-baf4-a1f959c9b5fe_258x145.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:145,&quot;width&quot;:258,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:13379,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191584270?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c5051df-926f-4daf-baf4-a1f959c9b5fe_258x145.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!d91X!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c5051df-926f-4daf-baf4-a1f959c9b5fe_258x145.png 424w, https://substackcdn.com/image/fetch/$s_!d91X!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c5051df-926f-4daf-baf4-a1f959c9b5fe_258x145.png 848w, https://substackcdn.com/image/fetch/$s_!d91X!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c5051df-926f-4daf-baf4-a1f959c9b5fe_258x145.png 1272w, https://substackcdn.com/image/fetch/$s_!d91X!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9c5051df-926f-4daf-baf4-a1f959c9b5fe_258x145.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3>The comparisons Function</h3><p>Three <strong>Angle</strong> objects are created and then used with the six comparison operators within print functions. As with the arithmetic function the angles use different units just to show that we can.</p><p>This is the output from the comparisons Function.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BJV-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F346e40cc-b274-411a-bf61-816a3193b7a6_243x108.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BJV-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F346e40cc-b274-411a-bf61-816a3193b7a6_243x108.png 424w, https://substackcdn.com/image/fetch/$s_!BJV-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F346e40cc-b274-411a-bf61-816a3193b7a6_243x108.png 848w, https://substackcdn.com/image/fetch/$s_!BJV-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F346e40cc-b274-411a-bf61-816a3193b7a6_243x108.png 1272w, https://substackcdn.com/image/fetch/$s_!BJV-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F346e40cc-b274-411a-bf61-816a3193b7a6_243x108.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BJV-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F346e40cc-b274-411a-bf61-816a3193b7a6_243x108.png" width="243" height="108" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/346e40cc-b274-411a-bf61-816a3193b7a6_243x108.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:108,&quot;width&quot;:243,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:9795,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191584270?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F346e40cc-b274-411a-bf61-816a3193b7a6_243x108.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BJV-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F346e40cc-b274-411a-bf61-816a3193b7a6_243x108.png 424w, https://substackcdn.com/image/fetch/$s_!BJV-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F346e40cc-b274-411a-bf61-816a3193b7a6_243x108.png 848w, https://substackcdn.com/image/fetch/$s_!BJV-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F346e40cc-b274-411a-bf61-816a3193b7a6_243x108.png 1272w, https://substackcdn.com/image/fetch/$s_!BJV-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F346e40cc-b274-411a-bf61-816a3193b7a6_243x108.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3>The approx_equal Function</h3><p>Firstly I created two <strong>Angle</strong> objects, the first for 360 degrees and the second for 2&#960; radians, the two being equivalent. If you sneak at the output below you&#8217;ll see that the equality operator returns true as both values resolve to the same value in seconds.</p><p>However, dividing each by 3 introduces a tiny difference between the seconds values so the <strong>==</strong> operator returns false, but <strong>approx_equal</strong> returns true as the two values are within the tolerances.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!E6Eu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18feb818-1c34-4ad1-a4d9-3817e2d044d5_331x144.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!E6Eu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18feb818-1c34-4ad1-a4d9-3817e2d044d5_331x144.png 424w, https://substackcdn.com/image/fetch/$s_!E6Eu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18feb818-1c34-4ad1-a4d9-3817e2d044d5_331x144.png 848w, https://substackcdn.com/image/fetch/$s_!E6Eu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18feb818-1c34-4ad1-a4d9-3817e2d044d5_331x144.png 1272w, https://substackcdn.com/image/fetch/$s_!E6Eu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18feb818-1c34-4ad1-a4d9-3817e2d044d5_331x144.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!E6Eu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18feb818-1c34-4ad1-a4d9-3817e2d044d5_331x144.png" width="331" height="144" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/18feb818-1c34-4ad1-a4d9-3817e2d044d5_331x144.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:144,&quot;width&quot;:331,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:14206,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191584270?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18feb818-1c34-4ad1-a4d9-3817e2d044d5_331x144.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!E6Eu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18feb818-1c34-4ad1-a4d9-3817e2d044d5_331x144.png 424w, https://substackcdn.com/image/fetch/$s_!E6Eu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18feb818-1c34-4ad1-a4d9-3817e2d044d5_331x144.png 848w, https://substackcdn.com/image/fetch/$s_!E6Eu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18feb818-1c34-4ad1-a4d9-3817e2d044d5_331x144.png 1272w, https://substackcdn.com/image/fetch/$s_!E6Eu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18feb818-1c34-4ad1-a4d9-3817e2d044d5_331x144.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3>The convert_units Function</h3><p>Changing the unit of an <strong>Angle</strong> object is easy - just do it. As the value is stored as seconds changing the unit has no effect on it, but in future output will of course be shown in the new unit. In the <strong>convert_units</strong> function we start of with a <strong>degreeminutesecond</strong> angle, then convert it to <strong>quadrant</strong>, and finally to <strong>gradian</strong>. As you can see from the output below the value is retained and output correctly.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ddtD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefabe9ba-6f1e-4211-bc53-90ef37db8830_154x145.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ddtD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefabe9ba-6f1e-4211-bc53-90ef37db8830_154x145.png 424w, https://substackcdn.com/image/fetch/$s_!ddtD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefabe9ba-6f1e-4211-bc53-90ef37db8830_154x145.png 848w, https://substackcdn.com/image/fetch/$s_!ddtD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefabe9ba-6f1e-4211-bc53-90ef37db8830_154x145.png 1272w, https://substackcdn.com/image/fetch/$s_!ddtD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefabe9ba-6f1e-4211-bc53-90ef37db8830_154x145.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ddtD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefabe9ba-6f1e-4211-bc53-90ef37db8830_154x145.png" width="154" height="145" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/efabe9ba-6f1e-4211-bc53-90ef37db8830_154x145.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:145,&quot;width&quot;:154,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:8981,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191584270?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefabe9ba-6f1e-4211-bc53-90ef37db8830_154x145.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ddtD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefabe9ba-6f1e-4211-bc53-90ef37db8830_154x145.png 424w, https://substackcdn.com/image/fetch/$s_!ddtD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefabe9ba-6f1e-4211-bc53-90ef37db8830_154x145.png 848w, https://substackcdn.com/image/fetch/$s_!ddtD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefabe9ba-6f1e-4211-bc53-90ef37db8830_154x145.png 1272w, https://substackcdn.com/image/fetch/$s_!ddtD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefabe9ba-6f1e-4211-bc53-90ef37db8830_154x145.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>I hope you found this article interesting and useful. Even if you have no need for the <strong>Angle</strong> class a few of the language features such as arithmetic and comparison operators, and the <strong>math.isclose</strong> function, might be usful for other projects.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/an-angle-class-in-python/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/an-angle-class-in-python/comments"><span>Leave a comment</span></a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p><p></p>]]></content:encoded></item><item><title><![CDATA[Frequency Analysis in Python]]></title><description><![CDATA[In a previous post I implemented a very simple and very insecure substitution cypher.]]></description><link>https://codedrome.substack.com/p/frequency-analysis-in-python</link><guid isPermaLink="false">https://codedrome.substack.com/p/frequency-analysis-in-python</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 21 Apr 2026 14:04:32 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!639f!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53c8b57-a263-4a39-8754-889bc99b1e83_600x600.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!639f!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53c8b57-a263-4a39-8754-889bc99b1e83_600x600.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!639f!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53c8b57-a263-4a39-8754-889bc99b1e83_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!639f!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53c8b57-a263-4a39-8754-889bc99b1e83_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!639f!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53c8b57-a263-4a39-8754-889bc99b1e83_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!639f!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53c8b57-a263-4a39-8754-889bc99b1e83_600x600.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!639f!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53c8b57-a263-4a39-8754-889bc99b1e83_600x600.jpeg" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d53c8b57-a263-4a39-8754-889bc99b1e83_600x600.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:140670,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191587651?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53c8b57-a263-4a39-8754-889bc99b1e83_600x600.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!639f!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53c8b57-a263-4a39-8754-889bc99b1e83_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!639f!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53c8b57-a263-4a39-8754-889bc99b1e83_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!639f!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53c8b57-a263-4a39-8754-889bc99b1e83_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!639f!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53c8b57-a263-4a39-8754-889bc99b1e83_600x600.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In a <a href="https://codedrome.substack.com/p/the-caesar-shift-cypher-in-python">previous post</a> I implemented a very simple and very insecure substitution cypher. It is insecure because each letter in the original text is always encrypted the same way, for example the most common letter &#8220;<em>e</em>&#8221; might always be encrypted as &#8220;<em>h</em>&#8221;, so if we find that &#8220;<em>h</em>&#8221; is the most common letter in the encrypted text then we can assume it represents &#8220;<em>e</em>&#8221;. This can be carried out for all letters, a process called frequency analysis which is the subject of this post.</p><p>The Caesar Shift Cypher implemented in the previous post uses a one-to-one mapping between plaintext characters and encrypted characters by shifting along the alphabet by a fixed amount (rolling round to &#8220;a&#8221; if you get to &#8220;z&#8221;). If you know or believe that such a cypher has been used you only need to decrypt one letter and you can work out the shift and therefore crack the entire code. Frequency analysis works with a shift cypher but also any substitution cypher where each letter always encrypts to just one other letter, so can be used on more random cyphers.</p><p>The frequency analysis process consists of the following steps:</p><ul><li><p>Create a list of plaintext frequencies, sorted by frequency</p></li><li><p>Create a list of encrypted text frequencies, sorted by frequency</p></li><li><p>Merge these lists to create an estimated mapping between encrypted letters and plaintext letters</p></li><li><p>Use this mapping to create an approximate decryption of the encrypted text</p></li></ul><p>I use the words &#8220;estimated&#8221; and &#8220;approximate&#8221; because there is no guarantee the relative letter frequencies in the plaintext original of the encrypted text will match those of the sample text used to create a decryption mapping. However, given texts of reasonable length it is probable that the majority of frequencies will be in the same order, and those that are not will often be just one or two places out enabling us to manually edit our mappings to get a better result.</p><h2>The Code</h2><p>This project consists of two Python files, and also a couple of text files which live in this <a href="https://github.com/CodeDrome/frequency-analysis-python">Github repository</a>.</p><ul><li><p>frequencyanalysis.py</p></li><li><p>fademo.py</p></li><li><p>the_sign_of_four_plaintext.txt</p></li><li><p>encrypted.txt</p></li></ul><p>The first text file is the whole of the Sherlock Holmes novel The Sign of Four and will be used to create a list of letter frequencies which will then be used to attempt a decryption of <em>encrypted.txt</em>.</p><p>This is the source code of <em>frequencyanalysis.py</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!DPnq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d03a96c-25ad-41d9-a264-4b5089f048c1_1848x2888.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DPnq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d03a96c-25ad-41d9-a264-4b5089f048c1_1848x2888.png 424w, https://substackcdn.com/image/fetch/$s_!DPnq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d03a96c-25ad-41d9-a264-4b5089f048c1_1848x2888.png 848w, https://substackcdn.com/image/fetch/$s_!DPnq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d03a96c-25ad-41d9-a264-4b5089f048c1_1848x2888.png 1272w, https://substackcdn.com/image/fetch/$s_!DPnq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d03a96c-25ad-41d9-a264-4b5089f048c1_1848x2888.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DPnq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d03a96c-25ad-41d9-a264-4b5089f048c1_1848x2888.png" width="1848" height="2888" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0d03a96c-25ad-41d9-a264-4b5089f048c1_1848x2888.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2888,&quot;width&quot;:1848,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:666365,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191587651?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4d7e41be-38c7-4482-8b95-805968c5336f_1848x2888.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!DPnq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d03a96c-25ad-41d9-a264-4b5089f048c1_1848x2888.png 424w, https://substackcdn.com/image/fetch/$s_!DPnq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d03a96c-25ad-41d9-a264-4b5089f048c1_1848x2888.png 848w, https://substackcdn.com/image/fetch/$s_!DPnq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d03a96c-25ad-41d9-a264-4b5089f048c1_1848x2888.png 1272w, https://substackcdn.com/image/fetch/$s_!DPnq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d03a96c-25ad-41d9-a264-4b5089f048c1_1848x2888.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>create_decryption_dictionary</h3><p>Firstly we read the contents of the two files using the <strong>_readfile</strong> function, and then call <strong>_count_letter_frequencies</strong> with each in turn to get dictionaries of the letter frequencies. The dictionaries come to us ready-sorted by frequency which is crucial.</p><p>We then create a new dictionary and, within a loop, add key/value pairs with the encrypted letters as keys and plaintext letters as values. We don&#8217;t add the actual frequencies as they are irrelevant; we are just interested in the frequency orders.</p><p>Finally the mappings dictionary is saved to the specified JSON file.</p><h3>decrypt_file</h3><p>This function uses the mappings dictionary in the specified dictionary file to decrypt the encrypted file and save it to the specified path.</p><p>Firstly we read the encrypted text into memory, and then the mapping dictionary. Then we create an empty list to hold the individual letters of the decrypted text.</p><p>Next we iterate the letters of the encrypted text, getting the ASCII code of the uppercase version of each letter. If it is an actual letter we use it as a dictionary key to get the corresponding decrypted letter which is added to <strong>decrypted_list</strong>.</p><p>Finally we use join to stick all the letters in <strong>decrypted_list</strong> together into a single string. This is far more efficient than appending letters one by one to a string. Finally we just need to save the decrypted text to a file.</p><h3>_count_letter_frequencies</h3><p>This function takes a string and creates a dictionary using the 26 letters of the alphabet as keys and their frequencies as values, sorted by frequency.</p><p>After creating an empty dictionary we add the keys to it with frequencies of 0. Next we iterate the text, incrementing the frequencies by 1 for each letter. Finally the dictionary is sorted in descending order and returned.</p><h3>_readfile</h3><p>Text files are read three times in the code above so I included a short utility function to simplify things very slightly - it just opens, reads and closes the file before returning the contents. There is no exception handling in any of the above code as this is done by the calling code.</p><p>That&#8217;s the module finished so we just need a few lines of code to test it. This is <em>fademo.py</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rOJI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38ecb27e-351c-4b5c-b788-cafa8796a598_1848x1128.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rOJI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38ecb27e-351c-4b5c-b788-cafa8796a598_1848x1128.png 424w, https://substackcdn.com/image/fetch/$s_!rOJI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38ecb27e-351c-4b5c-b788-cafa8796a598_1848x1128.png 848w, https://substackcdn.com/image/fetch/$s_!rOJI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38ecb27e-351c-4b5c-b788-cafa8796a598_1848x1128.png 1272w, https://substackcdn.com/image/fetch/$s_!rOJI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38ecb27e-351c-4b5c-b788-cafa8796a598_1848x1128.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rOJI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38ecb27e-351c-4b5c-b788-cafa8796a598_1848x1128.png" width="1848" height="1128" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/38ecb27e-351c-4b5c-b788-cafa8796a598_1848x1128.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1128,&quot;width&quot;:1848,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:178708,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191587651?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F242d2e29-1894-4049-b15c-d8bea00fd944_1848x1128.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rOJI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38ecb27e-351c-4b5c-b788-cafa8796a598_1848x1128.png 424w, https://substackcdn.com/image/fetch/$s_!rOJI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38ecb27e-351c-4b5c-b788-cafa8796a598_1848x1128.png 848w, https://substackcdn.com/image/fetch/$s_!rOJI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38ecb27e-351c-4b5c-b788-cafa8796a598_1848x1128.png 1272w, https://substackcdn.com/image/fetch/$s_!rOJI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F38ecb27e-351c-4b5c-b788-cafa8796a598_1848x1128.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Ignoring the heading and exception handling there are only two lines of code in main, calling the two &#8220;public&#8221; functions in <em>frequencyanalysis.py</em>. Firstly we call <strong>create_decryption_dictionary</strong> to create the dictionary and save it as JSON. Then we call <strong>decrypt_file</strong> to generate <em>decrypted.txt</em>.</p><p>The code is finished so run it with this command:</p><p><strong>python3 fademo.py</strong></p><p>If you open the folder where your files are you&#8217;ll find a new file called <em>decrypted.txt</em> which starts like this.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Kgi5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b7f8aa4-0d0f-47b7-abd6-dfbea034351a_937x297.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Kgi5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b7f8aa4-0d0f-47b7-abd6-dfbea034351a_937x297.png 424w, https://substackcdn.com/image/fetch/$s_!Kgi5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b7f8aa4-0d0f-47b7-abd6-dfbea034351a_937x297.png 848w, https://substackcdn.com/image/fetch/$s_!Kgi5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b7f8aa4-0d0f-47b7-abd6-dfbea034351a_937x297.png 1272w, https://substackcdn.com/image/fetch/$s_!Kgi5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b7f8aa4-0d0f-47b7-abd6-dfbea034351a_937x297.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Kgi5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b7f8aa4-0d0f-47b7-abd6-dfbea034351a_937x297.png" width="937" height="297" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9b7f8aa4-0d0f-47b7-abd6-dfbea034351a_937x297.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:297,&quot;width&quot;:937,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:108170,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191587651?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b7f8aa4-0d0f-47b7-abd6-dfbea034351a_937x297.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Kgi5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b7f8aa4-0d0f-47b7-abd6-dfbea034351a_937x297.png 424w, https://substackcdn.com/image/fetch/$s_!Kgi5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b7f8aa4-0d0f-47b7-abd6-dfbea034351a_937x297.png 848w, https://substackcdn.com/image/fetch/$s_!Kgi5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b7f8aa4-0d0f-47b7-abd6-dfbea034351a_937x297.png 1272w, https://substackcdn.com/image/fetch/$s_!Kgi5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b7f8aa4-0d0f-47b7-abd6-dfbea034351a_937x297.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>It doesn&#8217;t look very decrypted but if we think the encrypted text is a book then it is probable that it starts with the word &#8220;chapter&#8221;. It actually starts with &#8220;msapter&#8221; which is promising as only the first two letters are different.</p><p>Open <em>decryption_dict.json</em> and swap the following values. (Make sure you swap the lower case values, not the upper case keys.)</p><ul><li><p>swap m and c</p></li><li><p>swap s and h</p></li></ul><p>Comment out the call to <strong>frequencyanalysis.create_decryption_dictionary</strong> so we don&#8217;t overwrite the edited dictionary but use the one we just edited, and then run the program again. This is the new <em>decrypted.txt</em> file.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PjOG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd13d99de-6e7e-4da9-9cce-84edbb6dbadc_937x295.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PjOG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd13d99de-6e7e-4da9-9cce-84edbb6dbadc_937x295.png 424w, https://substackcdn.com/image/fetch/$s_!PjOG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd13d99de-6e7e-4da9-9cce-84edbb6dbadc_937x295.png 848w, https://substackcdn.com/image/fetch/$s_!PjOG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd13d99de-6e7e-4da9-9cce-84edbb6dbadc_937x295.png 1272w, https://substackcdn.com/image/fetch/$s_!PjOG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd13d99de-6e7e-4da9-9cce-84edbb6dbadc_937x295.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PjOG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd13d99de-6e7e-4da9-9cce-84edbb6dbadc_937x295.png" width="937" height="295" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d13d99de-6e7e-4da9-9cce-84edbb6dbadc_937x295.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:295,&quot;width&quot;:937,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:107942,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191587651?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd13d99de-6e7e-4da9-9cce-84edbb6dbadc_937x295.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!PjOG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd13d99de-6e7e-4da9-9cce-84edbb6dbadc_937x295.png 424w, https://substackcdn.com/image/fetch/$s_!PjOG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd13d99de-6e7e-4da9-9cce-84edbb6dbadc_937x295.png 848w, https://substackcdn.com/image/fetch/$s_!PjOG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd13d99de-6e7e-4da9-9cce-84edbb6dbadc_937x295.png 1272w, https://substackcdn.com/image/fetch/$s_!PjOG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd13d99de-6e7e-4da9-9cce-84edbb6dbadc_937x295.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The text now starts with the word &#8220;chapter&#8221;, followed by &#8220;ursherdockhoduesursherdockhodues&#8221;. (The character after the word &#8220;chapter&#8221; would usually be a number and therefore not encrypted by our simple cypher unless spelled out in words.) If we think the text might be about a character called &#8220;Mr Sherlock Holmes&#8221; we can easily identify which letters need to be swapped in the decryption dictionary:</p><ul><li><p>u and m</p></li><li><p>d and l</p></li></ul><p>Make these changes in the JSON file and run the program. This is the third version of the decrypted text.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!mNXj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F24170828-81eb-48f1-bab0-3aafa7018b22_937x296.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!mNXj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F24170828-81eb-48f1-bab0-3aafa7018b22_937x296.png 424w, https://substackcdn.com/image/fetch/$s_!mNXj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F24170828-81eb-48f1-bab0-3aafa7018b22_937x296.png 848w, https://substackcdn.com/image/fetch/$s_!mNXj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F24170828-81eb-48f1-bab0-3aafa7018b22_937x296.png 1272w, https://substackcdn.com/image/fetch/$s_!mNXj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F24170828-81eb-48f1-bab0-3aafa7018b22_937x296.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!mNXj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F24170828-81eb-48f1-bab0-3aafa7018b22_937x296.png" width="937" height="296" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/24170828-81eb-48f1-bab0-3aafa7018b22_937x296.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:296,&quot;width&quot;:937,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:108459,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191587651?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F24170828-81eb-48f1-bab0-3aafa7018b22_937x296.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!mNXj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F24170828-81eb-48f1-bab0-3aafa7018b22_937x296.png 424w, https://substackcdn.com/image/fetch/$s_!mNXj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F24170828-81eb-48f1-bab0-3aafa7018b22_937x296.png 848w, https://substackcdn.com/image/fetch/$s_!mNXj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F24170828-81eb-48f1-bab0-3aafa7018b22_937x296.png 1272w, https://substackcdn.com/image/fetch/$s_!mNXj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F24170828-81eb-48f1-bab0-3aafa7018b22_937x296.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We now have what can be interpreted as a chapter number, a chapter title and the first three words of the chapter itself. After that comes &#8220;yhoyaswswalluverulate&#8221; which with a bit of thought could be &#8220;whowasusuallyverylate&#8221; so make the following swap in the JSON file and run the program.</p><ul><li><p>y and w</p></li></ul><p>This is the next version of <em>decrypted.txt</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sKb1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb81aa05a-4559-4f9d-937a-2c86ffa07197_936x297.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sKb1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb81aa05a-4559-4f9d-937a-2c86ffa07197_936x297.png 424w, https://substackcdn.com/image/fetch/$s_!sKb1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb81aa05a-4559-4f9d-937a-2c86ffa07197_936x297.png 848w, https://substackcdn.com/image/fetch/$s_!sKb1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb81aa05a-4559-4f9d-937a-2c86ffa07197_936x297.png 1272w, https://substackcdn.com/image/fetch/$s_!sKb1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb81aa05a-4559-4f9d-937a-2c86ffa07197_936x297.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sKb1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb81aa05a-4559-4f9d-937a-2c86ffa07197_936x297.png" width="936" height="297" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b81aa05a-4559-4f9d-937a-2c86ffa07197_936x297.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:297,&quot;width&quot;:936,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:108469,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191587651?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb81aa05a-4559-4f9d-937a-2c86ffa07197_936x297.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!sKb1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb81aa05a-4559-4f9d-937a-2c86ffa07197_936x297.png 424w, https://substackcdn.com/image/fetch/$s_!sKb1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb81aa05a-4559-4f9d-937a-2c86ffa07197_936x297.png 848w, https://substackcdn.com/image/fetch/$s_!sKb1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb81aa05a-4559-4f9d-937a-2c86ffa07197_936x297.png 1272w, https://substackcdn.com/image/fetch/$s_!sKb1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb81aa05a-4559-4f9d-937a-2c86ffa07197_936x297.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We nearly have &#8220;whowasusuallyverylate&#8221; so make this final edit to the dictionary...</p><ul><li><p>y and u</p></li></ul><p>...and run the program again. This is the fifth version of <em>decrypted.txt</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!c3PJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6187535d-4060-47d8-8bce-928a43d3bd98_938x298.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!c3PJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6187535d-4060-47d8-8bce-928a43d3bd98_938x298.png 424w, https://substackcdn.com/image/fetch/$s_!c3PJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6187535d-4060-47d8-8bce-928a43d3bd98_938x298.png 848w, https://substackcdn.com/image/fetch/$s_!c3PJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6187535d-4060-47d8-8bce-928a43d3bd98_938x298.png 1272w, https://substackcdn.com/image/fetch/$s_!c3PJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6187535d-4060-47d8-8bce-928a43d3bd98_938x298.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!c3PJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6187535d-4060-47d8-8bce-928a43d3bd98_938x298.png" width="938" height="298" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6187535d-4060-47d8-8bce-928a43d3bd98_938x298.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:298,&quot;width&quot;:938,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:107769,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191587651?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6187535d-4060-47d8-8bce-928a43d3bd98_938x298.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!c3PJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6187535d-4060-47d8-8bce-928a43d3bd98_938x298.png 424w, https://substackcdn.com/image/fetch/$s_!c3PJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6187535d-4060-47d8-8bce-928a43d3bd98_938x298.png 848w, https://substackcdn.com/image/fetch/$s_!c3PJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6187535d-4060-47d8-8bce-928a43d3bd98_938x298.png 1272w, https://substackcdn.com/image/fetch/$s_!c3PJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6187535d-4060-47d8-8bce-928a43d3bd98_938x298.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>It&#8217;s not quite perfect - you might want to identify and make a couple more swaps - but it is entirely readable and recognisable as the first chapter of The Hound of the Baskervilles.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/frequency-analysis-in-python/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/frequency-analysis-in-python/comments"><span>Leave a comment</span></a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p><p></p>]]></content:encoded></item><item><title><![CDATA[Highest Common Factors with Euclidean Algorithm in Python]]></title><description><![CDATA[The Euclidean Algorithm is a simple method for finding the highest common factor or HCF (also known as greatest common divisor or GCD) of two positive integers.]]></description><link>https://codedrome.substack.com/p/highest-common-factors-with-euclidean-algorithm-python</link><guid isPermaLink="false">https://codedrome.substack.com/p/highest-common-factors-with-euclidean-algorithm-python</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 14 Apr 2026 13:55:15 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!tjay!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d160476-53ff-4552-8317-50cc96a4c816_600x600.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tjay!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d160476-53ff-4552-8317-50cc96a4c816_600x600.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tjay!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d160476-53ff-4552-8317-50cc96a4c816_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!tjay!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d160476-53ff-4552-8317-50cc96a4c816_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!tjay!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d160476-53ff-4552-8317-50cc96a4c816_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!tjay!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d160476-53ff-4552-8317-50cc96a4c816_600x600.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tjay!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d160476-53ff-4552-8317-50cc96a4c816_600x600.jpeg" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3d160476-53ff-4552-8317-50cc96a4c816_600x600.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:161732,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191675990?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d160476-53ff-4552-8317-50cc96a4c816_600x600.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tjay!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d160476-53ff-4552-8317-50cc96a4c816_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!tjay!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d160476-53ff-4552-8317-50cc96a4c816_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!tjay!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d160476-53ff-4552-8317-50cc96a4c816_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!tjay!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d160476-53ff-4552-8317-50cc96a4c816_600x600.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">image: <a href="https://pixabay.com/illustrations/banner-number-digit-maths-1183443/">Pixabay</a></figcaption></figure></div><p>The Euclidean Algorithm is a simple method for finding the highest common factor or HCF (also known as greatest common divisor or GCD) of two positive integers. This is an implementation of the algorithm in Python.</p><p>Wikipedia has a comprehensive article on the topic <a href="https://en.wikipedia.org/wiki/Euclidean_algorithm">here</a> so let&#8217;s get straight to the code. This article&#8217;s Python consists of a single file, <em>euclideanalgorithm.py</em>, which you can find in the <a href="https://github.com/CodeDrome/euclidean-algorithm-python">Github repository</a>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Iby2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ba2e59-c4e8-4fa5-a06e-62bd849755df_1140x1448.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Iby2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ba2e59-c4e8-4fa5-a06e-62bd849755df_1140x1448.png 424w, https://substackcdn.com/image/fetch/$s_!Iby2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ba2e59-c4e8-4fa5-a06e-62bd849755df_1140x1448.png 848w, https://substackcdn.com/image/fetch/$s_!Iby2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ba2e59-c4e8-4fa5-a06e-62bd849755df_1140x1448.png 1272w, https://substackcdn.com/image/fetch/$s_!Iby2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ba2e59-c4e8-4fa5-a06e-62bd849755df_1140x1448.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Iby2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ba2e59-c4e8-4fa5-a06e-62bd849755df_1140x1448.png" width="1140" height="1448" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a3ba2e59-c4e8-4fa5-a06e-62bd849755df_1140x1448.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1448,&quot;width&quot;:1140,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:228247,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191675990?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8259476a-a02d-4602-a8d9-e4d06943e871_1140x1448.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Iby2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ba2e59-c4e8-4fa5-a06e-62bd849755df_1140x1448.png 424w, https://substackcdn.com/image/fetch/$s_!Iby2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ba2e59-c4e8-4fa5-a06e-62bd849755df_1140x1448.png 848w, https://substackcdn.com/image/fetch/$s_!Iby2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ba2e59-c4e8-4fa5-a06e-62bd849755df_1140x1448.png 1272w, https://substackcdn.com/image/fetch/$s_!Iby2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ba2e59-c4e8-4fa5-a06e-62bd849755df_1140x1448.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The <strong>main</strong> function creates two lists containing pairs of numbers to apply the algorithm to. It then iterates the lists, calling the <strong>EuclideanHCF</strong> function and printing the result. It also prints each number divided by the HCF as a demonstration or proof.</p><p>The <strong>EuclideanHCF</strong> function replaces <strong>b</strong> with the modulo (remainder) of <strong>a</strong> and <strong>b</strong>, and replaces <strong>a</strong> with the previous value of <strong>b</strong>. This is repeated until <strong>b</strong> reaches 0, leaving a as the required HCF. This is an implementation of the Euclidean division variant of the algorithm which is more efficient than the subtraction method.</p><p>Run the program with this command in the terminal:</p><p><strong>python3 euclideanalgorithm.py</strong></p><p>The program output is:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!YErU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13dbc28f-629e-4511-a16f-519a59e9104d_180x397.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!YErU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13dbc28f-629e-4511-a16f-519a59e9104d_180x397.png 424w, https://substackcdn.com/image/fetch/$s_!YErU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13dbc28f-629e-4511-a16f-519a59e9104d_180x397.png 848w, https://substackcdn.com/image/fetch/$s_!YErU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13dbc28f-629e-4511-a16f-519a59e9104d_180x397.png 1272w, https://substackcdn.com/image/fetch/$s_!YErU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13dbc28f-629e-4511-a16f-519a59e9104d_180x397.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!YErU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13dbc28f-629e-4511-a16f-519a59e9104d_180x397.png" width="180" height="397" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/13dbc28f-629e-4511-a16f-519a59e9104d_180x397.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:397,&quot;width&quot;:180,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:20162,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191675990?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13dbc28f-629e-4511-a16f-519a59e9104d_180x397.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!YErU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13dbc28f-629e-4511-a16f-519a59e9104d_180x397.png 424w, https://substackcdn.com/image/fetch/$s_!YErU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13dbc28f-629e-4511-a16f-519a59e9104d_180x397.png 848w, https://substackcdn.com/image/fetch/$s_!YErU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13dbc28f-629e-4511-a16f-519a59e9104d_180x397.png 1272w, https://substackcdn.com/image/fetch/$s_!YErU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13dbc28f-629e-4511-a16f-519a59e9104d_180x397.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/highest-common-factors-with-euclidean-algorithm-python/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/highest-common-factors-with-euclidean-algorithm-python/comments"><span>Leave a comment</span></a></p><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p>]]></content:encoded></item><item><title><![CDATA[Factorization in Python]]></title><description><![CDATA[This is a simple Python project implementing a function to calculate the factors of a given integer, ie.]]></description><link>https://codedrome.substack.com/p/factorization-in-python</link><guid isPermaLink="false">https://codedrome.substack.com/p/factorization-in-python</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 07 Apr 2026 09:53:38 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!_JL-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa95ea56d-28ba-445b-a9d6-8c7a9b300024_600x600.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_JL-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa95ea56d-28ba-445b-a9d6-8c7a9b300024_600x600.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_JL-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa95ea56d-28ba-445b-a9d6-8c7a9b300024_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!_JL-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa95ea56d-28ba-445b-a9d6-8c7a9b300024_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!_JL-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa95ea56d-28ba-445b-a9d6-8c7a9b300024_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!_JL-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa95ea56d-28ba-445b-a9d6-8c7a9b300024_600x600.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_JL-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa95ea56d-28ba-445b-a9d6-8c7a9b300024_600x600.jpeg" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a95ea56d-28ba-445b-a9d6-8c7a9b300024_600x600.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:115908,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191603139?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa95ea56d-28ba-445b-a9d6-8c7a9b300024_600x600.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_JL-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa95ea56d-28ba-445b-a9d6-8c7a9b300024_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!_JL-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa95ea56d-28ba-445b-a9d6-8c7a9b300024_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!_JL-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa95ea56d-28ba-445b-a9d6-8c7a9b300024_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!_JL-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa95ea56d-28ba-445b-a9d6-8c7a9b300024_600x600.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Image: <a href="https://pixabay.com/illustrations/pay-numbers-digits-abundance-937882/">Pixabay</a></figcaption></figure></div><p>This is a simple Python project implementing a function to calculate the factors of a given integer, ie. all of the numbers which divide into that number. The core function returns a list of the factors of the number argument but as a bonus we can use that to write a generator function consisting of just one line of code!</p><p>To calculate the factors of a number <strong>n</strong> I will use a na&#239;ve brute-force method called trial division which attempts to divide <strong>n</strong> by all numbers up to and including <strong>n</strong> - if there is no remainder the number is a factor. No number more than <strong>n/2</strong> except <strong>n</strong> itself can be a factor of <strong>n</strong> so as an optimization I will only run the trial division on numbers up to <strong>n/2</strong>, and then add <strong>n</strong> to the factor list. Additionaly, 1 is a factor of all integers so it will be added to the list at the start and we will commence trial division at 2.</p><p>This article&#8217;s code consists of the following files which you can find in the <a href="https://github.com/CodeDrome/factorization-python">Github repository</a>.</p><ul><li><p>factorization.py</p></li><li><p>facdemo.py</p></li></ul><p>Let&#8217;s first look at <em>factorization.py</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2zuv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89d46547-29ad-4f32-9673-9ef820b60d4b_1345x1064.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2zuv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89d46547-29ad-4f32-9673-9ef820b60d4b_1345x1064.png 424w, https://substackcdn.com/image/fetch/$s_!2zuv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89d46547-29ad-4f32-9673-9ef820b60d4b_1345x1064.png 848w, https://substackcdn.com/image/fetch/$s_!2zuv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89d46547-29ad-4f32-9673-9ef820b60d4b_1345x1064.png 1272w, https://substackcdn.com/image/fetch/$s_!2zuv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89d46547-29ad-4f32-9673-9ef820b60d4b_1345x1064.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2zuv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89d46547-29ad-4f32-9673-9ef820b60d4b_1345x1064.png" width="1345" height="1064" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/89d46547-29ad-4f32-9673-9ef820b60d4b_1345x1064.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1064,&quot;width&quot;:1345,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:155382,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191603139?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a0f9157-a2d8-4ee2-b252-f33c139a42d0_1345x1064.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!2zuv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89d46547-29ad-4f32-9673-9ef820b60d4b_1345x1064.png 424w, https://substackcdn.com/image/fetch/$s_!2zuv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89d46547-29ad-4f32-9673-9ef820b60d4b_1345x1064.png 848w, https://substackcdn.com/image/fetch/$s_!2zuv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89d46547-29ad-4f32-9673-9ef820b60d4b_1345x1064.png 1272w, https://substackcdn.com/image/fetch/$s_!2zuv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89d46547-29ad-4f32-9673-9ef820b60d4b_1345x1064.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The <strong>get_factor_list</strong> function creates a list with a single value, 1, which is a factor of any integer so not worth checking. We then enter a loop to perform modular division of <strong>n</strong> by all numbers up to <strong>n/2</strong> - if the result is 0 then the current number is a factor and can be added to the list. Finally we add <strong>n</strong> to the list and return it.</p><p>The function <strong>factors</strong> is a generator which is a special kind of function returning a type of iterator called a generator object. This function can then be called using <strong>for/in</strong> syntax to retrieve all values. In this particular case we already have the function <strong>get_factor_list</strong> to actually calculate factors so within the generator function we can call <strong>get_factor_list</strong> and pass the list it returns to the <strong>iter</strong> function. This creates an iterator which we can loop using <strong>for/in</strong>. All that with just a single line of code.</p><p>Now we just need a bit of code to test these functions. This is <em>facdemo.py</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Xk-a!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79cef44e-a40f-4242-aa5d-239896d72a5c_1356x1320.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Xk-a!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79cef44e-a40f-4242-aa5d-239896d72a5c_1356x1320.png 424w, https://substackcdn.com/image/fetch/$s_!Xk-a!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79cef44e-a40f-4242-aa5d-239896d72a5c_1356x1320.png 848w, https://substackcdn.com/image/fetch/$s_!Xk-a!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79cef44e-a40f-4242-aa5d-239896d72a5c_1356x1320.png 1272w, https://substackcdn.com/image/fetch/$s_!Xk-a!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79cef44e-a40f-4242-aa5d-239896d72a5c_1356x1320.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Xk-a!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79cef44e-a40f-4242-aa5d-239896d72a5c_1356x1320.png" width="1356" height="1320" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/79cef44e-a40f-4242-aa5d-239896d72a5c_1356x1320.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1320,&quot;width&quot;:1356,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:205630,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191603139?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F854b848c-6f31-4d00-b136-8087dc6fbe0a_1356x1320.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Xk-a!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79cef44e-a40f-4242-aa5d-239896d72a5c_1356x1320.png 424w, https://substackcdn.com/image/fetch/$s_!Xk-a!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79cef44e-a40f-4242-aa5d-239896d72a5c_1356x1320.png 848w, https://substackcdn.com/image/fetch/$s_!Xk-a!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79cef44e-a40f-4242-aa5d-239896d72a5c_1356x1320.png 1272w, https://substackcdn.com/image/fetch/$s_!Xk-a!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79cef44e-a40f-4242-aa5d-239896d72a5c_1356x1320.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>At the top we <strong>import factorization</strong> and then within <strong>main</strong> create a list of numbers to factorize. We then iterate this list for the first time, calling <strong>get_factor_list</strong> on each item and printing the result.</p><p>We then iterate the numbers again, this time calling the generator using <strong>for/in</strong>. This time of course we get the factors one at a time instead of all together in a list.</p><p>That&#8217;s the code finished so run it with this command:</p><p><strong>python3 facdemo.py</strong></p><p>The output looks like this:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gFMc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a886b95-4450-496b-8a49-fbe922ac3386_298x299.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gFMc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a886b95-4450-496b-8a49-fbe922ac3386_298x299.png 424w, https://substackcdn.com/image/fetch/$s_!gFMc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a886b95-4450-496b-8a49-fbe922ac3386_298x299.png 848w, https://substackcdn.com/image/fetch/$s_!gFMc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a886b95-4450-496b-8a49-fbe922ac3386_298x299.png 1272w, https://substackcdn.com/image/fetch/$s_!gFMc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a886b95-4450-496b-8a49-fbe922ac3386_298x299.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gFMc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a886b95-4450-496b-8a49-fbe922ac3386_298x299.png" width="298" height="299" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9a886b95-4450-496b-8a49-fbe922ac3386_298x299.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:299,&quot;width&quot;:298,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:22636,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/191603139?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a886b95-4450-496b-8a49-fbe922ac3386_298x299.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gFMc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a886b95-4450-496b-8a49-fbe922ac3386_298x299.png 424w, https://substackcdn.com/image/fetch/$s_!gFMc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a886b95-4450-496b-8a49-fbe922ac3386_298x299.png 848w, https://substackcdn.com/image/fetch/$s_!gFMc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a886b95-4450-496b-8a49-fbe922ac3386_298x299.png 1272w, https://substackcdn.com/image/fetch/$s_!gFMc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a886b95-4450-496b-8a49-fbe922ac3386_298x299.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/factorization-in-python/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/factorization-in-python/comments"><span>Leave a comment</span></a></p><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p>]]></content:encoded></item><item><title><![CDATA[Estimating e in Python]]></title><description><![CDATA[A while ago I wrote a post on estimating Pi using a variety of methods.]]></description><link>https://codedrome.substack.com/p/estimating-e-in-python</link><guid isPermaLink="false">https://codedrome.substack.com/p/estimating-e-in-python</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 31 Mar 2026 14:37:59 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!-R2u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d70a0ef-4843-411b-a04b-a94635ca663d_600x600.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-R2u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d70a0ef-4843-411b-a04b-a94635ca663d_600x600.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-R2u!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d70a0ef-4843-411b-a04b-a94635ca663d_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!-R2u!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d70a0ef-4843-411b-a04b-a94635ca663d_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!-R2u!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d70a0ef-4843-411b-a04b-a94635ca663d_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!-R2u!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d70a0ef-4843-411b-a04b-a94635ca663d_600x600.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-R2u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d70a0ef-4843-411b-a04b-a94635ca663d_600x600.jpeg" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0d70a0ef-4843-411b-a04b-a94635ca663d_600x600.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:362626,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190628310?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d70a0ef-4843-411b-a04b-a94635ca663d_600x600.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-R2u!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d70a0ef-4843-411b-a04b-a94635ca663d_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!-R2u!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d70a0ef-4843-411b-a04b-a94635ca663d_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!-R2u!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d70a0ef-4843-411b-a04b-a94635ca663d_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!-R2u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d70a0ef-4843-411b-a04b-a94635ca663d_600x600.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Swiss mathematician Leonhard Euler, 15 April 1707 to 18 September 1783. Image: <a href="https://en.wikipedia.org/wiki/File:Leonhard_Euler_-_Jakob_Emanuel_Handmann_(Kunstmuseum_Basel).jpg">Wikipedia</a></figcaption></figure></div><p>A while ago I wrote a post on <a href="https://codedrome.substack.com/p/estimating-pi-in-python">estimating Pi</a> using a variety of methods. As a sort of follow-up I will now write a post on estimating <em>e</em>, or Euler&#8217;s Number, in Python. I won&#8217;t try to summarize what <em>e</em> is or what it is used for as Wikipedia has a <a href="https://en.wikipedia.org/wiki/E_(mathematical_constant)">comprehensive article</a> on the subject.</p><h2>The Code</h2><p>Let&#8217;s dive straight into the code which lives in a single file called <em>e.py</em>. (At least you can&#8217;t accuse me of using overly long filenames). You can clone/download the source code from the <a href="https://github.com/CodeDrome/estimating-e-python">GitHub repository</a>.</p><p>This is the first part of the source code.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MW8u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd24df57a-bbb5-4d09-a666-788f88e2bed6_1240x1544.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MW8u!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd24df57a-bbb5-4d09-a666-788f88e2bed6_1240x1544.png 424w, https://substackcdn.com/image/fetch/$s_!MW8u!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd24df57a-bbb5-4d09-a666-788f88e2bed6_1240x1544.png 848w, https://substackcdn.com/image/fetch/$s_!MW8u!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd24df57a-bbb5-4d09-a666-788f88e2bed6_1240x1544.png 1272w, https://substackcdn.com/image/fetch/$s_!MW8u!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd24df57a-bbb5-4d09-a666-788f88e2bed6_1240x1544.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MW8u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd24df57a-bbb5-4d09-a666-788f88e2bed6_1240x1544.png" width="1240" height="1544" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d24df57a-bbb5-4d09-a666-788f88e2bed6_1240x1544.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1544,&quot;width&quot;:1240,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:231278,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190628310?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa510d3e0-607c-4a70-8e25-ae7a65665281_1240x1544.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!MW8u!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd24df57a-bbb5-4d09-a666-788f88e2bed6_1240x1544.png 424w, https://substackcdn.com/image/fetch/$s_!MW8u!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd24df57a-bbb5-4d09-a666-788f88e2bed6_1240x1544.png 848w, https://substackcdn.com/image/fetch/$s_!MW8u!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd24df57a-bbb5-4d09-a666-788f88e2bed6_1240x1544.png 1272w, https://substackcdn.com/image/fetch/$s_!MW8u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd24df57a-bbb5-4d09-a666-788f88e2bed6_1240x1544.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Near the top of the file I have defined a constant called <strong>E15DP</strong> which is a string containing <em>e</em> to 15 decimal places. This will be used as a definitive value to compare with values we calculate. The name is all upper case to denote that it should be considered as a constant.</p><p>Next we have a few colour constants to output text to the console in various colours.</p><p>The <strong>main</strong> function consists of four function calls to estimate <em>e</em> using various methods. All but the first are commented out for now as we&#8217;ll run them one at a time.</p><p>The <strong>print_as_text</strong> function takes a value for <em>e</em> and converts it to a string with 15 decimal places. It then prints it after the definitive value, with correct digits in green and incorrect ones in red.</p><h2>First Expansion</h2><p>The first function to calculate <em>e</em> is expansion_1 which implements this formula...</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!KT44!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce026e0a-24cc-4dd4-84c9-475754fac3d3_158x44.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!KT44!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce026e0a-24cc-4dd4-84c9-475754fac3d3_158x44.png 424w, https://substackcdn.com/image/fetch/$s_!KT44!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce026e0a-24cc-4dd4-84c9-475754fac3d3_158x44.png 848w, https://substackcdn.com/image/fetch/$s_!KT44!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce026e0a-24cc-4dd4-84c9-475754fac3d3_158x44.png 1272w, https://substackcdn.com/image/fetch/$s_!KT44!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce026e0a-24cc-4dd4-84c9-475754fac3d3_158x44.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!KT44!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce026e0a-24cc-4dd4-84c9-475754fac3d3_158x44.png" width="158" height="44" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ce026e0a-24cc-4dd4-84c9-475754fac3d3_158x44.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:44,&quot;width&quot;:158,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1588,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190628310?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce026e0a-24cc-4dd4-84c9-475754fac3d3_158x44.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!KT44!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce026e0a-24cc-4dd4-84c9-475754fac3d3_158x44.png 424w, https://substackcdn.com/image/fetch/$s_!KT44!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce026e0a-24cc-4dd4-84c9-475754fac3d3_158x44.png 848w, https://substackcdn.com/image/fetch/$s_!KT44!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce026e0a-24cc-4dd4-84c9-475754fac3d3_158x44.png 1272w, https://substackcdn.com/image/fetch/$s_!KT44!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce026e0a-24cc-4dd4-84c9-475754fac3d3_158x44.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>...with a value of <strong>n</strong> = 100000.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5bqr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa31aaa8f-e3b7-49e4-bee8-076bebdeb4a8_1240x328.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5bqr!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa31aaa8f-e3b7-49e4-bee8-076bebdeb4a8_1240x328.png 424w, https://substackcdn.com/image/fetch/$s_!5bqr!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa31aaa8f-e3b7-49e4-bee8-076bebdeb4a8_1240x328.png 848w, https://substackcdn.com/image/fetch/$s_!5bqr!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa31aaa8f-e3b7-49e4-bee8-076bebdeb4a8_1240x328.png 1272w, https://substackcdn.com/image/fetch/$s_!5bqr!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa31aaa8f-e3b7-49e4-bee8-076bebdeb4a8_1240x328.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5bqr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa31aaa8f-e3b7-49e4-bee8-076bebdeb4a8_1240x328.png" width="1240" height="328" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a31aaa8f-e3b7-49e4-bee8-076bebdeb4a8_1240x328.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:328,&quot;width&quot;:1240,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:45426,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190628310?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F28babf48-44d4-4d0a-b524-122dc0b59110_1240x328.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5bqr!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa31aaa8f-e3b7-49e4-bee8-076bebdeb4a8_1240x328.png 424w, https://substackcdn.com/image/fetch/$s_!5bqr!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa31aaa8f-e3b7-49e4-bee8-076bebdeb4a8_1240x328.png 848w, https://substackcdn.com/image/fetch/$s_!5bqr!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa31aaa8f-e3b7-49e4-bee8-076bebdeb4a8_1240x328.png 1272w, https://substackcdn.com/image/fetch/$s_!5bqr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa31aaa8f-e3b7-49e4-bee8-076bebdeb4a8_1240x328.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We now have enough to run with this command.</p><p>Running the program</p><p><strong>python3 e.py</strong></p><p>The output is:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!mKEL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd1ae547-7c34-4dcb-ad14-55448854a796_242x92.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!mKEL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd1ae547-7c34-4dcb-ad14-55448854a796_242x92.png 424w, https://substackcdn.com/image/fetch/$s_!mKEL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd1ae547-7c34-4dcb-ad14-55448854a796_242x92.png 848w, https://substackcdn.com/image/fetch/$s_!mKEL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd1ae547-7c34-4dcb-ad14-55448854a796_242x92.png 1272w, https://substackcdn.com/image/fetch/$s_!mKEL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd1ae547-7c34-4dcb-ad14-55448854a796_242x92.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!mKEL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd1ae547-7c34-4dcb-ad14-55448854a796_242x92.png" width="242" height="92" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fd1ae547-7c34-4dcb-ad14-55448854a796_242x92.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:92,&quot;width&quot;:242,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:12748,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190628310?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd1ae547-7c34-4dcb-ad14-55448854a796_242x92.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!mKEL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd1ae547-7c34-4dcb-ad14-55448854a796_242x92.png 424w, https://substackcdn.com/image/fetch/$s_!mKEL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd1ae547-7c34-4dcb-ad14-55448854a796_242x92.png 848w, https://substackcdn.com/image/fetch/$s_!mKEL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd1ae547-7c34-4dcb-ad14-55448854a796_242x92.png 1272w, https://substackcdn.com/image/fetch/$s_!mKEL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd1ae547-7c34-4dcb-ad14-55448854a796_242x92.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>As you can see this gives us 4dp of accuracy; you might like to experiment with different values of <strong>n</strong>.</p><h2>Second Expansion</h2><p>Now let&#8217;s try another expansion to implement this formula with <strong>n</strong> = 0.000000001.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rLr5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F12a03db6-f905-486c-a92f-ac306f64991b_163x49.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rLr5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F12a03db6-f905-486c-a92f-ac306f64991b_163x49.png 424w, https://substackcdn.com/image/fetch/$s_!rLr5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F12a03db6-f905-486c-a92f-ac306f64991b_163x49.png 848w, https://substackcdn.com/image/fetch/$s_!rLr5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F12a03db6-f905-486c-a92f-ac306f64991b_163x49.png 1272w, https://substackcdn.com/image/fetch/$s_!rLr5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F12a03db6-f905-486c-a92f-ac306f64991b_163x49.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rLr5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F12a03db6-f905-486c-a92f-ac306f64991b_163x49.png" width="163" height="49" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/12a03db6-f905-486c-a92f-ac306f64991b_163x49.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:49,&quot;width&quot;:163,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1531,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190628310?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F12a03db6-f905-486c-a92f-ac306f64991b_163x49.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rLr5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F12a03db6-f905-486c-a92f-ac306f64991b_163x49.png 424w, https://substackcdn.com/image/fetch/$s_!rLr5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F12a03db6-f905-486c-a92f-ac306f64991b_163x49.png 848w, https://substackcdn.com/image/fetch/$s_!rLr5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F12a03db6-f905-486c-a92f-ac306f64991b_163x49.png 1272w, https://substackcdn.com/image/fetch/$s_!rLr5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F12a03db6-f905-486c-a92f-ac306f64991b_163x49.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0HBE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa209f0-3b5e-4326-bf00-74995811bd08_1240x392.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0HBE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa209f0-3b5e-4326-bf00-74995811bd08_1240x392.png 424w, https://substackcdn.com/image/fetch/$s_!0HBE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa209f0-3b5e-4326-bf00-74995811bd08_1240x392.png 848w, https://substackcdn.com/image/fetch/$s_!0HBE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa209f0-3b5e-4326-bf00-74995811bd08_1240x392.png 1272w, https://substackcdn.com/image/fetch/$s_!0HBE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa209f0-3b5e-4326-bf00-74995811bd08_1240x392.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0HBE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa209f0-3b5e-4326-bf00-74995811bd08_1240x392.png" width="1240" height="392" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9fa209f0-3b5e-4326-bf00-74995811bd08_1240x392.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:392,&quot;width&quot;:1240,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:54062,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190628310?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc44c551-0b4e-4508-bbe7-08df835e7087_1240x392.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!0HBE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa209f0-3b5e-4326-bf00-74995811bd08_1240x392.png 424w, https://substackcdn.com/image/fetch/$s_!0HBE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa209f0-3b5e-4326-bf00-74995811bd08_1240x392.png 848w, https://substackcdn.com/image/fetch/$s_!0HBE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa209f0-3b5e-4326-bf00-74995811bd08_1240x392.png 1272w, https://substackcdn.com/image/fetch/$s_!0HBE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9fa209f0-3b5e-4326-bf00-74995811bd08_1240x392.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Comment out <strong>expansion_1()</strong> in <strong>main</strong>, uncomment <strong>expansion_2()</strong> and run the program again. This is the output.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!NbVv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be501bc-25af-4a47-b108-4664a71cebd9_243x91.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NbVv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be501bc-25af-4a47-b108-4664a71cebd9_243x91.png 424w, https://substackcdn.com/image/fetch/$s_!NbVv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be501bc-25af-4a47-b108-4664a71cebd9_243x91.png 848w, https://substackcdn.com/image/fetch/$s_!NbVv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be501bc-25af-4a47-b108-4664a71cebd9_243x91.png 1272w, https://substackcdn.com/image/fetch/$s_!NbVv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be501bc-25af-4a47-b108-4664a71cebd9_243x91.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!NbVv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be501bc-25af-4a47-b108-4664a71cebd9_243x91.png" width="243" height="91" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7be501bc-25af-4a47-b108-4664a71cebd9_243x91.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:91,&quot;width&quot;:243,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:12879,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190628310?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be501bc-25af-4a47-b108-4664a71cebd9_243x91.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!NbVv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be501bc-25af-4a47-b108-4664a71cebd9_243x91.png 424w, https://substackcdn.com/image/fetch/$s_!NbVv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be501bc-25af-4a47-b108-4664a71cebd9_243x91.png 848w, https://substackcdn.com/image/fetch/$s_!NbVv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be501bc-25af-4a47-b108-4664a71cebd9_243x91.png 1272w, https://substackcdn.com/image/fetch/$s_!NbVv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be501bc-25af-4a47-b108-4664a71cebd9_243x91.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This gives us 5dp of accuracy but again you can try different values of n.</p><h2>Newton&#8217;s Series</h2><p>Now let&#8217;s move on to the third method, Newton&#8217;s Series, for which the formula is:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8H4t!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec892371-ce70-4463-87c6-931f4ec188bc_123x68.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8H4t!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec892371-ce70-4463-87c6-931f4ec188bc_123x68.png 424w, https://substackcdn.com/image/fetch/$s_!8H4t!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec892371-ce70-4463-87c6-931f4ec188bc_123x68.png 848w, https://substackcdn.com/image/fetch/$s_!8H4t!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec892371-ce70-4463-87c6-931f4ec188bc_123x68.png 1272w, https://substackcdn.com/image/fetch/$s_!8H4t!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec892371-ce70-4463-87c6-931f4ec188bc_123x68.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8H4t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec892371-ce70-4463-87c6-931f4ec188bc_123x68.png" width="123" height="68" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ec892371-ce70-4463-87c6-931f4ec188bc_123x68.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:68,&quot;width&quot;:123,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2329,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190628310?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec892371-ce70-4463-87c6-931f4ec188bc_123x68.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8H4t!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec892371-ce70-4463-87c6-931f4ec188bc_123x68.png 424w, https://substackcdn.com/image/fetch/$s_!8H4t!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec892371-ce70-4463-87c6-931f4ec188bc_123x68.png 848w, https://substackcdn.com/image/fetch/$s_!8H4t!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec892371-ce70-4463-87c6-931f4ec188bc_123x68.png 1272w, https://substackcdn.com/image/fetch/$s_!8H4t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec892371-ce70-4463-87c6-931f4ec188bc_123x68.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This means we start off with n = 0, set <em>e</em> to 1/n!, increment n and then keep adding 1/n! to <em>e</em> for as long as we want. (n! means the product of all integers up to and including n, for example 4! = 4 x 3 x 2 x 1 = 24. Incidentally 0! = 1.) This is the function.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZH9e!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38fb8fb-1ece-4bfe-aa38-ed7e1395bce2_1240x392.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZH9e!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38fb8fb-1ece-4bfe-aa38-ed7e1395bce2_1240x392.png 424w, https://substackcdn.com/image/fetch/$s_!ZH9e!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38fb8fb-1ece-4bfe-aa38-ed7e1395bce2_1240x392.png 848w, https://substackcdn.com/image/fetch/$s_!ZH9e!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38fb8fb-1ece-4bfe-aa38-ed7e1395bce2_1240x392.png 1272w, https://substackcdn.com/image/fetch/$s_!ZH9e!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38fb8fb-1ece-4bfe-aa38-ed7e1395bce2_1240x392.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZH9e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38fb8fb-1ece-4bfe-aa38-ed7e1395bce2_1240x392.png" width="1240" height="392" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c38fb8fb-1ece-4bfe-aa38-ed7e1395bce2_1240x392.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:392,&quot;width&quot;:1240,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:57613,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190628310?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ba0120d-eb89-4cda-9dad-2767396ef6f8_1240x392.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ZH9e!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38fb8fb-1ece-4bfe-aa38-ed7e1395bce2_1240x392.png 424w, https://substackcdn.com/image/fetch/$s_!ZH9e!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38fb8fb-1ece-4bfe-aa38-ed7e1395bce2_1240x392.png 848w, https://substackcdn.com/image/fetch/$s_!ZH9e!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38fb8fb-1ece-4bfe-aa38-ed7e1395bce2_1240x392.png 1272w, https://substackcdn.com/image/fetch/$s_!ZH9e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc38fb8fb-1ece-4bfe-aa38-ed7e1395bce2_1240x392.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Uncomment <strong>newtons_series()</strong> in <strong>main</strong> and run the program.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JTx1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5503fe93-1459-4822-b3c2-6c1eafe7ff78_243x90.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JTx1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5503fe93-1459-4822-b3c2-6c1eafe7ff78_243x90.png 424w, https://substackcdn.com/image/fetch/$s_!JTx1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5503fe93-1459-4822-b3c2-6c1eafe7ff78_243x90.png 848w, https://substackcdn.com/image/fetch/$s_!JTx1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5503fe93-1459-4822-b3c2-6c1eafe7ff78_243x90.png 1272w, https://substackcdn.com/image/fetch/$s_!JTx1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5503fe93-1459-4822-b3c2-6c1eafe7ff78_243x90.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JTx1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5503fe93-1459-4822-b3c2-6c1eafe7ff78_243x90.png" width="243" height="90" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5503fe93-1459-4822-b3c2-6c1eafe7ff78_243x90.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:90,&quot;width&quot;:243,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:12779,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190628310?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5503fe93-1459-4822-b3c2-6c1eafe7ff78_243x90.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JTx1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5503fe93-1459-4822-b3c2-6c1eafe7ff78_243x90.png 424w, https://substackcdn.com/image/fetch/$s_!JTx1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5503fe93-1459-4822-b3c2-6c1eafe7ff78_243x90.png 848w, https://substackcdn.com/image/fetch/$s_!JTx1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5503fe93-1459-4822-b3c2-6c1eafe7ff78_243x90.png 1272w, https://substackcdn.com/image/fetch/$s_!JTx1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5503fe93-1459-4822-b3c2-6c1eafe7ff78_243x90.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Sir Isaac gives us 9dp of accurancy with a maximum value of n = 12.</p><h2>Brothers Formula</h2><p>As with &#960; people are still working on ways to calculate <em>e</em> and I&#8217;ll finish off with this impressive recently discovered method, Brothers Formula, devised as recently as 2004.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lQGA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b36ab98-a48b-4746-9dc4-2e65c093f6c9_148x69.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lQGA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b36ab98-a48b-4746-9dc4-2e65c093f6c9_148x69.png 424w, https://substackcdn.com/image/fetch/$s_!lQGA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b36ab98-a48b-4746-9dc4-2e65c093f6c9_148x69.png 848w, https://substackcdn.com/image/fetch/$s_!lQGA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b36ab98-a48b-4746-9dc4-2e65c093f6c9_148x69.png 1272w, https://substackcdn.com/image/fetch/$s_!lQGA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b36ab98-a48b-4746-9dc4-2e65c093f6c9_148x69.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lQGA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b36ab98-a48b-4746-9dc4-2e65c093f6c9_148x69.png" width="148" height="69" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1b36ab98-a48b-4746-9dc4-2e65c093f6c9_148x69.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:69,&quot;width&quot;:148,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2858,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190628310?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b36ab98-a48b-4746-9dc4-2e65c093f6c9_148x69.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!lQGA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b36ab98-a48b-4746-9dc4-2e65c093f6c9_148x69.png 424w, https://substackcdn.com/image/fetch/$s_!lQGA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b36ab98-a48b-4746-9dc4-2e65c093f6c9_148x69.png 848w, https://substackcdn.com/image/fetch/$s_!lQGA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b36ab98-a48b-4746-9dc4-2e65c093f6c9_148x69.png 1272w, https://substackcdn.com/image/fetch/$s_!lQGA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b36ab98-a48b-4746-9dc4-2e65c093f6c9_148x69.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>As with Newton&#8217;s Series this is the sum of an infinite series although rather more complex.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4qYc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b710568-a041-4ada-a79e-b5f260aa1041_1240x392.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4qYc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b710568-a041-4ada-a79e-b5f260aa1041_1240x392.png 424w, https://substackcdn.com/image/fetch/$s_!4qYc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b710568-a041-4ada-a79e-b5f260aa1041_1240x392.png 848w, https://substackcdn.com/image/fetch/$s_!4qYc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b710568-a041-4ada-a79e-b5f260aa1041_1240x392.png 1272w, https://substackcdn.com/image/fetch/$s_!4qYc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b710568-a041-4ada-a79e-b5f260aa1041_1240x392.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4qYc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b710568-a041-4ada-a79e-b5f260aa1041_1240x392.png" width="1240" height="392" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1b710568-a041-4ada-a79e-b5f260aa1041_1240x392.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:392,&quot;width&quot;:1240,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:63718,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190628310?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4b5c979d-74a3-40cd-a00d-70d3f381188e_1240x392.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4qYc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b710568-a041-4ada-a79e-b5f260aa1041_1240x392.png 424w, https://substackcdn.com/image/fetch/$s_!4qYc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b710568-a041-4ada-a79e-b5f260aa1041_1240x392.png 848w, https://substackcdn.com/image/fetch/$s_!4qYc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b710568-a041-4ada-a79e-b5f260aa1041_1240x392.png 1272w, https://substackcdn.com/image/fetch/$s_!4qYc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b710568-a041-4ada-a79e-b5f260aa1041_1240x392.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The extra complexity pays off though: we only need to count to 8 to get 15dp (possibly more) of accuracy.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vQ3B!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82af004d-8e80-45a7-bb01-d6b1cd8e565a_243x90.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vQ3B!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82af004d-8e80-45a7-bb01-d6b1cd8e565a_243x90.png 424w, https://substackcdn.com/image/fetch/$s_!vQ3B!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82af004d-8e80-45a7-bb01-d6b1cd8e565a_243x90.png 848w, https://substackcdn.com/image/fetch/$s_!vQ3B!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82af004d-8e80-45a7-bb01-d6b1cd8e565a_243x90.png 1272w, https://substackcdn.com/image/fetch/$s_!vQ3B!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82af004d-8e80-45a7-bb01-d6b1cd8e565a_243x90.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vQ3B!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82af004d-8e80-45a7-bb01-d6b1cd8e565a_243x90.png" width="243" height="90" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/82af004d-8e80-45a7-bb01-d6b1cd8e565a_243x90.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:90,&quot;width&quot;:243,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:12851,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190628310?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82af004d-8e80-45a7-bb01-d6b1cd8e565a_243x90.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!vQ3B!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82af004d-8e80-45a7-bb01-d6b1cd8e565a_243x90.png 424w, https://substackcdn.com/image/fetch/$s_!vQ3B!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82af004d-8e80-45a7-bb01-d6b1cd8e565a_243x90.png 848w, https://substackcdn.com/image/fetch/$s_!vQ3B!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82af004d-8e80-45a7-bb01-d6b1cd8e565a_243x90.png 1272w, https://substackcdn.com/image/fetch/$s_!vQ3B!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82af004d-8e80-45a7-bb01-d6b1cd8e565a_243x90.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/estimating-e-in-python/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/estimating-e-in-python/comments"><span>Leave a comment</span></a></p><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p><p></p>]]></content:encoded></item><item><title><![CDATA[Mathematical Symbols in Python]]></title><description><![CDATA[I often use mathematical symbols in Python code which results in me scrabbling around to find the ones I need so I decided to write a simple module to simplify the process, which I will describe in this article.]]></description><link>https://codedrome.substack.com/p/mathematical-symbols-in-python</link><guid isPermaLink="false">https://codedrome.substack.com/p/mathematical-symbols-in-python</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 24 Mar 2026 17:54:19 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!oQC7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5cd365-a1bc-4557-a97e-a55a9cc3206a_600x600.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!oQC7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5cd365-a1bc-4557-a97e-a55a9cc3206a_600x600.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oQC7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5cd365-a1bc-4557-a97e-a55a9cc3206a_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!oQC7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5cd365-a1bc-4557-a97e-a55a9cc3206a_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!oQC7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5cd365-a1bc-4557-a97e-a55a9cc3206a_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!oQC7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5cd365-a1bc-4557-a97e-a55a9cc3206a_600x600.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oQC7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5cd365-a1bc-4557-a97e-a55a9cc3206a_600x600.jpeg" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8b5cd365-a1bc-4557-a97e-a55a9cc3206a_600x600.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:178169,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190414404?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5cd365-a1bc-4557-a97e-a55a9cc3206a_600x600.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!oQC7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5cd365-a1bc-4557-a97e-a55a9cc3206a_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!oQC7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5cd365-a1bc-4557-a97e-a55a9cc3206a_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!oQC7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5cd365-a1bc-4557-a97e-a55a9cc3206a_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!oQC7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b5cd365-a1bc-4557-a97e-a55a9cc3206a_600x600.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I often use mathematical symbols in Python code which results in me scrabbling around to find the ones I need so I decided to write a simple module to simplify the process, which I will describe in this article.</p><h2>Overview</h2><p>The Unicode character encoding system currently (version 17.0, released September 2025) consists of 159,801 characters across 30 sub-categories. The characters likely to be used for mathematical notation are scattered across several categories so for my <strong>mathsymbols</strong> module I gathered them together into a dictionary, and also provided functions for searching the dictionary and printing out details of each symbol.</p><p><em>With nearly 160,000 Unicode characters it is almost inevitable that I have missed some which could be considered mathematical so if you know of any that you think should be included please let me know.</em></p><p>Once you import the <strong>mathsymbols</strong> module there are several ways to use it in code which we&#8217;ll see later.</p><h2>The Project</h2><p>This project consists of the following files which you can clone/download from the <a href="https://github.com/CodeDrome/mathematical-symbols-python">GitHub repository</a>.</p><ul><li><p>mathsymbols.py</p></li><li><p>msdemo.py</p></li></ul><h2>The Code</h2><p>This is the <em>mathsymbols.py</em> file.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!HCrw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4901a369-909d-4943-90e3-8015a4dd2fbe_1442x4648.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!HCrw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4901a369-909d-4943-90e3-8015a4dd2fbe_1442x4648.png 424w, https://substackcdn.com/image/fetch/$s_!HCrw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4901a369-909d-4943-90e3-8015a4dd2fbe_1442x4648.png 848w, https://substackcdn.com/image/fetch/$s_!HCrw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4901a369-909d-4943-90e3-8015a4dd2fbe_1442x4648.png 1272w, https://substackcdn.com/image/fetch/$s_!HCrw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4901a369-909d-4943-90e3-8015a4dd2fbe_1442x4648.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!HCrw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4901a369-909d-4943-90e3-8015a4dd2fbe_1442x4648.png" width="1442" height="4648" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4901a369-909d-4943-90e3-8015a4dd2fbe_1442x4648.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:4648,&quot;width&quot;:1442,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:968539,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190414404?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1cef364c-9007-462e-bd15-c3e64d6b84be_1442x4648.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!HCrw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4901a369-909d-4943-90e3-8015a4dd2fbe_1442x4648.png 424w, https://substackcdn.com/image/fetch/$s_!HCrw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4901a369-909d-4943-90e3-8015a4dd2fbe_1442x4648.png 848w, https://substackcdn.com/image/fetch/$s_!HCrw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4901a369-909d-4943-90e3-8015a4dd2fbe_1442x4648.png 1272w, https://substackcdn.com/image/fetch/$s_!HCrw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4901a369-909d-4943-90e3-8015a4dd2fbe_1442x4648.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>The symbols Dictionary</h3><p>This dictionary, which is wrapped in a <strong>MappingProxyType</strong> to make it immutable, uses brief descriptions as its keys and the characters themselves as values. Note that the descriptions are not the same as those used by Unicode but chosen to be more consistent and easily searchable. This means there is a certain amount of duplication in some keys, for example fractions contain the numbers in both words and digits, eg. <strong>fraction_seven_eighths_7_8</strong>.</p><h3>The search Function</h3><p>This is a simple list comprehension wrapped as a dictionary, returning any key/values with a key containing namelike. I didn&#8217;t consider it necessary to provide any wildcard options due to the small number of items in the symbols dictionary.</p><h3>The print_symbols Function</h3><p>This prints the supplied dictionary, <strong>symbols_to_print</strong>, or the entire <strong>symbols</strong> dictionary if this is not supplied. We can use this function to print a dictionary obtained from the search function.</p><h2>Trying it Out</h2><p>Now let&#8217;s try it out with the <em>msdemo.py</em> program.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!YULb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299e28e0-947e-42e3-8a04-4253f6e52dea_1442x1160.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!YULb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299e28e0-947e-42e3-8a04-4253f6e52dea_1442x1160.png 424w, https://substackcdn.com/image/fetch/$s_!YULb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299e28e0-947e-42e3-8a04-4253f6e52dea_1442x1160.png 848w, https://substackcdn.com/image/fetch/$s_!YULb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299e28e0-947e-42e3-8a04-4253f6e52dea_1442x1160.png 1272w, https://substackcdn.com/image/fetch/$s_!YULb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299e28e0-947e-42e3-8a04-4253f6e52dea_1442x1160.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!YULb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299e28e0-947e-42e3-8a04-4253f6e52dea_1442x1160.png" width="1442" height="1160" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/299e28e0-947e-42e3-8a04-4253f6e52dea_1442x1160.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1160,&quot;width&quot;:1442,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:198373,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190414404?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe08fbda-cdbb-4817-9fdb-426dd94e8faa_1442x1160.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!YULb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299e28e0-947e-42e3-8a04-4253f6e52dea_1442x1160.png 424w, https://substackcdn.com/image/fetch/$s_!YULb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299e28e0-947e-42e3-8a04-4253f6e52dea_1442x1160.png 848w, https://substackcdn.com/image/fetch/$s_!YULb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299e28e0-947e-42e3-8a04-4253f6e52dea_1442x1160.png 1272w, https://substackcdn.com/image/fetch/$s_!YULb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F299e28e0-947e-42e3-8a04-4253f6e52dea_1442x1160.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This short program demonstrates printing all symbols, searching and printing the result, and the four ways of using symbols in code.</p><p>The line currently uncommented calls <strong>print_symbols()</strong> without an argument so prints all of the symbols dictionary. Note that I aliased <strong>mathsymbols</strong> as <strong>ms</strong> in the import. Run the code thus:</p><p><strong>python3 msdemo.py</strong></p><p>This will give you the following output. This is truncated but you might like to print all 106 symbols and browse through them to see the sort of stuff available. As you can see we also get the Unicode values in decimal and hexadecimal.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dbOf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9befb655-df14-4a5d-ad42-f40e3f81bb38_595x317.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dbOf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9befb655-df14-4a5d-ad42-f40e3f81bb38_595x317.png 424w, https://substackcdn.com/image/fetch/$s_!dbOf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9befb655-df14-4a5d-ad42-f40e3f81bb38_595x317.png 848w, https://substackcdn.com/image/fetch/$s_!dbOf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9befb655-df14-4a5d-ad42-f40e3f81bb38_595x317.png 1272w, https://substackcdn.com/image/fetch/$s_!dbOf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9befb655-df14-4a5d-ad42-f40e3f81bb38_595x317.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dbOf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9befb655-df14-4a5d-ad42-f40e3f81bb38_595x317.png" width="595" height="317" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9befb655-df14-4a5d-ad42-f40e3f81bb38_595x317.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:317,&quot;width&quot;:595,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:75620,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190414404?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9befb655-df14-4a5d-ad42-f40e3f81bb38_595x317.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!dbOf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9befb655-df14-4a5d-ad42-f40e3f81bb38_595x317.png 424w, https://substackcdn.com/image/fetch/$s_!dbOf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9befb655-df14-4a5d-ad42-f40e3f81bb38_595x317.png 848w, https://substackcdn.com/image/fetch/$s_!dbOf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9befb655-df14-4a5d-ad42-f40e3f81bb38_595x317.png 1272w, https://substackcdn.com/image/fetch/$s_!dbOf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9befb655-df14-4a5d-ad42-f40e3f81bb38_595x317.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Uncomment the next two lines which search for any symbols containing "root" and print the result. Running the program again gives us this output.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SLRb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43642cb-fcee-4548-85b0-f66f06afeba8_596x60.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SLRb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43642cb-fcee-4548-85b0-f66f06afeba8_596x60.png 424w, https://substackcdn.com/image/fetch/$s_!SLRb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43642cb-fcee-4548-85b0-f66f06afeba8_596x60.png 848w, https://substackcdn.com/image/fetch/$s_!SLRb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43642cb-fcee-4548-85b0-f66f06afeba8_596x60.png 1272w, https://substackcdn.com/image/fetch/$s_!SLRb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43642cb-fcee-4548-85b0-f66f06afeba8_596x60.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SLRb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43642cb-fcee-4548-85b0-f66f06afeba8_596x60.png" width="596" height="60" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b43642cb-fcee-4548-85b0-f66f06afeba8_596x60.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:60,&quot;width&quot;:596,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:16000,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190414404?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43642cb-fcee-4548-85b0-f66f06afeba8_596x60.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!SLRb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43642cb-fcee-4548-85b0-f66f06afeba8_596x60.png 424w, https://substackcdn.com/image/fetch/$s_!SLRb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43642cb-fcee-4548-85b0-f66f06afeba8_596x60.png 848w, https://substackcdn.com/image/fetch/$s_!SLRb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43642cb-fcee-4548-85b0-f66f06afeba8_596x60.png 1272w, https://substackcdn.com/image/fetch/$s_!SLRb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb43642cb-fcee-4548-85b0-f66f06afeba8_596x60.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Uncomment the rest of the code (be careful not to uncomment the actual comments!) which shows symbols printed using the following four methods:</p><ul><li><p>Accessing the dictionary value using the key</p></li><li><p>Copying/pasting symbols from the result of using the search function</p></li><li><p>Using the decimal code and the chr function</p></li><li><p>Using the hexadecimal code prefixed with <strong>\u</strong></p></li></ul><p>I can&#8217;t see any reason to use either decimal or hexadecimal in Python, but would suggest the ideal usage would be to copy/paste any well known symbols into code, and use the dictionary key for more obscure ones. The latter option makes code self-documenting although admittedly rather long. Ultimately it&#8217;s a personal choice based on who is likely to read or maintain your code.</p><p>After you have uncommented the last lines of code run the program one more time.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CZ8u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16a0de2d-1825-4fba-8b9a-c3b388a8e594_651x76.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CZ8u!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16a0de2d-1825-4fba-8b9a-c3b388a8e594_651x76.png 424w, https://substackcdn.com/image/fetch/$s_!CZ8u!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16a0de2d-1825-4fba-8b9a-c3b388a8e594_651x76.png 848w, https://substackcdn.com/image/fetch/$s_!CZ8u!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16a0de2d-1825-4fba-8b9a-c3b388a8e594_651x76.png 1272w, https://substackcdn.com/image/fetch/$s_!CZ8u!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16a0de2d-1825-4fba-8b9a-c3b388a8e594_651x76.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CZ8u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16a0de2d-1825-4fba-8b9a-c3b388a8e594_651x76.png" width="651" height="76" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/16a0de2d-1825-4fba-8b9a-c3b388a8e594_651x76.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:76,&quot;width&quot;:651,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:9420,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190414404?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16a0de2d-1825-4fba-8b9a-c3b388a8e594_651x76.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CZ8u!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16a0de2d-1825-4fba-8b9a-c3b388a8e594_651x76.png 424w, https://substackcdn.com/image/fetch/$s_!CZ8u!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16a0de2d-1825-4fba-8b9a-c3b388a8e594_651x76.png 848w, https://substackcdn.com/image/fetch/$s_!CZ8u!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16a0de2d-1825-4fba-8b9a-c3b388a8e594_651x76.png 1272w, https://substackcdn.com/image/fetch/$s_!CZ8u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16a0de2d-1825-4fba-8b9a-c3b388a8e594_651x76.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>As I mentioned above, if you know of any Unicode characters of a generally mathematical nature which I have missed please let me know.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/mathematical-symbols-in-python/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/mathematical-symbols-in-python/comments"><span>Leave a comment</span></a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p><p></p><p></p>]]></content:encoded></item><item><title><![CDATA[Pascal’s Triangle in Python]]></title><description><![CDATA[The numbers in the graphic below form the first five rows of Pascal's Triangle, which in this post I will implement in Python.]]></description><link>https://codedrome.substack.com/p/pascals-triangle-in-python</link><guid isPermaLink="false">https://codedrome.substack.com/p/pascals-triangle-in-python</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 17 Mar 2026 16:56:35 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!rs2e!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ddcb0b8-e978-4c3d-8961-2eb6b40285a7_600x600.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rs2e!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ddcb0b8-e978-4c3d-8961-2eb6b40285a7_600x600.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rs2e!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ddcb0b8-e978-4c3d-8961-2eb6b40285a7_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!rs2e!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ddcb0b8-e978-4c3d-8961-2eb6b40285a7_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!rs2e!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ddcb0b8-e978-4c3d-8961-2eb6b40285a7_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!rs2e!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ddcb0b8-e978-4c3d-8961-2eb6b40285a7_600x600.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rs2e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ddcb0b8-e978-4c3d-8961-2eb6b40285a7_600x600.jpeg" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8ddcb0b8-e978-4c3d-8961-2eb6b40285a7_600x600.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:96886,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190010938?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ddcb0b8-e978-4c3d-8961-2eb6b40285a7_600x600.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rs2e!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ddcb0b8-e978-4c3d-8961-2eb6b40285a7_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!rs2e!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ddcb0b8-e978-4c3d-8961-2eb6b40285a7_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!rs2e!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ddcb0b8-e978-4c3d-8961-2eb6b40285a7_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!rs2e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ddcb0b8-e978-4c3d-8961-2eb6b40285a7_600x600.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Blaise Pascal. image: <a href="https://commons.wikimedia.org/wiki/File:Blaise_Pascal_Versailles.JPG">Wikimedia</a></figcaption></figure></div><p>The numbers in the graphic below form the first five rows of Pascal's Triangle, which in this post I will implement in Python. The first row consists of a single number 1. In subsequent rows, each of which is has one more number than the previous, values are calculated by adding the two numbers above left and above right. For the first and last values in each row we just take the single value above, therefore these are always 1.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZC8R!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ed444f-9eb2-4d32-8e4a-390d46968162_258x145.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZC8R!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ed444f-9eb2-4d32-8e4a-390d46968162_258x145.png 424w, https://substackcdn.com/image/fetch/$s_!ZC8R!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ed444f-9eb2-4d32-8e4a-390d46968162_258x145.png 848w, https://substackcdn.com/image/fetch/$s_!ZC8R!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ed444f-9eb2-4d32-8e4a-390d46968162_258x145.png 1272w, https://substackcdn.com/image/fetch/$s_!ZC8R!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ed444f-9eb2-4d32-8e4a-390d46968162_258x145.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZC8R!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ed444f-9eb2-4d32-8e4a-390d46968162_258x145.png" width="258" height="145" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/36ed444f-9eb2-4d32-8e4a-390d46968162_258x145.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:145,&quot;width&quot;:258,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2318,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190010938?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ed444f-9eb2-4d32-8e4a-390d46968162_258x145.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ZC8R!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ed444f-9eb2-4d32-8e4a-390d46968162_258x145.png 424w, https://substackcdn.com/image/fetch/$s_!ZC8R!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ed444f-9eb2-4d32-8e4a-390d46968162_258x145.png 848w, https://substackcdn.com/image/fetch/$s_!ZC8R!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ed444f-9eb2-4d32-8e4a-390d46968162_258x145.png 1272w, https://substackcdn.com/image/fetch/$s_!ZC8R!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ed444f-9eb2-4d32-8e4a-390d46968162_258x145.png 1456w" sizes="100vw"></picture><div></div></div></a><figcaption class="image-caption"><em>Pascal&#8217;s Triangle in its conventional centred layout</em></figcaption></figure></div><p>At first glance you might think Pascal&#8217;s Triangle is of little interest or use other than perhaps teaching very young children to add up. Nothing could be further from the truth - as with so many areas of mathematics this simple idea has spawned a large area of study and overlaps with even more. The <a href="https://en.wikipedia.org/wiki/Pascal%27s_triangle">Wikipedia article</a> on the subject is fairly comprehensive but I am writing this article as a precursor to another on the subject of a contraption called a Galton Board, which takes us into the area of statistics and specifically the normal distribution. If you look at the last row of numbers you might be able to discern the first inklings of a normal distribution.</p><p>To that end, note that each number represents the number of possible paths to it from the top, assuming we travel downwards and either to the left or right. For example, the second number in the third row is 3, and there are three possible paths to it from the top:</p><ul><li><p>left, left, right</p></li><li><p>left, right, left</p></li><li><p>right, left, left</p></li></ul><h2>The Data Structure</h2><p>Let&#8217;s start of by considering the kind of data structure we need to represent Pascal&#8217;s Triangle. For this purpose it might be simpler to show it left-aligned rather than centred.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2XJT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe54ea18e-0244-4855-9fb4-291a22cdafbc_323x178.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2XJT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe54ea18e-0244-4855-9fb4-291a22cdafbc_323x178.png 424w, https://substackcdn.com/image/fetch/$s_!2XJT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe54ea18e-0244-4855-9fb4-291a22cdafbc_323x178.png 848w, https://substackcdn.com/image/fetch/$s_!2XJT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe54ea18e-0244-4855-9fb4-291a22cdafbc_323x178.png 1272w, https://substackcdn.com/image/fetch/$s_!2XJT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe54ea18e-0244-4855-9fb4-291a22cdafbc_323x178.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2XJT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe54ea18e-0244-4855-9fb4-291a22cdafbc_323x178.png" width="323" height="178" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e54ea18e-0244-4855-9fb4-291a22cdafbc_323x178.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:178,&quot;width&quot;:323,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:5487,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190010938?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe54ea18e-0244-4855-9fb4-291a22cdafbc_323x178.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!2XJT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe54ea18e-0244-4855-9fb4-291a22cdafbc_323x178.png 424w, https://substackcdn.com/image/fetch/$s_!2XJT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe54ea18e-0244-4855-9fb4-291a22cdafbc_323x178.png 848w, https://substackcdn.com/image/fetch/$s_!2XJT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe54ea18e-0244-4855-9fb4-291a22cdafbc_323x178.png 1272w, https://substackcdn.com/image/fetch/$s_!2XJT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe54ea18e-0244-4855-9fb4-291a22cdafbc_323x178.png 1456w" sizes="100vw"></picture><div></div></div></a><figcaption class="image-caption"><em>Pascal&#8217;s Triangle in a left aligned form</em></figcaption></figure></div><p>Looking at the layout above it becomes obvious that what we need is a list of lists. In a Pascal&#8217;s Triangle the rows and columns are numbered from 0 just like a Python list so we don&#8217;t even have to bother about adding or subtracting 1.</p><h2>Program Requirements</h2><p>The purpose of this program is simply to print out Pascal&#8217;s Triangle to the number of rows which will be specified as a function argument. Calculating values doesn&#8217;t present any obvious problems as it is simply the addition of two numbers. However, calculating the indexes of the two numbers in the previous row to add is fiddly so I&#8217;ll use the following formula, where n is the row number and k is the column number, both 0-based:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SAc8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc96ccd49-bfd6-4c22-8ec1-90b9f9e94ad8_313x48.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SAc8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc96ccd49-bfd6-4c22-8ec1-90b9f9e94ad8_313x48.png 424w, https://substackcdn.com/image/fetch/$s_!SAc8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc96ccd49-bfd6-4c22-8ec1-90b9f9e94ad8_313x48.png 848w, https://substackcdn.com/image/fetch/$s_!SAc8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc96ccd49-bfd6-4c22-8ec1-90b9f9e94ad8_313x48.png 1272w, https://substackcdn.com/image/fetch/$s_!SAc8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc96ccd49-bfd6-4c22-8ec1-90b9f9e94ad8_313x48.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SAc8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc96ccd49-bfd6-4c22-8ec1-90b9f9e94ad8_313x48.png" width="313" height="48" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c96ccd49-bfd6-4c22-8ec1-90b9f9e94ad8_313x48.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:48,&quot;width&quot;:313,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3536,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190010938?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc96ccd49-bfd6-4c22-8ec1-90b9f9e94ad8_313x48.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!SAc8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc96ccd49-bfd6-4c22-8ec1-90b9f9e94ad8_313x48.png 424w, https://substackcdn.com/image/fetch/$s_!SAc8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc96ccd49-bfd6-4c22-8ec1-90b9f9e94ad8_313x48.png 848w, https://substackcdn.com/image/fetch/$s_!SAc8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc96ccd49-bfd6-4c22-8ec1-90b9f9e94ad8_313x48.png 1272w, https://substackcdn.com/image/fetch/$s_!SAc8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc96ccd49-bfd6-4c22-8ec1-90b9f9e94ad8_313x48.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h2>The Code</h2><p>This project consists of a single file called <em>pascalstriangle.py</em> which you can clone or download from the <a href="https://github.com/CodeDrome/pascals-triangle-python">GitHub repository</a>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VI97!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac50a49f-bf33-4c10-862a-888508ed7414_1486x3080.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VI97!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac50a49f-bf33-4c10-862a-888508ed7414_1486x3080.png 424w, https://substackcdn.com/image/fetch/$s_!VI97!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac50a49f-bf33-4c10-862a-888508ed7414_1486x3080.png 848w, https://substackcdn.com/image/fetch/$s_!VI97!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac50a49f-bf33-4c10-862a-888508ed7414_1486x3080.png 1272w, https://substackcdn.com/image/fetch/$s_!VI97!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac50a49f-bf33-4c10-862a-888508ed7414_1486x3080.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VI97!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac50a49f-bf33-4c10-862a-888508ed7414_1486x3080.png" width="1486" height="3080" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ac50a49f-bf33-4c10-862a-888508ed7414_1486x3080.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:3080,&quot;width&quot;:1486,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:443530,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190010938?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe45d9414-e415-401a-b260-cf2dc3e99e4d_1486x3080.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VI97!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac50a49f-bf33-4c10-862a-888508ed7414_1486x3080.png 424w, https://substackcdn.com/image/fetch/$s_!VI97!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac50a49f-bf33-4c10-862a-888508ed7414_1486x3080.png 848w, https://substackcdn.com/image/fetch/$s_!VI97!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac50a49f-bf33-4c10-862a-888508ed7414_1486x3080.png 1272w, https://substackcdn.com/image/fetch/$s_!VI97!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac50a49f-bf33-4c10-862a-888508ed7414_1486x3080.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The <strong>main</strong> function simply calls the subsequent functions to create, populate and print the Pascal&#8217;s Triangle.</p><p>The <strong>create</strong> function creates a new list and then uses a for loop to add lists of 0s to it. These lists are created by multiplying [0] by the current row number; this is the reason we loop from 1 to <strong>rowcount</strong> + 1 rather than 0 to <strong>rowcount</strong>. Finally we return the list.</p><p>In <strong>populate</strong> we iterate the rows and columns, setting the values according to the formula above.</p><p>The <strong>print_left</strong> function uses similar nested loops to print each row, the <strong>:&lt;-4d</strong> left-aligns the output within four character widths.</p><p>The <strong>print_centre</strong> function uses similar loops but also utilises the inset variable to pad the output at the left with spaces. This is calculated using an ugly formula (which I&#8217;m not proud of but at least it works!) and is reduced by 3 after each row.</p><p>We have now finished the code so can run it using this command in Terminal . . .</p><p><strong>python3 pascalstriangle.py</strong></p><p>. . . which will give you the following output.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!y9WX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7e777b0-8495-4ec4-8fd3-be43f466f103_364x297.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!y9WX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7e777b0-8495-4ec4-8fd3-be43f466f103_364x297.png 424w, https://substackcdn.com/image/fetch/$s_!y9WX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7e777b0-8495-4ec4-8fd3-be43f466f103_364x297.png 848w, https://substackcdn.com/image/fetch/$s_!y9WX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7e777b0-8495-4ec4-8fd3-be43f466f103_364x297.png 1272w, https://substackcdn.com/image/fetch/$s_!y9WX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7e777b0-8495-4ec4-8fd3-be43f466f103_364x297.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!y9WX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7e777b0-8495-4ec4-8fd3-be43f466f103_364x297.png" width="364" height="297" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f7e777b0-8495-4ec4-8fd3-be43f466f103_364x297.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:297,&quot;width&quot;:364,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:11789,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/190010938?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7e777b0-8495-4ec4-8fd3-be43f466f103_364x297.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!y9WX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7e777b0-8495-4ec4-8fd3-be43f466f103_364x297.png 424w, https://substackcdn.com/image/fetch/$s_!y9WX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7e777b0-8495-4ec4-8fd3-be43f466f103_364x297.png 848w, https://substackcdn.com/image/fetch/$s_!y9WX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7e777b0-8495-4ec4-8fd3-be43f466f103_364x297.png 1272w, https://substackcdn.com/image/fetch/$s_!y9WX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7e777b0-8495-4ec4-8fd3-be43f466f103_364x297.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You can of course change the number of lines to another value if you wish.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/pascals-triangle-in-python/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/pascals-triangle-in-python/comments"><span>Leave a comment</span></a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p>]]></content:encoded></item><item><title><![CDATA[Memoization of Factorials in Python]]></title><description><![CDATA[I am currently working on an article about calculating sines and cosines using Taylor Polynomials.]]></description><link>https://codedrome.substack.com/p/memoization-of-factorials-in-python</link><guid isPermaLink="false">https://codedrome.substack.com/p/memoization-of-factorials-in-python</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 10 Mar 2026 13:53:38 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!TCTh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa78b35cb-ac30-42ac-8b38-83d0557b9710_600x600.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TCTh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa78b35cb-ac30-42ac-8b38-83d0557b9710_600x600.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TCTh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa78b35cb-ac30-42ac-8b38-83d0557b9710_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!TCTh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa78b35cb-ac30-42ac-8b38-83d0557b9710_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!TCTh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa78b35cb-ac30-42ac-8b38-83d0557b9710_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!TCTh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa78b35cb-ac30-42ac-8b38-83d0557b9710_600x600.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TCTh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa78b35cb-ac30-42ac-8b38-83d0557b9710_600x600.jpeg" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a78b35cb-ac30-42ac-8b38-83d0557b9710_600x600.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:224585,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/185837763?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa78b35cb-ac30-42ac-8b38-83d0557b9710_600x600.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!TCTh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa78b35cb-ac30-42ac-8b38-83d0557b9710_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!TCTh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa78b35cb-ac30-42ac-8b38-83d0557b9710_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!TCTh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa78b35cb-ac30-42ac-8b38-83d0557b9710_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!TCTh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa78b35cb-ac30-42ac-8b38-83d0557b9710_600x600.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">image: <a href="https://pixabay.com/photos/ram-memory-circuits-green-board-921458/">Pixabay</a></figcaption></figure></div><p>I am currently working on an article about calculating sines and cosines using Taylor Polynomials. These make heavy use of factorials so I started thinking about ways to streamline the process.</p><p>Factorials are the product of a given positive integer and all the integers below it down to 1. The notation for factorial is an exclamation mark after the starting or highest integer as illustrated below.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZIFX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138ea71b-9d30-4861-9531-ffeff3304f85_390x38.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZIFX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138ea71b-9d30-4861-9531-ffeff3304f85_390x38.png 424w, https://substackcdn.com/image/fetch/$s_!ZIFX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138ea71b-9d30-4861-9531-ffeff3304f85_390x38.png 848w, https://substackcdn.com/image/fetch/$s_!ZIFX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138ea71b-9d30-4861-9531-ffeff3304f85_390x38.png 1272w, https://substackcdn.com/image/fetch/$s_!ZIFX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138ea71b-9d30-4861-9531-ffeff3304f85_390x38.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZIFX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138ea71b-9d30-4861-9531-ffeff3304f85_390x38.png" width="390" height="38" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/138ea71b-9d30-4861-9531-ffeff3304f85_390x38.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:38,&quot;width&quot;:390,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3627,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/185837763?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138ea71b-9d30-4861-9531-ffeff3304f85_390x38.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ZIFX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138ea71b-9d30-4861-9531-ffeff3304f85_390x38.png 424w, https://substackcdn.com/image/fetch/$s_!ZIFX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138ea71b-9d30-4861-9531-ffeff3304f85_390x38.png 848w, https://substackcdn.com/image/fetch/$s_!ZIFX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138ea71b-9d30-4861-9531-ffeff3304f85_390x38.png 1272w, https://substackcdn.com/image/fetch/$s_!ZIFX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F138ea71b-9d30-4861-9531-ffeff3304f85_390x38.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><p>This post consists of a simple project using memoization with a lookup table to pre-calculate factorials and store them for future use.</p><p>Many algorithms and other processes make heavy and repeated use of values which need to be calculated, often in a way which is expensive in terms of memory and/or time. It therefore makes sense to develop some sort of mechanism whereby the values which are or are likely to be needed frequently are calculated only once. The use of factorials in calculating trigonometric functions mentioned above is one example. In a future article I&#8217;ll extend the principle to calculating and storing sines and cosines themselves, but of course there are many other common examples.</p><p>When considering factorials the broad outline of memoization using a lookup table is simple and obvious: just use a list of integers the highest index of which is the highest number we want the factorial of. The factorial of a given number is therefore set and retrieved using the number as the array&#8217;s index.</p><h2>The Code</h2><p>This project consists of a small class implementing memoization of factorials up to a given maximum. Factorials are calculated in <strong>__init__</strong> and stored in a list. There is also a method to get an individual factorial.</p><p>The code consists of the following files which you can find in the <a href="https://github.com/CodeDrome/memoization-factorials-python">Github repository</a>.</p><ul><li><p>factorialsmemoization.py</p></li><li><p>fmdemo.py</p></li></ul><p>This is the code for <em>factorialmemoization.py</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!f-XL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae859e5b-2971-47db-bea7-773509df61bf_1326x1320.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!f-XL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae859e5b-2971-47db-bea7-773509df61bf_1326x1320.png 424w, https://substackcdn.com/image/fetch/$s_!f-XL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae859e5b-2971-47db-bea7-773509df61bf_1326x1320.png 848w, https://substackcdn.com/image/fetch/$s_!f-XL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae859e5b-2971-47db-bea7-773509df61bf_1326x1320.png 1272w, https://substackcdn.com/image/fetch/$s_!f-XL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae859e5b-2971-47db-bea7-773509df61bf_1326x1320.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!f-XL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae859e5b-2971-47db-bea7-773509df61bf_1326x1320.png" width="1326" height="1320" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ae859e5b-2971-47db-bea7-773509df61bf_1326x1320.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1320,&quot;width&quot;:1326,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:226930,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/185837763?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fac6e29-2048-4efa-8b75-8188e3555b66_1326x1320.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!f-XL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae859e5b-2971-47db-bea7-773509df61bf_1326x1320.png 424w, https://substackcdn.com/image/fetch/$s_!f-XL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae859e5b-2971-47db-bea7-773509df61bf_1326x1320.png 848w, https://substackcdn.com/image/fetch/$s_!f-XL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae859e5b-2971-47db-bea7-773509df61bf_1326x1320.png 1272w, https://substackcdn.com/image/fetch/$s_!f-XL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae859e5b-2971-47db-bea7-773509df61bf_1326x1320.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The <strong>__init__</strong> method takes as an argument the maximum number to calculate factorials of. We then create an empty list, store the maximum in <strong>self.memoized_to</strong>, and initialize a variable called <strong>prev</strong> to 1; its purpose will become clear in a moment.</p><p>We then append the factorial of 0 which is 1 to the list before entering a loop to calculate the rest. Note that this loop starts at 1 as we have already set 0. Within the loop we set the next factorial to the previous multiplied by the current number, hence setting <strong>prev</strong> to 1 to start with. We then set <strong>prev</strong> to the current factorial.</p><p>Finally we have the <strong>get</strong> method which checks whether the requested factorial is out of range, raising an exception if so. If <strong>n</strong> is valid it returns the relevant value from the list.</p><p>That&#8217;s <em>factorialmemoization.py</em> finished so we can move on to <em>fmdemo.py</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ImK1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8c2de27-033a-46e8-9ac2-b16f161e9e89_1326x968.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ImK1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8c2de27-033a-46e8-9ac2-b16f161e9e89_1326x968.png 424w, https://substackcdn.com/image/fetch/$s_!ImK1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8c2de27-033a-46e8-9ac2-b16f161e9e89_1326x968.png 848w, https://substackcdn.com/image/fetch/$s_!ImK1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8c2de27-033a-46e8-9ac2-b16f161e9e89_1326x968.png 1272w, https://substackcdn.com/image/fetch/$s_!ImK1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8c2de27-033a-46e8-9ac2-b16f161e9e89_1326x968.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ImK1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8c2de27-033a-46e8-9ac2-b16f161e9e89_1326x968.png" width="1326" height="968" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c8c2de27-033a-46e8-9ac2-b16f161e9e89_1326x968.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:968,&quot;width&quot;:1326,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:138737,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/185837763?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac340978-6819-4121-b2e4-5bd4c692e345_1326x968.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ImK1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8c2de27-033a-46e8-9ac2-b16f161e9e89_1326x968.png 424w, https://substackcdn.com/image/fetch/$s_!ImK1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8c2de27-033a-46e8-9ac2-b16f161e9e89_1326x968.png 848w, https://substackcdn.com/image/fetch/$s_!ImK1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8c2de27-033a-46e8-9ac2-b16f161e9e89_1326x968.png 1272w, https://substackcdn.com/image/fetch/$s_!ImK1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc8c2de27-033a-46e8-9ac2-b16f161e9e89_1326x968.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>After importing the module created above we enter the <strong>main</strong> function which first creates an instance of the <strong>FactorialMemoization</strong> class with <strong>n</strong> set to 8. We then enter a loop to print out factorials of 0 to 10. I have deliberately overrun the range to demonstrate the exception handling.</p><p>Run the program with the following in Terminal...</p><p><strong>python3 fmdemo.py</strong></p><p>...which will give you the following output.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TKXX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2a49324-9f65-43a3-b221-87d7556387e1_395x178.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TKXX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2a49324-9f65-43a3-b221-87d7556387e1_395x178.png 424w, https://substackcdn.com/image/fetch/$s_!TKXX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2a49324-9f65-43a3-b221-87d7556387e1_395x178.png 848w, https://substackcdn.com/image/fetch/$s_!TKXX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2a49324-9f65-43a3-b221-87d7556387e1_395x178.png 1272w, https://substackcdn.com/image/fetch/$s_!TKXX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2a49324-9f65-43a3-b221-87d7556387e1_395x178.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TKXX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2a49324-9f65-43a3-b221-87d7556387e1_395x178.png" width="395" height="178" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d2a49324-9f65-43a3-b221-87d7556387e1_395x178.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:178,&quot;width&quot;:395,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:12627,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/185837763?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2a49324-9f65-43a3-b221-87d7556387e1_395x178.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!TKXX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2a49324-9f65-43a3-b221-87d7556387e1_395x178.png 424w, https://substackcdn.com/image/fetch/$s_!TKXX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2a49324-9f65-43a3-b221-87d7556387e1_395x178.png 848w, https://substackcdn.com/image/fetch/$s_!TKXX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2a49324-9f65-43a3-b221-87d7556387e1_395x178.png 1272w, https://substackcdn.com/image/fetch/$s_!TKXX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2a49324-9f65-43a3-b221-87d7556387e1_395x178.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>As you can see we get the correct factorials up to 8 but then hit an exception with 9 which is beyond the calculated range.</p><p>The approach I have used here is to pre-calculate a range of values. This is reasonable in situations where we know of think we&#8217;ll need a specific range of values. In situations where we cannot know or reasonably assume a certain range a completely different methodology would be more appropriate: calculate values as they are needed and add them to a suitable data structure. When subsequent requests for values are made check whether they exist in the data structure and if so return the required value; if not calculate it and add it to the data structure for possible further use.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/memoization-of-factorials-in-python/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/memoization-of-factorials-in-python/comments"><span>Leave a comment</span></a></p><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p>]]></content:encoded></item><item><title><![CDATA[Calculating Areas of Quadrilaterals from Coordinates in Python]]></title><description><![CDATA[In this, the last of my trilogy on calculating the areas of quadrilaterals, I will demonstrate how to use the coordinates of the four corners.]]></description><link>https://codedrome.substack.com/p/calculating-areas-of-quadrilaterals-from-coordinates-python</link><guid isPermaLink="false">https://codedrome.substack.com/p/calculating-areas-of-quadrilaterals-from-coordinates-python</guid><dc:creator><![CDATA[Chris Webb]]></dc:creator><pubDate>Tue, 03 Mar 2026 14:13:29 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!4J76!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6f8b7f2-6d43-4ddd-adcb-c88005f7d392_600x600.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4J76!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6f8b7f2-6d43-4ddd-adcb-c88005f7d392_600x600.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4J76!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6f8b7f2-6d43-4ddd-adcb-c88005f7d392_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!4J76!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6f8b7f2-6d43-4ddd-adcb-c88005f7d392_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!4J76!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6f8b7f2-6d43-4ddd-adcb-c88005f7d392_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!4J76!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6f8b7f2-6d43-4ddd-adcb-c88005f7d392_600x600.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4J76!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6f8b7f2-6d43-4ddd-adcb-c88005f7d392_600x600.jpeg" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b6f8b7f2-6d43-4ddd-adcb-c88005f7d392_600x600.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:176762,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/185182313?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6f8b7f2-6d43-4ddd-adcb-c88005f7d392_600x600.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4J76!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6f8b7f2-6d43-4ddd-adcb-c88005f7d392_600x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!4J76!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6f8b7f2-6d43-4ddd-adcb-c88005f7d392_600x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!4J76!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6f8b7f2-6d43-4ddd-adcb-c88005f7d392_600x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!4J76!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6f8b7f2-6d43-4ddd-adcb-c88005f7d392_600x600.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In this, the last of my trilogy on calculating the areas of quadrilaterals, I will demonstrate how to use the coordinates of the four corners.</p><h2>Example Quadrilaterals</h2><p>The diagrams below show the two quadrilaterals I will be using as examples. The corners or vertices are numbered 1-4, and their <em>x,y</em> coordinates are also shown. The quadrilaterals are identical (congruent) and only differ in their coordinates.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!08F2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fcf2eb9-aa33-44d1-ab48-1d8b68e7645e_600x600.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!08F2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fcf2eb9-aa33-44d1-ab48-1d8b68e7645e_600x600.png 424w, https://substackcdn.com/image/fetch/$s_!08F2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fcf2eb9-aa33-44d1-ab48-1d8b68e7645e_600x600.png 848w, https://substackcdn.com/image/fetch/$s_!08F2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fcf2eb9-aa33-44d1-ab48-1d8b68e7645e_600x600.png 1272w, https://substackcdn.com/image/fetch/$s_!08F2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fcf2eb9-aa33-44d1-ab48-1d8b68e7645e_600x600.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!08F2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fcf2eb9-aa33-44d1-ab48-1d8b68e7645e_600x600.png" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3fcf2eb9-aa33-44d1-ab48-1d8b68e7645e_600x600.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:18011,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/185182313?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fcf2eb9-aa33-44d1-ab48-1d8b68e7645e_600x600.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!08F2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fcf2eb9-aa33-44d1-ab48-1d8b68e7645e_600x600.png 424w, https://substackcdn.com/image/fetch/$s_!08F2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fcf2eb9-aa33-44d1-ab48-1d8b68e7645e_600x600.png 848w, https://substackcdn.com/image/fetch/$s_!08F2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fcf2eb9-aa33-44d1-ab48-1d8b68e7645e_600x600.png 1272w, https://substackcdn.com/image/fetch/$s_!08F2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fcf2eb9-aa33-44d1-ab48-1d8b68e7645e_600x600.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vRcU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a112c11-2b6d-457e-9d14-3d092c75e800_600x600.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vRcU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a112c11-2b6d-457e-9d14-3d092c75e800_600x600.png 424w, https://substackcdn.com/image/fetch/$s_!vRcU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a112c11-2b6d-457e-9d14-3d092c75e800_600x600.png 848w, https://substackcdn.com/image/fetch/$s_!vRcU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a112c11-2b6d-457e-9d14-3d092c75e800_600x600.png 1272w, https://substackcdn.com/image/fetch/$s_!vRcU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a112c11-2b6d-457e-9d14-3d092c75e800_600x600.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vRcU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a112c11-2b6d-457e-9d14-3d092c75e800_600x600.png" width="600" height="600" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8a112c11-2b6d-457e-9d14-3d092c75e800_600x600.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:600,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:18637,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/185182313?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a112c11-2b6d-457e-9d14-3d092c75e800_600x600.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!vRcU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a112c11-2b6d-457e-9d14-3d092c75e800_600x600.png 424w, https://substackcdn.com/image/fetch/$s_!vRcU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a112c11-2b6d-457e-9d14-3d092c75e800_600x600.png 848w, https://substackcdn.com/image/fetch/$s_!vRcU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a112c11-2b6d-457e-9d14-3d092c75e800_600x600.png 1272w, https://substackcdn.com/image/fetch/$s_!vRcU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a112c11-2b6d-457e-9d14-3d092c75e800_600x600.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>The Formula</h2><p>This is the formula for calculating the area of a quadrilateral from the <em>x,y</em> coordinates of its corners. The numbers 1-4 correspond to the corner numbering in the diagrams, and the x and y refer to the horizontal and vertical coordinates respectively.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BW3V!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08db8d20-10c9-427e-8186-ca5019335797_800x254.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BW3V!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08db8d20-10c9-427e-8186-ca5019335797_800x254.jpeg 424w, https://substackcdn.com/image/fetch/$s_!BW3V!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08db8d20-10c9-427e-8186-ca5019335797_800x254.jpeg 848w, https://substackcdn.com/image/fetch/$s_!BW3V!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08db8d20-10c9-427e-8186-ca5019335797_800x254.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!BW3V!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08db8d20-10c9-427e-8186-ca5019335797_800x254.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BW3V!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08db8d20-10c9-427e-8186-ca5019335797_800x254.jpeg" width="800" height="254" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/08db8d20-10c9-427e-8186-ca5019335797_800x254.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:254,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:77738,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/185182313?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08db8d20-10c9-427e-8186-ca5019335797_800x254.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BW3V!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08db8d20-10c9-427e-8186-ca5019335797_800x254.jpeg 424w, https://substackcdn.com/image/fetch/$s_!BW3V!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08db8d20-10c9-427e-8186-ca5019335797_800x254.jpeg 848w, https://substackcdn.com/image/fetch/$s_!BW3V!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08db8d20-10c9-427e-8186-ca5019335797_800x254.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!BW3V!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08db8d20-10c9-427e-8186-ca5019335797_800x254.jpeg 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The formula requires only multiplication, addition and subtraction - basic arithmetic - but the large number of similar terms make the formula difficult to grok*. To make things easier the following diagram shows how the two sets of inner brackets are calculated.</p><p><em>* I am using this word in its original Robert A. Heinlein meaning!</em></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JWIW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf5ed0f1-fd42-49e7-9326-04dd511510b1_800x353.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JWIW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf5ed0f1-fd42-49e7-9326-04dd511510b1_800x353.jpeg 424w, https://substackcdn.com/image/fetch/$s_!JWIW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf5ed0f1-fd42-49e7-9326-04dd511510b1_800x353.jpeg 848w, https://substackcdn.com/image/fetch/$s_!JWIW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf5ed0f1-fd42-49e7-9326-04dd511510b1_800x353.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!JWIW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf5ed0f1-fd42-49e7-9326-04dd511510b1_800x353.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JWIW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf5ed0f1-fd42-49e7-9326-04dd511510b1_800x353.jpeg" width="800" height="353" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bf5ed0f1-fd42-49e7-9326-04dd511510b1_800x353.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:353,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:113888,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/185182313?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf5ed0f1-fd42-49e7-9326-04dd511510b1_800x353.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JWIW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf5ed0f1-fd42-49e7-9326-04dd511510b1_800x353.jpeg 424w, https://substackcdn.com/image/fetch/$s_!JWIW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf5ed0f1-fd42-49e7-9326-04dd511510b1_800x353.jpeg 848w, https://substackcdn.com/image/fetch/$s_!JWIW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf5ed0f1-fd42-49e7-9326-04dd511510b1_800x353.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!JWIW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbf5ed0f1-fd42-49e7-9326-04dd511510b1_800x353.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This is the entire process:</p><ul><li><p>Multiply each of the pairs of numbers connected by green lines, and add up the products</p></li><li><p>Repeat with the numbers connected by red lines</p></li><li><p>Subtract the second result from the first</p></li><li><p>Multiply by 0.5</p></li></ul><h2>The Code</h2><p>This project consists of the following files which you can find in the <a href="https://github.com/CodeDrome/area-quadrilateral-coordinates-python">GitHub repository</a>.</p><ul><li><p>quadrilateralareascoordinates.py</p></li><li><p>qafcdemo.py</p></li></ul><p>This is <em>quadrilateralareascoordinates.py</em> which contains the function to implement the above formula.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FX1K!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa07cb137-4d1b-44b9-a68f-9c4124d7de20_1342x584.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FX1K!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa07cb137-4d1b-44b9-a68f-9c4124d7de20_1342x584.png 424w, https://substackcdn.com/image/fetch/$s_!FX1K!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa07cb137-4d1b-44b9-a68f-9c4124d7de20_1342x584.png 848w, https://substackcdn.com/image/fetch/$s_!FX1K!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa07cb137-4d1b-44b9-a68f-9c4124d7de20_1342x584.png 1272w, https://substackcdn.com/image/fetch/$s_!FX1K!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa07cb137-4d1b-44b9-a68f-9c4124d7de20_1342x584.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FX1K!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa07cb137-4d1b-44b9-a68f-9c4124d7de20_1342x584.png" width="1342" height="584" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a07cb137-4d1b-44b9-a68f-9c4124d7de20_1342x584.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:584,&quot;width&quot;:1342,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:86962,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/185182313?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa07cb137-4d1b-44b9-a68f-9c4124d7de20_1342x584.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!FX1K!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa07cb137-4d1b-44b9-a68f-9c4124d7de20_1342x584.png 424w, https://substackcdn.com/image/fetch/$s_!FX1K!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa07cb137-4d1b-44b9-a68f-9c4124d7de20_1342x584.png 848w, https://substackcdn.com/image/fetch/$s_!FX1K!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa07cb137-4d1b-44b9-a68f-9c4124d7de20_1342x584.png 1272w, https://substackcdn.com/image/fetch/$s_!FX1K!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa07cb137-4d1b-44b9-a68f-9c4124d7de20_1342x584.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>As you can see from the docstring, the single argument to <strong>calculate_area</strong> is a list of lists in this format:</p><p><strong>[[x1,y1],[x2,y2],[x3,y3],[x4,y4]]</strong></p><p>We then carry out the calculations represented by the sets of inner brackets. To obtain individual values we use an expression in the format <strong>c[a][b]</strong> where <strong>a</strong> is the corner (starting from 0 rather than 1 of course), and <strong>b</strong> is 0 for <em>x</em> and 1 for <em>y</em>.</p><p>I don&#8217;t often use single letter variable names but if I had called the argument <strong>coordinates</strong> these two lines of code would have been ridiculously long!</p><p>Lastly we just subtract the second result from the first and multiply by 0.5.</p><p>Now let&#8217;s try the function out on the two example quadrilaterals in <em>qafcdemo.py</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!nMXx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e715b23-441e-4369-b24f-2b2e5ec0fb38_1342x872.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!nMXx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e715b23-441e-4369-b24f-2b2e5ec0fb38_1342x872.png 424w, https://substackcdn.com/image/fetch/$s_!nMXx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e715b23-441e-4369-b24f-2b2e5ec0fb38_1342x872.png 848w, https://substackcdn.com/image/fetch/$s_!nMXx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e715b23-441e-4369-b24f-2b2e5ec0fb38_1342x872.png 1272w, https://substackcdn.com/image/fetch/$s_!nMXx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e715b23-441e-4369-b24f-2b2e5ec0fb38_1342x872.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!nMXx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e715b23-441e-4369-b24f-2b2e5ec0fb38_1342x872.png" width="1342" height="872" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7e715b23-441e-4369-b24f-2b2e5ec0fb38_1342x872.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:872,&quot;width&quot;:1342,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:127707,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/185182313?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e715b23-441e-4369-b24f-2b2e5ec0fb38_1342x872.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!nMXx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e715b23-441e-4369-b24f-2b2e5ec0fb38_1342x872.png 424w, https://substackcdn.com/image/fetch/$s_!nMXx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e715b23-441e-4369-b24f-2b2e5ec0fb38_1342x872.png 848w, https://substackcdn.com/image/fetch/$s_!nMXx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e715b23-441e-4369-b24f-2b2e5ec0fb38_1342x872.png 1272w, https://substackcdn.com/image/fetch/$s_!nMXx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e715b23-441e-4369-b24f-2b2e5ec0fb38_1342x872.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>For each quadrilateral I have created a dictionary containing the coordinates in the required format and also an area set to 0. The areas are then set with calls to <strong>calculate_area</strong>, and finally the dictionary is printed.</p><h2>Running the Program</h2><p>Now we can run the program like this:</p><p><strong>python3 qafcdemo.py</strong></p><p>This is the output.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5nPF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bd7d55a-f113-46d8-83c7-d782e195d0e4_555x60.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5nPF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bd7d55a-f113-46d8-83c7-d782e195d0e4_555x60.png 424w, https://substackcdn.com/image/fetch/$s_!5nPF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bd7d55a-f113-46d8-83c7-d782e195d0e4_555x60.png 848w, https://substackcdn.com/image/fetch/$s_!5nPF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bd7d55a-f113-46d8-83c7-d782e195d0e4_555x60.png 1272w, https://substackcdn.com/image/fetch/$s_!5nPF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bd7d55a-f113-46d8-83c7-d782e195d0e4_555x60.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5nPF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bd7d55a-f113-46d8-83c7-d782e195d0e4_555x60.png" width="555" height="60" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3bd7d55a-f113-46d8-83c7-d782e195d0e4_555x60.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:60,&quot;width&quot;:555,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:12884,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://codedrome.substack.com/i/185182313?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bd7d55a-f113-46d8-83c7-d782e195d0e4_555x60.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5nPF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bd7d55a-f113-46d8-83c7-d782e195d0e4_555x60.png 424w, https://substackcdn.com/image/fetch/$s_!5nPF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bd7d55a-f113-46d8-83c7-d782e195d0e4_555x60.png 848w, https://substackcdn.com/image/fetch/$s_!5nPF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bd7d55a-f113-46d8-83c7-d782e195d0e4_555x60.png 1272w, https://substackcdn.com/image/fetch/$s_!5nPF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bd7d55a-f113-46d8-83c7-d782e195d0e4_555x60.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/p/calculating-areas-of-quadrilaterals-from-coordinates-python/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://codedrome.substack.com/p/calculating-areas-of-quadrilaterals-from-coordinates-python/comments"><span>Leave a comment</span></a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://codedrome.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><em>I publish a new Python programming article each week on science, mathematics and related topics. Please subscribe to get them straight to your inbox, thanks, Chris.</em></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p><em>For updates and random ramblings please follow me on <a href="https://bsky.app/profile/codedrome.bsky.social">Bluesky</a>.</em></p><p><em>No AI was used in creating the code, text or images in this article.</em></p>]]></content:encoded></item></channel></rss>