{"id":18177,"date":"2025-01-22T09:56:38","date_gmt":"2025-01-22T09:56:38","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=18177"},"modified":"2025-01-22T09:57:38","modified_gmt":"2025-01-22T09:57:38","slug":"mutable-vs-immutable-python","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=18177","title":{"rendered":"Mutable vs. Immutable (Python)"},"content":{"rendered":"<p><strong>Immutable data types<\/strong><\/p>\n<ul>\n<li>They are not modifiable<\/li>\n<li>e.g. Numbers, Strings and Tuples<\/li>\n<\/ul>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; a = 1\r\n&gt;&gt;&gt; b = a\r\n&gt;&gt;&gt; b = b + 1\r\n&gt;&gt;&gt; print(a)\r\n1\r\n&gt;&gt;&gt; print(b)\r\n2\r\n<\/pre>\n<table>\n<tr>\n<th>Before line 3<\/th>\n<td>\n<pre>\r\na ---> 1\r\nb -----^\r\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<th>At line 3<\/th>\n<td>It creates a new value for the name b, because numbers are immutable.<\/td>\n<\/tr>\n<tr>\n<th>After line 3<\/th>\n<td>\n<pre>\r\na ---> 1\r\nb ---> 2\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<hr>\n<p><strong>Mutable data types<\/strong><\/p>\n<ul>\n<li>They are modifiable<\/li>\n<li>e.g. Lists, Dictionaries and Sets<\/li>\n<\/ul>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; a = [1,2,3]\r\n&gt;&gt;&gt; b = a\r\n&gt;&gt;&gt; b += [4]\r\n&gt;&gt;&gt; print(a)\r\n[1, 2, 3, 4]\r\n&gt;&gt;&gt; print(b)\r\n[1, 2, 3, 4]\r\n<\/pre>\n<table>\n<tr>\n<th>Before line 3<\/th>\n<td>\n<pre>\r\na ---> [1,2,3]\r\nb -----^\r\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<th>At line 3<\/th>\n<td>It changes the value of both names, because lists are mutable.<\/td>\n<\/tr>\n<tr>\n<th>After line 3<\/th>\n<td>\n<pre>\r\na ---> [1,2,3,4]\r\nb -----^\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<hr>\n<p><strong>Example<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; a = [1,2,3]\r\n&gt;&gt;&gt; for i in a:\r\n...   i += 1\r\n...\r\n&gt;&gt;&gt; print(a)\r\n[1, 2, 3]\r\n&gt;&gt;&gt; for i in range(len(a)):\r\n...   a[i] += 1\r\n...\r\n&gt;&gt;&gt; print(a)\r\n[2, 3, 4]\r\n<\/pre>\n<ul>\n<li>In line 3 it creates a new value and assigns it to i. (Does not modify the list.)<\/li>\n<li>In line 7 it creates a new value and assigns it to a[i]. (Does modify the list.)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Immutable data types They are not modifiable e.g. Numbers, Strings and Tuples Before line 3 a &#8212;> 1 b &#8212;&#8211;^<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[80,78],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18177"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18177"}],"version-history":[{"count":33,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18177\/revisions"}],"predecessor-version":[{"id":18211,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18177\/revisions\/18211"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18177"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}