<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Michael Morehouse</title>
    <description>The latest articles on DEV Community by Michael Morehouse (@yawpitch).</description>
    <link>https://dev.to/yawpitch</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F136299%2Fad08cd68-43ab-4c50-bcd9-2c80673f411d.png</url>
      <title>DEV Community: Michael Morehouse</title>
      <link>https://dev.to/yawpitch</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yawpitch"/>
    <language>en</language>
    <item>
      <title>The 35 Words You Need to Python</title>
      <dc:creator>Michael Morehouse</dc:creator>
      <pubDate>Sun, 28 Jul 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/yawpitch/the-35-words-you-need-to-python-13pe</link>
      <guid>https://dev.to/yawpitch/the-35-words-you-need-to-python-13pe</guid>
      <description>&lt;h2&gt;
  
  
  In the Beginning were Python's Keywords
&lt;/h2&gt;

&lt;p&gt;Let’s face it, learning to &lt;em&gt;write&lt;/em&gt; computer programs is &lt;em&gt;hard&lt;/em&gt;. Doing so involves picking up a lot of specialized skills and jargon, most of which is unfamiliar, and it also means cultivating a mental approach to the world that, at first, might seem downright &lt;em&gt;alien&lt;/em&gt;. It’s a long journey, but fluency starts with learning how to &lt;em&gt;read&lt;/em&gt; computer programs, and that &lt;em&gt;really&lt;/em&gt; shouldn’t be so hard.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Programs must be written for people to read, and only incidentally for machines to execute.&lt;/p&gt;

&lt;p&gt;&lt;cite&gt;Harold Abelson&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One of the defining traits of the Python programming language is that it’s &lt;em&gt;designed&lt;/em&gt; to be readable by humans. For the most part it fulfills that promise, however, after several years of helping others understand Python, I’ve come to realize that there’s an important caveat to that statement; it’s really only true for someone with a &lt;em&gt;significant&lt;/em&gt; and &lt;em&gt;specialized&lt;/em&gt; English-language vocabulary.&lt;/p&gt;

&lt;p&gt;The truth is you need enough mastery of the “rules” of English grammar and wordplay to recognize and understand, at a glance, words &lt;em&gt;written&lt;/em&gt; in English that &lt;em&gt;are not&lt;/em&gt; actually words you’ll find in an English dictionary. Nonstandard terms like &lt;strong&gt;def&lt;/strong&gt; (a &lt;em&gt;contraction&lt;/em&gt;), &lt;strong&gt;elif&lt;/strong&gt; (a &lt;em&gt;portmanteau&lt;/em&gt;) and &lt;strong&gt;nonlocal&lt;/strong&gt; (a &lt;em&gt;neologism&lt;/em&gt;) all abound, so even for those with quite advanced &lt;em&gt;native&lt;/em&gt; English fluency the task of learning Python is very much like trying to learn a foreign language that’s &lt;em&gt;slightly&lt;/em&gt; related to their native tongue. For those without that fluency learning Python is like learning a foreign language &lt;em&gt;inside&lt;/em&gt; a foreign language, and is therefore &lt;em&gt;far&lt;/em&gt; more difficult.&lt;/p&gt;

&lt;p&gt;To make that task a little easier I’m going to try, in this post and the ones that follow, to shed some light on the meaning of – and a &lt;em&gt;little&lt;/em&gt; of the etymological history behind – the fundamental units of Python fluency. In this first part we will start with the most basic of those units, Python’s 35 &lt;em&gt;keywords&lt;/em&gt;&lt;sup id="fnref1"&gt;1&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;That’s right; the entire vocabulary of Python you actually &lt;em&gt;need&lt;/em&gt; to know to start to do meaningful work is just 35 keywords. It’s not the smallest language, but it’s &lt;em&gt;far&lt;/em&gt; from the largest, and just compare it to the roughly 10,000 words required to achieve basic &lt;em&gt;native&lt;/em&gt; fluency in a non-programming language.&lt;/p&gt;

&lt;h3&gt;
  
  
  First, Some Conventions
&lt;/h3&gt;

&lt;p&gt;Python is what is known as a &lt;em&gt;statement&lt;/em&gt;-oriented language; but what &lt;em&gt;is&lt;/em&gt; a statement? Well, for the purposes of this article we’re just going to say that in Python a statement is a &lt;em&gt;single&lt;/em&gt; line of code that does &lt;em&gt;something&lt;/em&gt;. &lt;em&gt;What&lt;/em&gt; it does, specifically, depends on the building blocks of that statement.&lt;/p&gt;

&lt;p&gt;But what are those building blocks? Well, let’s define them quickly and &lt;em&gt;very&lt;/em&gt; roughly, since we’ll go into more detail about them in later posts. I’ll use UPPERCASE letters to make it easier to visually distinguish these abstract forms from the specific instances we’ll talk about later.&lt;/p&gt;

&lt;dl&gt;
&lt;dt&gt;&lt;strong&gt;KEYWORD&lt;/strong&gt;&lt;/dt&gt;
&lt;dd&gt;A reserved &lt;em&gt;word&lt;/em&gt; the meaning of which &lt;em&gt;cannot&lt;/em&gt; be changed by the user. We will visit all 35 of these in the next section of this article.&lt;/dd&gt;
&lt;/dl&gt;

&lt;dl&gt;
&lt;strong&gt;OPERATOR&lt;/strong&gt;
&lt;dd&gt;A reserved &lt;em&gt;symbol&lt;/em&gt; that indicates an action to be performed. For example, &lt;code&gt;=&lt;/code&gt; is the &lt;em&gt;assignment&lt;/em&gt; OPERATOR and &lt;code&gt;+&lt;/code&gt; is the &lt;em&gt;addition&lt;/em&gt; OPERATOR. There are quite a few others, but we’ll save them for the next post. A small number of KEYWORDs behave like OPERATORs, and I’ll point those out below.&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;These are both provided by Python and you can’t directly change their meaning, which means that they’re somewhat inflexible. To do most work you’ll need something more flexible, which is why Python gives you the ability to represent &lt;em&gt;anything&lt;/em&gt;.&lt;/p&gt;

&lt;dl&gt;
&lt;dt&gt;&lt;strong&gt;OBJECT&lt;/strong&gt;&lt;/dt&gt;
&lt;dd&gt;An individual &lt;em&gt;thing&lt;/em&gt; you can interact with. Unlike KEYWORDs and OPERATORs, you can directly manipulate these, though the degree to which you can manipulate them depends on what &lt;em&gt;type&lt;/em&gt; of OBJECT they are. You can also use KEYWORDs to define entirely new &lt;em&gt;types&lt;/em&gt;, which makes them a very expressive way of building new &lt;em&gt;things&lt;/em&gt; of your own. So expressive, in fact, that practically speaking &lt;em&gt;everything&lt;/em&gt; you interact with in Python will be an OBJECT.&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;That can be a bit abstract and hard to wrap your head around at this point, though. For now just know that OBJECTs tend to fall into three main categories.&lt;/p&gt;

&lt;dl&gt;
&lt;dt&gt;&lt;strong&gt;VALUE&lt;/strong&gt;&lt;/dt&gt;
&lt;dd&gt;An OBJECT that represents a single, concrete &lt;em&gt;thing&lt;/em&gt;; for the purposes of this discussion what that thing actually &lt;em&gt;is&lt;/em&gt; is irrelevant, but as an example, &lt;code&gt;4&lt;/code&gt; is a VALUE of the &lt;a href="https://docs.python.org/3/library/functions.html#int" title="Read about the int type"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/a&gt; (short for &lt;em&gt;integer&lt;/em&gt;) type and &lt;code&gt;hello&lt;/code&gt; is a VALUE of the &lt;a href="https://docs.python.org/3/library/functions.html#func-str" title="Read about the str type"&gt;&lt;strong&gt;str&lt;/strong&gt;&lt;/a&gt; (short for &lt;em&gt;string&lt;/em&gt;) type. These are both examples of &lt;em&gt;primitive&lt;/em&gt; types, which have a single meaningful value, but there are also &lt;em&gt;composite&lt;/em&gt; types for describing things the meaning of which is defined by more than one &lt;em&gt;attribute&lt;/em&gt;. A real-world example would be a rectangle, which cannot be defined without both height and width. As you’ll see below three special KEYWORDs all behave like VALUEs, though as before you cannot change their meaning.&lt;/dd&gt;
&lt;/dl&gt;

&lt;dl&gt;
&lt;strong&gt;COLLECTION&lt;/strong&gt;
&lt;dd&gt;An OBJECT that groups together or contains other OBJECTs; there are &lt;em&gt;many&lt;/em&gt; different types of COLLECTIONs in Python, but for the moment all we care about is that a COLLECTION &lt;em&gt;contains&lt;/em&gt; zero or more OBJECTs. For example the statement &lt;code&gt;[2, 3, 4]&lt;/code&gt; creates a COLLECTION of the type &lt;a href="https://docs.python.org/3/library/stdtypes.html#list" title="Read about the list type"&gt;&lt;strong&gt;list&lt;/strong&gt;&lt;/a&gt; that holds three VALUEs inside of it. A COLLECTION can contain &lt;em&gt;any&lt;/em&gt; OBJECT, so you can &lt;em&gt;nest&lt;/em&gt; a COLLECTION inside another COLLECTION.&lt;/dd&gt;
&lt;/dl&gt;

&lt;dl&gt;
&lt;strong&gt;CALLABLE&lt;/strong&gt;
&lt;dd&gt;An OBJECT that represents some action to perform: it performs that action when you call it with some number of &lt;em&gt;arguments&lt;/em&gt; then it &lt;em&gt;returns&lt;/em&gt; (or gives back) an OBJECT. For instance &lt;a href="https://docs.python.org/3/library/functions.html#sum" title="Read about the sum builtin function"&gt;&lt;strong&gt;sum&lt;/strong&gt;&lt;/a&gt; is a CALLABLE and when we call it using &lt;code&gt;sum([2, 3, 4])&lt;/code&gt; it gives us back the VALUE &lt;code&gt;9&lt;/code&gt;. There are several different kinds of CALLABLE, and we’ll touch on them in more detail below.&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;It wouldn’t be very efficient to type out the same OBJECT every time you needed to refer to it, though. It’s often very helpful to be able to refer to things &lt;em&gt;indirectly&lt;/em&gt;.&lt;/p&gt;

&lt;dl&gt;
&lt;dt&gt;&lt;strong&gt;NAME&lt;/strong&gt;&lt;/dt&gt;
&lt;dd&gt;Any word that &lt;em&gt;is not&lt;/em&gt; a KEYWORD, and that is used as an &lt;em&gt;alias&lt;/em&gt; to &lt;em&gt;refer to&lt;/em&gt; some specific OBJECT. Unlike a KEYWORD the meaning of a NAME &lt;em&gt;may&lt;/em&gt; change over the course of a program, which is why these are often – if a little incorrectly – thought of as &lt;em&gt;variables&lt;/em&gt;. There are several ways to create new NAMEs (and one to destroy them), as we’ll see below, but as a simple example in &lt;code&gt;number = 2&lt;/code&gt; the &lt;em&gt;assignment&lt;/em&gt; OPERATOR &lt;code&gt;=&lt;/code&gt; creates the NAME &lt;strong&gt;number&lt;/strong&gt; and assigns it to refer to the VALUE &lt;strong&gt;2&lt;/strong&gt;. When later that is followed by &lt;code&gt;number += 2&lt;/code&gt;, however, the &lt;em&gt;augmented assignment&lt;/em&gt; OPERATOR &lt;code&gt;+=&lt;/code&gt; will re-assign &lt;strong&gt;number&lt;/strong&gt; to refer to &lt;strong&gt;4&lt;/strong&gt;.&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;Now we’ve got all the simple building blocks defined and we can start organizing them into composite structures.&lt;/p&gt;

&lt;dl&gt;
&lt;dt&gt;&lt;strong&gt;EXPRESSION&lt;/strong&gt;&lt;/dt&gt;
&lt;dd&gt;Any composite form of one or more of the above that can be &lt;em&gt;evaluated&lt;/em&gt; to an OBJECT. For example, &lt;code&gt;4&lt;/code&gt;, &lt;code&gt;2 + 2&lt;/code&gt;, &lt;code&gt;1 * 3 + 1&lt;/code&gt; and &lt;code&gt;sum([1, 1, 1, 1])&lt;/code&gt; are all EXPRESSIONs that evaluate to &lt;code&gt;4&lt;/code&gt;. The EXPRESSION represents the smallest discrete unit of work in Python.&lt;/dd&gt;
&lt;/dl&gt;

&lt;dl&gt;
&lt;strong&gt;STATEMENT&lt;/strong&gt;
&lt;dd&gt;Any single line of code that is composed of at least one of the above. These can get quite complex, but to &lt;em&gt;do&lt;/em&gt; anything they’ll usually need to include KEYWORDs and/or OPERATORs plus EXPRESSIONs. You’ve already met a &lt;em&gt;useful&lt;/em&gt; STATEMENT in &lt;code&gt;number = 2&lt;/code&gt;. If you read each STATEMENT in a program out in turn you can track the program as it does its work.&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;That covers any given &lt;em&gt;line&lt;/em&gt; of code, but there are also a couple of higher level structures we need to define for the moment:&lt;/p&gt;

&lt;dl&gt;
&lt;dt&gt;&lt;strong&gt;BLOCK&lt;/strong&gt;&lt;/dt&gt;
&lt;dd&gt;At least two STATEMENTs that are bound together; the first STATEMENT will end in a &lt;strong&gt;:&lt;/strong&gt; character and indicates the start of the BLOCK. The second and all further STATEMENTs inside that BLOCK will be indented further right than the initial STATEMENT, to indicate that they &lt;em&gt;belong&lt;/em&gt; to the same BLOCK. The last such indented STATEMENT represents the end of the BLOCK.&lt;/dd&gt;
&lt;/dl&gt;

&lt;dl&gt;
&lt;strong&gt;MODULE&lt;/strong&gt;
&lt;dd&gt;A single Python .py file; it’s composed of some number of STATEMENTS. All Python programs are comprised of at least one MODULE. As you’ll see below we write all of our functionality inside MODULEs, and we use KEYWORDs and NAMEs to &lt;em&gt;import&lt;/em&gt; functionality from other MODULEs.&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;There are many other concepts you’ll need to become familiar with, but with these building blocks we can investigate all 35 words in Python’s relatively small vocabulary, and thus understand the skeleton of any Python program.&lt;/p&gt;

&lt;h2&gt;
  
  
  On to the Keywords
&lt;/h2&gt;

&lt;h3&gt;
  
  
  One That Does Nothing
&lt;/h3&gt;

&lt;dl&gt;
&lt;dt id="pass"&gt;
pass &lt;a href="https://docs.python.org/3/tutorial/controlflow.html?highlight=pass#pass-statements" title="See the documentation for “pass”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;A placeholder; technically known as Python’s &lt;em&gt;null operation&lt;/em&gt;, &lt;code&gt;pass&lt;/code&gt;
does nothing whatsoever; it exists solely to allow you to write a syntactically valid BLOCK.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;The general meaning here comes from a Middle English verb borrowed via Old French from the Latin &lt;strong&gt;&lt;em&gt;passus&lt;/em&gt;&lt;/strong&gt; that implies &lt;em&gt;“to move by [a place, without stopping]”&lt;/em&gt;. More specifically the meaning in Python is borrowed from its use in sequential-play card games such as Bridge, where if you do not wish to do anything on your turn you &lt;strong&gt;&lt;em&gt;pass&lt;/em&gt;&lt;/strong&gt; control of the game to the next player, doing nothing.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The need for this is simple; once you begin a BLOCK in Python it &lt;em&gt;must&lt;/em&gt; contain &lt;em&gt;at least one&lt;/em&gt; indented STATEMENT to be considered valid syntax. The &lt;code&gt;pass&lt;/code&gt; statement exists to allow you to write a valid BLOCK structure &lt;em&gt;before&lt;/em&gt; you’re ready to start writing meaningful statements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;STATEMENT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Because it’s mainly used early on when building the rough structure of a program you’ll rarely, if ever, see &lt;code&gt;pass&lt;/code&gt; in working code, but it’s good to know it exists.&lt;/p&gt;

&lt;h3&gt;
  
  
  Three That Are Objects
&lt;/h3&gt;

&lt;p&gt;The next three keywords are specialized because they each behave like a primitive VALUE. This means they can be assigned to a NAME, kept within a COLLECTION, and can be the result of evaluating an EXPRESSION. They’re also the only keywords that start with a capital letter, which makes them easy to distinguish.&lt;/p&gt;

&lt;h4&gt;
  
  
  Boolean values
&lt;/h4&gt;

&lt;p&gt;These two are used in most, if not all, programs, and are essential whenever performing &lt;a href="https://en.wikipedia.org/wiki/Boolean_algebra"&gt;Boolean logic&lt;/a&gt;.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="true"&gt;
True &lt;a href="https://docs.python.org/3.7/library/stdtypes.html#boolean-values" title="See the documentation for “True”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Indicator of logical &lt;em&gt;truth&lt;/em&gt; and the opposite of &lt;code&gt;False&lt;/code&gt;
; behaves like the &lt;em&gt;integer&lt;/em&gt; &lt;code&gt;1&lt;/code&gt;, except that it will always be displayed as &lt;code&gt;True&lt;/code&gt;
.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;From the Old English adjective &lt;strong&gt;&lt;em&gt;triewe&lt;/em&gt;&lt;/strong&gt; , which has German roots; the general meaning is of being “worthy of trust” and “consistent with fact”. In logic, though, the specific meaning is really just “that which is not false”, and in computer programming it’s usually a proxy for the binary digit 1.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;dl&gt;
&lt;dt id="false"&gt;
False &lt;a href="https://docs.python.org/3.7/library/stdtypes.html#boolean-values" title="See the documentation for “False”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Indicator of logical &lt;em&gt;untruth&lt;/em&gt; and the opposite of &lt;code&gt;True&lt;/code&gt;
; behaves like the &lt;em&gt;integer&lt;/em&gt; &lt;code&gt;0&lt;/code&gt;, except that it will always be displayed as &lt;code&gt;False&lt;/code&gt;
.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;From late Old English, borrowed via the Old French &lt;strong&gt;&lt;em&gt;faus&lt;/em&gt;&lt;/strong&gt; from the Latin &lt;strong&gt;&lt;em&gt;falsus&lt;/em&gt;&lt;/strong&gt; ; the general meaning is of “fake, incorrect, mistaken, or deceitful”. In logic the meaning is not so sinister, it just means “that which is not true”, and in computer programming it’s usually a proxy for the binary digit 0.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In some lower-level languages you would probably just use &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;0&lt;/code&gt; for these, but in Python they’ve been given special keywords to make it obvious that you’re working with the &lt;em&gt;logical&lt;/em&gt; meaning instead of the &lt;em&gt;numerical&lt;/em&gt; one.&lt;/p&gt;

&lt;p&gt;It’s important to understand that in Python &lt;em&gt;every&lt;/em&gt; OBJECT (hence ever VALUE and COLLECTION, and therefore every EXPRESSION), has a &lt;em&gt;logical&lt;/em&gt; value, in that it’s considered to be logically equivalent to either True or False. Testing the state of that logical value is known as &lt;a href="https://docs.python.org/3/library/stdtypes.html#truth-value-testing"&gt;truth-value testing&lt;/a&gt;, and as you’ll see below keywords like &lt;code&gt;and&lt;/code&gt;, &lt;code&gt;or&lt;/code&gt;, and &lt;code&gt;if&lt;/code&gt; all rely on truth-value testing for their operation.&lt;/p&gt;

&lt;p&gt;I will go into deeper detail about the specifics of truth-value testing in later articles, but for now you just need to know that most things are considered &lt;code&gt;True&lt;/code&gt; by default, except for “no value” VALUEs like &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;None&lt;/code&gt;, and &lt;code&gt;False&lt;/code&gt; itself, as well as “no content” COLLECTIONs like &lt;code&gt;[]&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  The null value
&lt;/h4&gt;

