<?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[Code 85]]></title><description><![CDATA[Plain language programming guides for non-developers]]></description><link>https://code85.substack.com</link><image><url>https://substackcdn.com/image/fetch/$s_!wuyN!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd46fcf58-4b0e-44c6-886c-d617dbab3c64_200x200.png</url><title>Code 85</title><link>https://code85.substack.com</link></image><generator>Substack</generator><lastBuildDate>Wed, 20 May 2026 20:57:15 GMT</lastBuildDate><atom:link href="https://code85.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Jonathan Hsu]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[code85@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[code85@substack.com]]></itunes:email><itunes:name><![CDATA[Jonathan Hsu]]></itunes:name></itunes:owner><itunes:author><![CDATA[Jonathan Hsu]]></itunes:author><googleplay:owner><![CDATA[code85@substack.com]]></googleplay:owner><googleplay:email><![CDATA[code85@substack.com]]></googleplay:email><googleplay:author><![CDATA[Jonathan Hsu]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[A Beginner’s Guide to Importing in Python]]></title><description><![CDATA[Learn to use the &#8216;import&#8217;, &#8216;from&#8217;, and &#8216;as&#8217; commands]]></description><link>https://code85.substack.com/p/a-beginners-guide-to-importing-in</link><guid isPermaLink="false">https://code85.substack.com/p/a-beginners-guide-to-importing-in</guid><dc:creator><![CDATA[Jonathan Hsu]]></dc:creator><pubDate>Mon, 20 Apr 2020 13:21:49 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!vVIb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe092399a-fa06-47e7-b605-ddc5805bedfb_2000x1332.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vVIb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe092399a-fa06-47e7-b605-ddc5805bedfb_2000x1332.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vVIb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe092399a-fa06-47e7-b605-ddc5805bedfb_2000x1332.jpeg 424w, https://substackcdn.com/image/fetch/$s_!vVIb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe092399a-fa06-47e7-b605-ddc5805bedfb_2000x1332.jpeg 848w, https://substackcdn.com/image/fetch/$s_!vVIb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe092399a-fa06-47e7-b605-ddc5805bedfb_2000x1332.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!vVIb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe092399a-fa06-47e7-b605-ddc5805bedfb_2000x1332.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vVIb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe092399a-fa06-47e7-b605-ddc5805bedfb_2000x1332.jpeg" width="1456" height="970" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/e092399a-fa06-47e7-b605-ddc5805bedfb_2000x1332.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:970,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:534429,&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;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!vVIb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe092399a-fa06-47e7-b605-ddc5805bedfb_2000x1332.jpeg 424w, https://substackcdn.com/image/fetch/$s_!vVIb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe092399a-fa06-47e7-b605-ddc5805bedfb_2000x1332.jpeg 848w, https://substackcdn.com/image/fetch/$s_!vVIb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe092399a-fa06-47e7-b605-ddc5805bedfb_2000x1332.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!vVIb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe092399a-fa06-47e7-b605-ddc5805bedfb_2000x1332.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><p></p><p>Importing modules is a critical skill when learning Python. To keep the language lightweight and fast, only a small core is available by default in any given Python script &#8212; you as the author are expected to then import whatever else is needed.</p><p>There are three main commands when adding modules to your Python code:&nbsp;<em>import</em>,&nbsp;<em>from</em>, and&nbsp;<em>as</em>.</p><p>We&#8217;ll go over each of these, but first let&#8217;s define what it is we&#8217;re actually importing.</p><h1>What Can You Import?</h1><p>The terminology can get confusing, especially when learning from multiple sources, so here&#8217;s a quick list of what can be imported and what they&#8217;re called.</p><ul><li><p><strong>Module</strong>: simply a file with a .py extension.</p></li><li><p><strong>Package</strong>: a directory containing an __init__.py file and normally other modules.</p></li><li><p><strong>Built-in Module</strong>: A module that is natively installed with Python.</p></li><li><p><strong>Object</strong>: Anything inside a module/package that can be referenced such as a class, function, or variable.</p></li></ul><p>Don&#8217;t get too caught up in the terminology. Just know that we&#8217;ll predominately be importing built-in modules or downloaded packages. Since&nbsp;<em>object</em>&nbsp;is such a multipurpose term, I&#8217;ll refer to them as&nbsp;<em>components</em>.</p><p>To download and install packages, use&nbsp;<em>pip</em>. This command line tool installs publicly available Python packages. For the examples below, we&#8217;ll only use built-in modules to ensure you can follow along.</p><h1>How To Import in Python</h1><p>Okay, now for the good stuff.</p><p>As mentioned earlier, there are three commands that can be used when loading modules into your code. Of the three, only&nbsp;<em>import</em>&nbsp;is absolutely required; the other two are optional depending on circumstance.</p><p>We&#8217;ll start with the simplest command, importing an entire module.</p><pre><code>import math
print(math.ceil(3.4)) # 4
print(math.floor(3.4)) # 3</code></pre><p>Just the command import and the module name? Super easy. Remember, if you&#8217;re importing a module that is not native to Python core installation, then ensure it has been properly downloaded.</p><p>Now, while this command is very straightforward, it isn&#8217;t very efficient. We may only need a small portion of the module, in fact that is often the case.</p><p>To be more specific, we&#8217;ll use the&nbsp;<em>from</em>&nbsp;command to specify our module, then the&nbsp;<em>import</em>&nbsp;command to explicitly list the components we need.</p><pre><code>from math import ceil, floor
print(ceil(3.4)) # 4
print(floor(3.4)) # 3</code></pre><p>So now we&#8217;re more efficient in terms of what we&#8217;re importing, but you may want to simplify the reference or provide context to a component&#8230;this is where the&nbsp;<em>as</em>&nbsp;command comes into play.</p><p>To locally rename a component, follow up the import command with&nbsp;<em>as</em>&nbsp;and specify the new name. Now we can more clearly reference the component in our local namespace.</p><pre><code>from datetime import datetime as dt
print(dt.now()) # 2020-03-29 01:43:03.170480</code></pre><h1>Conclusion</h1><p>A well planned import should only bring in what is needed. Taking a few seconds to plan this out will save you time in the long run. Modifying imports for existing code most likely mean updating references and statements in the body of your program as well.</p><p>There is more to mastering Python module imports, but this will get you started and cover 90% of your import commands.</p>]]></content:encoded></item><item><title><![CDATA[How to Export Dictionary Data as CSV in Python]]></title><description><![CDATA[Let the DictWriter class do your heavy lifting]]></description><link>https://code85.substack.com/p/how-to-export-dictionary-data-as</link><guid isPermaLink="false">https://code85.substack.com/p/how-to-export-dictionary-data-as</guid><dc:creator><![CDATA[Jonathan Hsu]]></dc:creator><pubDate>Mon, 20 Apr 2020 01:26:59 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!_PKz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd976638-cec4-442d-b91c-4b6ef4663996_1000x667.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_PKz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd976638-cec4-442d-b91c-4b6ef4663996_1000x667.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_PKz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd976638-cec4-442d-b91c-4b6ef4663996_1000x667.jpeg 424w, https://substackcdn.com/image/fetch/$s_!_PKz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd976638-cec4-442d-b91c-4b6ef4663996_1000x667.jpeg 848w, https://substackcdn.com/image/fetch/$s_!_PKz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd976638-cec4-442d-b91c-4b6ef4663996_1000x667.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!_PKz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd976638-cec4-442d-b91c-4b6ef4663996_1000x667.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_PKz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd976638-cec4-442d-b91c-4b6ef4663996_1000x667.jpeg" width="1000" height="667" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/fd976638-cec4-442d-b91c-4b6ef4663996_1000x667.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:667,&quot;width&quot;:1000,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:134423,&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;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_PKz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd976638-cec4-442d-b91c-4b6ef4663996_1000x667.jpeg 424w, https://substackcdn.com/image/fetch/$s_!_PKz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd976638-cec4-442d-b91c-4b6ef4663996_1000x667.jpeg 848w, https://substackcdn.com/image/fetch/$s_!_PKz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd976638-cec4-442d-b91c-4b6ef4663996_1000x667.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!_PKz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd976638-cec4-442d-b91c-4b6ef4663996_1000x667.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><p>Short for comma-separated values, CSV files are a fading relic of a simpler time. Yet, they are still a necessity and admittedly can be quite convenient.</p><p>The challenge in working with CSV files is that our data is often stored in a different format internally within Python memory. Python dictionaries are one of these common structures.</p><p>It&#8217;s possible to write your own custom CSV export script; however, for simple exports this is not necessary. The&nbsp;<code>csv</code>&nbsp;library that is native to Python and only needs a simple import has a class called DictWriter, which will allow you to export dictionary data to CSV in five lines of code.</p><p>We&#8217;ll start with a working solution and then circle back through all the specifics of using DictWriter.</p><h1>Our Sample Script</h1><p>Let&#8217;s use some data generated from the site&nbsp;<a href="http://mockaroo.com/">Mockaroo</a>. We&#8217;ll use their default setup minus ip address for a total of five fields. After setting the format to JSON and the number of rows to 3, here&#8217;s the output stored to a variable named data:</p><pre><code>data = [{
  "id": 1,
  "first_name": "Giuditta",
  "last_name": "Garland",
  "email": "ggarland0@blogspot.com",
  "gender": "Female"
}, {
  "id": 2,
  "first_name": "Ginger",
  "last_name": "Franzewitch",
  "email": "gfranzewitch1@furl.net",
  "gender": "Male"
}, {
  "id": 3,
  "first_name": "Ellwood",
  "last_name": "Shanahan",
  "email": "eshanahan2@ebay.com",
  "gender": "Male"
}]</code></pre><p>Now, above our variable definition we&#8217;ll add an import statement.  </p><pre><code>import csv</code></pre><p>Below the variable definition we&#8217;ll write the commands to export as CSV.  </p><pre><code>with open("data.csv", "w", newline="") as csv_file:
  cols = ["id","first_name","last_name","email","gender"] 
  writer = csv.DictWriter(csv_file, fieldnames=cols)
  writer.writeheader()
  writer.writerows(data)</code></pre><p>That&#8217;s it. Five lines, as promised. Run the script from your command-line and you should see a file named&nbsp;<code>data.csv</code>&nbsp;in the same directory.</p><h1><strong>Solution Breakdown</strong></h1><p>Okay, let&#8217;s break down exactly what happened with this concise script.</p><blockquote><p><em>Check out&nbsp;<a href="https://medium.com/code-85/a-beginners-guide-to-importing-in-python-bb3adbbacc2b">A Beginner&#8217;s Guide to Importing in Python</a>&nbsp;for more information on importing libraries.</em></p></blockquote><p>Since the solution is so brief, here is a line-by-line breakdown.</p><ol><li><p>We open a file called&nbsp;<code>test.csv</code>&nbsp;with the&nbsp;<code>w</code>&nbsp;attribute for writing and set the newline character equal to an empty string &#8212; this prevents compatibility issues on various operating systems. The created file pointer is stored in the variable&nbsp;<code>csv_file</code>.</p></li><li><p>Next, we set the order that we want the fields exported in our CSV file. In the example, the fields remain in the same order as the dictionary, but they can be rearranged as desired.</p></li><li><p>We create an instance of the DictWriter class and store it in&nbsp;<code>writer</code>. The class requires both a CSV file pointer and our fields passed to the&nbsp;<code>fieldnames</code>&nbsp;attribute.</p></li><li><p>The&nbsp;<code>.writeheader()</code>&nbsp;method takes the list stored in&nbsp;<code>fieldnames</code>&nbsp;and writes it to the CSV file.</p></li><li><p>The&nbsp;<code>.writerows()</code>&nbsp;method takes our variable data and writes each item in the list as a row in the CSV file.</p></li></ol><h1>Frequently Asked Questions &amp; Troubleshooting</h1><p>At this point you should be ready to try out DictWriter on your own. But just in case things don&#8217;t go smoothly, here are the answers to some common pitfalls that you may encounter.</p><h2>What if I miss a key in the fieldnames?</h2><p>Missing a key in the fieldnames list that is passed to DictWriter can result in an exception being thrown. If we take our solution code and remove&nbsp;<code>id</code>&nbsp;from the list, here is what happens:</p><pre><code>with open("data.csv", "w", newline="") as csv_file:
  cols = ["first_name","last_name","email","gender"] 
  writer = csv.DictWriter(csv_file, fieldnames=cols)
  writer.writeheader()
  writer.writerows(data)
