<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Vivek Shukla - Python</title>
    <subtitle>I am a Software Engineer from India. I am currently building Daestro. I work in Rust, Javascript and Python.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://vshukla.com/tags/python/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://vshukla.com/tags/python/"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2023-10-30T00:00:00+00:00</updated>
    <id>https://vshukla.com/tags/python/atom.xml</id>
    <entry xml:lang="en">
        <title>Python Poetry Cheat Sheet</title>
        <published>2023-10-30T00:00:00+00:00</published>
        <updated>2023-10-30T00:00:00+00:00</updated>
        <author>
          <name>Vivek Shukla</name>
        </author>
        <link rel="alternate" type="text/html" href="https://vshukla.com/tech/poetry-cheat-sheet/"/>
        <id>https://vshukla.com/tech/poetry-cheat-sheet/</id>
        <summary type="html">Poetry is an amazing tool for dependancy management in Python, it solves a lot of problems of pip. Here in this Poetry Cheat Sheet we have useful commands that we may need frequently.</summary>
        <content type="html" xml:base="https://vshukla.com/tech/poetry-cheat-sheet/">&lt;p&gt;Poetry is an amazing tool for dependancy management in Python, it solves a lot of problems of pip. Here in this Poetry Cheat Sheet we have useful commands that we may need frequently.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;initializing&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#initializing&quot; aria-label=&quot;Anchor link for: initializing&quot;&gt;🔗&lt;&#x2F;a&gt;Initializing&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;new-project&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#new-project&quot; aria-label=&quot;Anchor link for: new-project&quot;&gt;🔗&lt;&#x2F;a&gt;New Project&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; new poetry-demo&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;in-pre-existing-directory&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#in-pre-existing-directory&quot; aria-label=&quot;Anchor link for: in-pre-existing-directory&quot;&gt;🔗&lt;&#x2F;a&gt;In Pre-existing Directory&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; pre-existing-project&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; init&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;environments&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#environments&quot; aria-label=&quot;Anchor link for: environments&quot;&gt;🔗&lt;&#x2F;a&gt;Environments&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;displaying-the-environment-information&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#displaying-the-environment-information&quot; aria-label=&quot;Anchor link for: displaying-the-environment-information&quot;&gt;🔗&lt;&#x2F;a&gt;Displaying the environment information&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; env info&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;using-your-own-virtual-environment&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#using-your-own-virtual-environment&quot; aria-label=&quot;Anchor link for: using-your-own-virtual-environment&quot;&gt;🔗&lt;&#x2F;a&gt;Using your own virtual environment&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;strong&gt;Keep virtualenv in project root&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;More on this &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;python-poetry.org&#x2F;docs&#x2F;configuration&#x2F;#virtualenvsin-project&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This will also let VS Code discover your porject’s virtual env.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; config virtualenvs.in-project&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; true&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --local&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;External virtual environment management&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Poetry will detect and respect an existing virtual environment that has been externally activated. This is a powerful mechanism that is intended to be an alternative to Poetry’s built-in, simplified environment management.&lt;&#x2F;p&gt;