&lt;p&gt;This is most commonly used to represent the absence of any other VALUE.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="none"&gt;
None &lt;a href="https://docs.python.org/3/library/stdtypes.html#the-null-object" title="See the documentation for “None”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;A special name for nothing whatsoever; technically Python’s &lt;em&gt;null object&lt;/em&gt;; it’s considered equivalent to &lt;code&gt;False&lt;/code&gt;
for truth-value testing, but essentially represents no value at all. Is very commonly used in Python, and will always appear as &lt;code&gt;None&lt;/code&gt;
.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A Middle English pronoun from the Old English &lt;strong&gt;&lt;em&gt;nan&lt;/em&gt;&lt;/strong&gt; , meaning “not one” or “not any”. The meaning in programming, however, relates more to &lt;strong&gt;&lt;em&gt;null&lt;/em&gt;&lt;/strong&gt; , which means “not [any] thing” or just “nothing”. Python chose to use &lt;strong&gt;&lt;em&gt;none&lt;/em&gt;&lt;/strong&gt; because it’s a more commonly familiar word. It also helps to distinguish it from the use of the special value NULL in the C programming language, which has a similar meaning but behaves in a &lt;em&gt;very&lt;/em&gt; different way.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The notion of making &lt;em&gt;something&lt;/em&gt; that explicitly represents &lt;em&gt;nothing&lt;/em&gt; might seem a little odd, at first, but the need for &lt;code&gt;None&lt;/code&gt; becomes obvious when you start building &lt;em&gt;useful&lt;/em&gt; code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Three for Making Decisions
&lt;/h3&gt;

&lt;p&gt;Being able to tell if something is considered &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt; isn’t very useful unless you have the means to take different actions based on that knowledge. For that &lt;em&gt;most&lt;/em&gt; programming languages have some notion of &lt;em&gt;conditional operations&lt;/em&gt;. In Python there are three keywords dedicated to conditional tasks.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="if"&gt;
if &lt;a href="https://docs.python.org/3/reference/compound_stmts.html#the-if-statement" title="See the documentation for “if”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Starts a conditional BLOCK by checking the &lt;em&gt;truth-value&lt;/em&gt; of the EXPRESSION that follows it; the STATEMENT(s) indented underneath the &lt;code&gt;if&lt;/code&gt;
will be executed only if the EXPRESSION is considered &lt;code&gt;True&lt;/code&gt;
.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A Middle English conjunction from the Old English &lt;strong&gt;&lt;em&gt;gif&lt;/em&gt;&lt;/strong&gt; which means, “in the event” or “whether” or, oddly, just “ &lt;strong&gt;&lt;em&gt;if&lt;/em&gt;&lt;/strong&gt; ”. Has many Scandinavian/Germanic relatives, and possibly arrives via an Old Norse term for “doubt, hesitation”. The general use is to make one word or phrase conditional on another being true, as in “&lt;em&gt;if&lt;/em&gt; it is raining, open your umbrella”. The sense in computing is more formal but essentially the same; “&lt;em&gt;if&lt;/em&gt; [this condition is true], then [do some action].&lt;/p&gt;
&lt;/blockquote&gt;

&lt;dl&gt;
&lt;dt id="elif"&gt;elif&lt;/dt&gt;
&lt;dd&gt;Optionally &lt;em&gt;continues&lt;/em&gt; a conditional by adding another BLOCK; if present it &lt;em&gt;must&lt;/em&gt; follow either the initial &lt;code&gt;if&lt;/code&gt;
or another &lt;code&gt;elif&lt;/code&gt;
. Behaves exactly like an &lt;code&gt;if&lt;/code&gt;
, except that its conditional EXPRESSION will only be evaluated when no previous &lt;code&gt;if&lt;/code&gt;
/&lt;code&gt;elif&lt;/code&gt;
STATEMENT has evaluated as &lt;code&gt;True&lt;/code&gt;
.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;Not a proper English word, but instead a &lt;a href="https://en.wikipedia.org/wiki/Portmanteau"&gt;portmanteau&lt;/a&gt; that contracts &lt;strong&gt;&lt;em&gt;else&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;if&lt;/em&gt;&lt;/strong&gt; into a single artificial word, &lt;strong&gt;&lt;em&gt;elif&lt;/em&gt;&lt;/strong&gt;. Together it means “otherwise, if” or “as an alternative, if”, both of which imply that the action controlled by the &lt;strong&gt;&lt;em&gt;elif&lt;/em&gt;&lt;/strong&gt; is contingent on the outcome of some previous test or tests. So, in computing, “ &lt;strong&gt;&lt;em&gt;else&lt;/em&gt;&lt;/strong&gt; [after checking some prior condition] &lt;strong&gt;&lt;em&gt;if&lt;/em&gt;&lt;/strong&gt; [this other condition is True], then [do some other action]“.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;dl&gt;
&lt;dt id="else"&gt;else&lt;/dt&gt;
&lt;dd&gt;Optionally &lt;em&gt;terminates&lt;/em&gt; a conditional by adding a final BLOCK; if present it &lt;em&gt;must&lt;/em&gt; follow the last &lt;code&gt;if&lt;/code&gt;
/&lt;code&gt;elif&lt;/code&gt;
in the BLOCK. If no previous &lt;code&gt;if&lt;/code&gt;
/&lt;code&gt;elif&lt;/code&gt;
STATEMENT evaluated to &lt;code&gt;True&lt;/code&gt;
then the indented STATEMENT(s) below &lt;code&gt;else&lt;/code&gt;
will be run.&lt;/dd&gt;
&lt;dd&gt;Can also be used to terminate blocks started with other KEYWORDs; see &lt;code&gt;for&lt;/code&gt;
, &lt;code&gt;while&lt;/code&gt;
, and &lt;code&gt;try&lt;/code&gt;
below.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;An adverb from the Old English &lt;strong&gt;&lt;em&gt;elles&lt;/em&gt;&lt;/strong&gt; , meaning “[to do] instead of [some other action]” or “as an alternative”, or just “otherwise”. In computing it means “[check if any prior condition is true] &lt;strong&gt;&lt;em&gt;else&lt;/em&gt;&lt;/strong&gt; [perform some final action]“. It is used to take some default or fallback action when no better, more specific action should be taken.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Conditionals are key to a lot of Python programming, and are needed to better explain some of the keywords that follow, so I’ll provide a few examples of how they work.&lt;/p&gt;

&lt;p&gt;Sometimes you only want to take &lt;em&gt;any&lt;/em&gt; action if some condition is met; this is the simplest form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Many situations are binary, though, and so you’ll always want to take some fallback action if the condition is &lt;em&gt;not&lt;/em&gt; met:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT_A&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT_B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In complex cases you may need to have any number of alternative actions based on mutually exclusive conditions, as well as a fallback:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION_A&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT_A&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION_B&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT_B&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT_C&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For the middle, “either/or” case there’s another form you will sometimes see, known as the &lt;em&gt;ternary operator&lt;/em&gt; form. This is useful mainly because, unlike a standard &lt;code&gt;if&lt;/code&gt; conditional, it behaves like an EXPRESSION, and the value it evaluates to can be directly assigned to a NAME:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;STATEMENT_A&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;STATEMENT_B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which is a much shorter way of writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;STATEMENT_A&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;STATEMENT_B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We’ll find this useful when we look at the OPERATOR-like keywords below.&lt;/p&gt;

&lt;h3&gt;
  
  
  Five That Are Operators
&lt;/h3&gt;

&lt;p&gt;The next five keywords all behave like an OPERATOR to denote actions performed on OBJECTs and/or EXPRESSIONS.&lt;/p&gt;

&lt;h4&gt;
  
  
  Boolean logic operators
&lt;/h4&gt;

&lt;p&gt;These are used for making meaningful comparisons between things based on their &lt;em&gt;truth-value&lt;/em&gt;.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="not"&gt;
not &lt;a href="https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not" title="See the documentation for “not”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;A &lt;em&gt;unary&lt;/em&gt; OPERATOR that inverts the &lt;em&gt;truth-value&lt;/em&gt; of whatever follows it, as in &lt;code&gt;not EXPRESSION&lt;/code&gt;.&lt;/dd&gt;
&lt;dd&gt;Can be used to invert the meaning of another KEYWORD; see &lt;code&gt;is not&lt;/code&gt;
and &lt;code&gt;not in&lt;/code&gt;
below.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A Middle English adverb from the Old English &lt;strong&gt;&lt;em&gt;nawiht&lt;/em&gt;&lt;/strong&gt;, implying “nothing” or “zero”. The general meaning today negates (or flips) the meaning of the word or phrase that follows it. Compare “I do have apples” to “I do &lt;strong&gt;&lt;em&gt;not&lt;/em&gt;&lt;/strong&gt; have apples”. In programming, however, the specific meaning comes from &lt;a href="https://en.wikipedia.org/wiki/Negation"&gt;logical negation&lt;/a&gt;, and thus &lt;strong&gt;&lt;em&gt;not&lt;/em&gt;&lt;/strong&gt; negates true to false, and vice versa.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is Python’s &lt;em&gt;Boolean negation&lt;/em&gt; OPERATOR, used whenever you need the opposite of the truth-value of a thing. It is &lt;em&gt;unary&lt;/em&gt;, which means that it acts on whatever is to its immediate right.&lt;/p&gt;

&lt;p&gt;The usage of &lt;code&gt;not&lt;/code&gt; is straightforward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;EXPRESSION&lt;/code&gt; is considered &lt;code&gt;True&lt;/code&gt; then &lt;code&gt;not EXPRESSION&lt;/code&gt; evaluates to &lt;code&gt;False&lt;/code&gt; and otherwise it evaluates to &lt;code&gt;True&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That can be easier to understand if you think of &lt;code&gt;not&lt;/code&gt; as working like the following &lt;em&gt;ternary&lt;/em&gt; &lt;code&gt;if&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="bp"&gt;False&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;dl&gt;
&lt;dt id="and"&gt;
and &lt;a href="https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not" title="See the documentation for “and”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;A &lt;em&gt;binary&lt;/em&gt; OPERATOR that checks the &lt;em&gt;truth-value&lt;/em&gt; of two things, evaluating to the thing on the left if it tested &lt;code&gt;False&lt;/code&gt;
, otherwise to the thing on the right.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;An ancient Old English conjunction with Germanic roots vaguely meaning “thereupon” or “next [to]” and used to combine two words or phrases, as in “coffee &lt;strong&gt;&lt;em&gt;and&lt;/em&gt;&lt;/strong&gt; tea”. The meaning in Python, however, comes entirely from &lt;a href="https://en.wikipedia.org/wiki/Logical_conjunction"&gt;logical conjunction&lt;/a&gt;, and implies that either &lt;em&gt;both&lt;/em&gt; things it combines are true or the whole combination is false.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is Python’s &lt;em&gt;Boolean conjunction&lt;/em&gt; OPERATOR; used whenever you need to test if &lt;em&gt;both&lt;/em&gt; sides of the &lt;code&gt;and&lt;/code&gt; are considered &lt;code&gt;True&lt;/code&gt;. It is a &lt;em&gt;short-circuiting&lt;/em&gt; operation; if the left-hand EXPRESSION is considered &lt;code&gt;False&lt;/code&gt; the entire operation is considered &lt;code&gt;False&lt;/code&gt; and the right-hand side will never be evaluated at all. And unlike &lt;code&gt;not&lt;/code&gt; it does not necessarily evaluate to either &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;, evaluating instead to either the left side (if considered &lt;code&gt;False&lt;/code&gt;) or the right side.&lt;/p&gt;

&lt;p&gt;Thus the usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;EXPRESSION_A&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION_B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Can be thought of as working like the following &lt;em&gt;ternary&lt;/em&gt; &lt;code&gt;if&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;EXPRESSION_B&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION_A&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION_A&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which is why you may one day find yourself surprised to find that &lt;code&gt;True and 1&lt;/code&gt; evaluates to &lt;code&gt;1&lt;/code&gt; while &lt;code&gt;1 and True&lt;/code&gt; evaluates to &lt;code&gt;True&lt;/code&gt;.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="or"&gt;
or &lt;a href="https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not" title="See the documentation for “or”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;A &lt;em&gt;binary&lt;/em&gt; OPERATOR that checks the &lt;em&gt;truth-value&lt;/em&gt; of two things, evaluating to the thing on the left if it tested &lt;code&gt;True&lt;/code&gt;
, otherwise to the thing on the right.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;Derived from the Old English conjunction &lt;strong&gt;&lt;em&gt;oþþe&lt;/em&gt;&lt;/strong&gt; , meaning “either”, and implying that either of the ideas conjoined are acceptable, as in “coffee &lt;strong&gt;&lt;em&gt;or&lt;/em&gt;&lt;/strong&gt; tea”. In Python the meaning comes from &lt;a href="https://en.wikipedia.org/wiki/Logical_disjunction"&gt;logical disjunction&lt;/a&gt;, and implies that either &lt;em&gt;one&lt;/em&gt; of the things it combines is true or the whole combination is false.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is Python’s &lt;em&gt;Boolean disjunction&lt;/em&gt; OPERATOR; used whenever you need to test if &lt;em&gt;either&lt;/em&gt; side of the &lt;code&gt;or&lt;/code&gt; is considered &lt;code&gt;True&lt;/code&gt;. It is a &lt;em&gt;short-circuiting&lt;/em&gt; operation; if the left-hand EXPRESSION is considered &lt;code&gt;True&lt;/code&gt; the entire operation is considered &lt;code&gt;True&lt;/code&gt; and the right-hand side will never be evaluated at all. Also, unlike &lt;code&gt;not&lt;/code&gt; it does not necessarily evaluate to either &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;, evaluating instead to either the left side (if considered &lt;code&gt;True&lt;/code&gt;) or the right side.&lt;/p&gt;

&lt;p&gt;Thus the usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;EXPRESSION_A&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION_B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Can be thought of as working like the following &lt;em&gt;ternary&lt;/em&gt; &lt;code&gt;if&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;EXPRESSION_A&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION_A&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION_B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This subtlety can catch you out when you find that &lt;code&gt;True or 1&lt;/code&gt; evaluates to &lt;code&gt;True&lt;/code&gt; but &lt;code&gt;1 or True&lt;/code&gt; evaluates to &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Identity checking operator(s)
&lt;/h4&gt;

&lt;dl&gt;
&lt;dt id="is"&gt;
is &lt;a href="https://docs.python.org/3/reference/expressions.html#is" title="See the documentation for “is”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;A &lt;em&gt;binary&lt;/em&gt; OPERATOR that tests if the OBJECT on the left has the same &lt;a href="https://en.wikipedia.org/wiki/Identity_(object-oriented_programming)" title="Read about identity in programming"&gt;identity&lt;/a&gt; as the OBJECT on the right and then evaluates to either &lt;code&gt;True&lt;/code&gt;
or &lt;code&gt;False&lt;/code&gt;
.&lt;/dd&gt;
&lt;dd&gt;Can be inverted by &lt;code&gt;not&lt;/code&gt;
to become the &lt;code&gt;is not&lt;/code&gt;
operator.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;An Old English verb from the Germanic stem &lt;strong&gt;&lt;em&gt;*es-&lt;/em&gt;&lt;/strong&gt; ; it’s the third person singular present indicative form of the word &lt;strong&gt;&lt;em&gt;be&lt;/em&gt;&lt;/strong&gt; , so it generally means “to be [a thing]“. In Python its meaning is specific to &lt;em&gt;identity&lt;/em&gt;, and implies something more like “to be [some unique thing]“.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The usage is straightforward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;EXPRESSION_A&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION_B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And the usage with &lt;code&gt;not&lt;/code&gt; is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;EXPRESSION_A&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION_B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The notion of &lt;em&gt;identity&lt;/em&gt; is a little abstract, but think of it like this: Tom and Bob are twins, they’re the same height, age, and weight, and they share the same birthday, but they &lt;em&gt;do not&lt;/em&gt; have the same identity. Tom is Tom, and Bob is Bob, but Tom is not Bob.&lt;/p&gt;

&lt;p&gt;In most implementations of Python the identity of an OBJECT can be thought of as its unique address in memory, and since &lt;em&gt;every&lt;/em&gt; OBJECT you work with will have its own unique address, &lt;code&gt;is&lt;/code&gt; is usually only useful for telling if a NAME refers to a specific OBJECT:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;OBJECT&lt;/span&gt;
&lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;OBJECT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Or for testing if two different NAMEs refer to the same OBJECT in memory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;OTHER_NAME&lt;/span&gt;
&lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;OTHER_NAME&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There &lt;em&gt;are&lt;/em&gt; however some special cases: for instance, &lt;code&gt;True&lt;/code&gt;, &lt;code&gt;False&lt;/code&gt;, and &lt;code&gt;None&lt;/code&gt; are all &lt;a href="https://en.wikipedia.org/wiki/Singleton_pattern"&gt;singletons&lt;/a&gt; in memory, meaning there is only &lt;em&gt;ever&lt;/em&gt; one copy of &lt;code&gt;True&lt;/code&gt; in &lt;em&gt;any&lt;/em&gt; Python program. For the most part this is just a space-saving detail you don’t need to worry about, but it explains why in Python we use &lt;code&gt;VALUE == True&lt;/code&gt; and &lt;em&gt;not&lt;/em&gt; &lt;code&gt;VALUE is True&lt;/code&gt; when checking if something is considered &lt;em&gt;equivalent&lt;/em&gt; to &lt;code&gt;True&lt;/code&gt;. Testing &lt;em&gt;identity&lt;/em&gt; is &lt;em&gt;not&lt;/em&gt; the same as testing &lt;em&gt;value&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For this reason the uses of &lt;code&gt;is&lt;/code&gt; are limited and specific, which is why you’ll only rarely see &lt;code&gt;is&lt;/code&gt; and &lt;code&gt;is not&lt;/code&gt; used in practice.&lt;/p&gt;

&lt;h4&gt;
  
  
  Membership testing operator(s)
&lt;/h4&gt;

&lt;dl&gt;
&lt;dt id="in"&gt;
in &lt;a href="https://docs.python.org/3/reference/expressions.html#membership-test-operations" title="See the documentation for “in”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;A &lt;em&gt;binary&lt;/em&gt; OPERATOR that tests if the OBJECT on the left is a member of the COLLECTION on the right and then evaluates to either &lt;code&gt;True&lt;/code&gt;
or &lt;code&gt;False&lt;/code&gt;
. Also known as Python’s &lt;em&gt;inclusion operator&lt;/em&gt;.&lt;/dd&gt;
&lt;dd&gt;Can be inverted by &lt;code&gt;not&lt;/code&gt;
to become the &lt;code&gt;not in&lt;/code&gt;
&lt;em&gt;exclusion operator&lt;/em&gt;.&lt;/dd&gt;
&lt;dd&gt;Also used with &lt;code&gt;for&lt;/code&gt;
, see below.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A Middle English merger of the Old English words &lt;strong&gt;&lt;em&gt;in&lt;/em&gt;&lt;/strong&gt; , meaning “among”, and &lt;strong&gt;&lt;em&gt;inne&lt;/em&gt;&lt;/strong&gt; , meaning “within” or “inside”. The merged word has many usages and meanings, but the general sense here is from the prepositional form, which implies that some thing is contained within or inside some larger thing, as in “a page &lt;strong&gt;&lt;em&gt;in&lt;/em&gt;&lt;/strong&gt; a book” or “a book &lt;strong&gt;&lt;em&gt;in&lt;/em&gt;&lt;/strong&gt; a library”. In Python it is specifically used for &lt;em&gt;membership testing&lt;/em&gt;, when checking if an item is contained within a group of items.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The usual usage of &lt;code&gt;in&lt;/code&gt; is to test if an OBJECT is a member of a specific CONTAINER:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;OBJECT&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;CONTAINER&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Or to test if OBJECT is &lt;code&gt;not&lt;/code&gt; a member of CONTAINER:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;OBJECT&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;CONTAINER&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It can be tempting to think you can use &lt;code&gt;is&lt;/code&gt; with &lt;code&gt;in&lt;/code&gt;, but that’s invalid syntax. It helps to remember that since &lt;code&gt;is&lt;/code&gt;, &lt;code&gt;is not&lt;/code&gt;, &lt;code&gt;in&lt;/code&gt;, and &lt;code&gt;not in&lt;/code&gt; are all binary OPERATORs they &lt;em&gt;must&lt;/em&gt; have either an OBJECT or EXPRESSION on either side, &lt;em&gt;not&lt;/em&gt; another KEYWORD.&lt;/p&gt;