# ValueError: dict contains fields not in fieldnames: 'id'</code></pre><p>There are two ways to resolve this. Either add the key to&nbsp;<code>fieldnames&nbsp;</code>or change the behavior by setting the&nbsp;<code>extrasaction</code>value to&nbsp;<code>ignore</code>&nbsp;when creating our DictWriter object.</p><pre><code>with open("data.csv", "w", newline="") as csv_file:
  cols = ["first_name","last_name","email","gender"] 
  writer = csv.DictWriter(csv_file,
                          fieldnames=cols,
                          extrasaction='ignore')
  writer.writeheader()
  writer.writerows(data)</code></pre><h2><strong>What if my dictionary has nested data?</strong></h2><p>When your dictionary has a nested structure, the value is stringified to preserve a flat file structure.</p><p>If we modify our sample data to include a list of job titles and re-run the script, you can expect the following output.</p><pre><code># Sample Data
{
  "id": 3,
  "first_name": "Jon",
  "last_name": "Smith",
  "email": "jsmith@wisc.edu",
  "gender": "Male",
  "jobs": ["Advisor", "Pharmacist"]
}
# Output
id, first_name, last_name, email, gender, jobs
3, Jon, Smith, jsmith@wisc.edu, Male, ["Advisor", "Pharmacist"]</code></pre><h2><strong>What if a field is not in the dictionary?</strong></h2><p>When a key is specified in the&nbsp;<code>fieldnames</code>&nbsp;that does not exist in the dictionary, it will be written as an empty string by default. If you wish to set a different default value, use the&nbsp;<code>restval</code>&nbsp;attribute when creating the DictWriter object.</p><pre><code>writer = csv.DictWriter(csv_file,
                        fieldnames=["id","first_name", "car"],
                        restval='MISSING',
                        extrasaction='ignore')</code></pre><p>The above code snippet asks for the fields&nbsp;<em>id</em>,&nbsp;<em>first_name</em>, and&nbsp;<em>car</em>&nbsp;while setting the&nbsp;<em>restval</em>&nbsp;equal to &#8220;MISSING&#8221;. Let&#8217;s see how our sample data would turn out.</p><pre><code># Sample Data
{
  "id": 3,
  "first_name": "Jon",
  "last_name": "Smith",
  "email": "jsmith@wisc.edu",
  "gender": "Male",
  "jobs": ["Advisor", "Pharmacist"]
}
# Output
id, first_name, car
3, Jon, MISSING</code></pre><div><hr></div><p><em>Thanks for reading. Please share this if you found it helpful and visit our <a href="https://code85.substack.com">archives</a> for more content.</em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://code85.substack.com/p/how-to-export-dictionary-data-as?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://code85.substack.com/p/how-to-export-dictionary-data-as?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p>]]></content:encoded></item><item><title><![CDATA[How To Check for a Substring in JavaScript]]></title><description><![CDATA[Learn to use .indexOf() and .includes()]]></description><link>https://code85.substack.com/p/how-to-check-for-a-substring-in-javascript</link><guid isPermaLink="false">https://code85.substack.com/p/how-to-check-for-a-substring-in-javascript</guid><dc:creator><![CDATA[Jonathan Hsu]]></dc:creator><pubDate>Fri, 17 Apr 2020 14:21:53 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!vEj9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F128ab150-bba5-4bfa-acae-cb3a5d916b21_1400x1050.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vEj9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F128ab150-bba5-4bfa-acae-cb3a5d916b21_1400x1050.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vEj9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F128ab150-bba5-4bfa-acae-cb3a5d916b21_1400x1050.jpeg 424w, https://substackcdn.com/image/fetch/$s_!vEj9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F128ab150-bba5-4bfa-acae-cb3a5d916b21_1400x1050.jpeg 848w, https://substackcdn.com/image/fetch/$s_!vEj9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F128ab150-bba5-4bfa-acae-cb3a5d916b21_1400x1050.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!vEj9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F128ab150-bba5-4bfa-acae-cb3a5d916b21_1400x1050.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vEj9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F128ab150-bba5-4bfa-acae-cb3a5d916b21_1400x1050.jpeg" width="1400" height="1050" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/128ab150-bba5-4bfa-acae-cb3a5d916b21_1400x1050.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1050,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:163591,&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;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!vEj9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F128ab150-bba5-4bfa-acae-cb3a5d916b21_1400x1050.jpeg 424w, https://substackcdn.com/image/fetch/$s_!vEj9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F128ab150-bba5-4bfa-acae-cb3a5d916b21_1400x1050.jpeg 848w, https://substackcdn.com/image/fetch/$s_!vEj9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F128ab150-bba5-4bfa-acae-cb3a5d916b21_1400x1050.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!vEj9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F128ab150-bba5-4bfa-acae-cb3a5d916b21_1400x1050.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><p></p><p>Checking for a substring seems like a fairly straightforward task&#8230;</p><p>Yet, as with many things programming, there are considerations that can easily be overlooked as well as multiple ways to solve the same issue.</p><p>In JavaScript, there are two built-in functions &#8212;&nbsp;<code>.indexOf()</code>&nbsp;and&nbsp;<code>.includes()</code>&nbsp;&#8212; that accomplish the task at hand.</p><div><hr></div><h1><strong>The .indexOf() Method</strong></h1><p>This method is the most universally available (and thus, reliable) technique to search for a substring.</p><p>Using&nbsp;<code>indexOf()</code>&nbsp;is going to return the index where the substring begins. As indexes begin at&nbsp;<code>0</code>, the method will return&nbsp;<code>-1</code>&nbsp;if the substring is not found.</p><p>We can make a very simple function to take this return value and interpret it as a&nbsp;<code>true</code>&nbsp;or&nbsp;<code>false</code>&nbsp;to explicitly answer the question: &#8220;Is the substring present?&#8221;</p><pre><code>function foundSubstring(haystack,needle) {
   if(haystack.indexOf(needle) &gt;= 0) {
      return true;
   } else {
      return false;
}
var text = "Hello World";
console.log(foundSubstring(text,"llo")) // true</code></pre><h1><strong>The .includes() Method</strong></h1><p>This method more directly answers the question as it returns a&nbsp;<code>true</code>&nbsp;or&nbsp;<code>false</code>. The result is an implementation that reads more clearly.</p><pre><code>let text = "Hello World";
console.log(text.includes("llo")); // true</code></pre><p>The challenge with using this method is that it is not universally supported. Released in ECMAScript6,&nbsp;<code>.includes()</code>&nbsp;is widely available but not universally so.  </p>]]></content:encoded></item><item><title><![CDATA[How To Request Command-Line Input in Python]]></title><description><![CDATA[Learn to use input() and avoid common pitfalls]]></description><link>https://code85.substack.com/p/how-to-request-command-line-input</link><guid isPermaLink="false">https://code85.substack.com/p/how-to-request-command-line-input</guid><dc:creator><![CDATA[Jonathan Hsu]]></dc:creator><pubDate>Thu, 16 Apr 2020 22:29:08 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!5yKm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc515dc31-18e6-4d1e-8c1f-bc6d3ad20d3a_2000x1329.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5yKm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc515dc31-18e6-4d1e-8c1f-bc6d3ad20d3a_2000x1329.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5yKm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc515dc31-18e6-4d1e-8c1f-bc6d3ad20d3a_2000x1329.jpeg 424w, https://substackcdn.com/image/fetch/$s_!5yKm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc515dc31-18e6-4d1e-8c1f-bc6d3ad20d3a_2000x1329.jpeg 848w, https://substackcdn.com/image/fetch/$s_!5yKm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc515dc31-18e6-4d1e-8c1f-bc6d3ad20d3a_2000x1329.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!5yKm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc515dc31-18e6-4d1e-8c1f-bc6d3ad20d3a_2000x1329.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5yKm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc515dc31-18e6-4d1e-8c1f-bc6d3ad20d3a_2000x1329.jpeg" width="1456" height="968" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/c515dc31-18e6-4d1e-8c1f-bc6d3ad20d3a_2000x1329.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:968,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:332591,&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;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5yKm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc515dc31-18e6-4d1e-8c1f-bc6d3ad20d3a_2000x1329.jpeg 424w, https://substackcdn.com/image/fetch/$s_!5yKm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc515dc31-18e6-4d1e-8c1f-bc6d3ad20d3a_2000x1329.jpeg 848w, https://substackcdn.com/image/fetch/$s_!5yKm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc515dc31-18e6-4d1e-8c1f-bc6d3ad20d3a_2000x1329.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!5yKm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fc515dc31-18e6-4d1e-8c1f-bc6d3ad20d3a_2000x1329.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><p>If you are writing command-line Python scripts, then you will need to request input from the user at some point. In Python, the mechanism for requesting command-line values is the&nbsp;<code>input()</code>&nbsp;function.</p><p>Let&#8217;s go over how to use&nbsp;<code>input()</code>&nbsp;and make sure you&#8217;re prepared to avoid three common pitfalls.</p><h1>The input() Function</h1><p>Fortunately for new Python enthusiasts, the&nbsp;<code>input()</code>&nbsp;function is straightforward, accepting only one optional argument. The single argument is the text that will be shown on the command-line as the terminal waits for the user&#8217;s input. As the user, you can type in your value and submit by pressing ENTER.</p><pre><code>input("Enter your name: ")</code></pre><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!nXpv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd4371ff-8dc7-4a52-8c99-cc9053a742f7_1352x158.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!nXpv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd4371ff-8dc7-4a52-8c99-cc9053a742f7_1352x158.png 424w, https://substackcdn.com/image/fetch/$s_!nXpv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd4371ff-8dc7-4a52-8c99-cc9053a742f7_1352x158.png 848w, https://substackcdn.com/image/fetch/$s_!nXpv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd4371ff-8dc7-4a52-8c99-cc9053a742f7_1352x158.png 1272w, https://substackcdn.com/image/fetch/$s_!nXpv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd4371ff-8dc7-4a52-8c99-cc9053a742f7_1352x158.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!nXpv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd4371ff-8dc7-4a52-8c99-cc9053a742f7_1352x158.png" width="1352" height="158" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/bd4371ff-8dc7-4a52-8c99-cc9053a742f7_1352x158.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:158,&quot;width&quot;:1352,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:4730,&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;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!nXpv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd4371ff-8dc7-4a52-8c99-cc9053a742f7_1352x158.png 424w, https://substackcdn.com/image/fetch/$s_!nXpv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd4371ff-8dc7-4a52-8c99-cc9053a742f7_1352x158.png 848w, https://substackcdn.com/image/fetch/$s_!nXpv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd4371ff-8dc7-4a52-8c99-cc9053a742f7_1352x158.png 1272w, https://substackcdn.com/image/fetch/$s_!nXpv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd4371ff-8dc7-4a52-8c99-cc9053a742f7_1352x158.png 1456w" sizes="100vw"></picture><div></div></div></a><p>Now, the code we have above is incomplete because we aren&#8217;t storing the user&#8217;s value. We can fix that by assigning the return of&nbsp;<code>input()</code>&nbsp;to a variable.</p><pre><code>name = input("What is your name: ")
print(name)</code></pre><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iKVT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F49774738-d42d-4228-86e2-47124d286a47_1400x183.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iKVT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F49774738-d42d-4228-86e2-47124d286a47_1400x183.png 424w, https://substackcdn.com/image/fetch/$s_!iKVT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F49774738-d42d-4228-86e2-47124d286a47_1400x183.png 848w, https://substackcdn.com/image/fetch/$s_!iKVT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F49774738-d42d-4228-86e2-47124d286a47_1400x183.png 1272w, https://substackcdn.com/image/fetch/$s_!iKVT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F49774738-d42d-4228-86e2-47124d286a47_1400x183.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iKVT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F49774738-d42d-4228-86e2-47124d286a47_1400x183.png" width="1400" height="183" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/49774738-d42d-4228-86e2-47124d286a47_1400x183.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:183,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:18876,&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;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!iKVT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F49774738-d42d-4228-86e2-47124d286a47_1400x183.png 424w, https://substackcdn.com/image/fetch/$s_!iKVT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F49774738-d42d-4228-86e2-47124d286a47_1400x183.png 848w, https://substackcdn.com/image/fetch/$s_!iKVT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F49774738-d42d-4228-86e2-47124d286a47_1400x183.png 1272w, https://substackcdn.com/image/fetch/$s_!iKVT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F49774738-d42d-4228-86e2-47124d286a47_1400x183.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><p>That&#8217;s really all there is to using the&nbsp;<code>input()</code>&nbsp;function. While the function itself is very simple, there is nuance in implementing&nbsp;<code>input()</code>&nbsp;in a script so that it does not become a liability.</p><p>Below are three common pitfalls that new coders fall into. Fortunately for you, we&#8217;ll avoid them by reading ahead.</p><h1><strong>1. Requesting Numeric Input</strong></h1><p>We asked the user for a text value in our earlier example, but what happens when we need a numeric value?</p><pre><code>price = input("Enter the price: ")
total = price * 1.06