&lt;p&gt;You can use &lt;a href=&quot;&#x2F;tech&#x2F;pyenv-cheat-sheet&quot;&gt;pyenv&lt;&#x2F;a&gt; to create external virtual environment.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;switching-between-environments&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#switching-between-environments&quot; aria-label=&quot;Anchor link for: switching-between-environments&quot;&gt;🔗&lt;&#x2F;a&gt;Switching between environments&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; env use &#x2F;full&#x2F;path&#x2F;to&#x2F;python&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; env use&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 3.7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; env use system&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;listing-the-environments-associated-with-the-project&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#listing-the-environments-associated-with-the-project&quot; aria-label=&quot;Anchor link for: listing-the-environments-associated-with-the-project&quot;&gt;🔗&lt;&#x2F;a&gt;Listing the environments associated with the project&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; env list&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;deleting-the-environments&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#deleting-the-environments&quot; aria-label=&quot;Anchor link for: deleting-the-environments&quot;&gt;🔗&lt;&#x2F;a&gt;Deleting the environments&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; env remove &#x2F;full&#x2F;path&#x2F;to&#x2F;python&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; env remove python3.7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; env remove&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 3.7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; env remove test-O3eWbxRl-py3.7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Use the &lt;strong&gt;&lt;code&gt;--all&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt; option to delete all virtual environments at once.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; env remove&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --all&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;dependencies&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#dependencies&quot; aria-label=&quot;Anchor link for: dependencies&quot;&gt;🔗&lt;&#x2F;a&gt;Dependencies&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;add-new-dependency&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#add-new-dependency&quot; aria-label=&quot;Anchor link for: add-new-dependency&quot;&gt;🔗&lt;&#x2F;a&gt;Add new dependency&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; add&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;package nam&lt;&#x2F;span&gt;&lt;span&gt;e&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;removing-dependency&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#removing-dependency&quot; aria-label=&quot;Anchor link for: removing-dependency&quot;&gt;🔗&lt;&#x2F;a&gt;Removing dependency&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; remove&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;package nam&lt;&#x2F;span&gt;&lt;span&gt;e&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;installing-dependencies&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#installing-dependencies&quot; aria-label=&quot;Anchor link for: installing-dependencies&quot;&gt;🔗&lt;&#x2F;a&gt;Installing dependencies&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; install&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;updating-dependencies-to-their-latest-versions&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#updating-dependencies-to-their-latest-versions&quot; aria-label=&quot;Anchor link for: updating-dependencies-to-their-latest-versions&quot;&gt;🔗&lt;&#x2F;a&gt;Updating dependencies to their latest versions&lt;&#x2F;h3&gt;
&lt;p&gt;This will fetch the latest matching versions (according to your &lt;strong&gt;&lt;code&gt;pyproject.toml&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt; file) and update the lock file with the new versions. (This is equivalent to deleting the &lt;strong&gt;&lt;code&gt;poetry.lock&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt; file and running &lt;strong&gt;&lt;code&gt;install&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt; again.)&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; update&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;adding-a-dependency-to-a-group&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#adding-a-dependency-to-a-group&quot; aria-label=&quot;Anchor link for: adding-a-dependency-to-a-group&quot;&gt;🔗&lt;&#x2F;a&gt;Adding a dependency to a group&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; add pytest&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --group&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; test&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; add isort&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --group&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; dev&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;installing-group-dependencies&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#installing-group-dependencies&quot; aria-label=&quot;Anchor link for: installing-group-dependencies&quot;&gt;🔗&lt;&#x2F;a&gt;Installing group dependencies&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;without&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; install&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --without&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; test, docs&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;with&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; install&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --with&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; docs&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;only&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; install&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --only&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; main&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;removing-dependencies-from-a-group&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#removing-dependencies-from-a-group&quot; aria-label=&quot;Anchor link for: removing-dependencies-from-a-group&quot;&gt;🔗&lt;&#x2F;a&gt;Removing dependencies from a group&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; remove mkdocs&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --group&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; docs&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;synchronizing-dependencies&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#synchronizing-dependencies&quot; aria-label=&quot;Anchor link for: synchronizing-dependencies&quot;&gt;🔗&lt;&#x2F;a&gt;Synchronizing dependencies&lt;&#x2F;h3&gt;
&lt;p&gt;Poetry supports what’s called dependency synchronization. Dependency synchronization ensures that the locked dependencies in the &lt;strong&gt;&lt;code&gt;poetry.lock&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt; file are the only ones present in the environment, removing anything that’s not necessary.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; install&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --sync&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; install&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --without&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; dev&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --sync&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; install&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --with&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; docs&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --sync&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; install&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --only&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; dev&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;export&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#export&quot; aria-label=&quot;Anchor link for: export&quot;&gt;🔗&lt;&#x2F;a&gt;Export&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; export&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; -f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; requirements.txt&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --output&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; requirements.txt&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;--format (-f)&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt;: The format to export to (default: &lt;strong&gt;&lt;code&gt;requirements.txt&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt;). Currently, only &lt;strong&gt;&lt;code&gt;constraints.txt&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt; and &lt;strong&gt;&lt;code&gt;requirements.txt&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt; are supported.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;--output (-o)&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt;: The name of the output file. If omitted, print to standard output.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;--extras (-E)&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt;: Extra sets of dependencies to include.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;--without&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt;: The dependency groups to ignore.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;--with&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt;: The optional dependency groups to include.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;--only&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt;: The only dependency groups to include.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;--without-hashes&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt;: Exclude hashes from the exported file.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;--without-urls&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt;: Exclude source repository urls from the exported file.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;--with-credentials&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt;: Include credentials for extra indices.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;packaging&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#packaging&quot; aria-label=&quot;Anchor link for: packaging&quot;&gt;🔗&lt;&#x2F;a&gt;Packaging&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;build&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#build&quot; aria-label=&quot;Anchor link for: build&quot;&gt;🔗&lt;&#x2F;a&gt;Build&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; build&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;publish&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#publish&quot; aria-label=&quot;Anchor link for: publish&quot;&gt;🔗&lt;&#x2F;a&gt;Publish&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;poetry&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; publish&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;configuration&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#configuration&quot; aria-label=&quot;Anchor link for: configuration&quot;&gt;🔗&lt;&#x2F;a&gt;Configuration&lt;&#x2F;h2&gt;
&lt;p&gt;You can find all the &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;python-poetry.org&#x2F;docs&#x2F;configuration&#x2F;&quot;&gt;configuration options here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>pyenv Cheat Sheet</title>
        <published>2023-10-22T00:00:00+00:00</published>
        <updated>2023-10-22T00:00:00+00:00</updated>
        <author>
          <name>Vivek Shukla</name>
        </author>
        <link rel="alternate" type="text/html" href="https://vshukla.com/tech/pyenv-cheat-sheet/"/>
        <id>https://vshukla.com/tech/pyenv-cheat-sheet/</id>
        <summary type="html">pyenv is Python version management tool, it lets you easily switch between multiple versions of Python. Here is the pyenv cheat sheet of all the basic commands list.</summary>
        <content type="html" xml:base="https://vshukla.com/tech/pyenv-cheat-sheet/">&lt;p&gt;Here is the pyenv cheat sheet of all the basic commands list.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-is-pyenv&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-pyenv&quot; aria-label=&quot;Anchor link for: what-is-pyenv&quot;&gt;🔗&lt;&#x2F;a&gt;What is pyenv&lt;&#x2F;h2&gt;