&lt;h3&gt;
  
  
  Four Used to Loop
&lt;/h3&gt;

&lt;p&gt;The above keywords give you everything you need to perform simple decision making and to take basic actions, but they’re useless whenever you need to do something &lt;em&gt;repeatedly&lt;/em&gt;; that’s where &lt;em&gt;looping&lt;/em&gt; comes in. In Python the following four keywords give you everything you need to do that.&lt;/p&gt;

&lt;h4&gt;
  
  
  Starting a loop
&lt;/h4&gt;

&lt;p&gt;There are two ways to start a loop in Python, and they’re conceptually pretty similar, but your choice of which depends a great deal on exactly what you need to do with that loop.&lt;/p&gt;

&lt;h5&gt;
  
  
  Looping until some condition is reached
&lt;/h5&gt;

&lt;dl&gt;
&lt;dt id="while"&gt;
while &lt;a href="https://docs.python.org/3/reference/compound_stmts.html#the-while-statement" title="See the documentation for “while”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Starts a loop BLOCK by testing a the truth-value of an EXPRESSION; will iterate continuously until EXPRESSION evaluates to False.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;From the Old English word &lt;strong&gt;&lt;em&gt;hwile&lt;/em&gt;&lt;/strong&gt; for “a duration of time”, but here we’re using the conjunctive form which implies “[during the] time [that something is true]” or “for as long as [something is true]“. In programming languages the keyword is always associated with something to &lt;em&gt;test&lt;/em&gt; and something to &lt;em&gt;do&lt;/em&gt;, so the meaning becomes “ &lt;strong&gt;&lt;em&gt;while&lt;/em&gt;&lt;/strong&gt; [the test is true], [do something]“.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The form of a &lt;code&gt;while&lt;/code&gt; loop BLOCK is always the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If the EXPRESSION evaluates as &lt;code&gt;True&lt;/code&gt; then STATEMENT will be reached and executed. When the end of the indented BLOCK is reached, control returns immediately to the top, then EXPRESSION is tested again, and so on. So long as EXPRESSION evaluates as &lt;code&gt;True&lt;/code&gt; the entire indented BLOCK will be run again and again.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;while&lt;/code&gt; loop can also &lt;em&gt;optionally&lt;/em&gt; be terminated by an &lt;code&gt;else&lt;/code&gt; BLOCK:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;[...]&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this case the STATEMENT inside the &lt;code&gt;else&lt;/code&gt; block will executed if the &lt;code&gt;while&lt;/code&gt; loop runs all the way to completion (its test evaluates to &lt;code&gt;False&lt;/code&gt;) &lt;em&gt;without&lt;/em&gt; encountering &lt;code&gt;break&lt;/code&gt;. This can be useful when there is some cleanup action that needs to occur when a &lt;code&gt;while&lt;/code&gt; loop has exited naturally.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;while&lt;/code&gt; is the most basic form of loop, but it’s a little dangerous if not used with care. This is because any EXPRESSION that &lt;em&gt;always&lt;/em&gt; evaluates as &lt;code&gt;True&lt;/code&gt; will run &lt;em&gt;forever&lt;/em&gt;. For this reason it’s generally important to design the EXPRESSION so that it will &lt;em&gt;eventually&lt;/em&gt; evaluate to &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  Looping through the members of a COLLECTION
&lt;/h5&gt;

&lt;dl&gt;
&lt;dt id="for"&gt;
for &lt;a href="https://docs.python.org/3/reference/compound_stmts.html#the-for-statement" title="See the documentation for “for”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Starts a loop BLOCK that will iterate &lt;em&gt;once&lt;/em&gt; over a COLLECTION, visiting every item in it.&lt;/dd&gt;
&lt;dd&gt;Can also be marked with &lt;code&gt;async&lt;/code&gt;
, to start an &lt;code&gt;async for&lt;/code&gt;
loop, see below.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;An Old English word via the German &lt;strong&gt;&lt;em&gt;für&lt;/em&gt;&lt;/strong&gt; with a great many meanings; the general meaning is taken from a prepositional sense of “[performing an action] on behalf of [some thing]“. In computing, though, the meaning is actually taken from the contraction of the word &lt;strong&gt;&lt;em&gt;for&lt;/em&gt;&lt;/strong&gt; with either &lt;strong&gt;&lt;em&gt;every&lt;/em&gt;&lt;/strong&gt; (meaning “each [item] in a group”) or &lt;strong&gt;&lt;em&gt;each&lt;/em&gt;&lt;/strong&gt; (meaning “all [of a group]“) to form &lt;strong&gt;&lt;em&gt;for every&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;for each&lt;/em&gt;&lt;/strong&gt; , both of which mean “[to perform an action] on behalf of each item [in a group]“. In programming languages that descend from ALGOL &lt;a href="https://en.wikipedia.org/wiki/For_loop"&gt;&lt;strong&gt;&lt;em&gt;for&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; has traditionally been the most common name for such a loop, with &lt;strong&gt;&lt;em&gt;do&lt;/em&gt;&lt;/strong&gt; used in a smaller number of languages. Python takes the name from the traditional usage in ALGOL via C, however it is more accurate to describe Python’s version as a &lt;a href="https://en.wikipedia.org/wiki/Foreach_loop"&gt;&lt;strong&gt;&lt;em&gt;foreach&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; loop, because there is no explicit counter and the thing being looped over must be &lt;a href="https://en.wikipedia.org/wiki/Iteration"&gt;&lt;em&gt;iterable&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The usage of &lt;code&gt;for&lt;/code&gt; consistently involves assigning a user-assigned NAME to every OBJECT &lt;code&gt;in&lt;/code&gt; a COLLECTION.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;COLLECTION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Thus &lt;code&gt;for&lt;/code&gt; every item in the COLLECTION the NAME will refer to that item &lt;em&gt;inside&lt;/em&gt; the scope of the BLOCK; this allows the STATEMENT to use NAME to act on that thing.&lt;/p&gt;

&lt;p&gt;This can be a little easier to grasp if you think of &lt;code&gt;for&lt;/code&gt; as a specialized form of &lt;code&gt;while&lt;/code&gt; loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="n"&gt;remain&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;COLLECTION&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;increment&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="nb"&gt;next&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But obviously the &lt;code&gt;for&lt;/code&gt; loop is much simpler to work with, as you don’t need to worry about implementing the machinery necessary to track the start and stop conditions of the loop. You’ll find that you can, and usually should, use a &lt;code&gt;for&lt;/code&gt; loop whenever you’re visiting the individual contents of a COLLECTION, rather than risk a runaway &lt;code&gt;while&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; loop can also &lt;em&gt;optionally&lt;/em&gt; be terminated by an &lt;code&gt;else&lt;/code&gt; BLOCK:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;COLLECTION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;[...]&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this case the STATEMENT inside the &lt;code&gt;else&lt;/code&gt; block will executed if the &lt;code&gt;for&lt;/code&gt; loop has run to completion &lt;em&gt;without&lt;/em&gt; encountering &lt;code&gt;break&lt;/code&gt;. This can be useful when there is some cleanup action that needs to occur when a &lt;code&gt;for&lt;/code&gt; loop has exited naturally.&lt;/p&gt;

&lt;h4&gt;
  
  
  Controlling a loop
&lt;/h4&gt;

&lt;p&gt;You don’t always want to simply run the loop to completion; there may be good reason to exit early, or to skip a round of the loop.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="break"&gt;
break &lt;a href="https://docs.python.org/3/reference/simple_stmts.html#the-break-statement" title="See the documentation for “break”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Used to immediately interrupt the current loop iteration, ending the BLOCK it is found within. For this reason must &lt;em&gt;only&lt;/em&gt; be used within a loop BLOCK.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;From the Old English word &lt;strong&gt;&lt;em&gt;brecan&lt;/em&gt;&lt;/strong&gt; , which has several forms and meanings. The noun form generally means “to damage, destroy, or render unusable”, as in “to &lt;strong&gt;&lt;em&gt;break&lt;/em&gt;&lt;/strong&gt; a leg”. Here, however, we use the alternative meaning “to interrupt [a continuous sequence]“, as in “to &lt;strong&gt;&lt;em&gt;break&lt;/em&gt;&lt;/strong&gt; an electrical circuit”. In programming it specifically means to interrupt a loop from inside that loop.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;break&lt;/code&gt; statement always forms a line of its own, and it &lt;em&gt;must&lt;/em&gt; be used in either a &lt;code&gt;for&lt;/code&gt; or &lt;code&gt;while&lt;/code&gt; loop. The most common use is to stop a loop immediately if some particular condition is reached:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;COLLECTION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is particularly useful if there’s some situation that warrants stopping the loop before it would normally be completed.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="continue"&gt;
continue &lt;a href="https://docs.python.org/3/reference/simple_stmts.html#the-continue-statement" title="See the documentation for “continue”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Immediately skips the rest of the current loop BLOCK, allowing the loop to continue on to the next iteration. For this reason must &lt;em&gt;only&lt;/em&gt; be used within a loop BLOCK.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A Middle English verb borrowed via the Old French &lt;strong&gt;&lt;em&gt;continuer&lt;/em&gt;&lt;/strong&gt; from the Latin &lt;strong&gt;&lt;em&gt;continuare&lt;/em&gt;&lt;/strong&gt;. The general meaning used here is to “go forward or onward”, “carry on”, or “proceed”. In programming it means to cause a loop to start executing the next iteration, skipping any instructions that follow it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;continue&lt;/code&gt; statement always forms a line of its own, and it must be used in either a &lt;code&gt;for&lt;/code&gt; or &lt;code&gt;while&lt;/code&gt; loop. The most common use is to skip to the next iteration of a loop immediately when some particular condition is reached:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;COLLECTION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is particularly useful if there’s some situation that warrants skipping the current loop; for example if you only wanted to act on every second iteration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Three for Importing Other Things
&lt;/h3&gt;

&lt;p&gt;All of the above, plus the &lt;em&gt;builtin&lt;/em&gt; functions we’ll talk about in a later article, are sufficient to let you start using Python as a &lt;em&gt;scripting&lt;/em&gt; language, where you glue together things others have written with your own code to do some task you want to accomplish. But you need to be able to access those “things others have written” to do so. That’s what Python’s &lt;em&gt;import mechanism&lt;/em&gt; is for.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="import"&gt;
import &lt;a href="https://docs.python.org/3/reference/simple_stmts.html#the-import-statement" title="See the documentation for “import”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Used to bring the functionality of an external MODULE into your own code.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A verb from the Middle English &lt;strong&gt;&lt;em&gt;importen&lt;/em&gt;&lt;/strong&gt; , via the Old French from the Latin &lt;strong&gt;&lt;em&gt;importare&lt;/em&gt;&lt;/strong&gt;. The general meaning is to “bring/carry [goods into this country] from abroad”. In computing it means to bring or &lt;strong&gt;&lt;em&gt;import&lt;/em&gt;&lt;/strong&gt; some functionality &lt;em&gt;exported&lt;/em&gt; by another program written in the same language into the current program.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The most common usage is very simple, the keyword is followed by the MODULE you want to access:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;MODULE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also import multiple MODULEs separated by commas:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;MODULE_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;MODULE_B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And for organizational purposes you can put parentheses around them as well:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MODULE_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;MODULE_B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Any NAME inside the imported MODULE(s) can then be accessed within your own program using the &lt;em&gt;dot access&lt;/em&gt; pattern in the form MODULE.NAME. So, for instance, if you wanted to get the circumference of a circle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;math&lt;/span&gt;

&lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;circumference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tau&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Usually you’ll find all the &lt;code&gt;import&lt;/code&gt; usage at the top of a MODULE, which makes it pretty easy to determine where such functionality comes from.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="from"&gt;from&lt;/dt&gt;
&lt;dd&gt;Modifies &lt;code&gt;import&lt;/code&gt;
to allow you to import specific NAMEs from within an external MODULE.&lt;/dd&gt;
&lt;dd&gt;Can also be used with &lt;code&gt;raise&lt;/code&gt;
and &lt;code&gt;yield&lt;/code&gt;
, see below.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A Middle English word from the Old English &lt;strong&gt;&lt;em&gt;fram&lt;/em&gt;&lt;/strong&gt; , here we use the preposition form, with a general sense of “departure or movement away [from something]“; in computing we use a more specific sense of “taken &lt;strong&gt;&lt;em&gt;from&lt;/em&gt;&lt;/strong&gt; a source”.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It’s used to modify &lt;code&gt;import&lt;/code&gt; to import a specific NAME from a MODULE, rather than the &lt;em&gt;entire&lt;/em&gt; MODULE:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;MODULE&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you wish to import more than one NAME they can be separated by commas:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;MODULE&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;NAME_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;NAME_B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And for organizational purposes you can put parentheses around them as well:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;MODULE&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NAME_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;NAME_B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In all cases you can then use the imported NAME directly, so for instance if you want the area of a circle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;math&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tau&lt;/span&gt;

&lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;area&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tau&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The main reason for &lt;code&gt;from&lt;/code&gt; is to remove the need to have many references to a MODULE peppered throughout your code, but it’s best reserved for when the MODULE has many NAMEs that it exports and you want to just use one or two.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="as"&gt;as&lt;/dt&gt;
&lt;dd&gt;Modifies &lt;code&gt;import&lt;/code&gt;
to create an alternative NAME (or alias) for an imported NAME.&lt;/dd&gt;
&lt;dd&gt;Can also be used with &lt;code&gt;except&lt;/code&gt;
and &lt;code&gt;with&lt;/code&gt;
, see below.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;From the Old English &lt;strong&gt;&lt;em&gt;eallswā&lt;/em&gt;&lt;/strong&gt; meaning “just so” or simply “all so”, which makes it a reduced form of &lt;strong&gt;&lt;em&gt;also&lt;/em&gt;&lt;/strong&gt;. The usage here comes from the adverb form meaning “[to act] in the manner or role [of some other thing]“. In Python it very specifically means “[from here on refer to this thing] &lt;strong&gt;&lt;em&gt;as&lt;/em&gt;&lt;/strong&gt; [this instead]“.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because &lt;code&gt;from&lt;/code&gt; exists there are two forms of usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;MODULE&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;MODULE_ALIAS&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;MODULE&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;NAME_ALIAS&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The point of &lt;code&gt;as&lt;/code&gt; is to allow you to &lt;code&gt;import&lt;/code&gt; some MODULE or NAME but refer to it by some other name. This is useful if the original name is particularly long, would conflict with one already in use in your code, or could simply use some extra information in context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;math&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pi&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;half_as_good_as_tau&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Five for Exceptional Situations
&lt;/h3&gt;

&lt;p&gt;Now that you’ve got the basics down, you’re getting into more complicated territory. What happens if you find yourself reaching a point in the code where you’re in an obvious error state and you don’t want to continue? This is where the notion of &lt;a href="https://en.wikipedia.org/wiki/Exception_handling"&gt;exception handling&lt;/a&gt; come into play. We’ll go into more detail on the standard Exceptions in later articles, but for now let’s just say that an EXCEPTION is a special type of VALUE that signifies a specific issue that has come up in your program. That issue &lt;em&gt;might&lt;/em&gt; be an &lt;em&gt;error&lt;/em&gt;, in which case you might want to crash out of the program, or it might be a signal that something &lt;em&gt;unusual&lt;/em&gt; but expected has occurred. Either way you need to be able to emit those signals from your own code, as well as catch and react to such signals emitted by other code.&lt;/p&gt;

&lt;h4&gt;
  
  
  To signal that there’s a problem
&lt;/h4&gt;

&lt;dl&gt;
&lt;dt id="raise"&gt;
raise &lt;a href="https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement" title="See the documentation for “raise”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Used to raise a specified EXCEPTION, which will cause the program to stop immediately and exit if not handled by an &lt;code&gt;except&lt;/code&gt;
BLOCK.&lt;/dd&gt;
&lt;dd&gt;If used without an argument inside an &lt;code&gt;except&lt;/code&gt;
or &lt;code&gt;finally&lt;/code&gt;
BLOCK, re-raises the EXCEPTION being handled by the BLOCK.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A Middle English word with &lt;em&gt;many&lt;/em&gt; meanings, but in this case it comes from the verb form meaning “to lift upright, build, or construct” or “to make higher”. The meaning in computing is more specifically from a newer sense of “to mention [a question, issue, or argument] for discussion”, as in “to &lt;strong&gt;&lt;em&gt;raise&lt;/em&gt;&lt;/strong&gt; attention [to an issue]“. In several other programming languages &lt;strong&gt;&lt;em&gt;throw&lt;/em&gt;&lt;/strong&gt; is used with similar meaning.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The usage is &lt;em&gt;usually&lt;/em&gt; going to be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;EXCEPTION&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Quite rarely you might see the &lt;em&gt;chained&lt;/em&gt; form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;EXCEPTION&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;OTHER_EXCEPTION&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which is used to indicate that the EXCEPTION being raised was &lt;em&gt;caused by&lt;/em&gt; (or &lt;em&gt;came from&lt;/em&gt;) some other EXCEPTION. This isn’t used often, but sometimes is useful when you attempt to handle an exception raised by other code but somehow cannot do so.&lt;/p&gt;

&lt;p&gt;Lastly &lt;em&gt;inside&lt;/em&gt; an &lt;code&gt;except&lt;/code&gt; BLOCK you can simply:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;raise&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which will re-raise whatever EXCEPTION is currently being handled inside that BLOCK.&lt;/p&gt;

&lt;h4&gt;
  
  
  To signal that a particular condition is not met
&lt;/h4&gt;

&lt;dl&gt;
&lt;dt id="assert"&gt;
assert &lt;a href="https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement" title="See the documentation for “assert”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Used to test if some EXPRESSION is considered True, and if not raise an AssertionError.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;From the Latin &lt;strong&gt;&lt;em&gt;assertus&lt;/em&gt;&lt;/strong&gt; , with the general sense of “declared, protected, or claimed”. In programming it specifically means to specify that a condition must be met at a particular point in the code, and to error if it is not.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The usage of &lt;code&gt;assert&lt;/code&gt; is usually going to be in the form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which will simply &lt;code&gt;raise&lt;/code&gt; an AssertionError if EXPRESSION does not evaluate as &lt;code&gt;True&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An alternative form allows you to specify a message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"something is wrong!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And this message will be incorporated into the AssertionError.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;assert&lt;/code&gt; statement exists for you to test the things that &lt;em&gt;must&lt;/em&gt; be &lt;code&gt;True&lt;/code&gt; for your program to continue to work (we call these your &lt;em&gt;invariants&lt;/em&gt;). This can be very helpful when developing and debugging, but should not be relied on, &lt;em&gt;at all&lt;/em&gt;, in production code, as the person running your program can elect to disable all &lt;code&gt;assert&lt;/code&gt; statements by passing the &lt;code&gt;-O&lt;/code&gt; command line option to Python. Basically you can imagine that &lt;code&gt;assert&lt;/code&gt; works like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;O&lt;/span&gt; &lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt; &lt;span class="n"&gt;was&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;passed&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nb"&gt;AssertionError&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;… except that if the &lt;code&gt;-O&lt;/code&gt; flag was passed the &lt;code&gt;assert&lt;/code&gt; statement will simply be replaced by &lt;code&gt;pass&lt;/code&gt; and nothing whatsoever will happen.&lt;/p&gt;