print("The total price is", total)</code></pre><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rwEa!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F29ea18cd-66c3-416a-8575-5e34ab747a13_1400x182.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rwEa!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F29ea18cd-66c3-416a-8575-5e34ab747a13_1400x182.png 424w, https://substackcdn.com/image/fetch/$s_!rwEa!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F29ea18cd-66c3-416a-8575-5e34ab747a13_1400x182.png 848w, https://substackcdn.com/image/fetch/$s_!rwEa!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F29ea18cd-66c3-416a-8575-5e34ab747a13_1400x182.png 1272w, https://substackcdn.com/image/fetch/$s_!rwEa!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F29ea18cd-66c3-416a-8575-5e34ab747a13_1400x182.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rwEa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F29ea18cd-66c3-416a-8575-5e34ab747a13_1400x182.png" width="1400" height="182" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/29ea18cd-66c3-416a-8575-5e34ab747a13_1400x182.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:182,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:23271,&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;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rwEa!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F29ea18cd-66c3-416a-8575-5e34ab747a13_1400x182.png 424w, https://substackcdn.com/image/fetch/$s_!rwEa!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F29ea18cd-66c3-416a-8575-5e34ab747a13_1400x182.png 848w, https://substackcdn.com/image/fetch/$s_!rwEa!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F29ea18cd-66c3-416a-8575-5e34ab747a13_1400x182.png 1272w, https://substackcdn.com/image/fetch/$s_!rwEa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F29ea18cd-66c3-416a-8575-5e34ab747a13_1400x182.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><h1><strong>2. User Input Causing Value Errors</strong></h1><p>Setting a proper data type is only one step to properly implementing user input. Let&#8217;s say we want user input but the value that is actually entered causes an error. What do we do?</p><pre><code>total = float(input("How much did you pay? "))
units = int(input("How many did you purchase? "))