&lt;p&gt;pyenv is &lt;a href=&quot;&#x2F;tags&#x2F;python&#x2F;&quot;&gt;Python&lt;&#x2F;a&gt; version management tool, it lets you easily switch between multiple versions of Python.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-it-does&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-it-does&quot; aria-label=&quot;Anchor link for: what-it-does&quot;&gt;🔗&lt;&#x2F;a&gt;What it does&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Lets you change the global Python version on a per-user basis.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Provides support for per-project Python versions.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Allows you to override the Python version with an environment variable.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;list-available-python-versions-to-install&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#list-available-python-versions-to-install&quot; aria-label=&quot;Anchor link for: list-available-python-versions-to-install&quot;&gt;🔗&lt;&#x2F;a&gt;List available python versions to install&lt;&#x2F;h2&gt;
&lt;p&gt;This will list all the available version of python&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; install&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --list&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can filter it using grep. Here we are filtering list by python version:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; install&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-other&quot;&gt; --list&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; |&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; grep&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;quot; 3\.11&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;installing-python-version&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#installing-python-version&quot; aria-label=&quot;Anchor link for: installing-python-version&quot;&gt;🔗&lt;&#x2F;a&gt;Installing Python Version&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; install&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;versio&lt;&#x2F;span&gt;&lt;span&gt;n&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;add &lt;code&gt;-v&lt;&#x2F;code&gt; flag to see install progress output&lt;&#x2F;p&gt;
&lt;h2 id=&quot;list-installed-python-versions&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#list-installed-python-versions&quot; aria-label=&quot;Anchor link for: list-installed-python-versions&quot;&gt;🔗&lt;&#x2F;a&gt;List Installed Python Versions&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; versions&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Astrick (*) indicate current active version.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;see-list-of-all-pyenv-commands&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#see-list-of-all-pyenv-commands&quot; aria-label=&quot;Anchor link for: see-list-of-all-pyenv-commands&quot;&gt;🔗&lt;&#x2F;a&gt;See List Of All pyenv Commands&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; commands&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;activating-python-version&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#activating-python-version&quot; aria-label=&quot;Anchor link for: activating-python-version&quot;&gt;🔗&lt;&#x2F;a&gt;Activating Python Version&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;global&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#global&quot; aria-label=&quot;Anchor link for: global&quot;&gt;🔗&lt;&#x2F;a&gt;global&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Sets the given version as global python version.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;It overrides system’s default python version.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; global&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;versio&lt;&#x2F;span&gt;&lt;span&gt;n&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;local&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#local&quot; aria-label=&quot;Anchor link for: local&quot;&gt;🔗&lt;&#x2F;a&gt;local&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Use it to set directory &#x2F; project specific python version.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;It can override python version set by &lt;code&gt;global&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; local&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;versio&lt;&#x2F;span&gt;&lt;span&gt;n&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;shell&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#shell&quot; aria-label=&quot;Anchor link for: shell&quot;&gt;🔗&lt;&#x2F;a&gt;shell&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Sets shell specific python version.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;The python version stays active until shell has been closed or by using &lt;code&gt;--unset&lt;&#x2F;code&gt; flag.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;It can override python version set by &lt;code&gt;local&lt;&#x2F;code&gt; and &lt;code&gt;global&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; shell&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;versio&lt;&#x2F;span&gt;&lt;span&gt;n&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;virtual-environments&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#virtual-environments&quot; aria-label=&quot;Anchor link for: virtual-environments&quot;&gt;🔗&lt;&#x2F;a&gt;Virtual Environments&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;create-virtual-environments&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#create-virtual-environments&quot; aria-label=&quot;Anchor link for: create-virtual-environments&quot;&gt;🔗&lt;&#x2F;a&gt;Create Virtual Environments&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; virtualenv&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;versio&lt;&#x2F;span&gt;&lt;span&gt;n&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;env_nam&lt;&#x2F;span&gt;&lt;span&gt;e&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;set-virtual-environment-as-default-for-dir-project&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#set-virtual-environment-as-default-for-dir-project&quot; aria-label=&quot;Anchor link for: set-virtual-environment-as-default-for-dir-project&quot;&gt;🔗&lt;&#x2F;a&gt;Set virtual environment as default for dir&#x2F;project&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; local&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;env_nam&lt;&#x2F;span&gt;&lt;span&gt;e&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;activate-virtual-environment&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#activate-virtual-environment&quot; aria-label=&quot;Anchor link for: activate-virtual-environment&quot;&gt;🔗&lt;&#x2F;a&gt;Activate Virtual Environment&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; activate&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;env_nam&lt;&#x2F;span&gt;&lt;span&gt;e&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;deactivate-virtual-environment&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#deactivate-virtual-environment&quot; aria-label=&quot;Anchor link for: deactivate-virtual-environment&quot;&gt;🔗&lt;&#x2F;a&gt;Deactivate Virtual Environment&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name&quot;&gt;pyenv&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; deactivate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;recommended-reading-materials&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#recommended-reading-materials&quot; aria-label=&quot;Anchor link for: recommended-reading-materials&quot;&gt;🔗&lt;&#x2F;a&gt;Recommended Reading Materials&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Official Page of &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;pyenv&#x2F;pyenv&quot;&gt;pyenv&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Real Python: &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;realpython.com&#x2F;intro-to-pyenv&#x2F;#exploring-pyenv-commands&quot;&gt;Managing Multiple Python Versions With pyenv&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>How to convert datetime to different timezone in Python using pytz</title>
        <published>2023-06-20T00:00:00+00:00</published>
        <updated>2023-06-20T00:00:00+00:00</updated>
        <author>
          <name>Vivek Shukla</name>
        </author>
        <link rel="alternate" type="text/html" href="https://vshukla.com/tech/convert-datetime-to-different-timezone-in-python-pytz/"/>
        <id>https://vshukla.com/tech/convert-datetime-to-different-timezone-in-python-pytz/</id>
        <summary type="html">Convert datetime to different timezone in Python using pytz.</summary>
        <content type="html" xml:base="https://vshukla.com/tech/convert-datetime-to-different-timezone-in-python-pytz/">&lt;h2 id=&quot;create-datetime-instance-with-timezone-info&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#create-datetime-instance-with-timezone-info&quot; aria-label=&quot;Anchor link for: create-datetime-instance-with-timezone-info&quot;&gt;🔗&lt;&#x2F;a&gt;&lt;strong&gt;Create datetime instance with timezone info&lt;&#x2F;strong&gt;&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; datetime&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; pytz&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;dt&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; datetime&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;datetime&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;2021&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;tz&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; pytz&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;timezone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Asia&#x2F;Kolkata&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;tzinfo_dt&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; tz&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;localize&lt;&#x2F;span&gt;&lt;span&gt;(dt)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;tzinfo_dt&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# output:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# datetime.datetime(2021, 1, 6, 0, 0, tzinfo=&amp;lt;DstTzInfo &amp;#39;Asia&#x2F;Kolkata&amp;#39; IST+5:30:00 STD&amp;gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note: Passing &lt;code&gt;tzinfo&lt;&#x2F;code&gt; in datetime is not recommended by &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;pytz&#x2F;&quot;&gt;pytz&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;convert-datetime-to-different-timezone&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#convert-datetime-to-different-timezone&quot; aria-label=&quot;Anchor link for: convert-datetime-to-different-timezone&quot;&gt;🔗&lt;&#x2F;a&gt;&lt;strong&gt;Convert datetime to different timezone&lt;&#x2F;strong&gt;&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; datetime&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; pytz&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;#creating timezone aware datetime instance&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;dt&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; pytz&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;utc&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;localize&lt;&#x2F;span&gt;&lt;span&gt;(datetime&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;datetime&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;now&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# converting to IST&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ist_dt&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; dt&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;astimezone&lt;&#x2F;span&gt;&lt;span&gt;(pytz&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;timezone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Asia&#x2F;Kolkata&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ist_dt&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# output:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# datetime.datetime(2021, 1, 6, 9, 28, 57, 974939, tzinfo=&amp;lt;DstTzInfo &amp;#39;Asia&#x2F;Kolkata&amp;#39; IST+5:30:00 STD&amp;gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;us_east&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; dt&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;astimezone&lt;&#x2F;span&gt;&lt;span&gt;(pytz&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;timezone&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;US&#x2F;Eastern&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# output:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# datetime.datetime(2021, 1, 5, 23, 1, 49, 588549, tzinfo=&amp;lt;DstTzInfo &amp;#39;US&#x2F;Eastern&amp;#39; EST-1 day, 19:00:00 STD&amp;gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</content>
    </entry>
    <entry xml:lang="en">
        <title>Master Python Interview Questions with Answers</title>
        <published>2023-05-22T00:00:00+00:00</published>
        <updated>2023-05-22T00:00:00+00:00</updated>
        <author>
          <name>Vivek Shukla</name>
        </author>
        <link rel="alternate" type="text/html" href="https://vshukla.com/tech/master-python-interview-questions-with-answers/"/>
        <id>https://vshukla.com/tech/master-python-interview-questions-with-answers/</id>
        <summary type="html">Prepare for Python interview questions using this extensive list of questions and answers. This list takes you from beginner to intermediate to advanced level of python concepts in question-answer format.</summary>
        <content type="html" xml:base="https://vshukla.com/tech/master-python-interview-questions-with-answers/">&lt;p&gt;Prepare for &lt;a href=&quot;&#x2F;tags&#x2F;python&#x2F;&quot;&gt;Python&lt;&#x2F;a&gt; interview questions using this extensive list of questions and answers. This list takes you from beginner to intermediate to advanced level of python concepts in question-answer format.&lt;&#x2F;p&gt;
