<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Wilfredinni</title>
    <description>The latest articles on DEV Community by Wilfredinni (@wilfredinni).</description>
    <link>https://dev.to/wilfredinni</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F174306%2F3774c3ba-087e-4270-a193-612e161d7bc9.jpeg</url>
      <title>DEV Community: Wilfredinni</title>
      <link>https://dev.to/wilfredinni</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wilfredinni"/>
    <language>en</language>
    <item>
      <title>Python projects with Poetry and VSCode. Part 3</title>
      <dc:creator>Wilfredinni</dc:creator>
      <pubDate>Tue, 24 Sep 2019 20:52:30 +0000</pubDate>
      <link>https://dev.to/wilfredinni/python-projects-with-poetry-and-vscode-part-3-1ojl</link>
      <guid>https://dev.to/wilfredinni/python-projects-with-poetry-and-vscode-part-3-1ojl</guid>
      <description>&lt;p&gt;In the &lt;a href="https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-1" rel="noopener noreferrer"&gt;First Part&lt;/a&gt; we:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Started a new project.&lt;/li&gt;
&lt;li&gt;Created a Virtual Environment.&lt;/li&gt;
&lt;li&gt;Managed dependencies.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the &lt;a href="https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-2" rel="noopener noreferrer"&gt;Second Part&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Added our virtual Environment to &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;VSCode&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Integrated our dev dependencies with the editor:

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Flake8&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Black&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Pytest&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;And finally, in this third and last part we'll:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write a sample library.&lt;/li&gt;
&lt;li&gt;Build our project with &lt;em&gt;Poetry&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Publish it on &lt;em&gt;PyPI&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Poetry Commands
&lt;/h2&gt;