print("The unit price is", total/units)</code></pre><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ahHd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4c321f47-61cb-40ec-ba17-71f9cc53257e_1400x306.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ahHd!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4c321f47-61cb-40ec-ba17-71f9cc53257e_1400x306.png 424w, https://substackcdn.com/image/fetch/$s_!ahHd!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4c321f47-61cb-40ec-ba17-71f9cc53257e_1400x306.png 848w, https://substackcdn.com/image/fetch/$s_!ahHd!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4c321f47-61cb-40ec-ba17-71f9cc53257e_1400x306.png 1272w, https://substackcdn.com/image/fetch/$s_!ahHd!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4c321f47-61cb-40ec-ba17-71f9cc53257e_1400x306.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ahHd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4c321f47-61cb-40ec-ba17-71f9cc53257e_1400x306.png" width="1400" height="306" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/4c321f47-61cb-40ec-ba17-71f9cc53257e_1400x306.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:306,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:89665,&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;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ahHd!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4c321f47-61cb-40ec-ba17-71f9cc53257e_1400x306.png 424w, https://substackcdn.com/image/fetch/$s_!ahHd!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4c321f47-61cb-40ec-ba17-71f9cc53257e_1400x306.png 848w, https://substackcdn.com/image/fetch/$s_!ahHd!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4c321f47-61cb-40ec-ba17-71f9cc53257e_1400x306.png 1272w, https://substackcdn.com/image/fetch/$s_!ahHd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F4c321f47-61cb-40ec-ba17-71f9cc53257e_1400x306.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><p>Before dismissing the sequence in the screenshot, always remember how easy it is to make a typing mistake. We strive to code in a way that is error-proof.</p><p>Use&nbsp;<code>try/except</code>&nbsp;statements to solve for erroneous values.</p><pre><code>try:
  total = float(input("How much did you pay? "))
  units = int(input("How many did you purchase? "))
  print("The unit price is", total/units)