&lt;h4&gt;
  
  
  To catch a signal and react to it
&lt;/h4&gt;

&lt;p&gt;With both &lt;code&gt;raise&lt;/code&gt; and &lt;code&gt;assert&lt;/code&gt; in your toolkit you know how to signal an EXCEPTION, but how do you catch and react to (or ignore) one? This is where &lt;a href="https://docs.python.org/3/tutorial/errors.html#handling-exceptions"&gt;Python’s exception handling&lt;/a&gt; mechanism comes into play.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="try"&gt;
try &lt;a href="https://docs.python.org/3/reference/compound_stmts.html#the-try-statement" title="See the documentation for “try”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Starts an exception handler BLOCK; &lt;em&gt;must&lt;/em&gt; be followed either an &lt;code&gt;except&lt;/code&gt;
BLOCK, an &lt;code&gt;else&lt;/code&gt;
block or a &lt;code&gt;finally&lt;/code&gt;
BLOCK in order to be valid syntax.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A verb borrowed from the Old French word &lt;strong&gt;&lt;em&gt;trier&lt;/em&gt;&lt;/strong&gt; , meaning to “test”, “experiment”, or “attempt to do”. The meaning in Python is essentially the same, “[start a] test [of something that &lt;em&gt;may&lt;/em&gt; error]“.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;dl&gt;
&lt;dt id="except"&gt;except&lt;/dt&gt;
&lt;dd&gt;Optionally &lt;em&gt;continues&lt;/em&gt; an exception handler BLOCK by catching EXCEPTIONs; can (and &lt;em&gt;should&lt;/em&gt;) be limited to specific types of EXCEPTION. More than one of these can follow a &lt;code&gt;try&lt;/code&gt;
and each will be checked in turn until either the EXCEPTION is handled or no more &lt;code&gt;except&lt;/code&gt;
statements remain.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A verb borrowed from the Middle French &lt;strong&gt;&lt;em&gt;excepter&lt;/em&gt;&lt;/strong&gt; and Latin &lt;strong&gt;&lt;em&gt;exceptus&lt;/em&gt;&lt;/strong&gt; ; the original meaning is “to receive”, but the more general uses it to “exclude [something]” or “object to [something]“, as in “every fruit except apples”. The meaning in Python is a little vague, but can be thought of as “catch, capture, or trap [an exception]“. In fact in many other languages &lt;strong&gt;&lt;em&gt;catch&lt;/em&gt;&lt;/strong&gt; is used for the same purpose; Python’s &lt;strong&gt;&lt;em&gt;except&lt;/em&gt;&lt;/strong&gt; is the exception to that rule.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;dl&gt;
&lt;dt id="finally"&gt;finally&lt;/dt&gt;
&lt;dd&gt;Optionally &lt;em&gt;cleans up&lt;/em&gt; an exception handler BLOCK to provide a means of &lt;em&gt;always&lt;/em&gt; performing some action whether or not the EXCEPTION was handled. &lt;em&gt;Must&lt;/em&gt; appear after any &lt;code&gt;except&lt;/code&gt;
BLOCKS that are present, as well as the optional &lt;code&gt;else&lt;/code&gt;
BLOCK if that is also present. If no &lt;code&gt;except&lt;/code&gt;
BLOCK is present then &lt;code&gt;finally&lt;/code&gt;
&lt;em&gt;must&lt;/em&gt; terminate the exception handler.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;From the Middle English &lt;strong&gt;&lt;em&gt;fynaly&lt;/em&gt;&lt;/strong&gt; meaning “at the end or conclusion” or just “lastly”. It implies the very last thing to do in a sequence, which is the meaning here as well.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Exception handling is the first circumstance you’ve encountered in which one BLOCK &lt;em&gt;must&lt;/em&gt; be followed by another, and the rules are somewhat more complicated than anything you’ve yet seen. For instance there are &lt;em&gt;two&lt;/em&gt; different &lt;em&gt;minimal syntactically valid&lt;/em&gt; forms of exception handler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;BLOCK&lt;/span&gt;
&lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This form does not actually handle any EXCEPTION raised within BLOCK, it merely ensures that STATEMENT is run in all circumstances. Any EXCEPTION not handled will continue to “raise up” until it is either handled or crashes your program. This is useful for situations in which you want to do the same action (such as closing a file or database connection) whether or not there has been an error.&lt;/p&gt;

&lt;p&gt;The second form &lt;em&gt;does&lt;/em&gt; handle an EXCEPTION:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;BLOCK&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; : because this form will catch &lt;em&gt;any&lt;/em&gt; EXCEPTION, including several that are used by Python to perform critical operations, you should, practically speaking, &lt;strong&gt;never&lt;/strong&gt; use the above form. Instead use the &lt;em&gt;minimum safe&lt;/em&gt; form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;BLOCK&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;EXCEPTION&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This ensures that the exception handler &lt;em&gt;only&lt;/em&gt; handles the EXCEPTION &lt;em&gt;type&lt;/em&gt; specified. The &lt;a href="https://docs.python.org/3/library/exceptions.html#exception-hierarchy"&gt;hierarchy of built-in exceptions&lt;/a&gt; in Python is complex and touches on concepts we can’t cover in this article, but for now just know that &lt;code&gt;except Exception&lt;/code&gt; is the &lt;em&gt;least&lt;/em&gt; specific &lt;code&gt;except&lt;/code&gt; statement you should ever use in production code.&lt;/p&gt;

&lt;p&gt;That minimum safe form, however, isn’t particularly useful, because the STATEMENT can’t actually know what EXCEPTION it’s handling. Most of the time you’ll see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;EXCEPTION&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which uses &lt;code&gt;as&lt;/code&gt; to provide an alias NAME that can be used to inspect the actual EXCEPTION that was raised. This can be very helpful for seeing precisely what went wrong, which will help you decide if you can ignore the problem or need to respond in some way.&lt;/p&gt;

&lt;p&gt;You can also specify multiple different types of EXCEPTION to catch, which can be helpful if you want to respond to any of those forms in the &lt;em&gt;same&lt;/em&gt; way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EXCEPTION_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EXCEPTION_B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EXCEPTION_C&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And if you want to respond to them each in a &lt;em&gt;different&lt;/em&gt; way you can just stack &lt;code&gt;except&lt;/code&gt; BLOCKS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;EXCEPTION_A&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT_A&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;EXCEPTION_B&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT_B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also optionally use &lt;code&gt;else&lt;/code&gt; with an exception handler, so long as at least one &lt;code&gt;except&lt;/code&gt; statement is used, and as long as the &lt;code&gt;else&lt;/code&gt; is positioned &lt;em&gt;before&lt;/em&gt; any &lt;code&gt;finally&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;BLOCK_A&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;EXCEPTION&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;BLOCK_B&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However this is relatively rare, because the STATEMENT indented under &lt;code&gt;else&lt;/code&gt; will &lt;em&gt;only&lt;/em&gt; be executed when no EXCEPTION was raised inside the &lt;code&gt;try&lt;/code&gt; BLOCK &lt;em&gt;and&lt;/em&gt; no &lt;code&gt;break&lt;/code&gt;, &lt;code&gt;continue&lt;/code&gt;, or &lt;code&gt;return&lt;/code&gt; statements were encountered within it.&lt;/p&gt;

&lt;p&gt;As you’ve seen exception handling has some fairly dense syntax compared to the rest of Python. A fully fleshed out exception handler in a program that does something fairly complex, like interacting with a database, might involve all these parts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ConnectionError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="n"&gt;success&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But, thankfully, it’s often not that complex, and you usually only have to deal with exception handling when something truly exceptional has happened.&lt;/p&gt;

&lt;h3&gt;
  
  
  Four for Writing Functions
&lt;/h3&gt;

&lt;p&gt;Now that you’ve got all the structures you need to write an arbitrarily complex program, with the ability to make decisions, loop, and handle errors, your biggest problem is going to be organizing those structures into re-usable units. For instance you don’t want to type out a complex exception handler for every single time you connect to and query a database, as that would quickly lead to an unmanageable amount of repetition.&lt;/p&gt;

&lt;p&gt;One of the key mantras of programming is &lt;a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself"&gt;Don’t Repeat Yourself&lt;/a&gt; (aka &lt;strong&gt;DRY&lt;/strong&gt; ); the less boilerplate, the better, and so you want to be able to create &lt;a href="https://en.wikipedia.org/wiki/Subroutine"&gt;subroutines&lt;/a&gt;, which are the most basic form of &lt;em&gt;code re-use&lt;/em&gt;. In Python the most common form of subroutine is the &lt;em&gt;function&lt;/em&gt;, which comes in two primary forms.&lt;/p&gt;

&lt;h4&gt;
  
  
  Anonymous (unnamed) functions
&lt;/h4&gt;

&lt;p&gt;A lot of people teaching (and learning) Python tend to skip over anonymous functions, or treat them as an advanced feature, but that’s really just because they’re poorly named. The “anonymous” part just means the function isn’t given a specific name at creation time. That might not sound like the easiest thing to re-use – and you’re right – but it’s useful to understand them before we head on to &lt;em&gt;named&lt;/em&gt; functions because they’re fundamentally simpler and more constrained.&lt;/p&gt;

&lt;p&gt;Unfortunately the powers that be decided that anonymous functions, already saddled with a bad and confusing name, should &lt;em&gt;definitely&lt;/em&gt; get a worse name in Python.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="lambda"&gt;
lambda &lt;a href="https://docs.python.org/3/reference/expressions.html#lambda" title="See the documentation for “lambda”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Used to define a CALLABLE &lt;a href="https://en.wikipedia.org/wiki/Anonymous_function" title="Read about anonymous functions"&gt;anonymous function&lt;/a&gt; and its &lt;em&gt;signature&lt;/em&gt;.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;The 11th letter of the Classical Greek alphabet, &lt;strong&gt;&lt;em&gt;λ&lt;/em&gt;&lt;/strong&gt; ; it has no general meaning in English. In Python it is used because anonymous functions are a fundamental unit of &lt;a href="https://en.wikipedia.org/wiki/Lambda_calculus"&gt;Alonzo Church’s Lambda Calculus&lt;/a&gt;, which provides much of the mathematical underpinnings of modern computation. As an honor, that’s nice; in reality &lt;strong&gt;&lt;em&gt;lambda&lt;/em&gt;&lt;/strong&gt; would be used more often if it had a more fun name.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Despite the name a &lt;code&gt;lambda&lt;/code&gt; is actually the conceptually simplest form of function in Python, because you can think of it as a just a way of creating a delayed-evaluation EXPRESSION that’s stated with a specific form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which evaluates to a CALLABLE that will evaluate the EXPRESSION that comes &lt;em&gt;after&lt;/em&gt; the colon only when the CALLABLE is itself called. To call it you use the &lt;em&gt;call syntax&lt;/em&gt; that is common to all Python CALLABLEs, however since the &lt;code&gt;lambda&lt;/code&gt; is itself an EXPRESSION you need to surround it with parentheses to do that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;&lt;span class="p"&gt;)()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And &lt;em&gt;voila&lt;/em&gt;, everything you’ve just written will instantly be replaced by whatever the inner EXPRESSION evaluates to.&lt;/p&gt;

&lt;p&gt;Thus all three of these are entirely identical:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)()&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But just delaying an EXPRESSION isn’t really the most useful tool. So the &lt;code&gt;lambda&lt;/code&gt; introduces the idea of a &lt;em&gt;function signature&lt;/em&gt;: you can add a NAME to the left side of the colon and the thing that NAME refers to will able to be used inside the inner EXPRESSION when it evaluates. This name is known as a &lt;em&gt;parameter&lt;/em&gt; of the function.&lt;/p&gt;

&lt;p&gt;Here’s a &lt;code&gt;lambda&lt;/code&gt; with a single parameter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And here’s one with two parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;NAME_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;NAME_B&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And when we want to use the &lt;code&gt;lambda&lt;/code&gt; we just call it, passing in a concrete VALUE for every parameter in the signature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;NAME_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;NAME_B&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;VALUE_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;VALUE_B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which you can imagine can be useful if we want to calculate the area of a rectangle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But, again, we’re going to end up writing that a lot if we don’t assign it to a NAME:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;area&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;
&lt;span class="n"&gt;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;rectangle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Great! The &lt;code&gt;square&lt;/code&gt; is now &lt;code&gt;4&lt;/code&gt; and &lt;code&gt;rectangle&lt;/code&gt; is &lt;code&gt;15&lt;/code&gt;; we’ve got the basis of code re-use!&lt;/p&gt;

&lt;p&gt;But &lt;code&gt;lambda&lt;/code&gt; is going to get pretty nasty when the EXPRESSION starts to get long. And beyond that there are some pretty significant limitations, since we actually &lt;em&gt;cannot&lt;/em&gt; execute many kinds of STATEMENT within a &lt;code&gt;lambda&lt;/code&gt;, much less an arbitrary BLOCK. They have their place, but maybe there’s a better and more flexible way?&lt;/p&gt;

&lt;p&gt;Since we’ve just gone and assigned what is supposed to be an &lt;em&gt;anonymous&lt;/em&gt; (which means “has no name”) function to a NAME, let’s look at how we should &lt;em&gt;usually&lt;/em&gt; write functions in Python.&lt;/p&gt;

&lt;h4&gt;
  
  
  Named functions
&lt;/h4&gt;

&lt;p&gt;The &lt;em&gt;named&lt;/em&gt; function builds on the ideas of the &lt;code&gt;lambda&lt;/code&gt; but take them to a &lt;em&gt;much&lt;/em&gt; more flexible place, allowing you to re-use large chunks of code quite easily. They’re a little more subtle to master too, because they don’t evaluate exactly like an EXPRESSION. In fact by default they’ll always evaluate to &lt;code&gt;None&lt;/code&gt; unless you explicitly tell them to do otherwise.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="def"&gt;
def &lt;a href="https://docs.python.org/3/reference/compound_stmts.html#function-definitions" title="See the documentation for “def”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Used to define a &lt;em&gt;named&lt;/em&gt; function and its &lt;em&gt;signature&lt;/em&gt;, the indented BLOCK that follows can then be re-used by calling that NAME using the &lt;code&gt;function()&lt;/code&gt; syntax.&lt;/dd&gt;
&lt;dd&gt;If used inside a &lt;code&gt;class&lt;/code&gt;
defines a named &lt;em&gt;method&lt;/em&gt; instead, which is called using the &lt;code&gt;class.method()&lt;/code&gt; syntax.&lt;/dd&gt;
&lt;dd&gt;Can also be marked with &lt;code&gt;async&lt;/code&gt;
, to start an &lt;code&gt;async def&lt;/code&gt;
, see below.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A contraction of the word &lt;strong&gt;&lt;em&gt;define&lt;/em&gt;&lt;/strong&gt; , which comes via the Middle English &lt;strong&gt;&lt;em&gt;deffinen&lt;/em&gt;&lt;/strong&gt; from Old French and Latin roots. It’s a verb that means “to specify or fix [the meaning of a word or phrase]“. In Python it is used specifically to create a named subroutine. In other languages &lt;strong&gt;&lt;em&gt;define&lt;/em&gt;&lt;/strong&gt; , &lt;strong&gt;&lt;em&gt;fn&lt;/em&gt;&lt;/strong&gt; , &lt;strong&gt;&lt;em&gt;fun&lt;/em&gt;&lt;/strong&gt; , &lt;strong&gt;&lt;em&gt;func&lt;/em&gt;&lt;/strong&gt; , &lt;strong&gt;&lt;em&gt;function&lt;/em&gt;&lt;/strong&gt; , and &lt;strong&gt;&lt;em&gt;let&lt;/em&gt;&lt;/strong&gt; are often used instead.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because &lt;code&gt;def&lt;/code&gt; can be used to create arbitrarily complex CALLABLEs, it’s going to require some explanation. Essentially it’s used to create a new NAME that refers to a CALLABLE. In fact, from now on let’s just use FUNCTION to mean exactly that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;FUNCTION&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This defines a FUNCTION that can be called using &lt;code&gt;FUNCTION()&lt;/code&gt;, which will then execute every STATEMENT inside the indented BLOCK. As you can see it’s conceptually equivalent to the “named” &lt;code&gt;lambda&lt;/code&gt; form you saw earlier. Compare these two forms:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;FUNCTION&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NAME_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;NAME_B&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;span class="n"&gt;FUNCTION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;NAME_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;NAME_B&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And you’ll see they’re &lt;em&gt;very&lt;/em&gt; similar, except that the &lt;code&gt;def&lt;/code&gt; version can run an arbitrary number of STATEMENTs&lt;sup id="fnref2"&gt;2&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;These STATEMENTs will be executed within the &lt;em&gt;local scope&lt;/em&gt; of the function, meaning that &lt;em&gt;any&lt;/em&gt; NAME assigned &lt;em&gt;inside&lt;/em&gt; the function cannot be &lt;em&gt;directly&lt;/em&gt; seen or accessed by any code &lt;em&gt;outside&lt;/em&gt; the function. In this example there are no such names, but there will be as soon as we change the &lt;em&gt;signature&lt;/em&gt; of the function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;FUNCTION&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NAME&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And just as with &lt;code&gt;lambda&lt;/code&gt; we can have multiple parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;FUNCTION&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NAME_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;NAME_B&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which let’s us build the &lt;code&gt;area&lt;/code&gt; function we made before with a &lt;code&gt;lambda&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;

&lt;span class="n"&gt;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;rectangle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Except, wait a minute … didn’t I say just above that &lt;code&gt;def&lt;/code&gt; functions evaluate to &lt;code&gt;None&lt;/code&gt; when called? Yep, right now both &lt;code&gt;square&lt;/code&gt; and &lt;code&gt;rectangle&lt;/code&gt; are assigned to &lt;code&gt;None&lt;/code&gt;. We’ve calculated two areas, but then discard them again as soon as the &lt;em&gt;local scope&lt;/em&gt; of &lt;code&gt;area&lt;/code&gt; is closed. Now, how the heck do we get the area &lt;em&gt;out&lt;/em&gt; of &lt;code&gt;area&lt;/code&gt;, anyway?&lt;/p&gt;

&lt;h5&gt;
  
  
  Stop the function and give back a value
&lt;/h5&gt;

&lt;dl&gt;
&lt;dt id="return"&gt;
return &lt;a href="https://docs.python.org/3/reference/simple_stmts.html#the-return-statement" title="See the documentation for “return”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Used to immediately give up control and end execution of the function at the point at which it is encountered. If followed by an EXPRESSION, that is evaluated first and the resulting OBJECT is given back to the caller of the function. If no EXPRESSION is present &lt;code&gt;None&lt;/code&gt;
is returned instead. Has no meaning outside a function, thus if present at all it &lt;em&gt;must&lt;/em&gt; be inside a BLOCK that follows a &lt;code&gt;def&lt;/code&gt;
.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A Middle English verb from the Old French &lt;strong&gt;&lt;em&gt;retourner&lt;/em&gt;&lt;/strong&gt; , meaning “to turn back” or “to go back [to a former position]“. In computing it has two meanings:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The intransitive meaning is “to give back (or relinquish) control [to the calling procedure]“, as in “when the function exits it will &lt;strong&gt;&lt;em&gt;return&lt;/em&gt;&lt;/strong&gt; control”.&lt;/li&gt;
&lt;li&gt;The transitive meaning is “to pass [some data] back to the calling procedure”, as in “this function will &lt;strong&gt;&lt;em&gt;return&lt;/em&gt;&lt;/strong&gt; the current time”.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In Python both meanings are combined, since a function will always &lt;strong&gt;&lt;em&gt;return&lt;/em&gt;&lt;/strong&gt; both control &lt;em&gt;and&lt;/em&gt; data to the caller.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The most basic usage of &lt;code&gt;return&lt;/code&gt; immediately ends the function and returns &lt;code&gt;None&lt;/code&gt; to the caller:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This form is relatively rarely used; as mentioned before a function &lt;em&gt;always&lt;/em&gt; evaluates to &lt;code&gt;None&lt;/code&gt; by default; thus you can imagine that &lt;code&gt;return&lt;/code&gt; is the implicit last line of &lt;em&gt;every&lt;/em&gt; function.&lt;/p&gt;