&lt;p&gt;Here is a table with the commands used in this series as well as their descriptions. For a full list read the &lt;a href="https://poetry.eustace.io/docs/cli/" rel="noopener noreferrer"&gt;Poetry Documentation&lt;/a&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry new [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Start a new Python Project.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry init&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Create a &lt;em&gt;pyproject.toml&lt;/em&gt; file interactively.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry install&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Install the packages inside the &lt;em&gt;pyproject.toml&lt;/em&gt; file.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry add [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Add a package to a Virtual Environment.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry add -D [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Add a dev package to a Virtual Environment.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry remove [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove a package from a Virtual Environment.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry remove -D [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove a dev package from a Virtual Environment.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry update&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Get the latest versions of the dependencies&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry shell&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Spawns a shell within the virtual environment.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry build&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;builds the source and wheels archives.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry publish&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Publish the package to PyPi.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry publish --build&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Build and publish a package.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry self:update&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Update poetry to the latest stable version.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  The Project
&lt;/h2&gt;

&lt;p&gt;You can download the source code from &lt;a href="https://github.com/wilfredinni/how-long" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; if you want, but as mentioned earlier, this will be a very simple decorator that the only thing it will do is print to the console how long it takes for a function to run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;how_long&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt;

&lt;span class="nd"&gt;@timer&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

&lt;span class="nf"&gt;test_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# Execution Time: 955 ms.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the end, the project directory will look, more or less, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;how-long
├── how_long
│   ├── how_long.py
│   └── __init__.py
├── how_long.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── requires.txt
│   ├── SOURCES.txt
│   └── top_level.txt
├── LICENSE
├── poetry.lock
├── pyproject.toml
├── README.rst
└── tests
    ├── __init__.py
    └── test_how_long.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before we start, check for package updates with the &lt;code&gt;poetry update&lt;/code&gt; command:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FMay%2Fpoetry_vscode_p3%2Fimg%2Fpoetry_update.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FMay%2Fpoetry_vscode_p3%2Fimg%2Fpoetry_update.png" alt="poetry update"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, now add a short description of the project in the &lt;code&gt;README.rst&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;how_long
========

Simple Decorator to measure a function execution time.

Example
_______

.. code-block:: python

    from how_long import timer


    @timer
    def some_function():
        return [x for x in range(10_000_000)]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Navigate to &lt;code&gt;how_long/how_long.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# how_long.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;wraps&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pendulum&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Simple Decorator to measure a function execution time.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

    &lt;span class="nd"&gt;@wraps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;function_wrapper&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pendulum&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;ellapsed_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pendulum&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Execution Time: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ellapsed_time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;microseconds&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; ms.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;function_wrapper&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;how_long/__init__.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;.how_long&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt;

&lt;span class="n"&gt;__version__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0.1.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally, the &lt;code&gt;tests/test_how_long.py&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;how_long&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;__version__&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;how_long&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_version&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;__version__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0.1.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_wrap&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nd"&gt;@timer&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wrapped_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;

    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;wrapped_function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;wrapped_function&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now use &lt;code&gt;poetry install&lt;/code&gt; on your terminal to install and prove your package locally. Activate your virtual environment if you haven't and in the Python interactive shell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;how_long&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;@timer&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="bp"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;test_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;Execution&lt;/span&gt; &lt;span class="n"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;705&lt;/span&gt; &lt;span class="n"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-2#Pytest" rel="noopener noreferrer"&gt;Run the tests&lt;/a&gt; and if everything is fine, move on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building and Publishing
&lt;/h2&gt;

&lt;p&gt;Finally, the time to make this project available to the world has come! First, make sure you have an account on &lt;a href="https://pypi.or" rel="noopener noreferrer"&gt;Pypi&lt;/a&gt;, and if not, &lt;a href="https://pypi.org/account/register/" rel="noopener noreferrer"&gt;register&lt;/a&gt; one. Remember that the package name must be unique, if unsure go and use the &lt;a href="https://pypi.org/search/?q=" rel="noopener noreferrer"&gt;search&lt;/a&gt; to check it out.&lt;/p&gt;

&lt;h3&gt;
  
  
  Build
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;poetry build&lt;/code&gt; command builds the source and &lt;a href="https://pythonwheels.com/" rel="noopener noreferrer"&gt;wheels&lt;/a&gt; archives that will letter be uploaded as the source of the project:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FMay%2Fpoetry_vscode_p3%2Fimg%2Fpoetry_build.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FMay%2Fpoetry_vscode_p3%2Fimg%2Fpoetry_build.png" alt="poetry build"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;how_long.egg-info&lt;/em&gt; directory will be created.&lt;/p&gt;

&lt;h3&gt;
  
  
  Publish
&lt;/h3&gt;

&lt;p&gt;This command publishes the package to &lt;em&gt;Pypi&lt;/em&gt; and automatically register it before uploading if this is the first time it is submitted:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FMay%2Fpoetry_vscode_p3%2Fimg%2Fpoetry_publish.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FMay%2Fpoetry_vscode_p3%2Fimg%2Fpoetry_publish.png" alt="poetry publish"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can also build and publish your project with &lt;code&gt;$ poetry publish --build&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Enter your credentials and if everything is ok, &lt;a href="https://pypi.org/project/how-long/" rel="noopener noreferrer"&gt;browse&lt;/a&gt; your project and you'll see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FMay%2Fpoetry_vscode_p3%2Fimg%2Fpypi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FMay%2Fpoetry_vscode_p3%2Fimg%2Fpypi.png" alt="pipy how-long"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can now let others know that they can &lt;code&gt;pip install how-long&lt;/code&gt; from any machine, anywhere!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I remember the first time I tried to publish a package, and it was a nightmare. I was just starting in Python and I have to spend a "few hours" trying to understand what the &lt;code&gt;setup.py&lt;/code&gt; file was and how to use it. In the end, I ended up with several different files: a &lt;code&gt;Makefile&lt;/code&gt;, a &lt;code&gt;MANIFEST.in&lt;/code&gt;, a &lt;code&gt;requirements.txt&lt;/code&gt; and a &lt;code&gt;test_requirements.txt&lt;/code&gt;. That's why the words of &lt;a href="https://github.com/sdispater" rel="noopener noreferrer"&gt;Sébastien Eustace&lt;/a&gt;, the creator of &lt;a href="https://github.com/sdispater/poetry" rel="noopener noreferrer"&gt;Poetry&lt;/a&gt;, made a lot of sense to me:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Packaging and dependency management in Python are rather convoluted and hard to understand for newcomers. Even for seasoned developers it might be cumbersome at times to create all files needed in a Python project: &lt;code&gt;setup.py&lt;/code&gt;, &lt;code&gt;requirements.txt&lt;/code&gt;, &lt;code&gt;setup.cfg&lt;/code&gt;, &lt;code&gt;MANIFEST.in&lt;/code&gt; and the newly added &lt;code&gt;Pipfile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So I wanted a tool that would limit everything to a single configuration file to do: dependency management, packaging and publishing.&lt;/p&gt;

&lt;p&gt;It takes inspiration in tools that exist in other languages, like &lt;code&gt;composer&lt;/code&gt; (PHP) or &lt;code&gt;cargo&lt;/code&gt; (Rust).&lt;/p&gt;

&lt;p&gt;And, finally, there is no reliable tool to properly resolve dependencies in Python, so I started &lt;code&gt;poetry&lt;/code&gt; to bring an exhaustive dependency resolver to the Python community.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Poetry is by &lt;a href="https://frostming.com/2019/01-04/pipenv-poetry#what-about-poetry" rel="noopener noreferrer"&gt;no means perfect&lt;/a&gt; but, unlike other tools, it really does what promises.&lt;/p&gt;

</description>
      <category>python</category>
      <category>packaging</category>
      <category>poetry</category>
      <category>pypi</category>
    </item>
    <item>
      <title>Python projects with Poetry and VSCode. Part 2</title>
      <dc:creator>Wilfredinni</dc:creator>
      <pubDate>Sun, 25 Aug 2019 22:20:17 +0000</pubDate>
      <link>https://dev.to/wilfredinni/python-projects-with-poetry-and-vscode-part-2-596b</link>
      <guid>https://dev.to/wilfredinni/python-projects-with-poetry-and-vscode-part-2-596b</guid>
      <description>&lt;p&gt;In the &lt;a href="https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-1" rel="noopener noreferrer"&gt;First Part&lt;/a&gt;, we learned what the &lt;code&gt;pyproject.toml&lt;/code&gt; file is and how to work with it, used &lt;a href="https://poetry.eustace.io/" rel="noopener noreferrer"&gt;Poetry&lt;/a&gt; to start a new project, create a Virtual Environment and to add and remove dependencies. All of that with the following commands:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry new [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Start a new Python Project.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry init&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Create a &lt;em&gt;pyproject.toml&lt;/em&gt; file interactively.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry install&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Install the packages inside the &lt;em&gt;pyproject.toml&lt;/em&gt; file.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry add [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Add a package to a Virtual Environment.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry add -D [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Add a dev package to a Virtual Environment.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry remove [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove a package from a Virtual Environment.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry remove -D [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove a dev package from a Virtual Environment.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In this second part we'll:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add our virtual Environment to &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;VSCode&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Update our dependencies.&lt;/li&gt;
&lt;li&gt;Integrate our dev dependencies with the editor:

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Flake8&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Black&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Pytest&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;And in the third part we will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write a sample library.&lt;/li&gt;
&lt;li&gt;Build our project with &lt;em&gt;Poetry&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Publish it on &lt;em&gt;PyPI&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before we start, make sure you have installed &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;VSCode&lt;/a&gt;, added the &lt;a href="https://marketplace.visualstudio.com/itemdetails?itemName=ms-python.python" rel="noopener noreferrer"&gt;Python&lt;/a&gt; extension and that you have followed and/or understood the &lt;a href="https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-1" rel="noopener noreferrer"&gt;First Part&lt;/a&gt; of this series.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up Poetry on VSCode
&lt;/h2&gt;

&lt;p&gt;A few days have passed since the first part, so it may be a good idea to check for new versions of our dependencies. Open your terminal and navigate inside your project folder and type the &lt;code&gt;poetry update&lt;/code&gt; command:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fupdate.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fupdate.png" alt="poetry update"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, to this day there are no new versions available.&lt;/p&gt;

&lt;p&gt;When you create a Virtual Environment with the &lt;em&gt;venv&lt;/em&gt; command, &lt;em&gt;VSCode&lt;/em&gt; will automatically set it as the default Python Environment for that project. When working With &lt;em&gt;Poetry&lt;/em&gt;, the first time we will need to type the following in the terminal and inside the project folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ poetry shell
$ code .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first command, &lt;code&gt;poetry shell&lt;/code&gt;, will spawn us inside our virtual environment, and &lt;code&gt;code .&lt;/code&gt; will open the current folder inside &lt;em&gt;VSCode&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fvscode.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fvscode.png" alt="vscode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open the &lt;strong&gt;how-long&lt;/strong&gt; folder (or the one with your project name) using the left panel and alongside to &lt;code&gt;__init__.py&lt;/code&gt;, create a &lt;code&gt;how-long.py&lt;/code&gt; file. In the bottom left corner you'll see the current Python Environment:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fpython-code.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fpython-code.png" alt="python version"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click it and a list of available Environments will display. Choose the one that has the name of your project in it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fchoose-environment.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fchoose-environment.png" alt="choose python"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's integrate our dev dependencies, &lt;em&gt;Flake8&lt;/em&gt;, &lt;em&gt;Black&lt;/em&gt;, and &lt;em&gt;Pytest&lt;/em&gt; into Visual Studio Code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Flake8
&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://flake8.pycqa.org/en/latest/" rel="noopener noreferrer"&gt;Flake8&lt;/a&gt; will provide our projects with &lt;em&gt;linting&lt;/em&gt; capabilities. In other words, warning of syntax and style errors, and thanks to VSCode, we will know them as we type.&lt;/p&gt;

&lt;p&gt;By default, the Python extension comes with &lt;em&gt;Pylint&lt;/em&gt; enabled, which is powerful but complex to configure. To switch to &lt;em&gt;Flake8&lt;/em&gt; make a change to any Python file and save it, in the bottom right corner a popup message will show:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fselect-linter.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fselect-linter.png" alt="flake8"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on &lt;strong&gt;Select Linter&lt;/strong&gt; and choose &lt;strong&gt;Flake8&lt;/strong&gt; from the list. Now, &lt;em&gt;VSCode&lt;/em&gt; will tell us our &lt;em&gt;syntax&lt;/em&gt; and &lt;em&gt;style&lt;/em&gt; problems, in green or red depending on its severity, always with a nice description of what is wrong:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Flinting.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Flinting.png" alt="linting"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Seems like we have two problems: we are missing a blank line at the end of our file (style) and forgot to add quotes to our &lt;em&gt;Hello, World!&lt;/em&gt; string (syntax). Fix them and see all warnings disappear.&lt;/p&gt;

&lt;h2&gt;
  
  
  Black
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/ambv/black" rel="noopener noreferrer"&gt;Black&lt;/a&gt; is a code formatter, a tool that will look at our code and automatically format it in compliance with the &lt;a href="https://www.python.org/dev/peps/pep-0008/" rel="noopener noreferrer"&gt;PEP 8&lt;/a&gt; style guide, the same &lt;em&gt;PEP&lt;/em&gt; that uses &lt;em&gt;Flake8&lt;/em&gt; to lint our style errors.&lt;/p&gt;

&lt;p&gt;Hold &lt;code&gt;shift + cmd/ctrl + p&lt;/code&gt; to open the Command Palette, type &lt;strong&gt;Format Document&lt;/strong&gt;, and press enter. A new popup message will appear:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fformat-popup.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fformat-popup.png" alt="black formatter popup"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select &lt;strong&gt;Use Black&lt;/strong&gt;. Now copy this poorly formatted code into your python file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;         &lt;span class="c1"&gt;# this comment has too many spaces
&lt;/span&gt;      &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# this line has 6 space indentation.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What an ugly piece of s**&lt;em&gt;... code. Try formatting it again and see how *Black&lt;/em&gt; fixes all of them for you!&lt;/p&gt;

&lt;p&gt;Another thing we can do is configure VSCode so that every time we save, &lt;em&gt;Black&lt;/em&gt; will automatically format our code. Hold &lt;code&gt;cmd/ctrl + ,&lt;/code&gt; to open the Settings. Make sure you are in the &lt;strong&gt;Workspace Settings&lt;/strong&gt;, search for &lt;strong&gt;Format On Save&lt;/strong&gt; and activate the checkbox:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fformat-on-save.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fformat-on-save.png" alt="format on save"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lastly, &lt;em&gt;Black&lt;/em&gt; defaults to 88 characters per line in contrast with the 80 allowed by &lt;em&gt;Flake8&lt;/em&gt;, so to avoid conflicts, open the &lt;strong&gt;.vscode&lt;/strong&gt; folder and add the following at the end of the &lt;strong&gt;settings.json&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"python.linting.flake8Args"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"--max-line-length=88"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fblack-settings.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fblack-settings.png" alt="black-settings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pytest
&lt;/h2&gt;

&lt;p&gt;If you are serious about programming, it is crucial for you to learn how to test your projects. It's an incredibly useful skill that will allow you to write and deliver programs with confidence by reducing the possibility of catastrophic bugs appearing after shipping.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.pytest.org/en/latest/" rel="noopener noreferrer"&gt;Pytest&lt;/a&gt; is a very popular and user-friendly framework for writing tests. We &lt;a href="https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-1#Dependency-Management" rel="noopener noreferrer"&gt;already installed it&lt;/a&gt; so we will also integrate it with &lt;em&gt;VSCode&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Open the &lt;strong&gt;tests&lt;/strong&gt; folder and select the &lt;code&gt;test_how_long.py&lt;/code&gt; file. &lt;em&gt;Poetry&lt;/em&gt; already gives us our first test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# test_how_long.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;how_long&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;__version__&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_version&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;__version__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;0.1.0&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This test import the &lt;code&gt;__version__&lt;/code&gt; variable from the &lt;code&gt;__init__.py&lt;/code&gt; file that is inside the &lt;strong&gt;how_long&lt;/strong&gt; folder and asserts that the current version is &lt;em&gt;0.1.0&lt;/em&gt;. Open the integrated terminal by going to &lt;strong&gt;Terminal &amp;gt; New Terminal&lt;/strong&gt; and type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pytest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Output will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fpytest-terminal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Fpytest-terminal.png" alt="pytest"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, everything is fine. Open your Command Palette with &lt;code&gt;shift + cmd/ctrl + p&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write &lt;strong&gt;unit&lt;/strong&gt; and select &lt;strong&gt;Python: Configure Unit Tests&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;pytest&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Choose the directory in which the tests are stored, &lt;strong&gt;tests&lt;/strong&gt; in our case.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Three things happened:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A new button appeared at the status bar: &lt;strong&gt;Run Tests&lt;/strong&gt;. This is the same as typing &lt;em&gt;pytest&lt;/em&gt; in the terminal. Press it and select &lt;strong&gt;Run All Unit Tests&lt;/strong&gt;. When finished, it will inform you the number of tests that passed and the tests that not:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Ftest-statusbar.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Ftest-statusbar.png" alt="test status bar"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A new icon at the left bar. If you click on it a panel displaying all the test will appear. Here, you can run each one individually:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Ftest-side-panel.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Ftest-side-panel.png" alt="test side panel"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Inside the test file, new options will be displayed before every test function: a check icon will appear if is ok, and an &lt;em&gt;x&lt;/em&gt; otherwise. It also allows you to run specific tests:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Ftest-inline.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fwilfredinni%2FpysheetComments%2Fmaster%2F2019%2FApril%2Fpoetry_vscode_p2%2Fimg%2Ftest-inline.png" alt="test inline"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So far we have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-1#Starting-a-New-Project" rel="noopener noreferrer"&gt;Started a new project&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-1#Creating-a-Virtual-Environment" rel="noopener noreferrer"&gt;Created a Virtual Environment&lt;/a&gt; with &lt;em&gt;Poetry&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-1#Dependency-Management" rel="noopener noreferrer"&gt;Added, Deleted and Updated&lt;/a&gt; dependencies.&lt;/li&gt;
&lt;li&gt;Added our Virtual Environment to VSCode.&lt;/li&gt;
&lt;li&gt;Configured &lt;em&gt;Flake8&lt;/em&gt; to &lt;em&gt;lint&lt;/em&gt; our code as we type.&lt;/li&gt;
&lt;li&gt;Choose &lt;em&gt;Black&lt;/em&gt; as the formatter of our project.&lt;/li&gt;
&lt;li&gt;And &lt;em&gt;Pytest&lt;/em&gt; to run our tests in a visual way.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the &lt;a href="https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-3" rel="noopener noreferrer"&gt;third&lt;/a&gt; and last part we will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write a sample library.&lt;/li&gt;
&lt;li&gt;Build our project with &lt;em&gt;Poetry&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Publish it on &lt;em&gt;PyPI&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it, Have a nice day!&lt;/p&gt;

</description>
      <category>python</category>
      <category>packaging</category>
      <category>poetry</category>
      <category>pypi</category>
    </item>
    <item>
      <title>pythoncheatsheet.org</title>
      <dc:creator>Wilfredinni</dc:creator>
      <pubDate>Sat, 20 Jul 2019 19:48:21 +0000</pubDate>
      <link>https://dev.to/wilfredinni/pythoncheatsheet-org-36g4</link>
      <guid>https://dev.to/wilfredinni/pythoncheatsheet-org-36g4</guid>
      <description>&lt;p&gt;Anyone can forget how to make &lt;a href="https://www.pythoncheatsheet.org/#Making-Your-Own-Character-Classes"&gt;character classes for a regex&lt;/a&gt;, &lt;a href="https://www.pythoncheatsheet.org/#Getting-Sublists-with-Slices"&gt;slice a list&lt;/a&gt; or do a &lt;a href="https://www.pythoncheatsheet.org/#Using-for-Loops-with-Lists"&gt;for loop&lt;/a&gt;. This Python cheat sheet tries to provide a basic reference for beginner and advanced developers, lower the entry barrier for newcomers and help veterans refresh the old tricks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pythoncheatsheet.org/"&gt;pythoncheatsheet.org&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, check out the &lt;a href="https://javascript.pythoncheatsheet.org/"&gt;Javascript Cheatsheet&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>python</category>
      <category>cheatsheet</category>
    </item>
    <item>
      <title>Python projects with Poetry and VSCode. Part 1</title>
      <dc:creator>Wilfredinni</dc:creator>
      <pubDate>Mon, 01 Jul 2019 15:29:06 +0000</pubDate>
      <link>https://dev.to/wilfredinni/https-pythoncheatsheet-org-blog-python-projects-with-poetry-and-vscode-part-1-3pm5</link>
      <guid>https://dev.to/wilfredinni/https-pythoncheatsheet-org-blog-python-projects-with-poetry-and-vscode-part-1-3pm5</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Updates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;18-05-2019: Installing Poetry.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;A Virtual Environment is an isolated Python installation designed to avoid filling our base one with libraries we might use for only one project. It also allows us to manage multiple versions of the same package in different projects. We could, for example, need Django 2.2 for one and 1.9 in other.&lt;/p&gt;

&lt;p&gt;Poetry on the other hand:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;poetry&lt;/code&gt; is a tool to handle dependency installation as well as building and packaging of Python packages. It only needs one file to do all of that: the new, &lt;a href="https://www.python.org/dev/peps/pep-0518/" rel="noopener noreferrer"&gt;standardized&lt;/a&gt; &lt;code&gt;pyproject.toml&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In other words, poetry uses &lt;code&gt;pyproject.toml&lt;/code&gt; to replace &lt;code&gt;setup.py&lt;/code&gt;, &lt;code&gt;requirements.txt&lt;/code&gt;, &lt;code&gt;setup.cfg&lt;/code&gt;, &lt;code&gt;MANIFEST.in&lt;/code&gt; and the newly added &lt;code&gt;Pipfile&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this series of articles, we'll use &lt;a href="https://poetry.eustace.io/" rel="noopener noreferrer"&gt;Poetry&lt;/a&gt; to manage our dependencies, build a simple project and, with a single command, publish it on &lt;a href="https://pypi.org/" rel="noopener noreferrer"&gt;PyPI&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this first part we will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start a new project.&lt;/li&gt;
&lt;li&gt;Create a Virtual Environment.&lt;/li&gt;
&lt;li&gt;Manage dependencies.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the &lt;a href="https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-2" rel="noopener noreferrer"&gt;Second Part&lt;/a&gt; we'll:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add our virtual Environment to &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;VSCode&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Integrate our dev dependencies with the editor.

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Flake8&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Black&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Pytest&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;And finally, in a &lt;a href="https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-3" rel="noopener noreferrer"&gt;third&lt;/a&gt; part we'll:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write a sample library.&lt;/li&gt;
&lt;li&gt;Build our project with &lt;em&gt;Poetry&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Publish it on &lt;em&gt;PyPI&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installing Poetry
&lt;/h2&gt;

&lt;p&gt;The easiest way is to use &lt;em&gt;pip&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pip install poetry
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But we will use Poetry own installer to isolate it from the rest of the system by vendorizing its dependencies. This is the recommended way of installing poetry:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ curl -SSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If installed this way, you will later be able to update poetry to the latest stable version with the &lt;code&gt;poetry self:update&lt;/code&gt; command.&lt;/p&gt;

&lt;h2&gt;
  
  
  Starting a New Project
&lt;/h2&gt;

&lt;p&gt;We can now start a new Python project by using the &lt;code&gt;poetry new [project_name]&lt;/code&gt; command. I will call it &lt;strong&gt;&lt;em&gt;how-long&lt;/em&gt;&lt;/strong&gt; and is going to be a very simple library to measure a function execution time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ poetry new how-long
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: For existing projects, you can use the &lt;code&gt;poetry init&lt;/code&gt; command and interactively create a &lt;em&gt;pyproject.toml&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The directory &lt;em&gt;how-long&lt;/em&gt; is created and inside is a basic project structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;how-long
├── README.rst
├── how_long
│   └── __init__.py
├── pyproject.toml
└── tests
    ├── __init__.py
    └── test_how_long.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: To be able to publish your project, you need an available name. Use the &lt;a href="https://pypi.org/" rel="noopener noreferrer"&gt;PyPI&lt;/a&gt; search tool for this.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The pyproject.toml File
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;pyproject.toml&lt;/strong&gt; file will manage the details and dependencies of the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[tool.poetry]
name = "how-long"
version = "0.1.0"
description = "A simple decorator to measure a function execution time."
authors = ["wilfredinni &amp;lt;carlos.w.montecinos@gmail.com&amp;gt;"]

[tool.poetry.dependencies]
python = "^3.7"

[tool.poetry.dev-dependencies]
pytest = "^3.0"

[build-system]
requires = ["poetry&amp;gt;=0.12"]
build-backend = "poetry.masonry.api"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  [tool.poetry]
&lt;/h4&gt;

&lt;p&gt;The details. Adding a &lt;a href="https://poetry.eustace.io/docs/pyproject/#license" rel="noopener noreferrer"&gt;license&lt;/a&gt; and a &lt;a href="https://poetry.eustace.io/docs/pyproject/#readme" rel="noopener noreferrer"&gt;Readme&lt;/a&gt; might be a good idea:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[tool.poetry]
...
license = "MIT"
readme = "README.rst"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  [tool.poetry.dependencies]
&lt;/h4&gt;

&lt;p&gt;First is the Python version. Basically, this project will be compatible with Python 3.7 and up. Also, from now on, every package we install that is meant to be used in production will be listed here.&lt;/p&gt;

&lt;h4&gt;
  
  
  [tool.poetry.dev-dependencies]
&lt;/h4&gt;

&lt;p&gt;These packages are only for development and will not be included when we publish our project. By default Poetry includes &lt;a href="https://docs.pytest.org/en/latest/" rel="noopener noreferrer"&gt;Pytest&lt;/a&gt;, so we will use it to test our project later on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Virtual Environment
&lt;/h2&gt;

&lt;p&gt;Now, let's create a Virtual Environment and install &lt;em&gt;Pytest&lt;/em&gt; with the &lt;code&gt;poetry install&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ poetry install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmf61b1uxa9qmt2ikbojl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmf61b1uxa9qmt2ikbojl.png" alt="poetry-install-command" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After is done, a new file, &lt;code&gt;poetry.lock&lt;/code&gt; will be created.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When Poetry has finished installing, it writes all of the packages and the exact versions of them that it downloaded to the poetry.lock file, locking the project to those specific versions. You should commit the poetry.lock file to your project repo so that all people working on the project are locked to the same versions of dependencies.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Dependency Management
&lt;/h2&gt;

&lt;p&gt;One way to add or remove dependencies is to directly edit &lt;em&gt;pyproject.toml&lt;/em&gt; and then run &lt;code&gt;poetry install&lt;/code&gt; to apply the changes. We will instead use the &lt;code&gt;add&lt;/code&gt; and &lt;code&gt;remove&lt;/code&gt; commands to avoid manual modifications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Dependencies
&lt;/h3&gt;

&lt;p&gt;Let's add two packages to the project, &lt;em&gt;pendulum&lt;/em&gt; and &lt;em&gt;coo&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ poetry add pendulum coo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frwujei7ptnqqh8oto8pb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frwujei7ptnqqh8oto8pb.png" alt="poetry-add-command" width="800" height="562"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open &lt;em&gt;pyproject.toml&lt;/em&gt; and &lt;em&gt;poetry.lock&lt;/em&gt; and see how they have updated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Dev Dependencies
&lt;/h3&gt;

&lt;p&gt;These dependencies will be available only during development, Poetry will not include them when building and publishing the project.&lt;/p&gt;

&lt;p&gt;We already installed &lt;em&gt;Pytest&lt;/em&gt;, but we will also use &lt;a href="http://flake8.pycqa.org/en/latest/" rel="noopener noreferrer"&gt;flake8&lt;/a&gt; for linting and &lt;a href="http://mypy-lang.org/" rel="noopener noreferrer"&gt;mypy&lt;/a&gt; for static typing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ poetry add -D flake8 mypy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that I think about it, I forgot to add a formatter. We'll go with &lt;a href="https://black.readthedocs.io/en/stable/" rel="noopener noreferrer"&gt;black&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ poetry add -D black
[ValueError]
Could not find a matching version of package black

add [-D|--dev] [--git GIT] [--path PATH] [-E|--extras EXTRAS] [--optional] [--python PYTHON] [--platform PLATFORM] [--allow-prereleases] [--dry-run] [--] &amp;lt;name&amp;gt; (&amp;lt;name&amp;gt;)...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This error happens because &lt;em&gt;black&lt;/em&gt; is in a pre-release state, so Poetry cannot find any stable version for us. But I really want it so let's install it anyway using the &lt;code&gt;--allow-prereleases&lt;/code&gt; flag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ poetry add -D black --allow-prereleases
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgh57y5y9970fogf01zfn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgh57y5y9970fogf01zfn.png" alt="poetry-add-dev-command" width="800" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Removing Dependencies
&lt;/h3&gt;

&lt;p&gt;You know what, I changed my mind, this project will use nor &lt;em&gt;coo&lt;/em&gt; nor &lt;em&gt;mypy&lt;/em&gt;. Start by removing &lt;em&gt;coo&lt;/em&gt;, a normal dependency of our project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ poetry remove coo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;em&gt;mypy&lt;/em&gt; which is a dev dependency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ poetry remove -D mypy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this first part, we have started a new project, created a Virtual Environment and added and removed dependencies by using the following commands:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry new [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Start a new Python Project.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry init&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Create a &lt;em&gt;pyproject.toml&lt;/em&gt; file interactively.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry install&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Install the packages inside the &lt;em&gt;pyproject.toml&lt;/em&gt; file.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry add [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Add a package to a Virtual Environment.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry add -D [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Add a dev package to a Virtual Environment.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry remove [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove a package from a Virtual Environment.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry remove -D [package-name]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove a dev package from a Virtual Environment.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;poetry self:update&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Update poetry to the latest stable version.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In the &lt;a href="https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-2" rel="noopener noreferrer"&gt;Second Part&lt;/a&gt;, we will see more &lt;em&gt;Poetry&lt;/em&gt; commands, add our Virtual Environment to &lt;em&gt;VSCode&lt;/em&gt; and use the dev packages we installed to lint (Flake8), format (Black) and test (Pytest) our code inside the editor. Finally, in a third one, we will write and publish a sample library to &lt;em&gt;PyPI&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Any doubt or suggestion? Please leave a comment.&lt;/p&gt;

</description>
      <category>python</category>
      <category>packaging</category>
      <category>pypi</category>
      <category>poetry</category>
    </item>
    <item>
      <title>Python *args and **kwargs Made Easy</title>
      <dc:creator>Wilfredinni</dc:creator>
      <pubDate>Tue, 11 Jun 2019 17:20:17 +0000</pubDate>
      <link>https://dev.to/wilfredinni/python-args-and-kwargs-made-easy-29ef</link>
      <guid>https://dev.to/wilfredinni/python-args-and-kwargs-made-easy-29ef</guid>
      <description>&lt;p&gt;I do not know about you, but every time I saw some function with &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; as parameters, I'd get a little scared. I've even "used" them while doing some backend work with Django without understanding a thing. If you're a self-taught developer like me, I know you've been there too.&lt;/p&gt;

&lt;p&gt;A few months ago I decided to stop being lazy and started to research it. To my surprise, they were very easy to grasp when playing with the interpreter but not so much when reading about them. I wrote this post trying to explain &lt;a href="https://www.pythoncheatsheet.org/#args-and-kwargs"&gt;args and kwargs&lt;/a&gt; the way I would have liked someone explained them to me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basics
&lt;/h2&gt;

&lt;p&gt;The first thing you need to know is that &lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; lets you pass an undefined number of &lt;code&gt;arguments&lt;/code&gt; and &lt;code&gt;keywords&lt;/code&gt; when calling a &lt;a href="https://www.pythoncheatsheet.org/#Functions"&gt;function&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="c1"&gt;# call some_function with any number of arguments
&lt;/span&gt;&lt;span class="n"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# call some_function with any number of keywords
&lt;/span&gt;&lt;span class="n"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;arg2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key3&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;arg3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# call both, arguments and keywords
&lt;/span&gt;&lt;span class="n"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# or none
&lt;/span&gt;&lt;span class="n"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second, the words &lt;code&gt;args&lt;/code&gt; and &lt;code&gt;kwargs&lt;/code&gt; are conventions. This means they are not imposed by the interpreter, but considered good practice among the Python community:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# This function would work just fine
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;keywords&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;A note about conventions:&lt;/p&gt;

&lt;p&gt;Even if the above function works, don't do it. Conventions are there to help you write readable code for you and anyone that might be interested in your project.&lt;/p&gt;

&lt;p&gt;Other conventions include the 4 space indentation, comments, and imports. Reading the &lt;a href="https://www.python.org/dev/peps/pep-0008/"&gt;PEP 8 -- Style Guide for Python Code&lt;/a&gt; is highly recommended.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, how does Python know that we want our function to accept multiple arguments and/or keywords? Yes, the answers are the &lt;code&gt;*&lt;/code&gt; and &lt;code&gt;**&lt;/code&gt; operators.&lt;/p&gt;

&lt;p&gt;Now that we have covered the basics, let's work with them 👊.&lt;/p&gt;

&lt;h2&gt;
  
  
  args
&lt;/h2&gt;

&lt;p&gt;We now know how to pass multiple arguments using &lt;code&gt;*args&lt;/code&gt; as a parameter to our functions, but how do we work with them? It's easy: all the arguments are contained within the &lt;code&gt;args&lt;/code&gt; variable as a &lt;a href="https://www.pythoncheatsheet.org/#Tuple-Data-Type"&gt;tuple&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;'Arguments passed: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; as &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'arg1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'arg2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'arg3'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# Arguments passed: ('arg1', 'arg2', 'arg3') as &amp;lt;class 'tuple'&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can iterate over them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'arg1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'arg2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'arg3'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# arg1
# arg2
# arg3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Access the elements with an index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;


&lt;span class="n"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'arg1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'arg2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'arg3'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# arg2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Slice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;


&lt;span class="n"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'arg1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'arg2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'arg3'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# ('arg1', 'arg2')
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whatever you do with a &lt;a href="https://www.pythoncheatsheet.org/#Tuple-Data-Type"&gt;tuple&lt;/a&gt;, you can do it with &lt;code&gt;args&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  kwargs
&lt;/h2&gt;

&lt;p&gt;While arguments are stored in the args variable, keywords are within &lt;code&gt;kwargs&lt;/code&gt;, but this time as a &lt;a href="https://www.pythoncheatsheet.org/#Dictionaries-and-Structuring-Data"&gt;dictionary&lt;/a&gt; where the key is the keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;'keywords: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; as &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'arg1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'arg2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key3&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'arg3'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# keywords: {'key1': 'arg1', 'key2': 'arg2', 'key3': 'arg3'} as &amp;lt;class 'dict'&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, we can do with &lt;code&gt;kwargs&lt;/code&gt; the same we would do with any &lt;a href="https://www.pythoncheatsheet.org/#Dictionaries-and-Structuring-Data"&gt;dictionary&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Iterate over:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'arg1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'arg2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key3&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'arg3'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# key1: arg1
# key2: arg2
# key3: arg3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the &lt;code&gt;get()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;


&lt;span class="n"&gt;some_function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'key3'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'arg1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'arg2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key3&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'arg3'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# arg3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And a lot &lt;a href="https://www.pythoncheatsheet.org/#Dictionaries-and-Structuring-Data"&gt;more&lt;/a&gt; =).&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;*args&lt;/code&gt; and &lt;code&gt;**kwargs&lt;/code&gt; may seem scary, but the truth is that they are not that difficult to grasp and have the power to grant your functions with flexibility and readability. If you know about &lt;a href="https://www.pythoncheatsheet.org/#Tuple-Data-Type"&gt;tuples&lt;/a&gt; and &lt;a href="https://www.pythoncheatsheet.org/#Dictionaries-and-Structuring-Data"&gt;dictionaries&lt;/a&gt;, you are ready to go.&lt;/p&gt;

&lt;p&gt;Want to play with args and kwargs? &lt;a href="https://mybinder.org/v2/gh/wilfredinni/python-cheatsheet/master?filepath=jupyter_notebooks"&gt;This&lt;/a&gt; is an online Jupyter Notebook for you to try.&lt;/p&gt;

&lt;p&gt;Also, some examples make use of &lt;code&gt;f-strings&lt;/code&gt;, a relatively new way to format strings in Python 3.6+. &lt;a href="https://www.pythoncheatsheet.org/#Formatted-String-Literals-or-f-strings"&gt;Here&lt;/a&gt; you can read more about it.&lt;/p&gt;

&lt;p&gt;Any doubt or suggestion? Please leave a comment and have a nice day!&lt;/p&gt;

</description>
      <category>python</category>
      <category>args</category>
      <category>kwargs</category>
    </item>
    <item>
      <title>Python: Who to Follow, What to Read and Where to Ask </title>
      <dc:creator>Wilfredinni</dc:creator>
      <pubDate>Tue, 04 Jun 2019 18:15:02 +0000</pubDate>
      <link>https://dev.to/wilfredinni/python-who-to-follow-what-to-read-and-where-to-ask-1an7</link>
      <guid>https://dev.to/wilfredinni/python-who-to-follow-what-to-read-and-where-to-ask-1an7</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Updates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;05-21-2019: Blogs, Podcasts.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;As a self-taught Python Developer and a newbie myself, I think that one of the most important things when starting is to surround ourselves with all things Python related. We can read Blogs, follow developers on Twitter and try to contribute to their projects on GitHub. We can also subscribe to the different mailing lists that exist, and of course, to the many YouTube channels that put online and free of charge millions of hours of content so we can just keep getting better and better at Python.&lt;/p&gt;

&lt;p&gt;In this post, I want to share some of the "assets" that I have collected to this point.&lt;/p&gt;

&lt;h2&gt;
  
  
  Awesome Python
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://awesome-python.com/"&gt;Awesome Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A curated list of awesome Python frameworks, libraries, software and resources.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://python.libhunt.com/"&gt;Python LibHunt&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Your go-to Python Toolbox.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Blogs
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://realpython.com/"&gt;Real Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The realpython.com tutorial team is known for making the highest-quality Python tutorials available online.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://blog.miguelgrinberg.com/index"&gt;Miguel Grinberg&lt;/a&gt; (Here you can find the &lt;a href="https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world"&gt;Flask Mega Tutorial&lt;/a&gt;. I used it myself as a starting point to build this site.)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;I spent some of my spare time in the last month or so writing a tiny blog engine, and I really like how it came out.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://rahmonov.me/"&gt; Jahongir Rahmonov&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;I write about Python, Django, AngularJS and sometimes something non-technical.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://simpleisbetterthancomplex.com/"&gt;simple is better than complex &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Stories about Python, Django and Web Development.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.fullstackpython.com/blog.html"&gt;Full Stack Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Now you want to take your initial Python knowledge and make something real, like a web application to show off to friends or sell as a service to customers. That's where Full Stack Python comes in. You have come to the right place to learn everything you need to create, deploy and operate Python-powered applications.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://djangostars.com/blog/"&gt;Diajngo Stars&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;In a blog we tell how to develop and grow sustainable and scalable Fintech, Taxi&amp;amp;Transportation and Booking products. We also answer customers' most common questions and cover useful topics. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://inventwithpython.com/blog/"&gt;The Invent with Python Blog&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Learning to program makes you a smarter and more capable human being. Computers are not only the most common tool we encounter in our day-to-day lives, but also the most powerful. Even if your plans don't include becoming a software engineer writing code for the next Mars mission, you can find value in learning to code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://overiq.com/blog/"&gt;OVERIQ&lt;/a&gt; (&lt;a href="https://overiq.com/"&gt;Here&lt;/a&gt;, you can also find coruses on Python, Django, Flask, SQLAlchemy and C)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;I'm a simple coder who have a knack with computer languages. My aim is to educate every one with the best of the language knowledge which is not easily available anywhere else in the Internet world.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.blog.pythonlibrary.org/"&gt;The Mouse vs the Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;My name is Mike Driscoll. I am a computer programmer by trade and use Python almost exclusively to make my living. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://snarky.ca/"&gt;Snarky&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Python core developer. Dev lead for the Python extension for VS Code. Tall, snarky Canadian.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.pythonforthelab.com/blog/"&gt;Python for the Lab&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Python For The Lab is a collection of resources to help you start using Python for developing solutions in your lab.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://lucumr.pocoo.org/"&gt;Armin Ronacher's Thoughts and Writings&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;My name is Armin Ronacher and I'm a passionate developer interested in web development, graphics, realtime rendering and tons of other stuff. I am one of the founding members of the Pocoo Team and lead developer on a couple of popular Python projects.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://thepythonguru.com/blog/"&gt;The Python Guru&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Become a better python developer&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://pbpython.com/"&gt;Practical Business Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Taking care of business, one python script at a time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://coolpythoncodes.com/"&gt;Cool Python Codes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Code, The Revolution.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://allofyourbases.com/"&gt;All your Base are Belong to Us&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Physics. Data Science. General Geekery.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.pydanny.com/"&gt;PyDanny&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Hi, I'm Daniel Roy Greenfeld, and welcome to my blog. I write about Python, Django, and much more. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pythontips.com/"&gt;Python Tips&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Your daily dose of bite sized python tips&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pybit.es/pages/articles.html"&gt;PyBites&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A Community that Masters Python through Code Challenges&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://stackabuse.com/tag/python/"&gt;Stack Abuse&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://girisagar46.github.io/"&gt;Sagar Giri&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://pransukh.blogspot.com/"&gt;Pran Sukh&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Weekly Mailing Lists
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.python.org/community/lists/"&gt;Python Mailing Lists and Newsgroups&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Here's an overview of the mail and news resources for python.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://importpython.com/newsletter/"&gt;Import Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Insightful Articles, Tutorials, Projects, Videos, Tweets delivered in your inbox.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pycoders.com/"&gt;PyCoders Weekly&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A free weekly e-mail newsletter, on Fridays, for those interested in python development and various topics around python.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.pythonweekly.com/"&gt;Python Weekly&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A free weekly newsletter featuring curated news, articles, new releases, jobs etc related to Python.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.djangoproject.com/"&gt;Django Mailing Lists&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Subscribe to one of our mailing lists to stay up to date with everything in the Django community&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Twitter
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/gvanrossum"&gt;Guido van Rossum&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Python's creator and emeritus BDFL. Working at Dropbox. Computer History Museum Fellow.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/dbader_org"&gt;Dan Bader&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Full-stack Pythonista &amp;amp; Python Coach. Write Clean + Pythonic code with my free tutorials: &lt;a href="https://dbader.org/get-pythonic"&gt;https://dbader.org/get-pythonic&lt;/a&gt;  🐍Previously: Lead Dev at Mobify.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/dabeaz"&gt;David Beazley&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Author of the Python Essential Reference and Python Cookbook. I tweet deep thoughts about code, bikes, kids, and stuff. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/CoreyMSchafer"&gt;Corey Schafer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Developer and Designer who enjoys woodworking, kayaking, astronomy, and building stuff.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/adrianholovaty"&gt;Adrian Holovaty&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Web developer, cocreator of @djangoproject, poster of YouTube guitar videos. Currently building @soundslice.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/miguelgrinberg"&gt;Miguel Grinberg&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;I write software. And from time to time, I write or speak about writing software.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/Sentdex"&gt;Harrison Kinsley&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;I like to build things. Programming is neat. INTJ &lt;a href="https://hkinsley.com"&gt;https://hkinsley.com&lt;/a&gt; , &lt;a href="https://pythonprogramming.net"&gt;https://pythonprogramming.net&lt;/a&gt; , &lt;a href="http://sentdex.com"&gt;http://sentdex.com&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/AlSweigart"&gt;Al Sweigart&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Author of programming books. I use Python 4.2.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/mkennedy"&gt;Michael Kennedy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A Python and MongoDB enthusiast, and an entrepreneur. Host of @TalkPython and @PythonBytes, founder of Talk Python Training. Python Software Foundation Fellow.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/ThePSF"&gt;Python Software&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The Python Software Foundation. For help with Python code, see comp.lang.python.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/PythonInsider"&gt;Python Insider&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The Python core development team.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/anacondainc"&gt;Anaconda&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Anaconda is the world’s most popular and trusted #Python #DataScience platform, with over 6M users. We help people change the world.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/ProjectJupyter"&gt;Project Jupyter&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Multi-language interactive computing environments. The future home of the IPython notebook and related projects (same folks, same code, new name, new repos).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/IPythonDev"&gt;IPython Developers&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Interactive Python tools. Please, Internet, it is IPython (or ipython), not iPython. Notebook and other language-agnostic parts are moving to @ProjectJupyter.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/djangoproject"&gt;Django&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A high-level Python Web framework that encourages rapid development and clean, pragmatic design. Tweets by Django Software Foundation and &lt;a class="mentioned-user" href="https://dev.to/jezdez"&gt;@jezdez&lt;/a&gt;
.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/realpython"&gt;Real Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Python developer resources: #Python news, tutorials, code examples, snippets, articles, and more!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/python_tip"&gt;Daily Python Tip&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;One #python tip per day. Do you have any? Send it to &lt;a href="http://bit.ly/pythontip"&gt;http://bit.ly/pythontip&lt;/a&gt; . Edited by @karlafej, &lt;a class="mentioned-user" href="https://dev.to/simecek"&gt;@simecek&lt;/a&gt;
, @pybites and... you?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/pybites"&gt;Pybites&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Python code Challenges, Articles and News.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/pythontrending"&gt;Python Trending&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;trending Python repositories on GitHub.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/importpython"&gt;Import Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Python Newsletter &lt;a href="https://clicktracker.in/VkXRR7Po"&gt;https://clicktracker.in/VkXRR7Po&lt;/a&gt;  keeps you updated on what's happening in the world of Python.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/fullstackpython"&gt;Full Stack Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Tweets links to great #Python tutorials. Creating &lt;a href="http://fullstackpython.com"&gt;http://fullstackpython.com&lt;/a&gt; , &lt;a href="http://bit.ly/deploy-python"&gt;http://bit.ly/deploy-python&lt;/a&gt;  &amp;amp; &lt;a href="http://bit.ly/python-entrepreneurs"&gt;http://bit.ly/python-entrepreneurs&lt;/a&gt; …. Run by &lt;a class="mentioned-user" href="https://dev.to/mattmakai"&gt;@mattmakai&lt;/a&gt;
 😎&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/PythonHub"&gt;Python Hub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;News &amp;amp; links about Python programming.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/getpy"&gt;Get Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Tweets for Python enthusiasts - by &lt;a class="mentioned-user" href="https://dev.to/originalankur"&gt;@originalankur&lt;/a&gt;
.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/PythonWeekly"&gt;Python Weekly&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Python Weekly is a free weekly newsletter, which features curated news, articles, new releases, tools and libraries, events, jobs etc related to Python.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/PythonStack"&gt;Python StackOverflow&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;StackOverflow feed of Python questions matching certain criteria.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/SciPyTip"&gt;Scientific Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Tweets about SciPy (Scientific Python) and related topics from @JohnDCook.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/pycoders"&gt;Pycoders Weekly&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Your weekly dose of all things Python! Python tweets by @mgrouchy and @myusuf3.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/BoostLog_Python"&gt;Boostlog - Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A blogging platform developers. Here will introduce the Python article posted on #Boostlog.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://twitter.com/pyCheatsheet"&gt;Python Cheatsheet&lt;/a&gt; 🤭&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Python cheat sheet and Articles for Beginner and Advanced Developers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Podcasts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/pythonbytes"&gt;Python Bytes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Python Headlines delivered directly to your earbuds.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://talkpython.fm/"&gt;Talk Python To Me&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A podcast on Python and related technologies.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.pythonpodcast.com/"&gt;The Python Podcast._ init _&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Each interview explores the motivations, challenges, and experiences behind the software that you use every day.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://testandcode.com/"&gt;Test &amp;amp; Code - Software Testing, Developtment, Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A Podcast about Software Testing, Software Development, and Python&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://djangochat.com/"&gt;Django Chat&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A weekly &lt;em&gt;podcast&lt;/em&gt; on the &lt;em&gt;Django&lt;/em&gt; Web Framework. Build websites with &lt;em&gt;Python&lt;/em&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://friday.hirelofty.com/"&gt;Friday Afternoon Deploy: A Developer Podcast&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Python and Django, Ruby on Rails, Golang.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  YouTube
&lt;/h2&gt;

&lt;p&gt;This list is going to be short, just my favorites. If you want a realy complete one, check &lt;a href="https://realpython.com/python-youtube-channels/"&gt;The Ultimate List of Python YouTube Channels&lt;/a&gt; from &lt;a href="https://realpython.com/"&gt;Real Python&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/channel/UCtxCXg-UvSnTKPOzLH4wJaQ"&gt;Coding Tech&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Coding Tech partners with the tech conferences around the world that provided Coding Tech with their EXPLICIT permissions to republish videos on this channel.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g"&gt;Corey Schafer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;This channel is focused on creating tutorials and walkthroughs for software developers, programmers, and engineers. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/channel/UC-QDfvrRIDB6F0bIO4I4HkQ"&gt;Pretty Printed&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;I'm Anthony. I make programming videos.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/channel/UCW6TXMZ5Pq6yL6_k5NZ2e0Q"&gt;Socratica&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Socratica makes high-quality educational videos on math and science. New videos every week! &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/channel/UC6HfeAa0vWeSWS6IcNAjZ2A"&gt;Sebastiaan Mathôt&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A mix of videos about programming in Python and OpenSesame, and psychology-related topics.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/channel/UCfzlCWGWYyIQ0aLC5w48gBQ"&gt;sentdex&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Python Programming tutorials, going further than just the basics. Learn about machine learning, finance, data analysis, robotics, web development, game development and more.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Questions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/"&gt;Stack Overflow&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Each month, over 50 million developers come to Stack Overflow to learn, share their knowledge, and build their careers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.quora.com/"&gt;Quora&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A place to share knowledge and better understand the world.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.python.org/community/forums/"&gt;Python Community Forums&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;If you're looking for a forum in your native language, please check out the local user groups page at the Python Wiki.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.reddit.com/r/Python/"&gt;r/python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.reddit.com/r/pythontips/"&gt;r/pythontips&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A place to get a quick fix of python tips and tricks to make you a better Pythonista.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.reddit.com/r/learnpython/"&gt;r/learnpython&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Subreddit for posting content, questions, and asking for general advice about learning the Python programming language.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.reddit.com/r/django/"&gt;r/django&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Django subreddit&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.reddit.com/r/djangolearning/"&gt;r/djangolearning&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A group dedicated to learning Django, a Python web framework.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.reddit.com/r/flask/"&gt;r/flask&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Flask is a Python micro-framework for web development. Flask is easy to get started and a great way to build web sites and web applications.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Just two things: First, &lt;em&gt;we have no excuse&lt;/em&gt;. And second, for all the people who try to put quality content online, for the ones that do it in their spare time and for free: subscribe to their channels, give them thumbs up even before you finish to watch their videos (so you don't forget later). If you read an article you liked, share it and disable your ad-blocker for their site. Support Them.&lt;/p&gt;

&lt;p&gt;As always, if you think something is missing, leave a comment and I will update the post later.&lt;/p&gt;

&lt;p&gt;Have a nice day, and eat what makes you happy!&lt;/p&gt;

</description>
      <category>python</category>
      <category>resources</category>
    </item>
    <item>
      <title>Python Sets: What, Why and How</title>
      <dc:creator>Wilfredinni</dc:creator>
      <pubDate>Sat, 01 Jun 2019 15:40:34 +0000</pubDate>
      <link>https://dev.to/wilfredinni/python-sets-what-why-and-how-5a1n</link>
      <guid>https://dev.to/wilfredinni/python-sets-what-why-and-how-5a1n</guid>
      <description>&lt;p&gt;Python comes equipped with several built-in data types to help us organize our data. These structures include lists, dictionaries, tuples and &lt;strong&gt;sets&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;From the Python 3 documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A set is an &lt;em&gt;unordered collection&lt;/em&gt; with no &lt;em&gt;duplicate elements&lt;/em&gt;. Basic uses include &lt;em&gt;membership testing&lt;/em&gt; and &lt;em&gt;eliminating duplicate entries&lt;/em&gt;. Set objects also support mathematical operations like &lt;em&gt;union&lt;/em&gt;, &lt;em&gt;intersection&lt;/em&gt;, &lt;em&gt;difference&lt;/em&gt;, and &lt;em&gt;symmetric difference&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this article, we are going to review and see examples of every one of the elements listed in the above definition. Let's start right away and see how we can create them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initializing a Set
&lt;/h3&gt;

&lt;p&gt;There are two ways to create a set: one is to provide the built-in function &lt;code&gt;set()&lt;/code&gt; with a list of elements, and the other is to use the curly braces &lt;code&gt;{}&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Initializing a set using the &lt;code&gt;set()&lt;/code&gt; built-in function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="nc"&gt;set&lt;/span&gt;&lt;span class="s"&gt;'&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initializing a set using curly braces &lt;code&gt;{}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="nc"&gt;set&lt;/span&gt;&lt;span class="s"&gt;'&amp;gt;
&amp;gt;&amp;gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, both options are valid. The problem comes when what we want is an empty one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="nc"&gt;dict&lt;/span&gt;&lt;span class="s"&gt;'&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's right, we will get a dictionary instead of a set if we use empty curly braces =)&lt;/p&gt;

&lt;p&gt;It's a good moment to mention that for the sake of simplicity, all the examples provided in this article will use single digit integers, but sets can have all the &lt;a href="https://docs.python.org/3/glossary.html#term-hashable"&gt;hashable&lt;/a&gt; data types that Python support. In other words, integers, strings and tuples, but not &lt;em&gt;mutable&lt;/em&gt; items like &lt;em&gt;lists&lt;/em&gt; or &lt;em&gt;dictionaries&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'coffee'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'python'&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;
&lt;span class="n"&gt;Traceback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;unhashable&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'list'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that you know how to create a set and what type of elements it can contain, let's continue and see &lt;em&gt;why&lt;/em&gt; we should always have them in our toolkit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why You Should Use Them
&lt;/h2&gt;

&lt;p&gt;When writing code, you can do it in more than a single way. Some are considered to be pretty bad, and others, &lt;em&gt;clear, concise and maintainable&lt;/em&gt;. Or "&lt;a href="http://docs.python-guide.org/en/latest/writing/style/"&gt;&lt;em&gt;pythonic&lt;/em&gt;&lt;/a&gt;". &lt;/p&gt;

&lt;p&gt;From the &lt;a href="http://docs.python-guide.org/en/latest/"&gt;The Hitchhiker’s Guide to Python&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When a veteran Python developer (a Pythonista) calls portions of code not “Pythonic”, they usually mean that these lines of code do not follow the common guidelines and fail to express its intent in what is considered the best (hear: most readable) way.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's start exploring the way that Python sets can help us not just with readability, but also speeding up our programs execution time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unordered Collection of Elements
&lt;/h3&gt;

&lt;p&gt;First things first: you can't access a set element using indexes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;Traceback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'set'&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt; &lt;span class="n"&gt;does&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;support&lt;/span&gt; &lt;span class="n"&gt;indexing&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or modify them with slices:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;Traceback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'set'&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;subscriptable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;BUT, if what we need is to remove duplicates, or do mathematical operations like combining lists (unions), we can, and &lt;em&gt;SHOULD&lt;/em&gt; always use Sets. &lt;/p&gt;

&lt;p&gt;I have to mention that when iterating over, sets are outperformed by lists, so prefer them if that is what you need. Why? well, this article does not intend to explain the inner workings of sets, but if you are interested, here are a couple of links where you can read about it: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://wiki.python.org/moin/TimeComplexity"&gt;TimeComplexity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/3949310/how-is-set-implemented"&gt;How is set() implemented?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/2831212/python-sets-vs-lists"&gt;Python Sets vs Lists&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://mail.python.org/pipermail/python-list/2011-June/606738.html"&gt;Is there any advantage or disadvantage to using sets over list comps to ensure a list of unique entries?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  No Duplicate Items
&lt;/h3&gt;

&lt;p&gt;While writing this I cannot stop thinking in all the times I used the &lt;em&gt;for&lt;/em&gt; loop and the &lt;em&gt;if&lt;/em&gt; statement to check and remove duplicate elements in a list. My face turns red remembering that, more than once, I wrote something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;my_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;no_duplicate_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;no_duplicate_list&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;             &lt;span class="n"&gt;no_duplicate_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;no_duplicate_list&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or used a list comprehension:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;my_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;no_duplicate_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;no_duplicate_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;my_list&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;no_duplicate_list&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;no_duplicate_list&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But it's ok, nothing of that matters anymore because we now have the sets in our arsenal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;my_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;no_duplicate_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;no_duplicate_list&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's use the &lt;em&gt;timeit&lt;/em&gt; module and see the excecution time of lists and sets when removing duplicates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;timeit&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;timeit&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;no_duplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="n"&gt;no_duplicate_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;no_duplicate_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;no_duplicate_list&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;no_duplicate_list&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;# first, let's see how the list perform:
&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'no_duplicates([1, 2, 3, 1, 7])'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;globals&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;globals&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="mf"&gt;0.0018683355819786227&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;timeit&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;timeit&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;# and the set:
&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'list(set([1, 2, 3, 1, 2, 3, 4]))'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="mf"&gt;0.0010220493243764395&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;# faster and cleaner =)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not only we write &lt;em&gt;fewer lines&lt;/em&gt; with sets than with lists comprehensions, we also obtain more &lt;em&gt;readable&lt;/em&gt; and &lt;em&gt;performant&lt;/em&gt; code.&lt;/p&gt;

&lt;p&gt;Note: remember that sets are unordered, so there is no guarantee that when converting them back to a list the order of the elements is going to be preserved.&lt;/p&gt;

&lt;p&gt;From the &lt;a href="https://www.python.org/dev/peps/pep-0020/"&gt;Zen of Python&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Beautiful is better than ugly. &lt;br&gt;&lt;br&gt;
Explicit is better than implicit.&lt;br&gt;&lt;br&gt;
Simple is better than complex.&lt;br&gt;&lt;br&gt;
Flat is better than nested.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Aren't sets just Beautiful, Explicit, Simple and Flat?  =) &lt;/p&gt;

&lt;h3&gt;
  
  
  Membership  Tests
&lt;/h3&gt;

&lt;p&gt;Every time we use an &lt;em&gt;if&lt;/em&gt; statement to check if an element is, for example, in a list, you are doing a membership test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;my_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Yes, this is a membership test!'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="n"&gt;Yes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;membership&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And sets are more performant than lists when doing them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;timeit&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;timeit&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;in_test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;             &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;                     &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;timeit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'in_test(iterable)'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"from __main__ import in_test; iterable = list(range(1000))"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mf"&gt;12.459663048726043&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;timeit&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;timeit&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;in_test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;             &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;iterable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;                     &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;timeit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'in_test(iterable)'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"from __main__ import in_test; iterable = set(range(1000))"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mf"&gt;0.12354438152988223&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: the above tests come from &lt;a href="https://stackoverflow.com/questions/2831212/python-sets-vs-lists"&gt;this&lt;/a&gt; StackOverflow thread.&lt;/p&gt;

&lt;p&gt;So if you are doing comparisons like this in huge lists, it should speed you a good bit if you convert that list into a set.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Use Them
&lt;/h2&gt;

&lt;p&gt;Now that you know what a set is and why you should use them, let's do a quick tour and see how can we modify and operate with them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Elements
&lt;/h3&gt;

&lt;p&gt;Depending on the number of elements to add, we will have to choose between the  &lt;code&gt;add()&lt;/code&gt; and &lt;code&gt;update()&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;add()&lt;/code&gt; will add a single element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And &lt;code&gt;update()&lt;/code&gt; multiple ones:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember, sets remove duplicates.&lt;/p&gt;

&lt;h3&gt;
  
  
  Removing Elements
&lt;/h3&gt;

&lt;p&gt;If you want to be alerted when your code tries to remove an element that is not in the set, use &lt;code&gt;remove()&lt;/code&gt;. Otherwise, &lt;code&gt;discard()&lt;/code&gt; provides a good alternative:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;Traceback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;KeyError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;discard()&lt;/code&gt; won't raise any errors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;discard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;discard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;# nothing happens!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also use &lt;code&gt;pop()&lt;/code&gt; to randomly discard an element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# removes an arbitrary element
&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or &lt;code&gt;clear()&lt;/code&gt; to remove all the values from a set:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# discard all the items
&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  union()
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;union()&lt;/code&gt; or &lt;code&gt;|&lt;/code&gt; will create a new set that contains all the elements from the sets we provide:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;union&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# or 's1 | s2'
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  intersection()
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;intersection&lt;/code&gt;  or &lt;code&gt;&amp;amp;&lt;/code&gt;  will return a set containing only the elements that are common in all of them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;intersection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# or 's1 &amp;amp; s2 &amp;amp; s3'
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  difference()
&lt;/h3&gt;

&lt;p&gt;Using &lt;code&gt;diference()&lt;/code&gt; or &lt;code&gt;-&lt;/code&gt;, creates a new set with the values that are in "s1" but not in "s2":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;difference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# or 's1 - s2'
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  symmetric_diference()
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;symetric_difference&lt;/code&gt; or &lt;code&gt;^&lt;/code&gt; will return all the values that are not common between the sets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;symmetric_difference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# or 's1 ^ s2'
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;I hope that after reading this article you know what a set is, how to manipulate their elements and the operations they can perform. Knowing when to use a set will definitely help you write cleaner code and speed up your programs.&lt;/p&gt;

&lt;p&gt;If you have any doubts, please leave a comment and I will gladly try to answer them. Also, don´t forget that if you already understand sets, they have their own &lt;a href="https://www.pythoncheatsheet.org/#sets"&gt;place&lt;/a&gt; in the &lt;a href="https://www.pythoncheatsheet.org/"&gt;Python Cheatsheet&lt;/a&gt;, where you can have a quick reference and refresh what you already know.&lt;/p&gt;

</description>
      <category>python</category>
      <category>sets</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