except Exception as e:
  print("Error:", e)</code></pre><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!NpBD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F546d6a60-bc0e-44da-985a-6ea59c597062_1400x207.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NpBD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F546d6a60-bc0e-44da-985a-6ea59c597062_1400x207.png 424w, https://substackcdn.com/image/fetch/$s_!NpBD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F546d6a60-bc0e-44da-985a-6ea59c597062_1400x207.png 848w, https://substackcdn.com/image/fetch/$s_!NpBD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F546d6a60-bc0e-44da-985a-6ea59c597062_1400x207.png 1272w, https://substackcdn.com/image/fetch/$s_!NpBD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F546d6a60-bc0e-44da-985a-6ea59c597062_1400x207.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!NpBD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F546d6a60-bc0e-44da-985a-6ea59c597062_1400x207.png" width="1400" height="207" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/546d6a60-bc0e-44da-985a-6ea59c597062_1400x207.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:207,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:40950,&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;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!NpBD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F546d6a60-bc0e-44da-985a-6ea59c597062_1400x207.png 424w, https://substackcdn.com/image/fetch/$s_!NpBD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F546d6a60-bc0e-44da-985a-6ea59c597062_1400x207.png 848w, https://substackcdn.com/image/fetch/$s_!NpBD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F546d6a60-bc0e-44da-985a-6ea59c597062_1400x207.png 1272w, https://substackcdn.com/image/fetch/$s_!NpBD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F546d6a60-bc0e-44da-985a-6ea59c597062_1400x207.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><p>A try/except statement will attempt to execute all commands in the try block. If an exception is encountered, then the except block will be executed. Aside from being able to report on the exception, the biggest advantage here is that your script will not stop executing from the error.</p><h1>3. Continually Requesting User Input</h1><p>We&#8217;ve learned how to convert our user input as well as account for erroneous values. But when we receive a bad input, most likely you&#8217;ll want to re-prompt the user.</p><p>The&nbsp;<code>input()</code>&nbsp;function does not have a built-in &#8220;retry&#8221; mechanism, so we will use a while loop to continually request user input until it is valid. There are many different ways to implement a while loop for continuous user input. We&#8217;ll use an infinite while loop with a break statement as a generic solution.</p><pre><code>total = float(input("How much did you pay? "))
