<?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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vishal khabale on Medium]]></title>
        <description><![CDATA[Stories by Vishal khabale on Medium]]></description>
        <link>https://medium.com/@khabalevishal34?source=rss-f2d056a59ff3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tU90EMn9TqfC8Tdb99Lxeg.jpeg</url>
            <title>Stories by Vishal khabale on Medium</title>
            <link>https://medium.com/@khabalevishal34?source=rss-f2d056a59ff3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 23:01:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@khabalevishal34/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Mutable and Immutable Objects in Python]]></title>
            <link>https://medium.com/@khabalevishal34/sure-heres-a-simple-blog-style-explanation-of-mutable-and-immutable-objects-in-python-suitable-856fd5541749?source=rss-f2d056a59ff3------2</link>
            <guid isPermaLink="false">https://medium.com/p/856fd5541749</guid>
            <dc:creator><![CDATA[Vishal khabale]]></dc:creator>
            <pubDate>Tue, 06 Oct 2026 05:11:38 GMT</pubDate>
            <atom:updated>2026-10-06T05:17:19.926Z</atom:updated>
            <content:encoded><![CDATA[<p>Python has different types of objects, and one important difference between them is whether they are <strong>mutable</strong> or <strong>immutable</strong>.</p><h3>What is Mutability?</h3><p><strong>Mutability</strong> means whether an object can be changed after it has been created.</p><ul><li><strong>Mutable objects</strong> can be changed after creation.</li><li><strong>Immutable objects</strong> cannot be changed after creation.</li></ul><p>This concept is important because it affects how variables behave when we modify them.</p><h3>Mutable Objects in Python</h3><p>A <strong>mutable object</strong> can be modified without creating a completely new object.</p><p>Common mutable data types are:</p><ul><li>list</li><li>dict</li><li>set</li><li>bytearray</li></ul><h3>Example of a Mutable List</h3><pre>numbers = [10, 20, 30]</pre><pre>numbers[0] = 100</pre><pre>print(numbers)</pre><p>Output:</p><pre>[100, 20, 30]</pre><p>Here, we changed the first element of the list from 10 to 100. The list itself was modified.</p><h3>Example with Two Variables</h3><pre>a = [1, 2, 3]<br>b = a</pre><pre>b.append(4)</pre><pre>print(a)<br>print(b)</pre><p>Output:</p><pre>[1, 2, 3, 4]<br>[1, 2, 3, 4]</pre><p>Both a and b refer to the <strong>same list object</strong>. Therefore, changing the list through b also affects a.</p><h3>Immutable Objects in Python</h3><p>An <strong>immutable object</strong> cannot be changed after it has been created.</p><p>Common immutable data types are:</p><ul><li>int</li><li>float</li><li>bool</li><li>str</li><li>tuple</li><li>frozenset</li><li>bytes</li></ul><h3>Example of an Immutable Integer</h3><pre>x = 10</pre><pre>x = x + 5</pre><pre>print(x)</pre><p>Output:</p><pre>15</pre><p>It may look like we changed 10 into 15, but Python actually creates a <strong>new integer object</strong> containing 15. The original integer object 10 cannot be modified.</p><h3>Example with Strings</h3><pre>name = &quot;Python&quot;</pre><pre>name = name + &quot; Programming&quot;</pre><pre>print(name)</pre><p>Output:</p><pre>Python Programming</pre><p>Strings are immutable. We cannot directly change a character inside a string.</p><p>For example:</p><pre>name = &quot;Python&quot;</pre><pre>name[0] = &quot;J&quot;</pre><p>This produces an error:</p><pre>TypeError: &#39;str&#39; object does not support item assignment</pre><p>Instead, a new string must be created.</p><h3>Mutable vs Immutable</h3><p>MutableImmutable Can be changed after creationCannot be changed after creation listint dictfloat setstr bytearraytuple Changes can happen to the same objectChanges create a new object</p><h3>Simple Example to Remember</h3><p>Think of a <strong>list as a notebook</strong>. You can erase or add information to the same notebook.</p><pre>items = [&quot;Apple&quot;, &quot;Banana&quot;]<br>items.append(&quot;Mango&quot;)</pre><p>The same list is modified.</p><p>Think of a <strong>string as a printed book</strong>. You cannot change a word directly on the printed page. You need to create a new version.</p><pre>text = &quot;Hello&quot;<br>text = text + &quot; World&quot;</pre><p>A new string is created.</p><h3>Conclusion</h3><p>Understanding mutable and immutable objects is an important part of learning Python.</p><p><strong>Mutable objects</strong> can be modified after creation, such as lists, dictionaries, and sets. <strong>Immutable objects</strong> cannot be modified after creation, such as integers, strings, and tuples.</p><p>A simple rule to remember is:</p><blockquote><strong><em>Mutable = can change</em></strong><em><br> </em><strong><em>Immutable = cannot change</em></strong></blockquote><p>This concept becomes especially important when working with <strong>functions, variables, loops, and object references</strong> in Python.Sure! Here’s a simple blog-style explanation of <strong>Mutable and Immutable objects in Python</strong>, suitable for beginners.</p><h3>Mutable and Immutable Objects in Python</h3><p>Python has different types of objects, and one important difference between them is whether they are <strong>mutable</strong> or <strong>immutable</strong>.</p><h3>What is Mutability?</h3><p><strong>Mutability</strong> means whether an object can be changed after it has been created.</p><ul><li><strong>Mutable objects</strong> can be changed after creation.</li><li><strong>Immutable objects</strong> cannot be changed after creation.</li></ul><p>This concept is important because it affects how variables behave when we modify them.</p><h3>Mutable Objects in Python</h3><p>A <strong>mutable object</strong> can be modified without creating a completely new object.</p><p>Common mutable data types are:</p><ul><li>list</li><li>dict</li><li>set</li><li>bytearray</li></ul><h3>Example of a Mutable List</h3><pre>numbers = [10, 20, 30]</pre><pre>numbers[0] = 100</pre><pre>print(numbers)</pre><p>Output:</p><pre>[100, 20, 30]</pre><p>Here, we changed the first element of the list from 10 to 100. The list itself was modified.</p><h3>Example with Two Variables</h3><pre>a = [1, 2, 3]<br>b = a</pre><pre>b.append(4)</pre><pre>print(a)<br>print(b)</pre><p>Output:</p><pre>[1, 2, 3, 4]<br>[1, 2, 3, 4]</pre><p>Both a and b refer to the <strong>same list object</strong>. Therefore, changing the list through b also affects a.</p><h3>Immutable Objects in Python</h3><p>An <strong>immutable object</strong> cannot be changed after it has been created.</p><p>Common immutable data types are:</p><ul><li>int</li><li>float</li><li>bool</li><li>str</li><li>tuple</li><li>frozenset</li><li>bytes</li></ul><h3>Example of an Immutable Integer</h3><pre>x = 10</pre><pre>x = x + 5</pre><pre>print(x)</pre><p>Output:</p><pre>15</pre><p>It may look like we changed 10 into 15, but Python actually creates a <strong>new integer object</strong> containing 15. The original integer object 10 cannot be modified.</p><h3>Example with Strings</h3><pre>name = &quot;Python&quot;</pre><pre>name = name + &quot; Programming&quot;</pre><pre>print(name)</pre><p>Output:</p><pre>Python Programming</pre><p>Strings are immutable. We cannot directly change a character inside a string.</p><p>For example:</p><pre>name = &quot;Python&quot;</pre><pre>name[0] = &quot;J&quot;</pre><p>This produces an error:</p><pre>TypeError: &#39;str&#39; object does not support item assignment</pre><p>Instead, a new string must be created.</p><h3>Mutable vs Immutable</h3><p>MutableImmutable Can be changed after creationCannot be changed after creation listint dictfloat setstr bytearraytuple Changes can happen to the same objectChanges create a new object</p><h3>Simple Example to Remember</h3><p>Think of a <strong>list as a notebook</strong>. You can erase or add information to the same notebook.</p><pre>items = [&quot;Apple&quot;, &quot;Banana&quot;]<br>items.append(&quot;Mango&quot;)</pre><p>The same list is modified.</p><p>Think of a <strong>string as a printed book</strong>. You cannot change a word directly on the printed page. You need to create a new version.</p><pre>text = &quot;Hello&quot;<br>text = text + &quot; World&quot;</pre><p>A new string is created.</p><h3>Conclusion</h3><p>Understanding mutable and immutable objects is an important part of learning Python.</p><p><strong>Mutable objects</strong> can be modified after creation, such as lists, dictionaries, and sets. <strong>Immutable objects</strong> cannot be modified after creation, such as integers, strings, and tuples.</p><p>A simple rule to remember is:</p><blockquote><strong><em>Mutable = can change</em></strong><em><br> </em><strong><em>Immutable = cannot change</em></strong></blockquote><p>This concept becomes especially important when working with <strong>functions, variables, loops, and object references</strong> in Python.Sure! Here’s a simple blog-style explanation of <strong>Mutable and Immutable objects in Python</strong>, suitable for beginners.</p><h3>Mutable and Immutable Objects in Python</h3><p>Python has different types of objects, and one important difference between them is whether they are <strong>mutable</strong> or <strong>immutable</strong>.</p><h3>What is Mutability?</h3><p><strong>Mutability</strong> means whether an object can be changed after it has been created.</p><ul><li><strong>Mutable objects</strong> can be changed after creation.</li><li><strong>Immutable objects</strong> cannot be changed after creation.</li></ul><p>This concept is important because it affects how variables behave when we modify them.</p><h3>Mutable Objects in Python</h3><p>A <strong>mutable object</strong> can be modified without creating a completely new object.</p><p>Common mutable data types are:</p><ul><li>list</li><li>dict</li><li>set</li><li>bytearray</li></ul><h3>Example of a Mutable List</h3><pre>numbers = [10, 20, 30]</pre><pre>numbers[0] = 100</pre><pre>print(numbers)</pre><p>Output:</p><pre>[100, 20, 30]</pre><p>Here, we changed the first element of the list from 10 to 100. The list itself was modified.</p><h3>Example with Two Variables</h3><pre>a = [1, 2, 3]<br>b = a</pre><pre>b.append(4)</pre><pre>print(a)<br>print(b)</pre><p>Output:</p><pre>[1, 2, 3, 4]<br>[1, 2, 3, 4]</pre><p>Both a and b refer to the <strong>same list object</strong>. Therefore, changing the list through b also affects a.</p><h3>Immutable Objects in Python</h3><p>An <strong>immutable object</strong> cannot be changed after it has been created.</p><p>Common immutable data types are:</p><ul><li>int</li><li>float</li><li>bool</li><li>str</li><li>tuple</li><li>frozenset</li><li>bytes</li></ul><h3>Example of an Immutable Integer</h3><pre>x = 10</pre><pre>x = x + 5</pre><pre>print(x)</pre><p>Output:</p><pre>15</pre><p>It may look like we changed 10 into 15, but Python actually creates a <strong>new integer object</strong> containing 15. The original integer object 10 cannot be modified.</p><h3>Example with Strings</h3><pre>name = &quot;Python&quot;</pre><pre>name = name + &quot; Programming&quot;</pre><pre>print(name)</pre><p>Output:</p><pre>Python Programming</pre><p>Strings are immutable. We cannot directly change a character inside a string.</p><p>For example:</p><pre>name = &quot;Python&quot;</pre><pre>name[0] = &quot;J&quot;</pre><p>This produces an error:</p><pre>TypeError: &#39;str&#39; object does not support item assignment</pre><p>Instead, a new string must be created.</p><h3>Mutable vs Immutable</h3><p>MutableImmutable Can be changed after creationCannot be changed after creation listint dictfloat setstr bytearraytuple Changes can happen to the same objectChanges create a new object</p><h3>Simple Example to Remember</h3><p>Think of a <strong>list as a notebook</strong>. You can erase or add information to the same notebook.</p><pre>items = [&quot;Apple&quot;, &quot;Banana&quot;]<br>items.append(&quot;Mango&quot;)</pre><p>The same list is modified.</p><p>Think of a <strong>string as a printed book</strong>. You cannot change a word directly on the printed page. You need to create a new version.</p><pre>text = &quot;Hello&quot;<br>text = text + &quot; World&quot;</pre><p>A new string is created.</p><h3>Conclusion</h3><p>Understanding mutable and immutable objects is an important part of learning Python.</p><p><strong>Mutable objects</strong> can be modified after creation, such as lists, dictionaries, and sets. <strong>Immutable objects</strong> cannot be modified after creation, such as integers, strings, and tuples.</p><p>A simple rule to remember is:</p><blockquote><strong><em>Mutable = can change</em></strong><em><br> </em><strong><em>Immutable = cannot change</em></strong></blockquote><p>This concept becomes especially important when working with <strong>functions, variables, loops, and object references</strong> in Python.Sure! Here’s a simple blog-style explanation of <strong>Mutable and Immutable objects in Python</strong>, suitable for beginners.</p><h3>Mutable and Immutable Objects in Python</h3><p>Python has different types of objects, and one important difference between them is whether they are <strong>mutable</strong> or <strong>immutable</strong>.</p><h3>What is Mutability?</h3><p><strong>Mutability</strong> means whether an object can be changed after it has been created.</p><ul><li><strong>Mutable objects</strong> can be changed after creation.</li><li><strong>Immutable objects</strong> cannot be changed after creation.</li></ul><p>This concept is important because it affects how variables behave when we modify them.</p><h3>Mutable Objects in Python</h3><p>A <strong>mutable object</strong> can be modified without creating a completely new object.</p><p>Common mutable data types are:</p><ul><li>list</li><li>dict</li><li>set</li><li>bytearray</li></ul><h3>Example of a Mutable List</h3><pre>numbers = [10, 20, 30]</pre><pre>numbers[0] = 100</pre><pre>print(numbers)</pre><p>Output:</p><pre>[100, 20, 30]</pre><p>Here, we changed the first element of the list from 10 to 100. The list itself was modified.</p><h3>Example with Two Variables</h3><pre>a = [1, 2, 3]<br>b = a</pre><pre>b.append(4)</pre><pre>print(a)<br>print(b)</pre><p>Output:</p><pre>[1, 2, 3, 4]<br>[1, 2, 3, 4]</pre><p>Both a and b refer to the <strong>same list object</strong>. Therefore, changing the list through b also affects a.</p><h3>Immutable Objects in Python</h3><p>An <strong>immutable object</strong> cannot be changed after it has been created.</p><p>Common immutable data types are:</p><ul><li>int</li><li>float</li><li>bool</li><li>str</li><li>tuple</li><li>frozenset</li><li>bytes</li></ul><h3>Example of an Immutable Integer</h3><pre>x = 10</pre><pre>x = x + 5</pre><pre>print(x)</pre><p>Output:</p><pre>15</pre><p>It may look like we changed 10 into 15, but Python actually creates a <strong>new integer object</strong> containing 15. The original integer object 10 cannot be modified.</p><h3>Example with Strings</h3><pre>name = &quot;Python&quot;</pre><pre>name = name + &quot; Programming&quot;</pre><pre>print(name)</pre><p>Output:</p><pre>Python Programming</pre><p>Strings are immutable. We cannot directly change a character inside a string.</p><p>For example:</p><pre>name = &quot;Python&quot;</pre><pre>name[0] = &quot;J&quot;</pre><p>This produces an error:</p><pre>TypeError: &#39;str&#39; object does not support item assignment</pre><p>Instead, a new string must be created.</p><h3>Mutable vs Immutable</h3><p>MutableImmutable Can be changed after creationCannot be changed after creation listint dictfloat setstr bytearraytuple Changes can happen to the same objectChanges create a new object</p><h3>Simple Example to Remember</h3><p>Think of a <strong>list as a notebook</strong>. You can erase or add information to the same notebook.</p><pre>items = [&quot;Apple&quot;, &quot;Banana&quot;]<br>items.append(&quot;Mango&quot;)</pre><p>The same list is modified.</p><p>Think of a <strong>string as a printed book</strong>. You cannot change a word directly on the printed page. You need to create a new version.</p><pre>text = &quot;Hello&quot;<br>text = text + &quot; World&quot;</pre><p>A new string is created.</p><h3>Conclusion</h3><p>Understanding mutable and immutable objects is an important part of learning Python.</p><p><strong>Mutable objects</strong> can be modified after creation, such as lists, dictionaries, and sets. <strong>Immutable objects</strong> cannot be modified after creation, such as integers, strings, and tuples.</p><p>A simple rule to remember is:</p><blockquote><strong><em>Mutable = can change</em></strong><em><br> </em><strong><em>Immutable = cannot change</em></strong></blockquote><p>This concept becomes especially important when working with <strong>functions, variables, loops, and object references</strong> in Python.Sure! Here’s a simple blog-style explanation of <strong>Mutable and Immutable objects in Python</strong>, suitable for beginners.</p><h3>Mutable and Immutable Objects in Python</h3><p>Python has different types of objects, and one important difference between them is whether they are <strong>mutable</strong> or <strong>immutable</strong>.</p><h3>What is Mutability?</h3><p><strong>Mutability</strong> means whether an object can be changed after it has been created.</p><ul><li><strong>Mutable objects</strong> can be changed after creation.</li><li><strong>Immutable objects</strong> cannot be changed after creation.</li></ul><p>This concept is important because it affects how variables behave when we modify them.</p><h3>Mutable Objects in Python</h3><p>A <strong>mutable object</strong> can be modified without creating a completely new object.</p><p>Common mutable data types are:</p><ul><li>list</li><li>dict</li><li>set</li><li>bytearray</li></ul><h3>Example of a Mutable List</h3><pre>numbers = [10, 20, 30]</pre><pre>numbers[0] = 100</pre><pre>print(numbers)</pre><p>Output:</p><pre>[100, 20, 30]</pre><p>Here, we changed the first element of the list from 10 to 100. The list itself was modified.</p><h3>Example with Two Variables</h3><pre>a = [1, 2, 3]<br>b = a</pre><pre>b.append(4)</pre><pre>print(a)<br>print(b)</pre><p>Output:</p><pre>[1, 2, 3, 4]<br>[1, 2, 3, 4]</pre><p>Both a and b refer to the <strong>same list object</strong>. Therefore, changing the list through b also affects a.</p><h3>Immutable Objects in Python</h3><p>An <strong>immutable object</strong> cannot be changed after it has been created.</p><p>Common immutable data types are:</p><ul><li>int</li><li>float</li><li>bool</li><li>str</li><li>tuple</li><li>frozenset</li><li>bytes</li></ul><h3>Example of an Immutable Integer</h3><pre>x = 10</pre><pre>x = x + 5</pre><pre>print(x)</pre><p>Output:</p><pre>15</pre><p>It may look like we changed 10 into 15, but Python actually creates a <strong>new integer object</strong> containing 15. The original integer object 10 cannot be modified.</p><h3>Example with Strings</h3><pre>name = &quot;Python&quot;</pre><pre>name = name + &quot; Programming&quot;</pre><pre>print(name)</pre><p>Output:</p><pre>Python Programming</pre><p>Strings are immutable. We cannot directly change a character inside a string.</p><p>For example:</p><pre>name = &quot;Python&quot;</pre><pre>name[0] = &quot;J&quot;</pre><p>This produces an error:</p><pre>TypeError: &#39;str&#39; object does not support item assignment</pre><p>Instead, a new string must be created.</p><h3>Mutable vs Immutable</h3><p>MutableImmutable Can be changed after creationCannot be changed after creation listint dictfloat setstr bytearraytuple Changes can happen to the same objectChanges create a new object</p><h3>Simple Example to Remember</h3><p>Think of a <strong>list as a notebook</strong>. You can erase or add information to the same notebook.</p><pre>items = [&quot;Apple&quot;, &quot;Banana&quot;]<br>items.append(&quot;Mango&quot;)</pre><p>The same list is modified.</p><p>Think of a <strong>string as a printed book</strong>. You cannot change a word directly on the printed page. You need to create a new version.</p><pre>text = &quot;Hello&quot;<br>text = text + &quot; World&quot;</pre><p>A new string is created.</p><h3>Conclusion</h3><p>Understanding mutable and immutable objects is an important part of learning Python.</p><p><strong>Mutable objects</strong> can be modified after creation, such as lists, dictionaries, and sets. <strong>Immutable objects</strong> cannot be modified after creation, such as integers, strings, and tuples.</p><p>A simple rule to remember is:</p><blockquote><strong><em>Mutable = can change</em></strong><em><br> </em><strong><em>Immutable = cannot change</em></strong></blockquote><p>This concept becomes especially important when working with <strong>functions, variables, loops, and object references</strong> in Python.Sure! Here’s a simple blog-style explanation of <strong>Mutable and Immutable objects in Python</strong>, suitable for beginners.</p><h3>Mutable and Immutable Objects in Python</h3><p>Python has different types of objects, and one important difference between them is whether they are <strong>mutable</strong> or <strong>immutable</strong>.</p><h3>What is Mutability?</h3><p><strong>Mutability</strong> means whether an object can be changed after it has been created.</p><ul><li><strong>Mutable objects</strong> can be changed after creation.</li><li><strong>Immutable objects</strong> cannot be changed after creation.</li></ul><p>This concept is important because it affects how variables behave when we modify them.</p><h3>Mutable Objects in Python</h3><p>A <strong>mutable object</strong> can be modified without creating a completely new object.</p><p>Common mutable data types are:</p><ul><li>list</li><li>dict</li><li>set</li><li>bytearray</li></ul><h3>Example of a Mutable List</h3><pre>numbers = [10, 20, 30]</pre><pre>numbers[0] = 100</pre><pre>print(numbers)</pre><p>Output:</p><pre>[100, 20, 30]</pre><p>Here, we changed the first element of the list from 10 to 100. The list itself was modified.</p><h3>Example with Two Variables</h3><pre>a = [1, 2, 3]<br>b = a</pre><pre>b.append(4)</pre><pre>print(a)<br>print(b)</pre><p>Output:</p><pre>[1, 2, 3, 4]<br>[1, 2, 3, 4]</pre><p>Both a and b refer to the <strong>same list object</strong>. Therefore, changing the list through b also affects a.</p><h3>Immutable Objects in Python</h3><p>An <strong>immutable object</strong> cannot be changed after it has been created.</p><p>Common immutable data types are:</p><ul><li>int</li><li>float</li><li>bool</li><li>str</li><li>tuple</li><li>frozenset</li><li>bytes</li></ul><h3>Example of an Immutable Integer</h3><pre>x = 10</pre><pre>x = x + 5</pre><pre>print(x)</pre><p>Output:</p><pre>15</pre><p>It may look like we changed 10 into 15, but Python actually creates a <strong>new integer object</strong> containing 15. The original integer object 10 cannot be modified.</p><h3>Example with Strings</h3><pre>name = &quot;Python&quot;</pre><pre>name = name + &quot; Programming&quot;</pre><pre>print(name)</pre><p>Output:</p><pre>Python Programming</pre><p>Strings are immutable. We cannot directly change a character inside a string.</p><p>For example:</p><pre>name = &quot;Python&quot;</pre><pre>name[0] = &quot;J&quot;</pre><p>This produces an error:</p><pre>TypeError: &#39;str&#39; object does not support item assignment</pre><p>Instead, a new string must be created.</p><h3>Mutable vs Immutable</h3><p>MutableImmutable Can be changed after creationCannot be changed after creation listint dictfloat setstr bytearraytuple Changes can happen to the same objectChanges create a new object</p><h3>Simple Example to Remember</h3><p>Think of a <strong>list as a notebook</strong>. You can erase or add information to the same notebook.</p><pre>items = [&quot;Apple&quot;, &quot;Banana&quot;]<br>items.append(&quot;Mango&quot;)</pre><p>The same list is modified.</p><p>Think of a <strong>string as a printed book</strong>. You cannot change a word directly on the printed page. You need to create a new version.</p><pre>text = &quot;Hello&quot;<br>text = text + &quot; World&quot;</pre><p>A new string is created.</p><h3>Conclusion</h3><p>Understanding mutable and immutable objects is an important part of learning Python.</p><p><strong>Mutable objects</strong> can be modified after creation, such as lists, dictionaries, and sets. <strong>Immutable objects</strong> cannot be modified after creation, such as integers, strings, and tuples.</p><p>A simple rule to remember is:</p><blockquote><strong><em>Mutable = can change</em></strong><em><br> </em><strong><em>Immutable = cannot change</em></strong></blockquote><p>This concept becomes especially important when working with <strong>functions, variables, loops, and object references</strong> in Python.Sure! Here’s a simple blog-style explanation of <strong>Mutable and Immutable objects in Python</strong>, suitable for beginners.</p><h3>Mutable and Immutable Objects in Python</h3><p>Python has different types of objects, and one important difference between them is whether they are <strong>mutable</strong> or <strong>immutable</strong>.</p><h3>What is Mutability?</h3><p><strong>Mutability</strong> means whether an object can be changed after it has been created.</p><ul><li><strong>Mutable objects</strong> can be changed after creation.</li><li><strong>Immutable objects</strong> cannot be changed after creation.</li></ul><p>This concept is important because it affects how variables behave when we modify them.</p><h3>Mutable Objects in Python</h3><p>A <strong>mutable object</strong> can be modified without creating a completely new object.</p><p>Common mutable data types are:</p><ul><li>list</li><li>dict</li><li>set</li><li>bytearray</li></ul><h3>Example of a Mutable List</h3><pre>numbers = [10, 20, 30]</pre><pre>numbers[0] = 100</pre><pre>print(numbers)</pre><p>Output:</p><pre>[100, 20, 30]</pre><p>Here, we changed the first element of the list from 10 to 100. The list itself was modified.</p><h3>Example with Two Variables</h3><pre>a = [1, 2, 3]<br>b = a</pre><pre>b.append(4)</pre><pre>print(a)<br>print(b)</pre><p>Output:</p><pre>[1, 2, 3, 4]<br>[1, 2, 3, 4]</pre><p>Both a and b refer to the <strong>same list object</strong>. Therefore, changing the list through b also affects a.</p><h3>Immutable Objects in Python</h3><p>An <strong>immutable object</strong> cannot be changed after it has been created.</p><p>Common immutable data types are:</p><ul><li>int</li><li>float</li><li>bool</li><li>str</li><li>tuple</li><li>frozenset</li><li>bytes</li></ul><h3>Example of an Immutable Integer</h3><pre>x = 10</pre><pre>x = x + 5</pre><pre>print(x)</pre><p>Output:</p><pre>15</pre><p>It may look like we changed 10 into 15, but Python actually creates a <strong>new integer object</strong> containing 15. The original integer object 10 cannot be modified.</p><h3>Example with Strings</h3><pre>name = &quot;Python&quot;</pre><pre>name = name + &quot; Programming&quot;</pre><pre>print(name)</pre><p>Output:</p><pre>Python Programming</pre><p>Strings are immutable. We cannot directly change a character inside a string.</p><p>For example:</p><pre>name = &quot;Python&quot;</pre><pre>name[0] = &quot;J&quot;</pre><p>This produces an error:</p><pre>TypeError: &#39;str&#39; object does not support item assignment</pre><p>Instead, a new string must be created.</p><h3>Mutable vs Immutable</h3><p>MutableImmutable Can be changed after creationCannot be changed after creation listint dictfloat setstr bytearraytuple Changes can happen to the same objectChanges create a new object</p><h3>Simple Example to Remember</h3><p>Think of a <strong>list as a notebook</strong>. You can erase or add information to the same notebook.</p><pre>items = [&quot;Apple&quot;, &quot;Banana&quot;]<br>items.append(&quot;Mango&quot;)</pre><p>The same list is modified.</p><p>Think of a <strong>string as a printed book</strong>. You cannot change a word directly on the printed page. You need to create a new version.</p><pre>text = &quot;Hello&quot;<br>text = text + &quot; World&quot;</pre><p>A new string is created.</p><h3>Conclusion</h3><p>Understanding mutable and immutable objects is an important part of learning Python.</p><p><strong>Mutable objects</strong> can be modified after creation, such as lists, dictionaries, and sets. <strong>Immutable objects</strong> cannot be modified after creation, such as integers, strings, and tuples.</p><p>A simple rule to remember is:</p><blockquote><strong><em>Mutable = can change</em></strong><em><br> </em><strong><em>Immutable = cannot change</em></strong></blockquote><p>This concept becomes especially important when working with <strong>functions, variables, loops, and object references</strong> in Python.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=856fd5541749" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>