<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ pythonic programming - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ pythonic programming - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Wed, 06 May 2026 17:00:43 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/pythonic-programming/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Mutable vs Immutable Objects in Python – A Visual and Hands-On Guide ]]>
                </title>
                <description>
                    <![CDATA[ Python is an awesome language. Because of its simplicity, many people choose it as their first programming language.  Experienced programmers use Python all the time as well, thanks to its wide community, abundance of packages, and clear syntax. But ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/mutable-vs-immutable-objects-python/</link>
                <guid isPermaLink="false">66c17c4058ee0865d2671b5f</guid>
                
                    <category>
                        <![CDATA[ pythonic programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ immutability ]]>
                    </category>
                
                    <category>
                        <![CDATA[ mutable ]]>
                    </category>
                
                    <category>
                        <![CDATA[ object ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Omer Rosenbaum ]]>
                </dc:creator>
                <pubDate>Wed, 11 Nov 2020 19:01:31 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c95a1740569d1a4ca0dd3.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Python is an awesome language. Because of its simplicity, many people choose it as their first programming language. </p>
<p>Experienced programmers use Python all the time as well, thanks to its wide community, abundance of packages, and clear syntax.</p>
<p>But there's one issue that seems to confuse beginners as well as some experienced developers: Python objects. Specifically, the difference between <strong>mutable</strong> and <strong>immutable</strong> objects.</p>
<p>In this post we will deepen our knowledge of Python objects, learn the difference between <strong>mutable</strong> and <strong>immutable</strong> objects, and see how we can use the <strong>interpreter</strong> to better understand how Python operates. </p>
<p>We will use important functions and keywords such as <code>id</code> and <code>is</code>, and we'll understand the difference between <code>x == y</code> and <code>x is y</code>.</p>
<p>Are you up for it? Let's get started.</p>
<h1 id="heading-in-python-everything-is-an-object">In Python, everything is an object</h1>
<p>Unlike other programming languages where the language <em>supports</em> objects, in Python really <strong>everything</strong> is an object – including integers, lists, and even functions.</p>
<p>We can use our interpreter to verify that:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(<span class="hljs-number">1</span>, object)
<span class="hljs-literal">True</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(<span class="hljs-literal">False</span>, object)
<span class="hljs-literal">True</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_func</span>():</span>
   <span class="hljs-keyword">return</span> <span class="hljs-string">"hello"</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(my_func, object)
<span class="hljs-literal">True</span>
</code></pre>
<p>Python has a built-in function, <code>id</code>, which returns the address of an object in memory. For example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-number">1</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>id(x)
<span class="hljs-number">1470416816</span>
</code></pre>
<p>Above, we created an <strong>object</strong> by the name of <code>x</code>, and assigned it the value of <code>1</code>. We then used <code>id(x)</code> and discovered that this object is found at the address <code>1470416816</code> in memory.</p>
<p>This allows us to check interesting things about Python. Let's say we create two variables in Python – one by the name of <code>x</code>, and one by the name of <code>y</code> – and assign them the same value. For example, here:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-string">"I love Python!"</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>y = <span class="hljs-string">"I love Python!"</span>
</code></pre>
<p>We can use the equality operator (<code>==</code>) to verify that they indeed have the same value in Python's eyes:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x == y
<span class="hljs-literal">True</span>
</code></pre>
<p>But are these the same object in memory? In theory, there can be two very different scenarios here. </p>
<p>According to scenario <strong>(1)</strong>, we really have two different objects, one by the name of <code>x</code>, and another by the name of <code>y</code>, that just happen to have the same value. </p>
<p>Yet, it could also be the case that Python actually stores here only one object, which has two names that reference it – as shown in scenario <strong>(2)</strong>:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/10/image-19.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>We can use the <code>id</code> function introduced above to check this:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-string">"I love Python!"</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>y = <span class="hljs-string">"I love Python!"</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>x == y
<span class="hljs-literal">True</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>id(x)
<span class="hljs-number">52889984</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>id(y)
<span class="hljs-number">52889384</span>
</code></pre>
<p>So as we can see, Python's behavior matches scenario (1) described above. Even though <code>x == y</code> in this example (that is, <code>x</code> and <code>y</code> have the same <em>values</em>), they are different objects in memory. This is because <code>id(x) != id(y)</code>, as we can verify explicitly:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>id(x) == id(y)
<span class="hljs-literal">False</span>
</code></pre>
<p>There is a shorter way to make the comparison above, and that is to use Python's <code>is</code> operator. Checking whether <code>x is y</code> is the same as checking <code>id(x) == id(y)</code>, which means whether <code>x</code> and <code>y</code> are the same object in memory:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x == y
<span class="hljs-literal">True</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>id(x) == id(y)
<span class="hljs-literal">False</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>x <span class="hljs-keyword">is</span> y
<span class="hljs-literal">False</span>
</code></pre>
<p>This sheds light on the important difference between the equality operator <code>==</code> and the identity operator <code>is</code>. </p>
<p>As you can see in the example above, it is completely possible for two names in Python (<code>x</code> and <code>y</code>) to be bound to two different objects (and thus, <code>x is y</code> is <code>False</code>), where these two objects have the same value (so <code>x == y</code> is <code>True</code>).</p>
<p>How can we create another variable that points to the same object that <code>x</code> is pointing to? We can simply use the assignment operator <code>=</code>, like so:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-string">"I love Python!"</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>z = x
</code></pre>
<p>To verify that they indeed point to the same object, we can use the <code>is</code> operator:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x <span class="hljs-keyword">is</span> z
<span class="hljs-literal">True</span>
</code></pre>
<p>Of course, this means they have the same address in memory, as we can verify explicitly by using <code>id</code>:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>id(x)
<span class="hljs-number">54221824</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>id(z)
<span class="hljs-number">54221824</span>
</code></pre>
<p>And, of course, they have the same value, so we expect <code>x == z</code> to return <code>True</code> as well:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x == z
<span class="hljs-literal">True</span>
</code></pre>
<h1 id="heading-mutable-and-immutable-objects-in-python">Mutable and immutable objects in Python</h1>
<p>We have said that everything in Python is an object, yet there is an important distinction between objects. Some objects are <strong>mutable</strong> while some are <strong>immutable</strong>. </p>
<p>As I mentioned before, this fact causes confusion for many people who are new to Python, so we are going to make sure it's clear.</p>
<h2 id="heading-immutable-objects-in-python">Immutable objects in Python</h2>
<p>For some types in Python, once we have created instances of those types, they never change. They are <strong>immutable</strong>. </p>
<p>For example, <code>int</code> objects are immutable in Python. What will happen if we try to change the value of an <code>int</code> object?</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-number">24601</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>x
<span class="hljs-number">24601</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-number">24602</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>x
<span class="hljs-number">24602</span>
</code></pre>
<p>Well, it seems that we changed <code>x</code> successfully. This is exactly where many people get confused. What exactly happened under the hood here? Let's use <code>id</code> to further investigate:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-number">24601</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>x
<span class="hljs-number">24601</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>id(x)
<span class="hljs-number">1470416816</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>x = <span class="hljs-number">24602</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>x
<span class="hljs-number">24602</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>id(x)
<span class="hljs-number">1470416832</span>
</code></pre>
<p>So we can see that by assigning <code>x = 24602</code>, we didn't change the value of the object that <code>x</code> had been bound to before. Rather, we created a new object, and bound the name <code>x</code> to it. </p>
<p>So after assigning <code>24601</code> to <code>x</code> by using <code>x = 24601</code>, we had the following state:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/10/image-46.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>And after using <code>x = 24602</code>, we created a new object, and bound the name <code>x</code> to this new object. The other object with the value of <code>24601</code> is no longer reachable by <code>x</code> (or any other name in this case):</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/10/image-47.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Whenever we assign a new value to a name (in the above example - <code>x</code>) that is bound to an <code>int</code> object, we actually change the binding of that name to another object. </p>
<p>The same applies for <code>tuple</code>s, strings (<code>str</code> objects), and <code>bool</code>s as well. In other words, <code>int</code> (and other number types such as <code>float</code>), <code>tuple</code>, <code>bool</code>, and <code>str</code> objects are <strong>immutable</strong>.</p>
<p>Let's test this hypothesis. What happens if we create a <code>tuple</code> object, and then give it a different value? </p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>my_tuple = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>id(my_tuple)
<span class="hljs-number">54263304</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>my_tuple = (<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>id(my_tuple)
<span class="hljs-number">56898184</span>
</code></pre>
<p>Just like an <code>int</code> object, we can see that our assignment actually changed the object that the name <code>my_tuple</code> is bound to.</p>
<p>What happens if we try to change one of the <code>tuple</code>'s elements?</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>my_tuple[<span class="hljs-number">0</span>] = <span class="hljs-string">'a new value'</span>
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;stdin&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
TypeError: <span class="hljs-string">'tuple'</span> object does <span class="hljs-keyword">not</span> support item assignment
</code></pre>
<p>As we can see, Python doesn't allow us to modify <code>my_tuple</code>'s contents, as it is immutable.</p>
<h2 id="heading-mutable-objects-in-python">Mutable objects in Python</h2>
<p>Some types in Python can be modified after creation, and they are called <strong>mutable</strong>. For example, we know that we can modify the contents of a <code>list</code> object:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>my_list = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>my_list[<span class="hljs-number">0</span>] = <span class="hljs-string">'a new value'</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>my_list
[<span class="hljs-string">'a new value'</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
</code></pre>
<p>Does that mean we actually created a new object when assigning a new value to the first element of <code>my_list</code>? Again, we can use <code>id</code> to check:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>my_list = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>id(my_list)
<span class="hljs-number">55834760</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>my_list
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]

<span class="hljs-meta">&gt;&gt;&gt; </span>my_list[<span class="hljs-number">0</span>] = <span class="hljs-string">'a new value'</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>id(my_list)
<span class="hljs-number">55834760</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>my_list
[<span class="hljs-string">'a new value'</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
</code></pre>
<p>So our first assignment <code>my_list = [1, 2, 3]</code> created an object in the address <code>55834760</code>, with the values of <code>1</code>, <code>2</code>, and <code>3</code>:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/10/image-22.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>We then modified the first element of this <code>list</code> object using <code>my_list[0] = 'a new value'</code>, that is - without creating a new <code>list</code> object:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/10/image-23.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now, let us create two names – <code>x</code> and <code>y</code>, both bound to the same <code>list</code> object. We can verify that either by using <code>is</code>, or by explicitly checking their <code>id</code>s:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = y = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>x <span class="hljs-keyword">is</span> y
<span class="hljs-literal">True</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>id(x)
<span class="hljs-number">18349096</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>id(y)
<span class="hljs-number">18349096</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>id(x) == id(y)
<span class="hljs-literal">True</span>
</code></pre>
<p>What happens now if we use <code>x.append(3)</code>? That is, if we add a new element (<code>3</code>) to the object by the name of <code>x</code>?</p>
<p>Will <code>x</code> by changed? Will <code>y</code>?</p>
<p>Well, as we already know, they are basically two names of the same object:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/10/image-28.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Since this object is changed, when we check its names we can see the new value:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x.append(<span class="hljs-number">3</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>x
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]

<span class="hljs-meta">&gt;&gt;&gt; </span>y
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
</code></pre>
<p>Note that <code>x</code> and <code>y</code> have the same <code>id</code> as before – as they are still bound to the same <code>list</code> object:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>id(x)
<span class="hljs-number">18349096</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>id(y)
<span class="hljs-number">18349096</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/10/image-27.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>In addition to <code>list</code>s, other Python types that are mutable include <code>set</code>s and <code>dict</code>s.</p>
<h1 id="heading-implications-for-dictionary-keys-in-python">Implications for dictionary keys in Python</h1>
<p>Dictionaries (<code>dict</code> objects) are commonly used in Python. As a quick reminder, we define them like so:</p>
<pre><code class="lang-python">my_dict = {<span class="hljs-string">"name"</span>: <span class="hljs-string">"Omer"</span>, <span class="hljs-string">"number_of_pets"</span>: <span class="hljs-number">1</span>}
</code></pre>
<p>We can then access a specific element by its key name:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>my_dict[<span class="hljs-string">"name"</span>]
<span class="hljs-string">'Omer'</span>
</code></pre>
<p>Dictionaries are <strong>mutable</strong>, so we can change their content after creation. At any given moment, a key in the dictionary can point to one element only:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>my_dict[<span class="hljs-string">"name"</span>] = <span class="hljs-string">"John"</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>my_dict[<span class="hljs-string">"name"</span>]
<span class="hljs-string">'John'</span>
</code></pre>
<p>It is interesting to note that a <strong>dictionary's keys must be immutable</strong>:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>my_dict = {[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>]: <span class="hljs-string">"Hello"</span>}
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;stdin&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
TypeError: unhashable type: <span class="hljs-string">'list'</span>
</code></pre>
<p>Why is that so? </p>
<p>Let's consider the following hypothetical scenario (note: the snippet below can't really be run in Python):</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>y = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>my_dict = {x: <span class="hljs-string">'a'</span>, y: <span class="hljs-string">'b'</span>}
</code></pre>
<p>So far, things don't seem that bad. We'd assume that if we access <code>my_dict</code> with the key of <code>[1, 2]</code>, we will get the corresponding value of <code>'a'</code>, and if we access the key <code>[1, 2, 3]</code>, we will get the value <code>'b'</code>. </p>
<p>Now, what would happen if we attempted to use:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x.append(<span class="hljs-number">3</span>)
</code></pre>
<p>In this case, <code>x</code> would have the value of <code>[1, 2, 3]</code>, and <code>y</code> would also have the value of <code>[1, 2, 3]</code>. What should we get when we ask for <code>my_dict[[1, 2, 3]]</code>? Will it be <code>'a'</code> or <code>'b'</code>? To avoid such cases, Python simply doesn't allow dictionary keys to be mutable.</p>
<h1 id="heading-taking-things-a-bit-further">Taking things a bit further</h1>
<p>Let's try to apply our knowledge to a case that is a bit more interesting.</p>
<p>Below, we define a <code>list</code> (a <strong>mutable</strong> object) and a <code>tuple</code> (an <strong>immutable</strong> object). The <code>list</code> includes a <code>tuple</code>, and the <code>tuple</code> includes a <code>list</code>:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>my_list = [(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>), <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>my_tuple = ([<span class="hljs-number">1</span>, <span class="hljs-number">1</span>], <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>type(my_list)
&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">list</span>'&gt;

&gt;&gt;&gt; <span class="hljs-title">type</span>(<span class="hljs-params">my_list[<span class="hljs-number">0</span>]</span>)
&lt;<span class="hljs-title">class</span> '<span class="hljs-title">tuple</span>'&gt;

&gt;&gt;&gt; <span class="hljs-title">type</span>(<span class="hljs-params">my_tuple</span>)
&lt;<span class="hljs-title">class</span> '<span class="hljs-title">tuple</span>'&gt;

&gt;&gt;&gt; <span class="hljs-title">type</span>(<span class="hljs-params">my_tuple[<span class="hljs-number">0</span>]</span>)
&lt;<span class="hljs-title">class</span> '<span class="hljs-title">list</span>'&gt;</span>
</code></pre>
<p>So far so good. Now, try to think for yourself – what will happen when we try to execute each of the following statements?</p>
<p>(1) <code>&gt;&gt;&gt; my_list[0][0] = 'Changed!'</code></p>
<p>(2) <code>&gt;&gt;&gt; my_tuple[0][0] = 'Changed!'</code></p>
<p>In statement (1), what we are trying to do is change <code>my_list</code>'s first element, that is, a <code>tuple</code>. Since a <code>tuple</code> is <strong>immutable</strong>, this attempt is destined to fail:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>my_list[<span class="hljs-number">0</span>][<span class="hljs-number">0</span>] = <span class="hljs-string">'Changed!'</span>
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;stdin&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
TypeError: <span class="hljs-string">'tuple'</span> object does <span class="hljs-keyword">not</span> support item assignment
</code></pre>
<p>Note that what we were trying to do is <em>not</em> change the list, but rather – change the contents of its first element. </p>
<p>Let's consider statement (2). In this case, we are accessing <code>my_tuple</code>'s first element, which happens to be a <code>list</code>, and modify it. Let's further investigate this case and look at the addresses of these elements:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>my_tuple = ([<span class="hljs-number">1</span>, <span class="hljs-number">1</span>], <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>id(my_tuple)
<span class="hljs-number">20551816</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>type(my_tuple[<span class="hljs-number">0</span>])
&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">list</span>'&gt;

&gt;&gt;&gt; <span class="hljs-title">id</span>(<span class="hljs-params">my_tuple[<span class="hljs-number">0</span>]</span>)
20446248</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/10/image-29.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>When we change <code>my_tuple[0][0]</code>, we do not really change <code>my_tuple</code> at all! Indeed, after the change, <code>my_tuple</code>'s first element will still be the object whose address in memory is <code>20446248</code>. We do, however, change the value of that object:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>my_tuple[<span class="hljs-number">0</span>][<span class="hljs-number">0</span>] = <span class="hljs-string">'Changed!'</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>id(my_tuple)
<span class="hljs-number">20551816</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>id(my_tuple[<span class="hljs-number">0</span>])
<span class="hljs-number">20446248</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>my_tuple
([<span class="hljs-string">'Changed!'</span>, <span class="hljs-number">1</span>], <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/10/image-48.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Since we only modified the value of <code>my_tuple[0]</code>, which is a mutable <code>list</code> object, this operation was indeed allowed by Python.</p>
<h1 id="heading-recap">Recap</h1>
<p>In this post we learned about Python objects. We said that in Python <strong>everything is an object</strong>, and got to use <code>id</code> and <code>is</code> to deepen our understanding of what's happening under the hood when using Python to create and modify objects.</p>
<p>We also learned the difference between <strong>mutable</strong> objects, that can be modified after creation, and <strong>immutable</strong> objects, which cannot. </p>
<p>We saw that when we ask Python to modify an immutable object that is bound to a certain name, we actually create a new object and bind that name to it.</p>
<p>We then learned why dictionary keys have to be <strong>immutable</strong> in Python.</p>
<p>Understanding how Python "sees" objects is a key to becoming a better Python programmer. I hope this post has helped you on your journey to mastering Python.</p>
<p><a target="_blank" href="https://www.linkedin.com/in/omer-rosenbaum-034a08b9/"><em>Omer Rosenbaum</em></a><em>,</em> <a target="_blank" href="https://swimm.io/"><em>Swimm</em></a><em>’s Chief Technology Officer. Cyber training expert and Founder of Checkpoint Security Academy. Author of</em> <a target="_blank" href="https://data.cyber.org.il/networks/networks.pdf"><em>Computer Networks (in Hebrew)</em></a><em>. Visit My</em> <a target="_blank" href="https://www.youtube.com/watch?v=79jlgESHzKQ&amp;list=PL9lx0DXCC4BMS7dB7vsrKI5wzFyVIk2Kg"><em>YouTube Channel</em></a><em>.</em></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