while True:
  try:
    units = int(input("How many did you purchase? "))
    if(units &gt; 0):
      break
    else:
      print("Invalid input")
  except Exception as e:
    print("Error:",e)
print("The unit price is", total/units)</code></pre><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zMR-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F567c952d-e98e-4524-a069-7a348940a18c_1400x280.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zMR-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F567c952d-e98e-4524-a069-7a348940a18c_1400x280.png 424w, https://substackcdn.com/image/fetch/$s_!zMR-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F567c952d-e98e-4524-a069-7a348940a18c_1400x280.png 848w, https://substackcdn.com/image/fetch/$s_!zMR-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F567c952d-e98e-4524-a069-7a348940a18c_1400x280.png 1272w, https://substackcdn.com/image/fetch/$s_!zMR-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F567c952d-e98e-4524-a069-7a348940a18c_1400x280.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!zMR-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F567c952d-e98e-4524-a069-7a348940a18c_1400x280.png" width="1400" height="280" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/567c952d-e98e-4524-a069-7a348940a18c_1400x280.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:280,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:56474,&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;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zMR-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F567c952d-e98e-4524-a069-7a348940a18c_1400x280.png 424w, https://substackcdn.com/image/fetch/$s_!zMR-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F567c952d-e98e-4524-a069-7a348940a18c_1400x280.png 848w, https://substackcdn.com/image/fetch/$s_!zMR-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F567c952d-e98e-4524-a069-7a348940a18c_1400x280.png 1272w, https://substackcdn.com/image/fetch/$s_!zMR-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F567c952d-e98e-4524-a069-7a348940a18c_1400x280.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><p>In the code example above, we&#8216;ve put all our strategies together. We convert our input to an integer, which is wrapped by a try/except which is wrapped by an infinite while loop.</p><div><hr></div><p><em>It is easy to start using the&nbsp;<code>input()</code>&nbsp;function, but there are many potential traps since user input is always uncertain. However, with the three tips we shared, you&#8217;re prepared to venture out into the wild.</em></p>]]></content:encoded></item><item><title><![CDATA[A Five-Minute Introduction to Python’s Style Guide: PEP 8]]></title><description><![CDATA[Readability counts]]></description><link>https://code85.substack.com/p/a-five-minute-introduction-to-pythons</link><guid isPermaLink="false">https://code85.substack.com/p/a-five-minute-introduction-to-pythons</guid><dc:creator><![CDATA[Jonathan Hsu]]></dc:creator><pubDate>Wed, 15 Apr 2020 20:47:59 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!xsxY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5e95fc3e-9c14-4813-9fd7-56ee80b97d3b_3984x2656.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!xsxY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5e95fc3e-9c14-4813-9fd7-56ee80b97d3b_3984x2656.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!xsxY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5e95fc3e-9c14-4813-9fd7-56ee80b97d3b_3984x2656.jpeg 424w, https://substackcdn.com/image/fetch/$s_!xsxY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5e95fc3e-9c14-4813-9fd7-56ee80b97d3b_3984x2656.jpeg 848w, https://substackcdn.com/image/fetch/$s_!xsxY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5e95fc3e-9c14-4813-9fd7-56ee80b97d3b_3984x2656.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!xsxY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5e95fc3e-9c14-4813-9fd7-56ee80b97d3b_3984x2656.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!xsxY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5e95fc3e-9c14-4813-9fd7-56ee80b97d3b_3984x2656.jpeg" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/5e95fc3e-9c14-4813-9fd7-56ee80b97d3b_3984x2656.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:410626,&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;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!xsxY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5e95fc3e-9c14-4813-9fd7-56ee80b97d3b_3984x2656.jpeg 424w, https://substackcdn.com/image/fetch/$s_!xsxY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5e95fc3e-9c14-4813-9fd7-56ee80b97d3b_3984x2656.jpeg 848w, https://substackcdn.com/image/fetch/$s_!xsxY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5e95fc3e-9c14-4813-9fd7-56ee80b97d3b_3984x2656.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!xsxY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5e95fc3e-9c14-4813-9fd7-56ee80b97d3b_3984x2656.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><p>PEP &#8212; short for Python Enhancement Proposal &#8212; is a list of documents that either propose new features or conventions for the language. One of these, PEP 8, is a living document of style conventions for writing Python.</p><p>Before we get too deep, your code will still work if you don&#8217;t adhere to PEP 8 guidelines. However, since the majority of the Python community follows these rules, adopting them will improve your reading speed; plus you should find they make it easier to read your own code.</p><p>Guido van Rossum &#8212; the author of Python &#8212; proposed PEP 8 explicitly to improve the readability of code and create a consistent experience. A key insight of his was that code is read much more often than it is written. Thus, readability counts.</p><p>https://www.python.org/dev/peps/pep-0008</p><p>Below are four quick summaries of PEP 8 topics intended to give you actionable next steps in your Python code.</p><h1>Naming Conventions</h1><p>If you take away nothing else from this guide, the naming conventions you use will play a tremendous role in how well or poorly your code reads. It is also the single easiest improvement you can make.</p><ul><li><p>For variables, use lowercase letters and underscores to separate words.</p></li><li><p>For functions, use lowercase letters and underscores to separate words.</p></li><li><p>For classes, capitalize the first letter of each word &#8212; called CapWords or CamelCase.</p></li><li><p>For constants, use all capital letters and underscores to separate words.</p></li><li><p>Avoid using overly abbreviated names such as&nbsp;<code>fn</code>; write out&nbsp;<code>first_name</code>&nbsp;instead.</p></li><li><p>Never use lowercase &#8216;L&#8217;, uppercase &#8216;O&#8217;, or uppercase &#8216;I&#8217; as variable names, they are too easily confused with a 1 or 0.</p></li></ul><h1><strong>Indentation</strong></h1><p>Since Python uses indentation in lieu of curly braces to denote block ownership, it is critical to have clean and consistent indentation style. Guidelines are not overly strict, often giving multiple options; however, the critical takeaway is to pick a convention and stick with it.</p><blockquote><p><em>PEP 8 clearly puts a stop to the spaces vs tabs argument, recommending four spaces instead of a tab character.</em></p></blockquote><p>Let&#8217;s go through some of the main conventions and their competitors.</p><h2>Opening Delimiter vs Hanging Indent</h2><p>For longer lists of values, either indent based on the opening delimiter or use a hanging indent. When using hanging indents, no values should exist on the first line and the indentation is one level deeper if there is an ensuing block.</p><pre><code># Opening Delimiter
result = my_function(first_argument, second_argument,
                    third_argument, fourth_argument)