&lt;p&gt;Because of that it is much more common to see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which immediately evaluates the EXPRESSION and returns the resulting OBJECT back to the caller.&lt;/p&gt;

&lt;p&gt;You can also use &lt;code&gt;return&lt;/code&gt; to pass back more than one OBJECT:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION_B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This evaluates each EXPRESSION in turn, ends the function, and returns a &lt;a href="https://docs.python.org/3/library/functions.html#func-tuple"&gt;&lt;strong&gt;tuple&lt;/strong&gt;&lt;/a&gt; (a type of fixed-length COLLECTION) containing one OBJECT for each EXPRESSION.&lt;/p&gt;

&lt;p&gt;This multiple form is also relatively rare, as it can be a bit of a surprise to the user of the code, and so requires a bit more effort in documentation, but it can be convenient for internal functions that you don’t intend to be used by others.&lt;/p&gt;

&lt;p&gt;What you’ll notice is that &lt;code&gt;return&lt;/code&gt;&lt;em&gt;terminates&lt;/em&gt; a function at the moment it’s evaluated. Anything below that point in the function essentially doesn’t exist (with one exception, the &lt;code&gt;finally&lt;/code&gt; block inside an exception handler). So how would you handle situations in which you needed to give back data, but continue to work afterwards?&lt;/p&gt;

&lt;h5&gt;
  
  
  Pause the function and give back a value
&lt;/h5&gt;

&lt;dl&gt;
&lt;dt id="yield"&gt;
yield &lt;a href="https://docs.python.org/3/reference/simple_stmts.html#the-yield-statement" title="See the documentation for “yield”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Used to immediately pause execution and temporarily give up control at the point at which it is encountered. If followed by an EXPRESSION, that is evaluated first and the resulting OBJECT is yielded back to the caller of the function; if no EXPRESSION is present &lt;code&gt;None&lt;/code&gt;
is yielded instead. Has no meaning outside a function, thus if present at all it &lt;em&gt;must&lt;/em&gt; be used inside a BLOCK that follows a &lt;code&gt;def&lt;/code&gt;
.&lt;/dd&gt;
&lt;dd&gt;Can be modified by &lt;code&gt;from&lt;/code&gt;
to form &lt;code&gt;yield from&lt;/code&gt;
, see below.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;Etymologically the oddest word in this list; derives from the Middle English &lt;strong&gt;&lt;em&gt;yielden&lt;/em&gt;&lt;/strong&gt; and the Old English &lt;strong&gt;&lt;em&gt;gieldan&lt;/em&gt;&lt;/strong&gt; , both of which mean “to pay”, and share their root with the Old Norse &lt;strong&gt;&lt;em&gt;gjald&lt;/em&gt;&lt;/strong&gt; and the German &lt;strong&gt;&lt;em&gt;geld&lt;/em&gt;&lt;/strong&gt; , both of which mean “money”. Today &lt;strong&gt;&lt;em&gt;geld&lt;/em&gt;&lt;/strong&gt; means an ancient form of compelled tax or ransom, but it also means “to castrate”. Historically the Dangeld was a tax raised on the English by their king. This tax was raised to pay waves of Danish Vikings to, presumably, not castrate the English (or, at the very least, their king). None of this is directly important here, but might explain a little of why the meaning in computing derives from both “to give way and relinquish control”, as in “ &lt;strong&gt;&lt;em&gt;yield&lt;/em&gt;&lt;/strong&gt; to oncoming traffic”, and “to give back [a result or return on investment]” as in “the fund has a &lt;strong&gt;&lt;em&gt;yield&lt;/em&gt;&lt;/strong&gt; of 5% per year”. In both cases the implication is that the situation is not yet final, and is likely recurring: you &lt;strong&gt;&lt;em&gt;yield&lt;/em&gt;&lt;/strong&gt; , then you wait, then you &lt;strong&gt;&lt;em&gt;yield&lt;/em&gt;&lt;/strong&gt; again.&lt;/p&gt;

&lt;p&gt;In Python it helps to remember an allegory: &lt;code&gt;return&lt;/code&gt; is Death, and comes exactly once, while &lt;code&gt;yield&lt;/code&gt; is Taxes, and may only end when Death arrives.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The usage of &lt;code&gt;yield&lt;/code&gt; is similar to &lt;code&gt;return&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;yield&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION_B&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However &lt;em&gt;any&lt;/em&gt; function that uses &lt;code&gt;yield&lt;/code&gt; actually returns a special kind of COLLECTION known as a GENERATOR when called. Unlike a normal COLLECTION a GENERATOR does not hold all of its items in memory simultaneously, but instead “generates” each item as required by running through the function until &lt;code&gt;yield&lt;/code&gt; is encountered and it gives forth an item. The GENERATOR pauses execution at that point, allowing the code using the GENERATOR to work with that item. When desired it can request the next item, at which point the GENERATOR continues running to the next &lt;code&gt;yield&lt;/code&gt;, and so on until there are no more &lt;code&gt;yield&lt;/code&gt; statements left to run or a &lt;code&gt;return&lt;/code&gt; is encountered.&lt;/p&gt;

&lt;p&gt;This is what allows the next form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;GENERATOR&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which is a specialized use case that allows you to write a GENERATOR that will &lt;code&gt;yield&lt;/code&gt; every item, in turn, from &lt;em&gt;another&lt;/em&gt; GENERATOR.&lt;/p&gt;

&lt;p&gt;A thorough explanation of GENERATORs is outside the scope of this series of articles, as they’re a fairly advanced topic. For now it’s sufficient to know that if you encounter &lt;code&gt;yield&lt;/code&gt; you’re looking at a GENERATOR.&lt;/p&gt;

&lt;h3&gt;
  
  
  Three for Manipulating Namespaces
&lt;/h3&gt;

&lt;p&gt;Now that you understand the basics of function, you’ll need to know about &lt;a href="https://en.wikipedia.org/wiki/Namespace"&gt;namespaces&lt;/a&gt;, as they become very important when you start building more complex programs.&lt;/p&gt;

&lt;p&gt;At its simplest a namespace is essentially just the place where Python stores any NAME you create, so they’re used whenever Python needs to look up the OBJECT referred to by that NAME. Each namespace has a &lt;em&gt;scope&lt;/em&gt;, which limits how long the namespace “lives” and whether or not any NAME within it is &lt;em&gt;readable&lt;/em&gt; and/or &lt;em&gt;writable&lt;/em&gt; by a given STATEMENT. Namespaces form a hierarchy, however, so the rules can be a bit tricky to remember.&lt;/p&gt;

&lt;p&gt;In any given namespace a STATEMENT can:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;both &lt;em&gt;read&lt;/em&gt; and &lt;em&gt;write&lt;/em&gt; any NAME defined in its own namespace&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;read&lt;/em&gt;, but not &lt;em&gt;write&lt;/em&gt;, any NAME defined in an enclosing &lt;em&gt;parent&lt;/em&gt; namespace&lt;/li&gt;
&lt;li&gt;neither &lt;em&gt;read&lt;/em&gt; nor &lt;em&gt;write&lt;/em&gt; any NAME defined in a &lt;em&gt;sibling&lt;/em&gt; or &lt;em&gt;child&lt;/em&gt; namespace&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;At the top level is Python’s own namespace, which contains all the &lt;em&gt;builtin&lt;/em&gt; NAMEs that Python provides for you. This namespace &lt;em&gt;cannot&lt;/em&gt; be written to, and any NAME in it essentially lives forever.&lt;/p&gt;

&lt;p&gt;Next comes the &lt;em&gt;global&lt;/em&gt; namespace, which is the namespace of the MODULE you’re working in; it becomes the parent of all other namespaces created within that MODULE. Any NAME defined here &lt;em&gt;usually&lt;/em&gt; lives for the duration your program is running.&lt;/p&gt;

&lt;p&gt;Next, each CALLABLE you create gets its own unique &lt;em&gt;local&lt;/em&gt; namespace &lt;em&gt;when it is called&lt;/em&gt;; any NAME created here lives only as long as the CALLABLE is running, and when it finishes both the NAME and the OBJECT it refers to will be destroyed, unless either &lt;code&gt;return&lt;/code&gt; or &lt;code&gt;yield&lt;/code&gt; is used to pass it back to the calling scope.&lt;/p&gt;

&lt;p&gt;Since you can define a CALLABLE that is nested &lt;em&gt;inside&lt;/em&gt; another CALLABLE you can build a namespace hierarchy of arbitrary depth, so any given NAME might exist in any number of namespaces further and further removed from where you’re actually trying to use that NAME. This can get unwieldy to think about pretty quick, though, which is why we &lt;em&gt;try&lt;/em&gt; to limit the amount of nesting we do in practical code.&lt;/p&gt;

&lt;p&gt;The assignment of a new NAME is pretty straightforward: unless modified by one of the keywords below a NAME is &lt;em&gt;always&lt;/em&gt; assigned in the scope in which the assignment STATEMENT occurs.&lt;/p&gt;

&lt;p&gt;But the &lt;em&gt;use&lt;/em&gt; of a NAME is a little less obvious: when used in a STATEMENT Python first searches for the NAME in the immediate &lt;em&gt;local&lt;/em&gt; scope. If it cannot find the NAME then it searches the immediate parent (known as the &lt;em&gt;nonlocal&lt;/em&gt;) scope, and then it keeps searching each successive parent scope until it either finds the NAME or cannot resolve it and errors.&lt;/p&gt;

&lt;p&gt;All of this usually somewhat invisible machinery exists to allow you to write code using NAMEs that are appropriate and clear to the scope in which you’re working. So long as it isn’t a keyword and you don’t &lt;em&gt;need&lt;/em&gt; to use the NAME from an ancestor’s scope you can use whatever NAME you want locally and not worry about it overwriting anything &lt;em&gt;outside&lt;/em&gt; its own scope.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Most&lt;/em&gt; of the time you don’t want to fool around with that machinery, but every so often there’s a good reason to. Or a bad one that’s gotten you confused.&lt;/p&gt;

&lt;h4&gt;
  
  
  Write to the top from anywhere
&lt;/h4&gt;

&lt;p&gt;In &lt;em&gt;extremely rare&lt;/em&gt; circumstances you want to directly manipulate the &lt;em&gt;global&lt;/em&gt; namespace in a way you wouldn’t normally be allowed.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="global"&gt;
global &lt;a href="https://docs.python.org/3/reference/simple_stmts.html#the-global-statement" title="See the documentation for “global”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Used to declare a NAME as part of the &lt;em&gt;global&lt;/em&gt; namespace; the NAME &lt;em&gt;cannot&lt;/em&gt; have been used previously in the same namespace. In effect this allows a &lt;em&gt;local&lt;/em&gt; STATEMENT to both create and assign to a &lt;em&gt;global&lt;/em&gt; NAME it otherwise could only read. &lt;em&gt;Can&lt;/em&gt; be used within the global namespace, but has no effect.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;An adjective borrowed from the French, with the general meaning of “worldwide” and “universal”. In computing the meaning is specifically “[a NAME that is] accessible by all parts of a program”. In Python it is both the top-most &lt;em&gt;namespace&lt;/em&gt;, which is &lt;em&gt;readable&lt;/em&gt; from within all functions and methods, but is also a keyword that binds a &lt;em&gt;local&lt;/em&gt; NAME into the &lt;strong&gt;&lt;em&gt;global&lt;/em&gt;&lt;/strong&gt; namespace, allowing it to be &lt;em&gt;writable&lt;/em&gt; from within that &lt;em&gt;local&lt;/em&gt; namespace.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The usage is quite simple; the main restriction is that it cannot appear &lt;em&gt;after&lt;/em&gt; the NAME has been used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;global&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;
&lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should be aware that the use of &lt;code&gt;global&lt;/code&gt; is an &lt;em&gt;immediate&lt;/em&gt; &lt;a href="https://en.wikipedia.org/wiki/Code_smell"&gt;code smell&lt;/a&gt;, as its use is &lt;em&gt;almost always&lt;/em&gt; an indication of bad coding habits, because it &lt;em&gt;usually&lt;/em&gt; can – and almost certainly &lt;em&gt;should&lt;/em&gt; – be replaced with &lt;em&gt;argument passing&lt;/em&gt; and &lt;em&gt;return values&lt;/em&gt;. There are data structures and dynamic code generation tasks that would be impossible to build without it, but the &lt;em&gt;odds&lt;/em&gt; are if you’re thinking about using it, &lt;strong&gt;don’t&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Write to the parent from the child
&lt;/h4&gt;

&lt;p&gt;Slightly more often there’s a need for the child to tell the parent what to do.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="nonlocal"&gt;
nonlocal &lt;a href="https://docs.python.org/3/reference/simple_stmts.html#the-nonlocal-statement" title="See the documentation for “nonlocal”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Used exclusively inside &lt;em&gt;nested&lt;/em&gt; functions / methods (also known as &lt;em&gt;closures&lt;/em&gt;) to bind a &lt;em&gt;pre-existing&lt;/em&gt; NAME in the &lt;em&gt;parent&lt;/em&gt;’s namespace. This allows a STATEMENT in the child to have write access to a NAME defined in its parent. &lt;em&gt;Must&lt;/em&gt; appear before any reference to that NAME in the local scope.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;An adjective formed by combining the prefix &lt;strong&gt;&lt;em&gt;non-&lt;/em&gt;&lt;/strong&gt; (“not”) and &lt;strong&gt;&lt;em&gt;local&lt;/em&gt;&lt;/strong&gt; (“pertaining to a particular place”). This is a primarily scientific and technical &lt;em&gt;neologism&lt;/em&gt; (literally “new word”) that has no true general meaning, only specific meanings within specific fields. In programming it signifies a &lt;a href="https://en.wikipedia.org/wiki/Non-local_variable"&gt;non-local variable&lt;/a&gt;, meaning “[a NAME that is] accessible in neither the &lt;em&gt;local&lt;/em&gt; nor the &lt;em&gt;global&lt;/em&gt; namespace”. In Python it very specifically applies to the namespace of the enclosing function from the point of view of a nested function, and is also a keyword that binds a &lt;em&gt;local&lt;/em&gt; NAME within that nested function into the &lt;strong&gt;&lt;em&gt;nonlocal&lt;/em&gt;&lt;/strong&gt; namespace, allowing it to be &lt;em&gt;writable&lt;/em&gt; from within the nested function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because it is &lt;em&gt;only&lt;/em&gt; of use inside a nested function, usage will always involve some enclosing structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;OUTER_FUNCTION&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;VALUE&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;INNER_FUNCTION&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;nonlocal&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;
        &lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;EXPRESSION&lt;/span&gt;
    &lt;span class="n"&gt;INNER_FUNCTION&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The key thing to understand is that &lt;code&gt;nonlocal&lt;/code&gt; is used in a &lt;em&gt;similar&lt;/em&gt; fashion to &lt;code&gt;global&lt;/code&gt;, but with the more limited goal of making a variable in the parent of a nested function writable. It has similar caveats as well, though the fact that its impact isn’t felt by the &lt;em&gt;entire&lt;/em&gt; program makes it inherently less dangerous. Still, its uses are &lt;em&gt;very&lt;/em&gt; narrow and &lt;em&gt;relatively&lt;/em&gt; few, so try not to abuse it when argument passing will do.&lt;/p&gt;

&lt;h4&gt;
  
  
  Kill it with fire
&lt;/h4&gt;

&lt;p&gt;So far you’ve seen how to &lt;em&gt;add&lt;/em&gt; a NAME to a namespace, but how do you &lt;em&gt;remove&lt;/em&gt; a NAME from a namespace?&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="del"&gt;
del &lt;a href="https://docs.python.org/3/reference/simple_stmts.html#the-del-statement" title="See the documentation for “del”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Used to delete a NAME (or NAMEs) from a namespace; if no other references exist to the OBJECT that NAME referred to, the underlying OBJECT is deleted as well.&lt;/dd&gt;
&lt;dd&gt;Can also be used to delete an attribute of an OBJECT or a member (or members) of a COLLECTION &lt;em&gt;if&lt;/em&gt; the specific type has allowed this operation.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A contraction of &lt;strong&gt;&lt;em&gt;delete&lt;/em&gt;&lt;/strong&gt; , which is in turn a verb derived from the Latin &lt;strong&gt;&lt;em&gt;deletus&lt;/em&gt;&lt;/strong&gt;. Its general meaning is to “destroy or eradicate”, “erase or smudge out”, and “utterly remove”, and while the specific meaning in computing is a little less violent it has similar effect. In Python the meaning is a little indirect: &lt;strong&gt;&lt;em&gt;del&lt;/em&gt;&lt;/strong&gt; is used to delete a &lt;em&gt;reference&lt;/em&gt; that the &lt;em&gt;user&lt;/em&gt; directly controls, which &lt;em&gt;may&lt;/em&gt; indirectly trigger deletion of the thing referred to from process memory, which the &lt;em&gt;interpreter&lt;/em&gt; controls.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The most common usage is to provide a single NAME:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;del&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However it is also valid to provide multiple NAMEs, comma separated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;del&lt;/span&gt; &lt;span class="n"&gt;NAME_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;NAME_B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;NAME_C&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In either case Python will proceed left to right, deleting each NAME from the namespace in which the &lt;code&gt;del&lt;/code&gt; invocation has occurred.&lt;/p&gt;

&lt;p&gt;Because the effect is limited to the &lt;em&gt;closest&lt;/em&gt; namespace, you have to use &lt;code&gt;global&lt;/code&gt; or &lt;code&gt;nonlocal&lt;/code&gt; to delete a NAME outside the local namespace:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;global&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;
&lt;span class="k"&gt;del&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also use &lt;code&gt;del&lt;/code&gt; to delete attributes of OBJECTs and member(s) of COLLECTIONs, however what actually happens depends on the type of the OBJECT / COLLECTION involved. The &lt;strong&gt;list&lt;/strong&gt; COLLECTION, for instance, allows you to use &lt;code&gt;del&lt;/code&gt; to delete both individual &lt;em&gt;indices&lt;/em&gt; and &lt;em&gt;slices&lt;/em&gt; of its contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;del&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# deletes the first item
&lt;/span&gt;&lt;span class="k"&gt;del&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;[:]&lt;/span&gt; &lt;span class="c1"&gt;# deletes all items
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We’ll leave a further exploration of this for when we discuss the &lt;em&gt;builtin&lt;/em&gt; types in later articles. In the meantime, let’s finally meet the keyword that will let you start defining such type-specific behaviors for yourself.&lt;/p&gt;

&lt;h3&gt;
  
  
  One for Defining New Types of Object
&lt;/h3&gt;

&lt;p&gt;Up until this point I’ve been pretty vague about what an OBJECT actually is. In the &lt;a href="https://en.wikipedia.org/wiki/Class_(computer_programming)"&gt;Object-Oriented Programming&lt;/a&gt; paradigm, also known as OOP, an object is, basically, a &lt;em&gt;thing&lt;/em&gt; that has both &lt;em&gt;state&lt;/em&gt; (in the form of named &lt;em&gt;attributes&lt;/em&gt;) and &lt;em&gt;behavior&lt;/em&gt; (in the form of callable &lt;em&gt;methods&lt;/em&gt;). Two individual things of the same &lt;em&gt;type&lt;/em&gt; may have different specific values for those attributes – we call them different &lt;em&gt;instances&lt;/em&gt; of the same &lt;em&gt;type&lt;/em&gt; – but they share the same overall &lt;em&gt;interface&lt;/em&gt;. However there’s a bit more to it than that; the programmer should be able to define partial interfaces, which we’ll call &lt;em&gt;traits&lt;/em&gt;, that types with similar needs can implement in different ways, such that they all share some common attributes and behaviors (a property known as &lt;a href="https://en.wikipedia.org/wiki/Polymorphism_(computer_science)"&gt;&lt;em&gt;polymorphism&lt;/em&gt;&lt;/a&gt;). Additionally these traits should be able to be passed from more generic types to more specific types via an &lt;a href="https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)"&gt;inheritance&lt;/a&gt; mechanism, much as a parent passes on traits to their children.&lt;/p&gt;