&lt;p&gt;You might want to &lt;strong&gt;bookmark&lt;&#x2F;strong&gt; this page as new contents will added and updated with time.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;beginner&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#beginner&quot; aria-label=&quot;Anchor link for: beginner&quot;&gt;🔗&lt;&#x2F;a&gt;&lt;strong&gt;Beginner&lt;&#x2F;strong&gt;&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;what-is-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-python&quot; aria-label=&quot;Anchor link for: what-is-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is Python?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;It was created by Guido van Rossum and first released in 1991.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;what-are-some-of-the-benefits-of-using-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-are-some-of-the-benefits-of-using-python&quot; aria-label=&quot;Anchor link for: what-are-some-of-the-benefits-of-using-python&quot;&gt;🔗&lt;&#x2F;a&gt;What are some of the benefits of using Python?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Python has a vast number of third-party modules and libraries that make it easy to incorporate additional functionality into your projects.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Python’s simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Python supports modules and packages, which encourages program modularity and code reuse.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;It has a large and active community that provides support, resources, and libraries.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Python has a vast number of third-party modules and libraries that make it easy to incorporate additional functionality into your projects.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Python is an interpreted language, which means that it does not need to be compiled before running, making it faster to write and test code.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;what-is-the-difference-between-str-and-repr-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-the-difference-between-str-and-repr-in-python&quot; aria-label=&quot;Anchor link for: what-is-the-difference-between-str-and-repr-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is the difference between str and repr in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-is-pep8&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-pep8&quot; aria-label=&quot;Anchor link for: what-is-pep8&quot;&gt;🔗&lt;&#x2F;a&gt;What is PEP8?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;PEP8 is a coding style guide for Python.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;It provides guidelines on how to write Python code that is easy to read and maintain.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;PEP 8 covers topics such as indentation, naming conventions, comments and documentation, code layout and structure, imports, error handling, and best practices.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;PEP 8 is not a strict set of rules, but rather a set of guidelines that are intended to be flexible and adaptable to different projects and contexts.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;PEP stands for Python Enhancement Proposal, which is a design document providing information to the Python community, or describing a new feature for Python or its processes or environment.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;what-is-a-list&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-a-list&quot; aria-label=&quot;Anchor link for: what-is-a-list&quot;&gt;🔗&lt;&#x2F;a&gt;What is a List?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;In Python, List is a data structure which is used to hold multiple items at once.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;It is mutable and ordered sequence of elements.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;They are similar to an array in other programming languages.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Python Lists are mutable and can hold items of different types simultaneously.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Lists are defined using &lt;code&gt;[]&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Example: &lt;code&gt;data = [&quot;a&quot;, 1, &quot;b&quot;, 2]&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Elements in a list can be accessed using indexing, for example &lt;code&gt;data[0]&lt;&#x2F;code&gt; would return first element in the list.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;what-is-a-tuple&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-a-tuple&quot; aria-label=&quot;Anchor link for: what-is-a-tuple&quot;&gt;🔗&lt;&#x2F;a&gt;What is a Tuple?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Tuple is an immutable and ordered sequence of elements.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Tuples are immutable which means they cannot be modified once created.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Since tuples are immutable, this also makes them hashable.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Tuples are defined using &lt;code&gt;()&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Example: &lt;code&gt;a_tuple = (1, 2, &quot;a&quot;, 4)&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Just like lists, tuples can also be accessed using indexing, i.e. &lt;code&gt;a_tuple[0]&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;what-is-the-difference-between-a-list-and-a-tuple&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-the-difference-between-a-list-and-a-tuple&quot; aria-label=&quot;Anchor link for: what-is-the-difference-between-a-list-and-a-tuple&quot;&gt;🔗&lt;&#x2F;a&gt;What is the difference between a List and a Tuple?&lt;&#x2F;h3&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;List&lt;&#x2F;th&gt;&lt;th&gt;Tuple&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Lists are mutable&lt;&#x2F;td&gt;&lt;td&gt;Tuples are immutable&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Lists are not hashable since they are mutable.&lt;&#x2F;td&gt;&lt;td&gt;Tuples are hashable&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Defined using []&lt;&#x2F;td&gt;&lt;td&gt;Defined using ()&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Lists take more memory due to the overhead of managing dynamic memory allocation&lt;&#x2F;td&gt;&lt;td&gt;Tuples take less memory compared to lists&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;It is slower for accessing elements due to dynamic memory allocation&lt;&#x2F;td&gt;&lt;td&gt;Tuples are faster in accessing elements&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;It is suitable for situations when elements are needed to be frequently added or removed&lt;&#x2F;td&gt;&lt;td&gt;Tuples are suitable for situations where data needs to be fixed and does not need to be modified&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;h3 id=&quot;what-are-virtual-environments-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-are-virtual-environments-in-python&quot; aria-label=&quot;Anchor link for: what-are-virtual-environments-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What are virtual environments in Python?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Virtual environments are isolated environments which contain their own Python version and packages.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;They allow you to create a separate environment for each project or application you are working on, with its own set of dependencies, without affecting the global environment or other projects.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;This can be especially useful when working on multiple projects that require different versions of the same package or library.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Python virtual environments can be created using the venv module, which is included with Python 3.3 and later versions.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Example: &lt;code&gt;python3 -m venv env_name&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;what-is-the-difference-between-a-function-and-a-method-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-the-difference-between-a-function-and-a-method-in-python&quot; aria-label=&quot;Anchor link for: what-is-the-difference-between-a-function-and-a-method-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is the difference between a function and a method in Python?&lt;&#x2F;h3&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Function&lt;&#x2F;th&gt;&lt;th&gt;Method&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;A block of usable code that performs a task&lt;&#x2F;td&gt;&lt;td&gt;A function that is associated with an object&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Defined outside of a class&lt;&#x2F;td&gt;&lt;td&gt;Defined inside a class&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Can be called anywhere in the program&lt;&#x2F;td&gt;&lt;td&gt;Can only be called on an instance of the class&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Accepts arguments and returns a value&lt;&#x2F;td&gt;&lt;td&gt;Accepts arguments but also can access and modify the attributes of the instance on which it is called upon&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;It is defined with the def keyword followed by function name and parameters&lt;&#x2F;td&gt;&lt;td&gt;Defined with def keyword followed by method name and parameters&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;It doesn’t have self parameter&lt;&#x2F;td&gt;&lt;td&gt;Implicit first argument is the object on which the method is called, self by convention&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Example: &lt;code&gt;len(&quot;world&quot;)&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Example: &lt;code&gt;&quot;world&quot;.lower()&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;h3 id=&quot;what-is-an-exception-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-an-exception-in-python&quot; aria-label=&quot;Anchor link for: what-is-an-exception-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is an exception in Python?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;An exception is an error that happens during execution of a program.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;When an exception occurs, the normal flow of the program is disrupted and interpreter raises an exception object (of Exception class).&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;If exception is not handled then program will terminate.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Python provides built-in exceptions for common error conditions, such as &lt;code&gt;IndexError&lt;&#x2F;code&gt;, &lt;code&gt;ValueError&lt;&#x2F;code&gt;, &lt;code&gt;TypeError&lt;&#x2F;code&gt;, etc.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;You can also create your own exception by inheriting from base Exception class.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;how-to-handle-exceptions-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#how-to-handle-exceptions-in-python&quot; aria-label=&quot;Anchor link for: how-to-handle-exceptions-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;How to handle Exceptions in Python?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Python provides a mechanism to handle exception using &lt;code&gt;try-except&lt;&#x2F;code&gt; blocks.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;A &lt;code&gt;try&lt;&#x2F;code&gt; block contains the code that may cause the exception.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;And &lt;code&gt;except&lt;&#x2F;code&gt; block contains the code that handles the exception.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;try&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    x&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;except&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt; ZeroDivisionError&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function&quot;&gt;    print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;this is not how maths work&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;You can also use a &lt;code&gt;finally&lt;&#x2F;code&gt; block after the except block to specify code that should be executed regardless of whether an exception was raised or not. The &lt;code&gt;finally&lt;&#x2F;code&gt; block is optional, but it’s useful for releasing resources or cleaning up after the code in the try block.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;We also can have &lt;code&gt;else&lt;&#x2F;code&gt; block. The &lt;code&gt;else&lt;&#x2F;code&gt; block contains the code that is executed if no exception is raised in the try block. It is optional and executes after the &lt;code&gt;try&lt;&#x2F;code&gt; block but before the &lt;code&gt;finally&lt;&#x2F;code&gt; block.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;how-do-you-handle-file-i-o-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#how-do-you-handle-file-i-o-in-python&quot; aria-label=&quot;Anchor link for: how-do-you-handle-file-i-o-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;How do you handle file I&#x2F;O in Python?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Python provides built-in function &lt;code&gt;open()&lt;&#x2F;code&gt; to handle file I&#x2F;O.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;open()&lt;&#x2F;code&gt; function takes two arguments: name of the file and the mode in which to open the file (e.g. read, write, append, etc.).&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;To read data from a file, you can use the &lt;code&gt;read()&lt;&#x2F;code&gt; method, which reads the entire contents of the file, or the &lt;code&gt;readline()&lt;&#x2F;code&gt; method, which reads one line at a time.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;To write data to a file, you can use the &lt;code&gt;write()&lt;&#x2F;code&gt; method, which writes a string to the file, or the &lt;code&gt;writelines()&lt;&#x2F;code&gt; method, which writes a list of strings to the file.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;After you are done working with the file, you should close the file using the &lt;code&gt;close()&lt;&#x2F;code&gt; method.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;what-is-a-static-method&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-a-static-method&quot; aria-label=&quot;Anchor link for: what-is-a-static-method&quot;&gt;🔗&lt;&#x2F;a&gt;What is a static method?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Static method is a method which belongs to class rather than instance of the class.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;It does not have access to the instance or it’s attributes.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;It does not require &lt;code&gt;self&lt;&#x2F;code&gt; parameter that instance method usually have.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Static methods are defined using the &lt;code&gt;@staticmethod&lt;&#x2F;code&gt; decorator.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; FooClass&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function z-decorator&quot;&gt;    @&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt;staticmethod&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-function&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; my_static_method&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;():&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function&quot;&gt;        print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;calling static method&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# Calling the static method without creating an instance of the class&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;FooClass&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;my_static_method&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;what-is-a-class-method&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-a-class-method&quot; aria-label=&quot;Anchor link for: what-is-a-class-method&quot;&gt;🔗&lt;&#x2F;a&gt;What is a class method?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A class method is method that is bound to the class and not to the instance of the class.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Class method knows about the class and it’s attributes, but it can’t access individual instance or their attributes.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; BarClass&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    class_attribute&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;quot;a class attribute.&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function z-decorator&quot;&gt;    @&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt;classmethod&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-function&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; my_class_method&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;cls&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function&quot;&gt;        print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;Class attribute:&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; cls&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;class_attribute)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# Calling the class method on the class itself&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;BarClass&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;my_class_method&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;what-are-difference-between-static-method-and-class-method&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-are-difference-between-static-method-and-class-method&quot; aria-label=&quot;Anchor link for: what-are-difference-between-static-method-and-class-method&quot;&gt;🔗&lt;&#x2F;a&gt;What are difference between static method and class method?&lt;&#x2F;h3&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Static Method&lt;&#x2F;th&gt;&lt;th&gt;Class Method&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Static method do not depend on the state of the class or instance&lt;&#x2F;td&gt;&lt;td&gt;Class method operate on the class&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;It does not take any special parameter&lt;&#x2F;td&gt;&lt;td&gt;Takes a special parameter called cls which refers to the class itself&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Can be called on the class or instance of the class&lt;&#x2F;td&gt;&lt;td&gt;It can be called on the class but not on instance of the class&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Cannot access or modify the class or instance state&lt;&#x2F;td&gt;&lt;td&gt;It can access and modify class state but not the instance state&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;h3 id=&quot;what-is-the-difference-between-an-instance-method-and-a-class-method-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-the-difference-between-an-instance-method-and-a-class-method-in-python&quot; aria-label=&quot;Anchor link for: what-is-the-difference-between-an-instance-method-and-a-class-method-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is the difference between an instance method and a class method in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-is-the-global-interpreter-lock-gil-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-the-global-interpreter-lock-gil-in-python&quot; aria-label=&quot;Anchor link for: what-is-the-global-interpreter-lock-gil-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is the Global Interpreter Lock (GIL) in Python?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The Global Interpreter Lock (GIL) is a mechanism used in Python to manage access to shared resources, such as the Python interpreter itself.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;The GIL is a mutex that prevents multiple threads from executing Python bytecodes at once.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;The GIL is necessary because Python interpreter is not thread-safe, meaning that it is not designed to handle multiple threads executing Python code simultaneously.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;The GIL is a feature of the CPython implementation of Python, which is the most widely used implementation.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;The GIL can prevent multi-core processors from fully utilizing all available cores.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;The GIL can be released using the &lt;code&gt;threading&lt;&#x2F;code&gt; module’s &lt;code&gt;Lock&lt;&#x2F;code&gt; object, the multiprocessing module, or by using an extension module written in C that releases GIL.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;what-is-python-package&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-python-package&quot; aria-label=&quot;Anchor link for: what-is-python-package&quot;&gt;🔗&lt;&#x2F;a&gt;What is Python package?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A package is a way of organizing related modules into a single namespace.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;A package is a directory that contains one or more Python modules, as well as an optional &lt;code&gt;__init__.py&lt;&#x2F;code&gt; file that initializes the package.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Packages can be imported using &lt;code&gt;import&lt;&#x2F;code&gt; statement.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Packages can be installed and distributed using tools such as &lt;code&gt;pip&lt;&#x2F;code&gt; and &lt;code&gt;setuptools&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;what-is-python-module&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-python-module&quot; aria-label=&quot;Anchor link for: what-is-python-module&quot;&gt;🔗&lt;&#x2F;a&gt;What is Python module?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A module is a file containing Python code that can be imported and used in other Python code.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;A module can define classes, functions and variables that can be used in other Python code.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;A module can be imported using &lt;code&gt;import&lt;&#x2F;code&gt; statement.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Modules can be organized into package which is a directory that contains one or more modules.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;what-is-the-difference-between-a-shallow-copy-and-a-deep-copy-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-the-difference-between-a-shallow-copy-and-a-deep-copy-in-python&quot; aria-label=&quot;Anchor link for: what-is-the-difference-between-a-shallow-copy-and-a-deep-copy-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is the difference between a shallow copy and a deep copy in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;intermediate&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#intermediate&quot; aria-label=&quot;Anchor link for: intermediate&quot;&gt;🔗&lt;&#x2F;a&gt;Intermediate&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;what-is-a-python-namespace&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-a-python-namespace&quot; aria-label=&quot;Anchor link for: what-is-a-python-namespace&quot;&gt;🔗&lt;&#x2F;a&gt;What is a Python namespace?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-is-the-difference-between-a-class-and-an-object-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-the-difference-between-a-class-and-an-object-in-python&quot; aria-label=&quot;Anchor link for: what-is-the-difference-between-a-class-and-an-object-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is the difference between a class and an object in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;how-do-you-implement-inheritance-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#how-do-you-implement-inheritance-in-python&quot; aria-label=&quot;Anchor link for: how-do-you-implement-inheritance-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;How do you implement inheritance in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;how-do-you-implement-a-singleton-pattern-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#how-do-you-implement-a-singleton-pattern-in-python&quot; aria-label=&quot;Anchor link for: how-do-you-implement-a-singleton-pattern-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;How do you implement a singleton pattern in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-is-a-decorator&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-a-decorator&quot; aria-label=&quot;Anchor link for: what-is-a-decorator&quot;&gt;🔗&lt;&#x2F;a&gt;What is a decorator?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A decorator is higher-order function that takes a function as input and returns a modified function as output.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Decorators are used to modify or enhance the behaviour of a function without changing it’s source code.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Decorators can be used with syntax &lt;code&gt;@decorator_name&lt;&#x2F;code&gt; where &lt;code&gt;decorator_name&lt;&#x2F;code&gt; is the name of decorator function.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-function&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; my_decorator&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;func&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-function&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; wrapper&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;():&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function&quot;&gt;        print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;Before function is called.&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;        func&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function&quot;&gt;        print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;After function is called.&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span&gt; wrapper&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function z-decorator&quot;&gt;@my_decorator&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-function&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; say_hello&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;():&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function&quot;&gt;    print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;Hello!&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;what-is-a-lambda-function-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-a-lambda-function-in-python&quot; aria-label=&quot;Anchor link for: what-is-a-lambda-function-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is a lambda function in Python?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A lambda function is an anonymous function that can be defined in a single line of code.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;A lambda function is defined using &lt;code&gt;lambda&lt;&#x2F;code&gt; keyword followed by the function parameters, a colon and then the function body.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Example: &lt;code&gt;lambda i, j: i * j&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Lambda functions can take any number of arguments, but they can have only one expression in the body.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Lambda functions are often used as a shortcut for simple functions that are only used once.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;what-is-a-generator-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-a-generator-in-python&quot; aria-label=&quot;Anchor link for: what-is-a-generator-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is a generator in Python?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A generator is type of iterable, like a list or a tuple, but it generates values on the fly instead of storing them in memory.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Since it doesn’t store values in memory, it is very memory efficient.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Generators can be iterated just like a list or a tuple but indexing is not possible.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Generators can be created using generator function or generator expression.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;A generator function is created using one or more &lt;code&gt;yield&lt;&#x2F;code&gt; statements.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Whereas a generator expression is similar to list comprehension but instead of list it returns a iterator.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# generator function&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-function&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; gen_number&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;limit&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    n&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    while&lt;&#x2F;span&gt;&lt;span&gt; n&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;=&lt;&#x2F;span&gt;&lt;span&gt; limit&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        yield&lt;&#x2F;span&gt;&lt;span&gt; n&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        n&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# generator expression&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;numbers&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-function&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;what-is-the-difference-between-a-list-comprehension-and-a-generator-expression-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-the-difference-between-a-list-comprehension-and-a-generator-expression-in-python&quot; aria-label=&quot;Anchor link for: what-is-the-difference-between-a-list-comprehension-and-a-generator-expression-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is the difference between a list comprehension and a generator expression in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-is-a-python-iterator&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-a-python-iterator&quot; aria-label=&quot;Anchor link for: what-is-a-python-iterator&quot;&gt;🔗&lt;&#x2F;a&gt;What is a Python iterator?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-is-the-difference-between-iterator-and-generator-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-the-difference-between-iterator-and-generator-in-python&quot; aria-label=&quot;Anchor link for: what-is-the-difference-between-iterator-and-generator-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is the difference between iterator and generator in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;advanced&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#advanced&quot; aria-label=&quot;Anchor link for: advanced&quot;&gt;🔗&lt;&#x2F;a&gt;Advanced&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;what-is-a-context-manager-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-a-context-manager-in-python&quot; aria-label=&quot;Anchor link for: what-is-a-context-manager-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is a context manager in Python?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;In Python, a context manager is an object that defines the methods &lt;code&gt;__enter__()&lt;&#x2F;code&gt; and &lt;code&gt;__exit__()&lt;&#x2F;code&gt;, which allows you to define a block of code that should be executed before and after a specific operation.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;A context manager is used in a &lt;code&gt;with&lt;&#x2F;code&gt; statement, which guarantees that the &lt;code&gt;__enter__()&lt;&#x2F;code&gt; method is called before the block of code is executed, and the &lt;code&gt;__exit__()&lt;&#x2F;code&gt; method is called after the block of code is executed.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;__enter__()&lt;&#x2F;code&gt; method returns an object that can be used within the &lt;code&gt;with&lt;&#x2F;code&gt; block, and the &lt;code&gt;__exit__()&lt;&#x2F;code&gt; method is used to clean up or release any resources that were used by the &lt;code&gt;with&lt;&#x2F;code&gt; block.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; CustomContextManager&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-function&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-function&quot;&gt; __enter__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;self&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function&quot;&gt;        print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;Entering the context.&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt; self&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-function&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-function&quot;&gt; __exit__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;self&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt; exc_type&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt; exc_value&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt; traceback&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function&quot;&gt;        print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;Exiting the context.&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-function&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; do_something&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;self&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function&quot;&gt;        print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;Doing something.&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# Use the context manager to manage a resource&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;with&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt; CustomContextManager&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; as&lt;&#x2F;span&gt;&lt;span&gt; cm&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    cm&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;do_something&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;how-do-you-implement-multithreading-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#how-do-you-implement-multithreading-in-python&quot; aria-label=&quot;Anchor link for: how-do-you-implement-multithreading-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;How do you implement multithreading in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;how-do-you-handle-data-serialization-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#how-do-you-handle-data-serialization-in-python&quot; aria-label=&quot;Anchor link for: how-do-you-handle-data-serialization-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;How do you handle data serialization in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-is-the-difference-between-pickle-and-cpickle-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-the-difference-between-pickle-and-cpickle-in-python&quot; aria-label=&quot;Anchor link for: what-is-the-difference-between-pickle-and-cpickle-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is the difference between pickle and cPickle in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-is-a-python-coroutine&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-a-python-coroutine&quot; aria-label=&quot;Anchor link for: what-is-a-python-coroutine&quot;&gt;🔗&lt;&#x2F;a&gt;What is a Python coroutine?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-is-the-difference-between-a-coroutine-and-a-thread-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-the-difference-between-a-coroutine-and-a-thread-in-python&quot; aria-label=&quot;Anchor link for: what-is-the-difference-between-a-coroutine-and-a-thread-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is the difference between a coroutine and a thread in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;how-do-you-implement-operator-overloading-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#how-do-you-implement-operator-overloading-in-python&quot; aria-label=&quot;Anchor link for: how-do-you-implement-operator-overloading-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;How do you implement operator overloading in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;how-do-you-implement-garbage-collection-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#how-do-you-implement-garbage-collection-in-python&quot; aria-label=&quot;Anchor link for: how-do-you-implement-garbage-collection-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;How do you implement garbage collection in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;how-do-you-handle-memory-management-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#how-do-you-handle-memory-management-in-python&quot; aria-label=&quot;Anchor link for: how-do-you-handle-memory-management-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;How do you handle memory management in Python?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;em&gt;coming soon&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-is-a-metaclass-in-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-a-metaclass-in-python&quot; aria-label=&quot;Anchor link for: what-is-a-metaclass-in-python&quot;&gt;🔗&lt;&#x2F;a&gt;What is a metaclass in Python?&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A metaclass is used to define the behavior of a class, such as how it is instantiated or how its attributes are accessed.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;A metaclass is defined by creating a new class that inherits from &lt;code&gt;type&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;When a new class is defined, Python looks for a metaclass definition in the class definition, and if one is found, it uses that metaclass to create a new class.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; MyMeta&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt;type&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-function&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-function&quot;&gt; __new__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;cls&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt; name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt; bases&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt; attrs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;        # Modify attributes of the class being created&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        attrs[&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;additional_attr&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 42&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt; super&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-function&quot;&gt;__new__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-language&quot;&gt;cls&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; bases&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; attrs)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; MyClass&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt;metaclass&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-other z-inherited-class&quot;&gt;MyMeta&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    pass&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;# Accessing the additional attribute added by the metaclass&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-function&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(MyClass&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;additional_attr)&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt;  # Output: 42&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Hope you benefited from the curation of these questions and answers. For further study you can also explore &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.python.org&#x2F;doc&#x2F;&quot;&gt;official python docs&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
    </entry>
    <entry xml:lang="en">
        <title>LeetCode 347: Top K Frequent Elements | Python</title>
        <published>2023-05-12T00:00:00+00:00</published>
        <updated>2023-05-12T00:00:00+00:00</updated>
        <author>
          <name>Vivek Shukla</name>
        </author>
        <link rel="alternate" type="text/html" href="https://vshukla.com/tech/leetcode-347-top-k-frequent-elements-python/"/>
        <id>https://vshukla.com/tech/leetcode-347-top-k-frequent-elements-python/</id>
        <summary type="html">Leetcode 347: You are given an array nums and an integer k, you need to return k most frequent elements.</summary>
        <content type="html" xml:base="https://vshukla.com/tech/leetcode-347-top-k-frequent-elements-python/">&lt;p&gt;Let’s solve &lt;a rel=&quot;noopener external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;leetcode.com&#x2F;problems&#x2F;top-k-frequent-elements&#x2F;&quot;&gt;Leetcode 347&lt;&#x2F;a&gt; using Python.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;problem-statement&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#problem-statement&quot; aria-label=&quot;Anchor link for: problem-statement&quot;&gt;🔗&lt;&#x2F;a&gt;Problem Statement&lt;&#x2F;h2&gt;