# Hanging Indent
result = my_function(
    first_argument, second_argument,
    third_argument, fourth_argument)
def my_function(
        first_argument, second_argument,
        third_argument, fourth_argument):
    print(first_argument)</code></pre><h2>Whitespace or First Character Alignment</h2><p>For multiline constructs, the closing symbol can be aligned with the used whitespace or with the first character of the statement.</p><pre><code># Whitespace alignment
my_list = [
    1, 2, 3,
    4, 5, 6,
    ]

# First Character Alignment
my_list = [
    1, 2, 3,
    4, 5, 6,
]</code></pre><h1><strong>Whitespace and Line Breaks</strong></h1><p>PEP 8 has pretty strict guidelines on when to split long lines and add whitespace. Don&#8217;t stress over being perfect here, there are tools to format your code if/when you get that serious. Use the following as a starting point, but most importantly: stay consistent.</p><ul><li><p>Use two blank lines for top-level function/class definitions and one blank line for method definitions.</p></li><li><p>Optionally use a blank line to separate logical steps in longer sequences.</p></li><li><p>Avoid excessive whitespace immediately inside of parenthesis, brackets, or braces.&nbsp;<code>bad_spacing = { 1 }</code></p></li><li><p>Use whitespace around assignment and logical operators.</p></li><li><p>Use whitespace to communicate order of operations.&nbsp;<code>x = 2*y + 2*z</code></p></li><li><p>Keep lines under 79 characters &#8212; this is intended to minimize line-wrapping on side-by-side windows.</p></li></ul><h1><strong>Single Quotes or Double Quotes</strong></h1><p>Both single and double quotes are used when defining string values. Additionally, triple quotes are used for multiline strings. Python does not advocate for single or double quotes, but does provide usage guidelines.</p><ul><li><p>Use double quotes for strings with an apostrophe.</p></li><li><p>Use single quotes for strings with quotation marks.</p></li><li><p>Always use double quotes for multiline strings.</p></li></ul><div><hr></div><p>Do not let these guidelines get in the way of learning Python. However, just like anything else, it&#8217;s easier to learn something right the first time compared to breaking a bad habit.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://code85.substack.com/p/a-five-minute-introduction-to-pythons/comments&quot;,&quot;text&quot;:&quot;View comments&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://code85.substack.com/p/a-five-minute-introduction-to-pythons/comments"><span>View comments</span></a></p>]]></content:encoded></item><item><title><![CDATA[Welcome to Code 85]]></title><description><![CDATA[Plain language programming guides for non-developers]]></description><link>https://code85.substack.com/p/coming-soon</link><guid isPermaLink="false">https://code85.substack.com/p/coming-soon</guid><pubDate>Sat, 11 Apr 2020 17:44:20 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!wuyN!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd46fcf58-4b0e-44c6-886c-d617dbab3c64_200x200.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Thanks for finding us before the door&#8217;s have been opened!</p><p>We have a passion for introducing people of all ages and backgrounds who want to learn to code.</p><p>Whether you&#8217;re a student, small business owner, or just a curious individual, we don&#8217;t assume you have a math background and computer science degree.</p><p>Sign up now so you don&#8217;t miss the first issue.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://code85.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://code85.substack.com/subscribe?"><span>Subscribe now</span></a></p><p>In the meantime, <a href="https://code85.substack.com/p/coming-soon?utm_source=substack&utm_medium=email&utm_content=share&action=share">tell your friends</a>!</p>]]></content:encoded></item></channel></rss>