&lt;p&gt;You can take an example from nature; a duck is a type of bird, and it inherits certain traits it shares will all birds. All birds are a type of animal, and thus inherit certain traits shared will all animals. An animal is a type of life, and so on. Thus Howard, a specific duck, has all the attributes and behaviors of life, animals, birds, and ducks, all rolled up nicely in one instance of the duck type.&lt;/p&gt;

&lt;p&gt;In fact it is from just such an example of biological &lt;em&gt;classification&lt;/em&gt; that Python takes the keyword it uses for defining new types of OBJECT.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="class"&gt;
class &lt;a href="https://docs.python.org/3/reference/compound_stmts.html#class-definitions" title="See the documentation for “class”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Used to define a template for a new type of OBJECT.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A noun borrowed via the French &lt;strong&gt;&lt;em&gt;classe&lt;/em&gt;&lt;/strong&gt; from the Latin &lt;strong&gt;&lt;em&gt;classis&lt;/em&gt;&lt;/strong&gt; , which meant “a division, army, fleet”, “the people of Rome under arms”, and, oddly specifically, “any one of the six orders into which Servius Tullius divided the Roman people for the purpose of taxation”, which goes a long way towards explaining why English is such a difficult language to master. The general meaning we use here, though, is loosely borrowed from evolutionary taxonomy, implying “a group that shares certain inheritable traits”. The specific meaning in Python comes from Object-Oriented Programming and implies “a template for creating objects that share common inheritable state (attributes) and behavior (methods)”.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In Python you define a new type by creating a new NAME that is also a CALLABLE; from now on we’ll call that a CLASS. The &lt;code&gt;class&lt;/code&gt; keyword is used to start a new BLOCK that defines the &lt;em&gt;implementation&lt;/em&gt; of that CLASS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CLASS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The CLASS can now be called to create a new instance of its type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CLASS&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now from the above you might expect that instance to have &lt;em&gt;no&lt;/em&gt; attributes or behavior, but in fact it does have the &lt;em&gt;minimal&lt;/em&gt; interface that all OBJECTs share (it can be printed and used in comparisons, for instance, though not very usefully). It receives this because &lt;em&gt;all&lt;/em&gt; new &lt;code&gt;class&lt;/code&gt; definitions implicitly inherit from &lt;a href="https://docs.python.org/3/library/functions.html#object"&gt;&lt;strong&gt;object&lt;/strong&gt;&lt;/a&gt;, which is Python’s &lt;em&gt;base type&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You can also specify a more generic CLASS (also known as a &lt;em&gt;superclass&lt;/em&gt;) to inherit from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CLASS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SUPERCLASS&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In fact you’ll often want to inherit from more than one superclass in order to mix together their various traits. This is done by separating the superclasses with commas:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CLASS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SUPERCLASS_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SUPERCLASS_B&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The notion of &lt;a href="https://docs.python.org/3.7/tutorial/classes.html#multiple-inheritance"&gt;multiple inheritance&lt;/a&gt; can get quite complicated when more than one superclass defines the same attribute or method NAMEs, so for now let’s just keep our classes simple and assume no such overlap.&lt;/p&gt;

&lt;p&gt;Just defining the CLASS and its superclasses without giving it some attributes and methods of its own is unusual, so you’ll &lt;em&gt;normally&lt;/em&gt; see at minimum a custom &lt;em&gt;initializer&lt;/em&gt; method, which by convention is done by defining the special &lt;em&gt;double underscore&lt;/em&gt; (or “dunder”) &lt;em&gt;instance method&lt;/em&gt; &lt;strong&gt;__init__&lt;/strong&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CLASS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see an instance method is just another kind of function definition made using &lt;code&gt;def&lt;/code&gt;. In fact a METHOD is just a FUNCTION that’s been added to the CLASS’s namespace. That’s right, the &lt;code&gt;class&lt;/code&gt; keyword also creates a new kind of local namespace, this one remains attached to the CLASS and allows names to be looked up on either the &lt;em&gt;instance&lt;/em&gt; or the class itself using the &lt;em&gt;dot access&lt;/em&gt; patterns INSTANCE.NAME and CLASS.NAME. Notice the &lt;strong&gt;self&lt;/strong&gt; parameter name in the method definition above? That’s the name given by convention to the &lt;em&gt;first&lt;/em&gt; parameter of any instance method, and it’s how you access attributes set on the instance itself.&lt;/p&gt;

&lt;p&gt;This all sounds a little abstract, so let’s demonstrate it by building a CLASS for thinking about circles.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;math&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tau&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;diameter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;circumference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;tau&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;tau&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now if we create an instance representing the &lt;em&gt;unit circle&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;unit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Circle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can access and &lt;a href="https://docs.python.org/3/library/functions.html#print"&gt;&lt;strong&gt;print&lt;/strong&gt;&lt;/a&gt; the &lt;strong&gt;self.radius&lt;/strong&gt; attribute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And we can also call any of its methods, but notice how we never have to pass the radius of the circle as an argument, since the methods can all access that via &lt;strong&gt;self&lt;/strong&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;diameter&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;  
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;circumference&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;area&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since they’re the fundamental building block of &lt;em&gt;everything&lt;/em&gt; in Python’s &lt;a href="https://docs.python.org/3/reference/datamodel.html"&gt;data model&lt;/a&gt;, there’s a &lt;em&gt;lot&lt;/em&gt; than can be said &lt;a href="https://docs.python.org/3.7/tutorial/classes.html#classes"&gt;about classes in Python&lt;/a&gt;, and there’s a lot of subtleties that can go into their proper use (and improper abuse), so we’ll come back to them often in later articles in this series. For now though the important thing is to start to recognize how &lt;code&gt;class&lt;/code&gt; is used to create them, and how instances are used when accessing their attributes and methods. It’s also helpful to know that there are quite a few “dunder” methods, and that these are used to implement a lot of the common “under-the-hood” functionality that supports all the &lt;a href="https://docs.python.org/3/library/functions.html"&gt;builtin functions&lt;/a&gt; we’ll start to meet in later articles.&lt;/p&gt;

&lt;h3&gt;
  
  
  One for Working Within a Context
&lt;/h3&gt;

&lt;p&gt;Sometimes there are actions you &lt;em&gt;always&lt;/em&gt; want to perform within a specific context, such as committing a database transaction or closing a network connection. These sort of things &lt;em&gt;usually&lt;/em&gt; involve some form of &lt;em&gt;exception handling&lt;/em&gt; or other boilerplate specific to the task, and that can lead to a lot of boilerplate that needs to be repeated. An example is something as simple as reading a file: opening the file must necessarily mean getting some resources from the underlying operating system, and you always want to free up those resources, even if you accidentally tried to open a file that didn’t exist or which you did not have permissions to read. The file is a &lt;em&gt;type&lt;/em&gt; of OBJECT that will always carry that contextual need to manage a resource with it, and so you’ll need to write the same &lt;code&gt;try&lt;/code&gt; and &lt;code&gt;finally&lt;/code&gt; boilerplate &lt;em&gt;every&lt;/em&gt; time you open a file.&lt;/p&gt;

&lt;p&gt;Unless the language you’re working in provides a convenient means of ensuring that it happens for you.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="with"&gt;
with &lt;a href="https://docs.python.org/3/reference/compound_stmts.html#the-with-statement" title="See the documentation for “with”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Starts a &lt;a href="https://docs.python.org/3/reference/datamodel.html#context-managers" title="Read about context managers in Python"&gt;&lt;em&gt;context manager&lt;/em&gt;&lt;/a&gt; BLOCK, which ensures that the indented STATEMENT(s) below it are performed within the context of the OBJECT being managed.&lt;/dd&gt;
&lt;dd&gt;Can also be marked with &lt;code&gt;async&lt;/code&gt;
, to start an &lt;code&gt;async with&lt;/code&gt;
, see below.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;A Middle English preposition that takes its pronunciation from one Old English term, &lt;strong&gt;&lt;em&gt;wið&lt;/em&gt;&lt;/strong&gt; (“against”), but takes its modern meaning from another, &lt;strong&gt;&lt;em&gt;mid&lt;/em&gt;&lt;/strong&gt; (“in association with”) which in turn comes from the German &lt;strong&gt;&lt;em&gt;mit&lt;/em&gt;&lt;/strong&gt;. The meaning in Python derives from &lt;strong&gt;&lt;em&gt;within&lt;/em&gt;&lt;/strong&gt; , meaning “inside the scope or context of [some thing or event]“.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Any use of &lt;code&gt;with&lt;/code&gt; requires an EXPRESSION that evaluates to an OBJECT that satisfies the &lt;a href="https://docs.python.org/3/library/stdtypes.html#context-manager-types"&gt;context manager type&lt;/a&gt;’s interface, and thus has implemented both the &lt;strong&gt;__enter__&lt;/strong&gt; and &lt;strong&gt;__exit__&lt;/strong&gt; “dunder” methods. From now on we’ll call that EXPRESSION a CONTEXT_MANAGER, to make the examples clearer.&lt;/p&gt;

&lt;p&gt;In the most basic usage you simply start a new BLOCK:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;CONTEXT_MANAGER&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which will enter the context by calling &lt;code&gt;CONTEXT_MANAGER. __enter__ ()&lt;/code&gt;, execute the STATEMENT within the context, and then exit the context by calling &lt;code&gt;CONTEXT_MANAGER. __exit__ ()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But most often you’ll want to use &lt;code&gt;as&lt;/code&gt; to assign the OBJECT returned by &lt;code&gt;CONTEXT_MANAGER. __enter__ ()&lt;/code&gt; to an alias NAME so the BLOCK can work with it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;CONTEXT_MANAGER&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This can save you a lot of boilerplate, especially with the many standard OBJECTs that already implement the context manager interface. For instance the recommended way to read a file’s contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;contents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Is &lt;em&gt;much&lt;/em&gt; simpler than what you’d otherwise have to write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;contents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Occasionally you’ll want to work within more than one CONTEXT_MANAGER:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;CONTEXT_MANAGER_A&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;NAME_A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CONTEXT_MANAGER_B&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;NAME_B&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which works exactly the same as nesting one &lt;code&gt;with&lt;/code&gt; inside another:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;CONTEXT_MANAGER_A&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;NAME_A&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;CONTEXT_MANAGER_B&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;NAME_B&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You’ll get to meet quite a few context managers as you work your way through the Python standard library. And now you know that building your own is just a matter of creating a new &lt;code&gt;class&lt;/code&gt; and implementing a couple of “dunder” methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  And, Finally… Two for Working Asynchronously
&lt;/h3&gt;

&lt;p&gt;Any function or method you write with &lt;code&gt;def&lt;/code&gt; alone is, by definition, &lt;em&gt;synchronous&lt;/em&gt;, meaning that the moment you call it your running code has to stop everything else and &lt;em&gt;wait&lt;/em&gt;, however long it takes, for your function to either &lt;code&gt;return&lt;/code&gt; or &lt;code&gt;yield&lt;/code&gt; control back to it before the rest of your code can be executed. For most tasks that happen on a local machine this is sensible and perfectly fine, especially when you &lt;em&gt;need&lt;/em&gt; the answer before you can proceed any further.&lt;/p&gt;

&lt;p&gt;In much modern programming though it’s often necessary to interact with things that &lt;em&gt;don’t&lt;/em&gt; respond quickly, like network interactions with servers that could be a world away, and sometimes there’s &lt;em&gt;plenty&lt;/em&gt; of work you could be doing while you wait for them to respond. You still &lt;em&gt;need&lt;/em&gt; the answer, but you need it &lt;em&gt;eventually&lt;/em&gt;. One way this can be addressed is via &lt;a href="https://en.wikipedia.org/wiki/Asynchrony_(computer_programming)"&gt;asynchronous programming&lt;/a&gt;, which as of Python 3.5 has become a first-class part of the Python language.&lt;/p&gt;

&lt;p&gt;Asynchronous programming is an advanced topic that is fairly specialized, so using it is well outside the scope of this series. It’s also quite a new addition to the language, and its usage has fluctuated from version to version. Rather than try to summarize those changed I’ll just describe the very basics of using these keywords in Python 3.7 below. If, however, you’re curious there’s a &lt;a href="https://realpython.com/async-io-python/"&gt;quite good async primer available here&lt;/a&gt; that seems to be kept up to date.&lt;/p&gt;

