<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>Real Python</title>
  <link href="https://realpython.com/atom.xml" rel="self"/>
  <link href="https://realpython.com/"/>
  <updated>2019-09-25T15:52:37+00:00</updated>
  <id>https://realpython.com/</id>
  <author>
    <name>Real Python</name>
  </author>

  
    <entry>
      <title>How to Use Generators and yield in Python</title>
      <id>https://realpython.com/introduction-to-python-generators/</id>
      <link href="https://realpython.com/introduction-to-python-generators/"/>
      <updated>2019-09-25T15:52:37+00:00</updated>
      <summary>In this step-by-step tutorial, you&#39;ll learn about generators and yielding in Python. You&#39;ll create generator functions and generator expressions using multiple Python yield statements. You&#39;ll also learn how to build data pipelines that take advantage of these Pythonic tools.</summary>
      <content type="html">
        &lt;p&gt;Have you ever had to work with a dataset so large that it overwhelmed your machine&amp;rsquo;s memory? Or maybe you have a complex function that needs to maintain an internal state every time it&amp;rsquo;s called, but the function is too small to justify creating its own class. In these cases and more, generators and the Python yield statement are here to help. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;By the end of this article, you&amp;rsquo;ll know:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What &lt;strong&gt;generators&lt;/strong&gt; are and how to use them&lt;/li&gt;
&lt;li&gt;How to create &lt;strong&gt;generator functions and expressions&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;How the &lt;strong&gt;Python yield&lt;/strong&gt; statement works&lt;/li&gt;
&lt;li&gt;How to use &lt;strong&gt;multiple&lt;/strong&gt; Python yield statements in a generator function&lt;/li&gt;
&lt;li&gt;How to use &lt;strong&gt;advanced generator methods&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;How to &lt;strong&gt;build data pipelines&lt;/strong&gt; with multiple generators&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you&amp;rsquo;re a beginner or intermediate Pythonista and you&amp;rsquo;re interested in learning how to work with large datasets in a more Pythonic fashion, then this is the tutorial for you.&lt;/p&gt;
&lt;p&gt;You can get a copy of the dataset used in this tutorial by clicking the link below:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Download Dataset:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/generators-yield/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-generators-yield&quot; data-focus=&quot;false&quot;&gt;Click here to download the dataset you&#39;ll use in this tutorial&lt;/a&gt; to learn about generators and yield in Python.&lt;/p&gt;&lt;/div&gt;

&lt;h2 id=&quot;using-generators&quot;&gt;Using Generators&lt;/h2&gt;
&lt;p&gt;Introduced with &lt;a href=&quot;https://www.python.org/dev/peps/pep-0255&quot;&gt;PEP 255&lt;/a&gt;, &lt;strong&gt;generator functions&lt;/strong&gt; are a special kind of function that return a &lt;a href=&quot;https://en.wikipedia.org/wiki/Lazy_evaluation&quot;&gt;lazy iterator&lt;/a&gt;. These are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store their contents in memory. For an overview of iterators in Python, take a look at &lt;a href=&quot;https://realpython.com/python-for-loop/&quot;&gt;Python &amp;ldquo;for&amp;rdquo; Loops (Definite Iteration)&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Now that you have a rough idea of what a generator does, you might wonder what they look like in action. Let&amp;rsquo;s take a look at two examples. In the first, you&amp;rsquo;ll see how generators work from a bird&amp;rsquo;s eye view. Then, you&amp;rsquo;ll zoom in and examine each example more thoroughly.&lt;/p&gt;
&lt;h3 id=&quot;example-1-reading-large-files&quot;&gt;Example 1: Reading Large Files&lt;/h3&gt;
&lt;p&gt;A common use case of generators is to work with data streams or large files, like &lt;a href=&quot;https://realpython.com/courses/reading-and-writing-csv-files/&quot;&gt;CSV files&lt;/a&gt;. These text files separate data into columns by using commas. This format is a common way to share data. Now, what if you want to count the number of rows in a CSV file? The code block below shows one way of counting those rows:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;csv_gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;csv_reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;some_csv.txt&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;row_count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;csv_gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;row_count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Row count is &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{row_count}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Looking at this example, you might expect &lt;code&gt;csv_gen&lt;/code&gt; to be a list. To populate this list, &lt;code&gt;csv_reader()&lt;/code&gt; opens a file and loads its contents into &lt;code&gt;csv_gen&lt;/code&gt;. Then, the program iterates over the list and increments &lt;code&gt;row_count&lt;/code&gt; for each row.&lt;/p&gt;
&lt;p&gt;This is a reasonable explanation, but would this design still work if the file is very large? What if the file is larger than the memory you have available? To answer this question, let&amp;rsquo;s assume that &lt;code&gt;csv_reader()&lt;/code&gt; just opens the file and reads it into an array:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;csv_reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This function opens a given file and uses &lt;code&gt;file.read()&lt;/code&gt; along with &lt;code&gt;.split()&lt;/code&gt; to add each line as a separate element to a list. If you were to use this version of &lt;code&gt;csv_reader()&lt;/code&gt; in the row counting code block you saw further up, then you&amp;rsquo;d get the following output:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;ex1_naive.py&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;22&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;ex1_naive.py&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;13&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;csv_gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;csv_reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;file.txt&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;ex1_naive.py&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;6&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;csv_reader&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gr&quot;&gt;MemoryError&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this case, &lt;code&gt;open()&lt;/code&gt; returns a generator object that you can lazily iterate through line by line. However,  &lt;code&gt;file.read().split()&lt;/code&gt; loads everything into memory at once, causing the &lt;code&gt;MemoryError&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Before that happens, you&amp;rsquo;ll probably notice your computer slow to a crawl. You might even need to kill the program with a &lt;code&gt;KeyboardInterrupt&lt;/code&gt;. So, how can you handle these huge data files? Take a look at a new definition of &lt;code&gt;csv_reader()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;csv_reader&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;r&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this version, you open the file, iterate through it, and yield a row. This code should produce the following output, with no memory errors:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;Row count is 64186394&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What&amp;rsquo;s happening here? Well, you&amp;rsquo;ve essentially turned &lt;code&gt;csv_reader()&lt;/code&gt; into a generator function. This version opens a file, loops through each line, and yields each row, instead of returning it.&lt;/p&gt;
&lt;p&gt;You can also define a &lt;strong&gt;generator expression&lt;/strong&gt; (also called a &lt;strong&gt;generator comprehension&lt;/strong&gt;), which has a very similar syntax to &lt;a href=&quot;https://realpython.com/courses/using-list-comprehensions-effectively/&quot;&gt;list comprehensions&lt;/a&gt;. In this way, you can use the generator without calling a function:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;csv_gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is a more succinct way to create the list &lt;code&gt;csv_gen&lt;/code&gt;. You&amp;rsquo;ll learn more about the Python yield statement soon. For now, just remember this key difference:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Using &lt;code&gt;yield&lt;/code&gt; will result in a generator object.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;return&lt;/code&gt; will result in the first line of the file &lt;em&gt;only&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;example-2-generating-an-infinite-sequence&quot;&gt;Example 2: Generating an Infinite Sequence&lt;/h3&gt;
&lt;p&gt;Let&amp;rsquo;s switch gears and look at infinite sequence generation. In Python, to get a finite sequence, you call &lt;code&gt;range()&lt;/code&gt; and evaluate it in a list context:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[0, 1, 2, 3, 4, 5]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Generating an &lt;strong&gt;infinite sequence&lt;/strong&gt;, however, will require the use of a generator, since your computer memory is finite:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;infinite_sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code block is short and sweet. First, you initialize the variable &lt;code&gt;num&lt;/code&gt; and start an infinite loop. Then, you immediately &lt;code&gt;yield num&lt;/code&gt; so that you can capture the initial state. This mimics the action of &lt;code&gt;range()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After &lt;code&gt;yield&lt;/code&gt;, you increment &lt;code&gt;num&lt;/code&gt; by 1. If you try this with a &lt;code&gt;for&lt;/code&gt; loop, then you&amp;rsquo;ll see that it really does seem infinite:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;infinite_sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot; &amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;30 31 32 33 34 35 36 37 38 39 40 41 42&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[...]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;6157818 6157819 6157820 6157821 6157822 6157823 6157824 6157825 6157826 6157827&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;6157828 6157829 6157830 6157831 6157832 6157833 6157834 6157835 6157836 6157837&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;6157838 6157839 6157840 6157841 6157842&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;KeyboardInterrupt&lt;/span&gt;
&lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The program will continue to execute until you stop it manually.&lt;/p&gt;
&lt;p&gt;Instead of using a &lt;code&gt;for&lt;/code&gt; loop, you can also call &lt;code&gt;next()&lt;/code&gt; on the generator object directly. This is especially useful for testing a generator in the console:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;infinite_sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, you have a generator called &lt;code&gt;gen&lt;/code&gt;, which you manually iterate over by repeatedly calling &lt;code&gt;next()&lt;/code&gt;. This works as a great sanity check to make sure your generators are producing the output you expect.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you use &lt;code&gt;next()&lt;/code&gt;, Python calls &lt;code&gt;.__next__()&lt;/code&gt; on the function you pass in as a parameter. There are some special effects that this parameterization allows, but it goes beyond the scope of this article. Experiment with changing the parameter you pass to &lt;code&gt;next()&lt;/code&gt; and see what happens!&lt;/p&gt;
&lt;/div&gt;
&lt;h3 id=&quot;example-3-detecting-palindromes&quot;&gt;Example 3: Detecting Palindromes&lt;/h3&gt;
&lt;p&gt;You can use infinite sequences in many ways, but one practical use for them is in building palindrome detectors. A &lt;strong&gt;palindrome detector&lt;/strong&gt; will locate all sequences of letters or numbers that are palindromes. These are words or numbers that are read the same forward and backward, like 121. First, define your numeric palindrome detector:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;is_palindrome&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Skip single-digit inputs&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;reversed_num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;reversed_num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reversed_num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reversed_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Don&amp;rsquo;t worry too much about understanding the underlying math in this code. Just note that the function takes an input number, reverses it, and checks to see if the reversed number is the same as the original. Now you can use your infinite sequence generator to get a running list of all numeric palindromes:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;infinite_sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;pal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_palindrome&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;22&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;33&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[...]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;99799&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;99899&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;99999&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;100001&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;101101&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;102201&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;KeyboardInterrupt&lt;/span&gt;
&lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;is_palindrome&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this case, the only numbers that are printed to the console are those that are the same forward or backward.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In practice, you&amp;rsquo;re unlikely to write your own infinite sequence generator. The &lt;a href=&quot;https://realpython.com/python-itertools/&quot;&gt;&lt;code&gt;itertools&lt;/code&gt;&lt;/a&gt; module provides a very efficient infinite sequence generator with &lt;code&gt;itertools.count()&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now that you&amp;rsquo;ve seen a simple use case for an infinite sequence generator, let&amp;rsquo;s dive deeper into how generators work.&lt;/p&gt;
&lt;h2 id=&quot;understanding-generators&quot;&gt;Understanding Generators&lt;/h2&gt;
&lt;p&gt;So far, you&amp;rsquo;ve learned about the two primary ways of creating generators: by using generator functions and generator expressions. You might even have an intuitive understanding of how generators work. Let&amp;rsquo;s take a moment to make that knowledge a little more explicit.&lt;/p&gt;
&lt;p&gt;Generator functions look and act just like regular functions, but with one defining characteristic. Generator functions use the Python yield keyword instead of &lt;code&gt;return&lt;/code&gt;. Recall the generator function you wrote earlier:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;infinite_sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This looks like a typical function definition, except for the Python yield statement and the code that follows it. &lt;code&gt;yield&lt;/code&gt; indicates where a value is sent back to the caller, but unlike &lt;code&gt;return&lt;/code&gt;, you don&amp;rsquo;t exit the function afterward.&lt;/p&gt;
&lt;p&gt;Instead, the &lt;strong&gt;state&lt;/strong&gt; of the function is remembered. That way, when &lt;code&gt;next()&lt;/code&gt; is called on a generator object (either explicitly or implicitly within a &lt;code&gt;for&lt;/code&gt; loop), the previously yielded variable &lt;code&gt;num&lt;/code&gt; is incremented, and then yielded again. Since generator functions look like other functions and act very similarly to them, you can assume that generator expressions are very similar to other comprehensions available in Python.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Are you rusty on Python&amp;rsquo;s list, set, and dictionary comprehensions? You can check out &lt;a href=&quot;https://realpython.com/courses/using-list-comprehensions-effectively/&quot;&gt;Using List Comprehensions Effectively&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;h3 id=&quot;building-generators-with-generator-expressions&quot;&gt;Building Generators With Generator Expressions&lt;/h3&gt;
&lt;p&gt;Like list comprehensions, generator expressions allow you to quickly create a generator object in just a few lines of code. They&amp;rsquo;re also useful in the same cases where list comprehensions are used, with an added benefit: you can create them without building and holding the entire object in memory before iteration. In other words, you&amp;rsquo;ll have no memory penalty when you use generator expressions. Take this example of squaring some numbers:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums_squared_lc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums_squared_gc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Both &lt;code&gt;nums_squared_lc&lt;/code&gt; and &lt;code&gt;nums_squared_gc&lt;/code&gt; look basically the same, but there&amp;rsquo;s one key difference. Can you spot it? Take a look at what happens when you inspect each of these objects:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums_squared_lc&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[0, 1, 4, 9, 16]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums_squared_gc&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;generator object &amp;lt;genexpr&amp;gt; at 0x107fbbc78&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The first object used brackets to build a list, while the second created a generator expression by using parentheses. The output confirms that you&amp;rsquo;ve created a generator object and that it is distinct from a list.&lt;/p&gt;
&lt;h3 id=&quot;profiling-generator-performance&quot;&gt;Profiling Generator Performance&lt;/h3&gt;
&lt;p&gt;You learned earlier that generators are a great way to optimize memory. While an infinite sequence generator is an extreme example of this optimization, let&amp;rsquo;s amp up the number squaring examples you just saw and inspect the size of the resulting objects. You can do this with a call to &lt;code&gt;sys.getsizeof()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums_squared_lc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getsizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums_squared_lc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;87624&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums_squared_gc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getsizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nums_squared_gc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;120&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this case, the list you get from the list comprehension is 87,624 bytes, while the generator object is only 120. This means that the list is over 700 times larger than the generator object!&lt;/p&gt;
&lt;p&gt;There is one thing to keep in mind, though. If the list is smaller than the running machine&amp;rsquo;s available memory, then list comprehensions can be &lt;a href=&quot;https://stackoverflow.com/questions/11964130/list-comprehension-vs-generator-expressions-weird-timeit-results/11964478#11964478&quot;&gt;faster to evaluate&lt;/a&gt; than the equivalent generator expression. To explore this, let&amp;rsquo;s sum across the results from the two comprehensions above. You can generate a readout with &lt;code&gt;cProfile.run()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;cProfile&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cProfile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;sum([i * 2 for i in range(10000)])&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;         5 function calls in 0.001 seconds&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;   Ordered by: standard name&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;   ncalls  tottime  percall  cumtime  percall filename:lineno(function)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;        1    0.001    0.001    0.001    0.001 &amp;lt;string&amp;gt;:1(&amp;lt;listcomp&amp;gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;        1    0.000    0.000    0.001    0.001 &amp;lt;string&amp;gt;:1(&amp;lt;module&amp;gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;        1    0.000    0.000    0.001    0.001 {built-in method builtins.exec}&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;        1    0.000    0.000    0.000    0.000 {built-in method builtins.sum}&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;        1    0.000    0.000    0.000    0.000 {method &amp;#39;disable&amp;#39; of &amp;#39;_lsprof.Profiler&amp;#39; objects}&lt;/span&gt;


&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cProfile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;sum((i * 2 for i in range(10000)))&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;         10005 function calls in 0.003 seconds&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;   Ordered by: standard name&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;   ncalls  tottime  percall  cumtime  percall filename:lineno(function)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;    10001    0.002    0.000    0.002    0.000 &amp;lt;string&amp;gt;:1(&amp;lt;genexpr&amp;gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;        1    0.000    0.000    0.003    0.003 &amp;lt;string&amp;gt;:1(&amp;lt;module&amp;gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;        1    0.000    0.000    0.003    0.003 {built-in method builtins.exec}&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;        1    0.001    0.001    0.003    0.003 {built-in method builtins.sum}&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;        1    0.000    0.000    0.000    0.000 {method &amp;#39;disable&amp;#39; of &amp;#39;_lsprof.Profiler&amp;#39; objects}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, you can see that summing across all values in the list comprehension took about a third of the time as summing across the generator. If speed is an issue and memory isn&amp;rsquo;t, then a list comprehension is likely a better tool for the job.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; These measurements aren&amp;rsquo;t only valid for objects made with generator expressions. They&amp;rsquo;re also the same for objects made from the analogous generator function since the resulting generators are equivalent.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Remember, list comprehensions return full lists, while generator expressions return generators. Generators work the same whether they&amp;rsquo;re built from a function or an expression. Using an expression just allows you to define simple generators in a single line, with an assumed &lt;code&gt;yield&lt;/code&gt; at the end of each inner iteration.&lt;/p&gt;
&lt;p&gt;The Python yield statement is certainly the linchpin on which all of the functionality of generators rests, so let&amp;rsquo;s dive into how &lt;code&gt;yield&lt;/code&gt; works in Python.&lt;/p&gt;
&lt;h2 id=&quot;understanding-the-python-yield-statement&quot;&gt;Understanding the Python Yield Statement&lt;/h2&gt;
&lt;p&gt;On the whole, &lt;code&gt;yield&lt;/code&gt; is a fairly simple statement. Its primary job is to control the flow of a generator function in a way that&amp;rsquo;s similar to &lt;code&gt;return&lt;/code&gt; statements. As briefly mentioned above, though, the Python yield statement has a few tricks up its sleeve.&lt;/p&gt;
&lt;p&gt;When you call a generator function or use a generator expression, you return a special iterator called a generator. You can assign this generator to a variable in order to use it. When you call special methods on the generator, such as &lt;code&gt;next()&lt;/code&gt;, the code within the function is executed up to &lt;code&gt;yield&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;When the Python yield statement is hit, the program suspends function execution and returns the yielded value to the caller. (In contrast, &lt;code&gt;return&lt;/code&gt; stops function execution completely.) When a function is suspended, the state of that function is saved. This includes any variable bindings local to the generator, the instruction pointer, the internal stack, and any exception handling.&lt;/p&gt;
&lt;p&gt;This allows you to resume function execution whenever you call one of the generator&amp;rsquo;s methods. In this way, all function evaluation picks back up right after &lt;code&gt;yield&lt;/code&gt;. You can see this in action by using multiple Python yield statements:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;multi_yield&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;yield_str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;This will print the first string&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yield_str&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;yield_str&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;This will print the second string&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yield_str&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multi_obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;multi_yield&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multi_obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;This will print the first string&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multi_obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;This will print the second string&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multi_obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
&lt;span class=&quot;gr&quot;&gt;StopIteration&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Take a closer look at that last calls to &lt;code&gt;next()&lt;/code&gt;. You can see that execution has blown up with a &lt;a href=&quot;https://realpython.com/python-traceback/&quot;&gt;traceback&lt;/a&gt;. This is because generators, like all iterators, can be exhausted. Unless your generator is infinite, you can iterate through it one time only. Once all values have been evaluated, iteration will stop and the &lt;code&gt;for&lt;/code&gt; loop will exit. If you used &lt;code&gt;next()&lt;/code&gt;, then instead you&amp;rsquo;ll get an explicit &lt;code&gt;StopIteration&lt;/code&gt; exception.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;StopIteration&lt;/code&gt; is a natural exception that&amp;rsquo;s raised to signal the end of an iterator. &lt;code&gt;for&lt;/code&gt; loops, for example, are built around &lt;code&gt;StopIteration&lt;/code&gt;. You can even implement your own &lt;code&gt;for&lt;/code&gt; loop by using a &lt;code&gt;while&lt;/code&gt; loop:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;letters&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;a&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;b&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;c&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;y&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;it&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;letters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;letter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;StopIteration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;letter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;c&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;y&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can read more about &lt;code&gt;StopIteration&lt;/code&gt; in the Python documentation on &lt;a href=&quot;https://docs.python.org/3.6/library/exceptions.html&quot;&gt;exceptions&lt;/a&gt;. For more on iteration in general, check out &lt;a href=&quot;https://realpython.com/python-for-loop/&quot;&gt;Python &amp;ldquo;for&amp;rdquo; Loops (Definite Iteration)&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/python-while-loop/&quot;&gt;Python &amp;ldquo;while&amp;rdquo; Loops (Indefinite Iteration)&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;yield&lt;/code&gt; can be used in many ways to control your generator&amp;rsquo;s execution flow. The use of multiple Python yield statements can be leveraged as far as your creativity allows.&lt;/p&gt;
&lt;h2 id=&quot;using-advanced-generator-methods&quot;&gt;Using Advanced Generator Methods&lt;/h2&gt;
&lt;p&gt;You&amp;rsquo;ve seen the most common uses and constructions of generators, but there are a few more tricks to cover. In addition to &lt;code&gt;yield&lt;/code&gt;, generator objects can make use of the following methods:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;.send()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.throw()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.close()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;how-to-use-send&quot;&gt;How to Use &lt;code&gt;.send()&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;For this next section, you&amp;rsquo;re going to build a program that makes use of all three methods. This program will print numeric palindromes like before, but with a few tweaks. Upon encountering a palindrome, your new program will add a digit and start a search for the next one from there. You&amp;rsquo;ll also handle exceptions with &lt;code&gt;.throw()&lt;/code&gt; and stop the generator after a given amount of digits with &lt;code&gt;.close()&lt;/code&gt;. First, let&amp;rsquo;s recall the code for your palindrome detector:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;is_palindrome&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Skip single-digit inputs&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;reversed_num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;reversed_num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reversed_num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reversed_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is the same code you saw earlier, except that now the program returns strictly &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;. You&amp;rsquo;ll also need to modify your original infinite sequence generator, like so:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;infinite_palindromes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_palindrome&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There are a lot of changes here! The first one you&amp;rsquo;ll see is in line 5, where &lt;code&gt;i = (yield num)&lt;/code&gt;. Though you learned earlier that &lt;code&gt;yield&lt;/code&gt; is a statement, that isn&amp;rsquo;t quite the whole story.&lt;/p&gt;
&lt;p&gt;As of Python 2.5 (the same release that introduced the methods you are learning about now), &lt;code&gt;yield&lt;/code&gt; is an &lt;strong&gt;expression&lt;/strong&gt;, rather than a statement. Of course, you can still use it as a statement. But now, you can also use it as you see in the code block above, where &lt;code&gt;i&lt;/code&gt; takes the value that is yielded. This allows you to manipulate the yielded value. More importantly, it allows you to &lt;code&gt;.send()&lt;/code&gt; a value back to the generator.  When execution picks up after &lt;code&gt;yield&lt;/code&gt;, &lt;code&gt;i&lt;/code&gt; will take the value that is sent.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll also check &lt;code&gt;if i is not None&lt;/code&gt;, which could happen if &lt;code&gt;next()&lt;/code&gt; is called on the generator object. (This can also happen when you iterate with a &lt;code&gt;for&lt;/code&gt; loop.) If &lt;code&gt;i&lt;/code&gt; has a value, then you update &lt;code&gt;num&lt;/code&gt; with the new value. But regardless of whether or not &lt;code&gt;i&lt;/code&gt; holds a value, you&amp;rsquo;ll then increment &lt;code&gt;num&lt;/code&gt; and start the loop again.&lt;/p&gt;
&lt;p&gt;Now, take a look at the main function code, which sends the lowest number with another digit back to the generator. For example, if the palindrome is 121, then it will &lt;code&gt;.send()&lt;/code&gt; 1000:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pal_gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;infinite_palindromes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pal_gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;digits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pal_gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;With this code, you create the generator object and iterate through it. The program only yields a value once a palindrome is found. It uses &lt;code&gt;len()&lt;/code&gt; to determine the number of digits in that palindrome. Then, it sends &lt;code&gt;10 ** digits&lt;/code&gt; to the generator. This brings execution back into the generator logic and assigns &lt;code&gt;10 ** digits&lt;/code&gt; to &lt;code&gt;i&lt;/code&gt;. Since &lt;code&gt;i&lt;/code&gt; now has a value, the program updates &lt;code&gt;num&lt;/code&gt;, increments, and checks for palindromes again.&lt;/p&gt;
&lt;p&gt;Once your code finds and yields another palindrome, you&amp;rsquo;ll iterate via the &lt;code&gt;for&lt;/code&gt; loop. This is the same as iterating with &lt;code&gt;next()&lt;/code&gt;. The generator also picks up at line 5 with &lt;code&gt;i = (yield num)&lt;/code&gt;. However, now &lt;code&gt;i&lt;/code&gt; is &lt;code&gt;None&lt;/code&gt;, because you didn&amp;rsquo;t explicitly send a value.&lt;/p&gt;
&lt;p&gt;What you&amp;rsquo;ve created here is a &lt;strong&gt;coroutine&lt;/strong&gt;, or a generator function into which you can pass data. These are useful for constructing data pipelines, but as you&amp;rsquo;ll see soon, they aren&amp;rsquo;t necessary for building them. (If you&amp;rsquo;re looking to dive deeper, then &lt;a href=&quot;http://www.dabeaz.com/coroutines/&quot;&gt;this course on coroutines and concurrency&lt;/a&gt; is one of the most comprehensive treatments available.)&lt;/p&gt;
&lt;p&gt;Now that you&amp;rsquo;ve learned about &lt;code&gt;.send()&lt;/code&gt;, let&amp;rsquo;s take a look at &lt;code&gt;.throw()&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;how-to-use-throw&quot;&gt;How to Use &lt;code&gt;.throw()&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;.throw()&lt;/code&gt; allows you to throw exceptions with the generator. In the below example, you raise the exception in line 6. This code will throw a &lt;code&gt;ValueError&lt;/code&gt; once &lt;code&gt;digits&lt;/code&gt; reaches 5:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pal_gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;infinite_palindromes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pal_gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;digits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;digits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;pal_gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;throw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ne&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;We don&amp;#39;t like large palindromes&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;pal_gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is the same as the previous code, but now you&amp;rsquo;ll check if &lt;code&gt;digits&lt;/code&gt; is equal to 5. If so, then you&amp;rsquo;ll &lt;code&gt;.throw()&lt;/code&gt; a &lt;code&gt;ValueError&lt;/code&gt;. To confirm that this works as expected, take a look at the code&amp;rsquo;s output:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;111&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;1111&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;10101&lt;/span&gt;
&lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;advanced_gen.py&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;47&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;advanced_gen.py&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;41&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pal_gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;throw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ne&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;We don&amp;#39;t like large palindromes&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;advanced_gen.py&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;26&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;infinite_palindromes&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gr&quot;&gt;ValueError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;We don&amp;#39;t like large palindromes&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;.throw()&lt;/code&gt; is useful in any areas where you might need to catch an &lt;a href=&quot;https://realpython.com/courses/introduction-python-exceptions/&quot;&gt;exception&lt;/a&gt;. In this example, you used &lt;code&gt;.throw()&lt;/code&gt; to control when you stopped iterating through the generator. You can do this more elegantly with &lt;code&gt;.close()&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;how-to-use-close&quot;&gt;How to Use &lt;code&gt;.close()&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;As its name implies, &lt;code&gt;.close()&lt;/code&gt; allows you to stop a generator. This can be especially handy when controlling an infinite sequence generator. Let&amp;rsquo;s update the code above by changing &lt;code&gt;.throw()&lt;/code&gt; to &lt;code&gt;.close()&lt;/code&gt; to stop the iteration:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pal_gen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;infinite_palindromes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pal_gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;digits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;digits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;n&quot;&gt;pal_gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;pal_gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Instead of calling &lt;code&gt;.throw()&lt;/code&gt;, you use &lt;code&gt;.close()&lt;/code&gt; in line 6. The advantage of using &lt;code&gt;.close()&lt;/code&gt; is that it raises &lt;code&gt;StopIteration&lt;/code&gt;, an exception used to signal the end of a finite iterator:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;11&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;111&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;1111&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;10101&lt;/span&gt;
&lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;advanced_gen.py&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;46&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;advanced_gen.py&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;42&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pal_gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;gr&quot;&gt;StopIteration&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now that you&amp;rsquo;ve learned more about the special methods that come with generators, let&amp;rsquo;s talk about using generators to build data pipelines.&lt;/p&gt;
&lt;h2 id=&quot;creating-data-pipelines-with-generators&quot;&gt;Creating Data Pipelines With Generators&lt;/h2&gt;
&lt;p&gt;Data pipelines allow you to string together code to process large datasets or streams of data without maxing out your machine&amp;rsquo;s memory. Imagine that you have a large CSV file:&lt;/p&gt;
&lt;div class=&quot;highlight csv&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;permalink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;company&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numEmps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;category&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;city&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fundedDate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raisedAmt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;raisedCurrency&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;round&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;digg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Digg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;San&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Francisco&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;Dec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;06&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8500000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;USD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;digg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Digg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;San&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Francisco&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Oct&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2800000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;USD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;facebook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Facebook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;450&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Palo&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Alto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;04&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;500000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;USD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;angel&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;facebook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Facebook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;450&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Palo&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Alto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;May&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12700000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;USD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;photobucket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Photobucket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;web&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Palo&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Alto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Mar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;USD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This example is pulled from the TechCrunch Continental USA set, which describes funding rounds and dollar amounts for various startups based in the USA. Click the link below to download the dataset:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Download Dataset:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/generators-yield/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-generators-yield&quot; data-focus=&quot;false&quot;&gt;Click here to download the dataset you&#39;ll use in this tutorial&lt;/a&gt; to learn about generators and yield in Python.&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;It&amp;rsquo;s time to do some processing in Python! To demonstrate how to build pipelines with generators, you&amp;rsquo;re going to analyze this file to get the total and average of all series A rounds in the dataset.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s think of a strategy:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Read every line of the file.&lt;/li&gt;
&lt;li&gt;Split each line into a list of values.&lt;/li&gt;
&lt;li&gt;Extract the column names.&lt;/li&gt;
&lt;li&gt;Use the column names and lists to create a dictionary.&lt;/li&gt;
&lt;li&gt;Filter out the rounds you aren&amp;rsquo;t interested in.&lt;/li&gt;
&lt;li&gt;Calculate the total and average values for the rounds you are interested in.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Normally, you can do this with a package like &lt;a href=&quot;https://realpython.com/courses/idiomatic-pandas-tricks-features-you-may-not-know/&quot;&gt;&lt;code&gt;pandas&lt;/code&gt;&lt;/a&gt;, but you can also achieve this functionality with just a few generators. You&amp;rsquo;ll start by reading each line from the file with a generator expression:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;techcrunch.csv&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then, you&amp;rsquo;ll use another generator expression in concert with the previous one to split each line into a list:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list_line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rstrip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;,&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, you created the generator &lt;code&gt;list_line&lt;/code&gt;, which iterates through the first generator &lt;code&gt;lines&lt;/code&gt;. This is a common pattern to use when designing generator pipelines. Next, you&amp;rsquo;ll pull the column names out of &lt;code&gt;techcrunch.csv&lt;/code&gt;. Since the column names tend to make up the first line in a CSV file, you can grab that with a short &lt;code&gt;next()&lt;/code&gt; call:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list_line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This call to &lt;code&gt;next()&lt;/code&gt; advances the iterator over the &lt;code&gt;list_line&lt;/code&gt; generator one time. Put it all together, and your code should look something like this:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;techcrunch.csv&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list_line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rstrip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;,&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list_line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To sum this up, you first create a generator expression &lt;code&gt;lines&lt;/code&gt; to yield each line in a file. Next, you iterate through that generator within the definition of &lt;em&gt;another&lt;/em&gt; generator expression called &lt;code&gt;list_line&lt;/code&gt;, which turns each line into a list of values. Then, you advance the iteration of &lt;code&gt;list_line&lt;/code&gt; just once with &lt;code&gt;next()&lt;/code&gt; to get a list of the column names from your CSV file.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Watch out for trailing newlines! This code takes advantage of &lt;code&gt;.rstrip()&lt;/code&gt; in the &lt;code&gt;list_line&lt;/code&gt; generator expression to make sure there are no trailing newline characters, which can be present in CSV files.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To help you filter and perform operations on the data, you&amp;rsquo;ll create dictionaries where the keys are the column names from the CSV:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;company_dicts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list_line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This generator expression iterates through the lists produced by &lt;code&gt;list_line&lt;/code&gt;. Then, it uses &lt;code&gt;zip()&lt;/code&gt; and &lt;code&gt;dict()&lt;/code&gt; to create the dictionary as specified above. Now, you&amp;rsquo;ll use a &lt;em&gt;fourth&lt;/em&gt; generator to filter the funding round you want and pull &lt;code&gt;raisedAmt&lt;/code&gt; as well:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;funding&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;company_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;raisedAmt&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;company_dict&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;company_dicts&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;company_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;round&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;a&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code snippet, your generator expression iterates through the results of &lt;code&gt;company_dicts&lt;/code&gt; and takes the &lt;code&gt;raisedAmt&lt;/code&gt; for any &lt;code&gt;company_dict&lt;/code&gt; where the &lt;code&gt;round&lt;/code&gt; key is &lt;code&gt;A&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Remember, you aren&amp;rsquo;t iterating through all these at once in the generator expression. In fact, you aren&amp;rsquo;t iterating through anything until you actually use a &lt;code&gt;for&lt;/code&gt; loop or a function that works on iterables, like &lt;code&gt;sum()&lt;/code&gt;. In fact, call &lt;code&gt;sum()&lt;/code&gt; now to iterate through the generators:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_series_a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;funding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Putting this all together, you&amp;rsquo;ll produce the following script:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;techcrunch.csv&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list_line&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rstrip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;,&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list_line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;company_dicts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list_line&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;funding&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;company_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;raisedAmt&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;company_dict&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;company_dicts&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;company_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;round&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;A&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_series_a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;funding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Total series A fundraising: $&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{total_series_a}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This script pulls together every generator you&amp;rsquo;ve built, and they all function as one big data pipeline. Here&amp;rsquo;s a line by line breakdown:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Line 2&lt;/strong&gt; reads in each line of the file.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 3&lt;/strong&gt; splits each line into values and puts the values into a list.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 4&lt;/strong&gt; uses &lt;code&gt;next()&lt;/code&gt; to store the column names in a list.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 5&lt;/strong&gt; creates dictionaries and unites them with a &lt;code&gt;zip()&lt;/code&gt; call:&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The keys&lt;/strong&gt; are the column names &lt;code&gt;cols&lt;/code&gt; from line 4.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The values&lt;/strong&gt; are the rows in list form, created in line 3.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 6&lt;/strong&gt; gets each company&amp;rsquo;s series A funding amounts. It also filters out any other raised amount.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 11&lt;/strong&gt; begins the iteration process by calling &lt;code&gt;sum()&lt;/code&gt; to get the total amount of series A funding found in the CSV.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When you run this code on &lt;code&gt;techcrunch.csv&lt;/code&gt;, you should find a total of $4,376,015,000 raised in series A funding rounds.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The methods for handling CSV files developed in this tutorial are important for understanding how to use generators and the Python yield statement. However, when you work with CSV files in Python, you should instead use the &lt;a href=&quot;https://docs.python.org/3/library/csv.html&quot;&gt;&lt;code&gt;csv&lt;/code&gt;&lt;/a&gt; module included in Python&amp;rsquo;s standard library. This module has optimized methods for handling CSV files efficiently.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To dig even deeper, try figuring out the average amount raised &lt;em&gt;per company&lt;/em&gt; in a series A round. This is a bit trickier, so here are some hints:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Generators exhaust themselves after being iterated over fully.&lt;/li&gt;
&lt;li&gt;You will still need the &lt;code&gt;sum()&lt;/code&gt; function.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Good luck!&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In this tutorial, you&amp;rsquo;ve learned about &lt;strong&gt;generator functions&lt;/strong&gt; and &lt;strong&gt;generator expressions&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;You now know:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;How to use and write generator functions and generator expressions&lt;/li&gt;
&lt;li&gt;How the all-important &lt;strong&gt;Python yield statement&lt;/strong&gt; enables generators&lt;/li&gt;
&lt;li&gt;How to use &lt;strong&gt;multiple&lt;/strong&gt; Python yield statements in a generator function&lt;/li&gt;
&lt;li&gt;How to use &lt;code&gt;.send()&lt;/code&gt; to send data to a generator&lt;/li&gt;
&lt;li&gt;How to use &lt;code&gt;.throw()&lt;/code&gt; to raise generator exceptions&lt;/li&gt;
&lt;li&gt;How to use &lt;code&gt;.close()&lt;/code&gt; to stop a generator&amp;rsquo;s iteration&lt;/li&gt;
&lt;li&gt;How to &lt;strong&gt;build a generator pipeline&lt;/strong&gt; to efficiently process large CSV files&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can get the dataset you used in this tutorial at the link below:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Download Dataset:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/generators-yield/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-generators-yield&quot; data-focus=&quot;false&quot;&gt;Click here to download the dataset you&#39;ll use in this tutorial&lt;/a&gt; to learn about generators and yield in Python.&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;How have generators helped you in your work or projects? If you&amp;rsquo;re just learning about them, then how do you plan to use them in the future? Did you find a good solution to the data pipeline problem? Let us know in the comments below!&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Thonny: The Beginner-Friendly Python Editor</title>
      <id>https://realpython.com/courses/python-thonny/</id>
      <link href="https://realpython.com/courses/python-thonny/"/>
      <updated>2019-09-24T14:00:00+00:00</updated>
      <summary>In this course, you’ll learn all about Thonny, a free Python Integrated Development Environment (IDE) that was especially designed with the beginner Pythonista in mind. It has a built-in debugger and allows you to do step-through expression evaluation.</summary>
      <content type="html">
        &lt;p&gt;Are you a &lt;a href=&quot;https://realpython.com/learning-paths/python3-introduction/&quot;&gt;Python beginner&lt;/a&gt; looking for a tool that can support your learning? This course is for you! Every programmer needs a place to write their code. This course will cover an awesome tool called &lt;strong&gt;Thonny&lt;/strong&gt; that will enable you to start working with Python in a &lt;strong&gt;beginner-friendly environment&lt;/strong&gt;. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this course, you&amp;rsquo;ll learn:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;How to install Thonny on your computer&lt;/li&gt;
&lt;li&gt;How to navigate Thonny&amp;rsquo;s user interface to use its built-in features&lt;/li&gt;
&lt;li&gt;How to use Thonny to write and run your code&lt;/li&gt;
&lt;li&gt;How to use Thonny to debug your code&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By the end of this course, you&amp;rsquo;ll be comfortable with the development workflow in Thonny and ready to use it for your Python learning. So what is Thonny? Great question!&lt;/p&gt;
&lt;p&gt;Thonny is a free Python Integrated Development Environment (&lt;a href=&quot;https://realpython.com/python-ides-code-editors-guide/&quot;&gt;IDE&lt;/a&gt;) that was especially designed with the beginner Pythonista in mind. Specifically, it has a built-in debugger that can help when you run into nasty bugs, and it allows you to do step-through expression evaluation, among other really awesome features.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Getting Started With Async Features in Python</title>
      <id>https://realpython.com/python-async-features/</id>
      <link href="https://realpython.com/python-async-features/"/>
      <updated>2019-09-23T14:00:00+00:00</updated>
      <summary>This step-by-step tutorial gives you the tools you need to start making asynchronous programming techniques a part of your repertoire. You&#39;ll learn how to use Python async features to take advantage of IO processes and free up your CPU.</summary>
      <content type="html">
        &lt;p&gt;Have you heard of asynchronous programming in Python? Are you curious to know more about Python async features and how you can use them in your work? Perhaps you&amp;rsquo;ve even tried to write threaded programs and run into some issues. If you&amp;rsquo;re looking to understand how to use Python async features, then you&amp;rsquo;ve come to the right place.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this article, you&amp;rsquo;ll learn:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What a &lt;strong&gt;synchronous program&lt;/strong&gt; is&lt;/li&gt;
&lt;li&gt;What an &lt;strong&gt;asynchronous program&lt;/strong&gt; is&lt;/li&gt;
&lt;li&gt;Why you might want to write an asynchronous program&lt;/li&gt;
&lt;li&gt;How to use Python async features&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All of the example code in this article have been tested with Python 3.7.2. You can grab a copy to follow along by clicking the link below:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Download Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/async-features/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-async-features&quot; data-focus=&quot;false&quot;&gt;Click here to download the code you&#39;ll use&lt;/a&gt; to learn about async features in Python in this tutorial.&lt;/p&gt;&lt;/div&gt;

&lt;h2 id=&quot;understanding-asynchronous-programming&quot;&gt;Understanding Asynchronous Programming&lt;/h2&gt;
&lt;p&gt;A &lt;strong&gt;synchronous program&lt;/strong&gt; is executed one step at a time. Even with conditional branching, loops and function calls, you can still think about the code in terms of taking one execution step at a time. When each step is complete, the program moves on to the next one.&lt;/p&gt;
&lt;p&gt;Here are two examples of programs that work this way:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Batch processing programs&lt;/strong&gt; are often created as synchronous programs. You get some input, process it, and create some output. Steps follow one after the other until the program reaches the desired output. The program only needs to pay attention to the steps and their order.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Command-line programs&lt;/strong&gt; are small, quick processes that run in a terminal. These scripts are used to create something, transform one thing into something else, generate a report, or perhaps list out some data. This can be expressed as a series of program steps that are executed sequentially until the program is done.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;An &lt;strong&gt;asynchronous program&lt;/strong&gt; behaves differently. It still takes one execution step at a time. The difference is that the system may not wait for an execution step to be completed before moving on to the next one.&lt;/p&gt;
&lt;p&gt;This means that the program will move on to future execution steps even though a previous step hasn&amp;rsquo;t yet finished and is still running elsewhere. This also means that the program knows what to do when a previous step does finish running.&lt;/p&gt;
&lt;p&gt;Why would you want to write a program in this manner? The rest of this article will help you answer that question and give you the tools you need to elegantly solve interesting asynchronous problems.&lt;/p&gt;
&lt;h3 id=&quot;building-a-synchronous-web-server&quot;&gt;Building a Synchronous Web Server&lt;/h3&gt;
&lt;p&gt;A web server&amp;rsquo;s basic unit of work is, more or less, the same as batch processing. The server will get some input, process it, and create the output. Written as a synchronous program, this would create a working web server.&lt;/p&gt;
&lt;p&gt;It would also be an &lt;em&gt;absolutely terrible&lt;/em&gt; web server.&lt;/p&gt;
&lt;p&gt;Why? In this case, one unit of work (input, process, output) is not the only purpose. The real purpose is to handle hundreds or even thousands of units of work as quickly as possible. This can happen over long periods of time, and several work units may even arrive all at once.&lt;/p&gt;
&lt;p&gt;Can a synchronous web server be made better? Sure, you could optimize the execution steps so that all the work coming in is handled as quickly as possible. Unfortunately, there are limitations to this approach. The result could be a web server that doesn&amp;rsquo;t respond fast enough, can&amp;rsquo;t handle enough work, or even one that times out when work gets stacked up.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; There are other limitations you might see if you tried to optimize the above approach. These include network speed, file IO speed, database query speed, and the speed of other connected services, to name a few. What these all have in common is that they are all IO functions. All of these items are orders of magnitude slower than the CPU&amp;rsquo;s processing speed.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;In a synchronous program, if an execution step starts a database query, then the CPU is essentially idle until the database query is returned. For batch-oriented programs, this isn&amp;rsquo;t a priority most of the time. Processing the results of that IO operation is the goal. Often, this can take longer than the IO operation itself. Any optimization efforts would be focused on the processing work, not the IO.&lt;/p&gt;
&lt;p&gt;Asynchronous programming techniques allow your programs to take advantage of relatively slow IO processes by freeing the CPU to do other work.&lt;/p&gt;
&lt;h3 id=&quot;thinking-differently-about-programming&quot;&gt;Thinking Differently About Programming&lt;/h3&gt;
&lt;p&gt;When you start trying to understand asynchronous programming, you might see a lot of discussion about the importance of &lt;strong&gt;blocking&lt;/strong&gt;, or writing &lt;a href=&quot;https://medium.com/vaidikkapoor/understanding-non-blocking-i-o-with-python-part-1-ec31a2e2db9b&quot;&gt;&lt;strong&gt;non-blocking code&lt;/strong&gt;&lt;/a&gt;. (Personally, I struggled to get a good grasp of these concepts from the people I asked and the documentation I read.)&lt;/p&gt;
&lt;p&gt;What is non-blocking code? What&amp;rsquo;s blocking code, for that matter? Would the answers to these questions help you write a better web server? If so, how could you do it? Let&amp;rsquo;s find out!&lt;/p&gt;
&lt;p&gt;Writing asynchronous programs requires that you think differently about programming. While this new way of thinking can be hard to wrap your head around, it&amp;rsquo;s also an interesting exercise. That&amp;rsquo;s because the real world is almost entirely asynchronous, and so is how you interact with it.&lt;/p&gt;
&lt;p&gt;Imagine this: you&amp;rsquo;re a parent trying to do several things at once. You have to balance the checkbook, do the laundry, and keep an eye on the kids. Somehow, you&amp;rsquo;re able to do all of these things at the same time without even thinking about it! Let&amp;rsquo;s break it down:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Balancing the checkbook is a &lt;strong&gt;synchronous&lt;/strong&gt; task. One step follows another until it&amp;rsquo;s done. You&amp;rsquo;re doing all the work yourself.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;However, you can break away from the checkbook to do laundry. You unload the dryer, move clothes from the washer to the dryer, and start another load in the washer.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Working with the washer and dryer is a synchronous task, but the bulk of the work happens &lt;em&gt;after&lt;/em&gt; the washer and dryer are started. Once you&amp;rsquo;ve got them going, you can walk away and get back to the checkbook task. At this point, the washer and dryer tasks have become &lt;strong&gt;asynchronous&lt;/strong&gt;. The washer and dryer will run independently until the buzzer goes off (notifying you that the task needs attention).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Watching your kids is another asynchronous task. Once they are set up and playing, they can do so independently for the most part. This changes when someone needs attention, like when someone gets hungry or hurt. When one of your kids yells in alarm, you react. The kids are a long-running task with high priority. Watching them supersedes any other tasks you might be doing, like the checkbook or laundry.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These examples can help to illustrate the concepts of blocking and non-blocking code. Let&amp;rsquo;s think about this in programming terms. In this example, you&amp;rsquo;re like the CPU. While you&amp;rsquo;re moving the laundry around, you (the CPU) are busy and &lt;strong&gt;blocked&lt;/strong&gt; from doing other work, like balancing the checkbook. But that&amp;rsquo;s okay because the task is relatively quick.&lt;/p&gt;
&lt;p&gt;On the other hand, starting the washer and dryer does not block you from performing other tasks. It&amp;rsquo;s an asynchronous function because you don&amp;rsquo;t have to wait for it to finish. Once it&amp;rsquo;s started, you can go back to something else. This is called a &lt;strong&gt;context switch&lt;/strong&gt;: the context of what you&amp;rsquo;re doing has changed, and the machine&amp;rsquo;s buzzer will notify you sometime in the future when the laundry task is complete.&lt;/p&gt;
&lt;p&gt;As a human, this is how you work all the time. You naturally juggle multiple things at once, often without thinking about it. As a developer, the trick is how to translate this kind of behavior into code that does the same kind of thing.&lt;/p&gt;
&lt;h2 id=&quot;programming-parents-not-as-easy-as-it-looks&quot;&gt;Programming Parents: Not as Easy as It Looks!&lt;/h2&gt;
&lt;p&gt;If you recognize yourself (or your parents) in the example above, then that&amp;rsquo;s great! You&amp;rsquo;ve got a leg up in understanding asynchronous programming. Again, you&amp;rsquo;re able to switch contexts between competing tasks fairly easily, picking up some tasks and resuming others. Now you&amp;rsquo;re going to try and program this behavior into virtual parents!&lt;/p&gt;
&lt;h3 id=&quot;thought-experiment-1-the-synchronous-parent&quot;&gt;Thought Experiment #1: The Synchronous Parent&lt;/h3&gt;
&lt;p&gt;How would you create a parent program to do the above tasks in a completely synchronous manner? Since watching the kids is a high-priority task, perhaps your program would do just that. The parent watches over the kids while waiting for something to happen that might need their attention. However, nothing else (like the checkbook or laundry) would get done in this scenario.&lt;/p&gt;
&lt;p&gt;Now, you can re-prioritize the tasks any way you want, but only one of them would happen at any given time. This is the result of a synchronous, step-by-step approach. Like the synchronous web server described above, this would work, but it might not be the best way to live. The parent wouldn&amp;rsquo;t be able to complete any other tasks until the kids fell asleep. All other tasks would happen afterward, well into the night. (A couple of weeks of this and many real parents might jump out the window!)&lt;/p&gt;
&lt;h3 id=&quot;thought-experiment-2-the-polling-parent&quot;&gt;Thought Experiment #2: The Polling Parent&lt;/h3&gt;
&lt;p&gt;If you used &lt;a href=&quot;https://en.wikipedia.org/wiki/Polling_(computer_science)&quot;&gt;&lt;strong&gt;polling&lt;/strong&gt;&lt;/a&gt;, then you could change things up so that multiple tasks are completed. In this approach, the parent would periodically break away from the current task and check to see if any other tasks need attention.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s make the polling interval something like fifteen minutes. Now, every fifteen minutes your parent checks to see if the washer, dryer or kids need any attention. If not, then the parent can go back to work on the checkbook. However, if any of those tasks &lt;em&gt;do&lt;/em&gt; need attention, then the parent will take care of it before going back to the checkbook. This cycle continues on until the next timeout out of the polling loop.&lt;/p&gt;
&lt;p&gt;This approach works as well since multiple tasks are getting attention. However, there are a couple of problems:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The parent may spend a lot of time checking on things that don&amp;rsquo;t need attention:&lt;/strong&gt; The washer and dryer haven&amp;rsquo;t yet finished, and the kids don&amp;rsquo;t need any attention unless something unexpected happens.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The parent may miss completed tasks that do need attention:&lt;/strong&gt; For instance, if the washer finished its cycle at the beginning of the polling interval, then it wouldn&amp;rsquo;t get any attention for up to fifteen minutes! What&amp;rsquo;s more, watching the kids is supposedly the highest priority task. They couldn&amp;rsquo;t tolerate fifteen minutes with no attention when something might be going drastically wrong.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You could address these issues by shortening the polling interval, but now your parent (the CPU) would be spending more time context switching between tasks. This is when you start to hit a point of diminishing returns. (Once again, a couple of weeks living like this and, well&amp;hellip; See the previous comment about windows and jumping.)&lt;/p&gt;
&lt;h3 id=&quot;thought-experiment-3-the-threading-parent&quot;&gt;Thought Experiment #3: The Threading Parent&lt;/h3&gt;
&lt;p&gt;&amp;ldquo;If I could only clone myself&amp;hellip;&amp;rdquo; If you&amp;rsquo;re a parent, then you&amp;rsquo;ve probably had similar thoughts! Since you&amp;rsquo;re programming virtual parents, you can essentially do this by using &lt;strong&gt;threading&lt;/strong&gt;. This is a mechanism that allows multiple sections of one program to run at the same time. Each section of code that runs independently is known as a &lt;strong&gt;thread&lt;/strong&gt;, and all threads share the same memory space.&lt;/p&gt;
&lt;p&gt;If you think of each task as a part of one program, then you can separate them and run them as threads. In other words, you can &amp;ldquo;clone&amp;rdquo; the parent, creating one instance for each task: watching the kids, monitoring the washer, monitoring the dryer, and balancing the checkbook. All of these &amp;ldquo;clones&amp;rdquo; are running independently.&lt;/p&gt;
&lt;p&gt;This sounds like a pretty nice solution, but there are some issues here as well. One is that you&amp;rsquo;ll have to explicitly tell each parent instance what to do in your program. This can lead to some problems since all instances share everything in the program space.&lt;/p&gt;
&lt;p&gt;For example, say that Parent A is monitoring the dryer. Parent A sees that the clothes are dry, so they take control of the dryer and begin unloading the clothes. At the same time, Parent B sees that the washer is done, so they take control of the washer and begin removing clothes. However, Parent B also needs to take control of the dryer so they can put the wet clothes inside. This can&amp;rsquo;t happen, because Parent A currently has control of the dryer.&lt;/p&gt;
&lt;p&gt;After a short while, Parent A has finished unloading clothes. Now they want to take control of the washer and start moving clothes into the empty dryer. This can&amp;rsquo;t happen, either, because Parent B currently has control of the washer!&lt;/p&gt;
&lt;p&gt;These two parents are now &lt;a href=&quot;https://realpython.com/intro-to-python-threading/#deadlock&quot;&gt;&lt;strong&gt;deadlocked&lt;/strong&gt;&lt;/a&gt;. Both have control of their own resource &lt;em&gt;and&lt;/em&gt; want control of the other resource. They&amp;rsquo;ll wait forever for the other parent instance to release control. As the programmer, you&amp;rsquo;d have to write code to work this situation out.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Threaded programs allow you to create multiple, parallel paths of execution that all share the same memory space. This is both an advantage and a disadvantage. Any memory shared between threads is subject to one or more threads trying to use the same shared memory at the same time. This can lead to data corruption, data read in an invalid state, and data that&amp;rsquo;s just messy in general.&lt;/p&gt;
&lt;p&gt;In threaded programming, the context switch happens under system control, not the programmer. The system controls when to switch contexts and when to give threads access to shared data, thereby changing the context of how the memory is being used. All of these kinds of problems are manageable in threaded code, but it&amp;rsquo;s difficult to get right, and hard to debug when it&amp;rsquo;s wrong.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Here&amp;rsquo;s another issue that might arise from threading. Suppose that a child gets hurt and needs to be taken to urgent care. Parent C has been assigned the task of watching over the kids, so they take the child right away. At the urgent care, Parent C needs to write a fairly large check to cover the cost of seeing the doctor.&lt;/p&gt;
&lt;p&gt;Meanwhile, Parent D is at home working on the checkbook. They&amp;rsquo;re unaware of this large check being written, so they&amp;rsquo;re very surprised when the family checking account is suddenly overdrawn!&lt;/p&gt;
&lt;p&gt;Remember, these two parent instances are working within the same program. The family checking account is a &lt;strong&gt;shared resource&lt;/strong&gt;, so you&amp;rsquo;d have to work out a way for the child-watching parent to inform the checkbook-balancing parent. Otherwise, you&amp;rsquo;d need to provide some kind of locking mechanism so that the checkbook resource can only be used by one parent at a time, with updates.&lt;/p&gt;
&lt;h2 id=&quot;using-python-async-features-in-practice&quot;&gt;Using Python Async Features in Practice&lt;/h2&gt;
&lt;p&gt;Now you&amp;rsquo;re going to take some of the approaches outlined in the thought experiments above and turn them into functioning Python programs.&lt;/p&gt;
&lt;p&gt;All of the examples in this article have been tested with Python 3.7.2. The &lt;code&gt;requirements.txt&lt;/code&gt; file indicates which modules you&amp;rsquo;ll need to install to run all the examples. If you haven&amp;rsquo;t yet downloaded the file, you can do so now:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Download Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/async-features/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-async-features&quot; data-focus=&quot;false&quot;&gt;Click here to download the code you&#39;ll use&lt;/a&gt; to learn about async features in Python in this tutorial.&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;You also might want to set up a &lt;a href=&quot;https://realpython.com/python-virtual-environments-a-primer/&quot;&gt;Python virtual environment&lt;/a&gt; to run the code so you don&amp;rsquo;t interfere with your system Python.&lt;/p&gt;
&lt;h3 id=&quot;synchronous-programming&quot;&gt;Synchronous Programming&lt;/h3&gt;
&lt;p&gt;This first example shows a somewhat contrived way of having a task retrieve work from a queue and process that work. A queue in Python is a nice &lt;a href=&quot;https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics)&quot;&gt;FIFO&lt;/a&gt; (first in first out) data structure. It provides methods to put things in a queue and take them out again in the order they were inserted.&lt;/p&gt;
&lt;p&gt;In this case, the work is to get a number from the queue and have a loop count up to that number. It prints to the console when the loop begins, and again to output the total. This program demonstrates one way for multiple synchronous tasks to process the work in a queue.&lt;/p&gt;
&lt;p&gt;The program named &lt;code&gt;example_1.py&lt;/code&gt; in the repository is listed in full below:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Task &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; nothing to do&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;            &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Task &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; running&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;            &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Task &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; total: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{total}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;    &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;    This is the main entry point for the program.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Create the queue of &amp;#39;work&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Put some &amp;#39;work&amp;#39; in the queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Create some synchronous tasks&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;One&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Two&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Run the tasks&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;34 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;35 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;36 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;__main__&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;37 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let&amp;rsquo;s take a look at what each line does:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Line 1&lt;/strong&gt; imports the &lt;code&gt;queue&lt;/code&gt; module. This is where the program stores work to be done by the tasks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lines 3 to 13&lt;/strong&gt; define &lt;code&gt;task()&lt;/code&gt;. This function pulls work out of &lt;code&gt;work_queue&lt;/code&gt; and processes the work until there isn&amp;rsquo;t any more to do.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 15&lt;/strong&gt; defines &lt;code&gt;main()&lt;/code&gt; to run the program tasks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 20&lt;/strong&gt; creates the &lt;code&gt;work_queue&lt;/code&gt;. All tasks use this shared resource to retrieve work.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lines 23 to 24&lt;/strong&gt; put work in &lt;code&gt;work_queue&lt;/code&gt;. In this case, it&amp;rsquo;s just a random count of values for the tasks to process.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lines 27 to 29&lt;/strong&gt; create a list of task tuples, with the parameter values those tasks will be passed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lines 33 to 34&lt;/strong&gt; iterate over the list of task tuples, calling each one and passing the previously defined parameter values.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 36&lt;/strong&gt; calls &lt;code&gt;main()&lt;/code&gt; to run the program.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The task in this program is just a function accepting a string and a queue as parameters. When executed, it looks for anything in the queue to process. If there is work to do, then it pulls values off the queue, starts a &lt;a href=&quot;https://realpython.com/courses/python-for-loop/&quot;&gt;&lt;code&gt;for&lt;/code&gt; loop&lt;/a&gt; to count up to that value, and outputs the total at the end. It continues getting work off the queue until there is nothing left and it exits.&lt;/p&gt;
&lt;p&gt;When this program is run, it produces the output you see below:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;Task One running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total: 15&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total: 10&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total: 5&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total: 2&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two nothing to do&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This shows that &lt;code&gt;Task One&lt;/code&gt; does all the work. The &lt;a href=&quot;https://realpython.com/courses/mastering-while-loops/&quot;&gt;&lt;code&gt;while&lt;/code&gt; loop&lt;/a&gt; that &lt;code&gt;Task One&lt;/code&gt; hits within &lt;code&gt;task()&lt;/code&gt; consumes all the work on the queue and processes it. When that loop exits, &lt;code&gt;Task Two&lt;/code&gt; gets a chance to run. However, it finds that the queue is empty, so &lt;code&gt;Task Two&lt;/code&gt; prints a statement that says it has nothing to do and then exits. There&amp;rsquo;s nothing in the code to allow both &lt;code&gt;Task One&lt;/code&gt; and &lt;code&gt;Task Two&lt;/code&gt; to switch contexts and work together.&lt;/p&gt;
&lt;h3 id=&quot;simple-cooperative-concurrency&quot;&gt;Simple Cooperative Concurrency&lt;/h3&gt;
&lt;p&gt;The next version of the program allows the two tasks to work together. Adding a &lt;code&gt;yield&lt;/code&gt; statement means the loop will yield control at the specified point while still maintaining its context. This way, the yielding task can be restarted later.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;yield&lt;/code&gt; statement turns &lt;code&gt;task()&lt;/code&gt; into a &lt;a href=&quot;https://realpython.com/introduction-to-python-generators/&quot;&gt;&lt;strong&gt;generator&lt;/strong&gt;&lt;/a&gt;.  A generator function is called just like any other function in Python, but when the &lt;code&gt;yield&lt;/code&gt; statement is executed, control is returned to the caller of the function. This is essentially a context switch, as control moves from the generator function to the caller.&lt;/p&gt;
&lt;p&gt;The interesting part is that control can be given &lt;em&gt;back&lt;/em&gt; to the generator function by calling &lt;code&gt;next()&lt;/code&gt; on the generator. This is a context switch back to the generator function, which picks up execution with all function variables that were defined before the &lt;code&gt;yield&lt;/code&gt; still intact.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;while&lt;/code&gt; loop in &lt;code&gt;main()&lt;/code&gt; takes advantage of this when it calls &lt;code&gt;next(t)&lt;/code&gt;. This statement restarts the task at the point where it previously yielded. All of this means that you&amp;rsquo;re in control when the context switch happens: when the &lt;code&gt;yield&lt;/code&gt; statement is executed in &lt;code&gt;task()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This is a form of &lt;strong&gt;cooperative multitasking&lt;/strong&gt;. The program is yielding control of its current context so that something else can run. In this case, it allows the &lt;code&gt;while&lt;/code&gt; loop in &lt;code&gt;main()&lt;/code&gt; to run two instances of &lt;code&gt;task()&lt;/code&gt; as a generator function. Each instance consumes work from the same queue. This is sort of clever, but it&amp;rsquo;s also a lot of work to get the same results as the first program. The program &lt;code&gt;example_2.py&lt;/code&gt; demonstrates this simple concurrency and is listed below:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Task &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; running&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Task &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; total: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{total}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;    &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;    This is the main entry point for the program.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Create the queue of &amp;#39;work&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Put some &amp;#39;work&amp;#39; in the queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Create some tasks&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;One&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Two&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Run the tasks&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;34 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;35 &lt;/span&gt;                &lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;36 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;StopIteration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;37 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;38 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;39 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;40 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;41 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;__main__&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;42 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here&amp;rsquo;s what&amp;rsquo;s happening in the code above:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Lines 3 to 11&lt;/strong&gt; define &lt;code&gt;task()&lt;/code&gt; as before, but the addition of &lt;code&gt;yield&lt;/code&gt; on Line 10 turns the function into a generator. This where the context switch is made and control is handed back to the &lt;code&gt;while&lt;/code&gt; loop in &lt;code&gt;main()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lines 25 to 28&lt;/strong&gt; create the task list, but in a slightly different manner than you saw in the previous example code. In this case, each task is called with its parameters as its entered in the &lt;code&gt;tasks&lt;/code&gt; list variable. This is necessary to get the &lt;code&gt;task()&lt;/code&gt; generator function running the first time.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lines 34 to 39&lt;/strong&gt; are the modifications to the &lt;code&gt;while&lt;/code&gt; loop in &lt;code&gt;main()&lt;/code&gt; that allow &lt;code&gt;task()&lt;/code&gt; to run cooperatively. This is where control returns to each instance of &lt;code&gt;task()&lt;/code&gt; when it yields, allowing the loop to continue and run another task.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 35&lt;/strong&gt; gives control back to &lt;code&gt;task()&lt;/code&gt;, and continues its execution after the point where &lt;code&gt;yield&lt;/code&gt; was called.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 39&lt;/strong&gt; sets the &lt;code&gt;done&lt;/code&gt; variable. The &lt;code&gt;while&lt;/code&gt; loop ends when all tasks have been completed and removed from &lt;code&gt;tasks&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is the output produced when you run this program:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;Task One running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two total: 10&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total: 15&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two total: 5&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total: 2&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can see that both &lt;code&gt;Task One&lt;/code&gt; and &lt;code&gt;Task Two&lt;/code&gt; are running and consuming work from the queue. This is what&amp;rsquo;s intended, as both tasks are processing work, and each is responsible for two items in the queue. This is interesting, but again, it takes quite a bit of work to achieve these results.&lt;/p&gt;
&lt;p&gt;The trick here is using the &lt;code&gt;yield&lt;/code&gt; statement, which turns &lt;code&gt;task()&lt;/code&gt; into a generator and performs a context switch. The program uses this context switch to give control to the &lt;code&gt;while&lt;/code&gt; loop in &lt;code&gt;main()&lt;/code&gt;, allowing two instances of a task to run cooperatively.&lt;/p&gt;
&lt;p&gt;Notice how &lt;code&gt;Task Two&lt;/code&gt; outputs its total first. This might lead you to think that the tasks are running asynchronously. However, this is still a synchronous program. It&amp;rsquo;s structured so the two tasks can trade contexts back and forth. The reason why &lt;code&gt;Task Two&lt;/code&gt; outputs its total first is that it&amp;rsquo;s only counting to 10, while &lt;code&gt;Task One&lt;/code&gt; is counting to 15. &lt;code&gt;Task Two&lt;/code&gt; simply arrives at its total first, so it gets to print its output to the console before &lt;code&gt;Task One&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;cooperative-concurrency-with-blocking-calls&quot;&gt;Cooperative Concurrency With Blocking Calls&lt;/h3&gt;
&lt;p&gt;The next version of the program is the same as the last, except for the addition of &lt;code&gt;time.sleep(delay)&lt;/code&gt; in the body of your task loop. This adds a delay based on the value retrieved from the work queue to every iteration of the task loop. The delay is added to simulate the effect of a &lt;strong&gt;blocking call&lt;/strong&gt; occurring in your task.&lt;/p&gt;
&lt;p&gt;A blocking call is code that stops the CPU from doing anything else for some time. In the thought experiments above, if a parent wasn&amp;rsquo;t able to break away from balancing the checkbook until it was complete, then that would be a blocking call.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;time.sleep(delay)&lt;/code&gt; does the same thing in this example, because the CPU can&amp;rsquo;t do anything else but wait for the delay to expire.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;elapsed_time&lt;/code&gt; provides a way to get the elapsed time from when an instance of the class is created until it&amp;rsquo;s called as a function. The program &lt;code&gt;example_3.py&lt;/code&gt; is listed below:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;lib.elapsed_time&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;et&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Task &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; running&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Task &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; total elapsed time: {et():.1f}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;    &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;    This is the main entry point for the program.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Create the queue of &amp;#39;work&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Put some &amp;#39;work&amp;#39; in the queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;One&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Two&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Run the tasks&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;et&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;34 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;35 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;36 &lt;/span&gt;                &lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;37 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;StopIteration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;38 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;39 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;40 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;41 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;42 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Total elapsed time: {et():.1f}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;43 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;44 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;__main__&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;45 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here&amp;rsquo;s what&amp;rsquo;s different in the code above:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Line 1&lt;/strong&gt; imports the &lt;code&gt;time&lt;/code&gt; module to give the program access to &lt;code&gt;time.sleep()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 11&lt;/strong&gt; changes &lt;code&gt;task()&lt;/code&gt; to include a &lt;code&gt;time.sleep(delay)&lt;/code&gt; to mimic an IO delay. This replaces the &lt;code&gt;for&lt;/code&gt; loop that did the counting in &lt;code&gt;example_1.py&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When you run this program, you&amp;rsquo;ll see the following output:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;Task One running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total elapsed time: 15.0&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two total elapsed time: 10.0&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total elapsed time: 5.0&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two total elapsed time: 2.0&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;Total elapsed time: 32.01021909713745&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As before, both &lt;code&gt;Task One&lt;/code&gt; and &lt;code&gt;Task Two&lt;/code&gt; are running, consuming work from the queue and processing it. However, even with the addition of the delay, you can see that cooperative concurrency hasn&amp;rsquo;t gotten you anything. The delay stops the processing of the entire program, and the CPU just waits for the IO delay to be over.&lt;/p&gt;
&lt;p&gt;This is exactly what&amp;rsquo;s meant by &lt;strong&gt;blocking code&lt;/strong&gt; in Python async documentation. You&amp;rsquo;ll notice that the time it takes to run the entire program is just the cumulative time of all the delays. Running tasks this way is not a win.&lt;/p&gt;
&lt;h3 id=&quot;cooperative-concurrency-with-non-blocking-calls&quot;&gt;Cooperative Concurrency With Non-Blocking Calls&lt;/h3&gt;
&lt;p&gt;The next version of the program has been modified quite a bit. It makes use of Python async features using &lt;a href=&quot;https://realpython.com/async-io-python/&quot;&gt;asyncio/await&lt;/a&gt; provided in Python 3.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;time&lt;/code&gt; and &lt;code&gt;queue&lt;/code&gt; modules have been replaced with the &lt;code&gt;asyncio&lt;/code&gt; package. This gives your program access to asynchronous friendly (non-blocking) sleep and queue functionality. The change to &lt;code&gt;task()&lt;/code&gt; defines it as asynchronous with the addition of the &lt;code&gt;async&lt;/code&gt; prefix on line 4. This indicates to Python that the function will be asynchronous.&lt;/p&gt;
&lt;p&gt;The other big change is removing the &lt;code&gt;time.sleep(delay)&lt;/code&gt; and &lt;code&gt;yield&lt;/code&gt; statements, and replacing them with &lt;code&gt;await asyncio.sleep(delay)&lt;/code&gt;. This creates a non-blocking delay that will perform a context switch back to the caller &lt;code&gt;main()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;while&lt;/code&gt; loop inside &lt;code&gt;main()&lt;/code&gt; no longer exists. Instead of &lt;code&gt;task_array&lt;/code&gt;, there&amp;rsquo;s a call to &lt;code&gt;await asyncio.gather(...)&lt;/code&gt;. This tells &lt;code&gt;asyncio&lt;/code&gt; two things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create two tasks based on &lt;code&gt;task()&lt;/code&gt; and start running them.&lt;/li&gt;
&lt;li&gt;Wait for both of these to be completed before moving forward.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The last line of the program &lt;code&gt;asyncio.run(main())&lt;/code&gt; runs &lt;code&gt;main()&lt;/code&gt;. This creates what&amp;rsquo;s known as an &lt;a href=&quot;https://realpython.com/lessons/asyncio-event-loop/&quot;&gt;event loop&lt;/a&gt;). It&amp;rsquo;s this loop that will run &lt;code&gt;main()&lt;/code&gt;, which in turn will run the two instances of &lt;code&gt;task()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The event loop is at the heart of the Python async system. It runs all the code, including &lt;code&gt;main()&lt;/code&gt;. When task code is executing, the CPU is busy doing work. When the &lt;code&gt;await&lt;/code&gt; keyword is reached, a context switch occurs, and control passes back to the event loop. The event loop looks at all the tasks waiting for an event (in this case, an &lt;code&gt;asyncio.sleep(delay)&lt;/code&gt; timeout) and passes control to a task with an event that&amp;rsquo;s ready.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;await asyncio.sleep(delay)&lt;/code&gt; is non-blocking in regards to the CPU. Instead of waiting for the delay to timeout, the CPU registers a sleep event on the event loop task queue and performs a context switch by passing control to the event loop. The event loop continuously looks for completed events and passes control back to the task waiting for that event. In this way, the CPU can stay busy if work is available, while the event loop monitors the events that will happen in the future.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; An asynchronous program runs in a single thread of execution. The context switch from one section of code to another that would affect data is completely in your control. This means you can atomize and complete all shared memory data access before making a context switch. This simplifies the shared memory problem inherent in threaded code.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;example_4.py&lt;/code&gt; code is listed below:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;asyncio&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;lib.elapsed_time&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;et&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Task &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; running&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncio&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;delay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Task &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; total elapsed time: {et():.1f}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;    &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;    This is the main entry point for the program.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Create the queue of &amp;#39;work&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncio&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Put some &amp;#39;work&amp;#39; in the queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Run the tasks&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;et&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncio&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gather&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;asyncio&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;One&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;asyncio&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Two&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Total elapsed time: {et():.1f}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;__main__&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;asyncio&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here&amp;rsquo;s what&amp;rsquo;s different between this program and &lt;code&gt;example_3.py&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Line 1&lt;/strong&gt; imports &lt;code&gt;asyncio&lt;/code&gt; to gain access to Python async functionality. This replaces the &lt;code&gt;time&lt;/code&gt; import.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 4&lt;/strong&gt; shows the addition of the &lt;code&gt;async&lt;/code&gt; keyword in front of the &lt;code&gt;task()&lt;/code&gt; definition. This informs the program that &lt;code&gt;task&lt;/code&gt; can run asynchronously.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 9&lt;/strong&gt; replaces &lt;code&gt;time.sleep(delay)&lt;/code&gt; with the non-blocking &lt;code&gt;asyncio.sleep(delay)&lt;/code&gt;, which also yields control (or switches contexts) back to the main event loop.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 17&lt;/strong&gt; creates the non-blocking asynchronous &lt;code&gt;work_queue&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lines 20 to 21&lt;/strong&gt; put work into &lt;code&gt;work_queue&lt;/code&gt; in an asynchronous manner using the &lt;code&gt;await&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lines 25 to 28&lt;/strong&gt; create the two tasks and gather them together, so the program will wait for both tasks to complete.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 32&lt;/strong&gt; starts the program running asynchronously. It also starts the internal event loop.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When you look at the output of this program, notice how both &lt;code&gt;Task One&lt;/code&gt; and &lt;code&gt;Task Two&lt;/code&gt; start at the same time, then wait at the mock IO call:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;Task One running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two total elapsed time: 10.0&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total elapsed time: 15.0&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One running&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two total elapsed time: 5.0&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total elapsed time: 2.0&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;Total elapsed time: 17.0&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This indicates that &lt;code&gt;await asyncio.sleep(delay)&lt;/code&gt; is non-blocking, and that other work is being done.&lt;/p&gt;
&lt;p&gt;At the end of the program, you&amp;rsquo;ll notice the total elapsed time is essentially half the time it took for &lt;code&gt;example_3.py&lt;/code&gt; to run. That&amp;rsquo;s the advantage of a program that uses Python async features! Each task was able to run &lt;code&gt;await asyncio.sleep(delay)&lt;/code&gt; at the same time. The total execution time of the program is now less than the sum of its parts. You&amp;rsquo;ve broken away from the synchronous model!&lt;/p&gt;
&lt;h3 id=&quot;synchronous-blocking-http-calls&quot;&gt;Synchronous (Blocking) HTTP Calls&lt;/h3&gt;
&lt;p&gt;The next version of the program is kind of a step forward as well as a step back. The program is doing some actual work with real IO by making HTTP requests to a list of URLs and getting the page contents. However, it&amp;rsquo;s doing so in a blocking (synchronous) manner.&lt;/p&gt;
&lt;p&gt;The program has been modified to import &lt;a href=&quot;https://realpython.com/python-requests/&quot;&gt;the wonderful &lt;code&gt;requests&lt;/code&gt; module&lt;/a&gt; to make the actual HTTP requests. Also, the queue now contains a list of URLs, rather than numbers. In addition, &lt;code&gt;task()&lt;/code&gt; no longer increments a counter. Instead, &lt;code&gt;requests&lt;/code&gt; gets the contents of a URL retrieved from the queue, and prints how long it took to do so.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;example_5.py&lt;/code&gt; code is listed below:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;requests&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;lib.elapsed_time&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;requests&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;            &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Task &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; getting URL: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{url}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;et&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;            &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Task &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; total elapsed time: {et():.1f}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;    &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;    This is the main entry point for the program.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Create the queue of &amp;#39;work&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Put some &amp;#39;work&amp;#39; in the queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;        &lt;span class=&quot;s2&quot;&gt;&amp;quot;http://google.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;        &lt;span class=&quot;s2&quot;&gt;&amp;quot;http://yahoo.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;        &lt;span class=&quot;s2&quot;&gt;&amp;quot;http://linkedin.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;        &lt;span class=&quot;s2&quot;&gt;&amp;quot;http://apple.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;        &lt;span class=&quot;s2&quot;&gt;&amp;quot;http://microsoft.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;        &lt;span class=&quot;s2&quot;&gt;&amp;quot;http://facebook.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;        &lt;span class=&quot;s2&quot;&gt;&amp;quot;http://twitter.com&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;34 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;35 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;One&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;36 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Two&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;37 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;38 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;39 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Run the tasks&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;40 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;et&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;41 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;42 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;43 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;44 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;45 &lt;/span&gt;                &lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;46 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;StopIteration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;47 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;48 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tasks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;49 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;done&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;50 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;51 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Total elapsed time: {et():.1f}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;52 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;53 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;__main__&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;54 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here&amp;rsquo;s what&amp;rsquo;s happening in this program:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Line 2&lt;/strong&gt; imports &lt;code&gt;requests&lt;/code&gt;, which provides a convenient way to make HTTP calls.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 11&lt;/strong&gt; introduces a delay, similar to &lt;code&gt;example_3.py&lt;/code&gt;. However, this time it calls &lt;code&gt;session.get(url)&lt;/code&gt;, which returns the contents of the URL retrieved from &lt;code&gt;work_queue&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lines 23 to 32&lt;/strong&gt; put the list of URLs into &lt;code&gt;work_queue&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When you run this program, you&amp;rsquo;ll see the following output:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;Task One getting URL: http://google.com&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total elapsed time: 0.3&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two getting URL: http://yahoo.com&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two total elapsed time: 0.8&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One getting URL: http://linkedin.com&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total elapsed time: 0.4&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two getting URL: http://apple.com&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two total elapsed time: 0.3&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One getting URL: http://microsoft.com&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total elapsed time: 0.5&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two getting URL: http://facebook.com&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two total elapsed time: 0.5&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One getting URL: http://twitter.com&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total elapsed time: 0.4&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;Total elapsed time: 3.2&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Just like in earlier versions of the program, &lt;code&gt;yield&lt;/code&gt; turns &lt;code&gt;task()&lt;/code&gt; into a generator. It also performs a context switch that lets the other task instance run.&lt;/p&gt;
&lt;p&gt;Each task gets a URL from the work queue, retrieves the contents of the page, and reports how long it took to get that content.&lt;/p&gt;
&lt;p&gt;As before, &lt;code&gt;yield&lt;/code&gt; allows both your tasks to run cooperatively. However, since this program is running synchronously, each &lt;code&gt;session.get()&lt;/code&gt; call blocks the CPU until the page is retrieved. &lt;strong&gt;Note the total time it took to run the entire program at the end.&lt;/strong&gt; This will be meaningful for the next example.&lt;/p&gt;
&lt;h3 id=&quot;asynchronous-non-blocking-http-calls&quot;&gt;Asynchronous (Non-Blocking) HTTP Calls&lt;/h3&gt;
&lt;p&gt;This version of the program modifies the previous one to use Python async features. It also imports the &lt;a href=&quot;https://aiohttp.readthedocs.io/en/stable/&quot;&gt;&lt;code&gt;aiohttp&lt;/code&gt;&lt;/a&gt; module, which is a library to make HTTP requests in an asynchronous fashion using &lt;code&gt;asyncio&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The tasks here have been modified to remove the &lt;code&gt;yield&lt;/code&gt; call since the code to make the HTTP &lt;code&gt;GET&lt;/code&gt; call is no longer blocking. It also performs a context switch back to the event loop.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;example_6.py&lt;/code&gt; program is listed below:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;asyncio&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;aiohttp&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;lib.elapsed_time&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aiohttp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ClientSession&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;            &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Task &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; getting URL: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{url}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;et&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;                &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;            &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Task &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; total elapsed time: {et():.1f}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;    &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;    This is the main entry point for the program.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Create the queue of &amp;#39;work&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncio&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Put some &amp;#39;work&amp;#39; in the queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;        &lt;span class=&quot;s2&quot;&gt;&amp;quot;http://google.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;        &lt;span class=&quot;s2&quot;&gt;&amp;quot;http://yahoo.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;        &lt;span class=&quot;s2&quot;&gt;&amp;quot;http://linkedin.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;        &lt;span class=&quot;s2&quot;&gt;&amp;quot;http://apple.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;        &lt;span class=&quot;s2&quot;&gt;&amp;quot;http://microsoft.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;        &lt;span class=&quot;s2&quot;&gt;&amp;quot;http://facebook.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;        &lt;span class=&quot;s2&quot;&gt;&amp;quot;http://twitter.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;34 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Run the tasks&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;35 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;et&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;36 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asyncio&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gather&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;37 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;asyncio&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;One&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;38 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;asyncio&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Two&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;work_queue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;39 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;40 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Total elapsed time: {et():.1f}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;41 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;42 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;__main__&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;43 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;asyncio&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here&amp;rsquo;s what&amp;rsquo;s happening in this program:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Line 2&lt;/strong&gt; imports the &lt;code&gt;aiohttp&lt;/code&gt; library, which provides an asynchronous way to make HTTP calls.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 5&lt;/strong&gt; marks &lt;code&gt;task()&lt;/code&gt; as an asynchronous function.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 6&lt;/strong&gt; creates an &lt;code&gt;aiohttp&lt;/code&gt; session context manager.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 11&lt;/strong&gt; creates an &lt;code&gt;aiohttp&lt;/code&gt; response context manager. It also makes an HTTP &lt;code&gt;GET&lt;/code&gt; call to the URL taken from &lt;code&gt;work_queue&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 12&lt;/strong&gt; uses the response to get the text retrieved from the URL asynchronously.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When you run this program, you&amp;rsquo;ll see the following output:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;Task One getting URL: http://google.com&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two getting URL: http://yahoo.com&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total elapsed time: 0.3&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One getting URL: http://linkedin.com&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total elapsed time: 0.3&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One getting URL: http://apple.com&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total elapsed time: 0.3&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One getting URL: http://microsoft.com&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two total elapsed time: 0.9&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two getting URL: http://facebook.com&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two total elapsed time: 0.4&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two getting URL: http://twitter.com&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task One total elapsed time: 0.5&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Task Two total elapsed time: 0.3&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;Total elapsed time: 1.7&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Take a look at the total elapsed time, as well as the individual times to get the contents of each URL. You&amp;rsquo;ll see that the duration is about half the cumulative time of all the HTTP &lt;code&gt;GET&lt;/code&gt; calls. This is because the HTTP &lt;code&gt;GET&lt;/code&gt; calls are running &lt;strong&gt;asynchronously&lt;/strong&gt;. In other words, you&amp;rsquo;re effectively taking better advantage of the CPU by allowing it to make multiple requests at once.&lt;/p&gt;
&lt;p&gt;Because the CPU is so fast, this example could likely create as many tasks as there are URLs. In this case, the program&amp;rsquo;s run time would be that of the single slowest URL retrieval.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;This article has given you the tools you need to start making asynchronous programming techniques a part of your repertoire. Using Python async features gives you programmatic control of when context switches take place. This means that many of the tougher issues you might see in threaded programming are easier to deal with.&lt;/p&gt;
&lt;p&gt;Asynchronous programming is a powerful tool, but it isn&amp;rsquo;t useful for every kind of program. If you&amp;rsquo;re writing a program that calculates pi to the millionth decimal place, for instance, then asynchronous code won&amp;rsquo;t help you. That kind of program is CPU bound, without much IO. However, if you&amp;rsquo;re trying to implement a server or a program that performs IO (like file or network access), then using Python async features could make a huge difference.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To sum it up, you&amp;rsquo;ve learned:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What &lt;strong&gt;synchronous programs&lt;/strong&gt; are&lt;/li&gt;
&lt;li&gt;How &lt;strong&gt;asynchronous programs&lt;/strong&gt; are different, but also powerful and manageable&lt;/li&gt;
&lt;li&gt;Why you might want to write asynchronous programs&lt;/li&gt;
&lt;li&gt;How to use the built-in async features in Python&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can get the code for all of the example programs used in this tutorial:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Download Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/async-features/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-async-features&quot; data-focus=&quot;false&quot;&gt;Click here to download the code you&#39;ll use&lt;/a&gt; to learn about async features in Python in this tutorial.&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;Now that you&amp;rsquo;re equipped with these powerful skills, you can take your programs to the next level!&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>How to Convert a Python String to int</title>
      <id>https://realpython.com/convert-python-string-to-int/</id>
      <link href="https://realpython.com/convert-python-string-to-int/"/>
      <updated>2019-09-18T14:00:00+00:00</updated>
      <summary>There are several ways to represent integers in Python. In this quick and practical tutorial, you&#39;ll learn how you can store integers using int and str as well as how you can convert a Python string to an int and vice versa.</summary>
      <content type="html">
        &lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Integer&quot;&gt;Integers&lt;/a&gt; are whole numbers. In other words, they have no fractional component. Two data types you can use to store an integer in Python are &lt;a href=&quot;https://realpython.com/python-data-types/#integers&quot;&gt;&lt;code&gt;int&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/python-data-types/#strings&quot;&gt;&lt;code&gt;str&lt;/code&gt;&lt;/a&gt;. These types offer flexibility for working with integers in different circumstances. In this tutorial, you&amp;rsquo;ll learn how you can convert a Python string to an &lt;code&gt;int&lt;/code&gt;. You&amp;rsquo;ll also learn how to convert an &lt;code&gt;int&lt;/code&gt; to a string.&lt;/p&gt;
&lt;p&gt;By the end of this tutorial, you&amp;rsquo;ll understand:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;How to store integers using &lt;code&gt;str&lt;/code&gt; and &lt;code&gt;int&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;How to convert a Python string to an &lt;code&gt;int&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;How to convert a Python &lt;code&gt;int&lt;/code&gt; to a string&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&amp;rsquo;s get started!&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Python Pit Stop:&lt;/strong&gt; This tutorial is a &lt;strong&gt;quick&lt;/strong&gt; and &lt;strong&gt;practical&lt;/strong&gt; way to find the info you need, so you&amp;rsquo;ll be back to your project in no time!&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-cheat-sheet-shortened&quot; data-focus=&quot;false&quot;&gt;Click here to get a Python Cheat Sheet&lt;/a&gt; and learn the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.&lt;/p&gt;&lt;/div&gt;

&lt;h2 id=&quot;representing-integers-in-python&quot;&gt;Representing Integers in Python&lt;/h2&gt;
&lt;p&gt;An integer can be stored using different types. Two possible &lt;a href=&quot;https://realpython.com/python-data-types/&quot;&gt;Python data types&lt;/a&gt; for representing an integer are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.python.org/3/library/stdtypes.html#textseq&quot;&gt;&lt;code&gt;str&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex&quot;&gt;&lt;code&gt;int&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For example, you can represent an integer using a string literal:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;110&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, Python understands you to mean that you want to store the integer &lt;code&gt;110&lt;/code&gt; as a string. You can do the same with the integer data type:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;110&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It&amp;rsquo;s important to consider what you specifically mean by &lt;code&gt;&quot;110&quot;&lt;/code&gt; and &lt;code&gt;110&lt;/code&gt; in the examples above. As a human who has used the decimal number system for your whole life, it may be obvious that you mean the number &lt;em&gt;one hundred and ten&lt;/em&gt;. However, there are several other &lt;a href=&quot;https://realpython.com/python-encodings-guide/#covering-all-the-bases-other-number-systems&quot;&gt;number systems&lt;/a&gt;, such as &lt;strong&gt;binary&lt;/strong&gt; and &lt;strong&gt;hexadecimal&lt;/strong&gt;, which use different &lt;a href=&quot;https://simple.wikipedia.org/wiki/Base_(mathematics)&quot;&gt;bases&lt;/a&gt; to represent an integer.&lt;/p&gt;
&lt;p&gt;For example, you can represent the number &lt;em&gt;one hundred and ten&lt;/em&gt; in binary and hexadecimal as &lt;em&gt;1101110&lt;/em&gt; and &lt;em&gt;6e&lt;/em&gt; respectively.&lt;/p&gt;
&lt;p&gt;You can also represent your integers with other number systems in Python using the &lt;code&gt;str&lt;/code&gt; and &lt;code&gt;int&lt;/code&gt; data types:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;binary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mb&quot;&gt;0b1010&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hexadecimal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;0xa&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Notice that &lt;code&gt;binary&lt;/code&gt; and &lt;code&gt;hexadecimal&lt;/code&gt; use &lt;a href=&quot;https://docs.python.org/reference/lexical_analysis.html#integers&quot;&gt;prefixes&lt;/a&gt; to identify the number system. All integer prefixes are in the form &lt;code&gt;0?&lt;/code&gt;, in which you replace &lt;code&gt;?&lt;/code&gt; with a character that refers to the number system:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;b:&lt;/strong&gt; binary (base 2)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;o:&lt;/strong&gt; octal (base 8)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;d:&lt;/strong&gt; decimal (base 10)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;x:&lt;/strong&gt; hexadecimal (base 16)&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Technical Detail:&lt;/strong&gt; The prefix is not required in either an integer or string representation when it can be inferred.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;int&lt;/code&gt; assumes the literal integer to be &lt;strong&gt;decimal&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;303&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hexadecimal_with_prefix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x12F&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hexadecimal_no_prefix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;hexadecimal_no_prefix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;
                              &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;
&lt;span class=&quot;gr&quot;&gt;SyntaxError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;invalid syntax&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The string representation of an integer is more flexible because a string holds arbitrary text data:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;303&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hexadecimal_with_prefix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;0x12F&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hexadecimal_no_prefix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;12F&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Each of these strings represent the same integer.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now that you have some foundational knowledge about how to represent integers using &lt;code&gt;str&lt;/code&gt; and &lt;code&gt;int&lt;/code&gt;, you&amp;rsquo;ll learn how to convert a Python string to an &lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;converting-a-python-string-to-an-int&quot;&gt;Converting a Python String to an &lt;code&gt;int&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;If you have a decimal integer represented as a string and you want to convert the Python string to an &lt;code&gt;int&lt;/code&gt;, then you just pass the string to &lt;code&gt;int()&lt;/code&gt;, which returns a decimal integer:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;10&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;10&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;class &amp;#39;int&amp;#39;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By default, &lt;code&gt;int()&lt;/code&gt; assumes that the string argument represents a decimal integer. If, however, you pass a hexadecimal string to &lt;code&gt;int()&lt;/code&gt;, then you&amp;rsquo;ll see a &lt;code&gt;ValueError&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;0x12F&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
&lt;span class=&quot;gr&quot;&gt;ValueError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;invalid literal for int() with base 10: &amp;#39;0x12F&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The error message says that the string is not a valid decimal integer.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s important to recognize the difference between two types of failed results of passing a string to &lt;code&gt;int()&lt;/code&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Syntax Error:&lt;/strong&gt; A &lt;code&gt;ValueError&lt;/code&gt; will occur when &lt;code&gt;int()&lt;/code&gt; doesn&amp;rsquo;t know how to parse the string using the provided base (10 by default).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Logical Error:&lt;/strong&gt; &lt;code&gt;int()&lt;/code&gt; does know how to parse the string, but not the way you expected.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here&amp;rsquo;s an example of a logical error:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;binary&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;11010010&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;binary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Using the default base of 10, instead of 2&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;11010010&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, you meant for the result to be &lt;em&gt;210&lt;/em&gt;, which is the decimal representation of the binary string. Unfortunately, because you didn&amp;rsquo;t specify that behavior, &lt;code&gt;int()&lt;/code&gt; assumed that the string was a decimal integer.&lt;/p&gt;
&lt;p&gt;One good safeguard for this behavior is to always define your string representations using explicit bases:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;0b11010010&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
&lt;span class=&quot;gr&quot;&gt;ValueError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;invalid literal for int() with base 10: &amp;#39;0b11010010&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, you get a &lt;code&gt;ValueError&lt;/code&gt; because &lt;code&gt;int()&lt;/code&gt; doesn&amp;rsquo;t know how to parse the binary string as a decimal integer.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;When you pass a string to &lt;code&gt;int()&lt;/code&gt;, you can specify the number system that you&amp;rsquo;re using to represent the integer. The way to specify the number system is to use &lt;code&gt;base&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;0x12F&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;303&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, &lt;code&gt;int()&lt;/code&gt; understands you are passing a hexadecimal string and expecting a decimal integer.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Technical Detail:&lt;/strong&gt; The argument that you pass to &lt;code&gt;base&lt;/code&gt; is not limited to 2, 8, 10, and 16:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;10&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;p&gt;Great! Now that you&amp;rsquo;re comfortable with the ins and outs of converting a Python string to an &lt;code&gt;int&lt;/code&gt;, you&amp;rsquo;ll learn how to do the inverse operation.&lt;/p&gt;
&lt;h2 id=&quot;converting-a-python-int-to-a-string&quot;&gt;Converting a Python &lt;code&gt;int&lt;/code&gt; to a String&lt;/h2&gt;
&lt;p&gt;In Python, you can convert a Python &lt;code&gt;int&lt;/code&gt; to a string using &lt;code&gt;str()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;10&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;class &amp;#39;str&amp;#39;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By default, &lt;code&gt;str()&lt;/code&gt; behaves like &lt;code&gt;int()&lt;/code&gt; in that it results in a decimal representation:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mb&quot;&gt;0b11010010&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;210&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, &lt;code&gt;str()&lt;/code&gt; is smart enough to interpret the binary literal and convert it to a decimal string.&lt;/p&gt;
&lt;p&gt;If you want a string to represent an integer in another number system, then you use a formatted string, such as an &lt;a href=&quot;https://realpython.com/python-f-strings/&quot;&gt;f-string&lt;/a&gt; (in Python 3.6+), and an &lt;a href=&quot;https://docs.python.org/library/string.html#format-specification-mini-language&quot;&gt;option&lt;/a&gt; that specifies the base:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;octal&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mo&quot;&gt;0o1073&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{octal}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Decimal&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;571&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{octal:x}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Hexadecimal&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;23b&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{octal:b}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Binary&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;1000111011&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;str&lt;/code&gt; is a flexible way to represent an integer in a variety of different number systems.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Congratulations! You&amp;rsquo;ve learned so much about integers and how to represent and convert them between Python string and &lt;code&gt;int&lt;/code&gt; data types.&lt;/p&gt;
&lt;p&gt;In this tutorial, you learned:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;How to use &lt;code&gt;str&lt;/code&gt; and &lt;code&gt;int&lt;/code&gt; to store integers&lt;/li&gt;
&lt;li&gt;How to specify an explicit number system for an integer representation&lt;/li&gt;
&lt;li&gt;How to convert a Python string to an &lt;code&gt;int&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;How to convert a Python &lt;code&gt;int&lt;/code&gt; to a string&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now that you know so much about &lt;code&gt;str&lt;/code&gt; and &lt;code&gt;int&lt;/code&gt;, you can learn more about representing numerical types using &lt;a href=&quot;https://docs.python.org/3/library/functions.html#float&quot;&gt;&lt;code&gt;float()&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://docs.python.org/3/library/functions.html#hex&quot;&gt;&lt;code&gt;hex()&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://docs.python.org/3/library/functions.html#oct&quot;&gt;&lt;code&gt;oct()&lt;/code&gt;&lt;/a&gt;, and &lt;a href=&quot;https://docs.python.org/3/library/functions.html#bin&quot;&gt;&lt;code&gt;bin()&lt;/code&gt;&lt;/a&gt;!&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Python Debugging With pdb</title>
      <id>https://realpython.com/courses/python-debugging-pdb/</id>
      <link href="https://realpython.com/courses/python-debugging-pdb/"/>
      <updated>2019-09-17T14:00:00+00:00</updated>
      <summary>In this hands-on course, you&#39;ll learn the basics of using pdb, Python&#39;s interactive source code debugger. pdb is a great tool for tracking down hard-to-find bugs, and it allows you to fix faulty code more quickly.</summary>
      <content type="html">
        &lt;p&gt;Nowadays, we often take for granted the excellent debuggers built into &lt;a href=&quot;https://realpython.com/python-ides-code-editors-guide/&quot;&gt;our favorite IDEs&lt;/a&gt;. But how do you debug your Python code when you don&amp;rsquo;t have the luxury of using an IDE?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;pdb&lt;/code&gt;&lt;/strong&gt;, short for &lt;strong&gt;Python DeBugger&lt;/strong&gt;, is a module for interactive source code debugging. It&amp;rsquo;s built into the Python Standard Library, so it&amp;rsquo;s always accessible to you. Because it runs in the command line, it&amp;rsquo;s especially helpful when you&amp;rsquo;re developing on remote systems.&lt;/p&gt;
&lt;p&gt;In this course, you&amp;rsquo;ll learn how to perform the most common debugging tasks using &lt;code&gt;pdb&lt;/code&gt;, including setting &lt;strong&gt;breakpoints&lt;/strong&gt;, &lt;strong&gt;stepping&lt;/strong&gt; through code, viewing &lt;strong&gt;stack traces&lt;/strong&gt;, creating &lt;strong&gt;watch lists&lt;/strong&gt;, and more.&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;#&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-pdb-command-reference&quot; data-focus=&quot;false&quot;&gt;Click here to get a printable &quot;pdb Command Reference&quot; (PDF)&lt;/a&gt; that you can keep on your desk and refer to while debugging.&lt;/p&gt;&lt;/div&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>PyGame: A Primer on Game Programming in Python</title>
      <id>https://realpython.com/pygame-a-primer/</id>
      <link href="https://realpython.com/pygame-a-primer/"/>
      <updated>2019-09-16T15:27:36+00:00</updated>
      <summary>In this step-by-step tutorial, you&#39;ll learn how to use PyGame. This library allows you to create games and rich multimedia programs in Python. You&#39;ll learn how to draw items on your screen, implement collision detection, handle user input, and much more!</summary>
      <content type="html">
        &lt;p&gt;When I started learning computer programming late in the last millennium, it was driven by my desire to write computer games. I tried to figure out how to write games in every language and on every platform I learned, including Python. That&amp;rsquo;s how I discovered &lt;a href=&quot;https://www.pygame.org/&quot;&gt;&lt;code&gt;pygame&lt;/code&gt;&lt;/a&gt; and learned how to use it to write games and other graphical programs. At the time, I really wanted a primer on &lt;code&gt;pygame&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;By the end of this article, you&amp;rsquo;ll be able to:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Draw items on your screen&lt;/li&gt;
&lt;li&gt;Play sound effects and music&lt;/li&gt;
&lt;li&gt;Handle user input&lt;/li&gt;
&lt;li&gt;Implement event loops&lt;/li&gt;
&lt;li&gt;Describe how game programming differs from standard procedural Python programming&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This primer assumes you have a &lt;a href=&quot;https://realpython.com/learning-paths/python3-introduction/&quot;&gt;basic understanding of writing Python programs&lt;/a&gt;, including user-defined functions, &lt;a href=&quot;https://realpython.com/courses/python-imports-101/&quot;&gt;imports&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/courses/mastering-while-loops/&quot;&gt;loops&lt;/a&gt;, and &lt;a href=&quot;https://realpython.com/courses/python-conditional-statements/&quot;&gt;conditionals&lt;/a&gt;. You should also be familiar with &lt;a href=&quot;https://realpython.com/read-write-files-python/&quot;&gt;how to open files&lt;/a&gt; on your platform. A &lt;a href=&quot;https://realpython.com/learning-paths/object-oriented-programming-oop-python/&quot;&gt;basic understanding of object-oriented Python&lt;/a&gt; is helpful as well. &lt;code&gt;pygame&lt;/code&gt; works with most versions of Python, but Python 3.6 is recommended and used throughout this article.&lt;/p&gt;
&lt;p&gt;You can get all of the code in this article to follow along:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Clone Repo:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/pygame/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-pygame&quot; data-focus=&quot;false&quot;&gt;Click here to clone the repo you&#39;ll use&lt;/a&gt; to learn how to use PyGame in this tutorial.&lt;/p&gt;&lt;/div&gt;

&lt;h2 id=&quot;background-and-setup&quot;&gt;Background and Setup&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;pygame&lt;/code&gt; is a Python wrapper for the &lt;a href=&quot;https://www.libsdl.org/&quot;&gt;SDL library&lt;/a&gt;, which stands for &lt;strong&gt;Simple DirectMedia Layer&lt;/strong&gt;. SDL provides cross-platform access to your system&amp;rsquo;s underlying multimedia hardware components, such as sound, video, mouse, keyboard, and joystick. &lt;code&gt;pygame&lt;/code&gt; started life as a replacement for the stalled &lt;a href=&quot;http://www.pygame.org/docs/tut/PygameIntro.html&quot;&gt;PySDL project&lt;/a&gt;. The cross-platform nature of both SDL and &lt;code&gt;pygame&lt;/code&gt; means you can write games and rich multimedia Python programs for every platform that supports them!&lt;/p&gt;
&lt;p&gt;To install &lt;code&gt;pygame&lt;/code&gt; on your platform, use the appropriate &lt;a href=&quot;https://realpython.com/what-is-pip/&quot;&gt;&lt;code&gt;pip&lt;/code&gt;&lt;/a&gt; command:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip install pygame
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can verify the install by loading one of the examples that comes with the library:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python3 -m pygame.examples.aliens
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If a game window appears, then &lt;code&gt;pygame&lt;/code&gt; is installed properly! If you run into problems, then the &lt;a href=&quot;https://www.pygame.org/wiki/GettingStarted&quot;&gt;Getting Started guide&lt;/a&gt; outlines some known issues and caveats for all platforms.&lt;/p&gt;
&lt;h2 id=&quot;basic-pygame-program&quot;&gt;Basic PyGame Program&lt;/h2&gt;
&lt;p&gt;Before getting down to specifics, let&amp;rsquo;s take a look at a basic &lt;code&gt;pygame&lt;/code&gt; program. This program creates a window, fills the background with white, and draws a blue circle in the middle of it:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Simple pygame program&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Import and initialize the pygame library&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pygame&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Set up the drawing window&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Run until the user asks to quit&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Did the user click the window close button?&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Fill the background with white&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Draw a solid blue circle in the center&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;circle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;75&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Flip the display&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Done! Time to quit.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When you run this program, you&amp;rsquo;ll see a window that looks like this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pygame-simple.5169327a10a3.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-50&quot; src=&quot;https://files.realpython.com/media/pygame-simple.5169327a10a3.png&quot; width=&quot;588&quot; height=&quot;637&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-simple.5169327a10a3.png&amp;amp;w=147&amp;amp;sig=52e18c7856b57dbf5631b901d20b22bbbd118faa 147w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-simple.5169327a10a3.png&amp;amp;w=294&amp;amp;sig=a1d301c602763d7b72f94fe2ea3bc9516e879f3f 294w, https://files.realpython.com/media/pygame-simple.5169327a10a3.png 588w&quot; sizes=&quot;75vw&quot; alt=&quot;A simple pygame program&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s break this code down, section by section:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lines 4 and 5&lt;/strong&gt; import and initialize the &lt;code&gt;pygame&lt;/code&gt; library. Without these lines, there is no &lt;code&gt;pygame&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Line 8&lt;/strong&gt; sets up your program&amp;rsquo;s display window. You provide either a list or a tuple that specifies the width and height of the window to create. This program uses a list to create a square window with 500 pixels on each side.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lines 11 and 12&lt;/strong&gt; set up a &lt;strong&gt;game loop&lt;/strong&gt; to control when the program ends. You&amp;rsquo;ll cover game loops later on in this tutorial.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lines 15 to 17&lt;/strong&gt; scan and handle &lt;strong&gt;events&lt;/strong&gt; within the game loop. You&amp;rsquo;ll get to events a bit later as well. In this case, the only event handled is  &lt;code&gt;pygame.QUIT&lt;/code&gt;, which occurs when the user clicks the window close button.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Line 20&lt;/strong&gt; fills the window with a solid color. &lt;code&gt;screen.fill()&lt;/code&gt; accepts either a list or tuple specifying the RGB values for the color. Since &lt;code&gt;(255, 255, 255)&lt;/code&gt; was provided, the window is filled with white.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Line 23&lt;/strong&gt; draws a circle in the window, using the following parameters:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;screen&lt;/code&gt;:&lt;/strong&gt; the window on which to draw&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;(0, 0, 255)&lt;/code&gt;:&lt;/strong&gt; a tuple containing RGB color values&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;(250, 250)&lt;/code&gt;:&lt;/strong&gt; a tuple specifying the center coordinates of the circle&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;75&lt;/code&gt;:&lt;/strong&gt; the radius of the circle to draw in pixels&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Line 26&lt;/strong&gt; updates the contents of the display to the screen. Without this call, nothing appears in the window!&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Line 29&lt;/strong&gt; exits &lt;code&gt;pygame&lt;/code&gt;. This only happens once the loop finishes.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That&amp;rsquo;s the &lt;code&gt;pygame&lt;/code&gt; version of &amp;ldquo;Hello, World.&amp;rdquo; Now let&amp;rsquo;s dig a little deeper into the concepts behind this code.&lt;/p&gt;
&lt;h2 id=&quot;pygame-concepts&quot;&gt;PyGame Concepts&lt;/h2&gt;
&lt;p&gt;As &lt;code&gt;pygame&lt;/code&gt; and the SDL library are portable across different platforms and devices, they both need to define and work with abstractions for various hardware realities. Understanding those concepts and abstractions will help you design and develop your own games.&lt;/p&gt;
&lt;h3 id=&quot;initialization-and-modules&quot;&gt;Initialization and Modules&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;pygame&lt;/code&gt; library is &lt;a href=&quot;https://www.pygame.org/docs/ref/pygame.html&quot;&gt;composed of a number of Python constructs&lt;/a&gt;, which include several different &lt;strong&gt;modules&lt;/strong&gt;. These modules provide abstract access to specific hardware on your system, as well as uniform methods to work with that hardware. For example, &lt;code&gt;display&lt;/code&gt; allows uniform access to your video display, while &lt;code&gt;joystick&lt;/code&gt; allows abstract control of your joystick.&lt;/p&gt;
&lt;p&gt;After importing the &lt;code&gt;pygame&lt;/code&gt; library in the example above, the first thing you did was &lt;a href=&quot;https://www.pygame.org/docs/tut/ImportInit.html&quot;&gt;initialize PyGame&lt;/a&gt; using &lt;code&gt;pygame.init()&lt;/code&gt;. This function &lt;a href=&quot;https://www.pygame.org/docs/ref/pygame.html#pygame.init&quot;&gt;calls the separate &lt;code&gt;init()&lt;/code&gt; functions&lt;/a&gt; of all the included &lt;code&gt;pygame&lt;/code&gt; modules. Since these modules are abstractions for specific hardware, this initialization step is required so that you can work with the same code on Linux, Windows, and Mac.&lt;/p&gt;
&lt;h3 id=&quot;displays-and-surfaces&quot;&gt;Displays and Surfaces&lt;/h3&gt;
&lt;p&gt;In addition to the modules, &lt;code&gt;pygame&lt;/code&gt; also includes several Python &lt;strong&gt;classes&lt;/strong&gt;, which encapsulate non-hardware dependent concepts. One of these is the &lt;a href=&quot;https://www.pygame.org/docs/ref/surface.html&quot;&gt;&lt;code&gt;Surface&lt;/code&gt;&lt;/a&gt; which, at its most basic, defines a rectangular area on which you can draw. &lt;code&gt;Surface&lt;/code&gt; objects are used in many contexts in &lt;code&gt;pygame&lt;/code&gt;. Later you&amp;rsquo;ll see how to load an image into a &lt;code&gt;Surface&lt;/code&gt; and display it on the screen.&lt;/p&gt;
&lt;p&gt;In &lt;code&gt;pygame&lt;/code&gt;, everything is viewed on a single user-created &lt;a href=&quot;https://www.pygame.org/docs/ref/display.html&quot;&gt;&lt;code&gt;display&lt;/code&gt;&lt;/a&gt;, which can be a window or a full screen. The display is created using &lt;a href=&quot;https://www.pygame.org/docs/ref/display.html#pygame.display.set_mode&quot;&gt;&lt;code&gt;.set_mode()&lt;/code&gt;&lt;/a&gt;, which returns a &lt;code&gt;Surface&lt;/code&gt; representing the visible part of the window. It is this &lt;code&gt;Surface&lt;/code&gt; that you pass into drawing functions like &lt;a href=&quot;https://www.pygame.org/docs/ref/draw.html#pygame.draw.circle&quot;&gt;&lt;code&gt;pygame.draw.circle()&lt;/code&gt;&lt;/a&gt;, and the contents of that &lt;code&gt;Surface&lt;/code&gt; are pushed to the display when you call &lt;a href=&quot;https://www.pygame.org/docs/ref/display.html#pygame.display.flip&quot;&gt;&lt;code&gt;pygame.display.flip()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;images-and-rects&quot;&gt;Images and Rects&lt;/h3&gt;
&lt;p&gt;Your basic &lt;code&gt;pygame&lt;/code&gt; program drew a shape directly onto the display&amp;rsquo;s &lt;code&gt;Surface&lt;/code&gt;, but you can also work with images on the disk. The &lt;a href=&quot;https://www.pygame.org/docs/ref/image.html&quot;&gt;&lt;code&gt;image&lt;/code&gt; module&lt;/a&gt; allows you to &lt;a href=&quot;https://www.pygame.org/docs/ref/image.html#pygame.image.load&quot;&gt;load&lt;/a&gt; and &lt;a href=&quot;https://www.pygame.org/docs/ref/image.html#pygame.image.save&quot;&gt;save&lt;/a&gt; images in a variety of popular formats. Images are loaded into &lt;code&gt;Surface&lt;/code&gt; objects, which can then be manipulated and displayed in numerous ways.&lt;/p&gt;
&lt;p&gt;As mentioned above, &lt;code&gt;Surface&lt;/code&gt; objects are represented by rectangles, as are many other objects in &lt;code&gt;pygame&lt;/code&gt;, such as images and windows. Rectangles are so heavily used that there is a &lt;a href=&quot;https://www.pygame.org/docs/ref/rect.html&quot;&gt;special &lt;code&gt;Rect&lt;/code&gt; class&lt;/a&gt; just to handle them. You&amp;rsquo;ll be using &lt;code&gt;Rect&lt;/code&gt; objects and images in your game to draw players and enemies, and to manage collisions between them.&lt;/p&gt;
&lt;p&gt;Okay, that&amp;rsquo;s enough theory. Let&amp;rsquo;s design and write a game!&lt;/p&gt;
&lt;h2 id=&quot;basic-game-design&quot;&gt;Basic Game Design&lt;/h2&gt;
&lt;p&gt;Before you start writing any code, it&amp;rsquo;s always a good idea to have some design in place. Since this is a tutorial game, let&amp;rsquo;s design some basic gameplay for it as well:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The goal of the game is to avoid incoming obstacles:&lt;ul&gt;
&lt;li&gt;The player starts on the left side of the screen.&lt;/li&gt;
&lt;li&gt;The obstacles enter randomly from the right and move left in a straight line.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The player can move left, right, up, or down to avoid the obstacles.&lt;/li&gt;
&lt;li&gt;The player cannot move off the screen.&lt;/li&gt;
&lt;li&gt;The game ends either when the player is hit by an obstacle or when the user closes the window.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When he was describing software projects, a &lt;a href=&quot;https://devblogs.microsoft.com/oldnewthing/?p=27543&quot;&gt;former colleague of mine&lt;/a&gt; used to say, &amp;ldquo;You don&amp;rsquo;t know what you do until you know what you don&amp;rsquo;t do.&amp;rdquo; With that in mind, here are some things that won&amp;rsquo;t be covered in this tutorial:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;No multiple lives&lt;/li&gt;
&lt;li&gt;No scorekeeping&lt;/li&gt;
&lt;li&gt;No player attack capabilities&lt;/li&gt;
&lt;li&gt;No advancing levels&lt;/li&gt;
&lt;li&gt;No boss characters&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You&amp;rsquo;re free to try your hand at adding these and other features to your own program.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s get started!&lt;/p&gt;
&lt;h3 id=&quot;importing-and-initializing-pygame&quot;&gt;Importing and Initializing PyGame&lt;/h3&gt;
&lt;p&gt;After you import &lt;code&gt;pygame&lt;/code&gt;, you&amp;rsquo;ll also need to initialize it. This allows &lt;code&gt;pygame&lt;/code&gt; to connect its abstractions to your specific hardware:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Import the pygame module&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pygame&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Import pygame.locals for easier access to key coordinates&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Updated to conform to flake8 and black standards&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pygame.locals&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_DOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_LEFT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_RIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_ESCAPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;KEYDOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Initialize pygame&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;pygame&lt;/code&gt; library defines many things besides modules and classes. It also defines some &lt;a href=&quot;https://www.pygame.org/docs/ref/locals.html&quot;&gt;local constants&lt;/a&gt; for things like keystrokes, mouse movements, and display attributes. You reference these constants using the syntax &lt;code&gt;pygame.&amp;lt;CONSTANT&amp;gt;&lt;/code&gt;. By importing specific constants from &lt;code&gt;pygame.locals&lt;/code&gt;, you can use the syntax &lt;code&gt;&amp;lt;CONSTANT&amp;gt;&lt;/code&gt; instead. This will save you some keystrokes and improve overall readability.&lt;/p&gt;
&lt;h3 id=&quot;setting-up-the-display&quot;&gt;Setting Up the Display&lt;/h3&gt;
&lt;p&gt;Now you need something to draw on! Create a &lt;a href=&quot;https://www.pygame.org/docs/ref/display.html&quot;&gt;screen&lt;/a&gt; to be the overall canvas:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Import the pygame module&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pygame&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Import pygame.locals for easier access to key coordinates&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Updated to conform to flake8 and black standards&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pygame.locals&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_DOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_LEFT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_RIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_ESCAPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;KEYDOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Initialize pygame&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# Define constants for the screen width and height&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;800&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_HEIGHT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;600&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# Create the screen object&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_HEIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You create the screen to use by calling &lt;code&gt;pygame.display.set_mode()&lt;/code&gt; and passing a tuple or list with the desired width and height. In this case, the window is 800x600, as defined by the constants &lt;code&gt;SCREEN_WIDTH&lt;/code&gt; and &lt;code&gt;SCREEN_HEIGHT&lt;/code&gt; on lines 20 and 21. This returns a &lt;code&gt;Surface&lt;/code&gt; which represents the inside dimensions of the window. This is the portion of the window you can control, while the OS controls the window borders and title bar.&lt;/p&gt;
&lt;p&gt;If you run this program now, then you&amp;rsquo;ll see a window pop up briefly and then immediately disappear as the program exits. Don&amp;rsquo;t blink or you might miss it! In the next section, you&amp;rsquo;ll focus on the main game loop to ensure that your program exits only when given the correct input.&lt;/p&gt;
&lt;h3 id=&quot;setting-up-the-game-loop&quot;&gt;Setting Up the Game Loop&lt;/h3&gt;
&lt;p&gt;Every game from Pong to Fortnite uses a &lt;a href=&quot;http://www.informit.com/articles/article.aspx?p=2167437&amp;amp;seqNum=2&quot;&gt;game loop&lt;/a&gt; to control gameplay. The game loop does four very important things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Processes user input&lt;/li&gt;
&lt;li&gt;Updates the state of all game objects&lt;/li&gt;
&lt;li&gt;Updates the display and audio output&lt;/li&gt;
&lt;li&gt;Maintains the speed of the game&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Every cycle of the game loop is called a &lt;strong&gt;frame&lt;/strong&gt;, and the quicker you can do things each cycle, the faster your game will run. Frames continue to occur until some condition to exit the game is met. In your design, there are two conditions that can end the game loop:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The player collides with an obstacle. (You&amp;rsquo;ll cover collision detection later.)&lt;/li&gt;
&lt;li&gt;The player closes the window.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The first thing the game loop does is process user input to allow the player to move around the screen. Therefore, you need some way to capture and process a variety of input. You do this using the &lt;code&gt;pygame&lt;/code&gt; event system.&lt;/p&gt;
&lt;h3 id=&quot;processing-events&quot;&gt;Processing Events&lt;/h3&gt;
&lt;p&gt;Key presses, mouse movements, and even joystick movements are some of the ways in which a user can provide input. All user input results in an &lt;a href=&quot;https://www.pygame.org/docs/ref/event.html&quot;&gt;event&lt;/a&gt; being generated. Events can happen at any time and often (but not always) originate outside the program. All events in &lt;code&gt;pygame&lt;/code&gt; are placed in the event queue, which can then be accessed and manipulated. Dealing with events is referred to as &lt;strong&gt;handling&lt;/strong&gt; them, and the code to do so is called an &lt;strong&gt;event handler&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Every event in &lt;code&gt;pygame&lt;/code&gt; has an event &lt;strong&gt;type&lt;/strong&gt; associated with it. For your game, the event types you&amp;rsquo;ll focus on are keypresses and window closure. Keypress events have the event type &lt;code&gt;KEYDOWN&lt;/code&gt;, and the window closure event has the type &lt;code&gt;QUIT&lt;/code&gt;. Different event types may also have other data associated with them. For example, the &lt;code&gt;KEYDOWN&lt;/code&gt; event type also has a variable called &lt;code&gt;key&lt;/code&gt; to indicate which key was pressed.&lt;/p&gt;
&lt;p&gt;You access the list of all active events in the queue by calling &lt;code&gt;pygame.event.get()&lt;/code&gt;. You then loop through this list, inspect each event type, and respond accordingly:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Variable to keep the main loop running&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Main loop&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Look at every event in the queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;34 &lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Did the user hit a key?&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;35 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;KEYDOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;36 &lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;# Was it the Escape key? If so, stop the loop.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;37 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K_ESCAPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;38 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;39 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;40 &lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Did the user click the window close button? If so, stop the loop.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;41 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;42 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let&amp;rsquo;s take a closer look at this game loop:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Line 28&lt;/strong&gt; sets up a control variable for the game loop. To exit the loop and the game, you set &lt;code&gt;running = False&lt;/code&gt;. The game loop starts on line 29.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Line 31&lt;/strong&gt; starts the event handler, walking through every event currently in the event queue. If there are no events, then the list is empty, and the handler won&amp;rsquo;t do anything.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lines 35 to 38&lt;/strong&gt; check if the current &lt;code&gt;event.type&lt;/code&gt; is a &lt;code&gt;KEYDOWN&lt;/code&gt; event. If it is, then the program checks which key was pressed by looking at the &lt;code&gt;event.key&lt;/code&gt; attribute. If the key is the &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-escape&quot;&gt;Esc&lt;/kbd&gt;&lt;/span&gt; key, indicated by &lt;code&gt;K_ESCAPE&lt;/code&gt;, then it exits the game loop by setting &lt;code&gt;running = False&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lines 41 and 42&lt;/strong&gt; do a similar check for the event type called &lt;code&gt;QUIT&lt;/code&gt;. This event only occurs when the user clicks the window close button. The user may also use any other operating system action to close the window.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When you add these lines to the previous code and run it, you&amp;rsquo;ll see a window with a blank or black screen:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pygame-empty.a8c1bc646b3b.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-66&quot; src=&quot;https://files.realpython.com/media/pygame-empty.a8c1bc646b3b.png&quot; width=&quot;888&quot; height=&quot;737&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-empty.a8c1bc646b3b.png&amp;amp;w=222&amp;amp;sig=add7b05115b3744966ca4f12fc8d173c0c30ff92 222w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-empty.a8c1bc646b3b.png&amp;amp;w=444&amp;amp;sig=a38c79917e6943ebe359e52b5718d9817df390b7 444w, https://files.realpython.com/media/pygame-empty.a8c1bc646b3b.png 888w&quot; sizes=&quot;75vw&quot; alt=&quot;An empty, but persistent, pygame window&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The window won&amp;rsquo;t disappear until you press the &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-escape&quot;&gt;Esc&lt;/kbd&gt;&lt;/span&gt; key, or otherwise trigger a &lt;code&gt;QUIT&lt;/code&gt; event by closing the window.&lt;/p&gt;
&lt;h3 id=&quot;drawing-on-the-screen&quot;&gt;Drawing on the Screen&lt;/h3&gt;
&lt;p&gt;In the sample program, you drew on the screen using two commands:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;screen.fill()&lt;/code&gt;&lt;/strong&gt; to fill the background&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;pygame.draw.circle()&lt;/code&gt;&lt;/strong&gt; to draw a circle&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now you&amp;rsquo;ll learn about a third way to draw to the screen: using a &lt;code&gt;Surface&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Recall that a &lt;a href=&quot;https://www.pygame.org/docs/ref/surface.html&quot;&gt;&lt;code&gt;Surface&lt;/code&gt;&lt;/a&gt; is a rectangular object on which you can draw, like a blank sheet of paper. The &lt;code&gt;screen&lt;/code&gt; object is a &lt;code&gt;Surface&lt;/code&gt;, and you can create your own &lt;code&gt;Surface&lt;/code&gt; objects separate from the display screen. Let&amp;rsquo;s see how that works:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;44 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Fill the screen with white&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;45 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;46 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;47 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Create a surface and pass in a tuple containing its length and width&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;48 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Surface&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;49 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;50 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Give the surface a color to separate it from the background&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;51 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;52 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_rect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After the screen is filled with white on line 45, a new &lt;code&gt;Surface&lt;/code&gt; is created on line 48. This &lt;code&gt;Surface&lt;/code&gt; is 50 pixels wide, 50 pixels tall, and assigned to &lt;code&gt;surf&lt;/code&gt;. At this point, you treat it just like the &lt;code&gt;screen&lt;/code&gt;. So on line, 51 you fill it with black. You can also access its underlying &lt;code&gt;Rect&lt;/code&gt; using &lt;code&gt;.get_rect()&lt;/code&gt;. This is stored as &lt;code&gt;rect&lt;/code&gt; for later use.&lt;/p&gt;
&lt;h3 id=&quot;using-blit-and-flip&quot;&gt;Using &lt;code&gt;.blit()&lt;/code&gt; and &lt;code&gt;.flip()&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Just creating a new &lt;code&gt;Surface&lt;/code&gt; isn&amp;rsquo;t enough to see it on the screen. To do that, you need to &lt;a href=&quot;https://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit&quot;&gt;blit&lt;/a&gt; the &lt;code&gt;Surface&lt;/code&gt; onto another &lt;code&gt;Surface&lt;/code&gt;. The term &lt;code&gt;blit&lt;/code&gt; stands for &lt;strong&gt;Block Transfer&lt;/strong&gt;, and &lt;code&gt;.blit()&lt;/code&gt; is how you copy the contents of one &lt;code&gt;Surface&lt;/code&gt; to another. You can only &lt;code&gt;.blit()&lt;/code&gt; from one &lt;code&gt;Surface&lt;/code&gt; to another, but since the screen is just another &lt;code&gt;Surface&lt;/code&gt;, that&amp;rsquo;s not a problem. Here&amp;rsquo;s how you draw &lt;code&gt;surf&lt;/code&gt; on the screen:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;54 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# This line says &amp;quot;Draw surf onto the screen at the center&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;55 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_HEIGHT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;56 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;.blit()&lt;/code&gt; call on line 55 takes two arguments:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;Surface&lt;/code&gt; to draw&lt;/li&gt;
&lt;li&gt;The location at which to draw it on the source &lt;code&gt;Surface&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The coordinates &lt;code&gt;(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)&lt;/code&gt; tell your program to place &lt;code&gt;surf&lt;/code&gt; in the exact center of the screen, but it doesn&amp;rsquo;t quite look that way:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pygame-blit.63a5d39f94e1.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-66&quot; src=&quot;https://files.realpython.com/media/pygame-blit.63a5d39f94e1.png&quot; width=&quot;888&quot; height=&quot;737&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-blit.63a5d39f94e1.png&amp;amp;w=222&amp;amp;sig=1261fc5207c890e1992ff131b1568b50d9be60d3 222w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-blit.63a5d39f94e1.png&amp;amp;w=444&amp;amp;sig=2a3571a6dc058134f39213aacbb3dbcbf10ffc51 444w, https://files.realpython.com/media/pygame-blit.63a5d39f94e1.png 888w&quot; sizes=&quot;75vw&quot; alt=&quot;Blitting a surface onto the screen&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The reason why the image looks off-center is that &lt;code&gt;.blit()&lt;/code&gt; puts the &lt;strong&gt;top-left corner&lt;/strong&gt; of &lt;code&gt;surf&lt;/code&gt; at the location given. If you want &lt;code&gt;surf&lt;/code&gt; to be centered, then you&amp;rsquo;ll have to do some math to shift it up and to the left. You can do this by subtracting the width and height of &lt;code&gt;surf&lt;/code&gt; from the width and height of the screen, dividing each by 2 to locate the center, and then passing those numbers as arguments to &lt;code&gt;screen.blit()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;54 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Put the center of surf at the center of the display&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;55 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf_center&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;56 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;57 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_HEIGHT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;58 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;59 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;60 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Draw surf at the new coordinates&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;61 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;surf_center&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;62 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Notice the call to &lt;a href=&quot;https://www.pygame.org/docs/ref/display.html#pygame.display.flip&quot;&gt;&lt;code&gt;pygame.display.flip()&lt;/code&gt;&lt;/a&gt; after the call to &lt;code&gt;blit()&lt;/code&gt;. This updates the entire screen with everything that&amp;rsquo;s been drawn since the last flip. Without the call to &lt;code&gt;.flip()&lt;/code&gt;, nothing is shown.&lt;/p&gt;
&lt;h2 id=&quot;sprites&quot;&gt;Sprites&lt;/h2&gt;
&lt;p&gt;In your game design, the player starts on the left, and obstacles come in from the right. You can represent all the obstacles with &lt;code&gt;Surface&lt;/code&gt; objects to make drawing everything easier, but how do you know where to draw them? How do you know if an obstacle has collided with the player? What happens when the obstacle flies off the screen? What if you want to draw background images that also move? What if you want your images to be animated? You can handle all these situations and more with &lt;a href=&quot;https://en.wikipedia.org/wiki/Sprite_%28computer_graphics%29&quot;&gt;sprites&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In programming terms, a &lt;strong&gt;sprite&lt;/strong&gt; is a 2D representation of something on the screen. Essentially, it&amp;rsquo;s a picture. &lt;code&gt;pygame&lt;/code&gt; provides a &lt;a href=&quot;https://www.pygame.org/docs/ref/sprite.html&quot;&gt;&lt;code&gt;Sprite&lt;/code&gt; class&lt;/a&gt;, which is designed to hold one or several graphical representations of any game object that you want to display on the screen. To use it, you create a new class that extends &lt;code&gt;Sprite&lt;/code&gt;. This allows you to use its built-in methods.&lt;/p&gt;
&lt;h3 id=&quot;players&quot;&gt;Players&lt;/h3&gt;
&lt;p&gt;Here&amp;rsquo;s how you use &lt;code&gt;Sprite&lt;/code&gt; objects with the current game to define the player. Insert this code after line 18:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Define a Player object by extending pygame.sprite.Sprite&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# The surface drawn on the screen is now an attribute of &amp;#39;player&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sprite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sprite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Surface&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;75&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_rect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You first define &lt;code&gt;Player&lt;/code&gt; by extending &lt;code&gt;pygame.sprite.Sprite&lt;/code&gt; on line 22. Then &lt;code&gt;.__init__()&lt;/code&gt; uses &lt;code&gt;.super()&lt;/code&gt; to call the &lt;code&gt;.__init__()&lt;/code&gt; method of &lt;code&gt;Sprite&lt;/code&gt;. For more info on why this is necessary, you can read &lt;a href=&quot;https://realpython.com/python-super/&quot;&gt;Supercharge Your Classes With Python super()&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Next, you define and initialize &lt;code&gt;.surf&lt;/code&gt; to hold the image to display, which is currently a white box. You also define and initialize &lt;code&gt;.rect&lt;/code&gt;, which you&amp;rsquo;ll use to draw the player later. To use this new class, you need to create a new object and change the drawing code as well. Expand the code block below to see it all together:&lt;/p&gt;
&lt;div class=&quot;card mb-3&quot; id=&quot;collapse_cardfd8884&quot;&gt;
&lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapsefd8884&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapsefd8884&quot;&gt;Expand for full code&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapsefd8884&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapsefd8884&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
&lt;div id=&quot;collapsefd8884&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_cardfd8884&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;

&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Import the pygame module&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pygame&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Import pygame.locals for easier access to key coordinates&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Updated to conform to flake8 and black standards&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pygame.locals&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_DOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_LEFT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_RIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_ESCAPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;KEYDOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Define constants for the screen width and height&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;800&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_HEIGHT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;600&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# Define a player object by extending pygame.sprite.Sprite&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# The surface drawn on the screen is now an attribute of &amp;#39;player&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sprite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sprite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Surface&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;75&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_rect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Initialize pygame&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Create the screen object&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;34 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_HEIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;35 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;36 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# Instantiate player. Right now, this is just a rectangle.&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;37 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;player&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;38 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;39 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Variable to keep the main loop running&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;40 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;41 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;42 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Main loop&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;43 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;44 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# for loop through the event queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;45 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;46 &lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Check for KEYDOWN event&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;47 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;KEYDOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;48 &lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;# If the Esc key is pressed, then exit the main loop&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;49 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K_ESCAPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;50 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;51 &lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Check for QUIT event. If QUIT, then set running to false.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;52 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;53 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;54 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;55 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Fill the screen with black&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;56 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;57 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;58 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;# Draw the player on the screen&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;59 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_HEIGHT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;60 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;61 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Update the display&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;62 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;p&gt;Run this code. You&amp;rsquo;ll see a white rectangle at roughly the middle of the screen:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pygame-player-sprite.e8a7c96f65b4.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-66&quot; src=&quot;https://files.realpython.com/media/pygame-player-sprite.e8a7c96f65b4.png&quot; width=&quot;888&quot; height=&quot;737&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-player-sprite.e8a7c96f65b4.png&amp;amp;w=222&amp;amp;sig=dfba97787e38bc32d9d39704ced38ae3ed40ff5f 222w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-player-sprite.e8a7c96f65b4.png&amp;amp;w=444&amp;amp;sig=cbd3ae45f4f66b796586b1cd7dbbfc509a27aba9 444w, https://files.realpython.com/media/pygame-player-sprite.e8a7c96f65b4.png 888w&quot; sizes=&quot;75vw&quot; alt=&quot;Basic player sprite being drawn&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;What do you think would happen if you changed line 59 to &lt;code&gt;screen.blit(player.surf, player.rect)&lt;/code&gt;? Try it and see:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;55 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Fill the screen with black&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;56 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;57 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;58 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Draw the player on the screen&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;59 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;60 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;61 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Update the display&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;62 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When you pass a &lt;code&gt;Rect&lt;/code&gt; to &lt;code&gt;.blit()&lt;/code&gt;, it uses the coordinates of the top left corner to draw the surface. You&amp;rsquo;ll use this later to make your player move!&lt;/p&gt;
&lt;h3 id=&quot;user-input&quot;&gt;User Input&lt;/h3&gt;
&lt;p&gt;So far, you&amp;rsquo;ve learned how to set up &lt;code&gt;pygame&lt;/code&gt; and draw objects on the screen. Now, the real fun starts! You&amp;rsquo;ll make the player controllable using the keyboard.&lt;/p&gt;
&lt;p&gt;Earlier, you saw that &lt;code&gt;pygame.event.get()&lt;/code&gt; returns a list of the events in the event queue, which you scan for &lt;code&gt;KEYDOWN&lt;/code&gt; event types. Well, that&amp;rsquo;s not the only way to read keypresses. &lt;code&gt;pygame&lt;/code&gt; also provides &lt;code&gt;pygame.event.get_pressed()&lt;/code&gt;, which returns a &lt;a href=&quot;https://realpython.com/python-dicts/&quot;&gt;dictionary&lt;/a&gt; containing all the current &lt;code&gt;KEYDOWN&lt;/code&gt; events in the queue.&lt;/p&gt;
&lt;p&gt;Put this in your game loop right after the event handling loop. This returns a dictionary containing the keys pressed at the beginning of every frame:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;54 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Get the set of keys pressed and check for user input&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;55 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_pressed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Next, you write a method in &lt;code&gt;Player&lt;/code&gt; to accepts that dictionary. This will define the behavior of the sprite based off the keys that are pressed. Here&amp;rsquo;s what that might look like:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Move the sprite based on user keypresses&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move_ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K_DOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;34 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move_ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;35 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K_LEFT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;36 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move_ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;37 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K_RIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;38 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move_ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;K_UP&lt;/code&gt;, &lt;code&gt;K_DOWN&lt;/code&gt;, &lt;code&gt;K_LEFT&lt;/code&gt;, and &lt;code&gt;K_RIGHT&lt;/code&gt; correspond to the arrow keys on the keyboard. If the dictionary entry for that key is &lt;code&gt;True&lt;/code&gt;, then that key is down, and you move the player &lt;code&gt;.rect&lt;/code&gt; in the proper direction. Here you use &lt;code&gt;.move_ip()&lt;/code&gt;, which stands for &lt;a href=&quot;https://www.pygame.org/docs/ref/rect.html#pygame.Rect.move_ip&quot;&gt;&lt;strong&gt;move in place&lt;/strong&gt;&lt;/a&gt;, to move the current &lt;code&gt;Rect&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Then you can call &lt;code&gt;.update()&lt;/code&gt; every frame to move the player sprite in response to keypresses. Add this call right after the call to &lt;code&gt;.get_pressed()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;52 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Main loop&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;53 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;54 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# for loop through the event queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;55 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;56 &lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Check for KEYDOWN event&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;57 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;KEYDOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;58 &lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;# If the Esc key is pressed, then exit the main loop&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;59 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K_ESCAPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;60 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;61 &lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Check for QUIT event. If QUIT, then set running to false.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;62 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;63 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;64 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;65 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Get all the keys currently pressed&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;66 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_pressed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;67 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;68 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;# Update the player sprite based on user keypresses&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;69 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;70 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;71 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Fill the screen with black&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;72 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now you can move your player rectangle around the screen with the arrow keys:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pygame-keypress-fast-cropped.1552149aa409.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border w-66&quot; src=&quot;https://files.realpython.com/media/pygame-keypress-fast-cropped.1552149aa409.gif&quot; width=&quot;423&quot; height=&quot;346&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-keypress-fast-cropped.1552149aa409.gif&amp;amp;w=105&amp;amp;sig=d65a068aa099a506358eb9cb5c48cff62b996088 105w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-keypress-fast-cropped.1552149aa409.gif&amp;amp;w=211&amp;amp;sig=9e0dedcda865affcb0a34c073e64eac82d2a9e45 211w, https://files.realpython.com/media/pygame-keypress-fast-cropped.1552149aa409.gif 423w&quot; sizes=&quot;75vw&quot; alt=&quot;Keypresses moving a sprite in pygame&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You may notice two small problems:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The player rectangle can move very fast if a key is held down. You&amp;rsquo;ll work on that later.&lt;/li&gt;
&lt;li&gt;The player rectangle can move off the screen. Let&amp;rsquo;s solve that one now.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To keep the player on the screen, you need to add some logic to detect if the &lt;code&gt;rect&lt;/code&gt; is going to move off screen. To do that, you check whether the &lt;code&gt;rect&lt;/code&gt; coordinates have moved beyond the screen&amp;rsquo;s boundary. If so, then you instruct the program to move it back to the edge:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Move the sprite based on user keypresses&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move_ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K_DOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move_ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K_LEFT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move_ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K_RIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;34 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move_ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;35 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;36 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;# Keep player on the screen&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;37 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;38 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;39 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;40 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;41 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;top&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;42 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;top&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;43 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bottom&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_HEIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;44 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bottom&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_HEIGHT&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, instead of using &lt;code&gt;.move()&lt;/code&gt;, you just change the corresponding coordinates of &lt;code&gt;.top&lt;/code&gt;, &lt;code&gt;.bottom&lt;/code&gt;, &lt;code&gt;.left&lt;/code&gt;, or &lt;code&gt;.right&lt;/code&gt; directly. Test this, and you&amp;rsquo;ll find the player rectangle can no longer move off the screen.&lt;/p&gt;
&lt;p&gt;Now let&amp;rsquo;s add some enemies!&lt;/p&gt;
&lt;h3 id=&quot;enemies&quot;&gt;Enemies&lt;/h3&gt;
&lt;p&gt;What&amp;rsquo;s a game without enemies? You&amp;rsquo;ll use the techniques you&amp;rsquo;ve already learned to create a basic enemy class, then create a lot of them for your player to avoid. First, import the &lt;code&gt;random&lt;/code&gt; library:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Import random for random numbers&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then create a new sprite class called &lt;code&gt;Enemy&lt;/code&gt;, following the same pattern you used for &lt;code&gt;Player&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;55 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Define the enemy object by extending pygame.sprite.Sprite&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;56 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# The surface you draw on the screen is now an attribute of &amp;#39;enemy&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;57 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Enemy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sprite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sprite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;58 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;59 &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Enemy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;60 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Surface&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;61 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;62 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_rect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;63 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;64 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;65 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_HEIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;66 &lt;/span&gt;            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;67 &lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;68 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;69 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;70 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Move the sprite based on speed&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;71 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Remove the sprite when it passes the left edge of the screen&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;72 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;73 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move_ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;74 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;75 &lt;/span&gt;            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There are four notable differences between &lt;code&gt;Enemy&lt;/code&gt; and &lt;code&gt;Player&lt;/code&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;On lines 62 to 67&lt;/strong&gt;, you update &lt;code&gt;rect&lt;/code&gt; to be a random location along the right edge of the screen. The center of the rectangle is just off the screen. It&amp;rsquo;s located at some position between 20 and 100 pixels away from the right edge, and somewhere between the top and bottom edges.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;On line 68&lt;/strong&gt;, you define &lt;code&gt;.speed&lt;/code&gt; as a random number between 5 and 20. This specifies how fast this enemy moves towards the player.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;On lines 73 to 76&lt;/strong&gt;, you define &lt;code&gt;.update()&lt;/code&gt;. It takes no arguments since enemies move automatically. Instead, &lt;code&gt;.update()&lt;/code&gt; moves the enemy toward the left side of the screen at the &lt;code&gt;.speed&lt;/code&gt; defined when it was created.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;On line 74&lt;/strong&gt;, you check whether the enemy has moved off-screen. To make sure the &lt;code&gt;Enemy&lt;/code&gt; is fully off the screen and won&amp;rsquo;t just disappear while it&amp;rsquo;s still visible, you check that the right side of the &lt;code&gt;.rect&lt;/code&gt; has gone past the left side of the screen. Once the enemy is off-screen, you call &lt;code&gt;.kill()&lt;/code&gt; to prevent it from being processed further.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So, what does &lt;code&gt;.kill()&lt;/code&gt; do? To figure this out, you have to know about &lt;strong&gt;Sprite Groups&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&quot;sprite-groups&quot;&gt;Sprite Groups&lt;/h2&gt;
&lt;p&gt;Another super useful class that &lt;code&gt;pygame&lt;/code&gt; provides is the &lt;a href=&quot;https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group&quot;&gt;&lt;code&gt;Sprite Group&lt;/code&gt;&lt;/a&gt;. This is an object that holds a group of &lt;code&gt;Sprite&lt;/code&gt; objects. So why use it? Can&amp;rsquo;t you just track your &lt;code&gt;Sprite&lt;/code&gt; objects in a list instead? Well, you can, but the advantage of using a &lt;code&gt;Group&lt;/code&gt; lies in the methods it exposes. These methods help to detect whether any &lt;code&gt;Enemy&lt;/code&gt; has collided with the &lt;code&gt;Player&lt;/code&gt;, which makes updates much easier.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s see how to create sprite groups. You&amp;rsquo;ll create two different &lt;code&gt;Group&lt;/code&gt; objects:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The first &lt;code&gt;Group&lt;/code&gt; will hold every &lt;code&gt;Sprite&lt;/code&gt; in the game.&lt;/li&gt;
&lt;li&gt;The second &lt;code&gt;Group&lt;/code&gt; will hold just the &lt;code&gt;Enemy&lt;/code&gt; objects.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here&amp;rsquo;s what that looks like in code:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;82 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Create the &amp;#39;player&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;83 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;player&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;84 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;85 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# Create groups to hold enemy sprites and all sprites&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;86 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# - enemies is used for collision detection and position updates&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;87 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# - all_sprites is used for rendering&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;88 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;enemies&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sprite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;89 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;all_sprites&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sprite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;90 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;all_sprites&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;91 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;92 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Variable to keep the main loop running&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;93 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When you call &lt;code&gt;.kill()&lt;/code&gt;, the &lt;code&gt;Sprite&lt;/code&gt; is removed from every &lt;code&gt;Group&lt;/code&gt; to which it belongs. This removes the references to the &lt;code&gt;Sprite&lt;/code&gt; as well, which allows Python&amp;rsquo;s garbage collector to reclaim the memory as necessary.&lt;/p&gt;
&lt;p&gt;Now that you have an &lt;code&gt;all_sprites&lt;/code&gt; group, you can change how objects are drawn. Instead of calling &lt;code&gt;.blit()&lt;/code&gt; on just &lt;code&gt;Player&lt;/code&gt;, you can iterate over everything in &lt;code&gt;all_sprites&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;117 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Fill the screen with black&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;118 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;119 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;120 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# Draw all sprites&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;121 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entity&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all_sprites&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;122 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;entity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;123 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;124 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Flip everything to the display&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;125 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, anything put into &lt;code&gt;all_sprites&lt;/code&gt; will be drawn with every frame, whether it&amp;rsquo;s an enemy or the player.&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s just one problem&amp;hellip; You don&amp;rsquo;t have any enemies! You could create a bunch of enemies at the beginning of the game, but the game would quickly become boring when they all left the screen a few seconds later. Instead, let&amp;rsquo;s explore how to keep a steady supply of enemies coming as the game progresses.&lt;/p&gt;
&lt;h2 id=&quot;custom-events&quot;&gt;Custom Events&lt;/h2&gt;
&lt;p&gt;The design calls for enemies to appear at regular intervals. This means that at set intervals, you need to do two things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create a new &lt;code&gt;Enemy&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Add it to &lt;code&gt;all_sprites&lt;/code&gt; and &lt;code&gt;enemies&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You already have code that handles random events. The event loop is designed to look for random events occurring every frame and deal with them appropriately. Luckily, &lt;code&gt;pygame&lt;/code&gt; doesn&amp;rsquo;t restrict you to using only the event types it has defined. You can define your own events to handle as you see fit.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s see how to create a custom event that&amp;rsquo;s generated every few seconds. You can create a custom event by naming it:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;78 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Create the screen object&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;79 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;80 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_HEIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;81 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;82 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# Create a custom event for adding a new enemy&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;83 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;ADDENEMY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;USEREVENT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;84 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_timer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ADDENEMY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;85 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;86 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Instantiate player. Right now, this is just a rectangle.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;87 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;player&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;pygame&lt;/code&gt; defines events internally as integers, so you need to define a new event with a unique integer. The last event &lt;code&gt;pygame&lt;/code&gt; reserves is called &lt;code&gt;USEREVENT&lt;/code&gt;, so defining &lt;code&gt;ADDENEMY = pygame.USEREVENT + 1&lt;/code&gt; on line 83 ensures it&amp;rsquo;s unique.&lt;/p&gt;
&lt;p&gt;Next, you need to insert this new event into the event queue at regular intervals throughout the game. That&amp;rsquo;s where the &lt;a href=&quot;https://www.pygame.org/docs/ref/time.html&quot;&gt;&lt;code&gt;time&lt;/code&gt;&lt;/a&gt; module comes in. Line 84 fires the new &lt;code&gt;ADDENEMY&lt;/code&gt; event every 250 milliseconds, or four times per second. You call &lt;code&gt;.set_timer()&lt;/code&gt; outside the game loop since you only need one timer, but it will fire throughout the entire game.&lt;/p&gt;
&lt;p&gt;Add the code to handle your new event:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;100 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Main loop&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;101 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;102 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Look at every event in the queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;103 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;104 &lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Did the user hit a key?&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;105 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;KEYDOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;106 &lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;# Was it the Escape key? If so, stop the loop.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;107 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K_ESCAPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;108 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;109 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;110 &lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Did the user click the window close button? If so, stop the loop.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;111 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;112 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;113 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;114 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;c1&quot;&gt;# Add a new enemy?&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;115 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ADDENEMY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;116 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;c1&quot;&gt;# Create the new enemy and add it to sprite groups&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;117 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;new_enemy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Enemy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;118 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;enemies&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_enemy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;119 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;all_sprites&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_enemy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;120 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;121 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Get the set of keys pressed and check for user input&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;122 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_pressed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;123 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;124 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;125 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;# Update enemy position&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;126 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;enemies&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Whenever the event handler sees the new &lt;code&gt;ADDENEMY&lt;/code&gt; event on line 115, it creates an &lt;code&gt;Enemy&lt;/code&gt; and adds it to &lt;code&gt;enemies&lt;/code&gt; and &lt;code&gt;all_sprites&lt;/code&gt;. Since  &lt;code&gt;Enemy&lt;/code&gt; is in &lt;code&gt;all_sprites&lt;/code&gt;, it will get drawn every frame. You also need to call &lt;code&gt;enemies.update()&lt;/code&gt; on line 126, which updates everything in &lt;code&gt;enemies&lt;/code&gt;, to ensure they move properly:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pygame-enemies-cropped.51821f9a69a6.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border w-66&quot; src=&quot;https://files.realpython.com/media/pygame-enemies-cropped.51821f9a69a6.gif&quot; width=&quot;424&quot; height=&quot;346&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-enemies-cropped.51821f9a69a6.gif&amp;amp;w=106&amp;amp;sig=37395cb6670bae79d00e1b10f74d578359d8af4d 106w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-enemies-cropped.51821f9a69a6.gif&amp;amp;w=212&amp;amp;sig=16560e2e38aabe62632945cb3b85bbaec27f673f 212w, https://files.realpython.com/media/pygame-enemies-cropped.51821f9a69a6.gif 424w&quot; sizes=&quot;75vw&quot; alt=&quot;Enemies flying by in pygame&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;However, that&amp;rsquo;s not the only reason there&amp;rsquo;s a group for just &lt;code&gt;enemies&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;collision-detection&quot;&gt;Collision Detection&lt;/h2&gt;
&lt;p&gt;Your game design calls for the game to end whenever an enemy collides with the player. Checking for collisions is a basic technique of game programming, and usually requires some non-trivial math to determine whether two sprites will overlap each other.&lt;/p&gt;
&lt;p&gt;This is where a framework like &lt;code&gt;pygame&lt;/code&gt; comes in handy! Writing collision detection code is tedious, but &lt;code&gt;pygame&lt;/code&gt; has a LOT of &lt;a href=&quot;https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.collide_rect&quot;&gt;collision detection methods&lt;/a&gt; available for you to use.&lt;/p&gt;
&lt;p&gt;For this tutorial, you&amp;rsquo;ll use a method called &lt;a href=&quot;https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollideany&quot;&gt;&lt;code&gt;.spritecollideany()&lt;/code&gt;&lt;/a&gt;, which is read as &amp;ldquo;sprite collide any.&amp;rdquo; This method accepts a &lt;code&gt;Sprite&lt;/code&gt; and a &lt;code&gt;Group&lt;/code&gt; as parameters. It looks at every object in the &lt;code&gt;Group&lt;/code&gt; and checks if its &lt;code&gt;.rect&lt;/code&gt; intersects with the &lt;code&gt;.rect&lt;/code&gt; of the &lt;code&gt;Sprite&lt;/code&gt;. If so, then it returns &lt;code&gt;True&lt;/code&gt;. Otherwise, it returns &lt;code&gt;False&lt;/code&gt;. This is perfect for this game since you need to check if the single &lt;code&gt;player&lt;/code&gt; collides with one of a &lt;code&gt;Group&lt;/code&gt; of &lt;code&gt;enemies&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s what that looks like in code:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;130 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Draw all sprites&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;131 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entity&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;all_sprites&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;132 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;entity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;133 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;134 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# Check if any enemies have collided with the player&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;135 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sprite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spritecollideany&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enemies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;136 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;# If so, then remove the player and stop the loop&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;137 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;138 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Line 135 tests whether &lt;code&gt;player&lt;/code&gt; has collided with any of the objects in &lt;code&gt;enemies&lt;/code&gt;. If so, then &lt;code&gt;player.kill()&lt;/code&gt; is called to remove it from every group to which it belongs. Since the only objects being rendered are in &lt;code&gt;all_sprites&lt;/code&gt;, the &lt;code&gt;player&lt;/code&gt; will no longer be rendered. Once the player has been killed, you need to exit the game as well, so you set &lt;code&gt;running = False&lt;/code&gt; to break out of the game loop on line 138.&lt;/p&gt;
&lt;p&gt;At this point, you&amp;rsquo;ve got the basic elements of a game in place:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pygame-part2.7d0bd84a877e.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border w-66&quot; src=&quot;https://files.realpython.com/media/pygame-part2.7d0bd84a877e.png&quot; width=&quot;790&quot; height=&quot;613&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-part2.7d0bd84a877e.png&amp;amp;w=197&amp;amp;sig=00e9cf19d593f9d3a39430a908de9b8e7517f982 197w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-part2.7d0bd84a877e.png&amp;amp;w=395&amp;amp;sig=bb36872ffef4a6ef93c03ce20d1e0983bc305df7 395w, https://files.realpython.com/media/pygame-part2.7d0bd84a877e.png 790w&quot; sizes=&quot;75vw&quot; alt=&quot;Pygame window&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now, let&amp;rsquo;s dress it up a bit, make it more playable, and add some advanced capabilities to help it stand out.&lt;/p&gt;
&lt;h2 id=&quot;sprite-images&quot;&gt;Sprite Images&lt;/h2&gt;
&lt;p&gt;Alright, you have a game, but let&amp;rsquo;s be honest&amp;hellip; It&amp;rsquo;s kind of ugly. The player and enemies are just white blocks on a black background. That was state-of-the-art when &lt;a href=&quot;https://en.wikipedia.org/wiki/Pong&quot;&gt;Pong&lt;/a&gt; was new, but it just doesn&amp;rsquo;t cut it anymore. Let&amp;rsquo;s replace all those boring white rectangles with some cooler images that will make the game feel like an actual game.&lt;/p&gt;
&lt;p&gt;Earlier, you learned that images on disk can be loaded into a &lt;code&gt;Surface&lt;/code&gt; with some help from the &lt;code&gt;image&lt;/code&gt; module. For this tutorial, we made a little jet for the player and some missiles for the enemies. You&amp;rsquo;re welcome to use this art, draw your own, or download some &lt;a href=&quot;https://realpython.com/pygame-a-primer/#note-on-sources&quot;&gt;free game art assets&lt;/a&gt; to use. You can click the link below to download the art used in this tutorial:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Clone Repo:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/pygame/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-pygame&quot; data-focus=&quot;false&quot;&gt;Click here to clone the repo you&#39;ll use&lt;/a&gt; to learn how to use PyGame in this tutorial.&lt;/p&gt;&lt;/div&gt;

&lt;h3 id=&quot;altering-the-object-constructors&quot;&gt;Altering the Object Constructors&lt;/h3&gt;
&lt;p&gt;Before you use images to represent the player and enemy sprites, you need to make some changes to their constructors. The code below replaces the code used previously:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Import pygame.locals for easier access to key coordinates&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Updated to conform to flake8 and black standards&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# from pygame.locals import *&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pygame.locals&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;RLEACCEL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_DOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_LEFT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_RIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;K_ESCAPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;KEYDOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Define constants for the screen width and height&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;800&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_HEIGHT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;600&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Define the Player object by extending pygame.sprite.Sprite&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Instead of a surface, use an image for a better-looking sprite&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sprite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sprite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;jet.png&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_colorkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;RLEACCEL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_rect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let&amp;rsquo;s unpack line 31 a bit. &lt;code&gt;pygame.image.load()&lt;/code&gt; loads an image from the disk. You pass it a path to the file. It returns a &lt;code&gt;Surface&lt;/code&gt;, and the &lt;code&gt;.convert()&lt;/code&gt; call optimizes the &lt;code&gt;Surface&lt;/code&gt;, making future &lt;code&gt;.blit()&lt;/code&gt; calls faster.&lt;/p&gt;
&lt;p&gt;Line 32 uses &lt;code&gt;.set_colorkey()&lt;/code&gt; to indicate the color &lt;code&gt;pygame&lt;/code&gt; will render as transparent. In this case, you choose white, because that&amp;rsquo;s the background color of the jet image. The &lt;a href=&quot;https://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_colorkey&quot;&gt;RLEACCEL&lt;/a&gt; constant is an optional parameter that helps &lt;code&gt;pygame&lt;/code&gt; render more quickly on non-accelerated displays. This is added to the &lt;code&gt;pygame.locals&lt;/code&gt; import statement on line 11.&lt;/p&gt;
&lt;p&gt;Nothing else needs to change. The image is still a &lt;code&gt;Surface&lt;/code&gt;, except now it has a picture painted on it. You still use it in the same way.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s what similar changes to the &lt;code&gt;Enemy&lt;/code&gt; look like:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;59 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Define the enemy object by extending pygame.sprite.Sprite&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;60 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Instead of a surface, use an image for a better-looking sprite&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;61 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Enemy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sprite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sprite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;62 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;63 &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Enemy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;64 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;missile.png&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;65 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_colorkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;RLEACCEL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;66 &lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# The starting position is randomly generated, as is the speed&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;67 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_rect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;68 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;69 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;70 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_HEIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;71 &lt;/span&gt;            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;72 &lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;73 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;speed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Running the program now should show that this is the same game you had before, except now you&amp;rsquo;ve added some nice graphics &lt;strong&gt;skins&lt;/strong&gt; with images. But why stop at just making the player and enemy sprites look nice? Let&amp;rsquo;s add a few clouds going past to give the impression of a jet flying through the sky.&lt;/p&gt;
&lt;h3 id=&quot;adding-background-images&quot;&gt;Adding Background Images&lt;/h3&gt;
&lt;p&gt;For background clouds, you use the same principles as you did for &lt;code&gt;Player&lt;/code&gt; and &lt;code&gt;Enemy&lt;/code&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create the &lt;code&gt;Cloud&lt;/code&gt; class.&lt;/li&gt;
&lt;li&gt;Add an image of a cloud to it.&lt;/li&gt;
&lt;li&gt;Create a method &lt;code&gt;.update()&lt;/code&gt; that moves the &lt;code&gt;cloud&lt;/code&gt; toward the left side of the screen.&lt;/li&gt;
&lt;li&gt;Create a custom event and handler to create new &lt;code&gt;cloud&lt;/code&gt; objects at a set time interval.&lt;/li&gt;
&lt;li&gt;Add the newly created &lt;code&gt;cloud&lt;/code&gt; objects to a new &lt;code&gt;Group&lt;/code&gt; called &lt;code&gt;clouds&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Update and draw the &lt;code&gt;clouds&lt;/code&gt; in your game loop.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here&amp;rsquo;s what &lt;code&gt;Cloud&lt;/code&gt; looks like:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 83 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Define the cloud object by extending pygame.sprite.Sprite&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 84 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Use an image for a better-looking sprite&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 85 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Cloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sprite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sprite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 86 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 87 &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 88 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;cloud.png&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 89 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_colorkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;RLEACCEL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 90 &lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# The starting position is randomly generated&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 91 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_rect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 92 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 93 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_WIDTH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 94 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SCREEN_HEIGHT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 95 &lt;/span&gt;            &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 96 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 97 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Move the cloud based on a constant speed&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 98 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Remove the cloud when it passes the left edge of the screen&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 99 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;100 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move_ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;101 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;102 &lt;/span&gt;            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That should all look very familiar. It&amp;rsquo;s pretty much the same as &lt;code&gt;Enemy&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To have clouds appear at certain intervals, you&amp;rsquo;ll use event creation code similar to what you used to create new enemies. Put it right below the enemy creation event:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;116 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Create custom events for adding a new enemy and a cloud&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;117 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ADDENEMY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;USEREVENT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;118 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_timer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ADDENEMY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;119 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;ADDCLOUD&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;USEREVENT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;120 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_timer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ADDCLOUD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This says to wait 1000 milliseconds, or one second, before creating the next &lt;code&gt;cloud&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Next, create a new &lt;code&gt;Group&lt;/code&gt; to hold each newly created &lt;code&gt;cloud&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;125 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Create groups to hold enemy sprites, cloud sprites, and all sprites&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;126 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# - enemies is used for collision detection and position updates&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;127 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# - clouds is used for position updates&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;128 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# - all_sprites is used for rendering&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;129 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enemies&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sprite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;130 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;clouds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sprite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;131 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_sprites&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sprite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;132 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_sprites&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Next, add a handler for the new &lt;code&gt;ADDCLOUD&lt;/code&gt; event in the event handler:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;137 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Main loop&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;138 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;139 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Look at every event in the queue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;140 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;141 &lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Did the user hit a key?&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;142 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;KEYDOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;143 &lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;# Was it the Escape key? If so, then stop the loop.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;144 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K_ESCAPE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;145 &lt;/span&gt;                &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;146 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;147 &lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Did the user click the window close button? If so, stop the loop.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;148 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;QUIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;149 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;150 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;151 &lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;# Add a new enemy?&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;152 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ADDENEMY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;153 &lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;# Create the new enemy and add it to sprite groups&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;154 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;new_enemy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Enemy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;155 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;enemies&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_enemy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;156 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;all_sprites&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_enemy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;157 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;158 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;c1&quot;&gt;# Add a new cloud?&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;159 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;        &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ADDCLOUD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;160 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;c1&quot;&gt;# Create the new cloud and add it to sprite groups&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;161 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;new_cloud&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;162 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;clouds&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_cloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;163 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;all_sprites&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_cloud&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Finally, make sure the &lt;code&gt;clouds&lt;/code&gt; are updated every frame:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;167 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Update the position of enemies and clouds&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;168 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;enemies&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;169 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;clouds&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;170 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;171 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Fill the screen with sky blue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;172 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;screen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;135&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;206&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Line 172 updates the original &lt;code&gt;screen.fill()&lt;/code&gt; to fill the screen with a pleasant sky blue color. You can change this color to something else. Maybe you want an alien world with a purple sky, a toxic wasteland in neon green, or the surface of Mars in red!&lt;/p&gt;
&lt;p&gt;Note that each new &lt;code&gt;Cloud&lt;/code&gt; and &lt;code&gt;Enemy&lt;/code&gt; are added to &lt;code&gt;all_sprites&lt;/code&gt; as well as &lt;code&gt;clouds&lt;/code&gt; and &lt;code&gt;enemies&lt;/code&gt;. This is done because each group is used for a separate purpose:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Rendering&lt;/strong&gt; is done using &lt;code&gt;all_sprites&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Position updates&lt;/strong&gt; are done using &lt;code&gt;clouds&lt;/code&gt; and &lt;code&gt;enemies&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Collision detection&lt;/strong&gt; is done using &lt;code&gt;enemies&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You create multiple groups so that you can change the way sprites move or behave without impacting the movement or behavior of other sprites.&lt;/p&gt;
&lt;h2 id=&quot;game-speed&quot;&gt;Game Speed&lt;/h2&gt;
&lt;p&gt;While testing the game you may have noticed that the enemies move a little fast. If not, then that&amp;rsquo;s okay, as different machines will see different results at this point.&lt;/p&gt;
&lt;p&gt;The reason for this is that the game loop processes frames as fast as the processor and environment will allow. Since all the sprites move once per frame, they can move hundreds of times each second. The number of frames handled each second is called the &lt;strong&gt;frame rate&lt;/strong&gt;, and getting this right is the difference between a playable game and a forgettable one.&lt;/p&gt;
&lt;p&gt;Normally, you want as high a frame rate as possible, but for this game, you need to slow it down a bit for the game to be playable. Fortunately, the module &lt;code&gt;time&lt;/code&gt; contains a &lt;a href=&quot;https://www.pygame.org/docs/ref/time.html#pygame.time.Clock&quot;&gt;&lt;code&gt;Clock&lt;/code&gt;&lt;/a&gt; which is designed exactly for this purpose.&lt;/p&gt;
&lt;p&gt;Using &lt;code&gt;Clock&lt;/code&gt; to establish a playable frame rate requires just two lines of code. The first creates a new &lt;code&gt;Clock&lt;/code&gt; before the game loop begins:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;106 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Setup the clock for a decent framerate&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;107 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clock&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The second calls &lt;code&gt;.tick()&lt;/code&gt; to inform &lt;code&gt;pygame&lt;/code&gt; that the program has reached the end of the frame:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;188 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Flip everything to the display&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;189 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;190 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;191 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# Ensure program maintains a rate of 30 frames per second&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;192 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;clock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tick&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The argument passed to &lt;code&gt;.tick()&lt;/code&gt; establishes the desired frame rate. To do this, &lt;code&gt;.tick()&lt;/code&gt; calculates the number of milliseconds each frame should take, based on the desired frame rate. Then, it compares that number to the number of milliseconds that have passed since the last time &lt;code&gt;.tick()&lt;/code&gt; was called. If not enough time has passed, then &lt;code&gt;.tick()&lt;/code&gt; delays processing to ensure that it never exceeds the specified frame rate.&lt;/p&gt;
&lt;p&gt;Passing in a smaller frame rate will result in more time in each frame for calculations, while a larger frame rate provides smoother (and possibly faster) gameplay:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pygame-playable-cropped.8c230439469b.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border w-66&quot; src=&quot;https://files.realpython.com/media/pygame-playable-cropped.8c230439469b.gif&quot; width=&quot;422&quot; height=&quot;346&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-playable-cropped.8c230439469b.gif&amp;amp;w=105&amp;amp;sig=87d43ce2e95c1c59d0ec1a0fa4884f133e25f3f2 105w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-playable-cropped.8c230439469b.gif&amp;amp;w=211&amp;amp;sig=fa60b7c67401627080feaeb23f9897b2de815b50 211w, https://files.realpython.com/media/pygame-playable-cropped.8c230439469b.gif 422w&quot; sizes=&quot;75vw&quot; alt=&quot;Setting the frame rate in pygame&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Play around with this number to see what feels best for you!&lt;/p&gt;
&lt;h2 id=&quot;sound-effects&quot;&gt;Sound Effects&lt;/h2&gt;
&lt;p&gt;So far, you&amp;rsquo;ve focused on gameplay and the visual aspects of your game. Now let&amp;rsquo;s explore giving your game some auditory flavor as well. &lt;code&gt;pygame&lt;/code&gt; provides &lt;a href=&quot;https://www.pygame.org/docs/ref/mixer.html&quot;&gt;&lt;code&gt;mixer&lt;/code&gt;&lt;/a&gt; to handle all sound-related activities. You&amp;rsquo;ll use this module&amp;rsquo;s classes and methods to provide background music and sound effects for various actions.&lt;/p&gt;
&lt;p&gt;The name &lt;code&gt;mixer&lt;/code&gt; refers to the fact that the module mixes various sounds into a cohesive whole. Using the &lt;a href=&quot;https://www.pygame.org/docs/ref/music.html&quot;&gt;&lt;code&gt;music&lt;/code&gt;&lt;/a&gt; sub-module, you can stream individual sound files in a variety of formats, such as &lt;a href=&quot;https://en.wikipedia.org/wiki/MP3&quot;&gt;MP3&lt;/a&gt;, &lt;a href=&quot;https://en.wikipedia.org/wiki/Ogg&quot;&gt;Ogg&lt;/a&gt;, and &lt;a href=&quot;https://en.wikipedia.org/wiki/Module_file&quot;&gt;Mod&lt;/a&gt;. You can also use &lt;code&gt;Sound&lt;/code&gt; to hold a single sound effect to be played, in either Ogg or &lt;a href=&quot;https://en.wikipedia.org/wiki/WAV&quot;&gt;uncompressed WAV&lt;/a&gt; formats. All playback happens in the background, so when you play a &lt;code&gt;Sound&lt;/code&gt;, the method returns immediately as the sound plays.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The &lt;a href=&quot;https://www.pygame.org/docs/ref/music.html&quot;&gt;&lt;code&gt;pygame&lt;/code&gt; documentation&lt;/a&gt; states that MP3 support is limited, and unsupported formats can cause system crashes. The sounds referenced in this article have been tested, and we recommend testing any sounds thoroughly before releasing your game.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;As with most things &lt;code&gt;pygame&lt;/code&gt;, using &lt;code&gt;mixer&lt;/code&gt; starts with an initialization step. Luckily, this is already handled by &lt;code&gt;pygame.init()&lt;/code&gt;. You only need to call &lt;code&gt;pygame.mixer.init()&lt;/code&gt; if you want to change the defaults:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;106 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# Setup for sounds. Defaults are good.&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;107 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mixer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;108 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;109 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Initialize pygame&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;110 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;111 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;112 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Set up the clock for a decent framerate&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;113 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clock&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Clock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;pygame.mixer.init()&lt;/code&gt; accepts &lt;a href=&quot;https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.init&quot;&gt;a number of arguments&lt;/a&gt;, but the defaults work fine in most cases. Note that if you want to change the defaults, you need to call &lt;code&gt;pygame.mixer.init()&lt;/code&gt; before calling &lt;code&gt;pygame.init()&lt;/code&gt;. Otherwise, the defaults will be in effect regardless of your changes.&lt;/p&gt;
&lt;p&gt;After the system is initialized, you can get your sounds and background music setup:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;135 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Load and play background music&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;136 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Sound source: http://ccmixter.org/files/Apoxode/59262&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;137 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# License: https://creativecommons.org/licenses/by/3.0/&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;138 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mixer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;music&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Apoxode_-_Electric_1.mp3&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;139 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mixer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;music&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loops&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;140 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;141 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Load all sound files&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;142 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Sound sources: Jon Fincher&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;143 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move_up_sound&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mixer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Rising_putter.ogg&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;144 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move_down_sound&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mixer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Falling_putter.ogg&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;145 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;collision_sound&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mixer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sound&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Collision.ogg&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Lines 138 and 139&lt;/strong&gt; load a background sound clip and begin playing it. You can tell the sound clip to loop and never end by setting the named parameter &lt;code&gt;loops=-1&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lines 143 to 145&lt;/strong&gt; load three sounds you&amp;rsquo;ll use for various sound effects. The first two are rising and falling sounds, which are played when the player moves up or down. The last is the sound used whenever there is a collision. You can add other sounds as well, such as a sound for whenever an &lt;code&gt;Enemy&lt;/code&gt; is created, or a final sound for when the game ends.&lt;/p&gt;
&lt;p&gt;So, how do you use the sound effects? You want to play each sound when a certain event occurs. For example, when the ship moves up, you want to play &lt;code&gt;move_up_sound&lt;/code&gt;. Therefore, you add a call to  &lt;code&gt;.play()&lt;/code&gt; whenever you handle that event. In the design, that means adding the following calls to &lt;code&gt;.update()&lt;/code&gt; for &lt;code&gt;Player&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Define the Player object by extending pygame.sprite.Sprite&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Instead of a surface, use an image for a better-looking sprite&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sprite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sprite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fm&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;jet.png&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_colorkey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;RLEACCEL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;surf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_rect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;34 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;35 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Move the sprite based on keypresses&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;36 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;37 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K_UP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;38 &lt;/span&gt;            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move_ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;39 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;move_up_sound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;40 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pressed_keys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;K_DOWN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;41 &lt;/span&gt;            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move_ip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;42 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;            &lt;span class=&quot;n&quot;&gt;move_down_sound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For a collision between the player and an enemy, you play the sound for when collisions are detected:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;201 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Check if any enemies have collided with the player&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;202 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sprite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spritecollideany&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enemies&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;203 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# If so, then remove the player&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;204 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;205 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;206 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Stop any moving sounds and play the collision sound&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;207 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;move_up_sound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;208 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;move_down_sound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;209 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;n&quot;&gt;collision_sound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;210 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;211 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Stop the loop&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;212 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, you stop any other sound effects first, because in a collision the player is no longer moving. Then you play the collision sound and continue execution from there.&lt;/p&gt;
&lt;p&gt;Finally, when the game is over, all sounds should stop. This is true whether the game ends due to a collision or the user exits manually. To do this, add the following lines at the end of the program after the loop:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;220 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# All done! Stop and quit the mixer.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;221 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mixer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;music&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;222 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pygame&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mixer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Technically, these last few lines are not required, as the program ends right after this. However, if you decide later on to add an intro screen or an exit screen to your game, then there may be more code running after the game ends.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s it! Test it again, and you should see something like this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pygame-part3.fdd9bec01862.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border w-66&quot; src=&quot;https://files.realpython.com/media/pygame-part3.fdd9bec01862.png&quot; width=&quot;791&quot; height=&quot;612&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-part3.fdd9bec01862.png&amp;amp;w=197&amp;amp;sig=7b409a1a744648dbdc015d363168caff0a4ef75d 197w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pygame-part3.fdd9bec01862.png&amp;amp;w=395&amp;amp;sig=e49efd83ad159c962ab2e01433fc4ac794fc3c5b 395w, https://files.realpython.com/media/pygame-part3.fdd9bec01862.png 791w&quot; sizes=&quot;75vw&quot; alt=&quot;Pygame window&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;a-note-on-sources&quot;&gt;A Note on Sources&lt;/h2&gt;
&lt;p&gt;You may have noticed the comment on lines 136-137 when the background music was loaded, listing the source of the music and a link to the Creative Commons license. This was done because the creator of that sound required it. The license requirements stated that in order to use the sound, both proper attribution and a link to the license must be provided.&lt;/p&gt;
&lt;p&gt;Here are some sources for music, sound, and art that you can search for useful content:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://opengameart.org&quot;&gt;&lt;strong&gt;OpenGameArt.org:&lt;/strong&gt;&lt;/a&gt; sounds, sound effects, sprites, and other artwork&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://kenney.nl&quot;&gt;&lt;strong&gt;Kenney.nl:&lt;/strong&gt;&lt;/a&gt; sounds, sound effects, sprites, and other artwork&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.gameart2d.com/&quot;&gt;&lt;strong&gt;Gamer Art 2D:&lt;/strong&gt;&lt;/a&gt; sprites and other artwork&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://ccmixter.org&quot;&gt;&lt;strong&gt;CC Mixter:&lt;/strong&gt;&lt;/a&gt; sounds and sound effects&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://freesound.org/&quot;&gt;&lt;strong&gt;Freesound:&lt;/strong&gt;&lt;/a&gt; sounds and sound effects&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you make your games and use downloaded content such as art, music, or code from other sources, please be sure that you are complying with the licensing terms of those sources.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Throughout this tutorial, you&amp;rsquo;ve learned how game programming with &lt;code&gt;pygame&lt;/code&gt; differs from standard procedural programming. You&amp;rsquo;ve also learned how to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Implement event loops&lt;/li&gt;
&lt;li&gt;Draw items on the screen&lt;/li&gt;
&lt;li&gt;Play sound effects and music&lt;/li&gt;
&lt;li&gt;Handle user input&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To do this, you used a subset of the &lt;code&gt;pygame&lt;/code&gt; modules, including the &lt;code&gt;display&lt;/code&gt;, &lt;code&gt;mixer&lt;/code&gt; and &lt;code&gt;music&lt;/code&gt;,  &lt;code&gt;time&lt;/code&gt;, &lt;code&gt;image&lt;/code&gt;, &lt;code&gt;event&lt;/code&gt;, and &lt;code&gt;key&lt;/code&gt; modules. You also used several &lt;code&gt;pygame&lt;/code&gt; classes, including &lt;code&gt;Rect&lt;/code&gt;, &lt;code&gt;Surface&lt;/code&gt;, &lt;code&gt;Sound&lt;/code&gt;, and &lt;code&gt;Sprite&lt;/code&gt;. But these only scratch the surface of what &lt;code&gt;pygame&lt;/code&gt; can do! Check out the &lt;a href=&quot;https://www.pygame.org/docs/&quot;&gt;official &lt;code&gt;pygame&lt;/code&gt; documentation&lt;/a&gt; for a full list of available modules and classes.&lt;/p&gt;
&lt;p&gt;You can find all of the code, graphics, and sound files for this article by clicking the link below:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Clone Repo:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/pygame/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-pygame&quot; data-focus=&quot;false&quot;&gt;Click here to clone the repo you&#39;ll use&lt;/a&gt; to learn how to use PyGame in this tutorial.&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;Feel free to leave comments below as well. Happy Pythoning!&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Python vs C++: Selecting the Right Tool for the Job</title>
      <id>https://realpython.com/python-vs-cpp/</id>
      <link href="https://realpython.com/python-vs-cpp/"/>
      <updated>2019-09-11T14:00:00+00:00</updated>
      <summary>In this intermediate-level article, you&#39;ll explore the similarities and differences you&#39;ll find when comparing Python vs C++. You&#39;ll learn about memory management, virtual machines, object-oriented programming differences, and much more!</summary>
      <content type="html">
        &lt;p&gt;Are you a C++ developer comparing Python vs C++? Are you looking at Python and wondering what all the fuss is about? Do you wonder how Python compares to the concepts you already know? Or perhaps you have a bet on who would win if you locked C++ and Python in a cage and let them battle it out? Then this article is for you!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this article, you&amp;rsquo;ll learn about&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Differences and similarities when you&amp;rsquo;re comparing Python vs C++&lt;/li&gt;
&lt;li&gt;Times when Python might be a better choice for a problem and vice versa&lt;/li&gt;
&lt;li&gt;Resources to turn to as you have questions while learning Python&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This article is aimed at C++ developers who are learning Python. It assumes a basic knowledge of both languages and will use concepts from Python 3.6 and up, as well as C++11 or later.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s dive into looking at Python vs C++!&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-tricks-sample&quot; data-focus=&quot;false&quot;&gt;Click here to get access to a chapter from Python Tricks: The Book&lt;/a&gt; that shows you Python&#39;s best practices with simple examples you can apply instantly to write more beautiful + Pythonic code.&lt;/p&gt;&lt;/div&gt;

&lt;h2 id=&quot;comparing-languages-python-vs-c&quot;&gt;Comparing Languages: Python vs C++&lt;/h2&gt;
&lt;p&gt;Frequently, you&amp;rsquo;ll find articles that extoll the virtues of one programming language over another. Quite often, they devolve into efforts to promote one language by degrading the other. This isn&amp;rsquo;t that type of article.  &lt;/p&gt;
&lt;p&gt;When you&amp;rsquo;re comparing Python vs C++, remember that they&amp;rsquo;re both tools, and they both have uses for different problems. Think about comparing a hammer and a screwdriver. You &lt;em&gt;could&lt;/em&gt; use a screwdriver to drive in nails, and you &lt;em&gt;could&lt;/em&gt; use a hammer to force in screws, but neither experience will be all that effective.&lt;/p&gt;
&lt;p&gt;Using the right tool for the job is important. In this article, you&amp;rsquo;ll learn about the features of Python and C++ that make each of them the right choice for certain types of problems. So, don&amp;rsquo;t view the &amp;ldquo;vs&amp;rdquo; in Python vs C++ as meaning &amp;ldquo;against.&amp;rdquo; Rather, think of it as a comparison.&lt;/p&gt;
&lt;h2 id=&quot;compilation-vs-virtual-machine&quot;&gt;Compilation vs Virtual Machine&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s start with the biggest difference when you&amp;rsquo;re comparing Python vs C++. In C++, you use a compiler that converts your source code into machine code and produces an executable. The executable is a separate file that can then be run as a stand-alone program:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/cpp_single_compile.209a9e10bff5.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-50&quot; src=&quot;https://files.realpython.com/media/cpp_single_compile.209a9e10bff5.png&quot; width=&quot;1233&quot; height=&quot;303&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/cpp_single_compile.209a9e10bff5.png&amp;amp;w=308&amp;amp;sig=5f6e74cc693b578b1f13ce0b7714754f4546f349 308w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/cpp_single_compile.209a9e10bff5.png&amp;amp;w=616&amp;amp;sig=bacc781219de00d704d49dced7fa4c1acf567467 616w, https://files.realpython.com/media/cpp_single_compile.209a9e10bff5.png 1233w&quot; sizes=&quot;75vw&quot; alt=&quot;Compiling a C++ program for windows.&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This process outputs actual machine instructions for the specific processor and operating system it&amp;rsquo;s built for. In this drawing, it&amp;rsquo;s a Windows program. This means you&amp;rsquo;d have to recompile your program separately for Windows, Mac, and Linux:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/cpp_multi_compile.3ff938bd23f1.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-50&quot; src=&quot;https://files.realpython.com/media/cpp_multi_compile.3ff938bd23f1.png&quot; width=&quot;1236&quot; height=&quot;1086&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/cpp_multi_compile.3ff938bd23f1.png&amp;amp;w=309&amp;amp;sig=ba5cbdd3e8ee6971378fa18fa2cf5b00e03a991e 309w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/cpp_multi_compile.3ff938bd23f1.png&amp;amp;w=618&amp;amp;sig=6f3032c9725c0562bafc2db81f49d43c276617e6 618w, https://files.realpython.com/media/cpp_multi_compile.3ff938bd23f1.png 1236w&quot; sizes=&quot;75vw&quot; alt=&quot;Compiling a C++ program on three operating systems.&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll likely need to modify your C++ code to run on those different systems as well.&lt;/p&gt;
&lt;p&gt;Python, on the other hand, uses a different process. Now, remember that you&amp;rsquo;ll be looking at &lt;code&gt;CPython&lt;/code&gt; which is the standard implementation for the language. Unless you&amp;rsquo;re doing something special, this is the Python you&amp;rsquo;re running.&lt;/p&gt;
&lt;p&gt;Python runs each time you execute your program. It compiles your source just like the C++ compiler. The difference is that Python compiles to &lt;a href=&quot;https://docs.python.org/3/glossary.html#term-bytecode&quot;&gt;bytecode&lt;/a&gt; instead of native machine code. Bytecode is the native instruction code for the &lt;a href=&quot;https://leanpub.com/insidethepythonvirtualmachine/read&quot;&gt;Python virtual machine&lt;/a&gt;. To speed up subsequent runs of your program, Python stores the bytecode in &lt;code&gt;.pyc&lt;/code&gt; files:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/python_compile_to_pyc.514448b58d8a.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-50&quot; src=&quot;https://files.realpython.com/media/python_compile_to_pyc.514448b58d8a.png&quot; width=&quot;1233&quot; height=&quot;303&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/python_compile_to_pyc.514448b58d8a.png&amp;amp;w=308&amp;amp;sig=01c3bdf0f690227766c03351343ed54ec5a68137 308w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/python_compile_to_pyc.514448b58d8a.png&amp;amp;w=616&amp;amp;sig=9cc2117cb8807e500fbcda4898e09cc7ebdeff59 616w, https://files.realpython.com/media/python_compile_to_pyc.514448b58d8a.png 1233w&quot; sizes=&quot;75vw&quot; alt=&quot;Python compiles a py file into a pyc file.&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re using Python 2, then you&amp;rsquo;ll find these files next to the &lt;code&gt;.py&lt;/code&gt; files. For Python 3, you&amp;rsquo;ll find them in a &lt;code&gt;__pycache__&lt;/code&gt; directory.&lt;/p&gt;
&lt;p&gt;The generated bytecode doesn&amp;rsquo;t run natively on your processor. Instead, it&amp;rsquo;s run by the Python virtual machine. This is similar to the Java virtual machine or the .NET Common Runtime Environment. The initial run of your code will result in a compilation step. Then, the bytecode will be interpreted to run on your specific hardware:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/python_initial_run.7d67c05e0bc2.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-75&quot; src=&quot;https://files.realpython.com/media/python_initial_run.7d67c05e0bc2.png&quot; width=&quot;1833&quot; height=&quot;303&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/python_initial_run.7d67c05e0bc2.png&amp;amp;w=458&amp;amp;sig=5b7ebaf8c06347236c6b8782e15825fd50922466 458w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/python_initial_run.7d67c05e0bc2.png&amp;amp;w=916&amp;amp;sig=746bafe3348367b13562e7c7dc9ca1812212b937 916w, https://files.realpython.com/media/python_initial_run.7d67c05e0bc2.png 1833w&quot; sizes=&quot;75vw&quot; alt=&quot;Python compiles a py file into a pyc file and then executes it.&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As long as the program hasn&amp;rsquo;t been changed, each subsequent run will skip the compilation step and use the previously compiled bytecode to interpret: &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/python_execute.pyc.f20350ccc1cf.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-33&quot; src=&quot;https://files.realpython.com/media/python_execute.pyc.f20350ccc1cf.png&quot; width=&quot;843&quot; height=&quot;303&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/python_execute.pyc.f20350ccc1cf.png&amp;amp;w=210&amp;amp;sig=eeda5bcba2735211d811a5223fa1ab6d7c2d7921 210w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/python_execute.pyc.f20350ccc1cf.png&amp;amp;w=421&amp;amp;sig=3d27ddf337bbc98e999e89aa109b01334260b004 421w, https://files.realpython.com/media/python_execute.pyc.f20350ccc1cf.png 843w&quot; sizes=&quot;75vw&quot; alt=&quot;Python executes a pyc file.&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Interpreting code is going to be slower than running native code directly on the hardware. So why does Python work that way? Well, interpreting the code in a virtual machine means that only the virtual machine needs to be compiled for a specific operating system on a specific processor. All of the Python code it runs will run on any machine that has Python. &lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; CPython is written in C, so it can run on most systems that have a C compiler.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Another feature of this cross-platform support is that Python&amp;rsquo;s extensive standard library is written to work on all operating systems. &lt;/p&gt;
&lt;p&gt;Using &lt;a href=&quot;https://docs.python.org/3.7/library/pathlib.html&quot;&gt;&lt;code&gt;pathlib&lt;/code&gt;&lt;/a&gt;, for example, will manage path separators for you whether you&amp;rsquo;re on Windows, Mac, or Linux. The developers of those libraries spent a lot of time making it portable so you don&amp;rsquo;t need to worry about it in your Python program!&lt;/p&gt;
&lt;p&gt;Before you move on, let&amp;rsquo;s start keeping track of a Python vs C++ comparison chart. As you cover new comparisons, they&amp;rsquo;ll be added in italics:&lt;/p&gt;
&lt;div class=&quot;table-responsive&quot;&gt;
&lt;table class=&quot;table table-hover&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Python&lt;/th&gt;
&lt;th&gt;C++&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Faster Execution&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Cross-Platform Execution&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;Now that you&amp;rsquo;ve seen the differences in run time when you&amp;rsquo;re comparing Python vs C++, let&amp;rsquo;s dig into the specifics of the languages&amp;rsquo; syntax.&lt;/p&gt;
&lt;h2 id=&quot;syntax-differences&quot;&gt;Syntax Differences&lt;/h2&gt;
&lt;p&gt;Python and C++ share many syntactical similarities, but there are a few areas worth discussing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Whitespace&lt;/li&gt;
&lt;li&gt;Boolean expressions&lt;/li&gt;
&lt;li&gt;Variables and pointers&lt;/li&gt;
&lt;li&gt;Comprehensions&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&amp;rsquo;s start with the most contentious one first: whitespace.&lt;/p&gt;
&lt;h3 id=&quot;whitespace&quot;&gt;Whitespace&lt;/h3&gt;
&lt;p&gt;The first thing most developers notice when comparing Python vs C++ is the &amp;ldquo;whitespace issue.&amp;rdquo; Python uses leading whitespace to mark scope. This means that the body of an &lt;code&gt;if&lt;/code&gt; block or other similar structure is indicated by the level of indentation. C++ uses curly braces (&lt;code&gt;{}&lt;/code&gt;) to indicate the same idea.&lt;/p&gt;
&lt;p&gt;While the Python lexer will accept any whitespace as long as you&amp;rsquo;re consistent, &lt;a href=&quot;https://realpython.com/python-pep8/&quot;&gt;PEP8&lt;/a&gt; (the &lt;a href=&quot;https://www.python.org/dev/peps/pep-0008/&quot;&gt;official style guide&lt;/a&gt; for Python) specifies 4 spaces for each level of indentation. Most editors can be configured to do this automatically.&lt;/p&gt;
&lt;p&gt;There has been an enormous amount of &lt;a href=&quot;https://unspecified.wordpress.com/2011/10/18/why-pythons-whitespace-rule-is-right/&quot;&gt;writing&lt;/a&gt;, &lt;a href=&quot;https://www.quora.com/Do-you-think-that-indentation-in-Python-is-annoying&quot;&gt;shouting&lt;/a&gt;, and &lt;a href=&quot;https://news.ycombinator.com/item?id=5231632&quot;&gt;ranting&lt;/a&gt; about Python&amp;rsquo;s whitespace rules already, so let&amp;rsquo;s just jump past that issue and on to other matters.&lt;/p&gt;
&lt;p&gt;Instead of relying on a lexical marker like &lt;code&gt;;&lt;/code&gt; to end each statement, Python uses the end of the line. If you need to extend a statement over a single line, then you can use the backslash (&lt;code&gt;\&lt;/code&gt;) to indicate that. (Note that if you&amp;rsquo;re inside a set of parentheses, then the continuation character is not needed.)&lt;/p&gt;
&lt;p&gt;There are people who are unhappy on both sides of the whitespace issue. Some Python developers love that you don&amp;rsquo;t have to type out braces and semicolons. Some C++ developers hate the reliance on formatting. Learning to be comfortable with both is your best bet.&lt;/p&gt;
&lt;p&gt;Now that you&amp;rsquo;ve looked at the whitespace issue, let&amp;rsquo;s move on to one that&amp;rsquo;s a bit less contentious: Boolean expressions.&lt;/p&gt;
&lt;h3 id=&quot;boolean-expressions&quot;&gt;Boolean Expressions&lt;/h3&gt;
&lt;p&gt;The way you&amp;rsquo;ll use &lt;a href=&quot;https://realpython.com/python-data-types/#boolean-type-boolean-context-and-truthiness&quot;&gt;Boolean expressions&lt;/a&gt; changes slightly in Python vs C++. In C++, you can use numeric values to indicate &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;, in addition to the built-in values. Anything that evaluates to &lt;code&gt;0&lt;/code&gt; is considered &lt;code&gt;false&lt;/code&gt;, while every other numeric value is &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Python has a similar concept but extends it to include other cases. The basics are quite similar. The &lt;a href=&quot;https://docs.python.org/3/library/stdtypes.html#truth-value-testing&quot;&gt;Python documentation&lt;/a&gt; states that the following items evaluate to &lt;code&gt;False&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Constants defined as false:&lt;ul&gt;
&lt;li&gt;&lt;code&gt;None&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;False&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Zeros of any numeric type:&lt;ul&gt;
&lt;li&gt;&lt;code&gt;0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0.0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0j&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Decimal(0)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Fraction(0, 1)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Empty sequences and collections:&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&#39;&#39;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;[]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;{}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;set()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;range(0)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All other items are &lt;code&gt;True&lt;/code&gt;. This means that an empty list &lt;code&gt;[]&lt;/code&gt; is &lt;code&gt;False&lt;/code&gt;, while a list containing only zero &lt;code&gt;[0]&lt;/code&gt; is still &lt;code&gt;True&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Most objects will evaluate to &lt;code&gt;True&lt;/code&gt;, unless the object has &lt;code&gt;__bool__()&lt;/code&gt; which returns &lt;code&gt;False&lt;/code&gt; or &lt;code&gt;__len__()&lt;/code&gt; which returns 0. This allows you to extend your custom classes to act as Boolean expressions.&lt;/p&gt;
&lt;p&gt;Python has a few slight changes from C++ in the Boolean operators as well. For starters, &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; statements do not require the surrounding parentheses as they do in C++. Parentheses can aid in readability, however, so use your best judgment.&lt;/p&gt;
&lt;p&gt;Most C++ Boolean operators have similar operators in Python:&lt;/p&gt;
&lt;div class=&quot;table-responsive&quot;&gt;
&lt;table class=&quot;table table-hover&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;C++ Operator&lt;/th&gt;
&lt;th&gt;Python Operator&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;and&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;||&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;or&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;!&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;not&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;|&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;|&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;Most of the operators are similar to C++, but if you want to brush up you can read &lt;a href=&quot;https://realpython.com/python-operators-expressions/&quot;&gt;Operators and Expressions in Python&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;variables-and-pointers&quot;&gt;Variables and Pointers&lt;/h3&gt;
&lt;p&gt;When you first start using Python after writing in C++, you might not give variables much thought. They seem to generally work as they do in C++. However, they&amp;rsquo;re not the same. Whereas in C++ you use variables to reference values, in Python you use names.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; For this section, where you&amp;rsquo;re looking at variables and names in Python vs C++, you&amp;rsquo;ll use &lt;strong&gt;variables&lt;/strong&gt; for C++ and &lt;strong&gt;names&lt;/strong&gt; for Python. Elsewhere, they will both be called &lt;strong&gt;variables&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;First, let&amp;rsquo;s back up a bit and take a broader look at Python&amp;rsquo;s &lt;a href=&quot;https://docs.python.org/3.6/reference/datamodel.html&quot;&gt;object model&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In Python, &lt;em&gt;everything&lt;/em&gt; is an object. Numbers are held in objects. Modules are held in objects. Both the object of a class &lt;em&gt;and&lt;/em&gt; the class itself are objects. Functions are also objects:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_list_object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_list_object&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_class_object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_class_object&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;class &amp;#39;list&amp;#39;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sayHi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;     &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_function_object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sayHi&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_function_object&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;function sayHi at 0x7faa326ac048&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Calling &lt;code&gt;list()&lt;/code&gt; creates a new list object, which you assign to &lt;code&gt;a_list_object&lt;/code&gt;. Using the name of the class &lt;code&gt;list&lt;/code&gt; by itself places a label on the class object. You can place a new label on a function as well. This is a powerful tool and, like all powerful tools, it can be dangerous. (I&amp;rsquo;m looking at you, Mr. Chainsaw.)&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The code above is shown running in a &lt;a href=&quot;https://realpython.com/interacting-with-python/&quot;&gt;REPL&lt;/a&gt;, which stands for &amp;ldquo;Read, Eval, Print Loop.&amp;rdquo; This interactive environment is used frequently to try out ideas in Python and other interpreted languages. &lt;/p&gt;
&lt;p&gt;If you type &lt;code&gt;python&lt;/code&gt; at a command prompt, then it will bring up a REPL where you can start typing in code and trying things out for yourself!&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Moving back to the Python vs C++ discussion, note that this is behavior is different from what you&amp;rsquo;ll see in C++. Unlike Python, C++ has variables that are assigned to a memory location, and you must indicate how much memory that variable will use:&lt;/p&gt;
&lt;div class=&quot;highlight cpp&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;an_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a_big_array_of_floats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REALLY_BIG_NUMBER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In Python, all objects are created in memory, and you apply labels to them. The labels themselves don&amp;rsquo;t have types, and they can be put on any type of object:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_flexible_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_flexible_name&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_flexible_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;This is a string&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_flexible_name&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;This is a string&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_flexible_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;more info&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;3.26&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_flexible_name&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[3, &amp;#39;more info&amp;#39;, 3.26]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_flexible_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_flexible_name&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;built-in function print&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can assign &lt;code&gt;my_flexible_name&lt;/code&gt; to any type of object, and Python will just roll with it.&lt;/p&gt;
&lt;p&gt;When you&amp;rsquo;re comparing Python vs C++, the difference in variables vs names can be a bit confusing, but it comes with some excellent benefits. One is that in Python you don&amp;rsquo;t have &lt;a href=&quot;https://realpython.com/pointers-in-python/&quot;&gt;pointers&lt;/a&gt;, and you never need to think about heap vs stack issues. You&amp;rsquo;ll dive into memory management a bit later in this article.&lt;/p&gt;
&lt;h3 id=&quot;comprehensions&quot;&gt;Comprehensions&lt;/h3&gt;
&lt;p&gt;Python has a language feature called &lt;a href=&quot;https://realpython.com/courses/using-list-comprehensions-effectively/&quot;&gt;&lt;strong&gt;list comprehensions&lt;/strong&gt;&lt;/a&gt;. While it&amp;rsquo;s possible to emulate list comprehensions in C++, it&amp;rsquo;s fairly tricky. In Python, they&amp;rsquo;re a basic tool that&amp;rsquo;s taught to beginning programmers.  &lt;/p&gt;
&lt;p&gt;One way of thinking about list comprehensions is that they&amp;rsquo;re like a super-charged initializer for lists, dicts, or sets. Given one iterable object, you can create a list, and filter or modify the original as you do so:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[0, 1, 4, 9, 16]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This script starts with the iterable &lt;code&gt;range(5)&lt;/code&gt; and creates a list that contains the square for each item in the iterable.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s possible to add conditions to the values in the first iterable:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;odd_squares&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;odd_squares&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[1, 9]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;if x % 2&lt;/code&gt; at the end of this comprehension limits the numbers used from &lt;code&gt;range(5)&lt;/code&gt; to only the odd ones.&lt;/p&gt;
&lt;p&gt;At this point you might be having two thoughts:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;That&amp;rsquo;s a powerful syntax trick that will simplify some parts of my code.&lt;/li&gt;
&lt;li&gt;You can do the same thing in C++.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;While it&amp;rsquo;s true that you can create a &lt;code&gt;vector&lt;/code&gt; of the squares of the odd numbers in C++, doing so usually means a little more code:&lt;/p&gt;
&lt;div class=&quot;highlight cpp&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;odd_squares&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ii&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ii&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ii&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ii&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;odd_squares&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ii&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For developers coming from C-style languages, list comprehensions are one of the first noticeable ways they can &lt;a href=&quot;https://realpython.com/learning-paths/writing-pythonic-code/&quot;&gt;write more Pythonic code&lt;/a&gt;. Many developers start writing Python with C++ structure:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;odd_squares&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ii&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ii&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;odd_squares&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ii&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is perfectly valid Python. It will likely run more slowly, however, and it&amp;rsquo;s not as clear and concise as the list comprehension. Learning to use list comprehensions will not only speed up your code, but it will also make your code more Pythonic and easier to read!&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you&amp;rsquo;re reading about Python, you&amp;rsquo;ll frequently see the word &lt;strong&gt;Pythonic&lt;/strong&gt; used to describe something. This is just a term the community uses to describe code that is clean, elegant and looks like it was written by a Python Jedi.&lt;/p&gt;
&lt;/div&gt;
&lt;h3 id=&quot;pythons-stdalgorithms&quot;&gt;Python&amp;rsquo;s &lt;code&gt;std::algorithms&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;C++ has a rich set of algorithms built into the standard library. Python has a similar set of built-in functions that cover the same ground.&lt;/p&gt;
&lt;p&gt;The first and most powerful of these is the &lt;a href=&quot;https://www.pythoncentral.io/quick-tip-using-pythons-in-operator/&quot;&gt;&lt;code&gt;in&lt;/code&gt; operator&lt;/a&gt;, which provides a quite readable test to see if an item is included in a &lt;a href=&quot;https://realpython.com/python-lists-tuples/&quot;&gt;list&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/python-sets/&quot;&gt;set&lt;/a&gt;, or &lt;a href=&quot;https://realpython.com/courses/dictionaries-python/&quot;&gt;dictionary&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;193&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Jim&amp;#39;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;gray&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Zoe&amp;#39;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;blond&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;David&amp;#39;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;brown&amp;#39;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Jim&amp;#39;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Fred&amp;#39;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;gray&amp;#39;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;False&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that the &lt;code&gt;in&lt;/code&gt; operator, when used on dictionaries, only tests for keys, not values. This is shown by the final test, &lt;code&gt;&#39;gray&#39; in y&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;in&lt;/code&gt; can be combined with &lt;code&gt;not&lt;/code&gt; for quite readable syntax:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{name}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; not found&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Next up in your parade of Python built-in operators is &lt;a href=&quot;https://docs.python.org/3.7/library/functions.html?any#any&quot;&gt;&lt;code&gt;any&lt;/code&gt;&lt;/a&gt;. This is a boolean function that returns &lt;code&gt;True&lt;/code&gt; if any element of the given iterable evaluates to &lt;code&gt;True&lt;/code&gt;. This can seem a little silly until you remember your list comprehensions! Combining these two can produce powerful, clear syntax for many situations:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_big_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;23&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;875&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_small_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_big_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_small_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;True&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Finally, you have &lt;a href=&quot;https://docs.python.org/3.7/library/functions.html?all#all&quot;&gt;&lt;code&gt;all&lt;/code&gt;&lt;/a&gt;, which is similar to &lt;code&gt;any&lt;/code&gt;. This returns &lt;code&gt;True&lt;/code&gt; &lt;em&gt;only&lt;/em&gt; if&amp;mdash;you guessed it&amp;mdash;&lt;em&gt;all&lt;/em&gt; of the elements in the iterable are &lt;code&gt;True&lt;/code&gt;. Again, combining this with list comprehensions produces a powerful language feature:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list_a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list_b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list_b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;True&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;any&lt;/code&gt; and &lt;code&gt;all&lt;/code&gt; can cover much of the same ground where C++ developers would look to &lt;code&gt;std::find&lt;/code&gt; or &lt;code&gt;std::find_if&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In the &lt;code&gt;any&lt;/code&gt; and &lt;code&gt;all&lt;/code&gt; examples above, you can remove the brackets (&lt;code&gt;[]&lt;/code&gt;) without any loss of functionality. (for example: &lt;code&gt;all(x % 2 for x in list_a)&lt;/code&gt;) This makes use of &lt;a href=&quot;https://realpython.com/introduction-to-python-generators/&quot;&gt;generator expressions&lt;/a&gt; which, while quite handy, are beyond the scope of this article.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Before moving on to variable typing, let&amp;rsquo;s update your Python vs C++ comparison chart:&lt;/p&gt;
&lt;div class=&quot;table-responsive&quot;&gt;
&lt;table class=&quot;table table-hover&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Python&lt;/th&gt;
&lt;th&gt;C++&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Faster Execution&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cross-Platform Execution&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Single-Type Variables&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Multiple-Type Variables&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Comprehensions&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Rich Set of Built-In Algorithms&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;Okay, now you&amp;rsquo;re ready to look at variable and parameter typing. Let&amp;rsquo;s go!&lt;/p&gt;
&lt;h2 id=&quot;static-vs-dynamic-typing&quot;&gt;Static vs Dynamic Typing&lt;/h2&gt;
&lt;p&gt;Another large topic when you&amp;rsquo;re comparing Python vs C++ is the use of data types. C++ is a statically typed language, while Python is dynamically typed. Let&amp;rsquo;s explore what that means.&lt;/p&gt;
&lt;h3 id=&quot;static-typing&quot;&gt;Static Typing&lt;/h3&gt;
&lt;p&gt;C++ is statically typed, which means that each variable you use in your code must have a specific data type like &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;float&lt;/code&gt;, and so forth. You can only assign values of the correct type to a variable, unless you jump through some hoops.&lt;/p&gt;
&lt;p&gt;This has some advantages for both the developer and the compiler. The developer gains the advantage of knowing what the type of a particular variable is ahead of time, and therefore which operations are allowed. The compiler can use the type information to optimize the code, making it smaller, faster, or both.&lt;/p&gt;
&lt;p&gt;This advance knowledge comes at a cost, however. The parameters passed into a function must match the type expected by the function, which can reduce the flexibility and potential usefulness of the code.&lt;/p&gt;
&lt;h3 id=&quot;duck-typing&quot;&gt;Duck Typing&lt;/h3&gt;
&lt;p&gt;Dynamic typing is frequently referred to as &lt;strong&gt;duck typing.&lt;/strong&gt; It&amp;rsquo;s an odd name, and you&amp;rsquo;ll read more about that in just a minute! But first, let&amp;rsquo;s start with an example. This function takes a file object and reads the first ten lines:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;read_ten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_like_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line_number&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_like_object&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{line_number}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; = {x.strip()}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To use this function, you&amp;rsquo;ll create a file object and pass it in:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;types.py&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;read_ten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This shows how the basic design of the function works. While this function was described as &amp;ldquo;reading the first ten lines from a file object,&amp;rdquo; there is nothing in Python that requires that &lt;code&gt;file_like_object&lt;/code&gt; &lt;em&gt;be&lt;/em&gt; a file. As long as the object passed in supports &lt;code&gt;.readline()&lt;/code&gt;, the object can be of any type:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Duck&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;quack&amp;quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;my_duck&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Duck&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;read_ten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_duck&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Calling &lt;code&gt;read_ten()&lt;/code&gt; with a &lt;code&gt;Duck&lt;/code&gt; object produces:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;0 = quack&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;1 = quack&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;2 = quack&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;3 = quack&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;4 = quack&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;5 = quack&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;6 = quack&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;7 = quack&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;8 = quack&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;9 = quack&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is the essence of &lt;strong&gt;duck typing&lt;/strong&gt;. The saying goes, &amp;ldquo;If it looks like a duck, swims like a duck, and quacks like a duck, then it probably &lt;em&gt;is&lt;/em&gt; a duck.&amp;rdquo; &lt;/p&gt;
&lt;p&gt;In other words, if the object has the needed methods, then it&amp;rsquo;s acceptable to pass it in, regardless of the object&amp;rsquo;s type. Duck or dynamic typing gives you an enormous amount of flexibility, as it allows any type to be used where it meets the required interfaces.&lt;/p&gt;
&lt;p&gt;However, there is a problem here. What happens if you pass in an object that &lt;em&gt;doesn&amp;rsquo;t&lt;/em&gt; meet the required interface? For example, what if you pass in a number to &lt;code&gt;read_ten()&lt;/code&gt;, like this: &lt;code&gt;read_ten(3)&lt;/code&gt;?&lt;/p&gt;
&lt;p&gt;This results in an exception being thrown. Unless you catch the exception, your program will blow up with a &lt;a href=&quot;https://realpython.com/python-traceback/&quot;&gt;traceback&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&amp;quot;duck_test.py&amp;quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;4&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;read_ten&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file_like_object&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;gr&quot;&gt;AttributeError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;&amp;#39;int&amp;#39; object has no attribute &amp;#39;readline&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Dynamic typing can be quite a powerful tool, but as you can see, you must use caution when employing it.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Python and C++ are both considered &lt;a href=&quot;https://stackoverflow.com/a/11328980&quot;&gt;strongly typed&lt;/a&gt; languages. Although C++ has a stronger type system, the details of this are generally not significant to someone learning Python.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Let&amp;rsquo;s move on to a feature that benefits from Python&amp;rsquo;s dynamic typing: templates.&lt;/p&gt;
&lt;h3 id=&quot;templates&quot;&gt;Templates&lt;/h3&gt;
&lt;p&gt;Python doesn&amp;rsquo;t have templates like C++, but it generally doesn&amp;rsquo;t need them. In Python, everything is a subclass of a single base type. This is what allows you to create duck typing functions like the ones above.&lt;/p&gt;
&lt;p&gt;The templating system in C++ allows you to create functions or algorithms that operate on multiple different types. This is quite powerful and can save you significant time and effort. However, it can also be a source of confusion and frustration, as compiler errors in templates can leave you baffled.&lt;/p&gt;
&lt;p&gt;Being able to use duck typing instead of templates makes some things much easier. But this, too, can cause hard-to-detect issues. As in all complex decisions, there are trade-offs when you&amp;rsquo;re comparing Python vs C++.&lt;/p&gt;
&lt;h3 id=&quot;type-checking&quot;&gt;Type Checking&lt;/h3&gt;
&lt;p&gt;There&amp;rsquo;s been a lot of interest and discussion in the Python community lately about static type checking in Python. Projects like &lt;a href=&quot;http://mypy-lang.org/&quot;&gt;mypy&lt;/a&gt; have raised the possibility of adding pre-runtime type checking to specific spots in the language. This can be quite useful in managing interfaces between portions of large packages or specific APIs. &lt;/p&gt;
&lt;p&gt;It helps to address one of the downsides of duck typing. For developers using a function, it helps if they can fully understand what each parameter needs to be. This can be useful on large project teams where many developers need to communicate through APIs.&lt;/p&gt;
&lt;p&gt;Once again, let&amp;rsquo;s take a look at your Python vs C++ comparison chart:&lt;/p&gt;
&lt;div class=&quot;table-responsive&quot;&gt;
&lt;table class=&quot;table table-hover&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Python&lt;/th&gt;
&lt;th&gt;C++&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Faster Execution&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cross-Platform Execution&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Single-Type Variables&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiple-Type Variables&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Comprehensions&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rich Set of Built-In Algorithms&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Static Typing&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Dynamic Typing&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;Now you&amp;rsquo;re ready to move on to differences in object-oriented programming.&lt;/p&gt;
&lt;h2 id=&quot;object-oriented-programming&quot;&gt;Object-Oriented Programming&lt;/h2&gt;
&lt;p&gt;Like C++, Python supports an &lt;a href=&quot;https://realpython.com/courses/intro-object-oriented-programming-oop-python/&quot;&gt;object-oriented programming model&lt;/a&gt;. Many of the same concepts you learned in C++ carry over into Python. You&amp;rsquo;ll still need to make decisions about &lt;a href=&quot;https://realpython.com/inheritance-composition-python/&quot;&gt;inheritance, composition&lt;/a&gt;, and multiple inheritance.&lt;/p&gt;
&lt;h3 id=&quot;similarities&quot;&gt;Similarities&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://realpython.com/python-super/&quot;&gt;Inheritance&lt;/a&gt; between classes works similarly in Python vs C++. A new class can inherit methods and attributes from one or more base classes, just like you&amp;rsquo;ve seen in C++. Some of the details are a bit different, however. &lt;/p&gt;
&lt;p&gt;Base classes in Python do not have their constructor called automatically like they do in C++. This can be confusing when you&amp;rsquo;re switching languages.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://realpython.com/python-super/#super-in-multiple-inheritance&quot;&gt;Multiple inheritance&lt;/a&gt; also works in Python, and it has just as many quirks and strange rules as it does in C++.&lt;/p&gt;
&lt;p&gt;Similarly, you can also use composition to build classes, where you have objects of one type hold other types. Considering everything is an object in Python, this means that classes can hold anything else in the language.&lt;/p&gt;
&lt;h3 id=&quot;differences&quot;&gt;Differences&lt;/h3&gt;
&lt;p&gt;There are some differences, however, when you&amp;rsquo;re comparing Python vs C++. The first two are related.&lt;/p&gt;
&lt;p&gt;The first difference is that Python has no concept of access modifiers for classes. Everything in a class object is public. The Python community has developed a convention that any member of a class starting with a single underscore is treated as private. This is in no way enforced by the language, but it seems to work out pretty well.&lt;/p&gt;
&lt;p&gt;The fact that every class member and method is public in Python leads to the second difference: Python has far weaker encapsulation support than C++. &lt;/p&gt;
&lt;p&gt;As mentioned, the single underscore convention makes this far less of an issue in practical codebases than it is in a theoretical sense. In general, any user that breaks this rule and depends on the internal workings of a class is asking for trouble.&lt;/p&gt;
&lt;h3 id=&quot;operator-overloads-vs-dunder-methods&quot;&gt;Operator Overloads vs Dunder Methods&lt;/h3&gt;
&lt;p&gt;In C++, you can add &lt;strong&gt;operator overloads&lt;/strong&gt;. These allow you to define the behavior of specific syntactical operators (like &lt;code&gt;==&lt;/code&gt;) for certain data types. Usually, this is used to add more natural usage of your classes. For the &lt;code&gt;==&lt;/code&gt; operator, you can define exactly what it means for two objects of a class to be equal.&lt;/p&gt;
&lt;p&gt;One difference that takes some developers a long time to grasp is how to work around the lack of operator overloads in Python. It&amp;rsquo;s great that Python&amp;rsquo;s objects all work in any of the standard containers, but what if you want the &lt;code&gt;==&lt;/code&gt; operator to do a deep comparison between two objects of your new class? In C++, you would create an &lt;code&gt;operator==()&lt;/code&gt; in your class and do the comparison.&lt;/p&gt;
&lt;p&gt;Python has a similar structure that&amp;rsquo;s used quite consistently across the language: &lt;strong&gt;dunder methods&lt;/strong&gt;. Dunder methods get their name because they all start and end with a double underscore, or &amp;ldquo;d-under.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;Many of the built-in functions that operate on objects in Python are handled by calls to that object&amp;rsquo;s &lt;a href=&quot;https://realpython.com/operator-function-overloading/&quot;&gt;dunder methods&lt;/a&gt;. For your example above, you can add &lt;code&gt;__eq__()&lt;/code&gt; to your class to do whatever fancy comparison you like:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyFancyComparisonClass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__eq__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This produces a class that compares the same way as any other instance of its class. Not particularly useful, but it demonstrates the point.&lt;/p&gt;
&lt;p&gt;There are a large number of dunder methods used in Python, and the built-in functions make use of them extensively. For example, adding &lt;code&gt;__lt__()&lt;/code&gt; will allow Python to compare the relative order of two of your objects. This means that not only will the &lt;code&gt;&amp;lt;&lt;/code&gt; operator now work, but that &lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;=&lt;/code&gt;, and &lt;code&gt;&amp;gt;=&lt;/code&gt; will also work as well.&lt;/p&gt;
&lt;p&gt;Even better, if you have several objects of your new class in a list, then you can use &lt;code&gt;sorted()&lt;/code&gt; on the list and they&amp;rsquo;ll be sorted using &lt;code&gt;__lt__()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Once again, let&amp;rsquo;s take a look at your Python vs C++ comparison chart:&lt;/p&gt;
&lt;div class=&quot;table-responsive&quot;&gt;
&lt;table class=&quot;table table-hover&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Python&lt;/th&gt;
&lt;th&gt;C++&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Faster Execution&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cross-Platform Execution&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Single-Type Variables&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiple-Type Variables&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Comprehensions&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rich Set of Built-In Algorithms&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Static Typing&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dynamic Typing&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Strict Encapsulation&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;Now that you&amp;rsquo;ve seen object-oriented coding across both languages, let&amp;rsquo;s look at how Python and C++ manage those objects in memory.&lt;/p&gt;
&lt;h2 id=&quot;memory-management&quot;&gt;Memory Management&lt;/h2&gt;
&lt;p&gt;One of the biggest differences, when you&amp;rsquo;re comparing Python vs C++, is how they handle memory. As you saw in the section about variables in C++ and Python&amp;rsquo;s names, Python does not have pointers, nor does it easily let you manipulate memory directly. While there are times when you want to have that level of control, most of the time it&amp;rsquo;s not necessary.&lt;/p&gt;
&lt;p&gt;Giving up direct control of memory locations brings a few benefits. You don&amp;rsquo;t need to worry about memory ownership, or making sure that memory is freed once (and only once) after it&amp;rsquo;s been allocated. You also never have to worry about whether or not an object was allocated on the stack or the heap, which tends to trip up beginning C++ developers.&lt;/p&gt;
&lt;p&gt;Python manages all of these issues for you. To do this everything in Python is a derived class from Python&amp;rsquo;s &lt;code&gt;object&lt;/code&gt;. This allows the Python interpreter to implement reference counting as a means of keeping track of which objects are still in use and which can be freed.&lt;/p&gt;
&lt;p&gt;This convenience comes at a price, of course. To free allocated memory objects for you, Python will occasionally need to run what is called a &lt;strong&gt;garbage collector&lt;/strong&gt;, which finds unused memory objects and frees them.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; CPython has a complex &lt;a href=&quot;https://realpython.com/python-memory-management/&quot;&gt;memory management scheme&lt;/a&gt;, which means that freeing memory doesn&amp;rsquo;t necessarily mean the memory gets returned to the operating system.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Python uses two tools to free memory: &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The reference counting collector &lt;/li&gt;
&lt;li&gt;The generational collector&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let&amp;rsquo;s look at each of these individually.&lt;/p&gt;
&lt;h3 id=&quot;reference-counting-collector&quot;&gt;Reference Counting Collector&lt;/h3&gt;
&lt;p&gt;The reference counting collector is fundamental to the standard Python interpreter and is always running. It works by keeping track of how many times a given block of memory (which is always a Python &lt;code&gt;object&lt;/code&gt;) has a name attached to it while your program is running. Many rules describe when the reference count is incremented or decremented, but an example of one case might clarify:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;A long string&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the above example, line 1 creates a new object containing the string &lt;code&gt;&quot;A long string&quot;&lt;/code&gt;. It then places the name &lt;code&gt;x&lt;/code&gt; on this object, increasing the object&amp;rsquo;s reference count to 1:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/cpp_ref_count_one.b70d0e13aa3b.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-33&quot; src=&quot;https://files.realpython.com/media/cpp_ref_count_one.b70d0e13aa3b.png&quot; width=&quot;1083&quot; height=&quot;423&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/cpp_ref_count_one.b70d0e13aa3b.png&amp;amp;w=270&amp;amp;sig=ac9db469ce090231a1574e4f755b72c7b1cbfe1a 270w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/cpp_ref_count_one.b70d0e13aa3b.png&amp;amp;w=541&amp;amp;sig=99b17ede52a12cf46bab2b90949e903779eb4d82 541w, https://files.realpython.com/media/cpp_ref_count_one.b70d0e13aa3b.png 1083w&quot; sizes=&quot;75vw&quot; alt=&quot;A Python object with reference count of one.&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;On line 2 it assigns &lt;code&gt;y&lt;/code&gt; to name the same object, which will increase the reference count to 2:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/cpp_ref_count_two.199793fc702e.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-33&quot; src=&quot;https://files.realpython.com/media/cpp_ref_count_two.199793fc702e.png&quot; width=&quot;1083&quot; height=&quot;663&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/cpp_ref_count_two.199793fc702e.png&amp;amp;w=270&amp;amp;sig=ec05c2b3594b82072f8a5eb00a8a48cf4ce8f85e 270w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/cpp_ref_count_two.199793fc702e.png&amp;amp;w=541&amp;amp;sig=7c70f6c76c862d08757a7a59a18366e336341250 541w, https://files.realpython.com/media/cpp_ref_count_two.199793fc702e.png 1083w&quot; sizes=&quot;75vw&quot; alt=&quot;A Python object with reference count of two.&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When you call &lt;code&gt;del&lt;/code&gt; with &lt;code&gt;x&lt;/code&gt; in line 3, you&amp;rsquo;re removing one of the references to the object, dropping the count back to 1:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/cpp_ref_count_one_one.47037ceb7f95.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-33&quot; src=&quot;https://files.realpython.com/media/cpp_ref_count_one_one.47037ceb7f95.png&quot; width=&quot;1083&quot; height=&quot;936&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/cpp_ref_count_one_one.47037ceb7f95.png&amp;amp;w=270&amp;amp;sig=84975043d6c8f09a0954ceb440954bdd9722a79d 270w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/cpp_ref_count_one_one.47037ceb7f95.png&amp;amp;w=541&amp;amp;sig=79518a340b46d37283250339bfa36b853fc65060 541w, https://files.realpython.com/media/cpp_ref_count_one_one.47037ceb7f95.png 1083w&quot; sizes=&quot;75vw&quot; alt=&quot;Two Python objects, each with reference count of one.&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Finally, when you remove &lt;code&gt;y&lt;/code&gt;, the final reference to the object, its reference count drops to zero and it can be freed by the reference counting garbage collector. It may or may not be freed immediately at this point, but generally, that shouldn&amp;rsquo;t matter to the developer:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/cpp_ref_count_zero_two.e3dff58e94b0.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-33&quot; src=&quot;https://files.realpython.com/media/cpp_ref_count_zero_two.e3dff58e94b0.png&quot; width=&quot;1083&quot; height=&quot;933&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/cpp_ref_count_zero_two.e3dff58e94b0.png&amp;amp;w=270&amp;amp;sig=9a72e29c9fba7d1bb35de32206de9c8d812ddf2c 270w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/cpp_ref_count_zero_two.e3dff58e94b0.png&amp;amp;w=541&amp;amp;sig=d2943e2c66f609c3a30d507ef877f22125b6d34e 541w, https://files.realpython.com/media/cpp_ref_count_zero_two.e3dff58e94b0.png 1083w&quot; sizes=&quot;75vw&quot; alt=&quot;The Python None object with reference count of two and another Python object with reference count of zero.&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;While this will take care of finding and freeing many of the objects that need to be freed, there are a few situations it will not catch. For that, you need the generational garbage collector.&lt;/p&gt;
&lt;h3 id=&quot;generational-garbage-collector&quot;&gt;Generational Garbage Collector&lt;/h3&gt;
&lt;p&gt;One of the big holes in the reference counting scheme is that your program can build a cycle of references, where object &lt;code&gt;A&lt;/code&gt; has a reference to object &lt;code&gt;B&lt;/code&gt;, which has a reference back to object &lt;code&gt;A&lt;/code&gt;. It&amp;rsquo;s entirely possible to hit this situation and have nothing in your code referring to either object. In this case, neither of the objects will ever hit a reference count of 0.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;generational garbage collector&lt;/strong&gt; involves a complex algorithm that is beyond the scope of this article, but it will find some of these orphaned reference cycles and free them for you. It runs on an occasional basis controlled by settings described &lt;a href=&quot;https://docs.python.org/3.7/library/gc.html&quot;&gt;in the documentation&lt;/a&gt;. One of these parameters is to disable this garbage collector entirely.&lt;/p&gt;
&lt;h3 id=&quot;when-you-dont-want-garbage-collection&quot;&gt;When You Don&amp;rsquo;t Want Garbage Collection&lt;/h3&gt;
&lt;p&gt;When you&amp;rsquo;re comparing Python vs C++, as when you&amp;rsquo;re comparing any two tools, each advantage comes with a trade-off. Python doesn&amp;rsquo;t require explicit memory management, but occasionally it will spend a longer amount of time than expected on garbage collection. The inverse is true for C++: your program will have consistent response times, but you&amp;rsquo;ll need to expend more effort in managing memory.&lt;/p&gt;
&lt;p&gt;In many programs the occasional garbage collection hit is unimportant. If you&amp;rsquo;re writing a script that only runs for 10 seconds, then you&amp;rsquo;re unlikely to notice the difference. Some situations, however, require consistent response times. Real-time systems are a great example, where responding to a piece of hardware in a fixed amount of time can be essential to the proper operation of your system.&lt;/p&gt;
&lt;p&gt;Systems with hard real-time requirements are some of the systems for which Python is a poor language choice. Having a tightly controlled system where you&amp;rsquo;re certain of the timing is a good use of C++. These are the types of issues to consider when you&amp;rsquo;re deciding on the language for a project.&lt;/p&gt;
&lt;p&gt;Time to update your Python vs C++ chart:&lt;/p&gt;
&lt;div class=&quot;table-responsive&quot;&gt;
&lt;table class=&quot;table table-hover&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Python&lt;/th&gt;
&lt;th&gt;C++&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Faster Execution&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cross-Platform Execution&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Single-Type Variables&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiple-Type Variables&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Comprehensions&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rich Set of Built-In Algorithms&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Static Typing&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dynamic Typing&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Strict Encapsulation&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Direct Memory Control&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Garbage Collection&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;h2 id=&quot;threading-multiprocessing-and-async-io&quot;&gt;Threading, Multiprocessing, and Async IO&lt;/h2&gt;
&lt;p&gt;The concurrency models in C++ and Python are similar, but they have different results and benefits. Both languages have support for threading, multiprocessing, and Async IO operations. Let&amp;rsquo;s look at each of these.&lt;/p&gt;
&lt;h3 id=&quot;threading&quot;&gt;Threading&lt;/h3&gt;
&lt;p&gt;While both C++ and Python have threading built into the language, the results can be markedly different, depending on the problem you&amp;rsquo;re solving. Frequently, &lt;a href=&quot;https://realpython.com/intro-to-python-threading/&quot;&gt;threading&lt;/a&gt; is used to address performance problems. In C++, threading can provide a general speed-up for both computationally bound and I/O bound problems, as threads can take full advantage of the cores on a multiprocessor system.&lt;/p&gt;
&lt;p&gt;Python, on the other hand, has made a design trade-off to use the &lt;strong&gt;Global Interpreter Lock&lt;/strong&gt;, or the &lt;a href=&quot;https://realpython.com/python-gil/&quot;&gt;GIL&lt;/a&gt;, to simplify its threading implementation. There are many benefits to the GIL, but the drawback is that only one thread will be running at a single time, even if there are multiple cores.&lt;/p&gt;
&lt;p&gt;If your problem is I/O bound, like fetching several web pages at once, then this limitation will not bother you in the least. You&amp;rsquo;ll appreciate Python&amp;rsquo;s easier threading model and built-in methods for &lt;a href=&quot;https://docs.python.org/3/library/queue.html&quot;&gt;inter-thread communications&lt;/a&gt;. If your problem is CPU-bound, however, then the GIL will restrict your performance to that of a single processor. Fortunately, Python&amp;rsquo;s multiprocessing library has a similar interface to its threading library.&lt;/p&gt;
&lt;h3 id=&quot;multiprocessing&quot;&gt;Multiprocessing&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://realpython.com/python-concurrency/&quot;&gt;Multiprocessing&lt;/a&gt; support in Python is built into the standard library. It has a clean interface that allows you to spin up multiple processes and share information between them. You can create a pool of processes and spread work across them using several techniques. &lt;/p&gt;
&lt;p&gt;While Python still uses similar OS primitives to create the new processes, much of the low-level complication is hidden from the developer.&lt;/p&gt;
&lt;p&gt;C++ relies on &lt;code&gt;fork()&lt;/code&gt; to provide multiprocessing support. While this gives you direct access to all of the controls and issues of spawning multiple processes, it&amp;rsquo;s also much more complex.&lt;/p&gt;
&lt;h3 id=&quot;async-io&quot;&gt;Async IO&lt;/h3&gt;
&lt;p&gt;While both Python and C++ support Async IO routines, they&amp;rsquo;re handled differently. In C++, the &lt;code&gt;std::async&lt;/code&gt; methods are likely to use threading to achieve the Async IO nature of their operations. In Python, &lt;a href=&quot;https://realpython.com/async-io-python/&quot;&gt;Async IO&lt;/a&gt; code will only run on a single thread.&lt;/p&gt;
&lt;p&gt;There are trade-offs here as well. Using separate threads allows the C++ Async IO code to perform faster on computationally bound problems. The Python tasks used in its Async IO implementation are more lightweight, so it&amp;rsquo;s faster to spin up a large number of them to handle I/O bound issues.&lt;/p&gt;
&lt;p&gt;Your Python vs C++ comparison chart remains unchanged for this section. Both languages support a full range of concurrency options, with varying trade-offs between speed and convenience. &lt;/p&gt;
&lt;h2 id=&quot;miscellaneous-issues&quot;&gt;Miscellaneous Issues&lt;/h2&gt;
&lt;p&gt;If you&amp;rsquo;re comparing Python vs C++ and looking at adding Python to your toolbelt, then there are a few other things to consider. While your current editor or IDE will certainly work for Python, you might want to add certain extensions or language packs. It&amp;rsquo;s also worth giving &lt;a href=&quot;https://www.jetbrains.com/pycharm/&quot;&gt;PyCharm&lt;/a&gt; a look, as it&amp;rsquo;s Python-specific.&lt;/p&gt;
&lt;p&gt;Several C++ projects have Python bindings. Things like &lt;a href=&quot;https://www.qt.io/qt-for-python&quot;&gt;Qt&lt;/a&gt;, &lt;a href=&quot;https://docs.wxwidgets.org/3.0/overview_python.html&quot;&gt;WxWidgets&lt;/a&gt;, and many messaging APIs having multiple-language bindings.&lt;/p&gt;
&lt;p&gt;If you want to &lt;a href=&quot;https://www.codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I&quot;&gt;embed Python in C++&lt;/a&gt;, then you can use the &lt;a href=&quot;https://docs.python.org/2/extending/embedding.html&quot;&gt;Python/C API&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Finally, there are several methods for using your C++ skills to extend Python and add functionality, or to call your existing C++ libraries from within your Python code. Tools like &lt;a href=&quot;https://dbader.org/blog/python-ctypes-tutorial&quot;&gt;CTypes&lt;/a&gt;, &lt;a href=&quot;https://cython.org/&quot;&gt;Cython&lt;/a&gt;, &lt;a href=&quot;https://dbader.org/blog/python-cffi&quot;&gt;CFFI&lt;/a&gt;, &lt;a href=&quot;https://www.boost.org/doc/libs/1_70_0/libs/python/doc/html/index.html&quot;&gt;Boost.Python&lt;/a&gt; and &lt;a href=&quot;http://www.swig.org/Doc3.0/Python.html&quot;&gt;Swig&lt;/a&gt; can help you combine these languages and use each for what it&amp;rsquo;s best at.&lt;/p&gt;
&lt;h2 id=&quot;summary-python-vs-c&quot;&gt;Summary: Python vs C++&lt;/h2&gt;
&lt;p&gt;You&amp;rsquo;ve spent some time reading and thinking about the differences between Python vs C++. While Python has easier syntax and fewer sharp edges, it&amp;rsquo;s not a perfect fit for all problems. You&amp;rsquo;ve looked at the syntax, memory management, processing, and several other aspects of these two languages.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s take a final look at your Python vs C++ comparison chart:&lt;/p&gt;
&lt;div class=&quot;table-responsive&quot;&gt;
&lt;table class=&quot;table table-hover&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Python&lt;/th&gt;
&lt;th&gt;C++&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Faster Execution&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cross-Platform Execution&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Single-Type Variables&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiple-Type Variables&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Comprehensions&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rich Set of Built-In Algorithms&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Static Typing&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dynamic Typing&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Strict Encapsulation&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Direct Memory Control&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Garbage Collection&lt;/td&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;If you&amp;rsquo;re comparing Python vs C++, then you can see from your chart that this is not a case where one is better than the other. Each of them is a tool that&amp;rsquo;s well crafted for various use cases. Just like you don&amp;rsquo;t use a hammer for driving screws, using the right language for the job will make your life easier!&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Congrats! You&amp;rsquo;ve now seen some of the strengths and weaknesses of both Python and C++. You&amp;rsquo;ve learned some of the features of each language and how they are similar.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ve seen that C++ is great when you want:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Fast execution speed (potentially at the cost of development speed)&lt;/li&gt;
&lt;li&gt;Complete control of memory&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Conversely, Python is great when you want:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Fast development speed (potentially at the cost of execution speed)&lt;/li&gt;
&lt;li&gt;Managed memory&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You&amp;rsquo;re now ready to make a wise language choice when it comes to your next project!&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Absolute vs Relative Imports in Python</title>
      <id>https://realpython.com/courses/absolute-vs-relative-imports-python/</id>
      <link href="https://realpython.com/courses/absolute-vs-relative-imports-python/"/>
      <updated>2019-09-10T14:00:00+00:00</updated>
      <summary>If you’ve worked on a Python project that has more than one file, chances are you’ve had to use an import statement before. In this course, you’ll not only cover the pros and cons of absolute and relative imports but also learn about the best practices for writing import statements.</summary>
      <content type="html">
        &lt;p&gt;If you&amp;rsquo;ve worked on a Python project that has more than one file, chances are you&amp;rsquo;ve had to use an &lt;code&gt;import&lt;/code&gt; statement before. Even for Pythonistas with a couple of projects under their belt, imports can be confusing! &lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re reading this because you&amp;rsquo;d like to gain a deeper understanding of imports in Python, particularly &lt;strong&gt;absolute&lt;/strong&gt; and &lt;strong&gt;relative imports&lt;/strong&gt;, then you&amp;rsquo;ve come to the right place! In this tutorial, you&amp;rsquo;ll learn the differences between the two, as well as their pros and cons.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>MATLAB vs Python: Why and How to Make the Switch</title>
      <id>https://realpython.com/matlab-vs-python/</id>
      <link href="https://realpython.com/matlab-vs-python/"/>
      <updated>2019-09-09T14:00:00+00:00</updated>
      <summary>In this step-by-step tutorial, you&#39;ll learn about MATLAB vs Python, why you should switch from MATLAB to Python, the packages you&#39;ll need to make a smooth transition, and the bumps you&#39;ll most likely encounter along the way.</summary>
      <content type="html">
        &lt;p&gt;MATLAB® is widely known as a high-quality environment for any work that involves arrays, matrices, or linear algebra. Python is newer to this arena but is becoming increasingly popular for similar tasks. As you&amp;rsquo;ll see in this article, Python has all of the computational power of MATLAB for science tasks and makes it fast and easy to develop robust applications. However, there are some important differences when comparing MATLAB vs Python that you&amp;rsquo;ll need to learn about to effectively switch over.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this article, you&amp;rsquo;ll learn how to:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Evaluate the differences of using MATLAB vs Python&lt;/li&gt;
&lt;li&gt;Set up an environment for Python that duplicates the majority of MATLAB functions&lt;/li&gt;
&lt;li&gt;Convert scripts from MATLAB to Python&lt;/li&gt;
&lt;li&gt;Avoid common issues you might have when switching from MATLAB to Python&lt;/li&gt;
&lt;li&gt;Write code that looks and feels like Python&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;#&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-numpy-learning-guide&quot; data-focus=&quot;false&quot;&gt;Click here to get access to a free NumPy Resources Guide&lt;/a&gt; that points you to the best tutorials, videos, and books for improving your NumPy skills.&lt;/p&gt;&lt;/div&gt;

&lt;h2 id=&quot;matlab-vs-python-comparing-features-and-philosophy&quot;&gt;MATLAB vs Python: Comparing Features and Philosophy&lt;/h2&gt;
&lt;p&gt;Python is a high-level, general-purpose programming language designed for ease of use by human beings accomplishing all sorts of tasks. Python was created by Guido van Rossum and first released in the early 1990s. Python is a mature language developed by hundreds of collaborators around the world.&lt;/p&gt;
&lt;p&gt;Python is used by developers working on small, personal projects all the way up to some of the largest internet companies in the world. Not only does Python run &lt;a href=&quot;https://redditblog.com/2005/12/05/on-lisp/&quot;&gt;Reddit&lt;/a&gt; and &lt;a href=&quot;https://anvilventures.com/blog/looking-inside-the-box.html&quot;&gt;Dropbox&lt;/a&gt;, but the &lt;a href=&quot;https://stackoverflow.com/questions/2560310/heavy-usage-of-python-at-google/2561008#2561008&quot;&gt;original Google algorithm&lt;/a&gt; was written in Python. Also, the Python-based Django Framework runs &lt;a href=&quot;https://thenewstack.io/instagram-makes-smooth-move-python-3/&quot;&gt;Instagram&lt;/a&gt; and many other websites. On the science and engineering side, the data to create the &lt;a href=&quot;https://www.sciencenews.org/article/black-hole-first-picture-event-horizon-telescope&quot;&gt;2019 photo of a black hole&lt;/a&gt; was &lt;a href=&quot;https://github.com/achael/eht-imaging&quot;&gt;processed in Python&lt;/a&gt;, and major companies like &lt;a href=&quot;https://medium.com/netflix-techblog/python-at-netflix-86b6028b3b3e&quot;&gt;Netflix use Python&lt;/a&gt; in their data analytics work.&lt;/p&gt;
&lt;p&gt;There is also an important philosophical difference in the MATLAB vs Python comparison. &lt;strong&gt;MATLAB&lt;/strong&gt; is &lt;strong&gt;proprietary, closed-source&lt;/strong&gt; software. For most people, a license to use MATLAB is quite expensive, which means that if you have code in MATLAB, then only people who can afford a license will be able to run it. Plus, users are charged for each additional toolbox they want to install to extend the basic functionality of MATLAB. Aside from the cost, the MATLAB language is developed exclusively by Mathworks. If Mathworks were ever to go out of business, then MATLAB would no longer be able to be developed and might eventually stop functioning.&lt;/p&gt;
&lt;p&gt;On the other hand, &lt;strong&gt;Python&lt;/strong&gt; is &lt;strong&gt;free and open-source&lt;/strong&gt; software. Not only can you download Python at no cost, but you can also download, look at, and modify the source code as well. This is a big advantage for Python because it means that anyone can pick up the development of the language if the current developers were unable to continue for some reason.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re a researcher or scientist, then using open-source software has some pretty big benefits. &lt;a href=&quot;https://en.wikipedia.org/wiki/Paul_Romer&quot;&gt;Paul Romer&lt;/a&gt;, the 2018 Nobel Laureate in Economics, is a recent convert to Python. By his estimation, switching to open-source software in general, and Python in particular, brought greater integrity and accountability to his research. This was because all of the code could be shared and run by any interested reader. Prof. Romer wrote an excellent article, &lt;a href=&quot;https://paulromer.net/jupyter-mathematica-and-the-future-of-the-research-paper/&quot;&gt;Jupyter, Mathematica, and the Future of the Research Paper&lt;/a&gt;, about his experience with open-source software.&lt;/p&gt;
&lt;p&gt;Moreover, since Python is available at no cost, a much broader audience can use the code you develop. As you&amp;rsquo;ll see a little later on in the article, Python has an awesome community that can help you get started with the language and advance your knowledge. There are tens of thousands of tutorials, articles, and books all about Python software development. Here are a few to get you started:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/python-introduction/&quot;&gt;Introduction to Python 3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/learning-paths/python3-introduction/#&quot;&gt;Basic Data Types in Python&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/learning-paths/python3-introduction/&quot;&gt;Python 3 Basics Learning Path&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Plus, with so many developers in the community, there are hundreds of thousands of free packages to accomplish many of the tasks that you&amp;rsquo;ll want to do with Python. You&amp;rsquo;ll learn more about how to get these packages later on in this article.&lt;/p&gt;
&lt;p&gt;Like &lt;strong&gt;MATLAB&lt;/strong&gt;, &lt;strong&gt;Python&lt;/strong&gt; is an &lt;strong&gt;interpreted&lt;/strong&gt; language. This means that Python code can be ported between all of the major operating system platforms and CPU architectures out there, with only small changes required for different platforms. There are distributions of Python for desktop and laptop CPUs and microcontrollers like Adafruit. Python can also talk to other microcontrollers like Arduino with a simple programming interface that is almost identical no matter the host operating system.&lt;/p&gt;
&lt;p&gt;For all of these reasons, and many more, Python is an excellent choice to replace MATLAB as your programming language of choice. Now that you&amp;rsquo;re convinced to try out Python, read on to find out how to get it on your computer and how to switch from MATLAB!&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: &lt;a href=&quot;https://www.gnu.org/software/octave/&quot;&gt;GNU Octave&lt;/a&gt; is a free and open-source clone of MATLAB. In this sense, GNU Octave has the same philosophical advantages that Python has around code reproducibility and access to the software.&lt;/p&gt;
&lt;p&gt;Octave&amp;rsquo;s syntax is mostly compatible with MATLAB syntax, so it provides a short learning curve for MATLAB developers who want to use open-source software. However, Octave can&amp;rsquo;t match Python&amp;rsquo;s community or the number of different kinds of applications that Python can serve, so we definitely recommend you switch whole hog over to Python.&lt;/p&gt;
&lt;p&gt;Besides, this website is called &lt;em&gt;Real Python&lt;/em&gt;, not &lt;em&gt;Real Octave&lt;/em&gt; 😀&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;setting-up-your-environment-for-python&quot;&gt;Setting Up Your Environment for Python&lt;/h2&gt;
&lt;p&gt;In this section, you&amp;rsquo;ll learn:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;How to install Python on your computer for a seamless transition from MATLAB&lt;/li&gt;
&lt;li&gt;How to install replacements for the MATLAB integrated development environment&lt;/li&gt;
&lt;li&gt;How to use the replacements for MATLAB on your computer&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;getting-python-via-anaconda&quot;&gt;Getting Python via Anaconda&lt;/h3&gt;
&lt;p&gt;Python can be downloaded from a number of different sources, called &lt;strong&gt;distributions&lt;/strong&gt;. For instance, the Python that you can download from the &lt;a href=&quot;https://python.org&quot;&gt;official Python website&lt;/a&gt; is one distribution. Another very popular Python distribution, particularly for math, science, engineering, and data science applications, is the &lt;a href=&quot;https://anaconda.com&quot;&gt;Anaconda distribution&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;There are two main reasons that Anaconda is so popular:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Anaconda distributes pre-built packages for Windows, macOS, and Linux, which means that the installation process is really easy and the same for all three major platforms.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Anaconda includes all of the most popular packages for engineering and data science type workloads in one single installer.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For the purposes of creating an environment that is very similar to MATLAB, you should download and &lt;a href=&quot;https://anaconda.com/download&quot;&gt;install Anaconda&lt;/a&gt;. As of this writing, there are two major versions of Python available: Python 2 and Python 3. You should definitely install the version of Anaconda for Python 3, since Python 2 will not be supported past January 1, 2020. Python 3.7 is the most recent version at the time of this writing, but Python 3.8 should be out a few months after this article is published. Either 3.7 or 3.8 will work the same for you, so choose the most recent version you can.&lt;/p&gt;
&lt;p&gt;Once you have downloaded the Anaconda installer, you can follow the default set up procedures depending on your platform. You should install Anaconda in a directory that does not require administrator permission to modify, which is the default setting in the installer.&lt;/p&gt;
&lt;p&gt;With Anaconda installed, there are a few specific programs you should know about. The easiest way to launch applications is to use the Anaconda Navigator. On Windows, you can find this in the Start Menu and on macOS you can find it in Launchpad. Here&amp;rsquo;s a screenshot of the Anaconda Navigator on Windows:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/anaconda-navigator.bff710edb0a0.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/anaconda-navigator.bff710edb0a0.png&quot; width=&quot;1023&quot; height=&quot;761&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/anaconda-navigator.bff710edb0a0.png&amp;amp;w=255&amp;amp;sig=4e334e506ec75eef5dffa2b1873e7e70a8124776 255w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/anaconda-navigator.bff710edb0a0.png&amp;amp;w=511&amp;amp;sig=c179d42d0bfe9b4ab5db42446ae216cbdf21c615 511w, https://files.realpython.com/media/anaconda-navigator.bff710edb0a0.png 1023w&quot; sizes=&quot;75vw&quot; alt=&quot;Main Anaconda Navigator window showing installed applications&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the screenshot, you can see several installed applications, including &lt;strong&gt;JupyterLab&lt;/strong&gt;, &lt;strong&gt;Jupyter Notebook&lt;/strong&gt;, and &lt;strong&gt;Spyder&lt;/strong&gt;, that you&amp;rsquo;ll learn more about later in this tutorial.&lt;/p&gt;
&lt;p&gt;On Windows, there is one other application that you should know about. This is called &lt;strong&gt;Anaconda Prompt&lt;/strong&gt;, and it is a command prompt set up specifically to work with &lt;code&gt;conda&lt;/code&gt; on Windows. If you want to type &lt;code&gt;conda&lt;/code&gt; commands in a terminal, rather than using the Navigator GUI, then you should use Anaconda Prompt on Windows.&lt;/p&gt;
&lt;p&gt;On macOS, you can use any terminal application such as the default Terminal.app or iTerm2 to access &lt;code&gt;conda&lt;/code&gt; from the command line. On Linux, you can use the terminal emulator of your choice and which specific emulator is installed will depend on your Linux distribution.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Terminology Note:&lt;/strong&gt; You may be a little bit confused about &lt;code&gt;conda&lt;/code&gt; versus Anaconda. The distinction is subtle but important. &lt;strong&gt;Anaconda&lt;/strong&gt; is a distribution of Python that includes many of the necessary packages for scientific work of all kinds. &lt;strong&gt;&lt;code&gt;conda&lt;/code&gt;&lt;/strong&gt; is a cross-platform package management software that is included with the Anaconda distribution of Python. &lt;code&gt;conda&lt;/code&gt; is the software that you use to build, install, and remove packages within the Anaconda distribution.&lt;/p&gt;
&lt;p&gt;You can read all about how to use &lt;code&gt;conda&lt;/code&gt; in &lt;a href=&quot;https://realpython.com/python-windows-machine-learning-setup/&quot;&gt;Setting Up Python for Machine Learning on Windows&lt;/a&gt;. Although that tutorial focuses on Windows, the &lt;code&gt;conda&lt;/code&gt; commands are the same on Windows, macOS, and Linux.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Python also includes another way to install packages, called &lt;code&gt;pip&lt;/code&gt;. If you&amp;rsquo;re using Anaconda, you should always prefer to install packages using &lt;code&gt;conda&lt;/code&gt; whenever possible. Sometimes, though, a package is only available with &lt;code&gt;pip&lt;/code&gt;, and for those cases, you can read &lt;a href=&quot;https://realpython.com/what-is-pip/&quot;&gt;What Is Pip? A Guide for New Pythonistas&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;getting-an-integrated-development-environment&quot;&gt;Getting an Integrated Development Environment&lt;/h3&gt;
&lt;p&gt;One of the big advantages of MATLAB is that it includes a development environment with the software. This is the window that you&amp;rsquo;re most likely used to working in. There is a console in the center where you can type commands, a variable explorer on the right, and a directory listing on the left.&lt;/p&gt;
&lt;p&gt;Unlike MATLAB, Python itself does not have a default development environment. It is up to each user to find one that fits their needs. Fortunately, Anaconda comes with two different integrated development environments (IDEs) that are similar to the MATLAB IDE to make your switch seamless. These are called Spyder and JupyterLab. In the next two sections, you&amp;rsquo;ll see a detailed introduction to Spyder and a brief overview of JupyterLab.&lt;/p&gt;
&lt;h3 id=&quot;spyder&quot;&gt;Spyder&lt;/h3&gt;
&lt;p&gt;Spyder is an IDE for Python that is developed specifically for scientific Python work. One of the really nice things about Spyder is that it has a mode specifically designed for people like you who are converting from MATLAB to Python. You&amp;rsquo;ll see that a little later on.&lt;/p&gt;
&lt;p&gt;First, you should open Spyder. If you followed the instructions in the previous section, you can open Spyder using the Anaconda Navigator. Just find the Spyder icon and click the &lt;em&gt;Launch&lt;/em&gt; button. You can also launch Spyder from the Start Menu if you&amp;rsquo;re using Windows or from Launchpad if you&amp;rsquo;re using macOS.&lt;/p&gt;
&lt;h4 id=&quot;changing-the-default-window-layout-in-spyder&quot;&gt;Changing the Default Window Layout in Spyder&lt;/h4&gt;
&lt;p&gt;The default window in Spyder looks like the image below. This is for version 3.3.4 of Spyder running on Windows 10. It should look quite similar on macOS or Linux:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/spyder-default.0ad112e1eb06.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/spyder-default.0ad112e1eb06.png&quot; width=&quot;995&quot; height=&quot;772&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/spyder-default.0ad112e1eb06.png&amp;amp;w=248&amp;amp;sig=0839a0f3d02e0ac8130933cdab634d9734f01c69 248w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/spyder-default.0ad112e1eb06.png&amp;amp;w=497&amp;amp;sig=6def5d7ca1ff225d6c692aed90dd09fb3b089633 497w, https://files.realpython.com/media/spyder-default.0ad112e1eb06.png 995w&quot; sizes=&quot;75vw&quot; alt=&quot;Spyder IDE&amp;#39;s default view&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Before you take a tour of the user interface, you can make the interface look a little more like MATLAB. In the &lt;em&gt;View → Window layouts&lt;/em&gt; menu choose &lt;em&gt;MATLAB layout.&lt;/em&gt; That will change the window automatically so it has the same areas that you&amp;rsquo;re used to from MATLAB, annotated on the figure below:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/spyder-matlab-view.c9e6fbfcffe4.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/spyder-matlab-view.c9e6fbfcffe4.png&quot; width=&quot;989&quot; height=&quot;784&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/spyder-matlab-view.c9e6fbfcffe4.png&amp;amp;w=247&amp;amp;sig=548bc886f46177bc4d9635ebc75e81850fc6a14c 247w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/spyder-matlab-view.c9e6fbfcffe4.png&amp;amp;w=494&amp;amp;sig=82510255efbd739bfc6d1357e9f7428e9259b000 494w, https://files.realpython.com/media/spyder-matlab-view.c9e6fbfcffe4.png 989w&quot; sizes=&quot;75vw&quot; alt=&quot;Spyder IDE&amp;#39;s MATLAB like view&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the top left of the window is the &lt;em&gt;File Explorer&lt;/em&gt; or &lt;strong&gt;directory listing&lt;/strong&gt;. In this pane, you can find files that you want to edit or create new files and folders to work with.&lt;/p&gt;
&lt;p&gt;In the top center is a file editor. In this editor, you can work on Python scripts that you want to save to re-run later on. By default, the editor opens a file called &lt;code&gt;temp.py&lt;/code&gt; located in Spyder&amp;rsquo;s configuration directory. This file is meant as a temporary place to try things out before you save them in a file somewhere else on your computer.&lt;/p&gt;
&lt;p&gt;In the bottom center is the console. Like in MATLAB, the console is where you can run commands to see what they do or when you want to debug some code. Variables created in the console are not saved if you close Spyder and open it up again. The console is technically running &lt;a href=&quot;https://ipython.org/&quot;&gt;IPython&lt;/a&gt; by default.&lt;/p&gt;
&lt;p&gt;Any commands that you type in the console will be logged into the history file in the bottom right pane of the window. Furthermore, any variables that you create in the console will be shown in the variable explorer in the top right pane.&lt;/p&gt;
&lt;p&gt;Notice that you can adjust the size of any pane by putting your mouse over the divider between panes, clicking, and dragging the edge to the size that you want. You can close any of the panes by clicking the &lt;em&gt;x&lt;/em&gt; in the top of the pane.&lt;/p&gt;
&lt;p&gt;You can also break any pane out of the main window by clicking the button that looks like two windows in the top of the pane, right next to the &lt;em&gt;x&lt;/em&gt; that closes the pane. When a pane is broken out of the main window, you can drag it around and rearrange it however you want. If you want to put the pane back in the main window, drag it with the mouse so a transparent blue or gray background appears and the neighboring panes resize, then let go and the pane will snap into place.&lt;/p&gt;
&lt;p&gt;Once you have the panes arranged exactly how you want, you can ask Spyder to save the layout. Go to the &lt;em&gt;View&lt;/em&gt; menu and find the &lt;em&gt;Window layouts&lt;/em&gt; flyout again. Then click &lt;em&gt;Save current layout&lt;/em&gt; and give it a name. This lets you reset to your preferred layout at any time if something gets changed by accident. You can also reset to one of the default configurations from this menu.&lt;/p&gt;
&lt;h4 id=&quot;running-statements-in-the-console-in-spyder&quot;&gt;Running Statements in the Console in Spyder&lt;/h4&gt;
&lt;p&gt;In this section, you&amp;rsquo;re going to be writing some simple Python commands, but don&amp;rsquo;t worry if you don&amp;rsquo;t quite understand what they mean yet. You&amp;rsquo;ll learn more about Python syntax a little later on in this article. What you want to do right now is get a sense for how Spyder&amp;rsquo;s interface is similar to and different from the MATLAB interface.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll be working a lot with the Spyder console in this article, so you should learn about how it works. In the console, you&amp;rsquo;ll see a line that starts with &lt;code&gt;In [1]:&lt;/code&gt;, for input line 1. Spyder (really, the IPython console) numbers all of the input lines that you type. Since this is the first input you&amp;rsquo;re typing, the line number is 1. In the rest of this article, you&amp;rsquo;ll see references to &amp;ldquo;input line X,&amp;rdquo; where X is the number in the square brackets.&lt;/p&gt;
&lt;p&gt;One of the first things I like to do with folks who are new to Python is show them the &lt;a href=&quot;https://www.python.org/dev/peps/pep-0020/&quot;&gt;&lt;em&gt;Zen of Python&lt;/em&gt;&lt;/a&gt;. This short poem gives you a sense of what Python is all about and how to approach working with Python.&lt;/p&gt;
&lt;p&gt;To see the &lt;em&gt;Zen of Python&lt;/em&gt;, type &lt;code&gt;import this&lt;/code&gt; on input line 1 and then run the code by pressing &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-enter&quot;&gt;Enter&lt;/kbd&gt;&lt;/span&gt;. You&amp;rsquo;ll see an output like below:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;this&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;The Zen of Python, by Tim Peters&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;Beautiful is better than ugly.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Explicit is better than implicit.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Simple is better than complex.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Complex is better than complicated.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Flat is better than nested.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Sparse is better than dense.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Readability counts.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Special cases aren&amp;#39;t special enough to break the rules.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Although practicality beats purity.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Errors should never pass silently.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Unless explicitly silenced.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;In the face of ambiguity, refuse the temptation to guess.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;There should be one-- and preferably only one --obvious way to do it.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Although that way may not be obvious at first unless you&amp;#39;re Dutch.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Now is better than never.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Although never is often better than *right* now.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;If the implementation is hard to explain, it&amp;#39;s a bad idea.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;If the implementation is easy to explain, it may be a good idea.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Namespaces are one honking great idea -- let&amp;#39;s do more of those!&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code has &lt;code&gt;import this&lt;/code&gt; on input line 1. The output from running &lt;code&gt;import this&lt;/code&gt; is to print the &lt;em&gt;Zen of Python&lt;/em&gt; onto the console. We&amp;rsquo;ll return to several of the stanzas in this poem later on in the article.&lt;/p&gt;
&lt;p&gt;In many of the code blocks in this article, you&amp;rsquo;ll see three greater-than signs (&lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt;) in the top right of the code block. If you click that, it will remove the input prompt and any output lines, so you can copy and paste the code right into your console.&lt;/p&gt;
&lt;p&gt;Many Pythonistas maintain a healthy sense of humor. This is displayed in many places throughout the language, including the &lt;em&gt;Zen of Python&lt;/em&gt;. For another one, in the Spyder console, type the following code, followed by &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-enter&quot;&gt;Enter&lt;/kbd&gt;&lt;/span&gt; to run it:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;antigravity&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That statement will open your web browser to the webcomic called &lt;em&gt;XKCD&lt;/em&gt;, specifically comic &lt;a href=&quot;https://xkcd.com/353&quot;&gt;#353&lt;/a&gt;, where the author has discovered that Python has given him the ability to fly!&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ve now successfully run your first two Python statements! Congratulations 😃🎉&lt;/p&gt;
&lt;p&gt;If you look at the History Log, you should see the first two commands you typed in the console (&lt;code&gt;import this&lt;/code&gt; and &lt;code&gt;import antigravity&lt;/code&gt;). Let&amp;rsquo;s define some variables and do some basic arithmetic now. In the console, type the following statements, pressing &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-enter&quot;&gt;Enter&lt;/kbd&gt;&lt;/span&gt; after each one:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [5]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var_2&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [6]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_3&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[6]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;30&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you defined 3 variables: &lt;code&gt;var_1&lt;/code&gt;, &lt;code&gt;var_2&lt;/code&gt;, and &lt;code&gt;var_3&lt;/code&gt;. You assigned &lt;code&gt;var_1&lt;/code&gt; the value 10, &lt;code&gt;var_2&lt;/code&gt; the value 20, and &lt;code&gt;var_3&lt;/code&gt; the sum of &lt;code&gt;var_1&lt;/code&gt; and &lt;code&gt;var_2&lt;/code&gt;. Then you showed the value of the &lt;code&gt;var_3&lt;/code&gt; variable by writing it as the only thing on the input line. The output from that statement is shown on the next &lt;code&gt;Out&lt;/code&gt; line, and the number on the &lt;code&gt;Out&lt;/code&gt; line matches the associated &lt;code&gt;In&lt;/code&gt; line.&lt;/p&gt;
&lt;p&gt;There are two main things for you to notice in these commands:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;If a statement does not include an assignment (with an &lt;code&gt;=&lt;/code&gt;), it is printed onto an &lt;code&gt;Out&lt;/code&gt; line. In MATLAB, you would need to include a semicolon to suppress the output even from assignment statements, but that is not necessary in Python.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;On input lines 3, 4, and 5, the &lt;em&gt;Variable explorer&lt;/em&gt; in the top right pane updated.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After you run these three commands, your &lt;em&gt;Variable explorer&lt;/em&gt; should look like the image below:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/spyder-variable-explorer.bed4ebb8ed14.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/spyder-variable-explorer.bed4ebb8ed14.png&quot; width=&quot;236&quot; height=&quot;224&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/spyder-variable-explorer.bed4ebb8ed14.png&amp;amp;w=59&amp;amp;sig=e2a27248dbe723f1c1983bc9305c822208b95c4f 59w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/spyder-variable-explorer.bed4ebb8ed14.png&amp;amp;w=118&amp;amp;sig=555db3d1dca6b4244a7bfc427217207c5abb10d9 118w, https://files.realpython.com/media/spyder-variable-explorer.bed4ebb8ed14.png 236w&quot; sizes=&quot;75vw&quot; alt=&quot;Spyder IDE&amp;#39;s Variable Explorer&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In this image, you can see a table with four columns:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Name&lt;/em&gt;&lt;/strong&gt; shows the name that you gave to &lt;code&gt;var_1&lt;/code&gt;, &lt;code&gt;var_2&lt;/code&gt;, and &lt;code&gt;var_3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Type&lt;/em&gt;&lt;/strong&gt; shows the Python type of the variable, in this case, all &lt;code&gt;int&lt;/code&gt; for integer numbers.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Size&lt;/em&gt;&lt;/strong&gt; shows the size of the data stored variable, which is more useful for lists and other data structures.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Value&lt;/em&gt;&lt;/strong&gt; shows the current value of the variable.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4 id=&quot;running-code-in-files-in-spyder&quot;&gt;Running Code in Files in Spyder&lt;/h4&gt;
&lt;p&gt;The last stop in our brief tour of the Spyder interface is the File editor pane. In this pane, you can create and edit Python scripts and run them using the console. By default, Spyder creates a temporary file called &lt;code&gt;temp.py&lt;/code&gt; which is intended for you to temporarily store commands as you&amp;rsquo;re working before you move or save them in another file.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s write some code into the &lt;code&gt;temp.py&lt;/code&gt; file and see how to run it. The file starts with the following code, which you can just leave in place:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# -*- coding: utf-8 -*-&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;Spyder Editor&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;This is a temporary script file.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you can see two Python syntax structures:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Line 1 has a comment. In Python, the comment character is the hash or pound sign (&lt;code&gt;#&lt;/code&gt;). MATLAB uses the percent symbol (&lt;code&gt;%&lt;/code&gt;) as the comment character. Anything following the hash on the line is a comment and is usually ignored by the Python interpreter.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Starting on line 2 is a string that provides some context for the contents of the file. This is often referred to as a &lt;strong&gt;documentation string&lt;/strong&gt; or &lt;strong&gt;docstring&lt;/strong&gt; for short. You&amp;rsquo;ll learn more about docstrings in &lt;a href=&quot;#comments-start-with-in-python&quot;&gt;a later section&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now you can start adding code to this file. Starting on line 8 in &lt;code&gt;temp.py&lt;/code&gt;, enter the following code that is similar to what you already typed in the console:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_6&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var_4&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var_5&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then, there are three ways to run the code:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;You can use the &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-f5&quot;&gt;F5&lt;/kbd&gt;&lt;/span&gt; keyboard shortcut to run the file just like in MATLAB.&lt;/li&gt;
&lt;li&gt;You can click the green right-facing triangle in the menu bar just above the &lt;em&gt;Editor&lt;/em&gt; and &lt;em&gt;File explorer&lt;/em&gt; panes.&lt;/li&gt;
&lt;li&gt;You can use the &lt;em&gt;Run → Run&lt;/em&gt; menu option.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The first time you run a file, Spyder will open a dialog window asking you to confirm the options you want to use. For this test, the default options are fine and you can click &lt;em&gt;Run&lt;/em&gt; at the bottom of the dialog box:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/spyder-run-settings.784e177e3ab9.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/spyder-run-settings.784e177e3ab9.png&quot; width=&quot;440&quot; height=&quot;525&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/spyder-run-settings.784e177e3ab9.png&amp;amp;w=110&amp;amp;sig=8ea221b7e2d3d3f5430e174ffeca679f91c03167 110w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/spyder-run-settings.784e177e3ab9.png&amp;amp;w=220&amp;amp;sig=826f3f0b51456fdb72cd6d4270ffda1cc9e9b3b7 220w, https://files.realpython.com/media/spyder-run-settings.784e177e3ab9.png 440w&quot; sizes=&quot;75vw&quot; alt=&quot;Spyder IDE&amp;#39;s Run Settings dialog box&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This will automatically execute the following code in the console:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [7]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;runfile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;C:/Users/Eleanor/.spyder-py3/temp.py&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;wdir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;C:/Users/Eleanor/.spyder-py3&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code will run the file that you were working on. Notice that running the file added three variables into the Variable explorer: &lt;code&gt;var_4&lt;/code&gt;, &lt;code&gt;var_5&lt;/code&gt;, and &lt;code&gt;var_6&lt;/code&gt;. These are the three variables that you defined in the file. You will also see &lt;code&gt;runfile()&lt;/code&gt; added to the History log.&lt;/p&gt;
&lt;p&gt;In Spyder, you can also create code cells that can be run individually. To create a code cell, add a line that starts with &lt;code&gt;# %%&lt;/code&gt; into the file open in the editor:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# %% This is a code cell&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_7&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_8&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var_7&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# %% This is a second code cell&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;This code will be executed in this cell&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you have created your first code cell on line 11 with the &lt;code&gt;# %%&lt;/code&gt; code. What follows is a line comment and is ignored by Python. On line 12, you are assigning &lt;code&gt;var_7&lt;/code&gt; to have the value 42 and then line 13 assigns &lt;code&gt;var_8&lt;/code&gt; to be &lt;code&gt;var_7&lt;/code&gt; times two. Line 15 starts another code cell that can be executed separately from the first one.&lt;/p&gt;
&lt;p&gt;To execute the code cells, click the &lt;em&gt;Run Current Cell&lt;/em&gt; or &lt;em&gt;Run Current Cell and Go to the Next One&lt;/em&gt; buttons next to the generic &lt;em&gt;Run&lt;/em&gt; button in the toolbar. You can also use the keyboard shortcuts &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-enter&quot;&gt;Enter&lt;/kbd&gt;&lt;/span&gt; to run the current cell and leave it selected, or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-enter&quot;&gt;Enter&lt;/kbd&gt;&lt;/span&gt; to run the current cell and select the next cell.&lt;/p&gt;
&lt;p&gt;Spyder also offers easy-to-use debugging features, just like in MATLAB. You can double-click any of the line numbers in the &lt;em&gt;Editor&lt;/em&gt; to set a breakpoint in your code. You can run the code in debug mode using the blue right-facing triangle with two vertical lines from the toolbar, or the &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f5&quot;&gt;F5&lt;/kbd&gt;&lt;/span&gt; keyboard shortcut. This will pause execution at any breakpoints you specify and open the &lt;code&gt;ipdb&lt;/code&gt; debugger in the console which is an IPython-enhanced way to run the Python debugger &lt;code&gt;pdb&lt;/code&gt;. You can read more in &lt;a href=&quot;https://realpython.com/python-debugging-pdb/&quot;&gt;Python Debugging With pdb&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id=&quot;summarizing-your-experience-in-spyder&quot;&gt;Summarizing Your Experience in Spyder&lt;/h4&gt;
&lt;p&gt;Now you have the basic tools to use Spyder as a replacement for the MATLAB integrated development environment. You know how to run code in the console or type code into a file and run the file. You also know where to look to see your directories and files, the variables that you&amp;rsquo;ve defined, and the history of the commands you typed.&lt;/p&gt;
&lt;p&gt;Once you&amp;rsquo;re ready to start organizing your code into modules and packages, you can check out the following resources:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/python-modules-packages/&quot;&gt;Python Modules and Packages – An Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/pypi-publish-python-package/&quot;&gt;How to Publish an Open-Source Python Package to PyPI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/courses/how-to-publish-your-own-python-package-pypi/&quot;&gt;How to Publish Your Own Python Package to PyPI&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Spyder is a really big piece of software, and you&amp;rsquo;ve only just scratched the surface. You can learn a lot more about Spyder by reading the &lt;a href=&quot;https://docs.spyder-ide.org/index.html&quot;&gt;official documentation&lt;/a&gt;, the &lt;a href=&quot;https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ&quot;&gt;troubleshooting and FAQ guide&lt;/a&gt;, and the &lt;a href=&quot;https://github.com/spyder-ide/spyder/wiki&quot;&gt;Spyder wiki&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;jupyterlab&quot;&gt;JupyterLab&lt;/h3&gt;
&lt;p&gt;JupyterLab is an IDE developed by Project Jupyter. You may have heard of Jupyter Notebooks, particularly if you&amp;rsquo;re a data scientist. Well, JupyterLab is the next iteration of the Jupyter Notebook. Although at the time of this writing JupyterLab is still in beta, Project Jupyter expects that JupyterLab will eventually replace the current Notebook server interface. However, JupyterLab is fully compatible with existing Notebooks so the transition should be fairly seamless.&lt;/p&gt;
&lt;p&gt;JupyterLab comes preinstalled with Anaconda, so you can launch it from the Anaconda Navigator. Find the JupyterLab box and click &lt;em&gt;Launch&lt;/em&gt;. This will open your web browser to the address &lt;code&gt;http://localhost:8888/lab&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The main JupyterLab window is shown in the picture below:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/jupyterlab-main-window.d9420a33f736.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/jupyterlab-main-window.d9420a33f736.png&quot; width=&quot;671&quot; height=&quot;740&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/jupyterlab-main-window.d9420a33f736.png&amp;amp;w=167&amp;amp;sig=121cf34a99d209ab6bd29ab6c9e194decc6b3ead 167w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/jupyterlab-main-window.d9420a33f736.png&amp;amp;w=335&amp;amp;sig=be3acce1805c6923d2f75141e51bc14ac526e154 335w, https://files.realpython.com/media/jupyterlab-main-window.d9420a33f736.png 671w&quot; sizes=&quot;75vw&quot; alt=&quot;JupyterLab&amp;#39;s Main Window&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;There are two main sections of the interface:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;On the left is a &lt;em&gt;File explorer&lt;/em&gt; that lets you open files from your computer.&lt;/li&gt;
&lt;li&gt;On the right side of the window is how you can open create new Notebook files, work in an IPython console or system terminal, or create a new text file.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If you&amp;rsquo;re interested in learning more about JupyterLab, you can read a lot more about the next evolution of the Notebook in the &lt;a href=&quot;https://blog.jupyter.org/jupyterlab-is-ready-for-users-5a6f039b8906&quot;&gt;blog post announcing the beta release&lt;/a&gt; or in the &lt;a href=&quot;https://jupyterlab.readthedocs.io/en/stable/&quot;&gt;JupyterLab documentation&lt;/a&gt;. You can also learn about the Notebook interface in &lt;a href=&quot;https://realpython.com/jupyter-notebook-introduction/&quot;&gt;Jupyter Notebook: An Introduction&lt;/a&gt; and the &lt;a href=&quot;https://realpython.com/courses/using-jupyter-notebooks/&quot;&gt;Using Jupyter Notebooks&lt;/a&gt; course. One neat thing about the Jupyter Notebook-style document is that the code cells you created in Spyder are very similar to the code cells in a Jupyter Notebook.&lt;/p&gt;
&lt;h2 id=&quot;learning-about-pythons-mathematical-libraries&quot;&gt;Learning About Python&amp;rsquo;s Mathematical Libraries&lt;/h2&gt;
&lt;p&gt;Now you&amp;rsquo;ve got Python on your computer and you&amp;rsquo;ve got an IDE where you feel at home. So how do you learn about how to actually accomplish a task in Python? With MATLAB, you can use a search engine to find the topic you&amp;rsquo;re looking for just by including &lt;code&gt;MATLAB&lt;/code&gt; in your query. With Python, you&amp;rsquo;ll usually get better search results if you can be a bit more specific in your query than just including &lt;code&gt;Python&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In this section, you&amp;rsquo;ll take the next step to really feeling comfortable with Python by learning about how Python functionality is divided into several libraries. You&amp;rsquo;ll also learn what each library does so you can get top-notch results with your searches!&lt;/p&gt;
&lt;p&gt;Python is sometimes called a &lt;strong&gt;batteries-included&lt;/strong&gt; language. This means that most of the important functions you need are already included when you install Python. For instance, Python has a &lt;code&gt;math&lt;/code&gt; library and a &lt;code&gt;statistics&lt;/code&gt; library built-in that include the basic operations.&lt;/p&gt;
&lt;p&gt;Sometimes, though, you want to do something that isn&amp;rsquo;t included in the language. One of the big advantages of Python is that someone else has probably done whatever you need to do and published the code to accomplish that task. There are &lt;em&gt;several hundred-thousand&lt;/em&gt; publicly available and free packages that you can easily install to perform various tasks. These range from processing PDF files to building and hosting an interactive website to working with highly optimized mathematical and scientific functions.&lt;/p&gt;
&lt;p&gt;Working with arrays or matrices, optimization, or plotting requires additional libraries to be installed. Fortunately, if you install Python with the Anaconda installer these libraries come preinstalled and you don&amp;rsquo;t need to worry. Even if you&amp;rsquo;re not using Anaconda, they are usually pretty easy to install for most operating systems.&lt;/p&gt;
&lt;p&gt;The set of important libraries you&amp;rsquo;ll need to switch over from MATLAB are typically called the &lt;strong&gt;SciPy stack&lt;/strong&gt;. At the base of the stack are libraries that provide fundamental array and matrix operations (&lt;strong&gt;NumPy&lt;/strong&gt;), integration, optimization, signal processing, and linear algebra functions (&lt;strong&gt;SciPy&lt;/strong&gt;), and plotting (&lt;strong&gt;Matplotlib&lt;/strong&gt;). Other libraries that build on these to provide more advanced functionality include &lt;strong&gt;Pandas&lt;/strong&gt;, &lt;strong&gt;scikit-learn&lt;/strong&gt;, &lt;strong&gt;SymPy&lt;/strong&gt;, and more.&lt;/p&gt;
&lt;h3 id=&quot;numpy-numerical-python&quot;&gt;NumPy (Numerical Python)&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;http://www.numpy.org&quot;&gt;NumPy&lt;/a&gt; is probably the most fundamental package for scientific computing in Python. It provides a highly efficient interface to create and interact with multi-dimensional arrays. Nearly every other package in the SciPy stack uses or integrates with NumPy in some way.&lt;/p&gt;
&lt;p&gt;NumPy arrays are the equivalent to the basic array data structure in MATLAB. With NumPy arrays, you can do things like inner and outer products, transposition, and element-wise operations. NumPy also contains a number of useful methods for reading text and binary data files, fitting polynomial functions, many mathematical functions (sine, cosine, square root, and so on), and generating random numbers.&lt;/p&gt;
&lt;p&gt;The performance-sensitive parts of NumPy are all written in the C language, so they are very fast. NumPy can also take advantage of optimized linear algebra libraries such as Intel&amp;rsquo;s MKL or OpenBLAS to further increase performance.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;/p&gt;
&lt;p&gt;&lt;em&gt;Real Python&lt;/em&gt; has &lt;a href=&quot;https://realpython.com/tutorials/numpy/&quot;&gt;several articles&lt;/a&gt; that cover how you can use NumPy to speed up your Python code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/numpy-array-programming/&quot;&gt;Look Ma, No For-Loops: Array Programming With NumPy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/how-to-use-numpy-arange/&quot;&gt;NumPy arange(): How to Use np.arange()&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/python-histograms/&quot;&gt;Python Histogram Plotting: NumPy, Matplotlib, Pandas &amp;amp; Seaborn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h3 id=&quot;scipy-scientific-python&quot;&gt;SciPy (Scientific Python)&lt;/h3&gt;
&lt;p&gt;The &lt;a href=&quot;https://docs.scipy.org/doc/scipy/reference/&quot;&gt;SciPy&lt;/a&gt; package (as distinct from the SciPy stack) is a library that provides a huge number of useful functions for scientific applications. If you need to do work that requires optimization, linear algebra or sparse linear algebra, discrete Fourier transforms, signal processing, physical constants, image processing, or numerical integration, then SciPy is the library for you! Since SciPy implements so many different features, it&amp;rsquo;s almost like having access to a bunch of the MATLAB toolboxes in one package.&lt;/p&gt;
&lt;p&gt;SciPy relies heavily on NumPy arrays to do its work. Like NumPy, many of the algorithms in SciPy are implemented in C or Fortran, so they are also very fast. Also like NumPy, SciPy can take advantage of optimized linear algebra libraries to further improve performance.&lt;/p&gt;
&lt;h3 id=&quot;matplotlib-matlab-like-plotting-library&quot;&gt;Matplotlib (MATLAB-like Plotting Library)&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://matplotlib.org&quot;&gt;Matplotlib&lt;/a&gt; is a library to produce high-quality and interactive two-dimensional plots. Matplotlib is designed to provide a plotting interface that is similar to the &lt;code&gt;plot()&lt;/code&gt; function in MATLAB, so people switching from MATLAB should find it somewhat familiar. Although the core functions in Matplotlib are for 2-D data plots, there are extensions available that allow plotting in three dimensions with the &lt;a href=&quot;https://matplotlib.org/3.1.0/api/toolkits/mplot3d.html&quot;&gt;mplot3d&lt;/a&gt; package, plotting geographic data with &lt;a href=&quot;https://scitools.org.uk/cartopy/docs/latest/&quot;&gt;cartopy&lt;/a&gt;, and many more listed in the &lt;a href=&quot;https://matplotlib.org/3.1.0/thirdpartypackages/index.html&quot;&gt;Matplotlib documentation&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;/p&gt;
&lt;p&gt;Here are some more resources on Matplotlib:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/python-matplotlib-guide/&quot;&gt;Python Plotting With Matplotlib (Guide)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://matplotlib.org/tutorials/introductory/sample_plots.html&quot;&gt;Matplotlib Examples&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://matplotlib.org/gallery/index.html&quot;&gt;Matplotlib Gallery&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h3 id=&quot;other-important-python-libraries&quot;&gt;Other Important Python Libraries&lt;/h3&gt;
&lt;p&gt;With NumPy, SciPy, and Matplotlib, you can switch a lot of your MATLAB code to Python. But there are a few more libraries that might be helpful to know about.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://pandas.pydata.org/&quot;&gt;&lt;strong&gt;Pandas&lt;/strong&gt;&lt;/a&gt; provides a DataFrame, an array with the ability to name rows and columns for easy access.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.sympy.org/en/index.html&quot;&gt;&lt;strong&gt;SymPy&lt;/strong&gt;&lt;/a&gt; provides symbolic mathematics and a computer algebra system.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://scikit-learn.org/stable/index.html&quot;&gt;&lt;strong&gt;scikit-learn&lt;/strong&gt;&lt;/a&gt; provides many functions related to machine learning tasks.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://scikit-image.org/&quot;&gt;&lt;strong&gt;scikit-image&lt;/strong&gt;&lt;/a&gt; provides functions related to image processing, compatible with the similar library in SciPy.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.tensorflow.org/&quot;&gt;&lt;strong&gt;Tensorflow&lt;/strong&gt;&lt;/a&gt; provides a common platform for many machine learning tasks.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://keras.io/&quot;&gt;&lt;strong&gt;Keras&lt;/strong&gt;&lt;/a&gt; provides a library to generate neural networks.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.python.org/3/library/multiprocessing.html&quot;&gt;&lt;strong&gt;multiprocessing&lt;/strong&gt;&lt;/a&gt; provides a way to perform multi-process based parallelism. It&amp;rsquo;s built into Python.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://pint.readthedocs.io&quot;&gt;&lt;strong&gt;Pint&lt;/strong&gt;&lt;/a&gt; provides a unit library to conduct automatic conversion between physical unit systems.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.pytables.org/&quot;&gt;&lt;strong&gt;PyTables&lt;/strong&gt;&lt;/a&gt; provides a reader and writer for HDF5 format files.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.pymc.io/&quot;&gt;&lt;strong&gt;PyMC3&lt;/strong&gt;&lt;/a&gt; provides Bayesian statistical modeling and probabilistic machine learning functionality.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;syntax-differences-between-matlab-and-python&quot;&gt;Syntax Differences Between MATLAB® and Python&lt;/h2&gt;
&lt;p&gt;In this section, you&amp;rsquo;ll learn how to convert your MATLAB code into Python code. You&amp;rsquo;ll learn about the main syntax differences between MATLAB and Python, see an overview of basic array operations and how they differ between MATLAB and Python, and find out about some ways to attempt automatic conversion of your code.&lt;/p&gt;
&lt;p&gt;The biggest technical difference between MATLAB and Python is that in MATLAB, everything is treated as an array, while in Python everything is a more general object. For instance, in MATLAB, strings are arrays of characters or arrays of strings, while in Python, strings have their own type of object called &lt;code&gt;str&lt;/code&gt;. This has profound consequences for how you approach coding in each language, as you&amp;rsquo;ll see below.&lt;/p&gt;
&lt;p&gt;With that out of the way, let&amp;rsquo;s get started! To help you, the sections below are organized into groups based on how likely you are to run into that syntax.&lt;/p&gt;
&lt;h3 id=&quot;you-will-probably-see-this-syntax&quot;&gt;You Will Probably See This Syntax&lt;/h3&gt;
&lt;p&gt;The examples in this section represent code that you are very likely to see in the wild. These examples also demonstrate some of the more basic Python language features. You should make sure that you have a good grasp of these examples before moving on.&lt;/p&gt;
&lt;h4 id=&quot;comments-start-with-in-python&quot;&gt;Comments Start With &lt;code&gt;#&lt;/code&gt; in Python&lt;/h4&gt;
&lt;p&gt;In MATLAB, a comment is anything that follows a percent sign (&lt;code&gt;%&lt;/code&gt;) on a line. In Python, comments are anything that follow the hash or pound sign (&lt;code&gt;#&lt;/code&gt;). You already saw a Python comment in the earlier section about Spyder. In general, the Python interpreter ignores the content of comments, just like the MATLAB interpreter, so you can write whatever content you want in the comment. One exception to this rule in Python is the example you saw earlier in the section about Spyder:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# -*- coding: utf-8 -*-&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When the Python interpreter reads this line, it will set the encoding that it uses to read the rest of the file. This comment must appear in one of the first two lines of the file to be valid.&lt;/p&gt;
&lt;p&gt;Another difference between MATLAB and Python is in how inline documentation is written. In MATLAB, documentation is written at the start of a function &lt;a href=&quot;https://www.mathworks.com/help/matlab/matlab_prog/add-help-for-your-program.html&quot;&gt;in a comment&lt;/a&gt;, like the code sample below:&lt;/p&gt;
&lt;div class=&quot;highlight matlab&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;[total] &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;addition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;num_1,num_2&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;% ADDITION  Adds two numbers together&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;%   TOTAL = ADDITION(NUM_1,NUM_2) adds NUM_1 and NUM_2 together&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;%&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;%   See also SUM and PLUS&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;However, Python does not use comments in this way. Instead, Python has an idea called &lt;strong&gt;documentation strings&lt;/strong&gt; or &lt;a href=&quot;https://www.python.org/dev/peps/pep-0257/&quot;&gt;&lt;strong&gt;docstrings&lt;/strong&gt;&lt;/a&gt; for short. In Python, you would document the MATLAB function shown above like this:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;Adds two numbers together.&lt;/span&gt;

&lt;span class=&quot;sd&quot;&gt;    Example&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    -------&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &amp;gt;&amp;gt;&amp;gt; total = addition(10, 20)&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &amp;gt;&amp;gt;&amp;gt; total&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    30&lt;/span&gt;

&lt;span class=&quot;sd&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Notice in this code that the docstring is between two sets of three quote characters (&lt;code&gt;&quot;&quot;&quot;&lt;/code&gt;). This allows the docstring to run onto multiple lines with the whitespace and newlines preserved. The triple quote characters are a special case of &lt;a href=&quot;https://docs.python.org/3/tutorial/introduction.html#strings&quot;&gt;&lt;strong&gt;string literals&lt;/strong&gt;&lt;/a&gt;. Don&amp;rsquo;t worry too much about the syntax of defining a function yet. You&amp;rsquo;ll see more about that &lt;a href=&quot;#function-definitions-start-with-def-and-return-values-in-python&quot;&gt;in a later section&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id=&quot;whitespace-at-the-beginning-of-a-line-is-significant-in-python&quot;&gt;Whitespace at the Beginning of a Line Is Significant in Python&lt;/h4&gt;
&lt;p&gt;When you write code in MATLAB, blocks like &lt;code&gt;if&lt;/code&gt; statements, &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; loops, and function definitions are finished with the &lt;code&gt;end&lt;/code&gt; keyword. It is generally considered a good practice in MATLAB to indent the code within the blocks so that the code is visually grouped together, but it is not syntactically necessary.&lt;/p&gt;
&lt;p&gt;For example, the following two blocks of code are functionally equivalent in MATLAB:&lt;/p&gt;
&lt;div class=&quot;highlight matlab&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;disp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;equal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;disp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;equal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;disp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;I&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;am&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;outside&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are first creating &lt;code&gt;num&lt;/code&gt; to store the value 10 and then checking whether the value of &lt;code&gt;num&lt;/code&gt; is equal to 10. If it is, you are displaying the phrase &lt;code&gt;num is equal to 10&lt;/code&gt; on the console from line 2. Otherwise, the &lt;code&gt;else&lt;/code&gt; clause will kick in and display &lt;code&gt;num is not equal to 10&lt;/code&gt;. Of course, if you run this code, you will see the &lt;code&gt;num is equal to 10&lt;/code&gt; output and then &lt;code&gt;I am now outside the if block&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now you should modify your code so it looks like the sample below:&lt;/p&gt;
&lt;div class=&quot;highlight matlab&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;nb&quot;&gt;disp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;equal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;nb&quot;&gt;disp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;equal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;disp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;I&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;am&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;outside&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you have only changed lines 3 and 5 by adding some spaces or indentation in the front of the line. The code will perform identically to the previous example code, but with the indentation, it is much easier to tell what code goes in the &lt;code&gt;if&lt;/code&gt; part of the statement and what code is in the &lt;code&gt;else&lt;/code&gt; part of the statement.&lt;/p&gt;
&lt;p&gt;In Python, indentation at the start of a line is used to delimit the beginning and end of class and function definitions, &lt;code&gt;if&lt;/code&gt; statements, and &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; loops. There is no &lt;code&gt;end&lt;/code&gt; keyword in Python. This means that indentation is very important in Python!&lt;/p&gt;
&lt;p&gt;In addition, in Python the definition line of an &lt;code&gt;if/else/elif&lt;/code&gt; statement, a &lt;code&gt;for&lt;/code&gt; or &lt;code&gt;while&lt;/code&gt; loop, a function, or a class is ended by a colon. In MATLAB, the colon is not used to end the line.&lt;/p&gt;
&lt;p&gt;Consider this code example:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num is equal to 10&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num is not equal to 10&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;I am now outside the if block&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;On the first line, you are defining &lt;code&gt;num&lt;/code&gt; and setting its value to 10. On line 2, writing &lt;code&gt;if num == 10:&lt;/code&gt; tests the value of &lt;code&gt;num&lt;/code&gt; compared to 10. Notice the colon at the end of the line.&lt;/p&gt;
&lt;p&gt;Next, line 3 &lt;em&gt;must&lt;/em&gt; be indented in Python&amp;rsquo;s syntax. On that line, you are using &lt;code&gt;print()&lt;/code&gt; to display some output to the console, in a similar way to &lt;code&gt;disp()&lt;/code&gt; in MATLAB. You&amp;rsquo;ll read more about &lt;code&gt;print()&lt;/code&gt; versus &lt;code&gt;disp()&lt;/code&gt; &lt;a href=&quot;#console-output-is-shown-with-print-in-python&quot;&gt;in a later section&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;On line 4, you are starting the &lt;code&gt;else&lt;/code&gt; block. Notice that the &lt;code&gt;e&lt;/code&gt; in the &lt;code&gt;else&lt;/code&gt; keyword is vertically aligned with the &lt;code&gt;i&lt;/code&gt; in the &lt;code&gt;if&lt;/code&gt; keyword, and the line is ended by a colon. Because the &lt;code&gt;else&lt;/code&gt; is dedented relative to &lt;code&gt;print()&lt;/code&gt; on line 3, and because it is aligned with the &lt;code&gt;if&lt;/code&gt; keyword, Python knows that the code within the &lt;code&gt;if&lt;/code&gt; part of the block has finished and the &lt;code&gt;else&lt;/code&gt; part is starting. Line 5 is indented by one level, so it forms the block of code to be executed when the &lt;code&gt;else&lt;/code&gt; statement is satisfied.&lt;/p&gt;
&lt;p&gt;Lastly, on line 6 you are printing a statement from outside the &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; block. This statement will be printed regardless of the value of &lt;code&gt;num&lt;/code&gt;. Notice that the &lt;code&gt;p&lt;/code&gt; in &lt;code&gt;print()&lt;/code&gt; is vertically aligned with the &lt;code&gt;i&lt;/code&gt; in &lt;code&gt;if&lt;/code&gt; and the &lt;code&gt;e&lt;/code&gt; in &lt;code&gt;else&lt;/code&gt;. This is how Python knows that the code in the &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; block has ended. If you run the code above, Python will display &lt;code&gt;num is equal to 10&lt;/code&gt; followed by &lt;code&gt;I am now outside the if block&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now you should modify the code above to remove the indentation and see what happens. If you try to type the code without indentation into the Spyder/IPython console, you will get an &lt;code&gt;IndentationError&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num is equal to 10&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gt&quot;&gt;  File&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt; &amp;quot;&amp;lt;ipython-input-2-f453ffd2bc4f&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;gt&quot;&gt;, line &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num is equal to 10&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;
&lt;span class=&quot;ne&quot;&gt;IndentationError&lt;/span&gt;: expected an indented block
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you first set the value of &lt;code&gt;num&lt;/code&gt; to 10 and then tried to write the &lt;code&gt;if&lt;/code&gt; statement without indentation. In fact, the IPython console is smart and automatically indents the line after the &lt;code&gt;if&lt;/code&gt; statement for you, so you&amp;rsquo;ll have to delete the indentation to produce this error.&lt;/p&gt;
&lt;p&gt;When you&amp;rsquo;re indenting your code, the official Python style guide called &lt;a href=&quot;#there-is-an-official-guide-to-writing-good-code-in-python&quot;&gt;PEP 8&lt;/a&gt; recommends using 4 space characters to represent one indentation level. Most text editors that are set up to work with Python files will automatically insert 4 spaces if you press the &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-tab&quot;&gt;Tab&lt;/kbd&gt;&lt;/span&gt; key on your keyboard. You can choose to use the tab character for your code if you want, but you shouldn&amp;rsquo;t mix tabs and spaces or you&amp;rsquo;ll probably end up with a &lt;code&gt;TabError&lt;/code&gt; if the indentation becomes mismatched.&lt;/p&gt;
&lt;h4 id=&quot;conditional-statements-use-elif-in-python&quot;&gt;Conditional Statements Use &lt;code&gt;elif&lt;/code&gt; in Python&lt;/h4&gt;
&lt;p&gt;In MATLAB, you can construct &lt;a href=&quot;https://www.mathworks.com/help/matlab/ref/if.html&quot;&gt;conditional statements&lt;/a&gt; with &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elseif&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt;. These kinds of statements allow you to control the flow of your program in response to different conditions.&lt;/p&gt;
&lt;p&gt;You should try this idea out with the code below, and then compare the example of MATLAB vs Python for conditional statements:&lt;/p&gt;
&lt;div class=&quot;highlight matlab&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;disp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;equal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;k&quot;&gt;elseif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;disp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;equal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;disp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;neither&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nor&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code block, you are defining &lt;code&gt;num&lt;/code&gt; to be equal to 10. Then you are checking if the value of &lt;code&gt;num&lt;/code&gt; is 10, and if it is, using &lt;code&gt;disp()&lt;/code&gt; to print output to the console. If &lt;code&gt;num&lt;/code&gt; is 20, you are printing a different statement, and if &lt;code&gt;num&lt;/code&gt; is neither 10 nor 20, you are printing the third statement.&lt;/p&gt;
&lt;p&gt;In Python, the &lt;code&gt;elseif&lt;/code&gt; keyword is replaced with &lt;code&gt;elif&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num is equal to 10&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num is equal to 20&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num is neither 10 nor 20&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code block is functionally equivalent to the previous MATLAB code block. There are 2 main differences. On line 4, &lt;code&gt;elseif&lt;/code&gt; is replaced with &lt;code&gt;elif&lt;/code&gt;, and there is no &lt;code&gt;end&lt;/code&gt; statement to end the block. Instead, the &lt;code&gt;if&lt;/code&gt; block ends when the next dedented line of code is found after the &lt;code&gt;else&lt;/code&gt;. You can read more in the &lt;a href=&quot;https://docs.python.org/3/tutorial/controlflow.html#if-statements&quot;&gt;Python documentation&lt;/a&gt; for &lt;code&gt;if&lt;/code&gt; statements.&lt;/p&gt;
&lt;h4 id=&quot;calling-functions-and-indexing-sequences-use-different-brackets-in-python&quot;&gt;Calling Functions and Indexing Sequences Use Different Brackets in Python&lt;/h4&gt;
&lt;p&gt;In MATLAB, when you want to call a function or when you want to index an array, you use round brackets (&lt;code&gt;()&lt;/code&gt;), sometimes also called parentheses. Square brackets (&lt;code&gt;[]&lt;/code&gt;) are used to create arrays.&lt;/p&gt;
&lt;p&gt;You can test out the differences in MATLAB vs Python with the example code below:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;    10&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;    60&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you first create an array using the square brackets on the right side of the equal sign. Then, you retrieve the value of the first element by &lt;code&gt;arr(1)&lt;/code&gt;, using the round brackets as the indexing operator. On the third input line, you are calling &lt;code&gt;sum()&lt;/code&gt; and using the round brackets to indicate the parameters that should be passed into &lt;code&gt;sum()&lt;/code&gt;, in this case just &lt;code&gt;arr&lt;/code&gt;. MATLAB computes the sum of the elements in &lt;code&gt;arr&lt;/code&gt; and returns that result.&lt;/p&gt;
&lt;p&gt;Python uses separate syntax for calling functions and indexing sequences. In Python, using round brackets means that a function should be executed and using square brackets will index a sequence:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[2]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;10&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[3]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;60&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are defining a Python list on input line 1. Python lists have some important distinctions from arrays in MATLAB and arrays from the NumPy package. You can read more about Python lists in &lt;a href=&quot;https://realpython.com/python-lists-tuples/&quot;&gt;Lists and Tuples in Python&lt;/a&gt;, and you&amp;rsquo;ll learn more about NumPy arrays in &lt;a href=&quot;#an-overview-of-basic-array-operations&quot;&gt;a later section&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;On the input line 2, you are displaying the value of the first element of the list with the indexing operation using square brackets. On input line 3, you are calling &lt;code&gt;sum()&lt;/code&gt; using round brackets and passing in the list stored in &lt;code&gt;arr&lt;/code&gt;. This results in the sum of the list elements being displayed on the last line. Notice that Python uses square brackets for indexing the list and round brackets for calling functions.&lt;/p&gt;
&lt;h4 id=&quot;the-first-index-in-a-sequence-is-0-in-python&quot;&gt;The First Index in a Sequence Is 0 in Python&lt;/h4&gt;
&lt;p&gt;In MATLAB, you can get the first value from an array by using &lt;code&gt;1&lt;/code&gt; as the index. This style follows the natural numbering convention and starts how you would count the number of items in the sequence. You can try out the differences of MATLAB vs Python with this example:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;    10&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Array indices must be positive integers or logical values.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating an array with three numbers: &lt;code&gt;10&lt;/code&gt;, &lt;code&gt;20&lt;/code&gt;, and &lt;code&gt;30&lt;/code&gt;. Then you are displaying the value of the first element with the index &lt;code&gt;1&lt;/code&gt;, which is &lt;code&gt;10&lt;/code&gt;. Trying to access the zeroth element results in an error in MATLAB, as shown on the last two lines.&lt;/p&gt;
&lt;p&gt;In Python, the index of the first element in a sequence is 0, not 1:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[2]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;10&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[3]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;20&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;a string&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [5]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[5]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;&amp;#39;a&amp;#39;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [6]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[6]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;&amp;#39; &amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are defining &lt;code&gt;arr&lt;/code&gt; as a Python list with three elements on input line 1. On input line 2, you are displaying the value of the first element of the list, which has the index 0. Then you are displaying the second element of the list, which has the index 1.&lt;/p&gt;
&lt;p&gt;On input lines 4, 5, and 6, you are defining &lt;code&gt;a_string&lt;/code&gt; with the contents &lt;code&gt;&quot;a string&quot;&lt;/code&gt; and then getting the first and second elements of the string. Notice that the second element (character) of the string is a space. This demonstrates a general Python feature, that many variable types operate as sequences and can be indexed, including lists, tuples, strings, and arrays.&lt;/p&gt;
&lt;h4 id=&quot;the-last-element-of-a-sequence-has-index-1-in-python&quot;&gt;The Last Element of a Sequence Has Index &lt;code&gt;-1&lt;/code&gt; in Python&lt;/h4&gt;
&lt;p&gt;In MATLAB, you can get the last value from an array by using &lt;a href=&quot;https://www.mathworks.com/help/matlab/ref/end.html&quot;&gt;&lt;code&gt;end&lt;/code&gt;&lt;/a&gt; as the index. This is really useful when you don&amp;rsquo;t know how long an array is, so you don&amp;rsquo;t know what number to access the last value.&lt;/p&gt;
&lt;p&gt;Try out the differences in MATLAB vs Python with this example:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;    30&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating an array with three numbers, &lt;code&gt;10&lt;/code&gt;, &lt;code&gt;20&lt;/code&gt;, and &lt;code&gt;30&lt;/code&gt;. Then you are displaying the value of the last element with the index &lt;code&gt;end&lt;/code&gt;, which is &lt;code&gt;30&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In Python, the last value in a sequence can be retrieved by using the index &lt;code&gt;-1&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[2]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;30&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are defining a Python list with three elements on input line 1. On input line 2, you are displaying the value of the last element of the list, which has the index &lt;code&gt;-1&lt;/code&gt; and the value 30.&lt;/p&gt;
&lt;p&gt;In fact, by using negative numbers as the index values you can work your way backwards through the sequence:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[3]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;20&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[4]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;10&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are retrieving the second-to-last and third-to-last elements from the list, which have values of &lt;code&gt;20&lt;/code&gt; and &lt;code&gt;10&lt;/code&gt;, respectively.&lt;/p&gt;
&lt;h4 id=&quot;exponentiation-is-done-with-in-python&quot;&gt;Exponentiation Is Done With &lt;code&gt;**&lt;/code&gt; in Python&lt;/h4&gt;
&lt;p&gt;In MATLAB, when you want to raise a number to a power you use the &lt;a href=&quot;https://www.mathworks.com/help/matlab/ref/mpower.html&quot;&gt;caret operator&lt;/a&gt; (&lt;code&gt;^&lt;/code&gt;). The caret operator is a &lt;strong&gt;binary operator&lt;/strong&gt; that takes two numbers.  Other binary operators include addition (&lt;code&gt;+&lt;/code&gt;), subtraction (&lt;code&gt;-&lt;/code&gt;), multiplication (&lt;code&gt;*&lt;/code&gt;), and division (&lt;code&gt;/&lt;/code&gt;), among others. The number on the left of the caret is the base and the number on the right is the exponent.&lt;/p&gt;
&lt;p&gt;Try out the differences of MATLAB vs Python with this example:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;^&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;    100&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are raising 10 to the power of 2 using the caret resulting an answer of 100.&lt;/p&gt;
&lt;p&gt;In Python, you use two asterisks (&lt;code&gt;**&lt;/code&gt;) when you want to raise a number to a power:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[1]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;100&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are raising 10 to the power of 2 using two asterisks resulting an answer of 100. Notice that there is no effect of including spaces on either side of the asterisks. In Python, the typical style is to have spaces on both sides of a binary operator.&lt;/p&gt;
&lt;h4 id=&quot;the-length-of-a-sequence-is-found-with-len-in-python&quot;&gt;The Length of a Sequence Is Found With &lt;code&gt;len()&lt;/code&gt; in Python&lt;/h4&gt;
&lt;p&gt;In MATLAB, you can get the length of an array with &lt;a href=&quot;https://www.mathworks.com/help/matlab/ref/length.html&quot;&gt;&lt;code&gt;length()&lt;/code&gt;&lt;/a&gt;. This function takes an array as the argument and returns back the size of the largest dimension in the array. You can see the basics of this function with this example:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;    3&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;    1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, on the first input line you are finding the length of an array with 3 elements. As expected, &lt;code&gt;length()&lt;/code&gt; returns an answer of 3. On the second input line, you are finding the length of the &lt;strong&gt;string array&lt;/strong&gt; that contains one element. Notice that MATLAB implicitly creates a string array, even though you did not use the square brackets to indicate it is an array.&lt;/p&gt;
&lt;p&gt;In Python, you can get the length of a sequence with &lt;code&gt;len()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[1]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;3&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;a string&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[2]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;8&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, on the input line 1 you are finding the length of a list with 3 elements. As expected, &lt;code&gt;len()&lt;/code&gt; returns a length of 3. On input line 2, you are finding the length of a string as the input. In Python, strings are sequences and &lt;code&gt;len()&lt;/code&gt; counts the number of characters in the string. In this case, &lt;code&gt;a string&lt;/code&gt; has 8 characters.&lt;/p&gt;
&lt;h4 id=&quot;console-output-is-shown-with-print-in-python&quot;&gt;Console Output Is Shown With &lt;code&gt;print()&lt;/code&gt; in Python&lt;/h4&gt;
&lt;p&gt;In MATLAB, you can use &lt;code&gt;disp()&lt;/code&gt;, &lt;code&gt;fprintf()&lt;/code&gt;, and &lt;code&gt;sprintf()&lt;/code&gt; to print the value of variables and other output to the console. &lt;code&gt;disp()&lt;/code&gt; is the simplest of these commands but does not give you any control over the format of the output. &lt;code&gt;fprintf()&lt;/code&gt; and &lt;code&gt;sprintf()&lt;/code&gt; give you total control over the output format, but the format specification is a little bit complicated.&lt;/p&gt;
&lt;p&gt;In Python, &lt;code&gt;print()&lt;/code&gt; serves the same function as &lt;code&gt;disp()&lt;/code&gt;. Unlike &lt;code&gt;disp()&lt;/code&gt;, &lt;code&gt;print()&lt;/code&gt; can send its output to a file similar to &lt;code&gt;fprintf()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Python&amp;rsquo;s &lt;code&gt;print()&lt;/code&gt; will display any number of arguments passed to it, separating them by a space in the output. This is different from &lt;code&gt;disp()&lt;/code&gt; in MATLAB, which only takes one argument. The following example shows how Python&amp;rsquo;s &lt;code&gt;print()&lt;/code&gt; can take any number of arguments, and the each argument is separated by a space in the output:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;str_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;any number of arguments&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;10 20 any number of arguments&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, the input lines 1, 2, and 3 define &lt;code&gt;val_1&lt;/code&gt;, &lt;code&gt;val_2&lt;/code&gt;, and &lt;code&gt;str_1&lt;/code&gt;, where &lt;code&gt;val_1&lt;/code&gt; and &lt;code&gt;val_1&lt;/code&gt; are integers, and &lt;code&gt;str_1&lt;/code&gt; is a string of text. On input line 4, you are printing the three variables using &lt;code&gt;print()&lt;/code&gt;. The output below this line the value of the three variables are shown in the console output, separated by spaces.&lt;/p&gt;
&lt;p&gt;You can control the separator used in the output between arguments to &lt;code&gt;print()&lt;/code&gt; by using the &lt;code&gt;sep&lt;/code&gt; keyword argument:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [5]: &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;; &amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;10; 20; any number of arguments&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are printing the same three variables but setting the separator to be a semicolon followed by a space. This separator is printed between the first and second and the second and third arguments, but not after the third argument. To control the character printed after the last value, you can use the &lt;code&gt;end&lt;/code&gt; keyword argument to &lt;code&gt;print()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [6]: &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;val_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;val_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;str_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;; &amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;10; 20; any number of arguments;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you have added the &lt;code&gt;end&lt;/code&gt; keyword argument to &lt;code&gt;print()&lt;/code&gt;, setting it to print a semicolon after the last value. This is shown in the output on line below the input.&lt;/p&gt;
&lt;p&gt;Like &lt;code&gt;disp()&lt;/code&gt; from MATLAB, &lt;code&gt;print()&lt;/code&gt; cannot directly control the output format of variables. If you want more control over the format of the output, you should use &lt;strong&gt;f-strings&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;format()&lt;/code&gt;&lt;/strong&gt;. In these strings, you can use very similar formatting style codes as &lt;code&gt;fprintf()&lt;/code&gt; in MATLAB to format numbers. You can read a lot more about f-strings and other string formatting in &lt;a href=&quot;https://realpython.com/python-f-strings/&quot;&gt;Python 3&amp;rsquo;s f-Strings&lt;/a&gt; or the &lt;a href=&quot;https://docs.python.org/3/library/string.html#format-specification-mini-language&quot;&gt;official documentation&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [7]: &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Python&amp;#39;s print() can handle &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{str_1}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Python&amp;#39;s print() can handle any number of arguments&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [8]: &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;The value of val_1 = &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{val_1:8.3f}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;The value of val_1 =   10.000&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [9]: &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# The following line will only work in Python 3.8&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [10]: &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;The value of {val_1=} and {val_2=}&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;The value of val_1 = 10, and val_2 = 20&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, input line 7 includes an f-string, indicated by the &lt;code&gt;f&lt;/code&gt; to start the string. This means that Python will substitute the value of any variables it encounters between &lt;code&gt;{}&lt;/code&gt;, or curly braces, within the string. You can see that in the output, Python has replaced &lt;code&gt;{str_1}&lt;/code&gt; with &lt;code&gt;any number of arguments&lt;/code&gt;, the value of &lt;code&gt;str_1&lt;/code&gt;. On input line 8, you are using some of the formatting syntax to print the value of &lt;code&gt;val_1&lt;/code&gt; as a floating point number with 8 columns in the output and 3 digits of precision.&lt;/p&gt;
&lt;p&gt;On input line 10, a new feature that is coming in Python 3.8 (due out in &lt;a href=&quot;https://www.python.org/dev/peps/pep-0569/&quot;&gt;late 2019&lt;/a&gt;) is demonstrated. If a variable name is immediately followed by an equals sign inside curly braces, the name of the variable and the value will be printed automatically.&lt;/p&gt;
&lt;p&gt;You can take a deep dive into Python&amp;rsquo;s &lt;code&gt;print()&lt;/code&gt; by checking out &lt;a href=&quot;https://realpython.com/python-print/&quot;&gt;The Ultimate Guide to Python Print&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;you-will-probably-see-these-but-you-can-learn-them-when-you-need-to&quot;&gt;You Will Probably See These, but You Can Learn Them When You Need To&lt;/h3&gt;
&lt;p&gt;In this section, you&amp;rsquo;ll find examples of code that you&amp;rsquo;ll probably see in the wild, but you can wait a little while to understand them if you want. These examples use some intermediate features in Python but are still in the core of how Python works. Just like in the last section, you&amp;rsquo;ll see comparisons of the MATLAB vs Python syntax differences.&lt;/p&gt;
&lt;h4 id=&quot;function-definitions-start-with-def-and-return-values-in-python&quot;&gt;Function Definitions Start With &lt;code&gt;def&lt;/code&gt; and &lt;code&gt;return&lt;/code&gt; Values in Python&lt;/h4&gt;
&lt;p&gt;In MATLAB, you can &lt;a href=&quot;https://www.mathworks.com/help/matlab/ref/function.html&quot;&gt;define a function&lt;/a&gt; by placing the &lt;code&gt;function&lt;/code&gt; keyword at the start of a line. This is followed by the name of any output variables, an equals (&lt;code&gt;=&lt;/code&gt;) sign, then the name of the function and any input arguments in parentheses. Within the the function you have to assign to any variables you specified in the definition line as outputs. A simple example MATLAB function is shown below:&lt;/p&gt;
&lt;div class=&quot;highlight matlab&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;[total] &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;addition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;num_1,num_2&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you see the &lt;code&gt;function&lt;/code&gt; definition on line 1. There is only one output variable, called &lt;code&gt;total&lt;/code&gt;, for this function. The name of the function is &lt;code&gt;addition&lt;/code&gt; and it takes two arguments, which will be assigned the names &lt;code&gt;num_1&lt;/code&gt; and &lt;code&gt;num_2&lt;/code&gt; in the function body. Line 2 is the implementation of the function. The value of &lt;code&gt;total&lt;/code&gt; is set equal to the sum of &lt;code&gt;num_1&lt;/code&gt; and &lt;code&gt;num_2&lt;/code&gt;. The last line of the function is the &lt;code&gt;end&lt;/code&gt; keyword that tells the MATLAB interpreter the definition of the function has finished.&lt;/p&gt;
&lt;p&gt;To use this function in MATLAB, you should save it in a file called &lt;code&gt;addition.m&lt;/code&gt;, matching the name of the function. Alternatively, it can be placed in file with other commands provided that the function definition is the last thing in the file and the file is &lt;em&gt;not&lt;/em&gt; named &lt;code&gt;addition.m&lt;/code&gt;. Then, you can run the function by typing the following code in the MATLAB console:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_of_vars&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;sum_of_vars =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;    30&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you have defined two variables called &lt;code&gt;var_1&lt;/code&gt; and &lt;code&gt;var_2&lt;/code&gt; that hold the values 20 and 10, respectively. Then you created a third variable called &lt;code&gt;sum_of_vars&lt;/code&gt; that stores the output from &lt;code&gt;addition()&lt;/code&gt;. Check out the &lt;em&gt;Variable explorer&lt;/em&gt;, and you&amp;rsquo;ll see that &lt;code&gt;sum_of_vars&lt;/code&gt; has the value 30, as expected. Notice that the name &lt;code&gt;sum_of_vars&lt;/code&gt; did not have to be the same name as the output variable used in the function definition, which was &lt;code&gt;total&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;MATLAB does not require a function to provide an output value. In this case, you would remove the output variable and the equals sign from the function definition. Modify your &lt;code&gt;addition.m&lt;/code&gt; file so that the code looks like this:&lt;/p&gt;
&lt;div class=&quot;highlight matlab&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;addition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;num_1,num_2&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The only change in this code from the earlier code is that you deleted the &lt;code&gt;[total] =&lt;/code&gt; from line 1, the other lines are exactly the same. Now if you try to assign the result of calling this function to a variable, MATLAB will generate an error in the console:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_of_vars&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Error using addition&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Too many output arguments.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you defined the same two variables &lt;code&gt;var_1&lt;/code&gt; and &lt;code&gt;var_2&lt;/code&gt; as before and called &lt;code&gt;addition()&lt;/code&gt; in the same way as before. However, since &lt;code&gt;addition()&lt;/code&gt; no longer specifies an output variable, MATLAB generates an error message that there are too many output arguments. Clicking on the word &lt;code&gt;addition&lt;/code&gt; will open the definition of the function for you to edit or view the source code to fix the problem.&lt;/p&gt;
&lt;p&gt;In Python, the &lt;code&gt;def&lt;/code&gt; keyword starts a function definition. The &lt;code&gt;def&lt;/code&gt; keyword must be followed by the name of the function and any arguments to the function inside parentheses, similar to MATLAB. The line with &lt;code&gt;def&lt;/code&gt; must be ended with a colon (&lt;code&gt;:&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Starting on the next line, the code that should be executed as part of the function must be indented one level. In Python, the function definition ends when a line of code starts at the same indentation level as the &lt;code&gt;def&lt;/code&gt; keyword on the first line.&lt;/p&gt;
&lt;p&gt;If your function returns some output back to the caller, Python does not require that you specify a name for an output variable. Instead, you use the &lt;code&gt;return&lt;/code&gt; statement to send an output value from the function.&lt;/p&gt;
&lt;p&gt;An equivalent function in Python to your first &lt;code&gt;addition()&lt;/code&gt; example with an output variable is shown below:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you see the &lt;code&gt;def&lt;/code&gt; keyword followed by the function name and the two arguments &lt;code&gt;num_1&lt;/code&gt; and &lt;code&gt;num_2&lt;/code&gt; on line 1. On line 2 you can see the creation of a new variable &lt;code&gt;total&lt;/code&gt; to store the sum of &lt;code&gt;num_1&lt;/code&gt; and &lt;code&gt;num_2&lt;/code&gt;, and on line 3 the value of &lt;code&gt;total&lt;/code&gt; is returned to the point where this function was called. Notice that lines 2 and 3 are indented by 4 spaces because they make up the body of the function.&lt;/p&gt;
&lt;p&gt;The variable that stores the sum of &lt;code&gt;num_1&lt;/code&gt; and &lt;code&gt;num_2&lt;/code&gt; can have any name, it doesn&amp;rsquo;t have to be called &lt;code&gt;total&lt;/code&gt;. In fact, you don&amp;rsquo;t need to create a variable there at all. You can simplify your previous function definition by eliminating &lt;code&gt;total&lt;/code&gt; and simply returning the value of &lt;code&gt;num_1 + num_2&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Line 1 in this code is the same as it was before, you have only changed line 2 and deleted line 3. Line 2 now computes the value of &lt;code&gt;num_1 + num_2&lt;/code&gt; and returns that value back to the caller of the function. Line 2 is indented by 4 spaces because it makes up the body of the function.&lt;/p&gt;
&lt;p&gt;To use this function in Python, you do not need to save it in a file with a special name. You can place the function definition in any Python file, at any point in the file. There is no restriction that the function definition has to be last. In fact, you can even define functions right from the console, which is not possible in MATLAB.&lt;/p&gt;
&lt;p&gt;Open Spyder and in the Console pane type:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;On this line of code you are creating the function definition. In the Spyder/IPython console, once you start a function definition and press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-enter&quot;&gt;Enter&lt;/kbd&gt;&lt;/span&gt;, the start of the line becomes three dots and the cursor is automatically indented. Now you can type the remainder of the function definition. You&amp;rsquo;ll have to press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-enter&quot;&gt;Enter&lt;/kbd&gt;&lt;/span&gt; twice to complete the definition:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...:&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you have the definition of the function on the first line and the body of the function on the second line. The console automatically adds the &lt;code&gt;...:&lt;/code&gt; at the start of the lines to indicate these are &lt;strong&gt;continuation lines&lt;/strong&gt; that apply to the function definition.&lt;/p&gt;
&lt;p&gt;Once you&amp;rsquo;ve completed the definition, you can execute the function from the console as well. You should type this code:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_of_vars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [5]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_of_vars&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[5]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;30&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you first create two variables &lt;code&gt;var_1&lt;/code&gt; and &lt;code&gt;var_2&lt;/code&gt; that store the values you want to add together. Then, on input line 4, you assign &lt;code&gt;sum_of_vars&lt;/code&gt; to the result that is returned from &lt;code&gt;addition()&lt;/code&gt;. On input line 5, you are outputting the value of &lt;code&gt;sum_of_vars&lt;/code&gt; to the console screen. This displays 30, the sum of 10 and 20.&lt;/p&gt;
&lt;p&gt;In Python, if you do not explicitly put a &lt;code&gt;return&lt;/code&gt; statement, your function will implicitly return the special value &lt;code&gt;None&lt;/code&gt;. You should change your Python definition of &lt;code&gt;addition()&lt;/code&gt; to see how this works. In the Spyder/IPython console, type the following:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [6]: &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;   ...:&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you have the same &lt;code&gt;def&lt;/code&gt; line on input line 6. You have changed the first continuation line to assign the result of the addition to &lt;code&gt;total&lt;/code&gt; instead of returning. Now you should see what happens when we execute this modified function:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [7]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_of_vars&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;var_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [8]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_of_vars&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [9]:&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, on input line 7 you are assigning &lt;code&gt;sum_of_vars&lt;/code&gt; to be the returned value from &lt;code&gt;addition()&lt;/code&gt;. Then, on input line 8, you are showing the value of &lt;code&gt;sum_of_vars&lt;/code&gt; on the console screen, just like before. This time though, there is no output! By default, Python prints nothing when it outputs a variable whose value is &lt;code&gt;None&lt;/code&gt;. You can double check the value of the &lt;code&gt;sum_of_vars&lt;/code&gt; variable by looking at the &lt;em&gt;Variable explorer&lt;/em&gt;. In the &lt;em&gt;Type&lt;/em&gt; column, it should list &lt;code&gt;NoneType&lt;/code&gt;, telling you that &lt;code&gt;sum_of_vars&lt;/code&gt; is the special &lt;code&gt;None&lt;/code&gt; value.&lt;/p&gt;
&lt;h4 id=&quot;functions-accept-positional-and-keyword-arguments-in-python&quot;&gt;Functions Accept Positional and Keyword Arguments in Python&lt;/h4&gt;
&lt;p&gt;In MATLAB, functions have input arguments specified on the first line, in the &lt;code&gt;function&lt;/code&gt; definition. When you call a function in MATLAB, you can pass from zero up to the number of arguments that are specified. In the body of the function, you can check the number of input arguments the caller actually passed to execute different code. This is useful when you want different arguments to have different meaning, like in the example below:&lt;/p&gt;
&lt;div class=&quot;highlight matlab&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;[result] &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;addOrSubtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;num_1,num_2,subtract&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;c&quot;&gt;% ADDORSUBTRACT  Add or subtract two value&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;c&quot;&gt;%   RESULT = addOrSubtract(NUM_1,NUM_2) adds NUM_1 and NUM_2 together&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;c&quot;&gt;%&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;c&quot;&gt;%   RESULT = addOrSubtract(NUM_1,NUM_2,true) subtracts NUM_2 from NUM_1&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nargin&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;otherwise&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are defining a function with three possible input arguments. On line 7, you are starting a &lt;code&gt;switch&lt;/code&gt;/&lt;code&gt;case&lt;/code&gt; block that determines how many input arguments were passed to the function by using the &lt;a href=&quot;https://www.mathworks.com/help/matlab/ref/nargin.html&quot;&gt;special variable &lt;code&gt;nargin&lt;/code&gt;&lt;/a&gt;. This variable stores the actual number of arguments the caller passed into the function.&lt;/p&gt;
&lt;p&gt;In your code above, you are defining three cases:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If the number of input arguments is 2, you are adding &lt;code&gt;num_1&lt;/code&gt; and &lt;code&gt;num_2&lt;/code&gt; together.&lt;/li&gt;
&lt;li&gt;If the number of input arguments is 3, you are subtracting &lt;code&gt;num_2&lt;/code&gt; from &lt;code&gt;num_1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If fewer than 2 arguments are passed, the output will be &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If more than 3 arguments are passed, MATLAB will raise an error.&lt;/p&gt;
&lt;p&gt;Now you should experiment with this function. Save the code above into a file called &lt;code&gt;addOrSubtract.m&lt;/code&gt; and then on the MATLAB console, try the version with two input arguments:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addOrSubtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;    30&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are calling &lt;code&gt;addOrSubtract()&lt;/code&gt; with two arguments, so the arguments are added together, resulting in an answer of &lt;code&gt;30&lt;/code&gt;. Next, try calling &lt;code&gt;addOrSubtract()&lt;/code&gt; with three arguments:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addOrSubtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;   -10&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you used three input arguments, and found that the second argument was subtracted from the first, resulting in an answer of &lt;code&gt;-10&lt;/code&gt;. Third, try calling &lt;code&gt;addOrSubtract()&lt;/code&gt; with one argument:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addOrSubtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;     0&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you used one input argument and found the answer was 0, because MATLAB only found one argument to the function and used the &lt;code&gt;otherwise&lt;/code&gt; case. Finally, try calling &lt;code&gt;addOrSubtract()&lt;/code&gt; with four arguments:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addOrSubtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Error using addOrSubtract&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Too many input arguments.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you find that MATLAB raises an error because there were more input arguments passed than were defined in the &lt;code&gt;function&lt;/code&gt; line.&lt;/p&gt;
&lt;p&gt;There are four key takeaways from this example with MATLAB:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;There is only one kind of argument in a function definition.&lt;/li&gt;
&lt;li&gt;The meaning of an argument in the code is determined by its position in the function definition.&lt;/li&gt;
&lt;li&gt;The maximum number of arguments that can be passed to a function is determined by the number of arguments specified in the function definition.&lt;/li&gt;
&lt;li&gt;Any number of arguments up to the maximum can be passed by the caller.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In Python, there are two kinds of arguments you can specify when defining a function. These are &lt;strong&gt;required&lt;/strong&gt; and &lt;strong&gt;optional&lt;/strong&gt; arguments. The key difference between these is that required arguments must be passed when a function is called, while optional are given a default value in the function definition.&lt;/p&gt;
&lt;p&gt;You can see the differences between these two styles in the next example:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add_or_subtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;subtract&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;    &lt;span class=&quot;sd&quot;&gt;&amp;quot;&amp;quot;&amp;quot;Add or subtract two numbers, depending on the value of subtract.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;subtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are defining a function called &lt;code&gt;add_or_subtract()&lt;/code&gt; that has three arguments: &lt;code&gt;num_1&lt;/code&gt;, &lt;code&gt;num_2&lt;/code&gt;, and &lt;code&gt;subtract&lt;/code&gt;. In the function definition, you can see the two types of arguments. The first two arguments, &lt;code&gt;num_1&lt;/code&gt; and &lt;code&gt;num_2&lt;/code&gt;, are required arguments.&lt;/p&gt;
&lt;p&gt;The third argument, &lt;code&gt;subtract&lt;/code&gt;, has a default value assigned to it by specifying a value after an equals sign in the function definition. This means that when the function is called, passing a value for &lt;code&gt;subtract&lt;/code&gt; is optional. If no value is passed, the default as defined in the function definition line will be used. In this case, the default value is &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In the body of the function, you are testing the value of &lt;code&gt;subtract&lt;/code&gt; with the &lt;code&gt;if&lt;/code&gt; statement to determine whether addition or subtraction should be performed. If &lt;code&gt;subtract&lt;/code&gt; is &lt;code&gt;True&lt;/code&gt;, &lt;code&gt;num_2&lt;/code&gt; will be subtracted from &lt;code&gt;num_1&lt;/code&gt;. Otherwise, if &lt;code&gt;subtract&lt;/code&gt; is &lt;code&gt;False&lt;/code&gt;, then &lt;code&gt;num_1&lt;/code&gt; will be added to &lt;code&gt;num_2&lt;/code&gt;. In either case, the result of the arithmetic operation will be returned to the caller.&lt;/p&gt;
&lt;p&gt;In addition to the two types of arguments you can use when &lt;strong&gt;defining&lt;/strong&gt; a function, there are two kinds of arguments you can specify when &lt;strong&gt;calling&lt;/strong&gt; a function. These are called &lt;strong&gt;positional&lt;/strong&gt; and &lt;a href=&quot;https://docs.python.org/3/tutorial/controlflow.html#keyword-arguments&quot;&gt;&lt;strong&gt;keyword&lt;/strong&gt;&lt;/a&gt; arguments. You can see the difference between these in the following example. First, try passing only two arguments to the function:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_or_subtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[1]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;30&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you passed only two arguments to &lt;code&gt;add_or_subtract()&lt;/code&gt;, &lt;code&gt;10&lt;/code&gt; and &lt;code&gt;20&lt;/code&gt;. In this case, you passed these values as positional arguments, and the meaning of the arguments is defined by their position in the function call.&lt;/p&gt;
&lt;p&gt;Since only the two required arguments were passed, &lt;code&gt;subtract&lt;/code&gt; will take on the default value, which is &lt;code&gt;False&lt;/code&gt;. Therefore, 10 and 20 will be added together, which you can see on the output line. Next, try passing a value for &lt;code&gt;subtract&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_or_subtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[2]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;30&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_or_subtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[3]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;-10&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you passed three arguments to &lt;code&gt;add_or_subtract()&lt;/code&gt;, with two different values for the &lt;code&gt;subtract&lt;/code&gt; argument. First, you passed &lt;code&gt;False&lt;/code&gt; on input line 2. The result was the addition of 10 and 20. Then, you passed &lt;code&gt;True&lt;/code&gt; on input line 3, resulting in the difference between 10 and 20, or -10.&lt;/p&gt;
&lt;p&gt;In these examples, you saw that it is possible in Python to define default values for arguments to a function. This means when you call the function, any arguments with default values are optional and do not have to be passed. If no value is passed for any default arguments, the default value will be used. However, you &lt;em&gt;must&lt;/em&gt; pass a value for every argument without a default value. Otherwise, Python will raise an error:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_or_subtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;  File &amp;quot;&amp;lt;ipython-input-4-f9d1f2ae4494&amp;gt;&amp;quot;, line 1, in &amp;lt;module&amp;gt;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;    add_or_subtract(10)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;TypeError: add_or_subtract() missing 1 required positional argument: &amp;#39;num_2&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you have only passed one of the two required arguments to &lt;code&gt;add_or_subtract()&lt;/code&gt;, so Python raises a &lt;code&gt;TypeError&lt;/code&gt;. The error message tells you that you did not pass a value for &lt;code&gt;num_2&lt;/code&gt;, because it does not have a default value.&lt;/p&gt;
&lt;p&gt;In these last three example, you have used &lt;strong&gt;positional&lt;/strong&gt; arguments, so which parameter is assigned to the variables in the function depends on the order they are passed. There is another method to pass arguments to functions in Python, called &lt;strong&gt;keyword&lt;/strong&gt; arguments. To use keyword arguments, you specify the name of the argument in the function call:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [5]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_or_subtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;subtract&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[5]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;-10&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you have used keyword arguments for all three arguments to &lt;code&gt;add_or_subtract()&lt;/code&gt;. Keyword arguments are specified by stating the argument name, then an equals sign, then the value that argument should have. One of the big advantages of keyword arguments is that they make your code more explicit. (As the &lt;em&gt;Zen of Python&lt;/em&gt; says, explicit is better than implicit.) However, they make the code somewhat longer, so it&amp;rsquo;s up to your judgement when to use keyword arguments or not.&lt;/p&gt;
&lt;p&gt;Another benefit of keyword arguments is that they can be specified in any order:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [6]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_or_subtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subtract&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[6]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;-10&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you have specified the three arguments for &lt;code&gt;add_or_subtract()&lt;/code&gt; as keyword arguments, but the order is different from in the function definition. Nonetheless, Python connects the right variables together because they are specified as keywords instead of positional arguments.&lt;/p&gt;
&lt;p&gt;You can also mix positional and keyword arguments together in the same function call. If positional and keyword arguments are mixed together, the positional arguments &lt;em&gt;must&lt;/em&gt; be specified first, before any keyword arguments:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [7]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_or_subtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;subtract&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[7]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;-10&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you have specified the values for &lt;code&gt;num_1&lt;/code&gt; and &lt;code&gt;num_2&lt;/code&gt; using positional arguments, and the value for &lt;code&gt;subtract&lt;/code&gt; using a keyword argument. This is probably the most common case of using keyword arguments, because it provides a good balance between being explicit and being concise.&lt;/p&gt;
&lt;p&gt;Finally, there is one last benefit of using keyword arguments and default values. Spyder, and other IDEs, provide introspection of function definitions. This will tell you the names of all of the defined function arguments, which ones have default arguments, and the value of the default arguments. This can save you time and make your code easier and faster to read.&lt;/p&gt;
&lt;h4 id=&quot;there-are-no-switchcase-blocks-in-python&quot;&gt;There Are No &lt;code&gt;switch&lt;/code&gt;/&lt;code&gt;case&lt;/code&gt; Blocks in Python&lt;/h4&gt;
&lt;p&gt;In MATLAB, you can use &lt;a href=&quot;https://www.mathworks.com/help/matlab/ref/switch.html&quot;&gt;&lt;code&gt;switch&lt;/code&gt;/&lt;code&gt;case&lt;/code&gt; blocks&lt;/a&gt; to execute code by checking the value of a variable for equality with some constants. This type of syntax is quite useful when you know you want to handle a few discrete cases. Try out a &lt;code&gt;switch&lt;/code&gt;/&lt;code&gt;case&lt;/code&gt; block with this example:&lt;/p&gt;
&lt;div class=&quot;highlight matlab&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;disp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;disp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;otherwise&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;disp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;neither&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nor&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you start by defining &lt;code&gt;num&lt;/code&gt; and setting it equal to 10 and on the following lines you test the value of &lt;code&gt;num&lt;/code&gt;. This code will result in the output &lt;code&gt;num is 10&lt;/code&gt; being displayed on the console, since &lt;code&gt;num&lt;/code&gt; is equal to 10.&lt;/p&gt;
&lt;p&gt;This syntax is an interesting comparison of MATLAB vs Python because Python does not have a similar syntax. Instead, you should use an &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;elif&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; block:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num is 10&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num is 20&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num is neither 10 nor 20&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you start by defining &lt;code&gt;num&lt;/code&gt; and setting it equal to &lt;code&gt;10&lt;/code&gt;. On the next several lines you are writing an &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;elif&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; block to check the different values that you are interested in.&lt;/p&gt;
&lt;h4 id=&quot;namespaces-are-one-honking-great-idea-in-python&quot;&gt;Namespaces Are One Honking Great Idea in Python&lt;/h4&gt;
&lt;p&gt;In MATLAB, all functions are found in a single scope. MATLAB has a &lt;a href=&quot;https://www.mathworks.com/help/matlab/matlab_prog/function-precedence-order.html&quot;&gt;defined search order&lt;/a&gt; for finding functions within the current scope. If you define your own function for something that MATLAB already includes, you may get unexpected behavior.&lt;/p&gt;
&lt;p&gt;As you saw in the &lt;em&gt;Zen of Python&lt;/em&gt;, &lt;strong&gt;namespaces&lt;/strong&gt; are one honking great idea. Namespaces are a way to provide different scopes for names of functions, classes, and variables. This means you have to tell Python which library has the function you want to use. This is a good thing, especially in cases where multiple libraries provide the same function.&lt;/p&gt;
&lt;p&gt;For instance, the built-in &lt;code&gt;math&lt;/code&gt; library provides a &lt;a href=&quot;https://realpython.com/python-square-root-function/&quot;&gt;square root function&lt;/a&gt;, as does the more advanced NumPy library. Without namespaces, it would be more difficult to tell Python which square root function you wanted to use.&lt;/p&gt;
&lt;p&gt;To tell Python where a function is located, you first have to &lt;code&gt;import&lt;/code&gt; the library, which creates the namespace for that library&amp;rsquo;s code. Then, when you want to use a function from the library, you tell Python which namespace to look in:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;math&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[2]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;2.0&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, on input line 1 you imported the &lt;code&gt;math&lt;/code&gt; library that is built-in to Python. Then, input line 2 computes the square root of 4 using the square root function from within the &lt;code&gt;math&lt;/code&gt; library. The &lt;code&gt;math.sqrt()&lt;/code&gt; line should be read as &amp;ldquo;from within &lt;code&gt;math&lt;/code&gt;, find &lt;code&gt;sqrt()&lt;/code&gt;.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;import&lt;/code&gt; keyword searches for the named library and binds the namespace to the same name as the library by default. You can read more about how Python searches for libraries in &lt;a href=&quot;https://realpython.com/python-modules-packages/&quot;&gt;Python Modules and Packages – An Introduction&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can also tell Python what name it should use for a library. For instance, it is very common to see &lt;code&gt;numpy&lt;/code&gt; shortened to &lt;code&gt;np&lt;/code&gt; with the following code:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[4]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;2.0&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, input line 3 imports NumPy and tells Python to put the library into the &lt;code&gt;np&lt;/code&gt; namespace. Then, whenever you want to use a function from NumPy, you use the &lt;code&gt;np&lt;/code&gt; abbreviation to find that function. On input line 4, you are computing the square root of 4 again, but this time, using &lt;code&gt;np.sqrt()&lt;/code&gt;. &lt;code&gt;np.sqrt()&lt;/code&gt; should be read as &amp;ldquo;from within NumPy, find &lt;code&gt;sqrt()&lt;/code&gt;.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;There are two main caveats to using namespaces where you should be careful:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;You should not name a variable with the same name as one of the functions built into Python. You can find a complete list of these functions in the &lt;a href=&quot;https://docs.python.org/3/library/functions.html&quot;&gt;Python documentation&lt;/a&gt;. The most common variable names that are also built-in functions and should not be used are &lt;code&gt;dir&lt;/code&gt;, &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;input&lt;/code&gt;, &lt;code&gt;list&lt;/code&gt;, &lt;code&gt;max&lt;/code&gt;, &lt;code&gt;min&lt;/code&gt;, &lt;code&gt;sum&lt;/code&gt;, &lt;code&gt;str&lt;/code&gt;, &lt;code&gt;type&lt;/code&gt;, and &lt;code&gt;vars&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You should not name a Python file (one with the extension &lt;code&gt;.py&lt;/code&gt;) with the same name as a library that you have installed. In other words, you should not create a Python file called &lt;code&gt;math.py&lt;/code&gt;. This is because Python searches the current working directory first when it tries to import a library. If you have a file called &lt;code&gt;math.py&lt;/code&gt;, that file will be found before the built-in &lt;code&gt;math&lt;/code&gt; library and you will probably see an &lt;code&gt;AttributeError&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h4 id=&quot;the-most-recent-unassigned-result-is-available-as-_-in-python&quot;&gt;The Most Recent Unassigned Result Is Available as &lt;code&gt;_&lt;/code&gt; in Python&lt;/h4&gt;
&lt;p&gt;The MATLAB console uses &lt;a href=&quot;https://www.mathworks.com/help/matlab/ref/ans.html&quot;&gt;&lt;code&gt;ans&lt;/code&gt;&lt;/a&gt; to store the result of the most recent calculation if that result was not assigned to a variable. This is really useful when you forgot to assign the result of a calculation to a variable or when you just want to chain a few simple calculations together. To see the differences between MATLAB vs Python, try out this example:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;    60&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ans&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;    70&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you use &lt;code&gt;sum()&lt;/code&gt; to calculate the sum of the array. Since there is no equal sign with a variable name on the left, MATLAB assigns the output from &lt;code&gt;sum()&lt;/code&gt; to &lt;code&gt;ans&lt;/code&gt;. You can then use that variable in further calculations, as you do here by adding 10 to the last result. Note that this will only work in the MATLAB console, not in a script file.&lt;/p&gt;
&lt;p&gt;In the Python console (including the IPython/Spyder console), the output from the most recent calculation is stored in &lt;code&gt;_&lt;/code&gt; (the underscore character). Try the following code:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[1]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;60&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[2]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;70&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you use &lt;code&gt;sum()&lt;/code&gt; to calculate the sum of the list. Since there is no equal sign with a variable name on the left, Python assigns the output from &lt;code&gt;sum()&lt;/code&gt; to the underscore (&lt;code&gt;_&lt;/code&gt;), in addition to printing it on the output line. You can then use that variable in further calculations, as you do here by adding 10 to the last result. Note that this will only work in the Python console, not in a script file.&lt;/p&gt;
&lt;p&gt;In the IPython console, there is one additional feature enabled. You can append a number after the underscore to retrieve the result of any previous line. Try the following code:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[3]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;80&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[4]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;90&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code on input line 3 you are using &lt;code&gt;_1&lt;/code&gt; to mean the value of output line 1, the &lt;code&gt;sum()&lt;/code&gt; line. To that result (60) you are adding 20, producing a result of 80. On input line 4, you are adding 20 to the value of output line 2, accessed with &lt;code&gt;_2&lt;/code&gt;, so the result is 90.&lt;/p&gt;
&lt;p&gt;Notice that the Spyder Variable explorer does not show this variable by default, whereas &lt;code&gt;ans&lt;/code&gt; is shown in the MATLAB Variable explorer. In &lt;a href=&quot;#there-are-no-private-properties-or-methods-in-python&quot;&gt;a few sections&lt;/a&gt;, you&amp;rsquo;ll see why the underscore isn&amp;rsquo;t shown by default and how you can see it.&lt;/p&gt;
&lt;h4 id=&quot;anonymous-functions-are-created-with-the-lambda-keyword-in-python&quot;&gt;Anonymous Functions Are Created With the &lt;code&gt;lambda&lt;/code&gt; Keyword in Python&lt;/h4&gt;
&lt;p&gt;MATLAB uses the the at-symbol (&lt;code&gt;@&lt;/code&gt;) to indicate that what follows is the definition of an &lt;a href=&quot;https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html&quot;&gt;anonymous function&lt;/a&gt;. Anonymous functions are functions that are not defined in a program file and do not use the &lt;code&gt;function&lt;/code&gt; keyword. A program file is a MATLAB file with a filename ending in &lt;code&gt;.m&lt;/code&gt;. Anonymous functions are limited to a single statement so they are intended for simple computations.&lt;/p&gt;
&lt;p&gt;You can try out the differences of anonymous functions in MATLAB vs Python with this example:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sayHello&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;@(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;Hello&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;%s\n&amp;quot;,x);&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sayHello&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;Eleanor&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Hello, Eleanor&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, the first input line defines the anonymous function with one input parameter, &lt;code&gt;x&lt;/code&gt;. The body of the function follows, which uses &lt;code&gt;fprintf()&lt;/code&gt; to format the input into a string. This function is assigned to &lt;code&gt;sayHello&lt;/code&gt;. On the second input line, &lt;code&gt;sayHello()&lt;/code&gt; is executed and passed &lt;code&gt;&quot;Eleanor&quot;&lt;/code&gt; as the value. The result the string &lt;code&gt;Hello, Eleanor&lt;/code&gt; printed on the console.&lt;/p&gt;
&lt;p&gt;Anonymous functions are most often used when you need to pass one function into another function. In these cases, it is often not necessary to assign the function definition to a variable:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;integral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(@(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.^&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;    243&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, the first input line executes &lt;code&gt;integral()&lt;/code&gt;, a function that computes the definite integral of a given function. The first argument to &lt;code&gt;integral()&lt;/code&gt; must be a function, so this is a perfect place to use an anonymous function. Here, your anonymous function squares whatever the input value is. The other two arguments to &lt;code&gt;integral()&lt;/code&gt; are the limits of the integration, such that the result of integrating &lt;code&gt;x.^2&lt;/code&gt; from 0 to 9 is 243.&lt;/p&gt;
&lt;p&gt;Python uses the &lt;code&gt;lambda&lt;/code&gt; keyword to define anonymous functions. Other than this syntax difference, anonymous functions work the same way in Python as in MATLAB:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;say_hello&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Hello, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{x:s}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;say_hello&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Eleanor&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Hello, Eleanor&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, input line 1 defines the &lt;code&gt;lambda&lt;/code&gt; function with one parameter, &lt;code&gt;x&lt;/code&gt;. You use &lt;code&gt;print()&lt;/code&gt; within the function definition to show an f-string with the value of the input parameter. The function is then stored in &lt;code&gt;say_hello()&lt;/code&gt;. Input line 2 evaluates &lt;code&gt;say_hello()&lt;/code&gt; with the input string &lt;code&gt;&quot;Eleanor&quot;&lt;/code&gt; and produces the &lt;code&gt;Hello, Eleanor&lt;/code&gt; output.&lt;/p&gt;
&lt;p&gt;In Python, the official style guide called PEP 8 &lt;a href=&quot;https://www.python.org/dev/peps/pep-0008/#programming-recommendations&quot;&gt;specifically disrecommends&lt;/a&gt; assigning &lt;code&gt;lambda&lt;/code&gt; expressions to variable names, as you saw in the last example. If you want to give a function a name to refer to it several times, the preference is to use the &lt;code&gt;def&lt;/code&gt; syntax and define a full function, even for one-line functions.&lt;/p&gt;
&lt;p&gt;However, &lt;code&gt;lambda&lt;/code&gt; functions are still useful when they are passed as arguments into another function:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;scipy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;integrate&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;integrate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[4]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;(243.0, 2.6978419498391304e-12)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, input line 3 imports the &lt;code&gt;scipy.integrate&lt;/code&gt; library and stores it in the &lt;code&gt;integrate&lt;/code&gt; namespace. On input line 4, you are using &lt;a href=&quot;https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html#scipy.integrate.quad&quot;&gt;&lt;code&gt;quad()&lt;/code&gt; from &lt;code&gt;scipy.integrate&lt;/code&gt;&lt;/a&gt; to compute the integral using quadrature, very similar to &lt;code&gt;integral()&lt;/code&gt; in MATLAB. The first argument to &lt;code&gt;quad()&lt;/code&gt; is the function to be integrated, and you use a &lt;code&gt;lambda&lt;/code&gt; function to specify that &lt;code&gt;x ** 2&lt;/code&gt; should be integrated. The second and third arguments to &lt;code&gt;quad()&lt;/code&gt; specify that the integral should be conducted from 0 to 9.&lt;/p&gt;
&lt;p&gt;You can see that the result on output line 4 has two values, &lt;code&gt;243.0&lt;/code&gt; and &lt;code&gt;2.6978419498391304e-12&lt;/code&gt;. The first value is the result of the integration, and is equal to the result from MATLAB. The second value is an estimate of the absolute error in the result. An error this small is approximately the precision of the numbers used to store the result, so the answer is about as accurate as it can be.&lt;/p&gt;
&lt;p&gt;You can read more about &lt;code&gt;lambda&lt;/code&gt; in &lt;a href=&quot;https://realpython.com/python-lambda/&quot;&gt;How to Use Python lambda Functions&lt;/a&gt; or watch the videos in the &lt;a href=&quot;https://realpython.com/courses/python-lambda-functions/&quot;&gt;How to Use Python Lambda Functions&lt;/a&gt; course.&lt;/p&gt;
&lt;h3 id=&quot;you-will-only-need-these-in-specialized-situations&quot;&gt;You Will Only Need These in Specialized Situations&lt;/h3&gt;
&lt;p&gt;In this section, the examples are more advanced concepts that you will need as you become more advanced in Python development. Some of the examples here deal with developing applications or higher-level code than the other sections. When you see these concepts in other code, you can dig in to them when you feel comfortable.&lt;/p&gt;
&lt;h4 id=&quot;class-definitions-start-with-class-in-python&quot;&gt;Class Definitions Start With &lt;code&gt;class&lt;/code&gt; in Python&lt;/h4&gt;
&lt;p&gt;MATLAB has two ways to define a class. With the first way, you can put all of the &lt;a href=&quot;https://www.mathworks.com/help/matlab/matlab_oop/create-a-simple-class.html&quot;&gt;class definition in a single file&lt;/a&gt; with the name of the class as the filename. Then within the file, you can use the &lt;a href=&quot;https://www.mathworks.com/help/matlab/ref/classdef.html&quot;&gt;&lt;code&gt;classdef&lt;/code&gt; keyword&lt;/a&gt; to define the properties and methods that belong to the class.&lt;/p&gt;
&lt;p&gt;With the second way, you can &lt;a href=&quot;https://www.mathworks.com/help/matlab/matlab_oop/methods-in-separate-files.html&quot;&gt;create a folder that starts with &lt;code&gt;@&lt;/code&gt;&lt;/a&gt; and has the same name as the class. In that folder, you can create a single file that has the same name as the class. The function definition in that file will be used as the class initializer, and it should call &lt;code&gt;class()&lt;/code&gt; to instantiate the class. Methods of the class can be defined in other files in the same folder, where the name of each file must be the same as the name of the method.&lt;/p&gt;
&lt;p&gt;Python only has one way to define a class, using the &lt;a href=&quot;https://docs.python.org/3/tutorial/classes.html&quot;&gt;&lt;code&gt;class&lt;/code&gt; keyword&lt;/a&gt;. Since Python uses indentation to find the end of the class definition, the entire definition must be contained in a single file:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyClass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# The rest of the class definition goes here&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, the first line defines the name of the class. It starts with the &lt;code&gt;class&lt;/code&gt; keyword, followed by the name of the class and a colon. Underneath this line, all of the code that is part of the class definition (methods and attributes) must be indented. Once a line of code starts in the same column as the &lt;code&gt;c&lt;/code&gt; in &lt;code&gt;class&lt;/code&gt;, the class definition will be ended.&lt;/p&gt;
&lt;p&gt;The second line in this code is a comment to note that the rest of the class definition would follow the &lt;code&gt;class&lt;/code&gt; line.&lt;/p&gt;
&lt;p&gt;As in all object-oriented code, Python classes can inherit from superclasses. The superclass of a given class can be given as a parameter in the class definition, as shown below:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyClass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MySuperClass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# The rest of the class definition goes here&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, the only change is that the name of the superclass is listed inside round brackets before the colon.&lt;/p&gt;
&lt;h4 id=&quot;there-are-no-private-properties-or-methods-in-python&quot;&gt;There Are No Private Properties or Methods in Python&lt;/h4&gt;
&lt;p&gt;MATLAB allows class properties and method to be set as one of four &lt;a href=&quot;https://www.mathworks.com/help/matlab/matlab_oop/method-attributes.html&quot;&gt;&lt;code&gt;Access&lt;/code&gt; options&lt;/a&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;public&lt;/code&gt;&lt;/strong&gt;: Access to the property or method is unrestricted.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;protected&lt;/code&gt;&lt;/strong&gt;: Access to the property or method is only allowed in this class or subclasses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;private&lt;/code&gt;&lt;/strong&gt;: Access to the property or method is only allowed in this class.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;meta.class&lt;/code&gt; or &lt;code&gt;{meta.class}&lt;/code&gt;&lt;/strong&gt;: Access to the property or method is only allowed in the listed class or classes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This allows you to specifically control the ways that a property or class method can be accessed.&lt;/p&gt;
&lt;p&gt;In Python, there is no way to set a class or instance attribute or method as protected or private. All classes and class instances can have their attributes and methods changed at runtime. Python&amp;rsquo;s convention is that attributes and methods that start with an underscore (&lt;code&gt;_&lt;/code&gt;) are intended to be private, or at least non-public. However, this convention is not enforced by any checks in the language and all attributes and methods can be modified by the user at runtime.&lt;/p&gt;
&lt;p&gt;As you saw in the section about &lt;a href=&quot;#the-most-recent-unassigned-result-is-available-as-_-in-python&quot;&gt;using the underscore to retrieve values in the console&lt;/a&gt;, the underscore is not visible by default in the Spyder &lt;em&gt;Variable explorer&lt;/em&gt;. This is because Spyder and other tools respect the convention that underscore indicates something should be non-public. However, underscores can be shown in the Spyder Variable explorer if you click the gear icon in the top right of that pane and uncheck the &lt;em&gt;Exclude private variables&lt;/em&gt; item. This will also show other non-public variables as well.&lt;/p&gt;
&lt;p&gt;Python uses several special methods that start with a double-underscore (&lt;code&gt;__&lt;/code&gt;), called &lt;strong&gt;dunder methods&lt;/strong&gt;, to implement specific behavior for classes. The most commonly used dunder method is &lt;code&gt;__init__()&lt;/code&gt;, which is the class initializer or constructor. You can read a lot more about dunder methods in &lt;a href=&quot;https://dbader.org/blog/python-dunder-methods&quot;&gt;Enriching Your Python Classes With Dunder (Magic, Special) Methods&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If you want more information about Python&amp;rsquo;s classes, you can read &lt;a href=&quot;https://realpython.com/oop-in-python-vs-java/&quot;&gt;Object-Oriented Programming in Python vs Java&lt;/a&gt;. Even though that article is about Java, Java is similar to the MATLAB OOP paradigm in terms of the nature of attributes and methods.&lt;/p&gt;
&lt;h4 id=&quot;a-class-refers-to-itself-as-self-in-python&quot;&gt;A Class Refers to Itself as &lt;code&gt;self&lt;/code&gt; in Python&lt;/h4&gt;
&lt;p&gt;MATLAB uses the name &lt;code&gt;obj&lt;/code&gt; when a class wants to refer to the current instance of itself. The &lt;code&gt;obj&lt;/code&gt; should be the first argument passed to an &lt;a href=&quot;https://www.mathworks.com/help/matlab/matlab_oop/ordinary-methods.html&quot;&gt;ordinary method&lt;/a&gt;. MATLAB also defines &lt;a href=&quot;https://www.mathworks.com/help/matlab/matlab_oop/static-methods.html&quot;&gt;static methods&lt;/a&gt; that have no reference to the class instance.&lt;/p&gt;
&lt;p&gt;Python uses the name &lt;code&gt;self&lt;/code&gt; when a class wants to refer to the current instance of itself, but this is actually only a convention. You can call the first argument to an &lt;strong&gt;instance method&lt;/strong&gt; any name you want, but &lt;code&gt;self&lt;/code&gt; is the most common convention. Python also defines static methods that don&amp;rsquo;t take an argument of the class instance and class methods that take an argument of the class object instead of the instance. You can read more about instance, static, and class methods in &lt;a href=&quot;https://realpython.com/instance-class-and-static-methods-demystified/&quot;&gt;Python&amp;rsquo;s Instance, Class, and Static Methods Demystified&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id=&quot;there-is-one-string-type-in-python&quot;&gt;There Is One String Type in Python&lt;/h4&gt;
&lt;p&gt;In MATLAB, strings of characters are stored in &lt;strong&gt;string arrays&lt;/strong&gt; when you use double quotes (&lt;code&gt;&quot;&lt;/code&gt;) or in &lt;strong&gt;character arrays&lt;/strong&gt; if you use single quotes (&lt;code&gt;&#39;&lt;/code&gt;). If you use both single and double quotes in an array assignment, the array will be promoted to a string array.&lt;/p&gt;
&lt;p&gt;In character arrays, each character in the string occupies one column in the array. For multidimensional character arrays, each row of the array must have the same number of characters, which is to say, the same number of columns. This is shown in the example below:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;charArray&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;Real&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39;Python&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;Dimensions of arrays being concatenated are not consistent.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;charArray&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;MATLAB&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39;Python&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;charArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;     2     6&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, line 1 shows an attempt to define a 2-row character array using single quotes. However, the number of characters in &lt;code&gt;Real&lt;/code&gt; is not the same as in &lt;code&gt;Python&lt;/code&gt;, so MATLAB shows an error message that the dimensions are not consistent.&lt;/p&gt;
&lt;p&gt;On line 4, you successfully create a character array, and on the third input line you are checking the size of the array. The output shows that there are 2 rows, as expected, and 6 columns, since the length of both &lt;code&gt;MATLAB&lt;/code&gt; and &lt;code&gt;Python&lt;/code&gt; are 6 characters.&lt;/p&gt;
&lt;p&gt;This is not the case for string arrays. In string arrays, each string occupies one column in the array and each row in a multidimensional array must have the same number of strings, although each string can have different length. This is shown in the example below:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stringArray&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;Real&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &amp;quot;&lt;span class=&quot;n&quot;&gt;Python&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &amp;quot;&lt;span class=&quot;n&quot;&gt;Real&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;Error using vertcat&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;Dimensions of arrays being concatenated are not consistent.&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stringArray&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&amp;quot;&lt;span class=&quot;n&quot;&gt;Real&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &amp;quot;&lt;span class=&quot;n&quot;&gt;Python&lt;/span&gt;&amp;quot;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stringArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;     2     1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, line 1 shows an attempt to define a 2-row string array using double quotes. However, the number of strings on the first row (2) does not match the number of strings in the second row (1), so MATLAB raises an error.&lt;/p&gt;
&lt;p&gt;On line 5, you successfully create a string array. Notice that even though the number of characters is different between &lt;code&gt;Real&lt;/code&gt; and &lt;code&gt;Python&lt;/code&gt;, MATLAB is able to create the string array. On line 6 you are checking the size of the string array, which shows that there are 2 rows and 1 column, as expected.&lt;/p&gt;
&lt;p&gt;In Python, there is only one string literal type, called &lt;code&gt;str&lt;/code&gt;. You can create a string literal using single quotes (&lt;code&gt;&#39;&lt;/code&gt;) or double quotes (&lt;code&gt;&quot;&lt;/code&gt;), there is no difference between the two definitions. However, there are some good arguments to prefer double quotes when defining string literals in Python, which are well expressed by the &lt;a href=&quot;https://black.readthedocs.io/en/stable/the_black_code_style.html#strings&quot;&gt;Black code formatting library&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;There is one additional way to define strings in Python, using triple-single-quotes (&lt;code&gt;&#39;&#39;&#39;&lt;/code&gt;) or triple-double-quotes (&lt;code&gt;&quot;&quot;&quot;&lt;/code&gt;). This method of creating strings allows the strings to be defined across multiple lines with the newline characters retained. You can see an example of this in the section about &lt;a href=&quot;#comments-start-with-in-python&quot;&gt;comments and docstrings&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can read a lot more about defining strings in &lt;a href=&quot;https://realpython.com/python-data-types/#strings&quot;&gt;Basic Data Types in Python&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/python-strings/&quot;&gt;Strings and Character Data in Python&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can generate similar data structures to the string arrays and character arrays in MATLAB using NumPy in Python. NumPy has several data types, or &lt;strong&gt;dtypes&lt;/strong&gt;, that are related to strings. In Python 3, the default string dtype for arrays is a fixed-width Unicode string:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Real&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Python&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[3]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;array([&amp;#39;Real&amp;#39;, &amp;#39;Python&amp;#39;], dtype=&amp;#39;&amp;lt;U6&amp;#39;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are importing the NumPy library on input line 1 and assigning it to the &lt;code&gt;np&lt;/code&gt; abbreviation. On input line 2, you are creating a NumPy &lt;code&gt;array&lt;/code&gt; with 2 string elements, &lt;code&gt;Real&lt;/code&gt; and &lt;code&gt;Python&lt;/code&gt;, and assigning the array to &lt;code&gt;arr&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;On input line 3, you are showing the value of &lt;code&gt;arr&lt;/code&gt;. The output from the third line shows that &lt;code&gt;arr&lt;/code&gt; is storing an &lt;code&gt;array&lt;/code&gt; that has 2 elements, &lt;code&gt;&#39;Real&#39;&lt;/code&gt; and &lt;code&gt;&#39;Python&#39;&lt;/code&gt;, as expected. Notice that although you defined the array with double-quote strings, Python is displaying them with single-quote strings. Remember that there is no difference between single and double quotes in Python.&lt;/p&gt;
&lt;p&gt;Output line 3 also shows the dtype of the data in the array. For this array, the dtype is &lt;code&gt;&amp;lt;U6&lt;/code&gt;. The three characters here represent the aspects of how the strings are arranged in memory. The &lt;a href=&quot;https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html#index-6&quot;&gt;&lt;code&gt;&amp;lt;&lt;/code&gt; means that&lt;/a&gt; the byte order of the array is &lt;a href=&quot;https://docs.scipy.org/doc/numpy/glossary.html#term-little-endian&quot;&gt;little endian&lt;/a&gt;. The &lt;code&gt;U&lt;/code&gt; means the string is &lt;a href=&quot;https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html#index-6&quot;&gt;of the Unicode type&lt;/a&gt;. Finally, the &lt;code&gt;6&lt;/code&gt; means the maximum length of an element is 6 characters. This was chosen as the length of the longest string in the input.&lt;/p&gt;
&lt;p&gt;Notice that the string &lt;code&gt;Real&lt;/code&gt; only has 4 characters. In NumPy string dtype arrays, elements can have fewer than the maximum number of characters without problems, but assigning to elements with strings that are longer than the maximum length will truncate the input:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;My favorite language is Python&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [5]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[5]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;array([&amp;#39;My fav&amp;#39;, &amp;#39;Python&amp;#39;], dtype=&amp;#39;&amp;lt;U6&amp;#39;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are attempting to reassign the first element of the array with the string &lt;code&gt;My favorite language is Python&lt;/code&gt;. Clearly, this string is longer than 6 characters, so it is truncated to only 6 when it is assigned, &lt;code&gt;My fav&lt;/code&gt;. (The space counts as 1 character.)&lt;/p&gt;
&lt;p&gt;If you want to create an array that can hold strings of any length, you should pass the &lt;code&gt;object&lt;/code&gt; dtype when you create the array:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [6]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Real&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Python&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [7]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[7]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;array([&amp;#39;Real&amp;#39;, &amp;#39;Python&amp;#39;], dtype=object)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating a new array, &lt;code&gt;arr_2&lt;/code&gt;, with two elements again, but this time you specified the dtype to be &lt;code&gt;object&lt;/code&gt;, which you confirmed by showing the output on the console. Now you should see how the &lt;code&gt;object&lt;/code&gt; dtype affects assigning long strings to an element:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [8]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;My favorite language is Python&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [9]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[9]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;array([&amp;#39;My favorite language is Python&amp;#39;, &amp;#39;Python&amp;#39;], dtype=object)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are again assigning the first element of the array to have the value &lt;code&gt;My favorite language is Python&lt;/code&gt;. You can see from the output line that that string is stored as the first element of the array without truncation, because the dtype is &lt;code&gt;object&lt;/code&gt;. The disadvantage of using the &lt;code&gt;object&lt;/code&gt; dtype is that it is usually much slower than the more specific &lt;code&gt;U&lt;/code&gt; dtype, because it has to create a a whole Python object for each element rather than just a NumPy-optimized Unicode object.&lt;/p&gt;
&lt;p&gt;One other difference you will notice from MATLAB is how the shape or size of the array is determined:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [10]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[10]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;(2,)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [11]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[11]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;(2,)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, we are printing the shape of &lt;code&gt;arr&lt;/code&gt; and &lt;code&gt;arr_2&lt;/code&gt;. Notice that they both have the same shape, two elements in a one-dimensional array. This is similar to the string array from MATLAB, where each string counts as one element in the array. However, the fact that NumPy arrays with the &lt;code&gt;U&lt;/code&gt; dtype have a fixed maximum size behaves more like the character array from MATLAB. You&amp;rsquo;ll see more about differences in how MATLAB and NumPy compute the shape of arrays &lt;a href=&quot;#one-dimensional-arrays-are-vectors-in-numpy&quot;&gt;in a later section&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id=&quot;libraries-are-not-automatically-reloaded-in-python&quot;&gt;Libraries Are Not Automatically Reloaded in Python&lt;/h4&gt;
&lt;p&gt;When executing a function or script, MATLAB will always use the most up-to-date copy of the file on the disk. Therefore, as you&amp;rsquo;re developing a script, you can run it in the console many times and new changes that you make will automatically be picked up.&lt;/p&gt;
&lt;p&gt;Python operates somewhat differently. Remember that when you want to access code from a file, you have to &lt;code&gt;import&lt;/code&gt; it into a namespace. When Python imports a file or module, it only reads the code the first time it is imported. This saves quite a bit of time if you&amp;rsquo;re importing the same file several times. However, if you&amp;rsquo;re testing your code in the interactive console prompt as you work on it, Python will not pick up any changes if you &lt;code&gt;import&lt;/code&gt; it again.&lt;/p&gt;
&lt;p&gt;When you&amp;rsquo;re developing a module, you have a few options to have Python reload your code when it is imported. If you&amp;rsquo;re using the Spyder IDE, this is not a problem at all, since Spyder has an automatic &lt;a href=&quot;https://docs.spyder-ide.org/ipythonconsole.html#using-umr-to-reload-changed-modules&quot;&gt;User Module Reloading&lt;/a&gt; feature enabled by default.&lt;/p&gt;
&lt;p&gt;Otherwise, if you&amp;rsquo;re using the IPython console outside of Spyder, or the Jupyter Notebook, you can use a magic command defined in those interpreters called &lt;a href=&quot;https://ipython.readthedocs.io/en/stable/config/extensions/autoreload.html&quot;&gt;&lt;code&gt;autoreload&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;load_ext&lt;/span&gt; autoreload
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;autoreload&lt;/span&gt; 2
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are using the &lt;code&gt;load_ext&lt;/code&gt; magic command to load the &lt;code&gt;autoreload&lt;/code&gt; extension. In IPython and Jupyter Notebooks, commands prefixed with the percent sign &lt;code&gt;%&lt;/code&gt; are magic commands. The &lt;code&gt;autoreload&lt;/code&gt; extension defines the &lt;code&gt;autoreload&lt;/code&gt; magic function, which you use on input line 2. You are passing the parameter &lt;code&gt;2&lt;/code&gt; to the &lt;code&gt;autoreload&lt;/code&gt; magic function, which means that all modules should be reloaded every time a line of code is executed.&lt;/p&gt;
&lt;h2 id=&quot;an-overview-of-basic-array-operations&quot;&gt;An Overview of Basic Array Operations&lt;/h2&gt;
&lt;p&gt;As you have seen, Python does not include a high-speed library for arrays in its standard library. However, the excellent NumPy library is easily available if you install Anaconda. NumPy functions as the &lt;em&gt;de facto&lt;/em&gt; array and matrix library for Python.&lt;/p&gt;
&lt;p&gt;NumPy has two array-like types:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;numpy.ndarray&lt;/code&gt;, also known as &lt;code&gt;numpy.array&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;numpy.matrix&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The main difference between these two types is that the &lt;code&gt;ndarray&lt;/code&gt; can be any number of dimensions, while the &lt;code&gt;matrix&lt;/code&gt; is limited to exactly two dimensions. For &lt;code&gt;ndarray&lt;/code&gt;, all operations such as addition, subtraction, multiplication, exponentiation, and division operate element-wise. However, for the &lt;code&gt;matrix&lt;/code&gt; type, operations like multiplication and exponentiation are matrix operations.&lt;/p&gt;
&lt;p&gt;When you&amp;rsquo;re converting from MATLAB, the &lt;code&gt;matrix&lt;/code&gt; type may seem more familiar. It offers similar behavior that you may be used to from MATLAB in terms of operation syntax. However, NumPy &lt;a href=&quot;https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html#array-or-matrix-which-should-i-use&quot;&gt;strongly recommends&lt;/a&gt; that you use the &lt;code&gt;ndarray&lt;/code&gt; type because it is more flexible and because &lt;code&gt;matrix&lt;/code&gt; will eventually be removed.&lt;/p&gt;
&lt;p&gt;In the rest of this section, you will get to know the major differences between MATLAB and NumPy arrays. You can go in-depth on how to use NumPy arrays by reading &lt;a href=&quot;https://realpython.com/numpy-array-programming/&quot;&gt;Look Ma, No For-Loops: Array Programming With NumPy&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;basic-mathematical-operators-work-element-wise-in-numpy&quot;&gt;Basic Mathematical Operators Work Element-Wise in NumPy&lt;/h3&gt;
&lt;p&gt;MATLAB, with its heritage as a matrix scripting language, assumes that all arithmetic operators will be operating on arrays. Therefore, MATLAB treats the multiplication of matrices or vectors as matrix multiplication. Consider this example:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Error using  *&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Incorrect dimensions for matrix multiplication. Check that the number of&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;columns in the first matrix matches the number of rows in the second&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;matrix. To perform elementwise multiplication, use &amp;#39;.*&amp;#39;.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating two 1x3 matrices, &lt;code&gt;arr_1&lt;/code&gt;, and &lt;code&gt;arr_2&lt;/code&gt;. Then, you are attempting to multiply them together. For these 1xN arrays, this is equivalent to taking the dot or scalar product. However, the scalar product only works when the left operand is 1xN and the right is Nx1, so MATLAB produces an error message and suggests the dot-star operator (&lt;code&gt;.*&lt;/code&gt;) as the proper syntax for element-wise multiplication:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;     4    10    18&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are performing the element-wise multiplication of &lt;code&gt;arr_1&lt;/code&gt; and &lt;code&gt;arr_2&lt;/code&gt;. This multiplies the first element of &lt;code&gt;arr_1&lt;/code&gt; with the first element of &lt;code&gt;arr_2&lt;/code&gt; (&lt;code&gt;4*1 = 4&lt;/code&gt;), second with second (&lt;code&gt;2*5 = 10&lt;/code&gt;), and third with third (&lt;code&gt;3*6 = 18&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;To perform the scalar product, you can take the transpose of &lt;code&gt;arr_2&lt;/code&gt; to convert it to a 3x1 array:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;    32&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are performing matrix multiplication with &lt;code&gt;arr_1&lt;/code&gt; and the transpose of &lt;code&gt;arr_2&lt;/code&gt;. Note that you can use either &lt;code&gt;transpose()&lt;/code&gt; or the quote operator (&lt;code&gt;&#39;&lt;/code&gt;) to take the transpose of &lt;code&gt;arr_2&lt;/code&gt;. Since &lt;code&gt;arr_1&lt;/code&gt; is 1x3 and &lt;code&gt;transpose(arr_2)&lt;/code&gt; is 3x1, this results in the scalar, or dot, product.&lt;/p&gt;
&lt;p&gt;With NumPy arrays, operations like multiplication with the asterisk (&lt;code&gt;*&lt;/code&gt;) operate element-wise by default:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[4]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;np.array([ 4, 10, 18])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are first importing the NumPy package and assigning it to the name &lt;code&gt;np&lt;/code&gt;. Then you are creating two one-dimensional arrays. Notice the syntax for creating arrays in NumPy. It starts with &lt;code&gt;np.array()&lt;/code&gt;, which should be read as &amp;ldquo;from within &lt;code&gt;np&lt;/code&gt;, find &lt;code&gt;array()&lt;/code&gt;.&amp;rdquo; Then, you have to pass a Python list or tuple to the array constructor that contains the elements of the array. In this case, you are passing a Python list, denoted by the square brackets.&lt;/p&gt;
&lt;p&gt;Finally, on input line 4, you are multiplying &lt;code&gt;arr_1&lt;/code&gt; and &lt;code&gt;arr_2&lt;/code&gt;. Notice that the result on output line 4 is another array with the elements 4, 10, and 18, the same result as the element-wise multiplication in MATLAB.&lt;/p&gt;
&lt;p&gt;If you want to perform the dot or scalar product for two arrays in NumPy, you have two options. The preferred option is to use the matrix multiplication operator (&lt;code&gt;@&lt;/code&gt;) added in Python 3.5. You may see some older code also use &lt;code&gt;dot()&lt;/code&gt; from the NumPy library and pass the two arrays:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [5]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[5]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;32&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [6]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[6]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;32&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, input line 5 uses the matrix multiplication operator to find the scalar product of &lt;code&gt;arr_1&lt;/code&gt; and &lt;code&gt;arr_2&lt;/code&gt;. As expected, the result is 32. Input line 5 uses &lt;code&gt;dot()&lt;/code&gt; and should be read as &amp;ldquo;from within &lt;code&gt;np&lt;/code&gt;, find &lt;code&gt;dot()&lt;/code&gt; and pass &lt;code&gt;arr_1&lt;/code&gt; and &lt;code&gt;arr_2&lt;/code&gt;.&amp;rdquo; You can see that the result is identical.&lt;/p&gt;
&lt;p&gt;Notice that NumPy did not require you to transpose &lt;code&gt;arr_2&lt;/code&gt; before performing the scalar product. You&amp;rsquo;ll learn more about this feature in the next section.&lt;/p&gt;
&lt;h3 id=&quot;one-dimensional-arrays-are-vectors-in-numpy&quot;&gt;One-Dimensional Arrays Are Vectors in NumPy&lt;/h3&gt;
&lt;p&gt;As you saw in the last section, MATLAB insists that the dimensions of arrays align when performing matrix multiplication, while NumPy is a little bit more flexible. This is because of how one-dimensional arrays are treated in MATLAB versus in NumPy.&lt;/p&gt;
&lt;p&gt;In MATLAB, every array always has at least two dimensions, even if only implicitly. You can see this by checking the &lt;code&gt;size()&lt;/code&gt; of a single number:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;     1     1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, you are finding the size of the integer 1. You can see that the result is an array with 1 row and 1 column.&lt;/p&gt;
&lt;p&gt;You can create row vectors or column vectors in MATLAB, and switch between them with the transpose operator (&lt;code&gt;&#39;&lt;/code&gt;) or &lt;code&gt;transpose()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;  3×1 logical array&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;   1&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;   1&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;   1&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;  1×3 logical array&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;   1   1   1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating two vectors: &lt;code&gt;arr_1&lt;/code&gt; and &lt;code&gt;arr_2&lt;/code&gt;. Arrays with values in one dimension only are called vectors. &lt;code&gt;arr_1&lt;/code&gt; is a row vector because the elements are arranged in one row and three columns, whereas &lt;code&gt;arr_2&lt;/code&gt; is a column vector because the elements are arranged in three rows and one column. In MATLAB, elements are put into different columns by separating them with a comma in the assignment and elements are put into different rows by separating them with a semicolon.&lt;/p&gt;
&lt;p&gt;Then, you are checking the equality of the transpose of &lt;code&gt;arr_1&lt;/code&gt; with &lt;code&gt;arr_2&lt;/code&gt;, and you find that all of the elements are equal and the result is a column vector of logical values. Finally, you are checking the equality of the transpose of &lt;code&gt;arr_2&lt;/code&gt; with &lt;code&gt;arr_1&lt;/code&gt;, and you find that all of the elements are equal and the result is a row vector of logical values.&lt;/p&gt;
&lt;p&gt;You can see that in MATLAB, even vectors have two dimensions associated with them: &lt;strong&gt;rows&lt;/strong&gt; and &lt;strong&gt;columns&lt;/strong&gt;. When the transpose is performed, the rows are switched with the columns, and the shape of the array is changed. This means there are two types of vectors in MATLAB: &lt;strong&gt;row-vectors&lt;/strong&gt; and &lt;strong&gt;column-vectors&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In NumPy, there are three types of one-dimensional arrays or vectors. The default is an N-element vector with only one dimension. This is different from the default in MATLAB, where every array has at least 2 dimensions. This one-dimensional vector in NumPy does not have a sense of rows and columns, since for a one-dimensional structure, it does not matter in general whether the elements are stored in rows or in columns, only how many elements there are.&lt;/p&gt;
&lt;p&gt;You can see an example of creating this kind of array in the following example. In the next few examples, there are extra spaces added before and after parentheses to clarify the syntax. These spaces are usually not considered good Python style, but they&amp;rsquo;re in the example to help you see what&amp;rsquo;s going on:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_vec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_vec&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[3]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;(3,)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating a default 3-element vector in NumPy. On input line 1, you import NumPy and make it available under &lt;code&gt;np&lt;/code&gt;. On input line 2 you are creating the array and storing it in &lt;code&gt;arr_vec&lt;/code&gt;. You are passing the list &lt;code&gt;[1, 2, 3]&lt;/code&gt; to &lt;code&gt;array()&lt;/code&gt;, where the list has 3 elements and none of the elements are themselves lists. This creates the 3-element array with only one dimension.&lt;/p&gt;
&lt;p&gt;You can verify that this is the case by displaying the shape of the array, as shown on input line 3. That line should be read as &amp;ldquo;from within &lt;code&gt;arr_vec&lt;/code&gt; (an array), find the &lt;code&gt;shape&lt;/code&gt;.&amp;rdquo; The &lt;code&gt;shape&lt;/code&gt; of the array is equivalent to &lt;code&gt;size()&lt;/code&gt; in MATLAB. In this case, the shape is &lt;code&gt;(3,)&lt;/code&gt;, indicating there are three elements and only one dimension, since there is not a second number after the comma.&lt;/p&gt;
&lt;p&gt;You can also create row-vectors and column-vectors in NumPy, analogous to the row-vectors and column-vectors in MATLAB. NumPy&amp;rsquo;s &lt;code&gt;array()&lt;/code&gt; takes a flat list or a nested list as input. Using the flat list gets you a one-dimensional, N-element vector. By using the nested list, you can create arrays of any dimension that you want. A nested list means that there are one or more lists contained within an outer list. Here&amp;rsquo;s an example of a nested list:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you see an outer list that has 2 elements. Each of these 2 elements of the outer list is another, nested, list that has three elements, the integers 1-3 and 4-6. In terms of arrays, you can think of the number of elements of each inner list as the number of columns, and the number of nested lists is the number of rows. This is easier to see if you change the formatting:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code is still valid Python syntax, but it emphasizes how the inner lists are each a row of the array, and the number of elements in each inner list is the number of columns. In this case, we would have an array with 2 rows and 3 columns. We can use these nested lists to create row-vectors and column-vectors in NumPy arrays:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_row&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [5]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[5]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;(1, 3)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating a row array or vector by using a nested list. Input line 4 is passing &lt;code&gt;[[1, 2, 3]]&lt;/code&gt; to &lt;code&gt;array()&lt;/code&gt;. You can break out the formatting of this nested list to see how it looks:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As you can see, there is one row in this nested list with three columns. On input line 5, you are displaying the shape of this array. As expected, the shape is &lt;code&gt;(1, 3)&lt;/code&gt;, or one row with three columns.&lt;/p&gt;
&lt;p&gt;Finally, you can create a column array by including three nested lists in the input:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [6]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_col&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [7]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[7]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;(3, 1)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, input line 6 is passing &lt;code&gt;[[1], [2], [3]]&lt;/code&gt; to the array constructor. You can break out the formatting of this nested list to see how it looks:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As you can see, there are three rows in this nested list with one column each. On input line 7, you are displaying the shape of this array. As expected, the shape is &lt;code&gt;(3, 1)&lt;/code&gt;, or three rows with one column.&lt;/p&gt;
&lt;p&gt;Since the general N-element vector has no sense of rows and columns, NumPy is able to shape the vector in whatever way makes sense for the operation being performed. You saw this in the last section, where the NumPy array did not need to be transposed to perform the scalar product, whereas the MATLAB array did need to be transposed.&lt;/p&gt;
&lt;p&gt;Trying to take the transpose of the N-element vector does not change the shape of the array. You can take the transpose using either &lt;code&gt;np.transpose()&lt;/code&gt; or the &lt;code&gt;.T&lt;/code&gt; attribute of the array:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [8]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_vec_transp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transpose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_vec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [9]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_vec_transp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[9]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;(3,)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are taking the transpose of the N-element vector &lt;code&gt;arr_vec&lt;/code&gt; and printing its shape. Notice that the shape is the same as the shape of the original &lt;code&gt;arr_vec&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;However, if you are using row-vectors and column-vectors, you will need to ensure that the dimensions are appropriate for the particular operation. For instance, trying to take the scalar product of the row vector with itself will result in an error:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [10]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_row&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_row&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;  File &amp;quot;&amp;lt;ipython-input-10-2b447c0bc8d5&amp;gt;&amp;quot;, line 1, in &amp;lt;module&amp;gt;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;    arr_row @ arr_row&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;with gufunc signature (n?,k),(k,m?)-&amp;gt;(n?,m?) (size 1 is different from 3)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, trying to find the scalar product of the row vector with itself results in a &lt;code&gt;ValueError&lt;/code&gt; informing you that the dimensions of the arrays are not aligned. Using &lt;code&gt;dot()&lt;/code&gt; gives the same error but a slightly different message:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [11]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;  File &amp;quot;&amp;lt;ipython-input-11-d6e191b317ae&amp;gt;&amp;quot;, line 1, in &amp;lt;module&amp;gt;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;    np.dot(arr_row, arr_row)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are using &lt;code&gt;dot()&lt;/code&gt; from the &lt;code&gt;np&lt;/code&gt; namespace to attempt to find the scalar product of two 1x3 row-vectors. Since this operation is not permitted, NumPy raises a &lt;code&gt;ValueError&lt;/code&gt;, similar to the matrix multiplication operator.&lt;/p&gt;
&lt;p&gt;Instead, you need to take the transpose of one of the arguments:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [12]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[12]:&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;array([[1],&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       [2],&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       [3]])&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [13]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sc_prod&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_row&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [14]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sc_prod&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[14]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;array([[14]])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;On input line 12, you are taking the transpose of the row vector to turn it into a column vector using the transpose attribute (&lt;code&gt;.T&lt;/code&gt;). This is shown in corresponding output line, where the elements are arranged to form a column for printing purposes. Then, you are taking the scalar product of the vector with its transpose, producing an array with a single value, 14. Notice that this is a 1x1 array, so to access just the value, you need to access the first element in each dimension:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [15]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sc_prod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[15]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;(1, 1)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [16]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sc_prod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[16]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;14&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are verifying that the shape is 1x1, and then accessing the first element in each dimension located at the 0th index. Remember that Python uses 0 as the first index, not 1.&lt;/p&gt;
&lt;p&gt;You can use the nested lists to create arrays of any shape that you want. To create a three-by-three array (two-dimensional), simply include three elements in each of your three nested lists:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [17]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [18]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[18]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;(3, 3)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you have nested three lists with three elements each into the constructor. As shown by the shape, this produces a 3x3 array with the elements 1 through 9.&lt;/p&gt;
&lt;h3 id=&quot;creating-arrays-is-very-flexible-in-numpy&quot;&gt;Creating Arrays Is Very Flexible in NumPy&lt;/h3&gt;
&lt;p&gt;MATLAB and NumPy both allow you to explicitly specify the specific elements in an array, as you have seen in the previous section. In addition to this direct creation of arrays, both MATLAB and NumPy support a number of other methods to create arrays without explicitly specifying each element. The NumPy project maintains a detailed list of the &lt;a href=&quot;https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html#table-of-rough-matlab-numpy-equivalents&quot;&gt;equivalent functions between MATLAB and NumPy&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Many functions operate identically between MATLAB and NumPy. This includes commonly used functions like &lt;a href=&quot;https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html&quot;&gt;&lt;code&gt;linspace()&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://docs.scipy.org/doc/numpy/reference/generated/numpy.logspace.html&quot;&gt;&lt;code&gt;logspace()&lt;/code&gt;&lt;/a&gt; to generate evenly spaced data and &lt;a href=&quot;https://docs.scipy.org/doc/numpy/reference/generated/numpy.ones.html&quot;&gt;&lt;code&gt;ones()&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html#numpy.zeros&quot;&gt;&lt;code&gt;zeros()&lt;/code&gt;&lt;/a&gt; to generate arrays of a given shape filled with ones and zeros, respectively. The full list of ways to create arrays in NumPy is listed &lt;a href=&quot;https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html&quot;&gt;in the official documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The one big difference between MATLAB and NumPy in terms of array creation routines is that MATLAB supports simply using the colon to create an array, while NumPy does not. Instead, NumPy uses &lt;a href=&quot;https://realpython.com/how-to-use-numpy-arange/&quot;&gt;&lt;code&gt;arange()&lt;/code&gt;&lt;/a&gt; to create an array between specified values.&lt;/p&gt;
&lt;p&gt;In MATLAB, you can use a &lt;a href=&quot;https://www.mathworks.com/help/matlab/ref/colon.html&quot;&gt;colon&lt;/a&gt; to create an array specification range. In general, you can use up to 2 colons in a specification. The syntax is as follows:&lt;/p&gt;
&lt;div class=&quot;highlight matlab&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this syntax, the, first method only uses one colon and specifies the start and stop values. The second method includes a second colon, where the value before the first colon is the start, the middle value is the step, and the last value is the stop.&lt;/p&gt;
&lt;p&gt;Try out these examples to experiment with this syntax:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;arr_1 =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;     1     2     3     4     5     6&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;     1     6&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, you are using the single colon with the start and stop to generate an array with the values from 1 to 6. You can see that when the step is omitted, it defaults to a value of 1. Notice that MATLAB includes both the start and the stop values in the array, and that the size of the array is 6 elements long. Next, change the value of the step size to create a new array:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;arr_2 =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;     1     3     5&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, you are using the two colons syntax with the start, step, and stop. The start value is 1, the step is 2, and the stop value is 6, so MATLAB starts with 1, increments to 3, and then to 5. The next step would exceed the stop value, so MATLAB does not include the stop value in the array. Next, change the starting value to create another new array:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_3&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;arr_3 =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;     2     4     6&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, you are again using the two colon method, but you are specifying the start value as 2 instead of 1. In this case, MATLAB starts at 2, increments to 4, increments to 6, and then has reached the stop value so does not go further. Notice that in this case, the the stop value of 6 is included in the array.&lt;/p&gt;
&lt;p&gt;With NumPy, you can use &lt;code&gt;arange()&lt;/code&gt; to create an array with specific start, stop, and step values. However, &lt;code&gt;arange()&lt;/code&gt; has one big difference from MATLAB, which is that the stop value is &lt;em&gt;not&lt;/em&gt; included in the resulting array. The reason for this is so that the size of the array is equal to &lt;code&gt;stop - start&lt;/code&gt; for the default case of a step size of 1. Notice in MATLAB that the size of the array of the integers from 1 to 6 is 6, but 6 - 1 = 5.&lt;/p&gt;
&lt;p&gt;There are three ways to use &lt;code&gt;arange()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you only pass one argument to &lt;code&gt;arange()&lt;/code&gt;, it will be interpreted as the stop value. The start value defaults to 0 and the step defaults to 1. If you pass two arguments to &lt;code&gt;arange()&lt;/code&gt;, they are interpreted as the start and stop values. Finally, you can pass all three of start, stop, and step to &lt;code&gt;arange()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Notice that the order of the arguments is different from MATLAB, going &lt;code&gt;start&lt;/code&gt;, &lt;code&gt;stop&lt;/code&gt;, &lt;code&gt;step&lt;/code&gt; in Python. If you&amp;rsquo;re having trouble remembering the order that these arguments go, remember that you can &lt;a href=&quot;#functions-accept-positional-and-keyword-arguments-in-python&quot;&gt;use keyword arguments in Python&lt;/a&gt; to be explicit about what each argument means.&lt;/p&gt;
&lt;p&gt;You can try out &lt;code&gt;arange()&lt;/code&gt; with the following examples:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[3]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;array([1, 2, 3, 4, 5, 6])&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[4]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;(6,)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, you are creating an array that contains the values from 1 to 6. As in MATLAB, if the step is omitted, it defaults to 1. Notice that you had to pass the stop value 7 so that the array stopped at 6. However, the size of the resulting array is 7 - 1 = 6 elements long. Next, you should see how to change the step size:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [5]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [6]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[6]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;array([1, 3, 5])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating an array that contains the values from 1 to 6, incrementing by two between each element. The step is two, so NumPy starts with 1, increments to 3, and then to 5. The next step would equal the stop value, but NumPy does not include the stop value in the array. Notice that the formula to compute the size of the array is a little bit different, since the step size is not 1.&lt;/p&gt;
&lt;p&gt;With step sizes other than 1, the size of the array can be computed by &lt;code&gt;(stop - start)/step&lt;/code&gt; if this results in an integer value. In this case, the size of the array is (7 - 1)/2 = 3 elements, as expected. If &lt;code&gt;(stop - start)/step&lt;/code&gt; results in a floating point number, the size of the array is equal to the next largest integer as demonstrated in the next example:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [7]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [8]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_3&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[8]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;array([2, 4, 6])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, you are creating an array that contains the values from 2 to 6, incrementing by two between each element. The step is two, so NumPy starts with 2, increments to 4, and then to 6. The next step would exceed the stop value, so NumPy stops at 6. Notice that the size of the array is (7 - 2)/2 = 2.5, so the next highest integer is 3 elements, as expected.&lt;/p&gt;
&lt;p&gt;Finally, you should usually use integer arguments to &lt;code&gt;arange()&lt;/code&gt; in NumPy and the colon operator in MATLAB. If you use floating point values (numbers with decimals), especially for the step, the elements may not come out exactly as you expect. If you want to use floating point numbers, &lt;code&gt;linspace()&lt;/code&gt; is a better choice in general.&lt;/p&gt;
&lt;h3 id=&quot;the-colon-operator-is-very-powerful-in-numpy&quot;&gt;The Colon Operator Is Very Powerful in NumPy&lt;/h3&gt;
&lt;p&gt;In MATLAB, the colon operator is used to perform a number of useful tasks. As you saw, it can be used to create arrays, and it can also be used to index or slice arrays. When indexing arrays, MATLAB supports the &lt;code&gt;end&lt;/code&gt; keyword to extend the specified range to the end of that dimension, as you &lt;a href=&quot;#the-last-element-of-a-sequence-has-index-1-in-python&quot;&gt;saw earlier&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;     3     5&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are indexing &lt;code&gt;arr_1&lt;/code&gt; starting at the second index and going to the end of the array. You can also specify a specific index as the stop value:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;     2     3     4&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating an array &lt;code&gt;arr_2&lt;/code&gt; with the numbers 1 through 6, inclusive. Then, you are specifying the second element as the start value and the fourth element as the stop value in the slice. MATLAB supports the two-colon increment syntax when indexing as well:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;     2     4     6&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are indexing the array, starting at the second element, skipping every other element, until the end of the array. You can also use &lt;code&gt;end&lt;/code&gt; as the starting point of the slice with a negative step:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;     6     5     4&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are indexing &lt;code&gt;arr_2&lt;/code&gt; starting from the last value, decrementing by 1, and ending at the 4th element. Finally, you can slice all of the element in a dimension by using just a bare colon:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(:)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;     1     2     3     4     5     6&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are selecting all of the first dimension of the array using just the colon.&lt;/p&gt;
&lt;p&gt;NumPy and Python in general also use the colon for the slice syntax, but the order of the values is slightly different. In Python, the order is &lt;code&gt;start : stop : step&lt;/code&gt;, whereas in MATLAB, it is &lt;code&gt;start : step : stop&lt;/code&gt;, as you saw earlier. In addition, in NumPy you can omit start or stop and they will have default a value of 0 (or the first element) for start and the last element for stop. In MATLAB, you must specify start &lt;em&gt;and&lt;/em&gt; stop if you want to specify either of them. Thus, Python does not have the &lt;code&gt;end&lt;/code&gt; keyword, since you can omit &lt;code&gt;stop&lt;/code&gt; to achieve the same behavior.&lt;/p&gt;
&lt;p&gt;Try out the following examples of the slice syntax in NumPy:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[3]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;array([3, 5])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating an array with the integers from 1 to 6, inclusive, skipping every other number. Then, you are slicing the array taking the second element (index 1) until the end of the array. Notice that the stop value was omitted, so it defaulted to the last element in the array.&lt;/p&gt;
&lt;p&gt;You can also specify a specific element as the stop value. You saw in using &lt;code&gt;arange()&lt;/code&gt; that the array did not include the stop value. The same is true of the slice syntax in Python, the slice will include everything up to, but not including, the stop index:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [5]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[5]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;array([2, 3, 4])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating an array with the integers from 1 to 6, inclusive. Then, you are slicing the array starting at the second element (index 1, value 2) until the fourth element (index 3, value 4). However, you specified the stop index as 4 (the fifth element in the array, value 5). The reason Python includes up to the (stop - 1) index is the same reason &lt;code&gt;arange()&lt;/code&gt; does not include the stop value, so that the length of the resulting array is equal to &lt;code&gt;stop - start&lt;/code&gt;. Next, try changing the step of the slice:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [6]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[6]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;array([2, 4, 6])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are slicing the array starting at the second element (index 1), going until the end of the array, and taking every second element. This results in an array with the values 2, 4, and 6. Notice that the stop value was omitted in the slice syntax, so it defaulted to the last element in the array.&lt;/p&gt;
&lt;p&gt;You can also use a negative step in the slicing syntax for Python:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [7]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[7]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;array([6, 5, 4])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are not specifying the start index of the slice, you are specifying the stop value should be index 2, and the step should be -1. Since the start index is not specified and the step is negative, the start value is assumed to be the last element in the array (or the first element in the reversed array). For the stop value, index 2 has the value of 3 and one index before that (in the reversed array) is index 3 with the value of 4.&lt;/p&gt;
&lt;p&gt;Finally, just like in MATLAB, a bare colon means to select all of the elements from that dimension:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [8]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[8]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;array([1, 2, 3, 4, 5, 6])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;array-slices-are-views-of-arrays-in-numpy&quot;&gt;Array Slices Are Views of Arrays in NumPy&lt;/h3&gt;
&lt;p&gt;In MATLAB, when you access a slice of an array and assign it to a variable, MATLAB will make a copy of that portion of the array into your new variable. This means that when you assign values to the slice, the original array is not affected. Try out this example to help explain the differences of MATLAB vs Python:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;arr_2 =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;     5     6&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;     8     9&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;arr_2 =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;    10     6&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;     8     9&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;arr_1 =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;     1     2     3&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;     4     5     6&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;     7     8     9&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you have created a 3x3 array &lt;code&gt;arr_1&lt;/code&gt; storing the values from 1 through 9. Then, you create a 2x2 slice of the original array storing from the second value to the end in both dimensions, &lt;code&gt;arr_2&lt;/code&gt;. On the third input line, you assign the value 10 to the upper left element in &lt;code&gt;arr_2&lt;/code&gt;. Finally, you print &lt;code&gt;arr_1&lt;/code&gt; again to verify that none of the values in &lt;code&gt;arr_1&lt;/code&gt; have changed.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Technical detail&lt;/strong&gt;: MATLAB employs a &lt;strong&gt;copy-on-write&lt;/strong&gt; memory management system, where an array may only be copied to a new memory location when it is modified. You can read more about MATLAB memory management in &lt;a href=&quot;https://blogs.mathworks.com/loren/2006/05/10/memory-management-for-functions-and-variables/&quot;&gt;Memory Management for Functions and Variables&lt;/a&gt; on the Mathworks blog and in &lt;a href=&quot;https://undocumentedmatlab.com/blog/internal-matlab-memory-optimizations&quot;&gt;Internal Matlab memory optimizations&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;In NumPy, slices of arrays are views to the original array. This behavior saves memory and time, since the values in the array don&amp;rsquo;t have to be copied to a new location. However, it means that changes that you make to a slice from an array will change the original array. You should try the following code to see how this works:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]])&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[4]:&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;array([[5, 6],&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       [8, 9]])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating a 3x3 array &lt;code&gt;arr_1&lt;/code&gt; storing the values from 1 through 9. Then, you create a 2x2 slice of the original array storing from the second value to the end in both dimensions, &lt;code&gt;arr_2&lt;/code&gt;. Notice that the Python indexing is 0-based, so the second element has the index 1. Finally, you are printing &lt;code&gt;arr_2&lt;/code&gt; to verify that it is a 2x2 array.&lt;/p&gt;
&lt;p&gt;Now you should see what happens when you change a value in &lt;code&gt;arr_2&lt;/code&gt;. Like in the MATLAB example, you should change the upper left element of &lt;code&gt;arr_2&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [5]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [6]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[6]:&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;array([[10,  6],&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       [ 8,  9]])&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [7]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[7]:&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;array([[ 1,  2,  3],&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       [ 4, 10,  6],&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       [ 7,  8,  9]])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you first assign the upper left element in &lt;code&gt;arr_2&lt;/code&gt;, at index (0, 0) to have a value of 10. Then you print &lt;code&gt;arr_2&lt;/code&gt; to verify that the appropriate value has changed. Finally, you print &lt;code&gt;arr_1&lt;/code&gt; and see that the value in the middle of the array has changed from 5 to 10!&lt;/p&gt;
&lt;p&gt;This is what is meant by &lt;code&gt;arr_2&lt;/code&gt; being a &lt;strong&gt;view&lt;/strong&gt; of &lt;code&gt;arr_1&lt;/code&gt;. Since it is a view, &lt;code&gt;arr_2&lt;/code&gt; points to the same memory location as &lt;code&gt;arr_1&lt;/code&gt;, so updating &lt;code&gt;arr_2&lt;/code&gt; also updates &lt;code&gt;arr_1&lt;/code&gt; because the value stored in the memory location accessed by both &lt;code&gt;arr_2&lt;/code&gt; and &lt;code&gt;arr_1&lt;/code&gt; has been updated. This also goes the other direction, where changing values in &lt;code&gt;arr_1&lt;/code&gt; will update the value in &lt;code&gt;arr_2&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [8]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [9]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[9]:&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;array([[ 1,  2,  3],&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       [ 4, 10,  6],&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       [ 7,  8, 42]])&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [10]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[10]:&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;array([[10,  6],&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       [ 8, 42]])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are assigning the bottom right element of &lt;code&gt;arr_1&lt;/code&gt; to have the value 42. Remember that in Python, an index of &lt;code&gt;-1&lt;/code&gt; means the last value on that dimension. Then you are printing &lt;code&gt;arr_1&lt;/code&gt; to verify that the lower right value has changed from 9 to 42. Finally, you are printing &lt;code&gt;arr_2&lt;/code&gt;, and you see for &lt;code&gt;arr_2&lt;/code&gt; as well, the bottom right value has changed from 9 to 42.&lt;/p&gt;
&lt;p&gt;If you want to generate a copy of an array, you can use &lt;code&gt;np.copy()&lt;/code&gt;. Copying an array creates a new place in memory for the copy to be stored, so changes to the copied array do not affect the original:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [11]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [12]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;37&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [13]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_3&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[13]:&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;array([[10,  6],&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       [37, 42]])&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [14]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_2&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[14]:&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;array([[10,  6],&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       [ 8, 42]])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating &lt;code&gt;arr_3&lt;/code&gt; as a copy of &lt;code&gt;arr_2&lt;/code&gt;. Then, you are changing the element in the second row, first column to have the value of 37. Then, you are printing &lt;code&gt;arr_3&lt;/code&gt; to verify that the specified change has been made. Finally, you are printing &lt;code&gt;arr_2&lt;/code&gt; to verify that no changes have occurred in &lt;code&gt;arr_2&lt;/code&gt;, as expected.&lt;/p&gt;
&lt;h2 id=&quot;tips-and-tricks-to-make-your-code-pythonic&quot;&gt;Tips and Tricks to Make Your Code Pythonic&lt;/h2&gt;
&lt;p&gt;Like any other programming language, Python code written by experienced Python developers often has a particular look and feel to it. This is because they are able to take advantage of specific idioms in Python to work with Python rather than against Python. Developers coming from other languages often miss out on what makes code Pythonic in their first projects.&lt;/p&gt;
&lt;p&gt;In this section, you&amp;rsquo;ll learn some tips and tricks to make your code Pythonic and level up your Python skills. There are many more tips and tricks than you can learn here, so feel free to check out &lt;a href=&quot;https://realpython.com/learning-paths/writing-pythonic-code/&quot;&gt;Write More Pythonic Code&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;you-should-not-use-semicolons-to-end-lines-in-python&quot;&gt;You Should Not Use Semicolons to End Lines in Python&lt;/h3&gt;
&lt;p&gt;In MATLAB, ending a line of code with a semicolon &lt;code&gt;;&lt;/code&gt; suppresses the output from that line. For instance, assigning a variable will print the value of the variable after the assignment if the semicolon is omitted.&lt;/p&gt;
&lt;p&gt;In Python, you should not end lines of code with semicolons. It is unnecessary, since Python does not change its behavior whether the line is ended with a semicolon or not. So you can save yourself a key stroke and not bother including the semicolon in your scripts and libraries.&lt;/p&gt;
&lt;p&gt;There is one case in Python where the semicolon is useful. When you want to execute several statements, but you cannot include a newline character in the input, you can separate the statements with semicolons. This is mostly useful to execute very short scripts from the command prompt or terminal. For instance, to find the particular Python executable that is running, you can type the following:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;import sys; print(sys.executable)&amp;quot;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;/home/eleanor/anaconda3/bin/python&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are executing the Python interpreter in the &lt;code&gt;python&lt;/code&gt; executable and passing the &lt;code&gt;-c&lt;/code&gt; switch. This switch takes the next argument and executes it within the interpreter. Since the shell environment would execute if you pressed &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-enter&quot;&gt;Enter&lt;/kbd&gt;&lt;/span&gt; to insert a new line, you can type the whole script on one line.&lt;/p&gt;
&lt;p&gt;In this case, you have two logical statements that need to be separated by the semicolon. First you are importing the built-in &lt;code&gt;sys&lt;/code&gt; library and then you are printing the value of &lt;code&gt;sys.executable&lt;/code&gt;. In this example, the Python interpreter that the shell is running comes from the &lt;code&gt;/home/eleanor/anaconda3/bin/python&lt;/code&gt; file.&lt;/p&gt;
&lt;h3 id=&quot;you-should-not-import-from-a-module-in-python&quot;&gt;You Should Not Import &lt;code&gt;*&lt;/code&gt; From a Module in Python&lt;/h3&gt;
&lt;p&gt;In a &lt;a href=&quot;#namespaces-are-one-honking-great-idea-in-python&quot;&gt;previous section&lt;/a&gt;, you read about how namespaces are one honking great idea in Python. In MATLAB, all functions are part of the global namespace by default, so every function and class name has to be unique. Python solves this problem by using namespaces and requiring you to specify which module a function should come from.&lt;/p&gt;
&lt;p&gt;You will find tutorials around the Web that suggest you write the following:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;a_module&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are using the &lt;code&gt;*&lt;/code&gt; to indicate that Python should import everything that is contained in &lt;code&gt;a_module&lt;/code&gt; and put it in the current scope without a prefix. This is slightly more convenient, because you no longer have to prefix functions and classes from &lt;code&gt;a_module&lt;/code&gt; with anything, you can just use them directly. However, it is not a good practice because you don&amp;rsquo;t know what names are defined in &lt;code&gt;a_module&lt;/code&gt; and whether or not they will override any existing names in your current scope.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Technical Note:&lt;/strong&gt; When you &lt;code&gt;from a_module import *&lt;/code&gt;, Python imports all of the names listed in a special variable called &lt;code&gt;__all__&lt;/code&gt; in &lt;code&gt;a_module&lt;/code&gt;. However, if that variable is not defined, Python will import all of the variables, functions, and classes defined in &lt;code&gt;a_module&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;h3 id=&quot;you-should-take-advantage-of-the-different-data-types-in-python&quot;&gt;You Should Take Advantage of the Different Data Types in Python&lt;/h3&gt;
&lt;p&gt;MATLAB, with its heritage as a linear algebra and array focused language, treats most data types as arrays of some sort. This often makes it a little bit tricky to work with more advanced data types like &lt;code&gt;structs&lt;/code&gt;, &lt;code&gt;containers.Map&lt;/code&gt;, cell arrays, and more.&lt;/p&gt;
&lt;p&gt;Python has several built-in data types that are very flexible and can be used to accomplish a number of useful tasks. The major ones that you&amp;rsquo;ll learn about in this section are &lt;a href=&quot;https://realpython.com/python-lists-tuples/&quot;&gt;lists&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/python-dicts/&quot;&gt;dictionaries&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id=&quot;lists&quot;&gt;Lists&lt;/h4&gt;
&lt;p&gt;Python lists are mutable sequences of values. Lists can contain heterogeneous data, which means that each element of the list can be of a different type. Because lists are mutable, you can change the value of any element in the list, or add or remove values from the list, without creating a new list object.&lt;/p&gt;
&lt;p&gt;Since lists are sequences, you can create loops that iterate over them. In Python, you do not need to access each element of a list with an index in a &lt;a href=&quot;https://realpython.com/courses/python-for-loop/&quot;&gt;&lt;code&gt;for&lt;/code&gt; loop&lt;/a&gt;, as you would do in MATLAB:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arr_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;    disp(arr_1(i))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;5&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating an array &lt;code&gt;arr_1&lt;/code&gt; with the integers from 1 to 6, taking every other number. Then you are creating a &lt;code&gt;for&lt;/code&gt; loop where the loop variable goes from 1 to the length of &lt;code&gt;arr_1&lt;/code&gt;. Finally, you are displaying the value of the element of &lt;code&gt;arr_1&lt;/code&gt; at the loop variable on each step by using the loop variable &lt;code&gt;i&lt;/code&gt; to index &lt;code&gt;arr_1&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In Python, you should not use an index for the list when you loop over it. Instead, you should loop directly over the items in a list:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lst_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;b&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lst_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...:&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;3.0&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, on input line 1 you are first creating a Python list with three elements:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The integer &lt;code&gt;1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The string &lt;code&gt;&quot;b&quot;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The float &lt;code&gt;3.0&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This list is assigned to &lt;code&gt;lst_1&lt;/code&gt;. Then you are using a &lt;code&gt;for&lt;/code&gt; loop to access each item in the list in turn. On each iteration, the next value in the list is put into the variable &lt;code&gt;item&lt;/code&gt; that you specified on the &lt;code&gt;for&lt;/code&gt; line. Then, you are printing the value of &lt;code&gt;item&lt;/code&gt; on each iteration.&lt;/p&gt;
&lt;p&gt;Notice in the previous example that you could loop over the value of each element in the list without using an index. Nonetheless, sometimes you want to access the index of each item in the list as you&amp;rsquo;re looping over it. For those cases, Python provides &lt;code&gt;enumerate()&lt;/code&gt; that returns the index and the value of the item:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;enumerate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lst_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;The index is &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{index}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; and the item is &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{item}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...:&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;The index is 0 and the item is 1&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;The index is 1 and the item is b&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;The index is 2 and the item is 3.0&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are looping over &lt;code&gt;lst_1&lt;/code&gt; again, but this time, you are using &lt;code&gt;enumerate()&lt;/code&gt; to get both the index and the item. Then you are printing the value of the index and item on each loop iteration. As you can see from the result, the index values start at 0 as expected, but you do not need to use the index to access the item from the list.&lt;/p&gt;
&lt;p&gt;In summary, you should not write Python code like this:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lst_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lst_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating a range of integers from 0 to the length of &lt;code&gt;lst_1&lt;/code&gt; and then you are accessing each element in the list by its index. This can lead to &lt;a href=&quot;https://en.wikipedia.org/wiki/Off-by-one_error&quot;&gt;off-by-one&lt;/a&gt; and &lt;a href=&quot;https://en.wikipedia.org/wiki/Off-by-one_error#Fencepost_error&quot;&gt;fencepost&lt;/a&gt; errors. Instead, you should write code that loops over the list directly:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;item&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lst_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can read a lot more about lists in &lt;a href=&quot;https://realpython.com/python-lists-tuples&quot;&gt;Lists and Tuples in Python&lt;/a&gt; and about &lt;code&gt;for&lt;/code&gt; loops and iteration in &lt;a href=&quot;https://realpython.com/python-for-loop&quot;&gt;Python &amp;ldquo;for&amp;rdquo; Loops (Definite Iteration)&lt;/a&gt;. There is also a more advanced concept called &lt;strong&gt;list comprehensions&lt;/strong&gt; that you can learn about in &lt;a href=&quot;https://realpython.com/courses/using-list-comprehensions-effectively&quot;&gt;Using List Comprehensions Effectively&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id=&quot;dictionaries&quot;&gt;Dictionaries&lt;/h4&gt;
&lt;p&gt;In MATLAB, you can create a map data type with &lt;code&gt;containers.Map()&lt;/code&gt;. This kind of data structure is useful when you have two pieces of data that are always related to each other and you want to connect them together. For instance, you can map cities to their population with a &lt;code&gt;containers.Map()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;containers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;Cleveland&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;Pittsburgh&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;Hartford&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;                           [383793,301048,122587]);&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;Cleveland&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;ans =&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;     383793&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are creating a &lt;code&gt;containers.Map()&lt;/code&gt; on the first line. The first argument is a cell array of character arrays with the city names. These are called the &lt;strong&gt;keys&lt;/strong&gt; of the map. The second argument is an array of populations. These are called the &lt;strong&gt;values&lt;/strong&gt; of the map. Then, you are accessing the value of the population in Cleveland by indexing the map with a character array.&lt;/p&gt;
&lt;p&gt;You can assign new values into the map by assigning to an undefined key value:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;Providence&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;180393&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you try to access a key that does not exist, you will receive an error message:&lt;/p&gt;
&lt;div class=&quot;highlight matlab repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;New York City&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Error using containers.Map/subsref&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;The specified key is not present in this container.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Python has an equivalent data structure called a &lt;strong&gt;dictionary&lt;/strong&gt;. To create a Python &lt;a href=&quot;https://realpython.com/python-dicts/&quot;&gt;dictionary&lt;/a&gt;, you can use curly braces and specify the keys and values with each other:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Cleveland&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;383_793&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Pittsburgh&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;301_048&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Hartford&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;122_587&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Cleveland&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[2]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;383793&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, on input line 1 you are creating the dictionary of cities using curly braces. Notice that the key and value are specified together, separated by a colon. The values are specified with &lt;code&gt;_&lt;/code&gt; in the numbers, a feature available since Python 3.6. This does not change the value of the number, it only makes it easier to read very large numbers. Then, you are accessing the value at the &lt;code&gt;&quot;Cleveland&quot;&lt;/code&gt; key using square brackets, similar to the indexing syntax for lists and arrays.&lt;/p&gt;
&lt;p&gt;You can add new keys to the dictionary by assigning to them:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Providence&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;180_393&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you assigned a new key &lt;code&gt;&quot;Providence&quot;&lt;/code&gt; to the dictionary with a value of 180,393. If you try to access a key that is not in the dictionary, you will get a &lt;code&gt;KeyError&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;New York City&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;  File &amp;quot;&amp;lt;ipython-input-4-6ebe5b35f3ea&amp;gt;&amp;quot;, line 1, in &amp;lt;module&amp;gt;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;    cities[&amp;quot;New York City&amp;quot;]&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;KeyError: &amp;#39;New York City&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, you are trying to access the dictionary using the &lt;code&gt;&quot;New York City&quot;&lt;/code&gt; key. However, this key does not exist in the dictionary so Python raises a &lt;code&gt;KeyError&lt;/code&gt; letting you know that &lt;code&gt;&quot;New York City&quot;&lt;/code&gt; is not an option.&lt;/p&gt;
&lt;p&gt;You can read a lot more about Python dictionaries in &lt;a href=&quot;https://realpython.com/python-dicts&quot;&gt;Dictionaries in Python&lt;/a&gt; and &lt;code&gt;KeyError&lt;/code&gt; exceptions in &lt;a href=&quot;https://realpython.com/python-keyerror&quot;&gt;Python KeyError Exceptions and How to Handle Them&lt;/a&gt;. You can also iterate through dictionaries and use dictionary comprehensions, similar to list comprehensions. You can read about these topics in &lt;a href=&quot;https://realpython.com/iterate-through-dictionary-python&quot;&gt;How to Iterate Through a Dictionary in Python&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;exceptions-help-you-control-program-flow-in-python&quot;&gt;Exceptions Help You Control Program Flow in Python&lt;/h3&gt;
&lt;p&gt;MATLAB and Python both use errors and exceptions to let you know when something has gone wrong in the code. In this section, you will learn about common exceptions in Python and how you can handle them appropriately.&lt;/p&gt;
&lt;p&gt;If you want an introduction to Python exceptions overall, you can read &lt;a href=&quot;https://realpython.com/python-exceptions/&quot;&gt;Python Exceptions: An Introduction&lt;/a&gt;. When a Python exception is raised, it produces a &lt;strong&gt;traceback&lt;/strong&gt;. You can read about how to interpret the traceback in &lt;a href=&quot;https://realpython.com/python-traceback/&quot;&gt;Understanding Python Tracebacks&lt;/a&gt;. Understanding tracebacks is very helpful to interpret and correct Python exceptions in general. There are a few specific cases that usually have the same resolution. You&amp;rsquo;ll see those described in the rest of this section.&lt;/p&gt;
&lt;h4 id=&quot;nameerror&quot;&gt;NameError&lt;/h4&gt;
&lt;p&gt;Python &lt;code&gt;NameError&lt;/code&gt; exceptions are usually the result of a variable being undefined. When you see a &lt;code&gt;NameError&lt;/code&gt;, check your code for typos and misspelled variable names. You can use the debugging features and the variable explorer in Spyder to find out which variables are defined.&lt;/p&gt;
&lt;h4 id=&quot;syntaxerror&quot;&gt;SyntaxError&lt;/h4&gt;
&lt;p&gt;Python &lt;code&gt;SyntaxError&lt;/code&gt; exceptions mean that you have input some improper syntax. This is usually caused by mismatched brackets, when you have only the opening bracket or closing bracket but not the matching one. These exceptions usually point to the line &lt;em&gt;after&lt;/em&gt; the place where the problem is located.&lt;/p&gt;
&lt;p&gt;Another common &lt;code&gt;SyntaxError&lt;/code&gt; is using only one equals sign in an &lt;code&gt;if&lt;/code&gt; statement. In this case, you either meant not-equals (&lt;code&gt;!=&lt;/code&gt;) or equals (&lt;code&gt;==&lt;/code&gt;), so you can correct the line.&lt;/p&gt;
&lt;h4 id=&quot;keyerror&quot;&gt;KeyError&lt;/h4&gt;
&lt;p&gt;Python &lt;code&gt;KeyError&lt;/code&gt; exceptions occur when you try to access a key in a dictionary that does not exist. You can use &lt;a href=&quot;https://realpython.com/python-dicts/#dgetltkeygt-ltdefaultgt&quot;&gt;&lt;code&gt;.get()&lt;/code&gt;&lt;/a&gt; to retrieve a key from a dictionary if it exists, or return a default value if the key does not exist. You can read more about &lt;code&gt;KeyError&lt;/code&gt; exceptions in &lt;a href=&quot;https://realpython.com/python-keyerror/&quot;&gt;Python KeyError Exceptions and How to Handle Them&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id=&quot;indexerror&quot;&gt;IndexError&lt;/h4&gt;
&lt;p&gt;Python &lt;code&gt;IndexError&lt;/code&gt; exceptions occur when you are trying to access the index of an array or list that does not exist. This usually means the array or list you are trying to access has fewer elements than the index you are trying to access. You can use the debugging features and the variable explorer in Spyder to see the size of lists and arrays and make sure you&amp;rsquo;re only accessing indices that exist.&lt;/p&gt;
&lt;h4 id=&quot;importerrormodulenotfounderror&quot;&gt;ImportError/ModuleNotFoundError&lt;/h4&gt;
&lt;p&gt;Python &lt;code&gt;ImportError&lt;/code&gt; and &lt;code&gt;ModuleNotFoundError&lt;/code&gt; exceptions occur when you try to import a module that Python cannot find. This might be because it is installed in a different &lt;code&gt;conda&lt;/code&gt; environment or &lt;code&gt;virtualenv&lt;/code&gt;, or it might be because you forgot to install the package.&lt;/p&gt;
&lt;p&gt;The solution to this error is usually to &lt;code&gt;conda install&lt;/code&gt; or &lt;code&gt;pip install&lt;/code&gt; the package and make sure the correct environment is activated. If you&amp;rsquo;re not using the &lt;code&gt;base&lt;/code&gt; environment in &lt;code&gt;conda&lt;/code&gt;, you also need to make sure to install Spyder or Jupyter into your environment.&lt;/p&gt;
&lt;h4 id=&quot;typeerrorvalueerror&quot;&gt;TypeError/ValueError&lt;/h4&gt;
&lt;p&gt;Python &lt;code&gt;TypeError&lt;/code&gt; exceptions happen when an argument is of the wrong type. This happens most commonly when you pass an argument of the wrong type into a function. For instance, a function that works with numbers would raise a &lt;code&gt;TypeError&lt;/code&gt; if a string were passed in.&lt;/p&gt;
&lt;p&gt;A related exception is the &lt;code&gt;ValueError&lt;/code&gt;. This exception happens when an argument is of the correct type, but has an incorrect value. For instance, a function that works only with positive numbers would raise a &lt;code&gt;ValueError&lt;/code&gt; if a negative number were passed in.&lt;/p&gt;
&lt;h4 id=&quot;attributeerror&quot;&gt;AttributeError&lt;/h4&gt;
&lt;p&gt;Python &lt;code&gt;AttributeError&lt;/code&gt; exceptions happen when you try to access an attribute of an object when the object does not have that attribute. You will often see this error associated with the message &lt;code&gt;NoneType object has no attribute&lt;/code&gt;. This message most likely means that a function has returned &lt;code&gt;None&lt;/code&gt; instead of the object you were expecting and you are trying to access an attribute that would be present on the real object, but is not defined for &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;how-to-handle-exceptions-in-python&quot;&gt;How to Handle Exceptions in Python&lt;/h3&gt;
&lt;p&gt;MATLAB allows you to &lt;code&gt;try&lt;/code&gt; a statement of code and &lt;code&gt;catch&lt;/code&gt; any errors that are thrown by the code. Once you have caught an error, you can do further processing of the error and assign variables based on the type of error. The &lt;a href=&quot;https://www.mathworks.com/help/matlab/ref/try.html&quot;&gt;MATLAB documentation&lt;/a&gt; has several good examples of how this would look in MATLAB.&lt;/p&gt;
&lt;p&gt;In Python, one big difference from MATLAB is that you can choose to catch only certain types of exceptions and handle them. This allows all other exceptions to continue to be displayed to the user. If you want to learn more about how to do this in Python, you can read &lt;a href=&quot;https://realpython.com/python-exceptions/#the-try-and-except-block-handling-exceptions&quot;&gt;The &lt;code&gt;try&lt;/code&gt; and &lt;code&gt;except&lt;/code&gt; Block: Handling Exceptions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To see how this works, you can try the following example:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;In [1]: &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;math&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;In [2]: &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;   &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;You passed the argument: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{number!r}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;       &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;ValueError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;       &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;You passed a number that cannot be operated on&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;TypeError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...: &lt;/span&gt;       &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;You passed an argument that was not a number&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;   ...:&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [3]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;You passed the argument: 4.0&lt;/span&gt;
&lt;span class=&quot;gh&quot;&gt;Out[3]: &lt;/span&gt;&lt;span class=&quot;go&quot;&gt;2.0&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;You passed the argument: -1.0&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;You passed a number that cannot be operated on&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;In [4]: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;4.0&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;You passed the argument: &amp;#39;4.0&amp;#39;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;You passed an argument that was not a number&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this code, on input line 1 you are importing the built-in &lt;code&gt;math&lt;/code&gt; library. Then, starting on input line 2, you are defining a function called &lt;code&gt;my_sqrt()&lt;/code&gt; that will take one argument, called &lt;code&gt;number&lt;/code&gt;. Inside the function definition, you first print the argument that the user passed.&lt;/p&gt;
&lt;p&gt;Next, you enter the &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;except&lt;/code&gt; block. First, you try to take the square root of the input argument and return the result. If taking the square root of the argument results in an error, Python will catch that error and check which type of error was raised.&lt;/p&gt;
&lt;p&gt;You have defined code that handles two specific exceptions: &lt;code&gt;ValueError&lt;/code&gt; and &lt;code&gt;TypeError&lt;/code&gt;. If &lt;code&gt;math.sqrt()&lt;/code&gt; raises a &lt;code&gt;ValueError&lt;/code&gt;, your code will print a message that the number cannot be operated on. If &lt;code&gt;math.sqrt()&lt;/code&gt; raises a &lt;code&gt;TypeError&lt;/code&gt;, your code will print a message that the argument was not a number. If any other type of exception is raised by &lt;code&gt;math.sqrt()&lt;/code&gt;, that error will be passed through without any processing, since there is no handler for any other error types.&lt;/p&gt;
&lt;p&gt;More specifically, Python checks for any error that is raised by the code in the &lt;code&gt;try&lt;/code&gt; block. In your case, you only defined one line of code in the &lt;code&gt;try&lt;/code&gt; block, but this is not required, and you can have as many lines as you want there. However, it is usually a good practice to minimize the number of lines of code in the &lt;code&gt;try&lt;/code&gt; block so you can be very specific about which code is raising any errors.&lt;/p&gt;
&lt;p&gt;On input line 3, you are testing out &lt;code&gt;my_sqrt()&lt;/code&gt;. First, you pass the value 4.0 to the function. The function prints the argument, and &lt;code&gt;math.sqrt()&lt;/code&gt; has no problems taking the square root of 4, resulting in 2.0 on the output line.&lt;/p&gt;
&lt;p&gt;On input line 4, you are passing -1.0 as the argument to &lt;code&gt;my_sqrt()&lt;/code&gt;. As you may recall, taking the square root of negative numbers results in a complex number, which the &lt;code&gt;math.sqrt()&lt;/code&gt; function is not equipped to handle. Taking the square root of a negative number using &lt;code&gt;math.sqrt()&lt;/code&gt; raises a &lt;code&gt;ValueError&lt;/code&gt;. You exception handler catches this &lt;code&gt;ValueError&lt;/code&gt; and prints the message that the number cannot be operated on.&lt;/p&gt;
&lt;p&gt;On input line 5, you are passing &lt;code&gt;&quot;4.0&quot;&lt;/code&gt; as the argument to &lt;code&gt;my_sqrt()&lt;/code&gt;. In this case, &lt;code&gt;math.sqrt()&lt;/code&gt; does not know how to take the square root of a string, even though that string appears to represent a number. You can see that you have passed a string by the quotes in the statement giving the value of the argument: &lt;code&gt;You passed the argument: &#39;4.0&#39;&lt;/code&gt;. Since &lt;code&gt;math.sqrt()&lt;/code&gt; cannot take the square root of a string, it raises a &lt;code&gt;TypeError&lt;/code&gt;, and your function prints the message that the argument was not a number.&lt;/p&gt;
&lt;h3 id=&quot;there-is-an-official-guide-to-writing-good-code-in-python&quot;&gt;There Is an Official Guide to Writing Good Code in Python&lt;/h3&gt;
&lt;p&gt;The Python community has developed a set of recommendations for how to style your Python code. These are codified in a document called PEP 8, which stands for Python Enhancement Proposal #8. PEP 8 can be found in full on the &lt;a href=&quot;https://www.python.org/dev/peps/pep-0008/&quot;&gt;Python website&lt;/a&gt;. You can also learn more about good Python style in &lt;a href=&quot;https://realpython.com/python-pep8/&quot;&gt;How to Write Beautiful Python Code With PEP 8&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/courses/idiomatic-python-101&quot;&gt;Idiomatic Python 101&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Perhaps the most important principle in PEP 8 is the saying that &amp;ldquo;a foolish consistency is the hobgoblin of little minds.&amp;rdquo; This means that you should follow the recommendations in PEP 8 for almost all of your code, but there may be some limited cases where it is a good idea not to follow the PEP 8 recommendations. For instance, if you are working with an existing codebase that has its own style, you should follow that style where it diverges from PEP 8. You can see an excellent discussion of this principle from Raymond Hettinger, one of the core Python developers, in a &lt;a href=&quot;https://realpython.com/must-watch-pycon-talks/#2-beyond-pep-8-best-practices-for-beautiful-intelligible-code&quot;&gt;talk from PyCon 2015&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Aside from reading PEP 8, you can use a few Python packages to automatically make sure that your code fits the style guidelines. &lt;a href=&quot;https://flake8.readthedocs.io&quot;&gt;Flake8&lt;/a&gt; is a code linter that reads your code and makes suggestions for how you can improve it. This is similar to the feature in the MATLAB code editor that makes improvement suggestions. In addition, packages such as &lt;a href=&quot;https://black.readthedocs.io/en/stable/&quot;&gt;Black&lt;/a&gt;, &lt;a href=&quot;https://github.com/google/yapf&quot;&gt;yapf&lt;/a&gt;, and &lt;a href=&quot;https://github.com/hhatto/autopep8&quot;&gt;autopep8&lt;/a&gt; will automatically format your code to be compliant with PEP 8 or your own style rules. Using these packages can help your code feel more Pythonic and help you learn good Python style!&lt;/p&gt;
&lt;h2 id=&quot;python-has-a-fantastic-and-supportive-community&quot;&gt;Python Has a Fantastic and Supportive Community&lt;/h2&gt;
&lt;p&gt;Python is known for having a very supportive, open, and welcoming community. Whether you are a brand new developer or an experienced one, whether you&amp;rsquo;re brand new to Python or have been to a dozen conferences, the community is there to support you and what you want to do.&lt;/p&gt;
&lt;p&gt;The community starts with the Python Package Index (called PyPI or the CheeseShop, a reference to the Monty Python sketch), which houses hundreds of thousands of different Python packages that you can download for free. These packages can be installed using &lt;code&gt;pip&lt;/code&gt;, a package manager that comes bundled with Python. This means that adding the functionality you need to Python can be as simple as &lt;code&gt;pip install package&lt;/code&gt; or if you&amp;rsquo;re using Anaconda, &lt;code&gt;conda install package&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Since Python is used in so many different areas of software development, data science, science, and engineering, there are always people around who want to talk about Python. Most large cities around the world have Python meetup groups. You can go to these groups to learn about Python by hearing people speak about their work or work on some open-source code.&lt;/p&gt;
&lt;p&gt;A few times a year, these groups coalesce into different PyCons which happen on every continent around the globe. PyCon North America is the largest of these, with several thousand attendees every year. You can read all about what it&amp;rsquo;s like to attend in &lt;a href=&quot;https://realpython.com/pycon-guide&quot;&gt;How to Get the Most Out of PyCon&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Python also has a very strong online community. If you have a question about programming in Python, you can ask on &lt;a href=&quot;https://stackoverflow.com/questions/tagged/python&quot;&gt;StackOverflow&lt;/a&gt; and some of the world-leading Python experts will be able to help you out. Make sure to follow the instructions for &lt;a href=&quot;https://stackoverflow.com/help/how-to-ask&quot;&gt;how to ask a question&lt;/a&gt; on StackOverflow. Remember that the more effort you put into your question, the more likely you are to either find the answer yourself (&lt;em&gt;hooray!&lt;/em&gt;) or get a good answer from someone else.&lt;/p&gt;
&lt;p&gt;If you want to follow along with Python&amp;rsquo;s development, you can sign up for one of the &lt;a href=&quot;https://www.python.org/community/lists/&quot;&gt;mailing lists&lt;/a&gt; covering different aspects of Python&amp;rsquo;s community. The general mailing list for asking questions about writing programs in Python is called &lt;a href=&quot;https://mail.python.org/mailman/listinfo/python-list&quot;&gt;comp.lang.python&lt;/a&gt;. If you are interested in the development of Python itself, you can follow the &lt;a href=&quot;https://mail.python.org/mailman3/lists/python-dev.python.org/&quot;&gt;python-dev&lt;/a&gt; mailing list.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re interested in learning much, much more about Python development, you can check out the &lt;em&gt;Real Python&lt;/em&gt; &lt;a href=&quot;https://realpython.com/learning-paths/&quot;&gt;Learning Paths&lt;/a&gt;!&lt;/p&gt;
&lt;h2 id=&quot;areas-where-you-should-still-use-matlab&quot;&gt;Areas Where You Should Still Use MATLAB®&lt;/h2&gt;
&lt;p&gt;In spite of the awesome community and terrific packages, there are still one or two areas where MATLAB works better than Python. The main place where Python can&amp;rsquo;t compete with MATLAB is the Simulink Toolbox. This toolbox offers advanced capabilities for signal processing and modeling in a convenient graphical interface.&lt;/p&gt;
&lt;p&gt;Python does not have an equivalent graphical interface to these kinds of functions. However, to the extent that Simulink is a convenient interface to ordinary differential equation solvers, Python has equivalent solvers as in MATLAB and the underlying functionality of Simulink can certainly be replicated in Python.&lt;/p&gt;
&lt;p&gt;Otherwise, you can do anything in Python that you can do in MATLAB! If you can think of work that you can do with MATLAB, but you&amp;rsquo;re not sure how to do it in Python, let us know in the comments and we&amp;rsquo;ll be able to help with suggestions.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Congratulations, you now have the knowledge you need to switch your MATLAB code to Python! In this article, you learned a little bit about what Python is, how to set up your computer to use Python, and how to convert your code from MATLAB to Python.&lt;/p&gt;
&lt;p&gt;Python is a really huge language and community, with lots to learn and lots of people to learn from. Remember, you weren&amp;rsquo;t a MATLAB expert the first time you opened the MATLAB development environment, and the same is true about the first time you write some Python code. Come back to this article as often as you need to improve your skills and learn more about becoming a Python wizard!&lt;/p&gt;
&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;
&lt;p&gt;There are tons of resources on the Web covering the differences in MATLAB vs Python. Here are a few of the resources that I found helpful when I transitioned from MATLAB to Python:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=YkCegjtoHFQ&quot;&gt;Webinar: Python for MATLAB Users, What You Need to Know (Video)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.enthought.com/white-paper-matlab-to-python&quot;&gt;MATLAB To Python Whitepaper&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://tobydriscoll.net/blog/matlab-vs.-julia-vs.-python/&quot;&gt;Matlab vs. Julia vs. Python&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://phillipmfeldman.org/Python/Advantages_of_Python_Over_Matlab.html&quot;&gt;Eight Advantages of Python Over MATLAB&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.stat.washington.edu/~hoytak/blog/whypython.html&quot;&gt;10 Reasons Python Rocks for Research (And a Few Reasons it Doesn&amp;rsquo;t)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Python args and kwargs: Demystified</title>
      <id>https://realpython.com/python-kwargs-and-args/</id>
      <link href="https://realpython.com/python-kwargs-and-args/"/>
      <updated>2019-09-04T14:00:00+00:00</updated>
      <summary>In this step-by-step tutorial, you&#39;ll learn how to use args and kwargs in Python to add more flexibility to your functions. You&#39;ll also take a closer look at the single and double-asterisk unpacking operators, which you can use to unpack any iterable object in Python.</summary>
      <content type="html">
        &lt;p&gt;Sometimes, when you look at a function definition in Python, you might see that it takes two strange arguments: &lt;strong&gt;&lt;code&gt;*args&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;**kwargs&lt;/code&gt;&lt;/strong&gt;. If you&amp;rsquo;ve ever wondered what these peculiar variables are, or why your IDE defines them in &lt;code&gt;main()&lt;/code&gt;, then this article is for you. You&amp;rsquo;ll learn how to use args and kwargs in Python to add more flexibility to your functions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;By the end of the article, you&amp;rsquo;ll know:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; actually mean&lt;/li&gt;
&lt;li&gt;How to use &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; in function definitions&lt;/li&gt;
&lt;li&gt;How to use a single asterisk (&lt;code&gt;*&lt;/code&gt;) to unpack iterables&lt;/li&gt;
&lt;li&gt;How to use two asterisks (&lt;code&gt;**&lt;/code&gt;) to unpack dictionaries&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This article assumes that you already know how to define Python functions and work with &lt;a href=&quot;https://realpython.com/lessons/mutable-data-structures-lists-dictionaries/&quot;&gt;lists and dictionaries&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-cheat-sheet-shortened&quot; data-focus=&quot;false&quot;&gt;Click here to get a Python Cheat Sheet&lt;/a&gt; and learn the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.&lt;/p&gt;&lt;/div&gt;

&lt;h2 id=&quot;passing-multiple-arguments-to-a-function&quot;&gt;Passing Multiple Arguments to a Function&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;*args&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;**kwargs&lt;/code&gt;&lt;/strong&gt; allow you to pass multiple arguments or keyword arguments to a function. Consider the following example. This is a simple function that takes two arguments and returns their sum:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This function works fine, but it&amp;rsquo;s limited to only two arguments. What if you need to sum a varying number of arguments, where the specific number of arguments passed is only determined at runtime? Wouldn&amp;rsquo;t it be great to create a function that could sum &lt;em&gt;all&lt;/em&gt; the integers passed to it, no matter how many there are?&lt;/p&gt;
&lt;h2 id=&quot;using-the-python-args-variable-in-function-definitions&quot;&gt;Using the Python args Variable in Function Definitions&lt;/h2&gt;
&lt;p&gt;There are a few ways you can pass a varying number of arguments to a function. The first way is often the most intuitive for people that have experience with collections. You simply pass a list or a &lt;a href=&quot;https://realpython.com/python-sets/&quot;&gt;set&lt;/a&gt; of all the arguments to your function. So for &lt;code&gt;my_sum()&lt;/code&gt;, you could pass a list of all the integers you need to add:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# sum_integers_list.py&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_integers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_integers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;list_of_integers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list_of_integers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This implementation works, but whenever you call this function you&amp;rsquo;ll also need to create a list of arguments to pass to it. This can be inconvenient, especially if you don&amp;rsquo;t know up front all the values that should go into the list.&lt;/p&gt;
&lt;p&gt;This is where &lt;code&gt;*args&lt;/code&gt; can be really useful, because it allows you to pass a varying number of positional arguments. Take the following example:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# sum_integers_args.py&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Iterating over the Python args tuple&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, you&amp;rsquo;re no longer passing a list to &lt;code&gt;my_sum()&lt;/code&gt;. Instead, you&amp;rsquo;re passing three different positional arguments. &lt;code&gt;my_sum()&lt;/code&gt; takes all the parameters that are provided in the input and packs them all into a single iterable object named &lt;code&gt;args&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Note that &lt;strong&gt;&lt;code&gt;args&lt;/code&gt; is just a name.&lt;/strong&gt; You&amp;rsquo;re not required to use the name &lt;code&gt;args&lt;/code&gt;. You can choose any name that you prefer, such as &lt;code&gt;integers&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# sum_integers_args_2.py&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;integers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;integers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The function still works, even if you pass the iterable object as &lt;code&gt;integers&lt;/code&gt; instead of &lt;code&gt;args&lt;/code&gt;. All that matters here is that you use the &lt;strong&gt;unpacking operator&lt;/strong&gt; (&lt;code&gt;*&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Bear in mind that the iterable object you&amp;rsquo;ll get using the unpacking operator &lt;code&gt;*&lt;/code&gt; is &lt;a href=&quot;https://realpython.com/python-lists-tuples/&quot;&gt;not a &lt;code&gt;list&lt;/code&gt; but a &lt;code&gt;tuple&lt;/code&gt;&lt;/a&gt;. A &lt;code&gt;tuple&lt;/code&gt; is similar to a &lt;code&gt;list&lt;/code&gt; in that they both support slicing and iteration. However, tuples are very different in at least one aspect: lists are &lt;a href=&quot;https://realpython.com/courses/immutability-python/&quot;&gt;mutable&lt;/a&gt;, while tuples are not. To test this, run the following code. This script tries to change a value of a list:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# change_list.py&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The value located at the very first index of the list should be updated to &lt;code&gt;9&lt;/code&gt;. If you execute this script, you will see that the list indeed gets modified:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python change_list.py
&lt;span class=&quot;go&quot;&gt;[9, 2, 3]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The first value is no longer &lt;code&gt;0&lt;/code&gt;, but the updated value &lt;code&gt;9&lt;/code&gt;. Now, try to do the same with a tuple:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# change_tuple.py&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_tuple&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, you see the same values, except they&amp;rsquo;re held together as a tuple. If you try to execute this script, you will see that the Python interpreter returns an &lt;a href=&quot;https://realpython.com/python-exceptions/&quot;&gt;error&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python change_tuple.py
&lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  File &amp;quot;change_tuple.py&amp;quot;, line 3, in &amp;lt;module&amp;gt;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;    my_tuple[0] = 9&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;TypeError: &amp;#39;tuple&amp;#39; object does not support item assignment&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is because a tuple is an immutable object, and its values cannot be changed after assignment. Keep this in mind when you&amp;rsquo;re working with tuples and &lt;code&gt;*args&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;using-the-python-kwargs-variable-in-function-definitions&quot;&gt;Using the Python kwargs Variable in Function Definitions&lt;/h2&gt;
&lt;p&gt;Okay, now you&amp;rsquo;ve understood what &lt;code&gt;*args&lt;/code&gt; is for, but what about &lt;code&gt;**kwargs&lt;/code&gt;? &lt;code&gt;**kwargs&lt;/code&gt; works just like &lt;code&gt;*args&lt;/code&gt;, but instead of accepting positional arguments it accepts keyword (or &lt;strong&gt;named&lt;/strong&gt;) arguments. Take the following example:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# concatenate.py&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Iterating over the Python kwargs dictionary&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Real&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Python&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Is&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Great&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When you execute the script above, &lt;code&gt;concatenate()&lt;/code&gt; will iterate through the Python kwargs &lt;a href=&quot;https://realpython.com/python-dicts/&quot;&gt;dictionary&lt;/a&gt; and concatenate all the values it finds:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python concatenate.py
&lt;span class=&quot;go&quot;&gt;RealPythonIsGreat!&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Like &lt;code&gt;args&lt;/code&gt;, &lt;code&gt;kwargs&lt;/code&gt; is just a name that can be changed to whatever you want. Again, what is important here is the use of the &lt;strong&gt;unpacking operator&lt;/strong&gt; (&lt;code&gt;**&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;So, the previous example could be written like this:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# concatenate_2.py&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Real&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Python&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Is&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Great&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that in the example above the iterable object is a standard &lt;code&gt;dict&lt;/code&gt;. If you &lt;a href=&quot;https://realpython.com/iterate-through-dictionary-python/&quot;&gt;iterate over the dictionary&lt;/a&gt; and want to return its values, like in the example shown, then you must use &lt;code&gt;.values()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In fact, if you forget to use this method, you will find yourself iterating through the &lt;strong&gt;keys&lt;/strong&gt; of your Python kwargs dictionary instead, like in the following example:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# concatenate_keys.py&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Iterating over the keys of the Python kwargs dictionary&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;concatenate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Real&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Python&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Is&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Great&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, if you try to execute this example, you&amp;rsquo;ll notice the following output:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python concatenate_keys.py
&lt;span class=&quot;go&quot;&gt;abcde&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As you can see, if you don&amp;rsquo;t specify &lt;code&gt;.values()&lt;/code&gt;, your function will iterate over the keys of your Python kwargs dictionary, returning the wrong result.&lt;/p&gt;
&lt;h2 id=&quot;ordering-arguments-in-a-function&quot;&gt;Ordering Arguments in a Function&lt;/h2&gt;
&lt;p&gt;Now that you have learned what &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; are for, you are ready to start writing functions that take a varying number of input arguments. But what if you want to create a function that takes a changeable number of both positional &lt;em&gt;and&lt;/em&gt; named arguments?&lt;/p&gt;
&lt;p&gt;In this case, you have to bear in mind that &lt;strong&gt;order counts&lt;/strong&gt;. Just as non-default arguments have to precede default arguments, so &lt;code&gt;*args&lt;/code&gt; must come before &lt;code&gt;**kwargs&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To recap, the correct order for your parameters is:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Standard arguments&lt;/li&gt;
&lt;li&gt;&lt;code&gt;*args&lt;/code&gt; arguments&lt;/li&gt;
&lt;li&gt;&lt;code&gt;**kwargs&lt;/code&gt; arguments&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For example, this function definition is correct:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# correct_function_definition.py&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;*args&lt;/code&gt; variable is appropriately listed before &lt;code&gt;**kwargs&lt;/code&gt;. But what if you try to modify the order of the arguments? For example, consider the following function:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# wrong_function_definition.py&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kwargs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, &lt;code&gt;**kwargs&lt;/code&gt; comes before &lt;code&gt;*args&lt;/code&gt; in the function definition. If you try to run this example, you&amp;rsquo;ll receive an error from the interpreter:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python wrong_function_definition.py
&lt;span class=&quot;go&quot;&gt;  File &amp;quot;wrong_function_definition.py&amp;quot;, line 2&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;    def my_function(a, b, **kwargs, *args):&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;                                    ^&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;SyntaxError: invalid syntax&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this case, since &lt;code&gt;*args&lt;/code&gt; comes after &lt;code&gt;**kwargs&lt;/code&gt;, the Python interpreter throws a &lt;code&gt;SyntaxError&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;unpacking-with-the-asterisk-operators&quot;&gt;Unpacking With the Asterisk Operators: &lt;code&gt;*&lt;/code&gt; &amp;amp; &lt;code&gt;**&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;You are now able to use &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; to define Python functions that take a varying number of input arguments. Let&amp;rsquo;s go a little deeper to understand something more about the &lt;strong&gt;unpacking operators&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The single and double asterisk unpacking operators were introduced in Python 2. As of the 3.5 release, they have become even more powerful, thanks to &lt;a href=&quot;https://www.python.org/dev/peps/pep-0448/&quot;&gt;PEP 448&lt;/a&gt;. In short, the unpacking operators are operators that unpack the values from iterable objects in Python. The single asterisk operator &lt;code&gt;*&lt;/code&gt; can be used on any iterable that Python provides, while the double asterisk operator &lt;code&gt;**&lt;/code&gt; can only be used on dictionaries.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s start with an example:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# print_list.py&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code defines a list and then prints it to the standard output:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python print_list.py
&lt;span class=&quot;go&quot;&gt;[1, 2, 3]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note how the list is printed, along with the corresponding brackets and commas.&lt;/p&gt;
&lt;p&gt;Now, try to prepend the unpacking operator &lt;code&gt;*&lt;/code&gt; to the name of your list:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# print_unpacked_list.py&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, the &lt;code&gt;*&lt;/code&gt; operator tells &lt;code&gt;print()&lt;/code&gt; to unpack the list first.&lt;/p&gt;
&lt;p&gt;In this case, the output is no longer the list itself, but rather &lt;em&gt;the content&lt;/em&gt; of the list:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python print_unpacked_list.py
&lt;span class=&quot;go&quot;&gt;1 2 3&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Can you see the difference between this execution and the one from &lt;code&gt;print_list.py&lt;/code&gt;? Instead of a list, &lt;code&gt;print()&lt;/code&gt; has taken three separate arguments as the input.&lt;/p&gt;
&lt;p&gt;Another thing you&amp;rsquo;ll notice is that in &lt;code&gt;print_unpacked_list.py&lt;/code&gt;, you used the unpacking operator &lt;code&gt;*&lt;/code&gt; to call a function, instead of in a function definition. In this case, &lt;code&gt;print()&lt;/code&gt; takes all the items of a list as though they were single arguments.&lt;/p&gt;
&lt;p&gt;You can also use this method to call your own functions, but if your function requires a specific number of arguments, then the iterable you unpack must have the same number of arguments.&lt;/p&gt;
&lt;p&gt;To test this behavior, consider this script:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# unpacking_call.py&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, &lt;code&gt;my_sum()&lt;/code&gt; explicitly states that &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, and &lt;code&gt;c&lt;/code&gt; are required arguments.&lt;/p&gt;
&lt;p&gt;If you run this script, you&amp;rsquo;ll get the sum of the three numbers in &lt;code&gt;my_list&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python unpacking_call.py
&lt;span class=&quot;go&quot;&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The 3 elements in &lt;code&gt;my_list&lt;/code&gt; match up perfectly with the required arguments in &lt;code&gt;my_sum()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now look at the following script, where &lt;code&gt;my_list&lt;/code&gt; has 4 arguments instead of 3:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# wrong_unpacking_call.py&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, &lt;code&gt;my_sum()&lt;/code&gt; still expects just three arguments, but the &lt;code&gt;*&lt;/code&gt; operator gets 4 items from the list. If you try to execute this script, you&amp;rsquo;ll see that the Python interpreter is unable to run it:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python wrong_unpacking_call.py
&lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  File &amp;quot;wrong_unpacking_call.py&amp;quot;, line 6, in &amp;lt;module&amp;gt;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;    my_sum(*my_list)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;TypeError: my_sum() takes 3 positional arguments but 4 were given&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When you use the &lt;code&gt;*&lt;/code&gt; operator to unpack a list and pass arguments to a function, it&amp;rsquo;s exactly as though you&amp;rsquo;re passing every single argument alone. This means that you can use multiple unpacking operators to get values from several lists and pass them all to a single function.&lt;/p&gt;
&lt;p&gt;To test this behavior, consider the following example:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# sum_integers_args_3.py&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;list1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;list2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;list3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you run this example, all three lists are unpacked. Each individual item is passed to &lt;code&gt;my_sum()&lt;/code&gt;, resulting in the following output:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python sum_integers_args_3.py
&lt;span class=&quot;go&quot;&gt;45&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There are other convenient uses of the unpacking operator. For example, say you need to split a list into three different parts. The output should show the first value, the last value, and all the values in between. With the unpacking operator, you can do this in just one line of code:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# extract_list_body.py&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_list&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, &lt;code&gt;my_list&lt;/code&gt; contains 6 items. The first variable is assigned to &lt;code&gt;a&lt;/code&gt;, the last to &lt;code&gt;c&lt;/code&gt;, and all other values are packed into a new list &lt;code&gt;b&lt;/code&gt;. If you run the &lt;a href=&quot;https://realpython.com/run-python-scripts/&quot;&gt;script&lt;/a&gt;, &lt;code&gt;print()&lt;/code&gt; will show you that your three variables have the values you would expect:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python extract_list_body.py
&lt;span class=&quot;go&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[2, 3, 4, 5]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Another interesting thing you can do with the unpacking operator &lt;code&gt;*&lt;/code&gt; is to split the items of any iterable object. This could be very useful if you need to merge two lists, for instance:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# merging_lists.py&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_first_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_second_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_merged_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_first_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_second_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_merged_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The unpacking operator &lt;code&gt;*&lt;/code&gt; is prepended to both &lt;code&gt;my_first_list&lt;/code&gt; and &lt;code&gt;my_second_list&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If you run this script, you&amp;rsquo;ll see that the result is a merged list:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python merging_lists.py
&lt;span class=&quot;go&quot;&gt;[1, 2, 3, 4, 5, 6]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can even merge two different dictionaries by using the unpacking operator &lt;code&gt;**&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# merging_dicts.py&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_first_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;B&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_second_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;C&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;D&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;my_merged_dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_first_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_second_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_merged_dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, the iterables to merge are &lt;code&gt;my_first_dict&lt;/code&gt; and &lt;code&gt;my_second_dict&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Executing this code outputs a merged dictionary:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python merging_dicts.py
&lt;span class=&quot;go&quot;&gt;{&amp;#39;A&amp;#39;: 1, &amp;#39;B&amp;#39;: 2, &amp;#39;C&amp;#39;: 3, &amp;#39;D&amp;#39;: 4}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Remember that the &lt;code&gt;*&lt;/code&gt; operator works on &lt;em&gt;any&lt;/em&gt; iterable object. It can also be used to unpack a &lt;a href=&quot;https://realpython.com/python-strings/&quot;&gt;string&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# string_to_list.py&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;RealPython&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In Python, strings are iterable objects, so &lt;code&gt;*&lt;/code&gt; will unpack it and place all individual values in a list &lt;code&gt;a&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python string_to_list.py
&lt;span class=&quot;go&quot;&gt;[&amp;#39;R&amp;#39;, &amp;#39;e&amp;#39;, &amp;#39;a&amp;#39;, &amp;#39;l&amp;#39;, &amp;#39;P&amp;#39;, &amp;#39;y&amp;#39;, &amp;#39;t&amp;#39;, &amp;#39;h&amp;#39;, &amp;#39;o&amp;#39;, &amp;#39;n&amp;#39;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The previous example seems great, but when you work with these operators it&amp;rsquo;s important to keep in mind the seventh rule of &lt;a href=&quot;https://www.python.org/dev/peps/pep-0020/&quot;&gt;&lt;em&gt;The Zen of Python&lt;/em&gt;&lt;/a&gt; by Tim Peters: &lt;em&gt;Readability counts&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;To see why, consider the following example:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# mysterious_statement.py&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;RealPython&amp;quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There&amp;rsquo;s the unpacking operator &lt;code&gt;*&lt;/code&gt;, followed by a variable, a comma, and an assignment. That&amp;rsquo;s a lot packed into one line! In fact, this code is no different from the previous example. It just takes the string &lt;code&gt;RealPython&lt;/code&gt; and assigns all the items to the new list &lt;code&gt;a&lt;/code&gt;, thanks to the unpacking operator &lt;code&gt;*&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The comma after the &lt;code&gt;a&lt;/code&gt; does the trick. When you use the unpacking operator with variable assignment, Python requires that your resulting variable is either a list or a tuple. With the trailing comma, you have actually defined a tuple with just one named variable &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;While this is a neat trick, many Pythonistas would not consider this code to be very readable. As such, it&amp;rsquo;s best to use these kinds of constructions sparingly.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;You are now able to use &lt;strong&gt;&lt;code&gt;*args&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;**kwargs&lt;/code&gt;&lt;/strong&gt; to accept a changeable number of arguments in your functions. You have also learned something more about the unpacking operators. &lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ve learned:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; actually mean&lt;/li&gt;
&lt;li&gt;How to use &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; in function definitions&lt;/li&gt;
&lt;li&gt;How to use a single asterisk (&lt;code&gt;*&lt;/code&gt;) to unpack iterables&lt;/li&gt;
&lt;li&gt;How to use two asterisks (&lt;code&gt;**&lt;/code&gt;) to unpack dictionaries&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you still have questions, don&amp;rsquo;t hesitate to reach out in the comments section below! To learn more about the use of the asterisks in Python, have a look at &lt;a href=&quot;https://treyhunner.com/2018/10/asterisks-in-python-what-they-are-and-how-to-use-them/&quot;&gt;Trey Hunner&amp;rsquo;s article on the subject&lt;/a&gt;.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Lists and Tuples in Python</title>
      <id>https://realpython.com/courses/lists-tuples-python/</id>
      <link href="https://realpython.com/courses/lists-tuples-python/"/>
      <updated>2019-09-03T14:00:00+00:00</updated>
      <summary>In this course, you&#39;ll cover the important characteristics of lists and tuples in Python 3. You&#39;ll learn how to define them and how to manipulate them. When you&#39;re finished, you&#39;ll have a good feel for when and how to use these object types in a Python program.</summary>
      <content type="html">
        &lt;p&gt;In this course, you&amp;rsquo;ll learn about working with lists and tuples. &lt;strong&gt;Lists&lt;/strong&gt; and &lt;strong&gt;tuples&lt;/strong&gt; are arguably Python&amp;rsquo;s most versatile, useful &lt;a href=&quot;https://realpython.com/python-data-types/&quot;&gt;data types&lt;/a&gt;. You&amp;rsquo;ll find them in virtually every non-trivial Python program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Here&amp;rsquo;s what you&amp;rsquo;ll learn in this tutorial:&lt;/strong&gt; You&amp;rsquo;ll cover the important characteristics of lists and tuples. You&amp;rsquo;ll learn how to define them and how to manipulate them.  When you&amp;rsquo;re finished, you&amp;rsquo;ll have a good feel for when and how to use these object types in a Python program.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;&lt;i class=&quot;fa fa-graduation-cap&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt; Take the Quiz:&lt;/strong&gt; Test your knowledge with our interactive “Python Lists and Tuples” quiz. Upon completion you will receive a score so you can track your learning progress over time:&lt;/p&gt;&lt;p class=&quot;text-center my-2&quot;&gt;&lt;a class=&quot;btn btn-primary&quot; href=&quot;/quizzes/python-lists-tuples/&quot; target=&quot;_blank&quot;&gt;Take the Quiz »&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Natural Language Processing With spaCy in Python</title>
      <id>https://realpython.com/natural-language-processing-spacy-python/</id>
      <link href="https://realpython.com/natural-language-processing-spacy-python/"/>
      <updated>2019-09-02T14:00:00+00:00</updated>
      <summary>In this step-by-step tutorial, you&#39;ll learn how to use spaCy. This free and open-source library for Natural Language Processing (NLP) in Python has a lot of built-in capabilities and is becoming increasingly popular for processing and analyzing data in NLP.</summary>
      <content type="html">
        &lt;p&gt;&lt;strong&gt;spaCy&lt;/strong&gt; is a free and open-source library for &lt;strong&gt;Natural Language Processing&lt;/strong&gt; (NLP) in Python with a lot of in-built capabilities. It&amp;rsquo;s becoming increasingly popular for processing and analyzing data in NLP. Unstructured textual data is produced at a large scale, and it&amp;rsquo;s important to process and derive insights from unstructured data. To do that, you need to represent the data in a format that can be understood by computers. NLP can help you do that.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this tutorial, you&amp;rsquo;ll learn:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What the foundational terms and concepts in NLP are&lt;/li&gt;
&lt;li&gt;How to implement those concepts in spaCy&lt;/li&gt;
&lt;li&gt;How to customize and extend built-in functionalities in spaCy&lt;/li&gt;
&lt;li&gt;How to perform basic statistical analysis on a text&lt;/li&gt;
&lt;li&gt;How to create a pipeline to process unstructured text&lt;/li&gt;
&lt;li&gt;How to parse a sentence and extract meaningful insights from it&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-tricks-sample&quot; data-focus=&quot;false&quot;&gt;Click here to get access to a chapter from Python Tricks: The Book&lt;/a&gt; that shows you Python&#39;s best practices with simple examples you can apply instantly to write more beautiful + Pythonic code.&lt;/p&gt;&lt;/div&gt;

&lt;h2 id=&quot;what-are-nlp-and-spacy&quot;&gt;What Are NLP and spaCy?&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;NLP&lt;/strong&gt; is a subfield of &lt;strong&gt;Artificial Intelligence&lt;/strong&gt; and is concerned with interactions between computers and human languages. NLP is the process of analyzing, understanding, and deriving meaning from human languages for computers.&lt;/p&gt;
&lt;p&gt;NLP helps you extract insights from unstructured text and has several use cases, such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Automatic_summarization&quot;&gt;Automatic summarization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Named-entity_recognition&quot;&gt;Named entity recognition&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Question_answering&quot;&gt;Question answering systems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Sentiment_analysis&quot;&gt;Sentiment analysis&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;spaCy is a free, open-source library for NLP in Python. It&amp;rsquo;s written in &lt;a href=&quot;https://cython.org/&quot;&gt;Cython&lt;/a&gt; and is designed to build information extraction or natural language understanding systems. It&amp;rsquo;s built for production use and provides a concise and user-friendly API.&lt;/p&gt;
&lt;h2 id=&quot;installation&quot;&gt;Installation&lt;/h2&gt;
&lt;p&gt;In this section, you&amp;rsquo;ll install spaCy and then download data and models for the English language.&lt;/p&gt;
&lt;h3 id=&quot;how-to-install-spacy&quot;&gt;How to Install spaCy&lt;/h3&gt;
&lt;p&gt;spaCy can be installed using &lt;strong&gt;&lt;code&gt;pip&lt;/code&gt;&lt;/strong&gt;, a Python package manager. You can use a &lt;strong&gt;virtual environment&lt;/strong&gt; to avoid depending on system-wide packages. To learn more about virtual environments and &lt;code&gt;pip&lt;/code&gt;, check out &lt;a href=&quot;https://realpython.com/what-is-pip/&quot;&gt;What Is Pip? A Guide for New Pythonistas&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/python-virtual-environments-a-primer/&quot;&gt;Python Virtual Environments: A Primer&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Create a new virtual environment:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python3 -m venv env
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Activate this virtual environment and install spaCy:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; ./env/bin/activate
&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip install spacy
&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;how-to-download-models-and-data&quot;&gt;How to Download Models and Data&lt;/h3&gt;
&lt;p&gt;spaCy has &lt;a href=&quot;https://spaCy.io/models&quot;&gt;different types&lt;/a&gt; of models. The default model for the English language is &lt;code&gt;en_core_web_sm&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Activate the virtual environment created in the previous step and download models and data for the English language:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; python -m spacy download en_core_web_sm
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Verify if the download was successful or not by loading it:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;en_core_web_sm&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If the &lt;code&gt;nlp&lt;/code&gt; object is created, then it means that spaCy was installed and that models and data were successfully downloaded.&lt;/p&gt;
&lt;h2 id=&quot;using-spacy&quot;&gt;Using spaCy&lt;/h2&gt;
&lt;p&gt;In this section, you&amp;rsquo;ll use spaCy for a given input string and a text file. Load the language model instance in spaCy:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;en_core_web_sm&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, the &lt;code&gt;nlp&lt;/code&gt; object is a language model instance. You can assume that, throughout this tutorial, &lt;code&gt;nlp&lt;/code&gt; refers to the language model loaded by &lt;code&gt;en_core_web_sm&lt;/code&gt;. Now you can use spaCy to read a string or a text file.&lt;/p&gt;
&lt;h3 id=&quot;how-to-read-a-string&quot;&gt;How to Read a String&lt;/h3&gt;
&lt;p&gt;You can use spaCy to create a processed &lt;a href=&quot;https://spaCy.io/api/doc&quot;&gt;Doc&lt;/a&gt; object, which is a container for accessing linguistic annotations, for a given input string:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;introduction_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;This tutorial is about Natural&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; Language Processing in Spacy.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;introduction_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;introduction_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract tokens for the given doc&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;introduction_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;This&amp;#39;, &amp;#39;tutorial&amp;#39;, &amp;#39;is&amp;#39;, &amp;#39;about&amp;#39;, &amp;#39;Natural&amp;#39;, &amp;#39;Language&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;Processing&amp;#39;, &amp;#39;in&amp;#39;, &amp;#39;Spacy&amp;#39;, &amp;#39;.&amp;#39;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the above example, notice how the text is converted to an object that is understood by spaCy. You can use this method to convert any text into a processed &lt;code&gt;Doc&lt;/code&gt; object and deduce attributes, which will be covered in the coming sections.&lt;/p&gt;
&lt;h3 id=&quot;how-to-read-a-text-file&quot;&gt;How to Read a Text File&lt;/h3&gt;
&lt;p&gt;In this section, you&amp;rsquo;ll create a processed &lt;a href=&quot;https://spaCy.io/api/doc&quot;&gt;Doc&lt;/a&gt; object for a text file:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;introduction.txt&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;introduction_file_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;introduction_file_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;introduction_file_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract tokens for the given doc&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;introduction_file_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;This&amp;#39;, &amp;#39;tutorial&amp;#39;, &amp;#39;is&amp;#39;, &amp;#39;about&amp;#39;, &amp;#39;Natural&amp;#39;, &amp;#39;Language&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;Processing&amp;#39;, &amp;#39;in&amp;#39;, &amp;#39;Spacy&amp;#39;, &amp;#39;.&amp;#39;, &amp;#39;\n&amp;#39;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is how you can convert a text file into a processed &lt;code&gt;Doc&lt;/code&gt; object.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;/p&gt;
&lt;p&gt;You can assume that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Variable names ending with the suffix &lt;strong&gt;&lt;code&gt;_text&lt;/code&gt;&lt;/strong&gt; are &lt;strong&gt;&lt;a href=&quot;https://realpython.com/python-encodings-guide/&quot;&gt;Unicode&lt;/a&gt; string objects&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Variable name ending with the suffix &lt;strong&gt;&lt;code&gt;_doc&lt;/code&gt;&lt;/strong&gt; are &lt;strong&gt;spaCy&amp;rsquo;s language model objects&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id=&quot;sentence-detection&quot;&gt;Sentence Detection&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Sentence Detection&lt;/strong&gt; is the process of locating the start and end of sentences in a given text. This allows you to you divide a text into linguistically meaningful units. You&amp;rsquo;ll use these units when you&amp;rsquo;re processing your text to perform tasks such as &lt;strong&gt;part of speech tagging&lt;/strong&gt; and &lt;strong&gt;entity extraction&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In spaCy, the &lt;code&gt;sents&lt;/code&gt; property is used to extract sentences. Here&amp;rsquo;s how you would extract the total number of sentences and the sentences for a given input text:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Gus Proto is a Python developer currently&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;              &lt;span class=&quot;s1&quot;&gt;&amp;#39; working for a London-based Fintech&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;              &lt;span class=&quot;s1&quot;&gt;&amp;#39; company. He is interested in learning&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;              &lt;span class=&quot;s1&quot;&gt;&amp;#39; Natural Language Processing.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;Gus Proto is a Python developer currently working for a&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;London-based Fintech company.&amp;#39;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;He is interested in learning Natural Language Processing.&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the above example, spaCy is correctly able to identify sentences in the English language, using a full stop(&lt;code&gt;.&lt;/code&gt;) as the sentence delimiter. You can also customize the sentence detection to detect sentences on custom delimiters.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s an example, where an ellipsis(&lt;code&gt;...&lt;/code&gt;) is used as the delimiter:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;set_custom_boundaries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Adds support to use `...` as the delimiter for sentence detection&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;...&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;            &lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_sent_start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ellipsis_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Gus, can you, ... never mind, I forgot&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                 &lt;span class=&quot;s1&quot;&gt;&amp;#39; what I was saying. So, do you think&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                 &lt;span class=&quot;s1&quot;&gt;&amp;#39; we should ...&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Load a new model instance&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;en_core_web_sm&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_pipe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_custom_boundaries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;before&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;parser&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_ellipsis_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ellipsis_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_ellipsis_sentences&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_ellipsis_doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;custom_ellipsis_sentences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Gus, can you, ...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;never mind, I forgot what I was saying.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;So, do you think we should ...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Sentence Detection with no customization&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ellipsis_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ellipsis_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ellipsis_sentences&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ellipsis_doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ellipsis_sentences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Gus, can you, ... never mind, I forgot what I was saying.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;So, do you think we should ...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that &lt;code&gt;custom_ellipsis_sentences&lt;/code&gt; contain three sentences, whereas &lt;code&gt;ellipsis_sentences&lt;/code&gt; contains two sentences. These sentences are still obtained via the &lt;code&gt;sents&lt;/code&gt; attribute, as you saw before.&lt;/p&gt;
&lt;h2 id=&quot;tokenization-in-spacy&quot;&gt;Tokenization in spaCy&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Tokenization&lt;/strong&gt; is the next step after sentence detection. It allows you to identify the basic units in your text. These basic units are called &lt;strong&gt;tokens&lt;/strong&gt;. Tokenization is useful because it breaks a text into meaningful units. These units are used for further analysis, like part of speech tagging.&lt;/p&gt;
&lt;p&gt;In spaCy, you can print tokens by iterating on the &lt;code&gt;Doc&lt;/code&gt; object:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Gus 0&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Proto 4&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;is 10&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;a 13&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Python 15&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;developer 22&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;currently 32&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;working 42&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;for 50&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;a 54&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;London 56&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;- 62&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;based 63&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Fintech 69&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;company 77&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;. 84&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;He 86&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;is 89&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;interested 92&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;in 103&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;learning 106&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Natural 115&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Language 123&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Processing 132&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;. 142&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note how spaCy preserves the &lt;strong&gt;starting index&lt;/strong&gt; of the tokens. It&amp;rsquo;s useful for in-place word replacement. spaCy provides &lt;a href=&quot;https://spacy.io/api/token#attributes&quot;&gt;various attributes&lt;/a&gt; for the &lt;code&gt;Token&lt;/code&gt; class:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_with_ws&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;           &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_alpha&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_punct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_space&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;           &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Gus 0 Gus  True False False Xxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Proto 4 Proto  True False False Xxxxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;is 10 is  True False False xx True&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;a 13 a  True False False x True&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Python 15 Python  True False False Xxxxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;developer 22 developer  True False False xxxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;currently 32 currently  True False False xxxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;working 42 working  True False False xxxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;for 50 for  True False False xxx True&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;a 54 a  True False False x True&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;London 56 London True False False Xxxxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;- 62 - False True False - False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;based 63 based  True False False xxxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Fintech 69 Fintech  True False False Xxxxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;company 77 company True False False xxxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;. 84 .  False True False . False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;He 86 He  True False False Xx True&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;is 89 is  True False False xx True&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;interested 92 interested  True False False xxxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;in 103 in  True False False xx True&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;learning 106 learning  True False False xxxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Natural 115 Natural  True False False Xxxxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Language 123 Language  True False False Xxxxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Processing 132 Processing True False False Xxxxx False&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;. 142 . False True False . False&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, some of the commonly required attributes are accessed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;text_with_ws&lt;/code&gt;&lt;/strong&gt; prints token text with trailing space (if present).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;is_alpha&lt;/code&gt;&lt;/strong&gt; detects if the token consists of alphabetic characters or not.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;is_punct&lt;/code&gt;&lt;/strong&gt; detects if the token is a punctuation symbol or not.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;is_space&lt;/code&gt;&lt;/strong&gt; detects if the token is a space or not.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;shape_&lt;/code&gt;&lt;/strong&gt; prints out the shape of the word.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;is_stop&lt;/code&gt;&lt;/strong&gt; detects if the token is a stop word or not.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You&amp;rsquo;ll learn more about &lt;strong&gt;stop words&lt;/strong&gt; in the next section.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can also customize the tokenization process to detect tokens on custom characters. This is often used for hyphenated words, which are words joined with hyphen. For example, &amp;ldquo;London-based&amp;rdquo; is a hyphenated word.&lt;/p&gt;
&lt;p&gt;spaCy allows you to customize tokenization by updating the &lt;code&gt;tokenizer&lt;/code&gt; property on the &lt;code&gt;nlp&lt;/code&gt; object:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;re&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy.tokenizer&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Tokenizer&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;en_core_web_sm&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prefix_re&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compile_prefix_regex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Defaults&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prefixes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;suffix_re&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compile_suffix_regex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Defaults&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;suffixes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;infix_re&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;re&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&amp;#39;[-~]&amp;#39;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;customize_tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Adds support to use `-` as the delimiter for tokenization&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prefix_search&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prefix_re&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                     &lt;span class=&quot;n&quot;&gt;suffix_search&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;suffix_re&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                     &lt;span class=&quot;n&quot;&gt;infix_finditer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;infix_re&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;finditer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                     &lt;span class=&quot;n&quot;&gt;token_match&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                     &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;customize_tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;custom_tokenizer_about_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;custom_nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;custom_tokenizer_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;Gus&amp;#39;, &amp;#39;Proto&amp;#39;, &amp;#39;is&amp;#39;, &amp;#39;a&amp;#39;, &amp;#39;Python&amp;#39;, &amp;#39;developer&amp;#39;, &amp;#39;currently&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;working&amp;#39;, &amp;#39;for&amp;#39;, &amp;#39;a&amp;#39;, &amp;#39;London&amp;#39;, &amp;#39;-&amp;#39;, &amp;#39;based&amp;#39;, &amp;#39;Fintech&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;company&amp;#39;, &amp;#39;.&amp;#39;, &amp;#39;He&amp;#39;, &amp;#39;is&amp;#39;, &amp;#39;interested&amp;#39;, &amp;#39;in&amp;#39;, &amp;#39;learning&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;Natural&amp;#39;, &amp;#39;Language&amp;#39;, &amp;#39;Processing&amp;#39;, &amp;#39;.&amp;#39;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In order for you to customize, you can pass various parameters to the &lt;code&gt;Tokenizer&lt;/code&gt; class:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;nlp.vocab&lt;/code&gt;&lt;/strong&gt; is a storage container for special cases and is used to handle cases like contractions and emoticons.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;prefix_search&lt;/code&gt;&lt;/strong&gt; is the function that is used to handle preceding punctuation, such as opening parentheses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;infix_finditer&lt;/code&gt;&lt;/strong&gt; is the function that is used to handle non-whitespace separators, such as hyphens.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;suffix_search&lt;/code&gt;&lt;/strong&gt; is the function that is used to handle succeeding punctuation, such as closing parentheses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;token_match&lt;/code&gt;&lt;/strong&gt; is an optional boolean function that is used to match strings that should never be split. It overrides the previous rules and is useful for entities like URLs or numbers.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; spaCy already detects hyphenated words as individual tokens. The above code is just an example to show how tokenization can be customized. It can be used for any other character.&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;stop-words&quot;&gt;Stop Words&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Stop words&lt;/strong&gt; are the most common words in a language. In the English language, some examples of stop words are &lt;code&gt;the&lt;/code&gt;, &lt;code&gt;are&lt;/code&gt;, &lt;code&gt;but&lt;/code&gt;, and &lt;code&gt;they&lt;/code&gt;. Most sentences need to contain stop words in order to be full sentences that make sense.&lt;/p&gt;
&lt;p&gt;Generally, stop words are removed because they aren&amp;rsquo;t significant and distort the word frequency analysis. spaCy has a list of stop words for the English language:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spacy_stopwords&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;en&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop_words&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STOP_WORDS&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spacy_stopwords&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;326&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stop_word&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spacy_stopwords&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop_word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;using&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;becomes&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;had&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;itself&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;once&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;often&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;is&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;herein&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;who&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;too&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can remove stop words from the input text:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Gus&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Proto&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Python&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;developer&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;currently&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;working&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;London&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;-&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;based&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Fintech&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;company&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;interested&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;learning&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Natural&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Language&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Processing&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Stop words like &lt;code&gt;is&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;the&lt;/code&gt;, and &lt;code&gt;in&lt;/code&gt; are not printed in the output above. You can also create a list of tokens not containing stop words:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_no_stopword_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_no_stopword_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[Gus, Proto, Python, developer, currently, working, London,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;-, based, Fintech, company, ., interested, learning, Natural,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Language, Processing, .]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;about_no_stopword_doc&lt;/code&gt; can be joined with spaces to form a sentence with no stop words.&lt;/p&gt;
&lt;h2 id=&quot;lemmatization&quot;&gt;Lemmatization&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Lemmatization&lt;/strong&gt; is the process of reducing inflected forms of a word while still ensuring that the reduced form belongs to the language. This reduced form or root word is called a &lt;strong&gt;lemma&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;For example, &lt;em&gt;organizes&lt;/em&gt;, &lt;em&gt;organized&lt;/em&gt; and &lt;em&gt;organizing&lt;/em&gt; are all forms of &lt;em&gt;organize&lt;/em&gt;. Here, &lt;em&gt;organize&lt;/em&gt; is the lemma. The inflection of a word allows you to express different grammatical categories like tense (&lt;em&gt;organized&lt;/em&gt; vs &lt;em&gt;organize&lt;/em&gt;), number (&lt;em&gt;trains&lt;/em&gt; vs &lt;em&gt;train&lt;/em&gt;), and so on. Lemmatization is necessary because it helps you reduce the inflected forms of a word so that they can be analyzed as a single item. It can also help you &lt;strong&gt;normalize&lt;/strong&gt; the text.&lt;/p&gt;
&lt;p&gt;spaCy has the attribute &lt;code&gt;lemma_&lt;/code&gt; on the &lt;code&gt;Token&lt;/code&gt; class. This attribute has the lemmatized form of a token:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_help_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Gus is helping organize a developer&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39;conference on Applications of Natural Language&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; Processing. He keeps organizing local Python meetups&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; and several internal talks at his workplace.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_help_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_help_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conference_help_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lemma_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Gus Gus&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;is be&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;helping help&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;organize organize&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;a a&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;developer developer&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;conference conference&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;on on&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Applications Applications&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;of of&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Natural Natural&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Language Language&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Processing Processing&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;. .&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;He -PRON-&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;keeps keep&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;organizing organize&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;local local&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Python Python&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;meetups meetup&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;and and&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;several several&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;internal internal&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;talks talk&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;at at&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;his -PRON-&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;workplace workplace&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;. .&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, &lt;code&gt;organizing&lt;/code&gt; reduces to its lemma form &lt;code&gt;organize&lt;/code&gt;. If you do not lemmatize the text, then &lt;code&gt;organize&lt;/code&gt; and &lt;code&gt;organizing&lt;/code&gt; will be counted as different tokens, even though they both have a similar meaning. Lemmatization helps you avoid duplicate words that have similar meanings.&lt;/p&gt;
&lt;h2 id=&quot;word-frequency&quot;&gt;Word Frequency&lt;/h2&gt;
&lt;p&gt;You can now convert a given text into tokens and perform statistical analysis over it. This analysis can give you various insights about word patterns, such as common words or unique words in the text:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;collections&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;complete_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Gus Proto is a Python developer currently&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39;working for a London-based Fintech company. He is&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; interested in learning Natural Language Processing.&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; There is a developer conference happening on 21 July&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; 2019 in London. It is titled &amp;quot;Applications of Natural&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; Language Processing&amp;quot;. There is a helpline number &amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; available at +1-1234567891. Gus is helping organize it.&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; He keeps organizing local Python meetups and several&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; internal talks at his workplace. Gus is also presenting&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; a talk. The talk will introduce the reader about &amp;quot;Use&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; cases of Natural Language Processing in Fintech&amp;quot;.&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; Apart from his work, he is very passionate about music.&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; Gus is learning to play the Piano. He has enrolled &amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; himself in the weekend batch of Great Piano Academy.&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; Great Piano Academy is situated in Mayfair or the City&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; of London and has world-class piano instructors.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;complete_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;complete_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Remove stop words and punctuation symbols&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;complete_doc&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_stop&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_punct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word_freq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 5 commonly occurring words with their frequencies&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;common_words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word_freq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;most_common&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;common_words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[(&amp;#39;Gus&amp;#39;, 4), (&amp;#39;London&amp;#39;, 3), (&amp;#39;Natural&amp;#39;, 3), (&amp;#39;Language&amp;#39;, 3), (&amp;#39;Processing&amp;#39;, 3)]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Unique words&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word_freq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;freq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;Proto&amp;#39;, &amp;#39;currently&amp;#39;, &amp;#39;working&amp;#39;, &amp;#39;based&amp;#39;, &amp;#39;company&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;interested&amp;#39;, &amp;#39;conference&amp;#39;, &amp;#39;happening&amp;#39;, &amp;#39;21&amp;#39;, &amp;#39;July&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;2019&amp;#39;, &amp;#39;titled&amp;#39;, &amp;#39;Applications&amp;#39;, &amp;#39;helpline&amp;#39;, &amp;#39;number&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;available&amp;#39;, &amp;#39;+1&amp;#39;, &amp;#39;1234567891&amp;#39;, &amp;#39;helping&amp;#39;, &amp;#39;organize&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;keeps&amp;#39;, &amp;#39;organizing&amp;#39;, &amp;#39;local&amp;#39;, &amp;#39;meetups&amp;#39;, &amp;#39;internal&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;talks&amp;#39;, &amp;#39;workplace&amp;#39;, &amp;#39;presenting&amp;#39;, &amp;#39;introduce&amp;#39;, &amp;#39;reader&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;Use&amp;#39;, &amp;#39;cases&amp;#39;, &amp;#39;Apart&amp;#39;, &amp;#39;work&amp;#39;, &amp;#39;passionate&amp;#39;, &amp;#39;music&amp;#39;, &amp;#39;play&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;enrolled&amp;#39;, &amp;#39;weekend&amp;#39;, &amp;#39;batch&amp;#39;, &amp;#39;situated&amp;#39;, &amp;#39;Mayfair&amp;#39;, &amp;#39;City&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;world&amp;#39;, &amp;#39;class&amp;#39;, &amp;#39;piano&amp;#39;, &amp;#39;instructors&amp;#39;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By looking at the common words, you can see that the text as a whole is probably about &lt;code&gt;Gus&lt;/code&gt;, &lt;code&gt;London&lt;/code&gt;, or &lt;code&gt;Natural Language Processing&lt;/code&gt;. This way, you can take any unstructured text and perform statistical analysis to know what it&amp;rsquo;s about.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s another example of the same text with stop words:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words_all&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;complete_doc&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_punct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word_freq_all&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 5 commonly occurring words with their frequencies&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;common_words_all&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word_freq_all&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;most_common&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;common_words_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[(&amp;#39;is&amp;#39;, 10), (&amp;#39;a&amp;#39;, 5), (&amp;#39;in&amp;#39;, 5), (&amp;#39;Gus&amp;#39;, 4), (&amp;#39;of&amp;#39;, 4)]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Four out of five of the most common words are stop words, which don&amp;rsquo;t tell you much about the text. If you consider stop words while doing word frequency analysis, then you won&amp;rsquo;t be able to derive meaningful insights from the input text. This is why removing stop words is so important.&lt;/p&gt;
&lt;h2 id=&quot;part-of-speech-tagging&quot;&gt;Part of Speech Tagging&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Part of speech&lt;/strong&gt; or &lt;strong&gt;POS&lt;/strong&gt; is a grammatical role that explains how a particular word is used in a sentence. There are eight parts of speech:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Noun&lt;/li&gt;
&lt;li&gt;Pronoun&lt;/li&gt;
&lt;li&gt;Adjective&lt;/li&gt;
&lt;li&gt;Verb&lt;/li&gt;
&lt;li&gt;Adverb&lt;/li&gt;
&lt;li&gt;Preposition&lt;/li&gt;
&lt;li&gt;Conjunction&lt;/li&gt;
&lt;li&gt;Interjection&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Part of speech tagging&lt;/strong&gt; is the process of assigning a &lt;strong&gt;POS tag&lt;/strong&gt; to each token depending on its usage in the sentence. POS tags are useful for assigning a syntactic category like &lt;strong&gt;noun&lt;/strong&gt; or &lt;strong&gt;verb&lt;/strong&gt; to each word.&lt;/p&gt;
&lt;p&gt;In spaCy, POS tags are available as an attribute on the &lt;code&gt;Token&lt;/code&gt; object:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;explain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Gus NNP PROPN noun, proper singular&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Proto NNP PROPN noun, proper singular&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;is VBZ VERB verb, 3rd person singular present&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;a DT DET determiner&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Python NNP PROPN noun, proper singular&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;developer NN NOUN noun, singular or mass&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;currently RB ADV adverb&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;working VBG VERB verb, gerund or present participle&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;for IN ADP conjunction, subordinating or preposition&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;a DT DET determiner&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;London NNP PROPN noun, proper singular&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;- HYPH PUNCT punctuation mark, hyphen&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;based VBN VERB verb, past participle&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Fintech NNP PROPN noun, proper singular&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;company NN NOUN noun, singular or mass&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;. . PUNCT punctuation mark, sentence closer&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;He PRP PRON pronoun, personal&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;is VBZ VERB verb, 3rd person singular present&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;interested JJ ADJ adjective&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;in IN ADP conjunction, subordinating or preposition&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;learning VBG VERB verb, gerund or present participle&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Natural NNP PROPN noun, proper singular&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Language NNP PROPN noun, proper singular&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Processing NNP PROPN noun, proper singular&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;. . PUNCT punctuation mark, sentence closer&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, two attributes of the &lt;code&gt;Token&lt;/code&gt; class are accessed:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;tag_&lt;/code&gt;&lt;/strong&gt; lists the fine-grained part of speech.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;pos_&lt;/code&gt;&lt;/strong&gt; lists the coarse-grained part of speech.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;code&gt;spacy.explain&lt;/code&gt; gives descriptive details about a particular POS tag. spaCy provides a &lt;a href=&quot;https://spaCy.io/api/annotation#pos-tagging&quot;&gt;complete tag list&lt;/a&gt; along with an explanation for each tag.&lt;/p&gt;
&lt;p&gt;Using POS tags, you can extract a particular category of words:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nouns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;adjectives&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;NOUN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;nouns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;ADJ&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;adjectives&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nouns&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[developer, company]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;adjectives&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[interested]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can use this to derive insights, remove the most common nouns, or see which adjectives are used for a particular noun.&lt;/p&gt;
&lt;h2 id=&quot;visualization-using-displacy&quot;&gt;Visualization: Using displaCy&lt;/h2&gt;
&lt;p&gt;spaCy comes with a built-in visualizer called &lt;strong&gt;displaCy&lt;/strong&gt;. You can use it to visualize a &lt;strong&gt;dependency parse&lt;/strong&gt; or &lt;strong&gt;named entities&lt;/strong&gt; in a browser or a &lt;a href=&quot;https://realpython.com/jupyter-notebook-introduction/&quot;&gt;Jupyter notebook&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can use displaCy to find POS tags for tokens:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;displacy&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_interest_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;He is interested in learning&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; Natural Language Processing.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_interest_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_interest_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;displacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_interest_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;dep&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The above code will spin a simple web server. You can see the visualization by opening &lt;a href=&quot;http://127.0.0.1:5000&quot;&gt;http://127.0.0.1:5000&lt;/a&gt; in your browser:&lt;/p&gt;
&lt;figure class=&quot;figure mx-auto d-block&quot;&gt;&lt;a href=&quot;https://files.realpython.com/media/displacy_pos_tags.45059f2bf851.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/displacy_pos_tags.45059f2bf851.png&quot; width=&quot;2630&quot; height=&quot;600&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/displacy_pos_tags.45059f2bf851.png&amp;amp;w=657&amp;amp;sig=a49d6b5a0e5952aea59c0241f61fb09440bb326b 657w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/displacy_pos_tags.45059f2bf851.png&amp;amp;w=1315&amp;amp;sig=858218e45ae1e23a87ad42204154aeae77c9cc0c 1315w, https://files.realpython.com/media/displacy_pos_tags.45059f2bf851.png 2630w&quot; sizes=&quot;75vw&quot; alt=&quot;Displacy: Part of Speech Tagging Demo&quot;/&gt;&lt;/a&gt;&lt;figcaption class=&quot;figure-caption text-center&quot;&gt;displaCy: Part of Speech Tagging Demo&lt;/figcaption&gt;&lt;/figure&gt;

&lt;p&gt;In the image above, each token is assigned a POS tag written just below the token.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Here&amp;rsquo;s how you can use displaCy in a Jupyter notebook:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;displacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_interest_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;dep&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jupyter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;h2 id=&quot;preprocessing-functions&quot;&gt;Preprocessing Functions&lt;/h2&gt;
&lt;p&gt;You can create a &lt;strong&gt;preprocessing function&lt;/strong&gt; that takes text as input and applies the following operations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Lowercases the text&lt;/li&gt;
&lt;li&gt;Lemmatizes each token&lt;/li&gt;
&lt;li&gt;Removes punctuation symbols&lt;/li&gt;
&lt;li&gt;Removes stop words&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A preprocessing function converts text to an analyzable format. It&amp;rsquo;s necessary for most NLP tasks. Here&amp;rsquo;s an example:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;is_token_allowed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;sd&quot;&gt;&amp;#39;&amp;#39;&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;        Only allow valid tokens which are not stop words&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;        and punctuation symbols.&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;    &amp;#39;&amp;#39;&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_stop&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is_punct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;preprocess_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Reduce token to its lowercase lemma form&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lemma_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;complete_filtered_tokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;preprocess_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;complete_doc&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_token_allowed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;complete_filtered_tokens&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;gus&amp;#39;, &amp;#39;proto&amp;#39;, &amp;#39;python&amp;#39;, &amp;#39;developer&amp;#39;, &amp;#39;currently&amp;#39;, &amp;#39;work&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;london&amp;#39;, &amp;#39;base&amp;#39;, &amp;#39;fintech&amp;#39;, &amp;#39;company&amp;#39;, &amp;#39;interested&amp;#39;, &amp;#39;learn&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;natural&amp;#39;, &amp;#39;language&amp;#39;, &amp;#39;processing&amp;#39;, &amp;#39;developer&amp;#39;, &amp;#39;conference&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;happen&amp;#39;, &amp;#39;21&amp;#39;, &amp;#39;july&amp;#39;, &amp;#39;2019&amp;#39;, &amp;#39;london&amp;#39;, &amp;#39;title&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;applications&amp;#39;, &amp;#39;natural&amp;#39;, &amp;#39;language&amp;#39;, &amp;#39;processing&amp;#39;, &amp;#39;helpline&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;number&amp;#39;, &amp;#39;available&amp;#39;, &amp;#39;+1&amp;#39;, &amp;#39;1234567891&amp;#39;, &amp;#39;gus&amp;#39;, &amp;#39;help&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;organize&amp;#39;, &amp;#39;keep&amp;#39;, &amp;#39;organize&amp;#39;, &amp;#39;local&amp;#39;, &amp;#39;python&amp;#39;, &amp;#39;meetup&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;internal&amp;#39;, &amp;#39;talk&amp;#39;, &amp;#39;workplace&amp;#39;, &amp;#39;gus&amp;#39;, &amp;#39;present&amp;#39;, &amp;#39;talk&amp;#39;, &amp;#39;talk&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;introduce&amp;#39;, &amp;#39;reader&amp;#39;, &amp;#39;use&amp;#39;, &amp;#39;case&amp;#39;, &amp;#39;natural&amp;#39;, &amp;#39;language&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;processing&amp;#39;, &amp;#39;fintech&amp;#39;, &amp;#39;apart&amp;#39;, &amp;#39;work&amp;#39;, &amp;#39;passionate&amp;#39;, &amp;#39;music&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;gus&amp;#39;, &amp;#39;learn&amp;#39;, &amp;#39;play&amp;#39;, &amp;#39;piano&amp;#39;, &amp;#39;enrol&amp;#39;, &amp;#39;weekend&amp;#39;, &amp;#39;batch&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;great&amp;#39;, &amp;#39;piano&amp;#39;, &amp;#39;academy&amp;#39;, &amp;#39;great&amp;#39;, &amp;#39;piano&amp;#39;, &amp;#39;academy&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;situate&amp;#39;, &amp;#39;mayfair&amp;#39;, &amp;#39;city&amp;#39;, &amp;#39;london&amp;#39;, &amp;#39;world&amp;#39;, &amp;#39;class&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;piano&amp;#39;, &amp;#39;instructor&amp;#39;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that the &lt;code&gt;complete_filtered_tokens&lt;/code&gt; does not contain any stop word or punctuation symbols and consists of lemmatized lowercase tokens.&lt;/p&gt;
&lt;h2 id=&quot;rule-based-matching-using-spacy&quot;&gt;Rule-Based Matching Using spaCy&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Rule-based matching&lt;/strong&gt; is one of the steps in extracting information from unstructured text. It&amp;rsquo;s used to identify and extract tokens and phrases according to patterns (such as lowercase) and grammatical features (such as part of speech).&lt;/p&gt;
&lt;p&gt;Rule-based matching can use &lt;a href=&quot;https://en.wikipedia.org/wiki/Regular_expression&quot;&gt;regular expressions&lt;/a&gt; to extract entities (such as phone numbers) from an unstructured text. It&amp;rsquo;s different from extracting text using regular expressions only in the sense that regular expressions don&amp;rsquo;t consider the lexical and grammatical attributes of the text.&lt;/p&gt;
&lt;p&gt;With rule-based matching, you can extract a first name and a last name, which are always &lt;strong&gt;proper nouns&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy.matcher&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Matcher&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matcher&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Matcher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;extract_full_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;POS&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;PROPN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;POS&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;PROPN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;matcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;FULL_NAME&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matcher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;match_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extract_full_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;Gus Proto&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, &lt;code&gt;pattern&lt;/code&gt; is a list of objects that defines the combination of tokens to be matched. Both POS tags in it are &lt;code&gt;PROPN&lt;/code&gt; (proper noun). So, the &lt;code&gt;pattern&lt;/code&gt; consists of two objects in which the POS tags for both tokens should be &lt;code&gt;PROPN&lt;/code&gt;. This pattern is then added to &lt;code&gt;Matcher&lt;/code&gt; using &lt;code&gt;FULL_NAME&lt;/code&gt; and the the &lt;code&gt;match_id&lt;/code&gt;. Finally, matches are obtained with their starting and end indexes.&lt;/p&gt;
&lt;p&gt;You can also use rule-based matching to extract phone numbers:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;spacy.matcher&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Matcher&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matcher&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Matcher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_org_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;There is a developer conference&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39;happening on 21 July 2019 in London. It is titled&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; &amp;quot;Applications of Natural Language Processing&amp;quot;.&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; There is a helpline number available&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; at (123) 456-789&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;extract_phone_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;ORTH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;(&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;SHAPE&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;ddd&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;               &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;ORTH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;)&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;SHAPE&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;ddd&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;               &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;ORTH&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;-&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;OP&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;?&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;               &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;SHAPE&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;ddd&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;matcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;PHONE_NUMBER&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matcher&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;match_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;span&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_org_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_org_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extract_phone_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_org_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;(123) 456-789&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, only the pattern is updated in order to match phone numbers from the previous example. Here, some attributes of the token are also used:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;ORTH&lt;/code&gt;&lt;/strong&gt; gives the exact text of the token.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;SHAPE&lt;/code&gt;&lt;/strong&gt; transforms the token string to show orthographic features.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;OP&lt;/code&gt;&lt;/strong&gt; defines operators. Using &lt;code&gt;?&lt;/code&gt; as a value means that the pattern is optional, meaning it can match 0 or 1 times.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; For simplicity, phone numbers are assumed to be of a particular format: &lt;code&gt;(123) 456-789&lt;/code&gt;. You can change this depending on your use case.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Rule-based matching helps you identify and extract tokens and phrases according to lexical patterns (such as lowercase) and grammatical features(such as part of speech).&lt;/p&gt;
&lt;h2 id=&quot;dependency-parsing-using-spacy&quot;&gt;Dependency Parsing Using spaCy&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Dependency parsing&lt;/strong&gt; is the process of extracting the dependency parse of a sentence to represent its grammatical structure. It defines the dependency relationship between &lt;strong&gt;headwords&lt;/strong&gt; and their &lt;strong&gt;dependents&lt;/strong&gt;. The head of a sentence has no dependency and is called the &lt;strong&gt;root of the sentence&lt;/strong&gt;. The &lt;strong&gt;verb&lt;/strong&gt; is usually the head of the sentence. All other words are linked to the headword.&lt;/p&gt;
&lt;p&gt;The dependencies can be mapped in a directed graph representation: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Words are the nodes.&lt;/li&gt;
&lt;li&gt;The grammatical relationships are the edges.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Dependency parsing helps you know what role a word plays in the text and how different words relate to each other. It&amp;rsquo;s also used in &lt;strong&gt;shallow parsing&lt;/strong&gt; and named entity recognition.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s how you can use dependency parsing to see the relationships between words:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Gus is learning piano&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;piano_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dep_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Gus NNP learning nsubj&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;is VBZ learning aux&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;learning VBG learning ROOT&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;piano NN learning dobj&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, the sentence contains three relationships:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;nsubj&lt;/code&gt;&lt;/strong&gt; is the subject of the word. Its headword is a verb.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;aux&lt;/code&gt;&lt;/strong&gt; is an auxiliary word. Its headword is a verb.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;dobj&lt;/code&gt;&lt;/strong&gt; is the direct object of the verb. Its headword is a verb.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;There is a detailed &lt;a href=&quot;https://nlp.stanford.edu/software/dependencies_manual.pdf&quot;&gt;list of relationships&lt;/a&gt; with descriptions. You can use displaCy to visualize the dependency tree:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;displacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;dep&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code will produce a visualization that can be accessed by opening &lt;a href=&quot;http://127.0.0.1:5000&quot;&gt;http://127.0.0.1:5000&lt;/a&gt; in your browser:&lt;/p&gt;
&lt;figure class=&quot;figure mx-auto d-block&quot;&gt;&lt;a href=&quot;https://files.realpython.com/media/displacy_dependency_parse.de72f9b1d115.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/displacy_dependency_parse.de72f9b1d115.png&quot; width=&quot;1278&quot; height=&quot;596&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/displacy_dependency_parse.de72f9b1d115.png&amp;amp;w=319&amp;amp;sig=111728c07cf2e1f64b8419cfce8a5f880c244d03 319w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/displacy_dependency_parse.de72f9b1d115.png&amp;amp;w=639&amp;amp;sig=f90a72529d7bc2d2dd944af3c02bbf487b65aaf3 639w, https://files.realpython.com/media/displacy_dependency_parse.de72f9b1d115.png 1278w&quot; sizes=&quot;75vw&quot; alt=&quot;Displacy: Dependency Parse Demo&quot;/&gt;&lt;/a&gt;&lt;figcaption class=&quot;figure-caption text-center&quot;&gt;displaCy: Dependency Parse Demo&lt;/figcaption&gt;&lt;/figure&gt;

&lt;p&gt;This image shows you that the subject of the sentence is the proper noun &lt;code&gt;Gus&lt;/code&gt; and that it has a &lt;code&gt;learn&lt;/code&gt; relationship with &lt;code&gt;piano&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;navigating-the-tree-and-subtree&quot;&gt;Navigating the Tree and Subtree&lt;/h2&gt;
&lt;p&gt;The dependency parse tree has all the properties of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Tree_(data_structure)&quot;&gt;tree&lt;/a&gt;. This tree contains information about sentence structure and grammar and can be traversed in different ways to extract relationships.&lt;/p&gt;
&lt;p&gt;spaCy provides attributes like &lt;code&gt;children&lt;/code&gt;, &lt;code&gt;lefts&lt;/code&gt;, &lt;code&gt;rights&lt;/code&gt;, and &lt;code&gt;subtree&lt;/code&gt; to navigate the parse tree:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_line_about_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Gus Proto is a Python developer&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; currently working for a London-based Fintech company&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_line_about_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract children of `developer`&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;children&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;a&amp;#39;, &amp;#39;Python&amp;#39;, &amp;#39;working&amp;#39;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract previous neighboring node of `developer`&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nbor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Python&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract next neighboring node of `developer`&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nbor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;currently&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract all tokens on the left of `developer`&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lefts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;a&amp;#39;, &amp;#39;Python&amp;#39;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract tokens on the right of `developer`&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;working&amp;#39;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Print subtree of `developer`&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subtree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[a, Python, developer, currently, working, for, a, London, -,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;based, Fintech, company]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can construct a function that takes a subtree as an argument and returns a string by merging words in it:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;flatten_tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_with_ws&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Print flattened subtree of `developer`&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flatten_tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;one_line_about_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;subtree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;a Python developer currently working for a London-based Fintech company&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can use this function to print all the tokens in a subtree.&lt;/p&gt;
&lt;h2 id=&quot;shallow-parsing&quot;&gt;Shallow Parsing&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Shallow parsing&lt;/strong&gt;, or &lt;strong&gt;chunking&lt;/strong&gt;, is the process of extracting phrases from unstructured text. Chunking groups adjacent tokens into phrases on the basis of their POS tags. There are some standard well-known chunks such as noun phrases, verb phrases, and prepositional phrases.&lt;/p&gt;
&lt;h3 id=&quot;noun-phrase-detection&quot;&gt;Noun Phrase Detection&lt;/h3&gt;
&lt;p&gt;A noun phrase is a phrase that has a noun as its head. It could also include other kinds of words, such as adjectives, ordinals, determiners. Noun phrases are useful for explaining the context of the sentence. They help you infer &lt;em&gt;what&lt;/em&gt; is being talked about in the sentence.&lt;/p&gt;
&lt;p&gt;spaCy has the property &lt;code&gt;noun_chunks&lt;/code&gt; on &lt;code&gt;Doc&lt;/code&gt; object. You can use it to extract noun phrases:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;There is a developer conference&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; happening on 21 July 2019 in London.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conference_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract Noun Phrases&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conference_doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;noun_chunks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;a developer conference&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;21 July&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;London&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By looking at noun phrases, you can get information about your text. For example, &lt;code&gt;a developer conference&lt;/code&gt; indicates that the text mentions a conference, while the date &lt;code&gt;21 July&lt;/code&gt; lets you know that conference is scheduled for &lt;code&gt;21 July&lt;/code&gt;. You can figure out whether the conference is in the past or the future. &lt;code&gt;London&lt;/code&gt; tells you that the conference is in &lt;code&gt;London&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;verb-phrase-detection&quot;&gt;Verb Phrase Detection&lt;/h3&gt;
&lt;p&gt;A &lt;strong&gt;verb phrase&lt;/strong&gt; is a syntactic unit composed of at least one verb. This verb can be followed by other chunks, such as noun phrases. Verb phrases are useful for understanding the actions that nouns are involved in. &lt;/p&gt;
&lt;p&gt;spaCy has no built-in functionality to extract verb phrases, so you&amp;rsquo;ll need a library called &lt;a href=&quot;https://chartbeat-labs.github.io/textacy/&quot;&gt;&lt;code&gt;textacy&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;/p&gt;
&lt;p&gt;You can use &lt;code&gt;pip&lt;/code&gt; to install &lt;code&gt;textacy&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip install textacy
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;p&gt;Now that you have &lt;code&gt;textacy&lt;/code&gt; installed, you can use it to extract verb phrases based on grammar rules:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;textacy&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_talk_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;The talk will introduce reader about Use&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                   &lt;span class=&quot;s1&quot;&gt;&amp;#39; cases of Natural Language Processing in&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                   &lt;span class=&quot;s1&quot;&gt;&amp;#39; Fintech&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sa&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;(&amp;lt;VERB&amp;gt;?&amp;lt;ADV&amp;gt;*&amp;lt;VERB&amp;gt;+)&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_talk_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;textacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_spacy_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_talk_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                        &lt;span class=&quot;n&quot;&gt;lang&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;en_core_web_sm&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;verb_phrases&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;textacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extract&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos_regex_matches&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;about_talk_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Print all Verb Phrase&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;verb_phrases&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;will introduce&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Extract Noun Phrase to explain what nouns are involved&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;about_talk_doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;noun_chunks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chunk&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;The talk&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;reader&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Use cases&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Natural Language Processing&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Fintech&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, the verb phrase &lt;code&gt;introduce&lt;/code&gt; indicates that something will be introduced. By looking at noun phrases, you can see that there is a &lt;code&gt;talk&lt;/code&gt; that will &lt;code&gt;introduce&lt;/code&gt; the &lt;code&gt;reader&lt;/code&gt; to &lt;code&gt;use cases&lt;/code&gt; of &lt;code&gt;Natural Language Processing&lt;/code&gt; or &lt;code&gt;Fintech&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The above code extracts all the verb phrases &lt;a href=&quot;https://chartbeat-labs.github.io/textacy/api_reference/information_extraction.html?highlight=pos#textacy.extract.pos_regex_matches&quot;&gt;using a regular expression pattern&lt;/a&gt; of POS tags. You can tweak the pattern for verb phrases depending upon your use case.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In the previous example, you could have also done dependency parsing to see what the &lt;a href=&quot;https://nlp.stanford.edu/software/dependencies_manual.pdf&quot;&gt;relationships&lt;/a&gt; between the words were.&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;named-entity-recognition&quot;&gt;Named Entity Recognition&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Named Entity Recognition&lt;/strong&gt; (NER) is the process of locating &lt;strong&gt;named entities&lt;/strong&gt; in unstructured text and then classifying them into pre-defined categories, such as person names, organizations, locations, monetary values, percentages, time expressions, and so on.&lt;/p&gt;
&lt;p&gt;You can use &lt;strong&gt;NER&lt;/strong&gt; to know more about the meaning of your text. For example, you could use it to populate tags for a set of documents in order to improve the keyword search. You could also use it to categorize customer support tickets into relevant categories.&lt;/p&gt;
&lt;p&gt;spaCy has the property &lt;code&gt;ents&lt;/code&gt; on &lt;code&gt;Doc&lt;/code&gt; objects. You can use it to extract named entities:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_class_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Great Piano Academy is situated&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; in Mayfair or the City of London and has&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&amp;#39; world-class piano instructors.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_class_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_class_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;piano_class_doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start_char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end_char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;explain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;label_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Great Piano Academy 0 19 ORG Companies, agencies, institutions, etc.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Mayfair 35 42 GPE Countries, cities, states&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;the City of London 46 64 GPE Countries, cities, states&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the above example, &lt;code&gt;ent&lt;/code&gt; is a &lt;a href=&quot;https://spacy.io/api/span&quot;&gt;&lt;code&gt;Span&lt;/code&gt;&lt;/a&gt; object with various attributes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;text&lt;/code&gt;&lt;/strong&gt; gives the Unicode text representation of the entity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;start_char&lt;/code&gt;&lt;/strong&gt; denotes the character offset for the start of the entity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;end_char&lt;/code&gt;&lt;/strong&gt; denotes the character offset for the end of the entity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;label_&lt;/code&gt;&lt;/strong&gt; gives the label of the entity.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;spacy.explain&lt;/code&gt; gives descriptive details about an entity label. The spaCy model has a pre-trained &lt;a href=&quot;https://spaCy.io/api/annotation#named-entities&quot;&gt;list of entity classes&lt;/a&gt;. You can use displaCy to visualize these entities:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;displacy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;piano_class_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;ent&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you open &lt;a href=&quot;http://127.0.0.1:5000&quot;&gt;http://127.0.0.1:5000&lt;/a&gt; in your browser, then you can see the visualization:&lt;/p&gt;
&lt;figure class=&quot;figure mx-auto d-block&quot;&gt;&lt;a href=&quot;https://files.realpython.com/media/displacy_ner.1fba6869638f.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/displacy_ner.1fba6869638f.png&quot; width=&quot;1930&quot; height=&quot;140&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/displacy_ner.1fba6869638f.png&amp;amp;w=482&amp;amp;sig=18b93b0aed61930a6eedd37dbd12fbbce22733d4 482w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/displacy_ner.1fba6869638f.png&amp;amp;w=965&amp;amp;sig=f6b3cfb460053397a23a0eb49ebc22cf05dd15ab 965w, https://files.realpython.com/media/displacy_ner.1fba6869638f.png 1930w&quot; sizes=&quot;75vw&quot; alt=&quot;Displacy: Named Entity Recognition Demo&quot;/&gt;&lt;/a&gt;&lt;figcaption class=&quot;figure-caption text-center&quot;&gt;displaCy: Named Entity Recognition Demo&lt;/figcaption&gt;&lt;/figure&gt;

&lt;p&gt;You can use NER to redact people&amp;rsquo;s names from a text. For example, you might want to do this in order to hide personal information collected in a survey. You can use spaCy to do that:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;survey_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Out of 5 people surveyed, James Robert,&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;               &lt;span class=&quot;s1&quot;&gt;&amp;#39; Julie Fuller and Benjamin Brooks like&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;               &lt;span class=&quot;s1&quot;&gt;&amp;#39; apples. Kelly Cox and Matthew Evans&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;               &lt;span class=&quot;s1&quot;&gt;&amp;#39; like oranges.&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;replace_person_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ent_iob&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ent_type_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;PERSON&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;[REDACTED] &amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;redact_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;ent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;merge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace_person_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;survey_doc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;survey_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;redact_names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;survey_doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;Out of 5 people surveyed, [REDACTED] , [REDACTED] and&amp;#39;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39; [REDACTED] like apples. [REDACTED] and [REDACTED]&amp;#39;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39; like oranges.&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, &lt;code&gt;replace_person_names()&lt;/code&gt; uses &lt;code&gt;ent_iob&lt;/code&gt;. It gives the IOB code of the named entity tag using &lt;a href=&quot;https://en.wikipedia.org/wiki/Inside%E2%80%93outside%E2%80%93beginning_(tagging)&quot;&gt;inside-outside-beginning (IOB) tagging&lt;/a&gt;. Here, it can assume a value other than zero, because zero means that no entity tag is set.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;spaCy is a powerful and advanced library that is gaining huge popularity for NLP applications due to its speed, ease of use, accuracy, and extensibility. Congratulations! You now know:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What the foundational terms and concepts in NLP are&lt;/li&gt;
&lt;li&gt;How to implement those concepts in spaCy&lt;/li&gt;
&lt;li&gt;How to customize and extend built-in functionalities in spaCy&lt;/li&gt;
&lt;li&gt;How to perform basic statistical analysis on a text&lt;/li&gt;
&lt;li&gt;How to create a pipeline to process unstructured text&lt;/li&gt;
&lt;li&gt;How to parse a sentence and extract meaningful insights from it&lt;/li&gt;
&lt;/ul&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>PyCharm for Productive Python Development (Guide)</title>
      <id>https://realpython.com/pycharm-guide/</id>
      <link href="https://realpython.com/pycharm-guide/"/>
      <updated>2019-08-28T14:00:00+00:00</updated>
      <summary>In this step-by-step tutorial, you&#39;ll learn how you can use PyCharm to be a more productive Python developer. PyCharm makes debugging and visualization easy so you can focus on business logic and just get the job done.</summary>
      <content type="html">
        &lt;p&gt;As a programmer, you should be focused on the business logic and creating useful applications for your users. In doing that, &lt;a href=&quot;https://www.jetbrains.com/pycharm/&quot;&gt;PyCharm&lt;/a&gt; by &lt;a href=&quot;https://www.jetbrains.com/&quot;&gt;JetBrains&lt;/a&gt; saves you a lot of time by taking care of the routine and by making a number of other tasks such as debugging and visualization easy.   &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this article, you&amp;rsquo;ll learn about:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Installing PyCharm&lt;/li&gt;
&lt;li&gt;Writing code in PyCharm&lt;/li&gt;
&lt;li&gt;Running your code in PyCharm&lt;/li&gt;
&lt;li&gt;Debugging and testing your code in PyCharm&lt;/li&gt;
&lt;li&gt;Editing an existing project in PyCharm&lt;/li&gt;
&lt;li&gt;Searching and navigating in PyCharm&lt;/li&gt;
&lt;li&gt;Using Version Control in PyCharm&lt;/li&gt;
&lt;li&gt;Using Plugins and External Tools in PyCharm&lt;/li&gt;
&lt;li&gt;Using PyCharm Professional features, such as Django support and Scientific mode&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This article assumes that you&amp;rsquo;re familiar with Python development and already have some form of Python installed on your system. Python 3.6 will be used for this tutorial. Screenshots and demos provided are for macOS. Because PyCharm runs on all major platforms, you may see slightly different UI elements and may need to modify certain commands.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: &lt;/p&gt;
&lt;p&gt;PyCharm comes in three editions: &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://www.jetbrains.com/pycharm-edu/&quot;&gt;PyCharm Edu&lt;/a&gt; is free and for educational purposes.  &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.jetbrains.com/pycharm&quot;&gt;PyCharm Community&lt;/a&gt; is free as well and intended for pure Python development. &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.jetbrains.com/pycharm&quot;&gt;PyCharm Professional&lt;/a&gt; is paid, has everything the Community edition has and also is very well suited for Web and Scientific development with support for such frameworks as Django and Flask, Database and SQL, and scientific tools such as Jupyter.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For more details on their differences, check out the &lt;a href=&quot;https://www.jetbrains.com/pycharm/features/editions_comparison_matrix.html&quot;&gt;PyCharm Editions Comparison Matrix&lt;/a&gt; by JetBrains. The company also has &lt;a href=&quot;https://www.jetbrains.com/pycharm/buy/#edition=discounts&quot;&gt;special offers&lt;/a&gt; for students, teachers, open source projects, and other cases.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Clone Repo:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/alcazar-web-framework/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-alcazar-web-framework&quot; data-focus=&quot;false&quot;&gt;Click here to clone the repo you&#39;ll use&lt;/a&gt; to explore the project-focused features of PyCharm in this tutorial.&lt;/p&gt;&lt;/div&gt;

&lt;h2 id=&quot;installing-pycharm&quot;&gt;Installing PyCharm&lt;/h2&gt;
&lt;p&gt;This article will use PyCharm Community Edition 2019.1 as it&amp;rsquo;s free and available on every major platform. Only the section about the professional features will use PyCharm Professional Edition 2019.1.  &lt;/p&gt;
&lt;p&gt;The recommended way of installing PyCharm is with the &lt;a href=&quot;https://www.jetbrains.com/toolbox/app/&quot;&gt;JetBrains Toolbox App&lt;/a&gt;. With its help, you&amp;rsquo;ll be able to install different JetBrains products or several versions of the same product, update, rollback, and easily remove any tool when necessary. You&amp;rsquo;ll also be able to quickly open any project in the right IDE and version.&lt;/p&gt;
&lt;p&gt;To install the Toolbox App, refer to the &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/installation-guide.html#toolbox&quot;&gt;documentation&lt;/a&gt; by JetBrains. It will automatically give you the right instructions depending on your OS. In case it didn&amp;rsquo;t recognize your OS correctly, you can always find it from the drop down list on the top right section: &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-jetbrains-os-list.231740335aaa.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-jetbrains-os-list.231740335aaa.png&quot; width=&quot;1010&quot; height=&quot;679&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-jetbrains-os-list.231740335aaa.png&amp;amp;w=252&amp;amp;sig=e331b2eb15a3c8b9396327dedc700bd2bcbbc9e3 252w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-jetbrains-os-list.231740335aaa.png&amp;amp;w=505&amp;amp;sig=4a0a0527b968050fb042a0565f5d6970d72ee1f9 505w, https://files.realpython.com/media/pycharm-jetbrains-os-list.231740335aaa.png 1010w&quot; sizes=&quot;75vw&quot; alt=&quot;List of OSes in the JetBrains website&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;After installing, launch the app and accept the user agreement. Under the &lt;em&gt;Tools&lt;/em&gt; tab, you&amp;rsquo;ll see a list of available products. Find PyCharm Community there and click &lt;em&gt;Install&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-toolbox-installed-pycharm.cdcf1b52bc02.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border w-33&quot; src=&quot;https://files.realpython.com/media/pycharm-toolbox-installed-pycharm.cdcf1b52bc02.png&quot; width=&quot;337&quot; height=&quot;537&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-toolbox-installed-pycharm.cdcf1b52bc02.png&amp;amp;w=84&amp;amp;sig=5f1e571c6c7bed958efddaec87d6ac5168713217 84w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-toolbox-installed-pycharm.cdcf1b52bc02.png&amp;amp;w=168&amp;amp;sig=0a76111939657ae01eaa8203b24c0c2e4fff5ee6 168w, https://files.realpython.com/media/pycharm-toolbox-installed-pycharm.cdcf1b52bc02.png 337w&quot; sizes=&quot;75vw&quot; alt=&quot;PyCharm installed with the Toolbox app&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Voilà! You have PyCharm available on your machine. If you don&amp;rsquo;t want to use the Toolbox app, then you can also do a &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/installation-guide.html#standalone&quot;&gt;stand-alone installation of PyCharm&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Launch PyCharm, and you&amp;rsquo;ll see the import settings popup:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-import-settings-popup.4e360260c697.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-50&quot; src=&quot;https://files.realpython.com/media/pycharm-import-settings-popup.4e360260c697.png&quot; width=&quot;416&quot; height=&quot;156&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-import-settings-popup.4e360260c697.png&amp;amp;w=104&amp;amp;sig=4920753cc035f162c505253937453e1aa7cc4d26 104w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-import-settings-popup.4e360260c697.png&amp;amp;w=208&amp;amp;sig=b65b1226bcc172a811d7cd1e00cd600408fba092 208w, https://files.realpython.com/media/pycharm-import-settings-popup.4e360260c697.png 416w&quot; sizes=&quot;75vw&quot; alt=&quot;PyCharm Import Settings Popup&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;PyCharm will automatically detect that this is a fresh install and choose &lt;em&gt;Do not import settings&lt;/em&gt; for you. Click &lt;em&gt;OK&lt;/em&gt;, and PyCharm will ask you to select a keymap scheme. Leave the default and click &lt;em&gt;Next: UI Themes&lt;/em&gt; on the bottom right:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-keymap-scheme.c8115fda9bdd.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/pycharm-keymap-scheme.c8115fda9bdd.png&quot; width=&quot;805&quot; height=&quot;666&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-keymap-scheme.c8115fda9bdd.png&amp;amp;w=201&amp;amp;sig=644595a94c07780a552f76abbfc5fe526b3c9459 201w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-keymap-scheme.c8115fda9bdd.png&amp;amp;w=402&amp;amp;sig=64c348872c1519bc4148e3dbbac2550ed6c0fa30 402w, https://files.realpython.com/media/pycharm-keymap-scheme.c8115fda9bdd.png 805w&quot; sizes=&quot;75vw&quot; alt=&quot;PyCharm Keymap Scheme&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;PyCharm will then ask you to choose a dark theme called Darcula or a light theme. Choose whichever you prefer and click &lt;em&gt;Next: Launcher Script&lt;/em&gt;:  &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-set-ui-theme.c48aac8e3fe0.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-set-ui-theme.c48aac8e3fe0.png&quot; width=&quot;803&quot; height=&quot;666&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-set-ui-theme.c48aac8e3fe0.png&amp;amp;w=200&amp;amp;sig=6998b85afd9e2ca1503624ba55b904f4051f1ffe 200w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-set-ui-theme.c48aac8e3fe0.png&amp;amp;w=401&amp;amp;sig=a6480f0b0073f0680068828fc2f0f5c8ee55cbdb 401w, https://files.realpython.com/media/pycharm-set-ui-theme.c48aac8e3fe0.png 803w&quot; sizes=&quot;75vw&quot; alt=&quot;PyCharm Set UI Theme Page&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;ll be using the dark theme Darcula throughout this tutorial. You can find and install other themes as &lt;a href=&quot;#using-plugins-and-external-tools-in-pycharm&quot;&gt;plugins&lt;/a&gt;, or you can also &lt;a href=&quot;https://blog.codota.com/5-best-intellij-themes/&quot;&gt;import them&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;On the next page, leave the defaults and click &lt;em&gt;Next: Featured plugins&lt;/em&gt;. There, PyCharm will show you a list of plugins you may want to install because most users like to use them. Click &lt;em&gt;Start using PyCharm&lt;/em&gt;, and now you are ready to write some code!&lt;/p&gt;
&lt;h2 id=&quot;writing-code-in-pycharm&quot;&gt;Writing Code in PyCharm&lt;/h2&gt;
&lt;p&gt;In PyCharm, you do everything in the context of a &lt;strong&gt;project&lt;/strong&gt;. Thus, the first thing you need to do is create one.&lt;/p&gt;
&lt;p&gt;After installing and opening PyCharm, you are on the welcome screen. Click &lt;em&gt;Create New Project&lt;/em&gt;, and you&amp;rsquo;ll see the &lt;em&gt;New Project&lt;/em&gt; popup:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-new-project.cc35f3aa1056.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-new-project.cc35f3aa1056.png&quot; width=&quot;664&quot; height=&quot;480&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-new-project.cc35f3aa1056.png&amp;amp;w=166&amp;amp;sig=6423b68127eae8ca93165323df4884844265f5e3 166w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-new-project.cc35f3aa1056.png&amp;amp;w=332&amp;amp;sig=50d660be9a42904b1161bd76df1ab9ddd77e2132 332w, https://files.realpython.com/media/pycharm-new-project.cc35f3aa1056.png 664w&quot; sizes=&quot;75vw&quot; alt=&quot;New Project in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Specify the project location and expand the &lt;em&gt;Project Interpreter&lt;/em&gt; drop down. Here, you have options to create a new project interpreter or reuse an existing one. Choose &lt;em&gt;New environment using&lt;/em&gt;. Right next to it, you have a drop down list to select one of &lt;em&gt;Virtualenv&lt;/em&gt;, &lt;em&gt;Pipenv&lt;/em&gt;, or &lt;em&gt;Conda&lt;/em&gt;, which are the tools that help to keep dependencies required by different projects separate by creating isolated Python environments for them. &lt;/p&gt;
&lt;p&gt;You are free to select whichever you like, but &lt;em&gt;Virtualenv&lt;/em&gt; is used for this tutorial. If you choose to, you can specify the environment location and choose the base interpreter from the list, which is a list of Python interpreters (such as Python2.7 and Python3.6) installed on your system. Usually, the defaults are fine. Then you have to select boxes to inherit global site-packages to your new environment and make it available to all other projects. Leave them unselected.  &lt;/p&gt;
&lt;p&gt;Click &lt;em&gt;Create&lt;/em&gt; on the bottom right and you will see the new project created:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-project-created.99dffd1d4e9a.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-project-created.99dffd1d4e9a.png&quot; width=&quot;1174&quot; height=&quot;734&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-project-created.99dffd1d4e9a.png&amp;amp;w=293&amp;amp;sig=d6394f174acab8ee63eb6ce0360d0174857f7afb 293w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-project-created.99dffd1d4e9a.png&amp;amp;w=587&amp;amp;sig=2fd8ea44cc0ad15f015aab687018ec7ad8861a53 587w, https://files.realpython.com/media/pycharm-project-created.99dffd1d4e9a.png 1174w&quot; sizes=&quot;75vw&quot; alt=&quot;Project created in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You will also see a small &lt;em&gt;Tip of the Day&lt;/em&gt; popup where PyCharm gives you one trick to learn at each startup. Go ahead and close this popup.&lt;/p&gt;
&lt;p&gt;It is now time to start a new Python program. Type &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-n&quot;&gt;N&lt;/kbd&gt;&lt;/span&gt; if you are on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-insert&quot;&gt;Ins&lt;/kbd&gt;&lt;/span&gt; if you are on Windows or Linux. Then, choose &lt;em&gt;Python File&lt;/em&gt;. You can also select &lt;em&gt;File → New&lt;/em&gt; from the menu. Name the new file &lt;code&gt;guess_game.py&lt;/code&gt; and click &lt;em&gt;OK&lt;/em&gt;. You will see a PyCharm window similar to the following:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-new-file.7ea9902d73ea.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-new-file.7ea9902d73ea.png&quot; width=&quot;1172&quot; height=&quot;734&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-new-file.7ea9902d73ea.png&amp;amp;w=293&amp;amp;sig=b1ee432e97d642aea67818cc7280971247196a62 293w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-new-file.7ea9902d73ea.png&amp;amp;w=586&amp;amp;sig=3563b00140a3edd3f1df0e522d5069f6efcb62d3 586w, https://files.realpython.com/media/pycharm-new-file.7ea9902d73ea.png 1172w&quot; sizes=&quot;75vw&quot; alt=&quot;PyCharm New File&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For our test code, let&amp;rsquo;s quickly code up a simple guessing game in which the program chooses a number that the user has to guess. For every guess, the program will tell if the user&amp;rsquo;s guess was smaller or bigger than the secret number. The game ends when the user guesses the number. Here&amp;rsquo;s the code for the game:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;random_int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;user_guess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;What number did we guess (0-100)?&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_guess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;            &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;You found the number (&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{random_int}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;). Congrats!&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_guess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;            &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Your number is less than the number we guessed.&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user_guess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;            &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Your number is more than the number we guessed.&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;            &lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Type this code directly rather than copying and pasting. You&amp;rsquo;ll see something like this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/typing-guess-game.fcaedeb8ece2.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/typing-guess-game.fcaedeb8ece2.gif&quot; width=&quot;528&quot; height=&quot;480&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/typing-guess-game.fcaedeb8ece2.gif&amp;amp;w=132&amp;amp;sig=7e5eb20fb9ae97b1cea80380f9ad00f35dd76707 132w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/typing-guess-game.fcaedeb8ece2.gif&amp;amp;w=264&amp;amp;sig=eb242bca301203a38741a986d7847cf0f3ef4cff 264w, https://files.realpython.com/media/typing-guess-game.fcaedeb8ece2.gif 528w&quot; sizes=&quot;75vw&quot; alt=&quot;Typing Guessing Game&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As you can see, PyCharm provides &lt;a href=&quot;https://www.jetbrains.com/pycharm/features/coding_assistance.html&quot;&gt;Intelligent Coding Assistance&lt;/a&gt; with code completion, code inspections, on-the-fly error highlighting, and quick-fix suggestions. In particular, note how when you typed &lt;code&gt;main&lt;/code&gt; and then hit tab, PyCharm auto-completed the whole &lt;code&gt;main&lt;/code&gt; clause for you. &lt;/p&gt;
&lt;p&gt;Also note how, if you forget to type &lt;code&gt;if&lt;/code&gt; before the condition, append &lt;code&gt;.if&lt;/code&gt;, and then hit &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-tab&quot;&gt;Tab&lt;/kbd&gt;&lt;/span&gt;, PyCharm fixes the &lt;code&gt;if&lt;/code&gt; clause for you. The same is true with &lt;code&gt;True.while&lt;/code&gt;. That&amp;rsquo;s &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/settings-postfix-completion.html&quot;&gt;PyCharm&amp;rsquo;s Postfix completions&lt;/a&gt; working for you to help reduce backward caret jumps.&lt;/p&gt;
&lt;h2 id=&quot;running-code-in-pycharm&quot;&gt;Running Code in PyCharm&lt;/h2&gt;
&lt;p&gt;Now that you&amp;rsquo;ve coded up the game, it&amp;rsquo;s time for you to run it.&lt;/p&gt;
&lt;p&gt;You have three ways of running this program:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the shortcut &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-r&quot;&gt;R&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f10&quot;&gt;F10&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
&lt;li&gt;Right-click the background and choose &lt;em&gt;Run &amp;lsquo;guess_game&amp;rsquo;&lt;/em&gt; from the menu.&lt;/li&gt;
&lt;li&gt;Since this program has the &lt;code&gt;__main__&lt;/code&gt; clause, you can click on the little green arrow to the left of the &lt;code&gt;__main__&lt;/code&gt; clause and choose &lt;em&gt;Run &amp;lsquo;guess_game&amp;rsquo;&lt;/em&gt; from there.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Use any one of the options above to run the program, and you&amp;rsquo;ll see the Run Tool pane appear at the bottom of the window, with your code output showing:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-running-script.33fb830f45b4.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-running-script.33fb830f45b4.gif&quot; width=&quot;1068&quot; height=&quot;720&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-running-script.33fb830f45b4.gif&amp;amp;w=267&amp;amp;sig=44be962297881f8ae66557c19905a55202ee14de 267w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-running-script.33fb830f45b4.gif&amp;amp;w=534&amp;amp;sig=000552999cea7a9ce79eeec2f9db7361e9585d77 534w, https://files.realpython.com/media/pycharm-running-script.33fb830f45b4.gif 1068w&quot; sizes=&quot;75vw&quot; alt=&quot;Running a script in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Play the game for a little bit to see if you can find the number guessed. Pro tip: start with 50.   &lt;/p&gt;
&lt;h2 id=&quot;debugging-in-pycharm&quot;&gt;Debugging in PyCharm&lt;/h2&gt;
&lt;p&gt;Did you find the number? If so, you may have seen something weird after you found the number. Instead of printing the congratulations message and exiting, the program seems to start over. That&amp;rsquo;s a bug right there. To discover why the program starts over, you&amp;rsquo;ll now debug the program.&lt;/p&gt;
&lt;p&gt;First, place a breakpoint by clicking on the blank space to the left of line number 8:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-debug-breakpoint.55cf93c49859.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-debug-breakpoint.55cf93c49859.png&quot; width=&quot;1042&quot; height=&quot;710&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-debug-breakpoint.55cf93c49859.png&amp;amp;w=260&amp;amp;sig=e714eeae34fad6c0e5889bee0f236f9c30e100a0 260w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-debug-breakpoint.55cf93c49859.png&amp;amp;w=521&amp;amp;sig=7c4692a273d49a627785a715fd0a08d4c8120649 521w, https://files.realpython.com/media/pycharm-debug-breakpoint.55cf93c49859.png 1042w&quot; sizes=&quot;75vw&quot; alt=&quot;Debug breakpoint in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This will be the point where the program will be suspended, and you can start exploring what went wrong from there on. Next, choose one of the following three ways to start debugging:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-d&quot;&gt;D&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f9&quot;&gt;F9&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
&lt;li&gt;Right-click the background and choose &lt;em&gt;Debug &amp;lsquo;guess_game&amp;rsquo;&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Click on the little green arrow to the left of the &lt;code&gt;__main__&lt;/code&gt; clause and choose &lt;em&gt;Debug &amp;lsquo;guess_game&lt;/em&gt; from there.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Afterwards, you&amp;rsquo;ll see a &lt;em&gt;Debug&lt;/em&gt; window open at the bottom:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-debugging-start.04246b743469.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-debugging-start.04246b743469.png&quot; width=&quot;1043&quot; height=&quot;711&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-debugging-start.04246b743469.png&amp;amp;w=260&amp;amp;sig=cea78f8df9a7f183330e3610c90a2abeab879923 260w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-debugging-start.04246b743469.png&amp;amp;w=521&amp;amp;sig=6ae0fe4515cf7f0d52cbcda986eb9c52d0a602ee 521w, https://files.realpython.com/media/pycharm-debugging-start.04246b743469.png 1043w&quot; sizes=&quot;75vw&quot; alt=&quot;Start of debugging in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Follow the steps below to debug the program:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Notice that the current line is highlighted in blue.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;See that &lt;code&gt;random_int&lt;/code&gt; and its value are listed in the Debug window. Make a note of this number. (In the picture, the number is 85.)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Hit &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-f8&quot;&gt;F8&lt;/kbd&gt;&lt;/span&gt; to execute the current line and step &lt;em&gt;over&lt;/em&gt; to the next one. You can also use &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-f7&quot;&gt;F7&lt;/kbd&gt;&lt;/span&gt; to step &lt;em&gt;into&lt;/em&gt; the function in the current line, if necessary. As you continue executing the statements, the changes in the variables will be automatically reflected in the Debugger window.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Notice that there is the Console tab right next to the Debugger tab that opened. This Console tab and the Debugger tab are mutually exclusive. In the Console tab, you will be interacting with your program, and in the Debugger tab you will do the debugging actions.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Switch to the Console tab to enter your guess.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Type the number shown, and then hit &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-enter&quot;&gt;Enter&lt;/kbd&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Switch back to the Debugger tab.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Hit &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-f8&quot;&gt;F8&lt;/kbd&gt;&lt;/span&gt; again to evaluate the &lt;code&gt;if&lt;/code&gt; statement. Notice that you are now on line 14. But wait a minute! Why didn&amp;rsquo;t it go to the line 11? The reason is that the &lt;code&gt;if&lt;/code&gt; statement on line 10 evaluated to &lt;code&gt;False&lt;/code&gt;. But why did it evaluate to &lt;code&gt;False&lt;/code&gt; when you entered the number that was chosen?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Look carefully at line 10 and notice that we are comparing &lt;code&gt;user_guess&lt;/code&gt; with the wrong thing. Instead of comparing it with &lt;code&gt;random_int&lt;/code&gt;, we are comparing it with &lt;code&gt;randint&lt;/code&gt;, the function that was imported from the &lt;code&gt;random&lt;/code&gt; package.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Change it to &lt;code&gt;random_int&lt;/code&gt;, restart the debugging, and follow the same steps again. You will see that, this time, it will go to line 11, and line 10 will evaluate to &lt;code&gt;True&lt;/code&gt;:&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-debugging-scripts.bb5a077da438.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-debugging-scripts.bb5a077da438.gif&quot; width=&quot;1092&quot; height=&quot;720&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-debugging-scripts.bb5a077da438.gif&amp;amp;w=273&amp;amp;sig=fc5de269fbc13ea5c1d8be4ca7f525e04a4bb68c 273w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-debugging-scripts.bb5a077da438.gif&amp;amp;w=546&amp;amp;sig=4719f69fd6e78ed3e68f16a9355d00d0edd85a26 546w, https://files.realpython.com/media/pycharm-debugging-scripts.bb5a077da438.gif 1092w&quot; sizes=&quot;75vw&quot; alt=&quot;Debugging Script in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Congratulations! You fixed the bug.&lt;/p&gt;
&lt;h2 id=&quot;testing-in-pycharm&quot;&gt;Testing in PyCharm&lt;/h2&gt;
&lt;p&gt;No application is reliable without unit tests. PyCharm helps you write and run them very quickly and comfortably. By default, &lt;a href=&quot;https://docs.python.org/3/library/unittest.html&quot;&gt;&lt;code&gt;unittest&lt;/code&gt;&lt;/a&gt; is used as the test runner, but PyCharm also supports other testing frameworks such as &lt;a href=&quot;http://www.pytest.org/en/latest/&quot;&gt;&lt;code&gt;pytest&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://nose.readthedocs.io/en/latest/&quot;&gt;&lt;code&gt;nose&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://docs.python.org/3/library/doctest.html&quot;&gt;&lt;code&gt;doctest&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/tox-support.html&quot;&gt;&lt;code&gt;tox&lt;/code&gt;&lt;/a&gt;, and &lt;a href=&quot;https://twistedmatrix.com/trac/wiki/TwistedTrial&quot;&gt;&lt;code&gt;trial&lt;/code&gt;&lt;/a&gt;. You can, for example, enable &lt;code&gt;pytest&lt;/code&gt; for your project like this:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Open the &lt;em&gt;Settings/Preferences → Tools → Python Integrated Tools&lt;/em&gt; settings dialog.&lt;/li&gt;
&lt;li&gt;Select &lt;code&gt;pytest&lt;/code&gt; in the Default test runner field.&lt;/li&gt;
&lt;li&gt;Click &lt;em&gt;OK&lt;/em&gt; to save the settings. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For this example, we&amp;rsquo;ll be using the default test runner &lt;code&gt;unittest&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;In the same project, create a file called &lt;code&gt;calculator.py&lt;/code&gt; and put the following &lt;code&gt;Calculator&lt;/code&gt; class in it:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Calculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;PyCharm makes it very easy to create tests for your existing code. With the &lt;code&gt;calculator.py&lt;/code&gt; file open, execute any one of the following that you like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-t&quot;&gt;T&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-t&quot;&gt;T&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
&lt;li&gt;Right-click in the background of the class and then choose &lt;em&gt;Go To&lt;/em&gt; and &lt;em&gt;Test&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;On the main menu, choose &lt;em&gt;Navigate → Test&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Choose &lt;em&gt;Create New Test&amp;hellip;&lt;/em&gt;, and you will see the following window:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-create-tests.9a6cea78f9c6.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-create-tests.9a6cea78f9c6.png&quot; width=&quot;500&quot; height=&quot;402&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-create-tests.9a6cea78f9c6.png&amp;amp;w=125&amp;amp;sig=0c50b83f35578fd8004dce9e7d55fcd3b09a1967 125w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-create-tests.9a6cea78f9c6.png&amp;amp;w=250&amp;amp;sig=f6740142e71d2d2f6196ffbe341ae09bc9a64453 250w, https://files.realpython.com/media/pycharm-create-tests.9a6cea78f9c6.png 500w&quot; sizes=&quot;75vw&quot; alt=&quot;Create tests in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Leave the defaults of &lt;em&gt;Target directory&lt;/em&gt;, &lt;em&gt;Test file name&lt;/em&gt;, and &lt;em&gt;Test class name&lt;/em&gt;. Select both of the methods and click &lt;em&gt;OK&lt;/em&gt;. Voila! PyCharm automatically created a file called &lt;code&gt;test_calculator.py&lt;/code&gt; and created the following stub tests for you in it:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;unittest&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TestCase&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TestCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TestCase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Run the tests using one of the methods below:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-r&quot;&gt;R&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f10&quot;&gt;F10&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
&lt;li&gt;Right-click the background and choose &lt;em&gt;Run &amp;lsquo;Unittests for test_calculator.py&amp;rsquo;&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Click on the little green arrow to the left of the test class name and choose &lt;em&gt;Run &amp;lsquo;Unittests for test_calculator.py&amp;rsquo;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You&amp;rsquo;ll see the tests window open on the bottom with all the tests failing:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-failed-tests.810aa9c365cb.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-failed-tests.810aa9c365cb.png&quot; width=&quot;972&quot; height=&quot;645&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-failed-tests.810aa9c365cb.png&amp;amp;w=243&amp;amp;sig=cb7ef285c20ed83b9771a91cc38d77342e4d3745 243w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-failed-tests.810aa9c365cb.png&amp;amp;w=486&amp;amp;sig=aaec55471da2d168048783b9c4e573f5ce876de4 486w, https://files.realpython.com/media/pycharm-failed-tests.810aa9c365cb.png 972w&quot; sizes=&quot;75vw&quot; alt=&quot;Failed tests in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Notice that you have the hierarchy of the test results on the left and the output of the terminal on the right. &lt;/p&gt;
&lt;p&gt;Now, implement &lt;code&gt;test_add&lt;/code&gt; by changing the code to the following:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;unittest&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TestCase&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;calculator&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Calculator&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TestCalculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TestCase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Calculator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assertEqual&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;calculator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Run the tests again, and you&amp;rsquo;ll see that one test passed and the other failed. Explore the options to show passed tests, to show ignored tests, to sort tests alphabetically, and to sort tests by duration:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-running-tests.6077562207ba.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-running-tests.6077562207ba.gif&quot; width=&quot;1092&quot; height=&quot;720&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-running-tests.6077562207ba.gif&amp;amp;w=273&amp;amp;sig=e2238425e1cfb9a9a298741244f3021f3984dbf8 273w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-running-tests.6077562207ba.gif&amp;amp;w=546&amp;amp;sig=85203afd5ff2189fdfadad0960da514b03063953 546w, https://files.realpython.com/media/pycharm-running-tests.6077562207ba.gif 1092w&quot; sizes=&quot;75vw&quot; alt=&quot;Running tests in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Note that the &lt;code&gt;sleep(0.1)&lt;/code&gt; method that you see in the GIF above is intentionally used to make one of the tests slower so that sorting by duration works. &lt;/p&gt;
&lt;h2 id=&quot;editing-an-existing-project-in-pycharm&quot;&gt;Editing an Existing Project in PyCharm&lt;/h2&gt;
&lt;p&gt;These single file projects are great for examples, but you&amp;rsquo;ll often work on much larger projects over a longer period of time. In this section, you&amp;rsquo;ll take a look at how PyCharm works with a larger project. &lt;/p&gt;
&lt;p&gt;To explore the project-focused features of PyCharm, you&amp;rsquo;ll use the Alcazar web framework that was built for learning purposes. To continue following along, clone the repo locally:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Clone Repo:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/alcazar-web-framework/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-alcazar-web-framework&quot; data-focus=&quot;false&quot;&gt;Click here to clone the repo you&#39;ll use&lt;/a&gt; to explore the project-focused features of PyCharm in this tutorial.&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;Once you have a project locally, open it in PyCharm using one of the following methods:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Click &lt;em&gt;File → Open&lt;/em&gt; on the main menu.&lt;/li&gt;
&lt;li&gt;Click &lt;em&gt;Open&lt;/em&gt; on the &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/welcome-screen.html&quot;&gt;Welcome Screen&lt;/a&gt; if you are there.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;After either of these steps, find the folder containing the project on your computer and open it.&lt;/p&gt;
&lt;p&gt;If this project contains a &lt;a href=&quot;https://realpython.com/python-virtual-environments-a-primer/&quot;&gt;virtual environment&lt;/a&gt;, then PyCharm will automatically use this virtual environment and make it the project interpreter.&lt;/p&gt;
&lt;p&gt;If you need to configure a different &lt;code&gt;virtualenv&lt;/code&gt;, then open &lt;em&gt;Preferences&lt;/em&gt; on Mac by pressing &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-comma&quot;&gt;,&lt;/kbd&gt;&lt;/span&gt; or &lt;em&gt;Settings&lt;/em&gt; on Windows or Linux by pressing &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-s&quot;&gt;S&lt;/kbd&gt;&lt;/span&gt; and find the &lt;em&gt;Project: ProjectName&lt;/em&gt; section. Open the drop-down and choose &lt;em&gt;Project Interpreter&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-project-interpreter.57282306555a.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-project-interpreter.57282306555a.png&quot; width=&quot;1083&quot; height=&quot;723&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-project-interpreter.57282306555a.png&amp;amp;w=270&amp;amp;sig=286643bc473f648bbcce27338c980eb023746ac2 270w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-project-interpreter.57282306555a.png&amp;amp;w=541&amp;amp;sig=6d33c5adf0380e8c5b01ae9430436983580b4b49 541w, https://files.realpython.com/media/pycharm-project-interpreter.57282306555a.png 1083w&quot; sizes=&quot;75vw&quot; alt=&quot;Project interpreter in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Choose the &lt;code&gt;virtualenv&lt;/code&gt; from the drop-down list. If it&amp;rsquo;s not there, then click on the settings button to the right of the drop-down list and then choose &lt;em&gt;Add&amp;hellip;&lt;/em&gt;. The rest of the steps should be the same as when we were &lt;a href=&quot;#writing-code-in-pycharm&quot;&gt;creating a new project&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;searching-and-navigating-in-pycharm&quot;&gt;Searching and Navigating in PyCharm&lt;/h2&gt;
&lt;p&gt;In a big project where it&amp;rsquo;s difficult for a single person to remember where everything is located, it&amp;rsquo;s very important to be able to quickly navigate and find what you looking for. PyCharm has you covered here as well. Use the project you opened in the section above to practice these shortcuts: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Searching for a fragment in the current file:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f&quot;&gt;F&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f&quot;&gt;F&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Searching for a fragment in the entire project:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f&quot;&gt;F&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f&quot;&gt;F&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Searching for a class:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-o&quot;&gt;O&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-n&quot;&gt;N&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Searching for a file:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-o&quot;&gt;O&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-n&quot;&gt;N&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Searching all if you don&amp;rsquo;t know whether it&amp;rsquo;s a file, class, or a code fragment that you are looking for:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;/span&gt; twice.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As for the navigation, the following shortcuts may save you a lot of time:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Going to the declaration of a variable:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux, and click on the variable.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Finding usages of a class, a method, or any symbol:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-f7&quot;&gt;F7&lt;/kbd&gt;&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Seeing your recent changes:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-shift&quot;&gt;Shift&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-c&quot;&gt;C&lt;/kbd&gt;&lt;/span&gt; or go to &lt;em&gt;View → Recent Changes&lt;/em&gt; on the main menu.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Seeing your recent files:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-e&quot;&gt;E&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-e&quot;&gt;E&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux, or go to &lt;em&gt;View → Recent Files&lt;/em&gt; on the main menu.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Going backward and forward through your history of navigation after you jumped around:&lt;/strong&gt; Press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-bracket-left&quot;&gt;[&lt;/kbd&gt;&lt;/span&gt; / &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-bracket-right&quot;&gt;]&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-arrow-left&quot;&gt;Left&lt;/kbd&gt;&lt;/span&gt; / &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-arrow-right&quot;&gt;Right&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For more details, see the &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/tutorial-exploring-navigation-and-search.html&quot;&gt;official documentation&lt;/a&gt;. &lt;/p&gt;
&lt;h2 id=&quot;using-version-control-in-pycharm&quot;&gt;Using Version Control in PyCharm&lt;/h2&gt;
&lt;p&gt;Version control systems such as &lt;a href=&quot;https://git-scm.com/&quot;&gt;Git&lt;/a&gt; and &lt;a href=&quot;https://www.mercurial-scm.org/&quot;&gt;Mercurial&lt;/a&gt; are some of the most important tools in the modern software development world. So, it is essential for an IDE to support them. PyCharm does that very well by integrating with a lot of popular VC systems such as Git (and &lt;a href=&quot;https://github.com/&quot;&gt;Github&lt;/a&gt;), Mercurial, &lt;a href=&quot;https://www.perforce.com/solutions/version-control&quot;&gt;Perforce&lt;/a&gt; and, &lt;a href=&quot;https://subversion.apache.org/&quot;&gt;Subversion&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: &lt;a href=&quot;https://realpython.com/python-git-github-intro/&quot;&gt;Git&lt;/a&gt; is used for the following examples.&lt;/p&gt;
&lt;/div&gt;
&lt;h3 id=&quot;configuring-vcs&quot;&gt;Configuring VCS&lt;/h3&gt;
&lt;p&gt;To enable VCS integration. Go to &lt;em&gt;VCS → VCS Operations Popup&amp;hellip;&lt;/em&gt; from the menu on the top or press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-v&quot;&gt;V&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-grave&quot;&gt;`&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux. Choose &lt;em&gt;Enable Version Control Integration&amp;hellip;&lt;/em&gt;. You&amp;rsquo;ll see the following window open:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-enable-vc-integration.b30ec94c1246.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-enable-vc-integration.b30ec94c1246.png&quot; width=&quot;715&quot; height=&quot;147&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-enable-vc-integration.b30ec94c1246.png&amp;amp;w=178&amp;amp;sig=7ef55e4ed6068c86d831adaefc1af11f4c083763 178w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-enable-vc-integration.b30ec94c1246.png&amp;amp;w=357&amp;amp;sig=c701d809b49e9837477c14e8dfe34dc4cfa66c33 357w, https://files.realpython.com/media/pycharm-enable-vc-integration.b30ec94c1246.png 715w&quot; sizes=&quot;75vw&quot; alt=&quot;Enable Version Control Integration in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Choose &lt;em&gt;Git&lt;/em&gt; from the drop down list, click &lt;em&gt;OK&lt;/em&gt;, and you have VCS enabled for your project. Note that if you opened an existing project that has version control enabled, then PyCharm will see that and automatically enable it.&lt;/p&gt;
&lt;p&gt;Now, if you go to the &lt;em&gt;VCS Operations Popup&amp;hellip;&lt;/em&gt;, you&amp;rsquo;ll see a different popup with the options to do &lt;code&gt;git add&lt;/code&gt;, &lt;code&gt;git stash&lt;/code&gt;, &lt;code&gt;git branch&lt;/code&gt;, &lt;code&gt;git commit&lt;/code&gt;, &lt;code&gt;git push&lt;/code&gt; and more:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-vcs-operations.70dbafcb983a.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border w-50&quot; src=&quot;https://files.realpython.com/media/pycharm-vcs-operations.70dbafcb983a.png&quot; width=&quot;392&quot; height=&quot;379&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-vcs-operations.70dbafcb983a.png&amp;amp;w=98&amp;amp;sig=f285b015c957936448441c4ec8b03cf8627cdffc 98w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-vcs-operations.70dbafcb983a.png&amp;amp;w=196&amp;amp;sig=a16c449a21dde2a2b0f2f7533c92e71574cd3d60 196w, https://files.realpython.com/media/pycharm-vcs-operations.70dbafcb983a.png 392w&quot; sizes=&quot;75vw&quot; alt=&quot;VCS operations in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you can&amp;rsquo;t find what you need, you can most probably find it by going to &lt;em&gt;VCS&lt;/em&gt; from the top menu and choosing &lt;em&gt;Git&lt;/em&gt;, where you can even create and view pull requests.&lt;/p&gt;
&lt;h3 id=&quot;committing-and-conflict-resolution&quot;&gt;Committing and Conflict Resolution&lt;/h3&gt;
&lt;p&gt;These are two features of VCS integration in PyCharm that I personally use and enjoy a lot! Let&amp;rsquo;s say you have finished your work and want to commit it. Go to &lt;em&gt;VCS → VCS Operations Popup&amp;hellip; → Commit&amp;hellip;&lt;/em&gt; or press &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-command&quot;&gt;Cmd&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-k&quot;&gt;K&lt;/kbd&gt;&lt;/span&gt; on Mac or &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-k&quot;&gt;K&lt;/kbd&gt;&lt;/span&gt; on Windows or Linux. You&amp;rsquo;ll see the following window open:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-commit-window.a4ceff16c2d3.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-commit-window.a4ceff16c2d3.png&quot; width=&quot;929&quot; height=&quot;682&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-commit-window.a4ceff16c2d3.png&amp;amp;w=232&amp;amp;sig=935dabf7a28cf757a5c87165e3da494540c3e4a6 232w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-commit-window.a4ceff16c2d3.png&amp;amp;w=464&amp;amp;sig=7a05fe8a5cbfb1c3524c24515f9a8d7fa3211591 464w, https://files.realpython.com/media/pycharm-commit-window.a4ceff16c2d3.png 929w&quot; sizes=&quot;75vw&quot; alt=&quot;Commit window in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In this window, you can do the following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Choose which files to commit&lt;/li&gt;
&lt;li&gt;Write your commit message&lt;/li&gt;
&lt;li&gt;Do all kinds of checks and cleanup &lt;a href=&quot;https://www.jetbrains.com/help/idea/commit-changes-dialog.html#before_commit&quot;&gt;before commit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;See the difference of changes&lt;/li&gt;
&lt;li&gt;Commit and push at once by pressing the arrow to the right of the &lt;em&gt;Commit&lt;/em&gt; button on the right bottom and choosing &lt;em&gt;Commit and Push&amp;hellip;&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;It can feel magical and fast, especially if you&amp;rsquo;re used to doing everything manually on the command line.&lt;/p&gt;
&lt;p&gt;When you work in a team, &lt;strong&gt;merge conflicts&lt;/strong&gt; do happen. When somebody commits changes to a file that you&amp;rsquo;re working on, but their changes overlap with yours because both of you changed the same lines, then VCS will not be able to figure out if it should choose your changes or those of your teammate. So you&amp;rsquo;ll get these unfortunate arrows and symbols:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-conflicts.74b23b9ec798.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-conflicts.74b23b9ec798.png&quot; width=&quot;996&quot; height=&quot;691&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-conflicts.74b23b9ec798.png&amp;amp;w=249&amp;amp;sig=d02221be0ce12dbc9a8ea7514e047bb608b16c08 249w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-conflicts.74b23b9ec798.png&amp;amp;w=498&amp;amp;sig=1286c7aa0f62795e01c6a2f0607096b8c0d67b20 498w, https://files.realpython.com/media/pycharm-conflicts.74b23b9ec798.png 996w&quot; sizes=&quot;75vw&quot; alt=&quot;Conflicts in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This looks strange, and it&amp;rsquo;s difficult to figure out which changes should be deleted and which ones should stay. PyCharm to the rescue! It has a much nicer and cleaner way of resolving conflicts. Go to &lt;em&gt;VCS&lt;/em&gt; in the top menu, choose &lt;em&gt;Git&lt;/em&gt; and then &lt;em&gt;Resolve conflicts&amp;hellip;&lt;/em&gt;. Choose the file whose conflicts you want to resolve and click on &lt;em&gt;Merge&lt;/em&gt;. You will see the following window open:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-conflict-resolving-window.eea8f79a12b2.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-conflict-resolving-window.eea8f79a12b2.png&quot; width=&quot;1174&quot; height=&quot;709&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-conflict-resolving-window.eea8f79a12b2.png&amp;amp;w=293&amp;amp;sig=ef195e8acbb5ec9fa55fca46a43486995c2efca7 293w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-conflict-resolving-window.eea8f79a12b2.png&amp;amp;w=587&amp;amp;sig=067731ba579736c92c747eb128b3fb02b0d68417 587w, https://files.realpython.com/media/pycharm-conflict-resolving-window.eea8f79a12b2.png 1174w&quot; sizes=&quot;75vw&quot; alt=&quot;Conflict resolving windown in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;On the left column, you will see your changes. On the right one, the changes made by your teammate. Finally, in the middle column, you will see the result. The conflicting lines are highlighted, and you can see a little &lt;em&gt;X&lt;/em&gt; and &lt;em&gt;&amp;gt;&amp;gt;&lt;/em&gt;/&lt;em&gt;&amp;lt;&amp;lt;&lt;/em&gt; right beside those lines. Press the arrows to accept the changes and the &lt;em&gt;X&lt;/em&gt; to decline. After you resolve all those conflicts, click the &lt;em&gt;Apply&lt;/em&gt; button: &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-resolving-conflicts.d3128ce78c45.gif&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-resolving-conflicts.d3128ce78c45.gif&quot; width=&quot;1200&quot; height=&quot;720&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-resolving-conflicts.d3128ce78c45.gif&amp;amp;w=300&amp;amp;sig=099dcca659431f9d2a1315b1fea5d7cbe246425c 300w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-resolving-conflicts.d3128ce78c45.gif&amp;amp;w=600&amp;amp;sig=6b98751300d8750c93d80cf7b65e96fb57d81714 600w, https://files.realpython.com/media/pycharm-resolving-conflicts.d3128ce78c45.gif 1200w&quot; sizes=&quot;75vw&quot; alt=&quot;Resolving Conflicts in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the GIF above, for the first conflicting line, the author declined his own changes and accepted those of his teammates. Conversely, the author accepted his own changes and declined his teammates&amp;rsquo; for the second conflicting line.&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s a lot more that you can do with the VCS integration in PyCharm. For more details, see &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/version-control-integration.html&quot;&gt;this documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;using-plugins-and-external-tools-in-pycharm&quot;&gt;Using Plugins and External Tools in PyCharm&lt;/h2&gt;
&lt;p&gt;You can find almost everything you need for development in PyCharm. If you can&amp;rsquo;t, there is most probably a &lt;a href=&quot;https://plugins.jetbrains.com/&quot;&gt;plugin&lt;/a&gt; that adds that functionality you need to PyCharm. For example, they can:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Add support for various languages and frameworks &lt;/li&gt;
&lt;li&gt;Boost your productivity with shortcut hints, file watchers, and so on &lt;/li&gt;
&lt;li&gt;Help you learn a new programming language with coding exercises&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For instance, &lt;a href=&quot;https://plugins.jetbrains.com/plugin/164-ideavim&quot;&gt;IdeaVim&lt;/a&gt; adds Vim emulation to PyCharm. If you like Vim, this can be a pretty good combination. &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://plugins.jetbrains.com/plugin/8006-material-theme-ui&quot;&gt;Material Theme UI&lt;/a&gt; changes the appearance of PyCharm to a Material Design look and feel: &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-material-theme.178175815adc.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/pycharm-material-theme.178175815adc.png&quot; width=&quot;1110&quot; height=&quot;743&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-material-theme.178175815adc.png&amp;amp;w=277&amp;amp;sig=60ec4c8b5f6a89af345a230518e21ee8a33d174b 277w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-material-theme.178175815adc.png&amp;amp;w=555&amp;amp;sig=fdac8603145ed77eb443abcbbe8ebdb9811bdff4 555w, https://files.realpython.com/media/pycharm-material-theme.178175815adc.png 1110w&quot; sizes=&quot;75vw&quot; alt=&quot;Material Theme in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://plugins.jetbrains.com/plugin/9442-vue-js&quot;&gt;Vue.js&lt;/a&gt; adds support for &lt;a href=&quot;https://vuejs.org/&quot;&gt;Vue.js&lt;/a&gt; projects. &lt;a href=&quot;https://plugins.jetbrains.com/plugin/7793-markdown&quot;&gt;Markdown&lt;/a&gt; provides the capability to edit Markdown files within the IDE and see the rendered HTML in a live preview. You can find and install all of the available plugins by going to the &lt;em&gt;Preferences → Plugins&lt;/em&gt; on Mac or &lt;em&gt;Settings → Plugins&lt;/em&gt; on Windows or Linux, under the &lt;em&gt;Marketplace&lt;/em&gt; tab:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-plugin-marketplace.7d1cecfdc8b3.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-plugin-marketplace.7d1cecfdc8b3.png&quot; width=&quot;1047&quot; height=&quot;687&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-plugin-marketplace.7d1cecfdc8b3.png&amp;amp;w=261&amp;amp;sig=8d4a9ba35b5eb27b5604f86108b79f28e40f3cc9 261w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-plugin-marketplace.7d1cecfdc8b3.png&amp;amp;w=523&amp;amp;sig=ec5e20ae96bf9ee37a1011bcc2203e7d1599b4a4 523w, https://files.realpython.com/media/pycharm-plugin-marketplace.7d1cecfdc8b3.png 1047w&quot; sizes=&quot;75vw&quot; alt=&quot;Plugin Marketplace in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you can&amp;rsquo;t find what you need, you can even &lt;a href=&quot;http://www.jetbrains.org/intellij/sdk/docs/basics.html&quot;&gt;develop your own plugin&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If you can&amp;rsquo;t find the right plugin and don&amp;rsquo;t want to develop your own because there&amp;rsquo;s already a package in PyPI, then you can add it to PyCharm as an external tool. Take &lt;a href=&quot;http://flake8.pycqa.org/en/latest/&quot;&gt;&lt;code&gt;Flake8&lt;/code&gt;&lt;/a&gt;, the code analyzer, as an example. &lt;/p&gt;
&lt;p&gt;First, install &lt;code&gt;flake8&lt;/code&gt; in your virtualenv with &lt;code&gt;pip install flake8&lt;/code&gt; in the Terminal app of your choice. You can also use the one integrated into PyCharm:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-terminal.bb20cae6697e.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/pycharm-terminal.bb20cae6697e.png&quot; width=&quot;972&quot; height=&quot;646&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-terminal.bb20cae6697e.png&amp;amp;w=243&amp;amp;sig=860217f31e60a4bb574e169ee05b6788cacaa388 243w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-terminal.bb20cae6697e.png&amp;amp;w=486&amp;amp;sig=26a30f328e42cc19e7e652c44767093480dbf352 486w, https://files.realpython.com/media/pycharm-terminal.bb20cae6697e.png 972w&quot; sizes=&quot;75vw&quot; alt=&quot;Terminal in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Then, go to &lt;em&gt;Preferences → Tools&lt;/em&gt; on Mac or &lt;em&gt;Settings → Tools&lt;/em&gt; on Windows/Linux, and then choose &lt;em&gt;External Tools&lt;/em&gt;. Then click on the little &lt;em&gt;+&lt;/em&gt; button at the bottom (1). In the new popup window, insert the details as shown below and click &lt;em&gt;OK&lt;/em&gt; for both windows:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-flake8-tool.3963506224b4.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/pycharm-flake8-tool.3963506224b4.png&quot; width=&quot;1082&quot; height=&quot;720&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-flake8-tool.3963506224b4.png&amp;amp;w=270&amp;amp;sig=152f2ccf0a75a950b6a5cd6b5087507b66288595 270w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-flake8-tool.3963506224b4.png&amp;amp;w=541&amp;amp;sig=ee7221ee226076dfeaedfd5d2756263e351d8d14 541w, https://files.realpython.com/media/pycharm-flake8-tool.3963506224b4.png 1082w&quot; sizes=&quot;75vw&quot; alt=&quot;Flake8 tool in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here, &lt;em&gt;Program&lt;/em&gt; (2) refers to the Flake8 executable that can be found in the folder &lt;em&gt;/bin&lt;/em&gt; of your virtual environment. &lt;em&gt;Arguments&lt;/em&gt; (3) refers to which file you want to analyze with the help of Flake8. &lt;em&gt;Working directory&lt;/em&gt; is the directory of your project.&lt;/p&gt;
&lt;p&gt;You could hardcode the absolute paths for everything here, but that would mean that you couldn&amp;rsquo;t use this external tool in other projects. You would be able to use it only inside one project for one file. &lt;/p&gt;
&lt;p&gt;So you need to use something called &lt;em&gt;Macros&lt;/em&gt;. Macros are basically variables in the format of &lt;code&gt;$name$&lt;/code&gt; that change according to your context. For example, &lt;code&gt;$FileName$&lt;/code&gt; is &lt;code&gt;first.py&lt;/code&gt; when you&amp;rsquo;re editing &lt;code&gt;first.py&lt;/code&gt;, and it is &lt;code&gt;second.py&lt;/code&gt; when you&amp;rsquo;re editing &lt;code&gt;second.py&lt;/code&gt;. You can see their list and insert any of them by clicking on the &lt;em&gt;Insert Macro&amp;hellip;&lt;/em&gt; buttons. Because you used macros here, the values will change according to the project you&amp;rsquo;re currently working on, and Flake8 will continue to do its job properly.   &lt;/p&gt;
&lt;p&gt;In order to use it, create a file &lt;code&gt;example.py&lt;/code&gt; and put the following code in it:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CONSTANT_VAR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It deliberately breaks some of the Flake8 rules. Right-click the background of this file. Choose &lt;em&gt;External Tools&lt;/em&gt; and then &lt;em&gt;Flake8&lt;/em&gt;. Voilà! The output of the Flake8 analysis will appear at the bottom: &lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-flake8-output.5b78e911e6d3.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/pycharm-flake8-output.5b78e911e6d3.png&quot; width=&quot;997&quot; height=&quot;634&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-flake8-output.5b78e911e6d3.png&amp;amp;w=249&amp;amp;sig=8fecbaaf9d4e2daa2bbe443be4b6dee2634f2a46 249w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-flake8-output.5b78e911e6d3.png&amp;amp;w=498&amp;amp;sig=4e41c6dbac28fbe23c18716f49c3cadf63767500 498w, https://files.realpython.com/media/pycharm-flake8-output.5b78e911e6d3.png 997w&quot; sizes=&quot;75vw&quot; alt=&quot;Flake8 Output in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In order to make it even better, you can add a shortcut for it. Go to &lt;em&gt;Preferences&lt;/em&gt; on Mac or to &lt;em&gt;Settings&lt;/em&gt; on Windows or Linux. Then, go to &lt;em&gt;Keymap → External Tools → External Tools&lt;/em&gt;. Double-click &lt;em&gt;Flake8&lt;/em&gt; and choose &lt;em&gt;Add Keyboard Shortcut&lt;/em&gt;. You&amp;rsquo;ll see this window:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/pycharm-add-shortcut.8c66b2bd12c0.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/pycharm-add-shortcut.8c66b2bd12c0.png&quot; width=&quot;1084&quot; height=&quot;724&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-add-shortcut.8c66b2bd12c0.png&amp;amp;w=271&amp;amp;sig=e95cc32634af125588e6881ec6992dace79ec667 271w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/pycharm-add-shortcut.8c66b2bd12c0.png&amp;amp;w=542&amp;amp;sig=836cbeec5256df8e8048aa6a0c3e7d89b2bac444 542w, https://files.realpython.com/media/pycharm-add-shortcut.8c66b2bd12c0.png 1084w&quot; sizes=&quot;75vw&quot; alt=&quot;Add shortcut in PyCharm&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the image above, the shortcut is &lt;span class=&quot;keys&quot;&gt;&lt;kbd class=&quot;key-control&quot;&gt;Ctrl&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-alt&quot;&gt;Alt&lt;/kbd&gt;&lt;span&gt;+&lt;/span&gt;&lt;kbd class=&quot;key-a&quot;&gt;A&lt;/kbd&gt;&lt;/span&gt; for this tool. Add your preferred shortcut in the textbox and click &lt;em&gt;OK&lt;/em&gt; for both windows. Now you can now use that shortcut to analyze the file you&amp;rsquo;re currently working on with Flake8.&lt;/p&gt;
&lt;h2 id=&quot;pycharm-professional-features&quot;&gt;PyCharm Professional Features&lt;/h2&gt;
&lt;p&gt;PyCharm Professional is a paid version of PyCharm with more out-of-the-box features and integrations. In this section, you&amp;rsquo;ll mainly be presented with overviews of its main features and links to the official documentation, where each feature is discussed in detail. Remember that none of the following features is available in the Community edition. &lt;/p&gt;
&lt;h3 id=&quot;django-support&quot;&gt;Django Support&lt;/h3&gt;
&lt;p&gt;PyCharm has extensive support for &lt;a href=&quot;https://www.djangoproject.com/&quot;&gt;Django&lt;/a&gt;, one of the most popular and beloved &lt;a href=&quot;https://realpython.com/learning-paths/become-python-web-developer/&quot;&gt;Python web frameworks&lt;/a&gt;. To make sure that it&amp;rsquo;s enabled, do the following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Open &lt;em&gt;Preferences&lt;/em&gt; on Mac or &lt;em&gt;Settings&lt;/em&gt; on Windows or Linux.&lt;/li&gt;
&lt;li&gt;Choose &lt;em&gt;Languages and Frameworks&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Choose &lt;em&gt;Django&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Check the checkbox &lt;em&gt;Enable Django support&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Apply changes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now that you&amp;rsquo;ve enabled Django support, your Django development journey will be a lot easier in PyCharm:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When creating a project, you&amp;rsquo;ll have a dedicated Django project type. This means that, when you choose this type, you&amp;rsquo;ll have all the necessary files and settings. This is the equivalent of using &lt;code&gt;django-admin startproject mysite&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You can run &lt;code&gt;manage.py&lt;/code&gt; commands directly inside PyCharm. &lt;/li&gt;
&lt;li&gt;Django templates are supported, including:&lt;ul&gt;
&lt;li&gt;Syntax and error highlighting&lt;/li&gt;
&lt;li&gt;Code completion&lt;/li&gt;
&lt;li&gt;Navigation&lt;/li&gt;
&lt;li&gt;Completion for block names&lt;/li&gt;
&lt;li&gt;Completion for custom tags and filters&lt;/li&gt;
&lt;li&gt;Quick documentation for tags and filters&lt;/li&gt;
&lt;li&gt;Capability to debug them&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Code completion in all other Django parts such as views, URLs and models, and code insight support for Django ORM.&lt;/li&gt;
&lt;li&gt;Model dependency diagrams for Django models.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For more details on Django support, see the &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/django-support7.html&quot;&gt;official documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;database-support&quot;&gt;Database Support&lt;/h3&gt;
&lt;p&gt;Modern database development is a complex task with many supporting systems and workflows. That&amp;rsquo;s why JetBrains, the company behind PyCharm, developed a standalone IDE called &lt;a href=&quot;https://www.jetbrains.com/datagrip/&quot;&gt;DataGrip&lt;/a&gt; for that. It&amp;rsquo;s a separate product from PyCharm with a separate license. &lt;/p&gt;
&lt;p&gt;Luckily, PyCharm supports all the features that are available in DataGrip through a plugin called &lt;em&gt;Database tools and SQL&lt;/em&gt;, which is enabled by default. With the help of it, you can query, create and manage databases whether they&amp;rsquo;re working locally, on a server, or in the cloud. The plugin supports MySQL, PostgreSQL, Microsoft SQL Server, SQLite, MariaDB, Oracle, Apache Cassandra, and others. For more information on what you can do with this plugin, check out &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/relational-databases.html&quot;&gt;the comprehensive documentation on the database support&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;thread-concurrency-visualization&quot;&gt;Thread Concurrency Visualization&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://channels.readthedocs.io/en/latest/&quot;&gt;&lt;code&gt;Django Channels&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/async-io-python/&quot;&gt;&lt;code&gt;asyncio&lt;/code&gt;&lt;/a&gt;, and the recent frameworks like &lt;a href=&quot;https://www.starlette.io/&quot;&gt;&lt;code&gt;Starlette&lt;/code&gt;&lt;/a&gt; are examples of a growing trend in asynchronous Python programming. While it&amp;rsquo;s true that asynchronous programs do bring a lot of benefits to the table, it&amp;rsquo;s also notoriously hard to write and debug them. In such cases, &lt;em&gt;Thread Concurrency Visualization&lt;/em&gt; can be just what the doctor ordered because it helps you take full control over your multi-threaded applications and optimize them.&lt;/p&gt;
&lt;p&gt;Check out &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/thread-concurrency-visualization.html&quot;&gt;the comprehensive documentation of this feature&lt;/a&gt; for more details.&lt;/p&gt;
&lt;h3 id=&quot;profiler&quot;&gt;Profiler&lt;/h3&gt;
&lt;p&gt;Speaking of optimization, profiling is another technique that you can use to optimize your code. With its help, you can see which parts of your code are taking most of the execution time. A profiler runs in the following order of priority: &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://vmprof.readthedocs.io/en/latest/&quot;&gt;&lt;code&gt;vmprof&lt;/code&gt;&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/sumerc/yappi&quot;&gt;&lt;code&gt;yappi&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.python.org/3/library/profile.html&quot;&gt;&lt;code&gt;cProfile&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If you don&amp;rsquo;t have &lt;code&gt;vmprof&lt;/code&gt; or &lt;code&gt;yappi&lt;/code&gt; installed, then it&amp;rsquo;ll fall back to the standard &lt;code&gt;cProfile&lt;/code&gt;. It&amp;rsquo;s &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/profiler.html&quot;&gt;well-documented&lt;/a&gt;, so I won&amp;rsquo;t rehash it here. &lt;/p&gt;
&lt;h3 id=&quot;scientific-mode&quot;&gt;Scientific Mode&lt;/h3&gt;
&lt;p&gt;Python is not only a language for general and web programming. It also emerged as the best tool for data science and machine learning over these last years thanks to libraries and tools like &lt;a href=&quot;http://www.numpy.org/&quot;&gt;NumPy&lt;/a&gt;, &lt;a href=&quot;https://www.scipy.org/&quot;&gt;SciPy&lt;/a&gt;, &lt;a href=&quot;https://scikit-learn.org/&quot;&gt;scikit-learn&lt;/a&gt;, &lt;a href=&quot;https://matplotlib.org/&quot;&gt;Matplotlib&lt;/a&gt;, &lt;a href=&quot;https://jupyter.org/&quot;&gt;Jupyter&lt;/a&gt;, and more. With such powerful libraries available, you need a powerful IDE to support all the functions such as graphing and analyzing those libraries have. PyCharm provides everything you need as &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/matplotlib-support.html&quot;&gt;thoroughly documented here&lt;/a&gt;.  &lt;/p&gt;
&lt;h3 id=&quot;remote-development&quot;&gt;Remote Development&lt;/h3&gt;
&lt;p&gt;One common cause of bugs in many applications is that development and production environments differ. Although, in most cases, it&amp;rsquo;s not possible to provide an exact copy of the production environment for development, pursuing it is a worthy goal.&lt;/p&gt;
&lt;p&gt;With PyCharm, you can debug your application using an interpreter that is located on the other computer, such as a Linux VM. As a result, you can have the same interpreter as your production environment to fix and avoid many bugs resulting from the difference between development and production environments. Make sure to check out the &lt;a href=&quot;https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html&quot;&gt;official documentation&lt;/a&gt; to learn more.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;PyCharm is one of best, if not the best, full-featured, dedicated, and versatile IDEs for Python development. It offers a ton of benefits, saving you a lot of time by helping you with routine tasks. Now you know how to be productive with it!&lt;/p&gt;
&lt;p&gt;In this article, you learned about a lot, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Installing PyCharm&lt;/li&gt;
&lt;li&gt;Writing code in PyCharm&lt;/li&gt;
&lt;li&gt;Running your code in PyCharm&lt;/li&gt;
&lt;li&gt;Debugging and testing your code in PyCharm&lt;/li&gt;
&lt;li&gt;Editing an existing project in PyCharm&lt;/li&gt;
&lt;li&gt;Searching and navigating in PyCharm&lt;/li&gt;
&lt;li&gt;Using Version Control in PyCharm&lt;/li&gt;
&lt;li&gt;Using Plugins and External Tools in PyCharm&lt;/li&gt;
&lt;li&gt;Using PyCharm Professional features, such as Django support and Scientific mode&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If there&amp;rsquo;s anything you&amp;rsquo;d like to ask or share, please reach out in the comments below. There&amp;rsquo;s also a lot more information at the &lt;a href=&quot;https://www.jetbrains.com/pycharm/documentation/&quot;&gt;PyCharm website&lt;/a&gt; for you to explore.&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Clone Repo:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/alcazar-web-framework/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-alcazar-web-framework&quot; data-focus=&quot;false&quot;&gt;Click here to clone the repo you&#39;ll use&lt;/a&gt; to explore the project-focused features of PyCharm in this tutorial.&lt;/p&gt;&lt;/div&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>How to Use Python Lambda Functions</title>
      <id>https://realpython.com/courses/python-lambda-functions/</id>
      <link href="https://realpython.com/courses/python-lambda-functions/"/>
      <updated>2019-08-27T14:00:00+00:00</updated>
      <summary>In this step-by-step course, you&#39;ll learn about Python lambda functions. You&#39;ll see how they compare with regular functions and how you can use them in accordance with best practices.</summary>
      <content type="html">
        &lt;p&gt;Python and other languages like Java, C#, and even C++ have had lambda functions added to their syntax, whereas languages like LISP or the ML family of languages, Haskell, OCaml, and F#, use lambdas as a core concept. Python lambdas are little, anonymous functions, subject to a more restrictive but more concise syntax than regular Python functions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;By the end of this course, you&amp;rsquo;ll know:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;How Python lambdas came to be &lt;/li&gt;
&lt;li&gt;How lambdas compare with regular function objects&lt;/li&gt;
&lt;li&gt;How to write lambda functions&lt;/li&gt;
&lt;li&gt;Which functions in the Python standard library leverage lambdas&lt;/li&gt;
&lt;li&gt;When to use or avoid Python lambda functions&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This course is mainly for intermediate to experienced Python programmers, but it is accessible to any curious minds with interest in programming. All the examples included in this tutorial have been tested with Python 3.7.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>A Guide to Excel Spreadsheets in Python With openpyxl</title>
      <id>https://realpython.com/openpyxl-excel-spreadsheets-python/</id>
      <link href="https://realpython.com/openpyxl-excel-spreadsheets-python/"/>
      <updated>2019-08-26T14:00:00+00:00</updated>
      <summary>In this step-by-step tutorial, you&#39;ll learn how to handle spreadsheets in Python using the openpyxl package. You&#39;ll learn how to manipulate Excel spreadsheets, extract information from spreadsheets, create simple or more complex spreadsheets, including adding styles, charts, and so on.</summary>
      <content type="html">
        &lt;p&gt;Excel spreadsheets are one of those things you might have to deal with at some point. Either it&amp;rsquo;s because your boss loves them or because marketing needs them, you might have to learn how to work with spreadsheets, and that&amp;rsquo;s when knowing &lt;code&gt;openpyxl&lt;/code&gt; comes in handy!&lt;/p&gt;
&lt;p&gt;Spreadsheets are a very intuitive and user-friendly way to manipulate large datasets without any prior technical background. That&amp;rsquo;s why they&amp;rsquo;re still so commonly used today.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this article, you&amp;rsquo;ll learn how to use openpyxl to:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Manipulate Excel spreadsheets with confidence&lt;/li&gt;
&lt;li&gt;Extract information from spreadsheets&lt;/li&gt;
&lt;li&gt;Create simple or more complex spreadsheets, including adding styles, charts, and so on&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This article is written for intermediate developers who have a pretty good knowledge of Python data structures, such as &lt;a href=&quot;https://realpython.com/python-dicts/&quot;&gt;dicts&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/python-lists-tuples/&quot;&gt;lists&lt;/a&gt;, but also feel comfortable around &lt;a href=&quot;https://realpython.com/python3-object-oriented-programming/&quot;&gt;OOP&lt;/a&gt; and more intermediate level topics.&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Download Dataset:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/openpyxl-sample-dataset/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-openpyxl-sample-dataset&quot; data-focus=&quot;false&quot;&gt;Click here to download the dataset for the openpyxl exercise you&#39;ll be following in this tutorial.&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;

&lt;h2 id=&quot;before-you-begin&quot;&gt;Before You Begin&lt;/h2&gt;
&lt;p&gt;If you ever get asked to extract some data from a database or log file into an Excel spreadsheet, or if you often have to convert an Excel spreadsheet into some more usable programmatic form, then this tutorial is perfect for you. Let&amp;rsquo;s jump into the &lt;code&gt;openpyxl&lt;/code&gt; caravan!&lt;/p&gt;
&lt;h3 id=&quot;practical-use-cases&quot;&gt;Practical Use Cases&lt;/h3&gt;
&lt;p&gt;First things first, when would you need to use a package like &lt;code&gt;openpyxl&lt;/code&gt; in a real-world scenario? You&amp;rsquo;ll see a few examples below, but really, there are hundreds of possible scenarios where this knowledge could come in handy.&lt;/p&gt;
&lt;h4 id=&quot;importing-new-products-into-a-database&quot;&gt;Importing New Products Into a Database&lt;/h4&gt;
&lt;p&gt;You are responsible for tech in an online store company, and your boss doesn&amp;rsquo;t want to pay for a cool and expensive CMS system.&lt;/p&gt;
&lt;p&gt;Every time they want to add new products to the online store, they come to you with an Excel spreadsheet with a few hundred rows and, for each of them, you have the product name, description, price, and so forth.&lt;/p&gt;
&lt;p&gt;Now, to import the data, you&amp;rsquo;ll have to iterate over each spreadsheet row and add each product to the online store.&lt;/p&gt;
&lt;h4 id=&quot;exporting-database-data-into-a-spreadsheet&quot;&gt;Exporting Database Data Into a Spreadsheet&lt;/h4&gt;
&lt;p&gt;Say you have a Database table where you record all your users&amp;rsquo; information, including name, phone number, email address, and so forth.&lt;/p&gt;
&lt;p&gt;Now, the Marketing team wants to contact all users to give them some discounted offer or promotion. However, they don&amp;rsquo;t have access to the Database, or they don&amp;rsquo;t know how to use SQL to extract that information easily.&lt;/p&gt;
&lt;p&gt;What can you do to help? Well, you can make a quick script using &lt;code&gt;openpyxl&lt;/code&gt; that iterates over every single User record and puts all the essential information into an Excel spreadsheet.&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s gonna earn you an extra slice of cake at your company&amp;rsquo;s next birthday party!&lt;/p&gt;
&lt;h4 id=&quot;appending-information-to-an-existing-spreadsheet&quot;&gt;Appending Information to an Existing Spreadsheet&lt;/h4&gt;
&lt;p&gt;You may also have to open a spreadsheet, read the information in it and, according to some business logic, append more data to it.&lt;/p&gt;
&lt;p&gt;For example, using the online store scenario again, say you get an Excel spreadsheet with a list of users and you need to append to each row the total amount they&amp;rsquo;ve spent in your store.&lt;/p&gt;
&lt;p&gt;This data is in the Database and, in order to do this, you have to read the spreadsheet, iterate through each row, fetch the total amount spent from the Database and then write back to the spreadsheet.&lt;/p&gt;
&lt;p&gt;Not a problem for &lt;code&gt;openpyxl&lt;/code&gt;!&lt;/p&gt;
&lt;h3 id=&quot;learning-some-basic-excel-terminology&quot;&gt;Learning Some Basic Excel Terminology&lt;/h3&gt;
&lt;p&gt;Here&amp;rsquo;s a quick list of basic terms you&amp;rsquo;ll see when you&amp;rsquo;re working with Excel spreadsheets:&lt;/p&gt;
&lt;div class=&quot;table-responsive&quot;&gt;
&lt;table class=&quot;table table-hover&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Term&lt;/th&gt;
&lt;th&gt;Explanation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Spreadsheet or Workbook&lt;/td&gt;
&lt;td&gt;A &lt;strong&gt;Spreadsheet&lt;/strong&gt; is the main file you are creating or working with.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Worksheet or Sheet&lt;/td&gt;
&lt;td&gt;A &lt;strong&gt;Sheet&lt;/strong&gt; is used to split different kinds of content within the same spreadsheet. A &lt;strong&gt;Spreadsheet&lt;/strong&gt; can have one or more &lt;strong&gt;Sheets&lt;/strong&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Column&lt;/td&gt;
&lt;td&gt;A &lt;strong&gt;Column&lt;/strong&gt; is a vertical line, and it&amp;rsquo;s represented by an uppercase letter: &lt;em&gt;A&lt;/em&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Row&lt;/td&gt;
&lt;td&gt;A &lt;strong&gt;Row&lt;/strong&gt; is a horizontal line, and it&amp;rsquo;s represented by a number: &lt;em&gt;1&lt;/em&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cell&lt;/td&gt;
&lt;td&gt;A &lt;strong&gt;Cell&lt;/strong&gt; is a combination of &lt;strong&gt;Column&lt;/strong&gt; and &lt;strong&gt;Row&lt;/strong&gt;, represented by both an uppercase letter and a number: &lt;em&gt;A1&lt;/em&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;h3 id=&quot;getting-started-with-openpyxl&quot;&gt;Getting Started With openpyxl&lt;/h3&gt;
&lt;p&gt;Now that you&amp;rsquo;re aware of the benefits of a tool like &lt;code&gt;openpyxl&lt;/code&gt;, let&amp;rsquo;s get down to it and start by installing the package. For this tutorial, you should use Python 3.7 and openpyxl 2.6.2. To install the package, you can do the following:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip install openpyxl
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After you install the package, you should be able to create a super simple spreadsheet with the following code:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;B1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;world!&amp;quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;hello_world.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The code above should create a file called &lt;code&gt;hello_world.xlsx&lt;/code&gt; in the folder you are using to run the code. If you open that file with Excel you should see something like this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_16.54.45.e646867e4dbb.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_16.54.45.e646867e4dbb.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_16.54.45.e646867e4dbb.png&amp;amp;w=540&amp;amp;sig=4c3acdcf35f528b6ed0cf6e299c2575781934414 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_16.54.45.e646867e4dbb.png&amp;amp;w=1080&amp;amp;sig=328d4ff12cec767d684f5b7666380d9f23a2a548 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_16.54.45.e646867e4dbb.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;A Simple Hello World Spreadsheet&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Woohoo&lt;/em&gt;, your first spreadsheet created!&lt;/p&gt;
&lt;h2 id=&quot;reading-excel-spreadsheets-with-openpyxl&quot;&gt;Reading Excel Spreadsheets With openpyxl&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s start with the most essential thing one can do with a spreadsheet: read it.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll go from a straightforward approach to reading a spreadsheet to more complex examples where you read the data and convert it into more useful Python structures.&lt;/p&gt;
&lt;h3 id=&quot;dataset-for-this-tutorial&quot;&gt;Dataset for This Tutorial&lt;/h3&gt;
&lt;p&gt;Before you dive deep into some code examples, you should &lt;strong&gt;download this sample dataset&lt;/strong&gt; and store it somewhere as &lt;code&gt;sample.xlsx&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Download Dataset:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/openpyxl-sample-dataset/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-openpyxl-sample-dataset&quot; data-focus=&quot;false&quot;&gt;Click here to download the dataset for the openpyxl exercise you&#39;ll be following in this tutorial.&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;This is one of the datasets you&amp;rsquo;ll be using throughout this tutorial, and it&amp;rsquo;s a spreadsheet with a sample of real data from Amazon&amp;rsquo;s online product reviews. This dataset is only a tiny fraction of what Amazon &lt;a href=&quot;https://registry.opendata.aws/amazon-reviews/&quot;&gt;provides&lt;/a&gt;, but for testing purposes, it&amp;rsquo;s more than enough.&lt;/p&gt;
&lt;h3 id=&quot;a-simple-approach-to-reading-an-excel-spreadsheet&quot;&gt;A Simple Approach to Reading an Excel Spreadsheet&lt;/h3&gt;
&lt;p&gt;Finally, let&amp;rsquo;s start reading some spreadsheets! To begin with, open our sample spreadsheet:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;Sheet 1&amp;#39;]&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;Worksheet &amp;quot;Sheet 1&amp;quot;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;Sheet 1&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the code above, you first open the spreadsheet &lt;code&gt;sample.xlsx&lt;/code&gt; using &lt;code&gt;load_workbook()&lt;/code&gt;, and then you can use &lt;code&gt;workbook.sheetnames&lt;/code&gt; to see all the sheets you have available to work with. After that,  &lt;code&gt;workbook.active&lt;/code&gt; selects the first available sheet and, in this case, you can see that it selects &lt;strong&gt;Sheet 1&lt;/strong&gt; automatically. Using these methods is the default way of opening a spreadsheet, and you&amp;rsquo;ll see it many times during this tutorial.&lt;/p&gt;
&lt;p&gt;Now, after opening a spreadsheet, you can easily retrieve data from it like this:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A1&amp;gt;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;marketplace&amp;#39;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;F10&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;quot;G-Shock Men&amp;#39;s Grey Sport Watch&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To return the actual value of a cell, you need to do &lt;code&gt;.value&lt;/code&gt;. Otherwise, you&amp;rsquo;ll get the main &lt;code&gt;Cell&lt;/code&gt; object. You can also use the method &lt;code&gt;.cell()&lt;/code&gt; to retrieve a cell using index notation. Remember to add &lt;code&gt;.value&lt;/code&gt; to get the actual value and not a &lt;code&gt;Cell&lt;/code&gt; object:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.F10&amp;gt;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;quot;G-Shock Men&amp;#39;s Grey Sport Watch&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can see that the results returned are the same, no matter which way you decide to go with. However, in this tutorial, you&amp;rsquo;ll be mostly using the first approach: &lt;code&gt;[&quot;A1&quot;]&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Even though in Python you&amp;rsquo;re used to a zero-indexed notation, with spreadsheets you&amp;rsquo;ll always use a one-indexed notation where the first row or column always has index &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The above shows you the quickest way to open a spreadsheet. However, you can pass additional parameters to change the way a spreadsheet is loaded.&lt;/p&gt;
&lt;h4 id=&quot;additional-reading-options&quot;&gt;Additional Reading Options&lt;/h4&gt;
&lt;p&gt;There are a few arguments you can pass to &lt;code&gt;load_workbook()&lt;/code&gt; that change the way a spreadsheet is loaded. The most important ones are the following two Booleans:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;read_only&lt;/strong&gt; loads a spreadsheet in read-only mode allowing you to open very large Excel files.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;data_only&lt;/strong&gt; ignores loading formulas and instead loads only the resulting values.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;importing-data-from-a-spreadsheet&quot;&gt;Importing Data From a Spreadsheet&lt;/h3&gt;
&lt;p&gt;Now that you&amp;rsquo;ve learned the basics about loading a spreadsheet, it&amp;rsquo;s about time you get to the fun part: &lt;strong&gt;the iteration and actual usage of the values within the spreadsheet&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This section is where you&amp;rsquo;ll learn all the different ways you can iterate through the data, but also how to convert that data into something usable and, more importantly, how to do it in a Pythonic way.&lt;/p&gt;
&lt;h4 id=&quot;iterating-through-the-data&quot;&gt;Iterating Through the Data&lt;/h4&gt;
&lt;p&gt;There are a few different ways you can iterate through the data depending on your needs.&lt;/p&gt;
&lt;p&gt;You can slice the data with a combination of columns and rows:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1:C2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;((&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.C1&amp;gt;),&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt; (&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A2&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B2&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.C2&amp;gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can get ranges of rows or columns:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Get all cells from column A&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A1&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt; &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A2&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt; ...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt; &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A99&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt; &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A100&amp;gt;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Get all cells for a range of columns&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A:B&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;((&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A1&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A2&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  ...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A99&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A100&amp;gt;),&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt; (&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B1&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B2&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  ...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B99&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B100&amp;gt;))&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Get all cells from row 5&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A5&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt; &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B5&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt; ...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt; &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.N5&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt; &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.O5&amp;gt;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Get all cells for a range of rows&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;((&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A5&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B5&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  ...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.N5&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.O5&amp;gt;),&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt; (&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A6&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B6&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  ...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.N6&amp;gt;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.O6&amp;gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You&amp;rsquo;ll notice that all of the above examples return a &lt;code&gt;tuple&lt;/code&gt;. If you want to refresh your memory on how to handle &lt;code&gt;tuples&lt;/code&gt; in Python, check out the article on &lt;a href=&quot;https://realpython.com/python-lists-tuples/#python-tuples&quot;&gt;Lists and Tuples in Python&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;There are also multiple ways of using normal Python &lt;a href=&quot;https://realpython.com/introduction-to-python-generators/&quot;&gt;generators&lt;/a&gt; to go through the data. The main methods you can use to achieve this are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;.iter_rows()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.iter_cols()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Both methods can receive the following arguments:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;min_row&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;max_row&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;min_col&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;max_col&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These arguments are used to set boundaries for the iteration:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                           &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                           &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                           &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.C1&amp;gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A2&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B2&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.C2&amp;gt;)&lt;/span&gt;


&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;column&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                              &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                              &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                              &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A2&amp;gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B2&amp;gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.C1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.C2&amp;gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You&amp;rsquo;ll notice that in the first example, when iterating through the rows using &lt;code&gt;.iter_rows()&lt;/code&gt;, you get one &lt;code&gt;tuple&lt;/code&gt; element per row selected. While when using &lt;code&gt;.iter_cols()&lt;/code&gt; and iterating through columns, you&amp;rsquo;ll get one &lt;code&gt;tuple&lt;/code&gt; per column instead.&lt;/p&gt;
&lt;p&gt;One additional argument you can pass to both methods is the Boolean &lt;code&gt;values_only&lt;/code&gt;. When it&amp;rsquo;s set to &lt;code&gt;True&lt;/code&gt;, the values of the cell are returned, instead of the &lt;code&gt;Cell&lt;/code&gt; object:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;values_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;#39;marketplace&amp;#39;, &amp;#39;customer_id&amp;#39;, &amp;#39;review_id&amp;#39;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;#39;US&amp;#39;, 3653882, &amp;#39;R3O9SGZBVQBV76&amp;#39;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you want to iterate through the whole dataset, then you can also use the attributes &lt;code&gt;.rows&lt;/code&gt; or &lt;code&gt;.columns&lt;/code&gt; directly, which are shortcuts to using &lt;code&gt;.iter_rows()&lt;/code&gt; and &lt;code&gt;.iter_cols()&lt;/code&gt; without any arguments:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.A1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.B1&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.C1&amp;gt;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.M100&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.N100&amp;gt;, &amp;lt;Cell &amp;#39;Sheet 1&amp;#39;.O100&amp;gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;These shortcuts are very useful when you&amp;rsquo;re iterating through the whole dataset.&lt;/p&gt;
&lt;h4 id=&quot;manipulate-data-using-pythons-default-data-structures&quot;&gt;Manipulate Data Using Python&amp;rsquo;s Default Data Structures&lt;/h4&gt;
&lt;p&gt;Now that you know the basics of iterating through the data in a workbook, let&amp;rsquo;s look at smart ways of converting that data into Python structures.&lt;/p&gt;
&lt;p&gt;As you saw earlier, the result from all iterations comes in the form of &lt;code&gt;tuples&lt;/code&gt;. However, since a &lt;code&gt;tuple&lt;/code&gt; is nothing more than an immutable &lt;code&gt;list&lt;/code&gt;, you can easily access its data and transform it into other structures.&lt;/p&gt;
&lt;p&gt;For example, say you want to extract product information from the &lt;code&gt;sample.xlsx&lt;/code&gt; spreadsheet and into a dictionary where each key is a product ID.&lt;/p&gt;
&lt;p&gt;A straightforward way to do this is to iterate over all the rows, pick the columns you know are related to product information, and then store that in a dictionary. Let&amp;rsquo;s code this out!&lt;/p&gt;
&lt;p&gt;First of all, have a look at the headers and see what information you care most about:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;values_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;#39;marketplace&amp;#39;, &amp;#39;customer_id&amp;#39;, &amp;#39;review_id&amp;#39;, &amp;#39;product_id&amp;#39;, ...)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code returns a list of all the column names you have in the spreadsheet. To start, grab the columns with names:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;product_id&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;product_parent&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;product_title&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;product_category&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Lucky for you, the columns you need are all next to each other so you can use the &lt;code&gt;min_column&lt;/code&gt; and &lt;code&gt;max_column&lt;/code&gt; to easily get the data you want:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;values_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;#39;B00FALQ1ZC&amp;#39;, 937001370, &amp;#39;Invicta Women\&amp;#39;s 15150 &amp;quot;Angel&amp;quot; 18k Yellow...)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;#39;B00D3RGO20&amp;#39;, 484010722, &amp;quot;Kenneth Cole New York Women&amp;#39;s KC4944...)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Nice! Now that you know how to get all the important product information you need, let&amp;rsquo;s put that data into a dictionary:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;json&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;products&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Using the values_only because you want to return the cells&amp;#39; values&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;values_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s2&quot;&gt;&amp;quot;parent&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;s2&quot;&gt;&amp;quot;title&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;s2&quot;&gt;&amp;quot;category&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;products&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Using json here to be able to format the output for displaying later&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dumps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The code above returns a JSON similar to this:&lt;/p&gt;
&lt;div class=&quot;highlight json&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;quot;B00FALQ1ZC&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;parent&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;937001370&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;title&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Invicta Women&amp;#39;s 15150 ...&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;category&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Watches&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;quot;B00D3RGO20&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;parent&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;484010722&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;title&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Kenneth Cole New York ...&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;quot;category&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Watches&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here you can see that the output is trimmed to 2 products only, but if you run the script as it is, then you should get 98 products.&lt;/p&gt;
&lt;h4 id=&quot;convert-data-into-python-classes&quot;&gt;Convert Data Into Python Classes&lt;/h4&gt;
&lt;p&gt;To finalize the reading section of this tutorial, let&amp;rsquo;s dive into Python classes and see how you could improve on the example above and better structure the data.&lt;/p&gt;
&lt;p&gt;For this, you&amp;rsquo;ll be using the new Python &lt;a href=&quot;https://realpython.com/python-data-classes/&quot;&gt;Data Classes&lt;/a&gt; that are available from Python 3.7. If you&amp;rsquo;re using an older version of Python, then you can use the default &lt;a href=&quot;https://realpython.com/python3-object-oriented-programming/#classes-in-python&quot;&gt;Classes&lt;/a&gt; instead.&lt;/p&gt;
&lt;p&gt;So, first things first, let&amp;rsquo;s look at the data you have and decide what you want to store and how you want to store it.&lt;/p&gt;
&lt;p&gt;As you saw right at the start, this data comes from Amazon, and it&amp;rsquo;s a list of product reviews. You can check the &lt;a href=&quot;https://s3.amazonaws.com/amazon-reviews-pds/tsv/index.txt&quot;&gt;list of all the columns and their meaning&lt;/a&gt; on Amazon.&lt;/p&gt;
&lt;p&gt;There are two significant elements you can extract from the data available:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Products&lt;/li&gt;
&lt;li&gt;Reviews&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A &lt;strong&gt;Product&lt;/strong&gt; has:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;ID&lt;/li&gt;
&lt;li&gt;Title&lt;/li&gt;
&lt;li&gt;Parent&lt;/li&gt;
&lt;li&gt;Category&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;strong&gt;Review&lt;/strong&gt; has a few more fields:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;ID&lt;/li&gt;
&lt;li&gt;Customer ID&lt;/li&gt;
&lt;li&gt;Stars&lt;/li&gt;
&lt;li&gt;Headline&lt;/li&gt;
&lt;li&gt;Body&lt;/li&gt;
&lt;li&gt;Date&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can ignore a few of the review fields to make things a bit simpler.&lt;/p&gt;
&lt;p&gt;So, a straightforward implementation of these two classes could be written in a separate file &lt;code&gt;classes.py&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;datetime&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dataclasses&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataclass&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;category&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Review&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;customer_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;stars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;headline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After defining your data classes, you need to convert the data from the spreadsheet into these new structures.&lt;/p&gt;
&lt;p&gt;Before doing the conversion, it&amp;rsquo;s worth looking at our header again and creating a mapping between columns and the fields you need:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                             &lt;span class=&quot;n&quot;&gt;values_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;#39;marketplace&amp;#39;, &amp;#39;customer_id&amp;#39;, &amp;#39;review_id&amp;#39;, &amp;#39;product_id&amp;#39;, ...)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Or an alternative&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;marketplace&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;customer_id&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;review_id&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;product_id&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;product_parent&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let&amp;rsquo;s create a file &lt;code&gt;mapping.py&lt;/code&gt; where you have a list of all the field names and their column location (zero-indexed) on the spreadsheet:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Product fields&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;PRODUCT_ID&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;PRODUCT_PARENT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;PRODUCT_TITLE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;PRODUCT_CATEGORY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Review fields&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;REVIEW_ID&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;REVIEW_CUSTOMER&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;REVIEW_STARS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;REVIEW_HEADLINE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;REVIEW_BODY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;REVIEW_DATE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;14&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You don&amp;rsquo;t necessarily have to do the mapping above. It&amp;rsquo;s more for readability when parsing the row data, so you don&amp;rsquo;t end up with a lot of magic numbers lying around.&lt;/p&gt;
&lt;p&gt;Finally, let&amp;rsquo;s look at the code needed to parse the spreadsheet data into a list of product and review objects:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;datetime&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;classes&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Review&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;mapping&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PRODUCT_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PRODUCT_PARENT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PRODUCT_TITLE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; \
    &lt;span class=&quot;n&quot;&gt;PRODUCT_CATEGORY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REVIEW_DATE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REVIEW_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REVIEW_CUSTOMER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; \
    &lt;span class=&quot;n&quot;&gt;REVIEW_STARS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REVIEW_HEADLINE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REVIEW_BODY&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Using the read_only method since you&amp;#39;re not gonna be editing the spreadsheet&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;read_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;products&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;reviews&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Using the values_only because you just want to return the cell value&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_PARENT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_TITLE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                      &lt;span class=&quot;n&quot;&gt;category&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PRODUCT_CATEGORY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;products&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# You need to parse the date from the spreadsheet into a datetime format&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;spread_date&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REVIEW_DATE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;parsed_date&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strptime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spread_date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;%Y-%m-&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%d&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;review&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Review&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REVIEW_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;customer_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REVIEW_CUSTOMER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;stars&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REVIEW_STARS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;headline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REVIEW_HEADLINE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REVIEW_BODY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parsed_date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;reviews&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;review&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reviews&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After you run the code above, you should get some output like this:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;B00FALQ1ZC&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;937001370&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Review&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;R3O9SGZBVQBV76&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;customer_id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3653882&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That&amp;rsquo;s it! Now you should have the data in a very simple and digestible class format, and you can start thinking of storing this in a &lt;a href=&quot;https://realpython.com/tutorials/databases/&quot;&gt;Database&lt;/a&gt; or any other type of data storage you like.&lt;/p&gt;
&lt;p&gt;Using this kind of OOP strategy to parse spreadsheets makes handling the data much simpler later on.&lt;/p&gt;
&lt;h3 id=&quot;appending-new-data&quot;&gt;Appending New Data&lt;/h3&gt;
&lt;p&gt;Before you start creating very complex spreadsheets, have a quick look at an example of how to append data to an existing spreadsheet.&lt;/p&gt;
&lt;p&gt;Go back to the first example spreadsheet you created (&lt;code&gt;hello_world.xlsx&lt;/code&gt;) and try opening it and appending some data to it, like this:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Start by opening the spreadsheet and selecting the main sheet&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;hello_world.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Write what you want into a specific cell&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;C1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;writing ;)&amp;quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Save the spreadsheet&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;hello_world_append.xlsx&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Et voilà&lt;/em&gt;, if you open the new &lt;code&gt;hello_world_append.xlsx&lt;/code&gt; spreadsheet, you&amp;rsquo;ll see the following change:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_17.44.22.e4f18e5abc42.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_17.44.22.e4f18e5abc42.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_17.44.22.e4f18e5abc42.png&amp;amp;w=540&amp;amp;sig=098886279b90048004feb6dcdbe1c66ac3e231ce 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_17.44.22.e4f18e5abc42.png&amp;amp;w=1080&amp;amp;sig=8619e04c109779499f96dcd8aee01c4cf1ed52eb 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_17.44.22.e4f18e5abc42.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Appending Data to a Spreadsheet&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Notice the additional &lt;em&gt;writing ;)&lt;/em&gt; on cell &lt;code&gt;C1&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;writing-excel-spreadsheets-with-openpyxl&quot;&gt;Writing Excel Spreadsheets With openpyxl&lt;/h2&gt;
&lt;p&gt;There are a lot of different things you can write to a spreadsheet, from simple text or number values to complex formulas, charts, or even images.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s start creating some spreadsheets!&lt;/p&gt;
&lt;h3 id=&quot;creating-a-simple-spreadsheet&quot;&gt;Creating a Simple Spreadsheet&lt;/h3&gt;
&lt;p&gt;Previously, you saw a very quick example of how to write &amp;ldquo;Hello world!&amp;rdquo; into a spreadsheet, so you can start with that:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;hello_world.xlsx&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;B1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;world!&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The highlighted lines in the code above are the most important ones for writing. In the code, you can see that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Line 5&lt;/strong&gt; shows you how to create a new empty workbook.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lines 8 and 9&lt;/strong&gt; show you how to add data to specific cells.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 11&lt;/strong&gt; shows you how to save the spreadsheet when you&amp;rsquo;re done.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Even though these lines above can be straightforward, it&amp;rsquo;s still good to know them well for when things get a bit more complicated.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You&amp;rsquo;ll be using the &lt;code&gt;hello_world.xlsx&lt;/code&gt; spreadsheet for some of the upcoming examples, so keep it handy.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;One thing you can do to help with coming code examples is add the following method to your Python file or console:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values_only&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It makes it easier to print all of your spreadsheet values by just calling &lt;code&gt;print_rows()&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;basic-spreadsheet-operations&quot;&gt;Basic Spreadsheet Operations&lt;/h3&gt;
&lt;p&gt;Before you get into the more advanced topics, it&amp;rsquo;s good for you to know how to manage the most simple elements of a spreadsheet.&lt;/p&gt;
&lt;h4 id=&quot;adding-and-updating-cell-values&quot;&gt;Adding and Updating Cell Values&lt;/h4&gt;
&lt;p&gt;You already learned how to add values to a spreadsheet like this:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;value&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There&amp;rsquo;s another way you can do this, by first selecting a cell and then changing its value:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;Cell &amp;#39;Sheet&amp;#39;.A1&amp;gt;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;hey&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;hey&amp;#39;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The new value is only stored into the spreadsheet once you call &lt;code&gt;workbook.save()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;openpyxl&lt;/code&gt; creates a cell when adding a value, if that cell didn&amp;rsquo;t exist before:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Before, our spreadsheet has only 1 row&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Try adding a value to row 10&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;B10&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;test&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, &amp;#39;test&amp;#39;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As you can see, when trying to add a value to cell &lt;code&gt;B10&lt;/code&gt;, you end up with a tuple with 10 rows, just so you can have that &lt;em&gt;test&lt;/em&gt; value.&lt;/p&gt;
&lt;h4 id=&quot;managing-rows-and-columns&quot;&gt;Managing Rows and Columns&lt;/h4&gt;
&lt;p&gt;One of the most common things you have to do when manipulating spreadsheets is adding or removing rows and columns. The &lt;code&gt;openpyxl&lt;/code&gt; package allows you to do that in a very straightforward way by using the methods:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;.insert_rows()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.delete_rows()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.insert_cols()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.delete_cols()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Every single one of those methods can receive two arguments:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;idx&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;amount&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Using our basic &lt;code&gt;hello_world.xlsx&lt;/code&gt; example again, let&amp;rsquo;s see how these methods work:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Insert a column before the existing column 1 (&amp;quot;A&amp;quot;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert_cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, &amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Insert 5 columns between column 2 (&amp;quot;B&amp;quot;) and 3 (&amp;quot;C&amp;quot;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert_cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, &amp;#39;hello&amp;#39;, None, None, None, None, None, &amp;#39;world!&amp;#39;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Delete the created columns&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;delete_cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;delete_cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Insert a new row in the beginning&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Insert 3 new rows in the beginning&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(None, None)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Delete the first 4 rows&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;delete_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;(&amp;#39;hello&amp;#39;, &amp;#39;world!&amp;#39;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The only thing you need to remember is that when inserting new data (rows or columns), the insertion happens &lt;strong&gt;before&lt;/strong&gt; the &lt;code&gt;idx&lt;/code&gt; parameter.&lt;/p&gt;
&lt;p&gt;So, if you do &lt;code&gt;insert_rows(1)&lt;/code&gt;, it inserts a new row &lt;strong&gt;before&lt;/strong&gt; the existing first row.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s the same for columns: when you call &lt;code&gt;insert_cols(2)&lt;/code&gt;, it inserts a new column right &lt;strong&gt;before&lt;/strong&gt; the already existing second column (&lt;code&gt;B&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;However, when deleting rows or columns, &lt;code&gt;.delete_...&lt;/code&gt; deletes data &lt;strong&gt;starting from&lt;/strong&gt; the index passed as an argument.&lt;/p&gt;
&lt;p&gt;For example, when doing &lt;code&gt;delete_rows(2)&lt;/code&gt; it deletes row &lt;code&gt;2&lt;/code&gt;, and when doing &lt;code&gt;delete_cols(3)&lt;/code&gt; it deletes the third column (&lt;code&gt;C&lt;/code&gt;).&lt;/p&gt;
&lt;h4 id=&quot;managing-sheets&quot;&gt;Managing Sheets&lt;/h4&gt;
&lt;p&gt;Sheet management is also one of those things you might need to know, even though it might be something that you don&amp;rsquo;t use that often.&lt;/p&gt;
&lt;p&gt;If you look back at the code examples from this tutorial, you&amp;rsquo;ll notice the following recurring piece of code:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is the way to select the default sheet from a spreadsheet. However, if you&amp;rsquo;re opening a spreadsheet with multiple sheets, then you can always select a specific one like this:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Let&amp;#39;s say you have two sheets: &amp;quot;Products&amp;quot; and &amp;quot;Company Sales&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;]&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# You can select a sheet using its title&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products_sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sales_sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Company Sales&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can also change a sheet title very easily:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;]&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products_sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products_sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;New Products&amp;quot;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;New Products&amp;#39;, &amp;#39;Company Sales&amp;#39;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you want to create or delete sheets, then you can also do that with &lt;code&gt;.create_sheet()&lt;/code&gt; and &lt;code&gt;.remove()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;]&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;operations_sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Operations&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;, &amp;#39;Operations&amp;#39;]&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# You can also define the position to create the sheet at&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hr_sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;HR&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;HR&amp;#39;, &amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;, &amp;#39;Operations&amp;#39;]&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# To remove them, just pass the sheet as an argument to the .remove()&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;operations_sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;HR&amp;#39;, &amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;]&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hr_sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;One other thing you can do is make duplicates of a sheet using &lt;code&gt;copy_worksheet()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;]&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products_sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;copy_worksheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products_sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;Worksheet &amp;quot;Products Copy&amp;quot;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheetnames&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[&amp;#39;Products&amp;#39;, &amp;#39;Company Sales&amp;#39;, &amp;#39;Products Copy&amp;#39;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you open your spreadsheet after saving the above code, you&amp;rsquo;ll notice that the sheet &lt;em&gt;Products Copy&lt;/em&gt; is a duplicate of the sheet &lt;em&gt;Products&lt;/em&gt;.&lt;/p&gt;
&lt;h4 id=&quot;freezing-rows-and-columns&quot;&gt;Freezing Rows and Columns&lt;/h4&gt;
&lt;p&gt;Something that you might want to do when working with big spreadsheets is to freeze a few rows or columns, so they remain visible when you scroll right or down.&lt;/p&gt;
&lt;p&gt;Freezing data allows you to keep an eye on important rows or columns, regardless of where you scroll in the spreadsheet.&lt;/p&gt;
&lt;p&gt;Again, &lt;code&gt;openpyxl&lt;/code&gt; also has a way to accomplish this by using the worksheet &lt;code&gt;freeze_panes&lt;/code&gt; attribute. For this example, go back to our &lt;code&gt;sample.xlsx&lt;/code&gt; spreadsheet and try doing the following:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;freeze_panes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;C2&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_frozen.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you open the &lt;code&gt;sample_frozen.xlsx&lt;/code&gt; spreadsheet in your favorite spreadsheet editor, you&amp;rsquo;ll notice that row &lt;code&gt;1&lt;/code&gt; and columns &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; are frozen and are always visible no matter where you navigate within the spreadsheet.&lt;/p&gt;
&lt;p&gt;This feature is handy, for example, to keep headers within sight, so you always know what each column represents.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s how it looks in the editor:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.12.20.55694a0781f8.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.12.20.55694a0781f8.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.12.20.55694a0781f8.png&amp;amp;w=540&amp;amp;sig=5826de23e5df2e08d625844698fc3a29b32ee7b2 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.12.20.55694a0781f8.png&amp;amp;w=1080&amp;amp;sig=c3abe2321f00372d975bbbf033f3ebe3687eb09f 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.12.20.55694a0781f8.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Frozen Rows and Columns&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Notice how you&amp;rsquo;re at the end of the spreadsheet, and yet, you can see both row &lt;code&gt;1&lt;/code&gt; and columns &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt;.&lt;/p&gt;
&lt;h4 id=&quot;adding-filters&quot;&gt;Adding Filters&lt;/h4&gt;
&lt;p&gt;You can use &lt;code&gt;openpyxl&lt;/code&gt; to add filters and sorts to your spreadsheet. However, when you open the spreadsheet, the data won&amp;rsquo;t be rearranged according to these sorts and filters.&lt;/p&gt;
&lt;p&gt;At first, this might seem like a pretty useless feature, but when you&amp;rsquo;re programmatically creating a spreadsheet that is going to be sent and used by somebody else, it&amp;rsquo;s still nice to at least create the filters and allow people to use it afterward.&lt;/p&gt;
&lt;p&gt;The code below is an example of how you would add some filters to our existing &lt;code&gt;sample.xlsx&lt;/code&gt; spreadsheet:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Check the used spreadsheet space using the attribute &amp;quot;dimensions&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dimensions&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;#39;A1:O100&amp;#39;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;auto_filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;A1:O100&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_with_filters.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You should now see the filters created when opening the spreadsheet in your editor:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.20.35.5fdbfe805194.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.20.35.5fdbfe805194.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.20.35.5fdbfe805194.png&amp;amp;w=540&amp;amp;sig=c1d7ad4f2dfc03fc8730e3babf9000ac74170c7d 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.20.35.5fdbfe805194.png&amp;amp;w=1080&amp;amp;sig=27e888a967ddc112f1e824be671a15e2c111fe6c 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.20.35.5fdbfe805194.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Filters&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You don&amp;rsquo;t have to use &lt;code&gt;sheet.dimensions&lt;/code&gt; if you know precisely which part of the spreadsheet you want to apply filters to.&lt;/p&gt;
&lt;h3 id=&quot;adding-formulas&quot;&gt;Adding Formulas&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Formulas&lt;/strong&gt; (or &lt;strong&gt;formulae&lt;/strong&gt;) are one of the most powerful features of spreadsheets.&lt;/p&gt;
&lt;p&gt;They gives you the power to apply specific mathematical equations to a range of cells. Using formulas with &lt;code&gt;openpyxl&lt;/code&gt; is as simple as editing the value of a cell.&lt;/p&gt;
&lt;p&gt;You can see the list of formulas supported by &lt;code&gt;openpyxl&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.utils&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FORMULAE&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FORMULAE&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;frozenset({&amp;#39;ABS&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;           &amp;#39;ACCRINT&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;           &amp;#39;ACCRINTM&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;           &amp;#39;ACOS&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;           &amp;#39;ACOSH&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;           &amp;#39;AMORDEGRC&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;           &amp;#39;AMORLINC&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;           &amp;#39;AND&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;           ...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;           &amp;#39;YEARFRAC&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;           &amp;#39;YIELD&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;           &amp;#39;YIELDDISC&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;           &amp;#39;YIELDMAT&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;           &amp;#39;ZTEST&amp;#39;})&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let&amp;rsquo;s add some formulas to our &lt;code&gt;sample.xlsx&lt;/code&gt; spreadsheet.&lt;/p&gt;
&lt;p&gt;Starting with something easy, let&amp;rsquo;s check the average star rating for the 99 reviews within the spreadsheet:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Star rating is column &amp;quot;H&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;P2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;=AVERAGE(H2:H100)&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_formulas.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you open the spreadsheet now and go to cell &lt;code&gt;P2&lt;/code&gt;, you should see that its value is: &lt;em&gt;4.18181818181818&lt;/em&gt;. Have a look in the editor:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.33.09.7c2633f706cc.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.33.09.7c2633f706cc.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.33.09.7c2633f706cc.png&amp;amp;w=540&amp;amp;sig=5d7a9eb97acf524d5d2b9b93ae0e9214bbcf95c8 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.33.09.7c2633f706cc.png&amp;amp;w=1080&amp;amp;sig=8af67321cb101fb2f30fd0ca2bdcc62de35c9334 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.33.09.7c2633f706cc.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Average Formula&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You can use the same methodology to add any formulas to your spreadsheet. For example, let&amp;rsquo;s count the number of reviews that had helpful votes:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# The helpful votes are counted on column &amp;quot;I&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;P3&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;=COUNTIF(I2:I100, &amp;quot;&amp;gt;0&amp;quot;)&amp;#39;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_formulas.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You should get the number &lt;code&gt;21&lt;/code&gt; on your &lt;code&gt;P3&lt;/code&gt; spreadsheet cell like so:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.35.24.e26e97b0c9c0.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.35.24.e26e97b0c9c0.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.35.24.e26e97b0c9c0.png&amp;amp;w=540&amp;amp;sig=0ec4c4c12a792a1a393e0273855282bfa0594d53 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.35.24.e26e97b0c9c0.png&amp;amp;w=1080&amp;amp;sig=67b399b8cb79ddbe7285da0325fe8f6b9edf3ecc 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.35.24.e26e97b0c9c0.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Average and CountIf Formula&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll have to make sure that the strings within a formula are always in double quotes, so you either have to use single quotes around the formula like in the example above or you&amp;rsquo;ll have to escape the double quotes inside the formula: &lt;code&gt;&quot;=COUNTIF(I2:I100, \&quot;&amp;gt;0\&quot;)&quot;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;There are a ton of other formulas you can add to your spreadsheet using the same procedure you tried above. Give it a go yourself!&lt;/p&gt;
&lt;h3 id=&quot;adding-styles&quot;&gt;Adding Styles&lt;/h3&gt;
&lt;p&gt;Even though styling a spreadsheet might not be something you would do every day, it&amp;rsquo;s still good to know how to do it.&lt;/p&gt;
&lt;p&gt;Using &lt;code&gt;openpyxl&lt;/code&gt;, you can apply multiple styling options to your spreadsheet, including fonts, borders, colors, and so on. Have a look at the &lt;code&gt;openpyxl&lt;/code&gt; &lt;a href=&quot;https://openpyxl.readthedocs.io/en/stable/styles.html&quot;&gt;documentation&lt;/a&gt; to learn more.&lt;/p&gt;
&lt;p&gt;You can also choose to either apply a style directly to a cell or create a template and reuse it to apply styles to multiple cells.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s start by having a look at simple cell styling, using our &lt;code&gt;sample.xlsx&lt;/code&gt; again as the base spreadsheet:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Import necessary style classes&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.styles&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Alignment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Border&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Create a few styles&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bold_font&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bold&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;big_red_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;center_aligned_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Alignment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;horizontal&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;center&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;double_border_side&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;border_style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;double&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square_border&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Border&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;top&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;double_border_side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                       &lt;span class=&quot;n&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;double_border_side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                       &lt;span class=&quot;n&quot;&gt;bottom&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;double_border_side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                       &lt;span class=&quot;n&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;double_border_side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Style some cells!&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bold_font&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A3&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;big_red_text&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A4&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alignment&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;center_aligned_text&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A5&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;border&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;square_border&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_styles.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you open your spreadsheet now, you should see quite a few different styles on the first 5 cells of column &lt;code&gt;A&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.43.15.e3aeb3fb06e3.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.43.15.e3aeb3fb06e3.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.43.15.e3aeb3fb06e3.png&amp;amp;w=540&amp;amp;sig=ecc21878006697a6135ae515442642a95ab2bfb6 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.43.15.e3aeb3fb06e3.png&amp;amp;w=1080&amp;amp;sig=6f0ef4a148f1ca5a588e0cb2c02b0c9aad4246f2 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.43.15.e3aeb3fb06e3.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Simple Cell Styles&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;There you go. You got:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;A2&lt;/strong&gt; with the text in bold&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A3&lt;/strong&gt; with the text in red and bigger font size&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A4&lt;/strong&gt; with the text centered&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A5&lt;/strong&gt; with a square border around the text&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; For the colors, you can also use HEX codes instead by doing  &lt;code&gt;Font(color=&quot;C70E0F&quot;)&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can also combine styles by simply adding them to the cell at the same time:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Reusing the same styles from the example above&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A6&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alignment&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;center_aligned_text&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A6&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;big_red_text&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A6&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;border&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;square_border&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_styles.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Have a look at cell &lt;code&gt;A6&lt;/code&gt; here:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.46.04.314517930065.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.46.04.314517930065.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.46.04.314517930065.png&amp;amp;w=540&amp;amp;sig=290bbf523eb24ac8c9741daf86701ca57cad4b96 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.46.04.314517930065.png&amp;amp;w=1080&amp;amp;sig=9decedef154c2138e26b287f2186213142650f6e 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.46.04.314517930065.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Coupled Cell Styles&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When you want to apply multiple styles to one or several cells, you can use a &lt;code&gt;NamedStyle&lt;/code&gt; class instead, which is like a style template that you can use over and over again. Have a look at the example below:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.styles&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NamedStyle&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Let&amp;#39;s create a style template for the header row&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;header&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NamedStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;header&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bold&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;border&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Border&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bottom&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Side&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;border_style&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;thin&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alignment&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Alignment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;horizontal&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;center&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vertical&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;center&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Now let&amp;#39;s apply this to all first row (header) cells&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;header_row&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;header_row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;style&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;header&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_styles.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you open the spreadsheet now, you should see that its first row is bold, the text is aligned to the center, and there&amp;rsquo;s a small bottom border! Have a look below:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.48.33.4bc57d1b24d5.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.48.33.4bc57d1b24d5.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.48.33.4bc57d1b24d5.png&amp;amp;w=540&amp;amp;sig=199107a0c9ea60fbf1dfcc078a7680b43faeef3a 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.48.33.4bc57d1b24d5.png&amp;amp;w=1080&amp;amp;sig=af3e5225e36a24dea088e8c175da02050bb5dda9 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.48.33.4bc57d1b24d5.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Named Styles&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As you saw above, there are many options when it comes to styling, and it depends on the use case, so feel free to check &lt;code&gt;openpyxl&lt;/code&gt; &lt;a href=&quot;https://openpyxl.readthedocs.io/en/stable/styles.html&quot;&gt;documentation&lt;/a&gt; and see what other things you can do.&lt;/p&gt;
&lt;h3 id=&quot;conditional-formatting&quot;&gt;Conditional Formatting&lt;/h3&gt;
&lt;p&gt;This feature is one of my personal favorites when it comes to adding styles to a spreadsheet.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s a much more powerful approach to styling because it dynamically applies styles according to how the data in the spreadsheet changes.&lt;/p&gt;
&lt;p&gt;In a nutshell, &lt;strong&gt;conditional formatting&lt;/strong&gt; allows you to specify a list of styles to apply to a cell (or cell range) according to specific conditions.&lt;/p&gt;
&lt;p&gt;For example, a widespread use case is to have a balance sheet where all the negative totals are in red, and the positive ones are in green. This formatting makes it much more efficient to spot good vs bad periods.&lt;/p&gt;
&lt;p&gt;Without further ado, let&amp;rsquo;s pick our favorite spreadsheet&amp;mdash;&lt;code&gt;sample.xlsx&lt;/code&gt;&amp;mdash;and add some conditional formatting.&lt;/p&gt;
&lt;p&gt;You can start by adding a simple one that adds a red background to all reviews with less than 3 stars:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.styles&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PatternFill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.styles.differential&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DifferentialStyle&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.formatting.rule&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Rule&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;red_background&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PatternFill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bgColor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diff_style&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DifferentialStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;red_background&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Rule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;expression&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dxf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diff_style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rule&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;formula&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;$H1&amp;lt;3&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conditional_formatting&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;A1:O100&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_conditional_formatting.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now you&amp;rsquo;ll see all the reviews with a star rating below 3 marked with a red background:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.55.41.17f234a186c6.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_18.55.41.17f234a186c6.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.55.41.17f234a186c6.png&amp;amp;w=540&amp;amp;sig=f3c141c2fe708c031c32c083cb038a736fd8da87 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_18.55.41.17f234a186c6.png&amp;amp;w=1080&amp;amp;sig=9ded3045cee09d34cd6f5dada7a4eea669ac4808 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_18.55.41.17f234a186c6.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Simple Conditional Formatting&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Code-wise, the only things that are new here are the objects &lt;code&gt;DifferentialStyle&lt;/code&gt; and &lt;code&gt;Rule&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;DifferentialStyle&lt;/code&gt;&lt;/strong&gt; is quite similar to &lt;code&gt;NamedStyle&lt;/code&gt;, which you already saw above, and it&amp;rsquo;s used to aggregate multiple styles such as fonts, borders, alignment, and so forth.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;Rule&lt;/code&gt;&lt;/strong&gt; is responsible for selecting the cells and applying the styles if the cells match the rule&amp;rsquo;s logic.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Using a &lt;code&gt;Rule&lt;/code&gt; object, you can create numerous conditional formatting scenarios.&lt;/p&gt;
&lt;p&gt;However, for simplicity sake, the &lt;code&gt;openpyxl&lt;/code&gt; package offers 3 built-in formats that make it easier to create a few common conditional formatting patterns. These built-ins are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ColorScale&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;IconSet&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;DataBar&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;strong&gt;ColorScale&lt;/strong&gt; gives you the ability to create color gradients:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.formatting.rule&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ColorScaleRule&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_scale_rule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ColorScaleRule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;min&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;start_color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;end_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;max&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;end_color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GREEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Again, let&amp;#39;s add this gradient to the star ratings, column &amp;quot;H&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conditional_formatting&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;H2:H100&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color_scale_rule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_conditional_formatting_color_scale.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now you should see a color gradient on column &lt;code&gt;H&lt;/code&gt;, from red to green, according to the star rating:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.00.57.26756963c1e9.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.00.57.26756963c1e9.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.00.57.26756963c1e9.png&amp;amp;w=540&amp;amp;sig=782964d150a8adc1de811fab78b0accde6357f85 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.00.57.26756963c1e9.png&amp;amp;w=1080&amp;amp;sig=4c7c87c194ec0a8eb3a9e73fdabf3ead991e96ea 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_19.00.57.26756963c1e9.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Color Scale Conditional Formatting&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You can also add a third color and make two gradients instead:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.formatting.rule&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ColorScaleRule&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color_scale_rule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ColorScaleRule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;start_value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;start_color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;mid_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;mid_value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;mid_color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;YELLOW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;end_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;end_value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                                  &lt;span class=&quot;n&quot;&gt;end_color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GREEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Again, let&amp;#39;s add this gradient to the star ratings, column &amp;quot;H&amp;quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conditional_formatting&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;H2:H100&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color_scale_rule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_conditional_formatting_color_scale_3.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This time, you&amp;rsquo;ll notice that star ratings between 1 and 3 have a gradient from red to yellow, and star ratings between 3 and 5 have a gradient from yellow to green:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.03.30.0de9a2ff9866.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.03.30.0de9a2ff9866.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.03.30.0de9a2ff9866.png&amp;amp;w=540&amp;amp;sig=17daddc356c8ff5c78497b200fe57ab69f80617d 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.03.30.0de9a2ff9866.png&amp;amp;w=1080&amp;amp;sig=6dcb18f4aa3f8ae0a395ca1e3581f8d4c59805ad 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_19.03.30.0de9a2ff9866.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With 2 Color Scales Conditional Formatting&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;IconSet&lt;/strong&gt; allows you to add an icon to the cell according to its value:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.formatting.rule&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IconSetRule&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;icon_set_rule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IconSetRule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;5Arrows&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;num&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conditional_formatting&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;H2:H100&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;icon_set_rule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_conditional_formatting_icon_set.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You&amp;rsquo;ll see a colored arrow next to the star rating. This arrow is red and points down when the value of the cell is 1 and, as the rating gets better, the arrow starts pointing up and becomes green:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.07.29.23e75ff46771.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.07.29.23e75ff46771.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.07.29.23e75ff46771.png&amp;amp;w=540&amp;amp;sig=388fc68ff53fa2e2d3d5678acfd41f10fa8eccde 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.07.29.23e75ff46771.png&amp;amp;w=1080&amp;amp;sig=e33f483f9758189782bb619a6714c948130375aa 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_19.07.29.23e75ff46771.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Icon Set Conditional Formatting&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;openpyxl&lt;/code&gt; package has a &lt;a href=&quot;https://openpyxl.readthedocs.io/en/stable/formatting.html#iconset&quot;&gt;full list&lt;/a&gt; of other icons you can use, besides the arrow.&lt;/p&gt;
&lt;p&gt;Finally, the &lt;strong&gt;DataBar&lt;/strong&gt; allows you to create progress bars:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.formatting.rule&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DataBarRule&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_bar_rule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DataBarRule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                            &lt;span class=&quot;n&quot;&gt;start_value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                            &lt;span class=&quot;n&quot;&gt;end_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;num&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                            &lt;span class=&quot;n&quot;&gt;end_value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;5&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;                            &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GREEN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conditional_formatting&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;H2:H100&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_bar_rule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample_conditional_formatting_data_bar.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You&amp;rsquo;ll now see a green progress bar that gets fuller the closer the star rating is to the number 5:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.09.10.ebbe032c088d.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_19.09.10.ebbe032c088d.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.09.10.ebbe032c088d.png&amp;amp;w=540&amp;amp;sig=a7b8e3515fde3ff6b662ff1780cbc290da9ce2ad 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_19.09.10.ebbe032c088d.png&amp;amp;w=1080&amp;amp;sig=1e3d2befc147a43c5b98061cf5888abf22ca4c4a 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_19.09.10.ebbe032c088d.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Data Bar Conditional Formatting&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As you can see, there are a lot of cool things you can do with conditional formatting.&lt;/p&gt;
&lt;p&gt;Here, you saw only a few examples of what you can achieve with it, but check the &lt;code&gt;openpyxl&lt;/code&gt; &lt;a href=&quot;https://openpyxl.readthedocs.io/en/stable/formatting.html&quot;&gt;documentation&lt;/a&gt; to see a bunch of other options.&lt;/p&gt;
&lt;h3 id=&quot;adding-images&quot;&gt;Adding Images&lt;/h3&gt;
&lt;p&gt;Even though images are not something that you&amp;rsquo;ll often see in a spreadsheet, it&amp;rsquo;s quite cool to be able to add them. Maybe you can use it for branding purposes or to make spreadsheets more personal.&lt;/p&gt;
&lt;p&gt;To be able to load images to a spreadsheet using &lt;code&gt;openpyxl&lt;/code&gt;, you&amp;rsquo;ll have to install &lt;code&gt;Pillow&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip install Pillow
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Apart from that, you&amp;rsquo;ll also need an image. For this example, you can grab the &lt;em&gt;Real Python&lt;/em&gt; logo below and convert it from &lt;code&gt;.webp&lt;/code&gt; to &lt;code&gt;.png&lt;/code&gt; using an online converter such as &lt;a href=&quot;https://cloudconvert.com/webp-to-png&quot;&gt;cloudconvert.com&lt;/a&gt;, save the final file as &lt;code&gt;logo.png&lt;/code&gt;, and copy it to the root folder where you&amp;rsquo;re running your examples:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/real-python-logo-round.4d95338e8944.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-25&quot; src=&quot;https://files.realpython.com/media/real-python-logo-round.4d95338e8944.png&quot; width=&quot;1500&quot; height=&quot;1500&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/real-python-logo-round.4d95338e8944.png&amp;amp;w=375&amp;amp;sig=e431a39c9d7f2d5963a81687571a41288c359142 375w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/real-python-logo-round.4d95338e8944.png&amp;amp;w=750&amp;amp;sig=a098752adfc378feee6bc69748af593ed078b8c0 750w, https://files.realpython.com/media/real-python-logo-round.4d95338e8944.png 1500w&quot; sizes=&quot;75vw&quot; alt=&quot;Real Python Logo&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Afterward, this is the code you need to import that image into the &lt;code&gt;hello_word.xlsx&lt;/code&gt; spreadsheet:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.drawing.image&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Let&amp;#39;s use the hello_world spreadsheet since it has less data&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;hello_world.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;logo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;logo.png&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# A bit of resizing to not fill the whole spreadsheet with the logo&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;height&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;150&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;150&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;A3&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;hello_world_logo.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You have an image on your spreadsheet! Here it is:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_20.05.30.2a69f2a77f68.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_20.05.30.2a69f2a77f68.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_20.05.30.2a69f2a77f68.png&amp;amp;w=540&amp;amp;sig=574c2c425c011fa21e790f7cd4a41547f2449b01 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_20.05.30.2a69f2a77f68.png&amp;amp;w=1080&amp;amp;sig=e6b9c78c7daa929ae86f887d560c3f50f0851d5d 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_20.05.30.2a69f2a77f68.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Image&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The image&amp;rsquo;s left top corner is on the cell you chose, in this case, &lt;code&gt;A3&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;adding-pretty-charts&quot;&gt;Adding Pretty Charts&lt;/h3&gt;
&lt;p&gt;Another powerful thing you can do with spreadsheets is create an incredible variety of charts. &lt;/p&gt;
&lt;p&gt;Charts are a great way to visualize and understand loads of data quickly. There are a lot of different chart types: bar chart, pie chart, line chart, and so on. &lt;code&gt;openpyxl&lt;/code&gt; has support for a lot of them.&lt;/p&gt;
&lt;p&gt;Here, you&amp;rsquo;ll see only a couple of examples of charts because the theory behind it is the same for every single chart type:&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; A few of the chart types that &lt;code&gt;openpyxl&lt;/code&gt; currently doesn&amp;rsquo;t have support for are Funnel, Gantt, Pareto, Treemap, Waterfall, Map, and Sunburst.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;For any chart you want to build, you&amp;rsquo;ll need to define the chart type: &lt;code&gt;BarChart&lt;/code&gt;, &lt;code&gt;LineChart&lt;/code&gt;, and so forth, plus the data to be used for the chart, which is called &lt;code&gt;Reference&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Before you can build your chart, you need to define what data you want to see represented in it. Sometimes, you can use the dataset as is, but other times you need to massage the data a bit to get additional information.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s start by building a new workbook with some sample data:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.chart&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BarChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Let&amp;#39;s create some sample sales data&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Product&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Online&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Store&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;45&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;35&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now you&amp;rsquo;re going to start by creating a &lt;strong&gt;bar chart&lt;/strong&gt; that displays the total number of sales per product:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BarChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worksheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;titles_from_data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;E2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;chart.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There you have it. Below, you can see a very straightforward bar chart  showing the difference between &lt;strong&gt;online&lt;/strong&gt; product sales online and &lt;strong&gt;in-store&lt;/strong&gt; product sales:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_20.59.43.7eac35127b97.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_20.59.43.7eac35127b97.png&quot; width=&quot;2160&quot; height=&quot;1352&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_20.59.43.7eac35127b97.png&amp;amp;w=540&amp;amp;sig=bcdfa015a56b903169702ddbeb1ec06c8d67bc87 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_20.59.43.7eac35127b97.png&amp;amp;w=1080&amp;amp;sig=904be9b2a172b3ae7436311c9d05f6a9ad8ae451 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_20.59.43.7eac35127b97.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Bar Chart&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Like with images, the top left corner of the chart is on the cell you added the chart to. In your case, it was on cell &lt;code&gt;E2&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Depending on whether you&amp;rsquo;re using Microsoft Excel or an open-source alternative (LibreOffice or OpenOffice), the chart might look slightly different.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Try creating a &lt;strong&gt;line chart&lt;/strong&gt; instead, changing the data a bit:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.chart&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Let&amp;#39;s create some sample sales data&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;January&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;February&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;March&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;April&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;    &lt;span class=&quot;s2&quot;&gt;&amp;quot;May&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;June&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;July&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;August&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;September&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;     &lt;span class=&quot;s2&quot;&gt;&amp;quot;October&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;November&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;December&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;                           &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;                           &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;24 &lt;/span&gt;                           &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;With the above code, you&amp;rsquo;ll be able to generate some random data regarding the sales of 3 different products across a whole year.&lt;/p&gt;
&lt;p&gt;Once that&amp;rsquo;s done, you can very easily create a line chart with the following code:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worksheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;34 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;35 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_rows&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;titles_from_data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;36 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;C6&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;37 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;38 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;line_chart.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here&amp;rsquo;s the outcome of the above piece of code:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.06.42.e4e52ab1b433.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.06.42.e4e52ab1b433.png&quot; width=&quot;2160&quot; height=&quot;1414&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.06.42.e4e52ab1b433.png&amp;amp;w=540&amp;amp;sig=362319a9716ded57c9567de98c52a4dc805b5346 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.06.42.e4e52ab1b433.png&amp;amp;w=1080&amp;amp;sig=cbd7b37aa77318e4ed8d2401281817fb53a7144b 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_21.06.42.e4e52ab1b433.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Line Chart&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One thing to keep in mind here is the fact that you&amp;rsquo;re using &lt;code&gt;from_rows=True&lt;/code&gt; when adding the data. This argument makes the chart plot row by row instead of column by column.&lt;/p&gt;
&lt;p&gt;In your sample data, you see that each product has a row with 12 values (1 column per month). That&amp;rsquo;s why you use &lt;code&gt;from_rows&lt;/code&gt;. If you don&amp;rsquo;t pass that argument, by default, the chart tries to plot by column, and you&amp;rsquo;ll get a month-by-month comparison of sales.&lt;/p&gt;
&lt;p&gt;Another difference that has to do with the above argument change is the fact that our &lt;code&gt;Reference&lt;/code&gt; now starts from the first column, &lt;code&gt;min_col=1&lt;/code&gt;, instead of the second one. This change is needed because the chart now expects the first column to have the titles.&lt;/p&gt;
&lt;p&gt;There are a couple of other things you can also change regarding the style of the chart. For example, you can add specific categories to the chart:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cats&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worksheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_categories&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Add this piece of code before saving the workbook, and you should see the month names appearing instead of numbers:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.08.05.8867e2cced85.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.08.05.8867e2cced85.png&quot; width=&quot;2160&quot; height=&quot;1414&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.08.05.8867e2cced85.png&amp;amp;w=540&amp;amp;sig=6e48719b75e585dcb58fec3768def0630bb367ff 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.08.05.8867e2cced85.png&amp;amp;w=1080&amp;amp;sig=9fed1350289fe067d8f50c07516bfcc460f4a720 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_21.08.05.8867e2cced85.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Line Chart and Categories&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Code-wise, this is a minimal change. But in terms of the readability of the spreadsheet, this makes it much easier for someone to open the spreadsheet and understand the chart straight away.&lt;/p&gt;
&lt;p&gt;Another thing you can do to improve the chart readability is to add an axis. You can do it using the attributes &lt;code&gt;x_axis&lt;/code&gt; and &lt;code&gt;y_axis&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Months&amp;quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Sales (per unit)&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This will generate a spreadsheet like the below one:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.09.46.ce55f629b073.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.09.46.ce55f629b073.png&quot; width=&quot;2160&quot; height=&quot;1414&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.09.46.ce55f629b073.png&amp;amp;w=540&amp;amp;sig=ac73f73702b55a957c77d6da224cde46c2c9a802 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.09.46.ce55f629b073.png&amp;amp;w=1080&amp;amp;sig=408f6929a900e4a5ccb5cb1cca8647cf64d0d069 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_21.09.46.ce55f629b073.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Line Chart, Categories and Axis Titles&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As you can see, small changes like the above make reading your chart a much easier and quicker task.&lt;/p&gt;
&lt;p&gt;There is also a way to style your chart by using Excel&amp;rsquo;s default &lt;code&gt;ChartStyle&lt;/code&gt; property. In this case, you have to choose a number between 1 and 48. Depending on your choice, the colors of your chart change as well:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# You can play with this by choosing any number between 1 and 48&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;style&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;With the style selected above, all lines have some shade of orange:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.16.31.7df18bbe94cb.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.16.31.7df18bbe94cb.png&quot; width=&quot;2160&quot; height=&quot;1414&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.16.31.7df18bbe94cb.png&amp;amp;w=540&amp;amp;sig=4b0439c6c48d0b3f96f411e6198f6fd49d5c7026 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.16.31.7df18bbe94cb.png&amp;amp;w=1080&amp;amp;sig=4d0aad0a27321bb321dc9c88158f357155968121 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_21.16.31.7df18bbe94cb.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Line Chart, Categories, Axis Titles and Style&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;There is no clear documentation on what each style number looks like, but &lt;a href=&quot;https://1drv.ms/x/s!Asf0Y5Y4GI3Mg6kZNRd1IA09NLWv9A&quot;&gt;this spreadsheet&lt;/a&gt; has a few examples of the styles available.&lt;/p&gt;
&lt;div class=&quot;card mb-3&quot; id=&quot;collapse_card0fb191&quot;&gt;
&lt;div class=&quot;card-header border-0&quot;&gt;&lt;p class=&quot;m-0&quot;&gt;&lt;button class=&quot;btn&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse0fb191&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse0fb191&quot;&gt;Complete Code Example&lt;/button&gt; &lt;button class=&quot;btn btn-link float-right&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapse0fb191&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapse0fb191&quot;&gt;Show/Hide&lt;/button&gt;&lt;/p&gt;&lt;/div&gt;
&lt;div id=&quot;collapse0fb191&quot; class=&quot;collapse&quot; data-parent=&quot;#collapse_card0fb191&quot;&gt;&lt;div class=&quot;card-body&quot; markdown=&quot;1&quot;&gt;

&lt;p&gt;Here&amp;rsquo;s the full code used to generate the line chart with categories, axis titles, and style:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.chart&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Let&amp;#39;s create some sample sales data&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;January&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;February&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;March&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;April&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&amp;quot;May&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;June&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;July&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;August&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;September&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
     &lt;span class=&quot;s2&quot;&gt;&amp;quot;October&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;November&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;December&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iter_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Create a LineChart and add the main data&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worksheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                           &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;titles_from_data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_rows&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Add categories to the chart&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;cats&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worksheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_categories&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Rename the X and Y Axis&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Months&amp;quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Sales (per unit)&amp;quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Apply a specific Style&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;style&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Save!&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;C6&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;line_chart.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;p&gt;There are a lot more chart types and customization you can apply, so be sure to check out the &lt;a href=&quot;https://openpyxl.readthedocs.io/en/stable/charts/introduction.html&quot;&gt;package documentation&lt;/a&gt; on this if you need some specific formatting.&lt;/p&gt;
&lt;h3 id=&quot;convert-python-classes-to-excel-spreadsheet&quot;&gt;Convert Python Classes to Excel Spreadsheet&lt;/h3&gt;
&lt;p&gt;You already saw how to convert an Excel spreadsheet&amp;rsquo;s data into Python classes, but now let&amp;rsquo;s do the opposite.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s imagine you have a database and are using some Object-Relational Mapping (ORM) to map DB objects into Python classes. Now, you want to export those same objects into a spreadsheet.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s assume the following &lt;a href=&quot;https://realpython.com/python-data-classes/&quot;&gt;data classes&lt;/a&gt; to represent the data coming from your database regarding product sales:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dataclasses&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataclass&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;typing&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@dataclass&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sales&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, let&amp;rsquo;s generate some random data, assuming the above classes are stored in a &lt;code&gt;db_classes.py&lt;/code&gt; file:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Ignore these for now. You&amp;#39;ll use them in a sec ;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.chart&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;db_classes&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sale&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 9 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;products&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Let&amp;#39;s create 5 products&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sales&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# Create 5 months of sales&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;sales&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;20 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;21 &lt;/span&gt;                      &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Product &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;22 &lt;/span&gt;                      &lt;span class=&quot;n&quot;&gt;sales&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sales&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;23 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;products&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By running this piece of code, you should get 5 products with 5 months of sales with a random quantity of sales for each month.&lt;/p&gt;
&lt;p&gt;Now, to convert this into a spreadsheet, you need to iterate over the data and append it to the spreadsheet:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;25 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;26 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;27 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;28 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Append column names first&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;29 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Product ID&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Product Name&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Month 1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;30 &lt;/span&gt;              &lt;span class=&quot;s2&quot;&gt;&amp;quot;Month 2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Month 3&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Month 4&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Month 5&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;31 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;32 &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Append the data&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;33 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;products&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;34 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;35 &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;product&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sales&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;36 &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sale&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quantity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;37 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That&amp;rsquo;s it. That should allow you to create a spreadsheet with some data coming from your database.&lt;/p&gt;
&lt;p&gt;However, why not use some of that cool knowledge you gained recently to add a chart as well to display that data more visually?&lt;/p&gt;
&lt;p&gt;All right, then you could probably do something like this:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;38 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LineChart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;39 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worksheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;40 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;41 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;42 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;43 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;44 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;45 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;titles_from_data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from_rows&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;46 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;B8&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;47 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;48 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cats&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Reference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worksheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;49 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;50 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_row&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;51 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;min_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;52 &lt;/span&gt;                 &lt;span class=&quot;n&quot;&gt;max_col&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;53 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_categories&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cats&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;54 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;55 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Months&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;56 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_axis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Sales (per unit)&amp;quot;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;57 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;58 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;oop_sample.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now we&amp;rsquo;re talking! Here&amp;rsquo;s a spreadsheet generated from database objects and with a chart and everything:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.26.23.1f355e76586d.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.26.23.1f355e76586d.png&quot; width=&quot;2160&quot; height=&quot;1414&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.26.23.1f355e76586d.png&amp;amp;w=540&amp;amp;sig=135f4ee5413467c91f65bbb6e914724cdf1fd413 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.26.23.1f355e76586d.png&amp;amp;w=1080&amp;amp;sig=5b7dd165f92c237ddd049350ca6fc6a165e10512 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_21.26.23.1f355e76586d.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Conversion from Python Data Classes&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;That&amp;rsquo;s a great way for you to wrap up your new knowledge of charts!&lt;/p&gt;
&lt;h3 id=&quot;bonus-working-with-pandas&quot;&gt;Bonus: Working With Pandas&lt;/h3&gt;
&lt;p&gt;Even though you can use &lt;a href=&quot;https://realpython.com/working-with-large-excel-files-in-pandas/&quot;&gt;Pandas to handle Excel files&lt;/a&gt;, there are few things that you either can&amp;rsquo;t accomplish with Pandas or that you&amp;rsquo;d be better off just using &lt;code&gt;openpyxl&lt;/code&gt; directly.&lt;/p&gt;
&lt;p&gt;For example, some of the advantages of using &lt;code&gt;openpyxl&lt;/code&gt; are the ability to easily customize your spreadsheet with styles, conditional formatting, and such.&lt;/p&gt;
&lt;p&gt;But guess what, you don&amp;rsquo;t have to worry about picking. In fact, &lt;code&gt;openpyxl&lt;/code&gt; has support for both converting data from a Pandas DataFrame into a workbook or the opposite, converting an &lt;code&gt;openpyxl&lt;/code&gt; workbook into a Pandas DataFrame.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you&amp;rsquo;re new to Pandas, check our &lt;a href=&quot;https://realpython.com/courses/pandas-dataframes-101/&quot;&gt;course on Pandas DataFrames&lt;/a&gt; beforehand.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;First things first, remember to install the &lt;code&gt;pandas&lt;/code&gt; package:&lt;/p&gt;
&lt;div class=&quot;highlight sh&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;$&lt;/span&gt; pip install pandas
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then, let&amp;rsquo;s create a sample DataFrame:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt; 1 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pd&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 2 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 3 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 4 &lt;/span&gt;    &lt;span class=&quot;s2&quot;&gt;&amp;quot;Product Name&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Product 1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Product 2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 5 &lt;/span&gt;    &lt;span class=&quot;s2&quot;&gt;&amp;quot;Sales Month 1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 6 &lt;/span&gt;    &lt;span class=&quot;s2&quot;&gt;&amp;quot;Sales Month 2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;35&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 7 &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt; 8 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now that you have some data, you can use &lt;code&gt;.dataframe_to_rows()&lt;/code&gt; to convert it from a DataFrame into a worksheet:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;lineno&quot;&gt;10 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;11 &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl.utils.dataframe&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataframe_to_rows&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;12 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;13 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;14 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;15 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;16 &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataframe_to_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;17 &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;18 &lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;19 &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;pandas.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You should see a spreadsheet that looks like this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.42.15.0a4208db25f0.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block border &quot; src=&quot;https://files.realpython.com/media/Screenshot_2019-06-24_21.42.15.0a4208db25f0.png&quot; width=&quot;2160&quot; height=&quot;1414&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.42.15.0a4208db25f0.png&amp;amp;w=540&amp;amp;sig=636303dd8f99512651c5868f4ef572b2afa75d3c 540w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/Screenshot_2019-06-24_21.42.15.0a4208db25f0.png&amp;amp;w=1080&amp;amp;sig=c2ddf015c46566fc545ad03187cc5780a61938f9 1080w, https://files.realpython.com/media/Screenshot_2019-06-24_21.42.15.0a4208db25f0.png 2160w&quot; sizes=&quot;75vw&quot; alt=&quot;Example Spreadsheet With Data from Pandas Data Frame&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you want to add the &lt;a href=&quot;https://realpython.com/python-data-cleaning-numpy-pandas/#changing-the-index-of-a-dataframe&quot;&gt;DataFrame&amp;rsquo;s index&lt;/a&gt;, you can change &lt;code&gt;index=True&lt;/code&gt;, and it adds each row&amp;rsquo;s index into your spreadsheet.&lt;/p&gt;
&lt;p&gt;On the other hand, if you want to convert a spreadsheet into a DataFrame, you can also do it in a very straightforward way like so:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pd&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Alternatively, if you want to add the correct headers and use the review ID as the index, for example, then you can also do it like this instead:&lt;/p&gt;
&lt;div class=&quot;highlight python&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pandas&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pd&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;openpyxl&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;mapping&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REVIEW_ID&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_workbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;sample.xlsx&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;workbook&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;active&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sheet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Set the first row as the columns for the DataFrame&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Set the field &amp;quot;review_id&amp;quot; as the indexes for each row&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;REVIEW_ID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Using indexes and columns allows you to access data from your DataFrame easily:&lt;/p&gt;
&lt;div class=&quot;highlight python repl&quot;&gt;&lt;span class=&quot;repl-toggle&quot; title=&quot;Toggle REPL prompts and output&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;columns&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Index([&amp;#39;marketplace&amp;#39;, &amp;#39;customer_id&amp;#39;, &amp;#39;review_id&amp;#39;, &amp;#39;product_id&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       &amp;#39;product_parent&amp;#39;, &amp;#39;product_title&amp;#39;, &amp;#39;product_category&amp;#39;, &amp;#39;star_rating&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       &amp;#39;helpful_votes&amp;#39;, &amp;#39;total_votes&amp;#39;, &amp;#39;vine&amp;#39;, &amp;#39;verified_purchase&amp;#39;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       &amp;#39;review_headline&amp;#39;, &amp;#39;review_body&amp;#39;, &amp;#39;review_date&amp;#39;],&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;      dtype=&amp;#39;object&amp;#39;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Get first 10 reviews&amp;#39; star rating&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;star_rating&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;R3O9SGZBVQBV76    5&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;RKH8BNC3L5DLF     5&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;R2HLE8WKZSU3NL    2&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;R31U3UH5AZ42LL    5&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;R2SV659OUJ945Y    4&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;RA51CP8TR5A2L     5&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;RB2Q7DLDN6TH6     5&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;R2RHFJV0UYBK3Y    1&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;R2Z6JOQ94LFHEP    5&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;RX27XIIWY5JPB     4&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Name: star_rating, dtype: int64&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Grab review with id &amp;quot;R2EQL1V1L6E0C9&amp;quot;, using the index&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;R2EQL1V1L6E0C9&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;marketplace               US&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;customer_id         15305006&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;review_id     R2EQL1V1L6E0C9&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;product_id        B004LURNO6&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;product_parent     892860326&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;review_headline   Five Stars&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;review_body          Love it&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;review_date       2015-08-31&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Name: R2EQL1V1L6E0C9, dtype: object&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There you go, whether you want to use &lt;code&gt;openpyxl&lt;/code&gt; to prettify your Pandas dataset or use Pandas to do some hardcore algebra, you now know how to switch between both packages.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;Phew&lt;/em&gt;, after that long read, you now know how to work with spreadsheets in Python! You can rely on &lt;code&gt;openpyxl&lt;/code&gt;, your trustworthy companion, to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Extract valuable information from spreadsheets in a Pythonic manner&lt;/li&gt;
&lt;li&gt;Create your own spreadsheets, no matter the complexity level&lt;/li&gt;
&lt;li&gt;Add cool features such as conditional formatting or charts to your spreadsheets&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are a few other things you can do with &lt;code&gt;openpyxl&lt;/code&gt; that might not have been covered in this tutorial, but you can always check the package&amp;rsquo;s official &lt;a href=&quot;https://openpyxl.readthedocs.io/en/stable/index.html&quot;&gt;documentation website&lt;/a&gt; to learn more about it. You can even venture into checking its &lt;a href=&quot;https://bitbucket.org/openpyxl/openpyxl/src/default/&quot;&gt;source code&lt;/a&gt; and improving the package further.&lt;/p&gt;
&lt;p&gt;Feel free to leave any comments below if you have any questions, or if there&amp;rsquo;s any section you&amp;rsquo;d love to hear more about.&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Download Dataset:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/optins/view/openpyxl-sample-dataset/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-openpyxl-sample-dataset&quot; data-focus=&quot;false&quot;&gt;Click here to download the dataset for the openpyxl exercise you&#39;ll be following in this tutorial.&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Python Histogram Plotting: NumPy, Matplotlib, Pandas &amp; Seaborn</title>
      <id>https://realpython.com/courses/python-histograms/</id>
      <link href="https://realpython.com/courses/python-histograms/"/>
      <updated>2019-08-20T14:00:00+00:00</updated>
      <summary>In this course, you&#39;ll be equipped to make production-quality, presentation-ready Python histogram plots with a range of choices and features. It&#39;s your one-stop shop for constructing and manipulating histograms with Python&#39;s scientific stack.</summary>
      <content type="html">
        &lt;p&gt;In this course, you&amp;rsquo;ll be equipped to make production-quality, presentation-ready Python histogram plots with a range of choices and features.&lt;/p&gt;
&lt;p&gt;If you have introductory to intermediate knowledge in Python and statistics, then you can use this article as a one-stop shop for building and plotting histograms in Python using libraries from its scientific stack, including NumPy, Matplotlib, Pandas, and Seaborn.&lt;/p&gt;
&lt;p&gt;A histogram is a great tool for quickly assessing a &lt;a href=&quot;https://en.wikipedia.org/wiki/Probability_distribution&quot;&gt;probability distribution&lt;/a&gt; that is intuitively understood by almost any audience.  Python offers a handful of different options for building and plotting histograms.  Most people know a histogram by its graphical representation, which is similar to a bar graph:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://files.realpython.com/media/commute_times.621e5b1ce062.png&quot; target=&quot;_blank&quot;&gt;&lt;img class=&quot;img-fluid mx-auto d-block w-75&quot; src=&quot;https://files.realpython.com/media/commute_times.621e5b1ce062.png&quot; width=&quot;1152&quot; height=&quot;888&quot; srcset=&quot;https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/commute_times.621e5b1ce062.png&amp;amp;w=288&amp;amp;sig=408f56b07d4fb71d47171405f51e3cb58a7e6cc2 288w, https://robocrop.realpython.net/?url=https%3A//files.realpython.com/media/commute_times.621e5b1ce062.png&amp;amp;w=576&amp;amp;sig=a2b0324c81a7cd89fe7ceaacdcd4ae797ad1a587 576w, https://files.realpython.com/media/commute_times.621e5b1ce062.png 1152w&quot; sizes=&quot;75vw&quot; alt=&quot;Histogram of commute times for 1000 commuters&quot;/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This course will guide you through creating plots like the one above as well as more complex ones.  Here&amp;rsquo;s what you&amp;rsquo;ll cover:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Building histograms in pure Python, without use of third party libraries&lt;/li&gt;
&lt;li&gt;Constructing histograms with NumPy to summarize the underlying data&lt;/li&gt;
&lt;li&gt;Plotting the resulting histogram with Matplotlib, Pandas, and Seaborn&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; Short on time? &lt;a href=&quot;https://realpython.com/optins/view/histograms-cheatsheet/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-histograms-cheatsheet&quot; data-focus=&quot;false&quot;&gt;Click here to get access to a free two-page Python histograms cheat sheet&lt;/a&gt; that summarizes the techniques explained in this tutorial.&lt;/p&gt;&lt;/div&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Traditional Face Detection With Python</title>
      <id>https://realpython.com/courses/traditional-face-detection-python/</id>
      <link href="https://realpython.com/courses/traditional-face-detection-python/"/>
      <updated>2019-08-13T14:00:00+00:00</updated>
      <summary>In this course on face detection with Python, you&#39;ll learn about a historically important algorithm for object detection that can be successfully applied to finding the location of a human face within an image.</summary>
      <content type="html">
        &lt;p&gt;&lt;strong&gt;Computer vision&lt;/strong&gt; is an exciting and growing field. There are tons of interesting problems to solve! One of them is face detection: the ability of a computer to recognize that a photograph contains a human face, and tell you where it is located. In this course, you&amp;rsquo;ll learn about &lt;strong&gt;face detection&lt;/strong&gt; with Python.&lt;/p&gt;
&lt;p&gt;To detect any object in an image, it is necessary to understand how images are represented inside a computer, and how that object differs &lt;em&gt;visually&lt;/em&gt; from any other object.&lt;/p&gt;
&lt;p&gt;Once that is done, the process of scanning an image and looking for those visual cues needs to be automated and optimized. All these steps come together to form a fast and reliable computer vision algorithm.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this course, you&amp;rsquo;ll learn:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What face detection is&lt;/li&gt;
&lt;li&gt;How computers understand features in images&lt;/li&gt;
&lt;li&gt;How to quickly analyze many different features to reach a decision&lt;/li&gt;
&lt;li&gt;How to use a minimal Python solution for detecting human faces in images&lt;/li&gt;
&lt;/ul&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>11 Beginner Tips for Learning Python</title>
      <id>https://realpython.com/courses/python-beginner-tips/</id>
      <link href="https://realpython.com/courses/python-beginner-tips/"/>
      <updated>2019-08-06T14:00:00+00:00</updated>
      <summary>In this course, you&#39;ll see several learning strategies and tips that will help you jumpstart your journey towards becoming a rockstar Python programmer!</summary>
      <content type="html">
        &lt;p&gt;We are so excited that you have decided to embark on the journey of learning Python! One of the most common questions we receive from our readers is “What’s the best way to learn Python?”&lt;/p&gt;
&lt;p&gt;The first step in learning any programming language is making sure that you understand how to learn. Learning how to learn is arguably the most critical skill involved in computer programming.&lt;/p&gt;
&lt;p&gt;Why is knowing how to learn so important? Languages evolve, libraries are created, and tools are upgraded. Knowing how to learn will be essential to keeping up with these changes and becoming a successful programmer.&lt;/p&gt;
&lt;p&gt;In this course, you&amp;rsquo;ll see several learning strategies that will help you jumpstart your journey towards becoming a rockstar Python programmer!&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Dictionaries in Python</title>
      <id>https://realpython.com/courses/dictionaries-python/</id>
      <link href="https://realpython.com/courses/dictionaries-python/"/>
      <updated>2019-07-30T14:00:00+00:00</updated>
      <summary>In this course on Python dictionaries, you&#39;ll cover the basic characteristics of dictionaries and learn how to access and manage dictionary data. Once you&#39;ve finished this course, you&#39;ll have a good sense of when a dictionary is the appropriate data type to use and know how to use it.</summary>
      <content type="html">
        &lt;p&gt;Python provides a composite &lt;a href=&quot;https://realpython.com/python-data-types/&quot;&gt;data type&lt;/a&gt; called a &lt;strong&gt;dictionary&lt;/strong&gt;, which is similar to a &lt;a href=&quot;https://realpython.com/python-lists-tuples/&quot;&gt;list&lt;/a&gt; in that it is a collection of objects.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Here&amp;rsquo;s what you&amp;rsquo;ll learn in this course:&lt;/strong&gt; You&amp;rsquo;ll cover the basic characteristics of Python dictionaries and learn how to access and manage dictionary data.  Once you&amp;rsquo;ve finished this course, you&amp;rsquo;ll have a good sense of when a dictionary is the appropriate data type to use and know how to use it.&lt;/p&gt;
&lt;p&gt;Dictionaries and lists share the following characteristics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Both are mutable.&lt;/li&gt;
&lt;li&gt;Both are dynamic.  They can grow and shrink as needed.&lt;/li&gt;
&lt;li&gt;Both can be nested. A list can contain another list. A dictionary can contain another dictionary.  A dictionary can also contain a list, and vice versa.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Dictionaries differ from lists primarily in how elements are accessed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;List elements are accessed by their position in the list, via indexing.&lt;/li&gt;
&lt;li&gt;Dictionary elements are accessed via keys.&lt;/li&gt;
&lt;/ul&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Logging in Python</title>
      <id>https://realpython.com/courses/logging-python/</id>
      <link href="https://realpython.com/courses/logging-python/"/>
      <updated>2019-07-23T14:00:00+00:00</updated>
      <summary>In this video course, you&#39;ll learn why and how to get started with Python&#39;s powerful logging module to meet the needs of beginners and enterprise teams alike.</summary>
      <content type="html">
        &lt;p&gt;Logging is a very useful tool in a programmer’s toolbox. It can help you develop a better understanding of the flow of a program and discover scenarios that you might not even have thought of while developing.&lt;/p&gt;
&lt;p&gt;Logs provide developers with an extra set of eyes that are constantly looking at the flow that an application is going through. They can store information, like which user or IP accessed the application. If an error occurs, then they can provide more insights than a stack trace by telling you what the state of the program was before it arrived at the line of code where the error occurred.&lt;/p&gt;
&lt;p&gt;By logging useful data from the right places, you can not only debug errors easily but also use the data to analyze the performance of the application to plan for scaling or look at usage patterns to plan for marketing.&lt;/p&gt;
&lt;p&gt;Python provides a logging system as a part of its standard library, so you can quickly add logging to your application. In this course, you&amp;rsquo;ll learn why using this module is the best way to add logging to your application as well as how to get started quickly, and you will get an introduction to some of the advanced features available.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  

</feed>