&lt;p&gt;You are given an array &lt;code&gt;nums&lt;&#x2F;code&gt; and an integer &lt;code&gt;k&lt;&#x2F;code&gt;, you need to return &lt;code&gt;k&lt;&#x2F;code&gt; most frequent elements.&lt;&#x2F;p&gt;
&lt;figure&gt;
  &lt;img src=&quot;leetcode-top-k-frequent.png&quot; alt=&quot;leetcode 347&quot; &#x2F;&gt;&lt;figcaption class=&quot;text-sm text-base-content&#x2F;80 text-center mt-2 mb-4&quot;&gt;
      Leetcode 347: Top K Frequent Elements
    &lt;&#x2F;figcaption&gt;&lt;&#x2F;figure&gt;
&lt;h2 id=&quot;problem-explanation&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#problem-explanation&quot; aria-label=&quot;Anchor link for: problem-explanation&quot;&gt;🔗&lt;&#x2F;a&gt;Problem Explanation&lt;&#x2F;h2&gt;
&lt;p&gt;Given array consists of repeated elements and we need to return &lt;code&gt;k&lt;&#x2F;code&gt; which is the number of elements that are most repeated in the array. The length of array is between 1 and 10^5, and it is guaranteed that the answer is unique and there will always be &lt;code&gt;k&lt;&#x2F;code&gt; unique elements.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;solution-1&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#solution-1&quot; aria-label=&quot;Anchor link for: solution-1&quot;&gt;🔗&lt;&#x2F;a&gt;Solution 1&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;strong&gt;Step 1&lt;&#x2F;strong&gt;: To solve this, we would need to find how many times each element is occurring. To do this in the most efficient manner we can go through each element once and store the frequency in a dictionary.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;record&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt; dict&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; num&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; nums&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    record[num]&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; record&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(num&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Step 2&lt;&#x2F;strong&gt;: Sort the record dictionary in descending order of their occurrence.&lt;&#x2F;p&gt;
&lt;p&gt;We are using &lt;code&gt;sorted&lt;&#x2F;code&gt; function which takes an iterable, and we are passing dictionary with &lt;code&gt;.items()&lt;&#x2F;code&gt; it will convert key and value into tuple format.&lt;&#x2F;p&gt;
&lt;p&gt;record = sorted(record.items(), key=lambda x:x[1], reverse=True)&lt;&#x2F;p&gt;
&lt;p&gt;Step 3: Since now record is ordered in descending order of occurrence of elements, we can return the first &lt;code&gt;k&lt;&#x2F;code&gt; elements.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;final-code&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#final-code&quot; aria-label=&quot;Anchor link for: final-code&quot;&gt;🔗&lt;&#x2F;a&gt;Final Code&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Solution&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-function&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; topKFrequent&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;self&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt; nums&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; List[&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt; k&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; List[&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;]:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        record&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt; dict&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span&gt; num&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; nums&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            record[num]&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; record&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(num&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        record&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-function&quot;&gt; sorted&lt;&#x2F;span&gt;&lt;span&gt;(record&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;items&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function-call&quot;&gt; key&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type z-function&quot;&gt;lambda&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt; x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;:x[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function-call&quot;&gt; reverse&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; [elem[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; elem&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; record[&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;k]]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;algorithm-analysis&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#algorithm-analysis&quot; aria-label=&quot;Anchor link for: algorithm-analysis&quot;&gt;🔗&lt;&#x2F;a&gt;Algorithm Analysis&lt;&#x2F;h3&gt;
&lt;p&gt;Time Complexity: O(n log n)&lt;&#x2F;p&gt;
&lt;p&gt;Space Complexity: O(n)&lt;&#x2F;p&gt;
&lt;h2 id=&quot;solution-2&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#solution-2&quot; aria-label=&quot;Anchor link for: solution-2&quot;&gt;🔗&lt;&#x2F;a&gt;Solution 2&lt;&#x2F;h2&gt;
&lt;p&gt;The above solution had the time complexity of O(n log n), can we do better than that? In above solution, the time complexity comes from sorting the dictionary value, so if somehow we don’t need to sort then we might achieve O(n) of time complexity.&lt;&#x2F;p&gt;
&lt;p&gt;To do that we can take a hint from Bucket Sort strategy. We can create a lists of list, where index of the list represents occurrence of an element and then we can return the &lt;code&gt;k&lt;&#x2F;code&gt; number of elements from that bucket list (tail first).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Step 1:&lt;&#x2F;strong&gt; Count the occurrence of each element and store it in the dictionary.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;record&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt; dict&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; num&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; nums&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    record[num]&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; record&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(num&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Step 2:&lt;&#x2F;strong&gt; Store the occurrences in a list &lt;code&gt;bucket&lt;&#x2F;code&gt;. &lt;code&gt;bucket&lt;&#x2F;code&gt; is a list of list, initialized to the length of the array+1, each index represents occurrence, and since there can be more than 1 element of same occurrence therefore we have used list of list.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;bucket&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [[]&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; _&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-function&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-function&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(nums)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;+&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; num&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; freq&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; record&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;items&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    bucket[freq]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;append&lt;&#x2F;span&gt;&lt;span&gt;(num)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Step 3:&lt;&#x2F;strong&gt; Iterate over the &lt;code&gt;bucket&lt;&#x2F;code&gt; list from the tail till we get &lt;code&gt;k&lt;&#x2F;code&gt; number of elements and then return the resulting array.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;final-code-1&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#final-code-1&quot; aria-label=&quot;Anchor link for: final-code-1&quot;&gt;🔗&lt;&#x2F;a&gt;Final Code&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name&quot;&gt; Solution&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-function&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; topKFrequent&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;self&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt; nums&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; List[&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt; k&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; List[&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-section&quot;&gt;]:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        record&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-type&quot;&gt; dict&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        bucket&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [[]&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; _&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-function&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-function&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(nums)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;+&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span&gt; num&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; nums&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            record[num]&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; record&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(num&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span&gt; num&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; freq&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; record&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;items&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            bucket[freq]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;append&lt;&#x2F;span&gt;&lt;span&gt;(num)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        res&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; []&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-function&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-function&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(bucket)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;            for&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; bucket[i]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                res&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-meta z-function-call z-generic&quot;&gt;append&lt;&#x2F;span&gt;&lt;span&gt;(x)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;                if&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-function&quot;&gt; len&lt;&#x2F;span&gt;&lt;span&gt;(res)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; k&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;                    return&lt;&#x2F;span&gt;&lt;span&gt; res&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;algorithm-analysis-1&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#algorithm-analysis-1&quot; aria-label=&quot;Anchor link for: algorithm-analysis-1&quot;&gt;🔗&lt;&#x2F;a&gt;Algorithm Analysis&lt;&#x2F;h3&gt;
&lt;p&gt;Time Complexity: O(n)&lt;&#x2F;p&gt;
&lt;p&gt;Space Complexity: O(n)&lt;&#x2F;p&gt;
</content>
    </entry>
</feed>