&lt;p&gt;Because asynchronous code does the same work as synchronous code, but &lt;em&gt;schedules&lt;/em&gt; the execution of that work differently, only two new keywords needed to be added to the language.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="async"&gt;
async &lt;a href="https://docs.python.org/3/reference/compound_stmts.html#coroutine-function-definition" title="See the documentation for “async”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Used to mark another KEYWORD as one that works asynchronously. As such, &lt;code&gt;async&lt;/code&gt;
&lt;em&gt;cannot&lt;/em&gt; appear on its own.&lt;/dd&gt;
&lt;dd&gt;With &lt;code&gt;def&lt;/code&gt;
as &lt;code&gt;async def&lt;/code&gt;
to define an asynchronous function, also known as a COROUTINE.&lt;/dd&gt;
&lt;dd&gt;With &lt;code&gt;for&lt;/code&gt;
as &lt;code&gt;async for&lt;/code&gt;
to loop over an &lt;em&gt;asynchronous iterator&lt;/em&gt; inside an &lt;code&gt;async def&lt;/code&gt;
.&lt;/dd&gt;
&lt;dd&gt;With &lt;code&gt;with&lt;/code&gt;
as &lt;code&gt;async with&lt;/code&gt;
to use an &lt;em&gt;asynchronous context manager&lt;/em&gt; inside an &lt;code&gt;async def&lt;/code&gt;
, see below.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;As you may have guessed, this is a contraction of the Modern English word &lt;strong&gt;&lt;em&gt;asynchronous&lt;/em&gt;&lt;/strong&gt; , which is formed by combining the Latin roots &lt;strong&gt;&lt;em&gt;a-&lt;/em&gt;&lt;/strong&gt; (“not”) and &lt;strong&gt;&lt;em&gt;syn-&lt;/em&gt;&lt;/strong&gt; (“together”) with &lt;strong&gt;&lt;em&gt;Khronus&lt;/em&gt;&lt;/strong&gt; , the Ancient Greek personification of &lt;em&gt;time&lt;/em&gt;. It unambiguously means “not occurring at the same time”. In Python it more specifically marks an operation as “not occurring in the same time as the caller”, which allows the caller to wait for the result of that operation, which will occur at &lt;em&gt;some&lt;/em&gt; point in the future.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because the meaning of &lt;code&gt;async&lt;/code&gt; is tied to the KEYWORD it marks, its usage is always the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;KEYWORD&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However, unlike other any other keyword we’ve seen, the usage of &lt;code&gt;async&lt;/code&gt; will &lt;em&gt;always&lt;/em&gt; begin with the definition of a new COROUTINE:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;COROUTINE&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Both of the other forms of &lt;code&gt;async&lt;/code&gt; exist to allow you to work &lt;em&gt;within&lt;/em&gt; a COROUTINE with &lt;em&gt;other&lt;/em&gt; COROUTINEs, and thus they can only exist &lt;em&gt;inside&lt;/em&gt; an &lt;code&gt;async def&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For instance you can use &lt;code&gt;async for&lt;/code&gt; to loop over an &lt;a href="https://docs.python.org/3/reference/datamodel.html#asynchronous-iterators"&gt;asynchronous iterator&lt;/a&gt; such as a GENERATOR COROUTINE (which is simply a COROUTINE that uses &lt;code&gt;yield&lt;/code&gt; instead of &lt;code&gt;return&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;COROUTINE&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;GENERATOR_COROUTINE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also use &lt;code&gt;async with&lt;/code&gt; to perform work within the context of an &lt;a href="https://docs.python.org/3/reference/datamodel.html#asynchronous-context-managers"&gt;asynchronous context manager&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;COROUTINE&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;CONTEXT_MANAGER_COROUTINE&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;NAME&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;STATEMENT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Virtually every COROUTINE will need to wait on other COROUTINE(s), which is why there’s another keyword that can only be used within an &lt;code&gt;async def&lt;/code&gt;.&lt;/p&gt;

&lt;dl&gt;
&lt;dt id="await"&gt;
await &lt;a href="https://docs.python.org/3/reference/expressions.html#await-expression" title="See the documentation for “await”"&gt;🗎&lt;/a&gt;
&lt;/dt&gt;
&lt;dd&gt;Used to suspend the execution of the COROUTINE it is found &lt;em&gt;within&lt;/em&gt; and waits for the COROUTINE to its right to complete; can only be used inside an &lt;code&gt;async def&lt;/code&gt;
.&lt;/dd&gt;
&lt;/dl&gt;

&lt;blockquote&gt;
&lt;p&gt;From the Middle English verb &lt;strong&gt;&lt;em&gt;awaiten&lt;/em&gt;&lt;/strong&gt; (“to wait for”) from the Old French &lt;strong&gt;&lt;em&gt;awaitier&lt;/em&gt;&lt;/strong&gt; / &lt;strong&gt;&lt;em&gt;agaitier&lt;/em&gt;&lt;/strong&gt; (“to lie in wait for, watch, or observe”). The general sense is more active and hostile than &lt;strong&gt;&lt;em&gt;wait&lt;/em&gt;&lt;/strong&gt; , which it’s a modification of. In asynchronous programming it means to “[suspend execution] and wait [for something to finish]“.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;await&lt;/code&gt; keyword is always going to be used to call a COROUTINE inside an &lt;code&gt;async def&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;OUTER_COROUTINE&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;COROUTINE&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which will pause the execution of the outer COROUTINE and wait until the called COROUTINE, eventually, returns control. Any VALUE returned by the called COROUTINE is passed through &lt;code&gt;await&lt;/code&gt;, so you can think of &lt;code&gt;await COROUTINE()&lt;/code&gt; as an asynchronous EXPRESSION that will, &lt;em&gt;eventually&lt;/em&gt; evaluate to whatever the COROUTINE returns when called.&lt;/p&gt;

&lt;p&gt;Of course now that you’ve got the basics of doing asynchronous work down, how do you actually &lt;em&gt;perform&lt;/em&gt; such work? Well, as of Python 3.7 that still requires using functionality provided by the &lt;a href="https://docs.python.org/3/library/asyncio.html"&gt;asyncio&lt;/a&gt; module, the details of which are &lt;em&gt;well&lt;/em&gt; outside the scope of these articles. See the &lt;a href="https://realpython.com/async-io-python/"&gt;async primer&lt;/a&gt; I mentioned earlier for details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Whew…
&lt;/h2&gt;

&lt;p&gt;There you have them, Python’s 35 keywords: in and of themselves not enough to make you fluent in Python, but if you’ve read this far and digested it you’re well on your way to truly &lt;em&gt;understanding&lt;/em&gt; (and not merely &lt;em&gt;using&lt;/em&gt;) the skeleton of &lt;em&gt;what is going on&lt;/em&gt; in one of the fastest growing general programming languages around. In the next post in this series we’ll take a step away from &lt;em&gt;words&lt;/em&gt; and look instead at &lt;em&gt;symbols&lt;/em&gt;, with a dive into Python’s slightly smaller, but thankfully &lt;em&gt;much&lt;/em&gt; simpler, list of &lt;em&gt;operators&lt;/em&gt;.&lt;/p&gt;







&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;There are 35 keywords as of Python 3.7, the current major version of the language as of the time of writing. New keywords are added quite rarely, and it’s even more rare for keywords to be removed, but in whatever version you’re on you can use &lt;code&gt;from keyword import kwlist; print(kwlist)&lt;/code&gt; to view the current list. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;Putting &lt;code&gt;def&lt;/code&gt; on a single line makes the equivalence with &lt;code&gt;lambda&lt;/code&gt; more obvious, but for the sake of readability don’t do this very often. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>learning</category>
      <category>reference</category>
    </item>
    <item>
      <title>Look Before You Leap Year</title>
      <dc:creator>Michael Morehouse</dc:creator>
      <pubDate>Wed, 12 Jun 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/yawpitch/look-before-you-leap-year-no4</link>
      <guid>https://dev.to/yawpitch/look-before-you-leap-year-no4</guid>
      <description>&lt;h2&gt;
  
  
  Leap Years are Stupid
&lt;/h2&gt;

&lt;p&gt;Actually, &lt;a href="https://en.wikipedia.org/wiki/Leap_year"&gt;leap years&lt;/a&gt; are &lt;em&gt;absurd&lt;/em&gt;, and consequently &lt;em&gt;extremely&lt;/em&gt; stupid. From the day some long-dead forbear of astronomers&lt;sup id="fnref1"&gt;1&lt;/sup&gt; and astrologers&lt;sup id="fnref2"&gt;2&lt;/sup&gt; peered up at both the Sun and Moon in the same sky, put two and two together, and realized that they were in the same place as the last time it was &lt;em&gt;this bloody cold&lt;/em&gt;&lt;sup id="fnref3"&gt;3&lt;/sup&gt;, we’ve been &lt;em&gt;stuck&lt;/em&gt; with them, whether we realized it or not. In a sad quest to pretend that an &lt;em&gt;ellipse&lt;/em&gt;&lt;sup id="fnref4"&gt;4&lt;/sup&gt; is, nevertheless, a &lt;em&gt;circle&lt;/em&gt;, we doomed ourselves to forever having to &lt;em&gt;maybe&lt;/em&gt; offset our mental clocks when looking sufficiently far forward or back in time, lest &lt;em&gt;history&lt;/em&gt; and &lt;em&gt;reality&lt;/em&gt; drift dangerously out of sync.&lt;/p&gt;

&lt;p&gt;In the modern West the leap year is usually attributed to Julius Caesar, who so succesfully promoted the concept that it &lt;em&gt;only&lt;/em&gt; took the Romans fifty years after his death&lt;sup id="fnref5"&gt;5&lt;/sup&gt; to reliably incorporate it into what history now tries to forget as the Julian calendar. Unfortunately Caesar and his scholars – being pretty poor programmers – had made a &lt;em&gt;wee&lt;/em&gt; bit of a rounding error, and so in 1582 Pope Gregory XIII was forced to “correct” the situation&lt;sup id="fnref6"&gt;6&lt;/sup&gt; by shifting the average year from 365.25 to 365.2425 days. And so was born the Gregorian calendar we all know and use today&lt;sup id="fnref7"&gt;7&lt;/sup&gt;. This approximation was &lt;em&gt;good enough&lt;/em&gt; for the Church, and so history has been positively &lt;em&gt;littered&lt;/em&gt; with February 29th every so often ever since.&lt;/p&gt;

&lt;p&gt;I feel &lt;em&gt;contempt&lt;/em&gt; for the leap year. So why am I writing about it?&lt;/p&gt;

&lt;h3&gt;
  
  
  It’s an Ideal Programming Exercise
&lt;/h3&gt;

&lt;p&gt;It turns out that determining if a particular Gregorian calendar year&lt;sup id="fnref8"&gt;8&lt;/sup&gt; is, or is not, a leap year is a problem with some &lt;em&gt;unusual&lt;/em&gt; properties that make it an &lt;em&gt;excellent&lt;/em&gt; test of a programmer’s mastery of some of the most fundamental tools they have available to them. That, in turn, means that it’s not only a very good challenge for the budding programmer, but it could – and I’d argue &lt;em&gt;should&lt;/em&gt; – also be used as an impressively diagnostic question to ask a candidate in a technical interview.&lt;/p&gt;

&lt;p&gt;In every programming language that I’ve yet encountered there exists a short, simple, and performance-optimal solution for this problem. Indeed in most cases the optimum implementation is a single line long&lt;sup id="fnref9"&gt;9&lt;/sup&gt;, but most who attempt it will end up with something quite far from elegant on the first try. And, unlike most problems with such an ideal answer, if solved inelegantly it’s also almost certainly solved inefficiently.&lt;/p&gt;

&lt;p&gt;The beauty of the exercise is that the pathway to the ideal solution can easily be discovered just by employing the sort of straightforward reasoning that is the bread and butter of professional programming. It encourages you to &lt;em&gt;think&lt;/em&gt; through your solution by first &lt;em&gt;understanding&lt;/em&gt; what you’re trying to solve.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why does all this matter?
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;From the masochist to the sadist: "Hurt me."&lt;br&gt;&lt;br&gt;
From the sadist to the masochist: "No."&lt;/p&gt;

&lt;p&gt;&lt;cite&gt;Clive Barker, for one&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well maybe you’ve just joined &lt;a href="https://exercism.io"&gt;Exercism&lt;/a&gt;. Or perhaps you’re just looking for a good code kata. Or you’ve been assigned to a team working with calendars. Maybe you’re simply a masochist. Or worse, perhaps your interviewer is a sadist.&lt;/p&gt;

&lt;p&gt;One way or another, let’s assume you’ve just been assigned the task of implementing a function &lt;code&gt;is_leap&lt;/code&gt; that takes an &lt;em&gt;integer&lt;/em&gt; argument &lt;strong&gt;year&lt;/strong&gt; and returns a &lt;em&gt;Boolean&lt;/em&gt; result.&lt;/p&gt;

&lt;h3&gt;
  
  
  Luckily There’s an Algorithm for That
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Every year that is exactly divisible by 4 is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400.&lt;/p&gt;

&lt;p&gt;&lt;cite&gt;United States Naval Observatory&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unusually for a programming problem the basic algorithm will most likely be presented to you up front, and you need merely implement it. The algorithm is very old, and has been stated in more or less precise ways over the centuries, so it helps to transcribe it into pseudocode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IF the year is a multiple of 4 THEN
    IF the year is a multiple of 100 THEN
        IF the year is a multiple of 400 THEN
            RETURN True
        RETURN False
    RETURN True
RETURN False
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice the chevron-like &lt;strong&gt;&amp;gt;&lt;/strong&gt; shape that results from the multiple layers of nesting? It turns out that’s fairly typical of a first-pass mental model of the algorithm, at least in a language where indentation is meaningful. It’s not the most elegant form – we’ll get to that – but it conveys some important properties about our algorithm:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;It’s &lt;em&gt;simple&lt;/em&gt;&lt;/strong&gt; : You have one argument, you perform (up to) three tests on it using a basic math operation, and it’s the &lt;em&gt;same&lt;/em&gt; operation for all three tests. The most complicated thing you &lt;em&gt;need&lt;/em&gt; to know to solve it is &lt;em&gt;how&lt;/em&gt; your language performs the &lt;a href="https://en.wikipedia.org/wiki/Modulo_operation"&gt;modulo&lt;/a&gt; operation. Once you’ve got that, comparison with zero, simple conditions, and how to return a Boolean-equivalent value in your language of choice you’re &lt;em&gt;golden&lt;/em&gt;. After five minutes of well-focussed instruction a novice to &lt;em&gt;any&lt;/em&gt; high-level language should be able to bash out a general solution that mimics the above pseudocode.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;It’s &lt;em&gt;progressive&lt;/em&gt;&lt;/strong&gt; : After each successive test you either know the answer or you know what to ask next. Most algorithms – indeed most non-trivial problems – aren’t nearly so cut and dried. There is no need for complicated branching, or your language’s version of &lt;code&gt;else_if&lt;/code&gt;, you need merely proceed forward until the moment you are &lt;em&gt;certain&lt;/em&gt; you know the answer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;It’s &lt;em&gt;robust&lt;/em&gt;&lt;/strong&gt; : There are no bugs, no weird or exceptional edge cases; every possible (integer) input maps directly to a single, unwavering output. Additionally there are no side effects, no state to mutate… you don’t even need to carry forward the results of each modulo test. It’s as &lt;em&gt;pure&lt;/em&gt; as a function can get, and so constrained that you can know from glancing at it if it will always return the correct answer.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Know Thine Enemy and Thine Self
&lt;/h3&gt;

&lt;p&gt;Understanding the algorithm is great, but understanding the problem it’s addressing is &lt;em&gt;better&lt;/em&gt;, and the insights gained above tell us nothing about the &lt;em&gt;domain&lt;/em&gt; we’re working with. We’ve treated the various modulo tests as black boxes, but really they’re specialized filters that apply to specific, progressively smaller, subsets of the overall domain of Gregorian years.&lt;/p&gt;

&lt;p&gt;So what can we glean from looking a little more closely at those tests?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The answer is &lt;em&gt;usually&lt;/em&gt; no&lt;/strong&gt; : From the very first test we know that 3 out of every 4 years &lt;em&gt;won’t&lt;/em&gt; be a leap year. By looking at the rest we can see that an additional 3 out of every 4 &lt;em&gt;centuries&lt;/em&gt; won’t be either… returning False by default will correctly answer &amp;gt;75% of all possible questions without any additional effort.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;They &lt;em&gt;rapidly&lt;/em&gt; cull the herd&lt;/strong&gt; : Just 1 in every 4 years is even a &lt;em&gt;potential&lt;/em&gt; leap year, just 1 year in every 100 is a century, and just 1 year in 400 is a century that’s also a leap year… we learn a &lt;em&gt;lot&lt;/em&gt; fast about our year.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;They have a &lt;em&gt;natural&lt;/em&gt; order&lt;/strong&gt; : Each test is a special case of the previous one, and from the above we know that each applies to a progressively smaller number of cases. Sure, if we test division by 400 first we solve the problem very quickly in an &lt;em&gt;extremely&lt;/em&gt; small number of cases, but if it &lt;em&gt;doesn’t&lt;/em&gt; give us the answer it also doesn’t give us any useful knowledge about the question being asked. Proceed in the natural order, however, and each step refines the last.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Taking the first of these to heart we can rewrite our pseudocode so that we need only ever consider the paths to a True answer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IF the year is a multiple of 4 THEN
    IF the year is NOT a multiple of 100 THEN
        RETURN True
    IF the year is a multiple of 400 THEN
        RETURN True
RETURN False
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But this is interesting. Glancing at the above we can now see that there are &lt;em&gt;exactly&lt;/em&gt; two criteria that result in a year being a leap year, and that those criteria are exclusive. So for any given year the answer is True if &lt;em&gt;either&lt;/em&gt; of the following is True:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the year is a multiple of 4, but not of 100&lt;/li&gt;
&lt;li&gt;the year is a multiple of 4, 100, and 400&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The Path of the Righteous
&lt;/h3&gt;

&lt;p&gt;From either of the above the path to the ideal form can pretty neatly unroll in front of you. Especially if we introduce some abstraction and call our three tests &lt;strong&gt;a&lt;/strong&gt; , &lt;strong&gt;b&lt;/strong&gt; , and &lt;strong&gt;c&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For instance, if we progressively introduce &lt;em&gt;Boolean logic&lt;/em&gt; operations to our pseudocode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IF a THEN
    IF NOT b OR c THEN
        RETURN True
RETURN False
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;… becomes…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IF a AND (IF NOT b OR c) THEN
    RETURN True
RETURN False
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;… which, in a language that allows you to return an expressions becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RETURN a AND (NOT b OR c)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Oooh, that’s pretty concise.&lt;/p&gt;

&lt;h3&gt;
  
  
  Or We Could Go The Other Way
&lt;/h3&gt;

&lt;p&gt;And if we’d started from the criteria we derived alone?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IF a AND NOT b THEN
    RETURN True
IF a AND b AND c THEN
    RETURN True
RETURN False
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;… can be combined with another Boolean logic operator as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IF (a AND NOT b) OR (a AND b AND c) THEN
    RETURN True
RETURN False
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;… which allows us to extract the common operation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IF a AND ((NOT b) OR (b AND c)) THEN
    RETURN True
RETURN False
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;… but there’s no reason to perform the same Boolean test twice, so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IF a AND (NOT b OR c) THEN
    RETURN True
RETURN False
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;… and since we’re returning expressions brings us right back to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RETURN a AND (NOT b OR c)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  All Roads Lead Here
&lt;/h3&gt;

&lt;p&gt;And here lies the ultimate, optimal form. In a language that has &lt;em&gt;short-circuiting&lt;/em&gt; Boolean operations this is also the most performant form, because neither side of the &lt;code&gt;or&lt;/code&gt; will be evaluated for the vast majority of cases. Thankfully most languages either do short-circuit Boolean operations, or they tend to provide an alternate short-circuiting operator like &lt;code&gt;and_also&lt;/code&gt; and &lt;code&gt;or_else&lt;/code&gt;, so if you’re not sure yours does you’ll need to do a bit of research.&lt;/p&gt;

&lt;p&gt;Speaking of research, you &lt;em&gt;might&lt;/em&gt; be tempted to remove those parentheses, which certainly does look nicer. But therein lies the final lesson &lt;code&gt;is_leap&lt;/code&gt; can teach you; your ability to elide those parentheses depends &lt;em&gt;entirely&lt;/em&gt; on the &lt;a href="https://en.wikipedia.org/wiki/Evaluation_strategy"&gt;evaluation strategy&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Order_of_operations"&gt;operator precedence&lt;/a&gt; rules for your language. If your language evaluates left to right and binds &lt;code&gt;or&lt;/code&gt; at the same or lower precedence as &lt;code&gt;and&lt;/code&gt;, then no, you cannot remove those &lt;em&gt;significant&lt;/em&gt; parentheses. At least not without paying the price of checking division by 400 for a year that doesn’t even divide by 4.&lt;/p&gt;

&lt;p&gt;Which, like leap years, would be &lt;em&gt;stupid&lt;/em&gt;.&lt;/p&gt;







&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;Those who look up at the night sky and find it full of &lt;em&gt;stuff&lt;/em&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;Those who look up at the night sky and find it full of &lt;em&gt;nonsense&lt;/em&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;They’ve never &lt;em&gt;actually&lt;/em&gt; occupied the same absolute positions twice, of course, and never will, but neither angular momentum or orbital mechanics were yet available to our prototypical &lt;em&gt;yutz&lt;/em&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;Actually something closer to an ellipse being frantically sketched over and over on an imaginary two dimensional plane being pulled away from the artist in four dimensional spacetime. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;Caesar’s assassination, which famously happened on March 15th, 44 BCE, rather threw off the adoption of leap years. Things got a bit wonky until 8 CE and, since no one in this period could be relied on to write anything down consistently, we know exactly when he died, just not when &lt;em&gt;in our calendar&lt;/em&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;Gregory XIII may have been liturgically infallible, but the fact that we’ve since had to introduce &lt;em&gt;leap seconds&lt;/em&gt; tends to imply he wasn’t mathematically infallible as well. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn7"&gt;
&lt;p&gt;Unless you use the Hindu, Islamic, or Buddhist calendars, are Thai or Chinese, subscribe to the Juche ideology of North Korea, or measure everything in Unix time. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn8"&gt;
&lt;p&gt;Specifically, for the &lt;em&gt;truly&lt;/em&gt; pedantic, the &lt;em&gt;proleptic&lt;/em&gt; Gregorian calendar with &lt;em&gt;astronomical year numbering&lt;/em&gt; and a year zero. Which is &lt;em&gt;fine&lt;/em&gt; for the purposes of an exercise. To accurately reflect leap years actually &lt;em&gt;observed&lt;/em&gt; by humans you’d need at minimum a quite convoluted lookup table before the 9th century CE, and probably a time machine. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn9"&gt;
&lt;p&gt;Your mileage may vary; in Perl it may even be less, but no one will know because of the braces. If you’re writing in 8086 Assembly Language then, honestly, &lt;em&gt;why&lt;/em&gt; are you even reading this? ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Hugo Probably Isn't For You</title>
      <dc:creator>Michael Morehouse</dc:creator>
      <pubDate>Wed, 05 Jun 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/yawpitch/hugo-probably-isn-t-for-you-5e57</link>
      <guid>https://dev.to/yawpitch/hugo-probably-isn-t-for-you-5e57</guid>
      <description>&lt;h2&gt;
  
  
  Yes, It’s Quick Enough to Break Your Neck
&lt;/h2&gt;

&lt;p&gt;As it says on the proverbial box, the &lt;a href="https://gohugo.io"&gt;Hugo&lt;/a&gt; static site generator is &lt;em&gt;fast&lt;/em&gt;. Written in &lt;a href="https://golang.org"&gt;Go&lt;/a&gt;, Hugo promises &lt;em&gt;blistering&lt;/em&gt; speed that it certainly delivers, and I’ve got all the redness, pain, and burning discomfort needed to prove it.&lt;/p&gt;

&lt;p&gt;As I’m writing this – in Markdown, using Vim – all I have to do is save the file and the &lt;code&gt;hugo --server&lt;/code&gt; process I’ve left running in the background will render to HTML and update my browser window by the time I can shift my eyes over to it. Near instant feedback, even if I’ve made significant changes to multiple posts. Hugo is &lt;em&gt;quick&lt;/em&gt;, no question, and &lt;em&gt;once you’re up and running&lt;/em&gt; your site will do nothing but benefit from that speed, &lt;em&gt;especially&lt;/em&gt; when you’ve built up a significant catalogue of content.&lt;/p&gt;

&lt;h3&gt;
  
  
  But…
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Prepare ship for Ludicrous Speed!&lt;/p&gt;

&lt;p&gt;&lt;cite&gt;Colonel Sanders&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There’s something to be said for the idea that &lt;em&gt;fast&lt;/em&gt; isn’t necessarily &lt;em&gt;efficient&lt;/em&gt;, and in the hands of the ill-prepared it’s more often than not downright lethal. To get to the point where you could be reading this post I needed to do &lt;em&gt;vastly&lt;/em&gt; more than is implied by the &lt;a href="https://gohugo.io/getting-started/quick-start/"&gt;quickstart&lt;/a&gt; tutorial, and to even do &lt;em&gt;that&lt;/em&gt; I had to learn vastly more than I’d originally been prepared for. For &lt;em&gt;months&lt;/em&gt; the only thing Hugo actually delivered to me at blistering speed was a blistering headache. To be fair that wasn’t entirely Hugo’s fault – I made several false starts based on my aspirations more than my established needs – but I let my desire for speed and some exciting marketing copy trip me up right out of the gate. Which means I spent most of those months on my face, or often worse. All Hugo really did to help me during that time was &lt;em&gt;fail&lt;/em&gt; blisteringly fast; it &lt;em&gt;often&lt;/em&gt; did things that hindered me.&lt;/p&gt;

&lt;p&gt;Frankly it’s been an &lt;em&gt;agonizing&lt;/em&gt; journey. Pray you do not repeat it. Read on, and perhaps you won’t.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Bit of Background
&lt;/h3&gt;

&lt;p&gt;I am an experienced programmer, but I am &lt;strong&gt;emphatically not&lt;/strong&gt; a &lt;em&gt;web&lt;/em&gt; programmer. Before finally deciding to build out &lt;a href="https://www.yawpitchroll.com"&gt;my personal site&lt;/a&gt; as a home for my technical musings I’d only ever blogged with WYSIWYG tools like Tumblr, and then only sporadically. Professionally I mainly write backend and process automation code for the Visual Effects industry, so it’s charitable to say that I’ve developed the UX/UI design chops of your average wild-born gibbon. The last &lt;em&gt;serious&lt;/em&gt; experience I had with HTML/CSS predated the Marvel Cinematic Universe, and let’s just say there have been some plot developments since then.&lt;/p&gt;

&lt;p&gt;So I don’t have a lot of experience in web development, but I’m also not seeking to gain that experience. I’ve also got no large back catalogue of existing content that I need to port over, so I’m basically a greenfield blogger who wants to get up and running relatively quickly. I just need to make a choice about &lt;em&gt;how&lt;/em&gt; I’m going to do that.&lt;/p&gt;

&lt;p&gt;I wasn’t naive about that choice; I knew that any choice I made would involve a learning curve, I just wanted it to be slightly under the slope of, say, picking up &lt;a href="http://i.imgur.com/4AzYP.jpg"&gt;Dwarf Fortress&lt;/a&gt;, so I tried to approach the choice of tooling critically.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1: Establish Key Criteria
&lt;/h4&gt;

&lt;p&gt;After a fair amount of offline research I’d eventually narrowed down exactly what I &lt;em&gt;thought&lt;/em&gt; I wanted out of my Minimum Viable Website:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A static site I can host on the free tier of a CDN-backed service like Github Pages or Netlify&lt;/li&gt;
&lt;li&gt;A responsive, mobile-first theme with syntax highlighting and readable typography&lt;/li&gt;
&lt;li&gt;Modern markup practices; JSON-LD Structured Data, OpenGraph, and semantic HTML5&lt;/li&gt;
&lt;li&gt;Reasonable privacy practices; I live in the EU, so I must make a good-faith effort at GPDR compliance&lt;/li&gt;
&lt;li&gt;Easy social sharing to Facebook, LinkedIn, and Twitter, but &lt;em&gt;without&lt;/em&gt; unnecessary tracking&lt;/li&gt;
&lt;li&gt;Some form of commenting on posts, so I can hear from the readers I sincerely &lt;em&gt;hope&lt;/em&gt; will arrive&lt;/li&gt;
&lt;li&gt;Minimal ability to sign up for a mailing list; a lot of people left RSS after Google Reader died&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Fast&lt;/em&gt; load time; I’ve been &lt;em&gt;badly&lt;/em&gt; bitten by Tumblr; Google’s AMP pages &lt;em&gt;seemed&lt;/em&gt; ideal&lt;/li&gt;
&lt;li&gt;A decent nod to accessibility; I’m getting older and I’ve got friends that are visually impaired&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, those nine criteria might seem like a lot – and, in hindsight, maybe they were – but really they can be boiled down to &lt;strong&gt;deliverable&lt;/strong&gt; , &lt;strong&gt;discoverable&lt;/strong&gt; , and &lt;strong&gt;readable&lt;/strong&gt; , and since my visual needs are &lt;em&gt;pretty&lt;/em&gt; bare-bones they didn’t seem like too much of an ask.&lt;/p&gt;

&lt;p&gt;Of course I’d quicky learn that some of these had natural, even insurmountable conflicts – AMP would eventually be abandoned because it renders commenting and mailing list signup unfeasible, for instance, and social sharing and privacy don’t exactly go hand in hand – but I had &lt;em&gt;hope&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2: Assess the Field
&lt;/h4&gt;

&lt;p&gt;Dynamic tools like Wordpress were obviously eliminated by my first criterion, so I set out to understand the options for static site generators, of which there are &lt;a href="https://myles.github.io/awesome-static-generators"&gt;many&lt;/a&gt;. Though I came across Hugo early in my research I &lt;em&gt;really&lt;/em&gt; didn’t want to go in full bore on anything without reading up on the competition, so I set it aside for the moment.&lt;/p&gt;

&lt;p&gt;I tried to narrow the field by eliminating generators written in what were, to me, &lt;em&gt;exotic&lt;/em&gt; languages. As I’m extremely comfortable with Python – far more so than I am, or ever intend to be, with Go – I started by investigating &lt;a href="https://blog.getpelican.com/"&gt;Pelican&lt;/a&gt;, &lt;a href="https://getnikola.com/"&gt;Nikola&lt;/a&gt;, and &lt;a href="https://hyde.github.io/"&gt;Hyde&lt;/a&gt;&lt;sup id="fnref1"&gt;1&lt;/sup&gt;. In each case it quickly became apparent that these weren’t the right choice for me, as none seemed either very deep or in very wide use, and I wanted something with relatively broad adoption and a community to help me through the learning curve. No options in my number one language, then, but really how often was I going to need – or want – to monkey around with the generator’s internals, anyway? If the tool &lt;em&gt;worked&lt;/em&gt; I shouldn’t need to, I reasoned, though the ability to comprehend &lt;em&gt;how&lt;/em&gt; it worked would be nice. Plus, at the end of the day, my itch for speed stems in part from &lt;em&gt;many&lt;/em&gt; years waiting on Python, so the siren song of &lt;em&gt;fast&lt;/em&gt; lured me a little further out of my comfort zone.&lt;/p&gt;

&lt;p&gt;Of course I also checked out &lt;a href="https://jekyllrb.com/"&gt;Jekyll&lt;/a&gt;, the Ruby-written, Github Pages first class member; an extremely solid alternative that’s polished enough to be on its third major release, with a community large enough to demand some level of stability, and which certainly seemed friendly enough. But I’m not &lt;em&gt;at all&lt;/em&gt; interested in Ruby, so lingering fear of having to get under the hood at least once, coupled with the – by all accounts quite obvious – speed differential, tipped the balance towards Hugo. Besides, Jeykll &lt;em&gt;felt&lt;/em&gt; long in the tooth, with some long unaddressed issues and theme showcases bloated by what certainly looked to me like a large amount of swine and only a few pearls. Even its own website seemed dated and rather dull, which added to the perception that it wasn’t keeping up with the times.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 3: Embrace Confirmation Bias
&lt;/h4&gt;

&lt;p&gt;By contrast there was an obvious, churning buzz around Hugo among tech bloggers I admire, a solid amount of recent commit history, and some definitely cool sounding features like &lt;a href="https://gohugo.io/hugo-pipes/introduction"&gt;Hugo Pipes&lt;/a&gt;. As a neophyte to static, self-managed blogging I really hoped for something that might save me from the confusing tide of tooling that seems to inundate the world of web development&lt;sup id="fnref2"&gt;2&lt;/sup&gt;, so Pipes looked like a big shiny win.&lt;/p&gt;

&lt;p&gt;And, glancing through the &lt;a href="https://themes.gohugo.io"&gt;theme browser&lt;/a&gt;, there were a high proportion of options that looked good – if not &lt;em&gt;ideal&lt;/em&gt; – for the minimalist tech blog I was &lt;em&gt;obviously&lt;/em&gt; going to &lt;em&gt;finally&lt;/em&gt; have up and running &lt;strong&gt;any day now&lt;/strong&gt;. I quickly found themes that met &lt;em&gt;most&lt;/em&gt; of my functionality criteria, and others that seemed to implement some of what was missing. So I might need to figure out how to transplant some elements, Frankenstein-like, from one theme into another, but the templating syntax &lt;em&gt;looked&lt;/em&gt; simple enough, there seemed to be a lot of &lt;a href="https://gohugo.io/documentation/"&gt;documentation&lt;/a&gt;, and there were obviously active &lt;a href="https://discourse.gohugo.io/"&gt;forums&lt;/a&gt; with lots of meat to chew through.&lt;/p&gt;

&lt;p&gt;So, after some more comparison on drearier topics like CI deployment and hosting options, the decision was made. Hugo had been an early contender, and now it was Hugo for the win! I was confident; I’m a greenfield blogger, no big – or for that matter &lt;em&gt;any&lt;/em&gt; – back catalogue to worry about importing… all I’ve got to do is pick a theme, start writing my first post, and deploy! I’ll be up in days! Maybe even &lt;em&gt;hours&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;What could go wrong?&lt;/p&gt;

&lt;h3&gt;
  
  
  It Doesn’t Exactly Work Out of the Box
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;You never get a second chance to make a first impression.&lt;/p&gt;

&lt;p&gt;&lt;cite&gt;Head &amp;amp; Shoulders&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Maybe it was bad luck, maybe it was the winds of change, most likely it was an errant commit on the Ananke theme – who knows? – but on the fateful day I first did the &lt;a href="https://gohugo.io/getting-started/quick-start/"&gt;quickstart&lt;/a&gt; I rapidly and tenaciously made it all the way to the moment of truth, breathlessly ran &lt;code&gt;hugo server -D&lt;/code&gt; and then, aching with anticipation, opened a browser window and pointed it at the localhost address conventiently displayed in the terminal. Where I found absolutely &lt;em&gt;bupkiss&lt;/em&gt; being served. Nada. A blank screen with an empty source file. Back in the terminal where I’d launched the command there were several warnings, but nothing that could obviously explain the error&lt;sup id="fnref3"&gt;3&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;This was &lt;em&gt;disappointing&lt;/em&gt;. But at the end of the day I had zero interest in using Ananke, so I decided to try the rest of the quickstart with one of the more me-appropriate themes I’d already bookmarked.&lt;/p&gt;

&lt;p&gt;… it didn’t work. Neither did the next.&lt;/p&gt;

&lt;p&gt;Neither did the following &lt;em&gt;three&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Agony of Teething
&lt;/h4&gt;

&lt;p&gt;Hugo is an open source project; as of right now its most recent &lt;a href="https://github.com/gohugoio/hugo/releases"&gt;release&lt;/a&gt; is 0.55. Unlike Jekyll it hasn’t yet reached a 1.0 release, and thus has made no promises about backwards compatability. And that’s &lt;em&gt;fine&lt;/em&gt;. Each of the themes is similarly an open source effort, typically – though not always – with a single developer who is very possibly the sole invested consumer as well. And so things have broken, &lt;em&gt;badly&lt;/em&gt;. Which maybe is to be expected, in a project this immature.&lt;/p&gt;

&lt;p&gt;But for each and every one of these themes the “official” hosted demo of the theme appears to work fine, which gives a Hugo-newbie like me the impression that there are &lt;em&gt;many&lt;/em&gt; more out-of-the-box functional themes than there actually are.&lt;/p&gt;

&lt;p&gt;So what gives? Well it turns out it’s because the hosted showcase uses the &lt;code&gt;exampleSite&lt;/code&gt; directory embedded in each submitted theme, something not even mentioned in the quickstart, and &lt;em&gt;very&lt;/em&gt; often the templates in the theme depend on configuration that simply won’t be present in your quickstart site.&lt;/p&gt;

&lt;p&gt;Eventually, after &lt;em&gt;much&lt;/em&gt; reading of old forum posts and tripping through a lot of documentation well past its best before date, I discovered the following &lt;em&gt;usually&lt;/em&gt; gives you a local theme demonstration that behaves roughly as the author intended:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mv config.toml config.toml.bak
cp -r themes/YOUR_THEME/exampleSite/* .
find . -maxdepth 1 -type f \( -name "config.toml" -or -name "config.yaml" -or -name "config.json" \) -print -quit | grep -q "." || cp config.toml.bak config.toml
cp themes/YOUR_THEME/archetypes/*.* ./archetypes/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first line backs up the default &lt;code&gt;config.toml&lt;/code&gt; file before the contents of the theme’s &lt;code&gt;exampleSite&lt;/code&gt; directory are copied into the root. We need to move &lt;code&gt;config.toml&lt;/code&gt; because that file, if present, will take precedence over &lt;code&gt;config.yaml&lt;/code&gt;, which will in turn take precedence over &lt;code&gt;config.json&lt;/code&gt;. That’s right, Hugo has had &lt;strong&gt;three&lt;/strong&gt; configuration file formats over its short life, and all three remain viable options, though for some reason TOML has the edge. Since &lt;em&gt;any&lt;/em&gt; of them may have been present in the theme, we’ll only move the backup back into place if &lt;em&gt;none&lt;/em&gt; of them were.&lt;/p&gt;

&lt;p&gt;Similarly the final line overwrites the &lt;code&gt;archetypes/default.md&lt;/code&gt; file with any and all files in the theme’s &lt;code&gt;archetypes&lt;/code&gt; directory; this is because currently Hugo will &lt;em&gt;always&lt;/em&gt; assume that a &lt;code&gt;default.md&lt;/code&gt; in the root &lt;code&gt;archetypes&lt;/code&gt; directory should override &lt;em&gt;any&lt;/em&gt; archetype from the theme, no matter how specialized it may be. The end result is that the &lt;code&gt;hugo new posts/my-first-post.md&lt;/code&gt; command mentioned in the quickstart won’t include theme-critical elements in the post’s &lt;a href="https://gohugo.io/content-management/front-matter"&gt;front matter&lt;/a&gt;, quite often leading to errors or missing content.&lt;/p&gt;

&lt;p&gt;And as I said the above fixes will only &lt;em&gt;usually&lt;/em&gt; give you a working example of the theme you’ve chosen. The Ananke theme itself, for instance, will need further steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sed -i 's/gohugo-theme-ananke/ananke/' config.toml
sed -i 's/^themesDir/#themesDir/' config.toml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;These are required because the quickstart instructions had you rename the theme directory as you made the submodule, and the &lt;strong&gt;themesDir&lt;/strong&gt; configuration key was set apparently as a hack to make the theme easier to test.&lt;/p&gt;

&lt;p&gt;These sorts of issues mostly derive from the various themes having been written at various stages in Hugo’s &lt;em&gt;rapid&lt;/em&gt; evolution, and many of them have committed to old choices that no longer remain the best practice, or are now pending deprecation&lt;sup id="fnref4"&gt;4&lt;/sup&gt;. One phylogentic tree of themes &lt;em&gt;requires&lt;/em&gt; content to be in &lt;code&gt;content/post&lt;/code&gt; instead of &lt;code&gt;content/posts&lt;/code&gt;, another will fall down if it’s not the other way around.&lt;/p&gt;

&lt;p&gt;These are, though, the relatively minor issues; take a dew days, pore over the forums and the documentation, make a few key configuration changes, possibly clear out a few of the cobwebs in a partial or two, and you’re good, right?&lt;/p&gt;

&lt;h4&gt;
  
  
  Failure is a Recursing Theme
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;This isn't life in the fast lane, it's life in the oncoming traffic.&lt;/p&gt;

&lt;p&gt;&lt;cite&gt;Terry Pratchett&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If I’ve learned anything on this saga it’s that themes for static blogs tend to be fairly and surprisingly complex beasts, and that human beings can make them &lt;em&gt;far&lt;/em&gt; more complex than that. As a rule you should assume, going in, that whoever wrote the theme you’ve chosen only understood – or at least only encountered – a fraction of the potential issues. As just one example of an issue repeatedly encountered, a small and seemingly innocuous change to a configuration value can suddenly turn Hugo’s vaunted speed into an equally impressive amount of thumb twiddling, as you wait for the server to time out and spit out a complaint about excess recursion. Unfortunately this is one of a family of errors that leaves you with no clear idea where the problem child might be, or even what file was being processed.&lt;/p&gt;

&lt;p&gt;My encounters with these recursion issues weren’t extremely common, but frustrating enough that I tried to figure out what was going on; they &lt;em&gt;seem&lt;/em&gt; to be related to the usage of the &lt;a href="https://gohugo.io/functions/scratch/"&gt;Scratch&lt;/a&gt; area, a sort of variously site / page / template localized “scratchpad” that the Hugo team bolted on to overcome an apparent deficiency in the Go template language&lt;sup id="fnref5"&gt;5&lt;/sup&gt;. The Scratch facility seems to have been added to attend to a narrow problem, but it was apparently adopted enthusiastically – and perhaps too widely and, at times, inappropriately – by theme authors. Unfortunately they’ve done that off of sparse documentation on what is an obviously powerful, but seemingly dangerous, feature, and more importantly a feature you, the theme &lt;em&gt;consumer&lt;/em&gt;, can’t know is in use without knowing your theme’s various templates both &lt;em&gt;deeply&lt;/em&gt; and &lt;em&gt;intimately&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Debugging these sort of issue is &lt;em&gt;hard&lt;/em&gt;, and if you’re not a reasonably seasoned developer you won’t have a hope of doing so. And, beyond Scratch, the Go templating language is &lt;em&gt;impressively&lt;/em&gt; flexible and powerful, allowing you to embed significantly more complex logic into templates than I’ve seen in other such template DSLs. But this brings with it a consequent and potentially explosive increase in the complexity and number of possible errors. Since there’s – as of yet – no meaningful or obvious way to test a template, you can imagine that the reliability of the various themes in the showcase is &lt;em&gt;extremely&lt;/em&gt; variable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lessons Hard Won
&lt;/h3&gt;

&lt;p&gt;None of the above is to say that there aren’t themes out there that work, and work well, out of the box, just that I never found them. However I also got nowhere near testing them all, and it’s entirely possible that I just chose un-wisely, so your mileage may vary. And of course your needs may simply not overlap with mine; if you need a single page landing site or a product launch page there are slick looking, seemingly professional offerings that may work perfectly with only the most minimal configuration.&lt;/p&gt;

&lt;p&gt;But if you &lt;em&gt;are&lt;/em&gt; like me, with relatively low experience in web programming and a strong desire to start producing content quickly, and with as much out-of-the-box support as conceivably possible, then be forewarned that the &lt;em&gt;implicit&lt;/em&gt; promise of getting up and running fast with Hugo is quite a hollow one.&lt;/p&gt;

&lt;p&gt;If you’re not interested in – or capable of – monkeying around with theme internals, then Hugo is almost certainly &lt;strong&gt;not&lt;/strong&gt; the right choice for you, at least not yet. If you &lt;em&gt;are&lt;/em&gt; interested and capable, then realize that even if you don’t end up rewriting every line of your chosen theme – and there’s a &lt;em&gt;very&lt;/em&gt; good chance you will &lt;em&gt;want&lt;/em&gt; to – then you’re &lt;em&gt;definitely&lt;/em&gt; going to invest a significant amount of time browsing, installing, shake-down testing, and rejecting themes in the showcase. And ultimately you’re going to find that you will be &lt;em&gt;required&lt;/em&gt; to understand what is happening internally to a degree that simply isn’t immediately true with other more mature, stable, and well documented projects in this space.&lt;/p&gt;

&lt;p&gt;And if you don’t intend to be &lt;em&gt;especially&lt;/em&gt; prolific – or if you haven’t already got a &lt;em&gt;substantial&lt;/em&gt; back catalogue of posts – then there’s a very substantial chance that the dividends paid by Hugo’s &lt;em&gt;extremely&lt;/em&gt; impressive rendering speed simply won’t ever repay that investment, much less bring you real returns on it.&lt;/p&gt;

&lt;p&gt;That said if you &lt;em&gt;do&lt;/em&gt; have the temerity to start from scratch – as I eventually ended up doing with my own theme – then pat yourself on the back when those frustrating months of effort are done, because that first post – this one – feels &lt;em&gt;good&lt;/em&gt;, even if it took a very long time to see it finally rendered.&lt;/p&gt;







&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;Hyde’s own site looking different when served over http vs https didn’t inspire confidence. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;I’m staring balefully at you &lt;code&gt;npm&lt;/code&gt;, &lt;code&gt;grunt&lt;/code&gt;, &lt;code&gt;yarn&lt;/code&gt;, &lt;code&gt;webpack&lt;/code&gt;, &lt;code&gt;yack&lt;/code&gt; and &lt;code&gt;pork&lt;/code&gt;, the last two of which I’m not even &lt;em&gt;sure&lt;/em&gt; I just made up. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;As of the day I’m writing this running through the quickstart instructions does in fact work, though with a great many deprecation warnings. You’ll end up with a site that looks nothing like the theme demo page, though; even the gargoylish top splash image doesn’t load. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;Hence the warnings you’ll see in the quickstart with Ananke, even with the fixes describe above. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;Variables could be assigned in templates with the &lt;code&gt;:=&lt;/code&gt; assignment operator, but not updated. This appears to have been quietly fixed at some point (there is now a &lt;code&gt;=&lt;/code&gt; update operator), reducing or possibly eliminating the need for the Scratch space, at least &lt;em&gt;within&lt;/em&gt; a single template’s scope. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>hugo</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
