{"id":20683,"date":"2023-06-14T11:25:12","date_gmt":"2023-06-14T05:55:12","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/"},"modified":"2024-10-15T00:16:47","modified_gmt":"2024-10-14T18:46:47","slug":"understanding-mutable-and-immutable-in-python","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/","title":{"rendered":"Understanding Mutable and Immutable in Python"},"content":{"rendered":"\n<p><em>Contributed by: <a href=\"https:\/\/www.linkedin.com\/in\/karuna-kumari-3b9757167\/\" target=\"_blank\" rel=\"noreferrer noopener\">Karuna Kumari<\/a><\/em><\/p>\n\n\n\n<p>In the programming world, understanding the concepts of mutability and immutability is crucial, especially when working with Python. Python, being a dynamically-typed language, allows us to manipulate objects and change their state during program execution. However, not all objects in Python behave in the same way when it comes to modification. Some objects can be altered, while others remain constant once created. This fundamental distinction between mutable and immutable objects forms the cornerstone of Python's design philosophy. By comprehending the concepts of mutability and immutability, developers can write more efficient, reliable, and bug-free code. In this article, we will explore the concept of mutability and immutability in Python, understand their differences, and examine their implications in practical programming scenarios.<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-python-programming\" class=\"courses-cta-title-link\">Python Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions. You will also explore OOP concepts and objects to build robust programs.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>11.5 Hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>51 Coding Exercises<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-python-programming\" class=\"courses-cta-button\">\n                Start Free Trial\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"mutable-and-immutable-in-python\"><strong>Mutable and Immutable in Python<\/strong><\/h2>\n\n\n\n<p>In Python, the terms \"mutable\" and \"immutable\" refer to the ability of an object to be changed after it is created.<\/p>\n\n\n\n<p>An object is considered mutable if its state or value can be modified after it is created. This means that you can alter its internal data or attributes without creating a new object. Examples of mutable objects in Python include lists, dictionaries, and sets. If you modify a mutable object, any references to that object will reflect the changes.<\/p>\n\n\n\n<p><em>Both of these states are integral to Python data structure. If you want to become more knowledgeable in the entire Python Data Structure, take this <strong><a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/python-data-structures\" target=\"_blank\" rel=\"noreferrer noopener\">free course which covers multiple data structures in Python<\/a> <\/strong>including tuple data structure which is immutable. You will also receive a certificate on completion which is sure to add value to your portfolio. <\/em> <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-mutable\"><strong>What is Mutable?<\/strong><\/h2>\n\n\n\n<p>Mutable is when something is changeable or has the ability to change. In Python, 'mutable' is the ability of objects to change their values. These are often the objects that store a collection of data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-immutable\"><strong>What is Immutable?<\/strong><\/h2>\n\n\n\n<p>Immutable is the when no change is possible over time. In Python, if the value of an object cannot be changed over time, then it is known as immutable. Once created, the value of these objects is permanent. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"list-of-mutable-and-immutable-objects\"><strong>List of Mutable and Immutable objects<\/strong><\/h2>\n\n\n\n<p>Objects of built-in type that are mutable are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Lists<\/li>\n\n\n\n<li>Sets<\/li>\n\n\n\n<li>Dictionaries<\/li>\n\n\n\n<li>User-Defined Classes (It purely depends upon the user to define the characteristics)&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>Objects of built-in type that are immutable are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Numbers (Integer, Rational, Float, Decimal, Complex &amp; Booleans)<\/li>\n\n\n\n<li>Strings<\/li>\n\n\n\n<li>Tuples<\/li>\n\n\n\n<li>Frozen Sets<\/li>\n\n\n\n<li>User-Defined Classes (It purely depends upon the user to define the characteristics)<\/li>\n<\/ul>\n\n\n\n<p>Object mutability is one of the characteristics that makes Python a dynamically typed language. Though Mutable and Immutable in <a rel=\"noreferrer noopener\" href=\"https:\/\/www.mygreatlearning.com\/blog\/python-tutorial-for-beginners-a-complete-guide\/\" target=\"_blank\">Python<\/a> is a very basic concept, it can at times be a little confusing due to the intransitive nature of immutability.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"objects-in-python\"><strong>Objects in Python<\/strong><\/h2>\n\n\n\n<p>In Python, everything is treated as an object. Every object has these three attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Identity - This refers to the address that the object refers to in the computer's memory.<\/li>\n\n\n\n<li>Type \u2013 This refers to the kind of object that is created. For example- integer, list, string etc.&nbsp;<\/li>\n\n\n\n<li>Value \u2013 This refers to the value stored by the object. For example \u2013 List=[1,2,3] would hold the numbers 1,2 and 3<\/li>\n<\/ul>\n\n\n\n<p>While ID and Type cannot be changed once it's created, values can be changed for Mutable objects.<\/p>\n\n\n\n<p>Check out this <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/python-fundamentals-for-beginners\" target=\"_blank\" rel=\"noreferrer noopener\">free python certificate course<\/a> to get started with Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"mutable-objects-in-python\"><strong>Mutable Objects in Python<\/strong><\/h2>\n\n\n\n<p>I believe, rather than diving deep into the theory aspects of mutable and immutable in Python, a simple code would be the best way to depict what it means in Python. Hence, let us discuss the below code step-by-step:<\/p>\n\n\n\n<p>#Creating a list which contains name of Indian cities&nbsp;&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cities = &#091;\u2018Delhi\u2019, \u2018Mumbai\u2019, \u2018Kolkata\u2019]\n<\/code><\/pre>\n\n\n\n<p># Printing the elements from the list cities, separated by a comma &amp; space<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for city in cities:\n\t\tprint(city, end=\u2019, \u2019)\n\nOutput &#091;1]: Delhi, Mumbai, Kolkata\n<\/code><\/pre>\n\n\n\n<p>#Printing the location of the object created in the memory address in hexadecimal format<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(hex(id(cities)))\n\nOutput &#091;2]: 0x1691d7de8c8\n<\/code><\/pre>\n\n\n\n<p>#Adding a new city to the list cities<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cities.append(\u2018Chennai\u2019)\n<\/code><\/pre>\n\n\n\n<p>#Printing the elements from the list cities, separated by a comma &amp; space&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for city in cities:\n\tprint(city, end=\u2019, \u2019)\n\nOutput &#091;3]: Delhi, Mumbai, Kolkata, Chennai\n<\/code><\/pre>\n\n\n\n<p>#Printing the location of the object created in the memory address in hexadecimal format<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(hex(id(cities)))\n\nOutput &#091;4]: 0x1691d7de8c8\n<\/code><\/pre>\n\n\n\n<p>The above example shows us that we were able to change the internal state of the object \u2018cities\u2019 by adding one more city \u2018Chennai\u2019 to it, yet, the memory address of the object did not change. This confirms that we did not create a new object, rather, the same object was changed or mutated. Hence, we can say that the object which is a type of list with reference variable name \u2018cities\u2019 is a MUTABLE OBJECT.<\/p>\n\n\n\n<p>Let us now discuss the term IMMUTABLE. Considering that we understood what mutable stands for, it is obvious that the definition of immutable will have \u2018NOT\u2019 included in it. Here is the simplest definition of immutable\u2013 An object whose internal state can NOT be changed is IMMUTABLE.<\/p>\n\n\n\n<p>Again, if you try and concentrate on different error messages, you have encountered, thrown by the respective IDE; you use you would be able to identify the immutable objects in Python. For instance, consider the below code &amp; associated error message with it, while trying to change the value of a Tuple at index 0.&nbsp;<\/p>\n\n\n\n<p>#Creating a Tuple with variable name \u2018foo\u2019<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>foo = (1, 2)\n<\/code><\/pre>\n\n\n\n<p>#Changing the index[0] value from 1 to 3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>foo&#091;0] = 3\n\t\nTypeError: 'tuple' object does not support item assignment \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"immutable-objects-in-python\"><strong>Immutable Objects in Python <\/strong><\/h2>\n\n\n\n<p>Once again, a simple code would be the best way to depict what immutable stands for. Hence, let us discuss the below code step-by-step:<\/p>\n\n\n\n<p>#Creating a Tuple which contains English name of weekdays<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>weekdays = \u2018Sunday\u2019, \u2018Monday\u2019, \u2018Tuesday\u2019, \u2018Wednesday\u2019, \u2018Thursday\u2019, \u2018Friday\u2019, \u2018Saturday\u2019\n<\/code><\/pre>\n\n\n\n<p># Printing the elements of tuple weekdays <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(weekdays)\n\nOutput &#091;1]:  (\u2018Sunday\u2019, \u2018Monday\u2019, \u2018Tuesday\u2019, \u2018Wednesday\u2019, \u2018Thursday\u2019, \u2018Friday\u2019, \u2018Saturday\u2019)\n<\/code><\/pre>\n\n\n\n<p> #Printing the location of the object created in the memory address in hexadecimal format <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(hex(id(weekdays)))\n\nOutput &#091;2]: 0x1691cc35090\n<\/code><\/pre>\n\n\n\n<p>#tuples are immutable, so you cannot add new elements, hence, using merge of tuples with the # + operator to add a new imaginary day in the tuple \u2018weekdays\u2019<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>weekdays  +=  \u2018Pythonday\u2019,\n<\/code><\/pre>\n\n\n\n<p>#Printing the elements of tuple weekdays<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(weekdays)\n\nOutput &#091;3]: (\u2018Sunday\u2019, \u2018Monday\u2019, \u2018Tuesday\u2019, \u2018Wednesday\u2019, \u2018Thursday\u2019, \u2018Friday\u2019, \u2018Saturday\u2019, \u2018Pythonday\u2019)\n<\/code><\/pre>\n\n\n\n<p>#Printing the location of the object created in the memory address in hexadecimal format<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(hex(id(weekdays)))\n\nOutput &#091;4]: 0x1691cc8ad68\n\n<\/code><\/pre>\n\n\n\n<p>This above example shows that we were able to use the same variable name that is referencing an object which is a type of tuple with seven elements in it. However, the ID or the memory location of the old &amp; new tuple is not the same. We were not able to change the internal state of the object \u2018weekdays\u2019. The Python program manager created a new object in the memory address and the variable name \u2018weekdays\u2019 started referencing the new object with eight elements in it.&nbsp; Hence, we can say that the object which is a type of tuple with reference variable name \u2018weekdays\u2019 is an IMMUTABLE OBJECT.<\/p>\n\n\n\n<p>Also Read: <a href=\"https:\/\/www.mygreatlearning.com\/blog\/understanding-eda-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Understanding the Exploratory Data Analysis (EDA) in Python (opens in a new tab)\">Understanding the Exploratory Data Analysis (EDA) in Python<\/a><\/p>\n\n\n\n<p>Where can you use mutable and immutable objects:<\/p>\n\n\n\n<p>Mutable objects can be used where you want to allow for any updates. For example, you have a list of employee names in your organizations, and that needs to be updated every time a new member is hired. You can create a mutable list, and it can be updated easily.<\/p>\n\n\n\n<p>Immutability offers a lot of useful applications to different sensitive tasks we do in a network centred environment where we allow for parallel processing. By creating immutable objects, you seal the values and ensure that no threads can invoke overwrite\/update to your data. This is also useful in situations where you would like to write a piece of code that cannot be modified. For example, a debug code that attempts to find the value of an immutable object.<\/p>\n\n\n\n<p>Watch outs:&nbsp; Non transitive nature of Immutability:<\/p>\n\n\n\n<p>OK! Now we do understand what mutable &amp; immutable objects in Python are. Let\u2019s go ahead and discuss the combination of these two and explore the possibilities. Let\u2019s discuss, as to how will it behave if you have an immutable object which contains the mutable object(s)? Or vice versa? Let us again use a code to understand this behaviour\u2013<\/p>\n\n\n\n<p>#creating a tuple (immutable object) which contains 2 lists(mutable) as it\u2019s elements<\/p>\n\n\n\n<p>#The elements (lists) contains the name, age &amp; gender&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>person = (&#091;'Ayaan', 5, 'Male'], &#091;'Aaradhya', 8, 'Female'])\n<\/code><\/pre>\n\n\n\n<p>#printing the tuple<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(person)\n\nOutput &#091;1]: (&#091;'Ayaan', 5, 'Male'], &#091;'Aaradhya', 8, 'Female'])\n\n<\/code><\/pre>\n\n\n\n<p>#printing the location of the object created in the memory address in hexadecimal format<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(hex(id(person)))\n\nOutput &#091;2]: 0x1691ef47f88\n<\/code><\/pre>\n\n\n\n<p>#Changing the age for the 1st element. Selecting 1<sup>st<\/sup> element of tuple by using indexing [0] then 2<sup>nd<\/sup> element of the list by using indexing [1] and assigning a new value for age as 4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>person&#091;0]&#091;1] = 4\n<\/code><\/pre>\n\n\n\n<p>#printing the updated tuple<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(person)\n\nOutput &#091;3]: (&#091;'Ayaan', 4, 'Male'], &#091;'Aaradhya', 8, 'Female'])\n<\/code><\/pre>\n\n\n\n<p>#printing the location of the object created in the memory address in hexadecimal format <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(hex(id(person)))\n\nOutput &#091;4]: 0x1691ef47f88\n<\/code><\/pre>\n\n\n\n<p>In the above code, you can see that the object \u2018person\u2019 is immutable since it is a type of tuple. However, it has two lists as it\u2019s elements, and we can change the state of lists (lists being mutable). So, here we did not change the object reference inside the Tuple, but the referenced object was mutated.<\/p>\n\n\n\n<p>Also Read: <a href=\"https:\/\/www.mygreatlearning.com\/blog\/object-detection-using-tensorflow\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Real-Time Object Detection Using TensorFlow (opens in a new tab)\">Real-Time Object Detection Using TensorFlow<\/a><\/p>\n\n\n\n<p>Same way, let\u2019s explore how it will behave if you have a mutable object which contains an immutable object? Let us again use a code to understand the behaviour\u2013<\/p>\n\n\n\n<p>#creating a list (mutable object) which contains tuples(immutable) as it\u2019s elements<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list1 = &#091;(1, 2, 3), (4, 5, 6)]\n<\/code><\/pre>\n\n\n\n<p>#printing the list<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(list1)\n\nOutput &#091;1]: &#091;(1, 2, 3), (4, 5, 6)]\n\n<\/code><\/pre>\n\n\n\n<p>#printing the location of the object created in the memory address in hexadecimal format<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(hex(id(list1)))\n\nOutput &#091;2]: 0x1691d5b13c8\t<\/code><\/pre>\n\n\n\n<p>#changing object reference at index 0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list1&#091;0] = (7, 8, 9)\n<\/code><\/pre>\n\n\n\n<p>#printing the list<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Output &#091;3]: &#091;(7, 8, 9), (4, 5, 6)]<\/code><\/pre>\n\n\n\n<p>#printing the location of the object created in the memory address in hexadecimal format<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(hex(id(list1)))\n\nOutput &#091;4]: 0x1691d5b13c8\n<\/code><\/pre>\n\n\n\n<p>As an individual, it completely depends upon you and your requirements as to what kind of data structure you would like to create with a combination of mutable &amp; immutable objects. I hope that this information will help you while deciding the type of object you would like to select going forward.<\/p>\n\n\n\n<p>Before I end our discussion on IMMUTABILITY, allow me to use the word \u2018CAVITE\u2019 when we discuss the String and Integers. There is an exception, and you may see some surprising results while checking the truthiness for immutability. For instance:<br>#creating an object of integer type with value 10 and reference variable name \u2018x\u2019&nbsp; <\/p>\n\n\n\n<p>x = 10<br><\/p>\n\n\n\n<p>#printing the value of \u2018x\u2019<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(x)\n\nOutput &#091;1]: 10\n<\/code><\/pre>\n\n\n\n<p>#Printing the location of the object created in the memory address in hexadecimal format<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(hex(id(x)))\n\nOutput &#091;2]: 0x538fb560\n\n<\/code><\/pre>\n\n\n\n<p>#creating an object of integer type with value 10 and reference variable name \u2018y\u2019  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>y = 10\n<\/code><\/pre>\n\n\n\n<p>#printing the value of \u2018y\u2019<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(y)\n\nOutput &#091;3]: 10\n<\/code><\/pre>\n\n\n\n<p>#Printing the location of the object created in the memory address in hexadecimal format <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(hex(id(y)))\n\nOutput &#091;4]: 0x538fb560\n<\/code><\/pre>\n\n\n\n<p>As per our discussion and understanding, so far, the memory address for x &amp; y should have been different, since, 10 is an instance of Integer class which is immutable. However, as shown in the above code, it has the same memory address. This is not something that we expected. It seems that what we have understood and discussed, has an exception as well.<\/p>\n\n\n\n<p><strong>Quick check<\/strong> - <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/python-data-structures\" target=\"_blank\" rel=\"noreferrer noopener\">Python Data Structures<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"immutability-of-tuple\"><strong>Immutability of Tuple<\/strong><\/h2>\n\n\n\n<p>Tuples are immutable and hence cannot have any changes in them once they are created in Python. This is because they support the same sequence operations as strings. We all know that strings are immutable. The index operator will select an element from a tuple just like in a string. Hence, they are immutable. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"exceptions-in-immutability\"><strong>Exceptions in immutability<\/strong><\/h2>\n\n\n\n<p>Like all, there are exceptions in the immutability in python too. Not all immutable objects are really mutable. This will lead to a lot of doubts in your mind. Let us just take an example to understand this. <\/p>\n\n\n\n<p>Consider a tuple 'tup'. <\/p>\n\n\n\n<p>Now, if we consider tuple <strong>tup = ('GreatLearning',[4,3,1,2])<\/strong> ;<\/p>\n\n\n\n<p>We see that the tuple has elements of different data types. The first element here is a string which as we all know is immutable in nature. The second element is a list which we all know is mutable. Now, we all know that the tuple itself is an immutable data type. It cannot change its contents. But, the list inside it can change its contents. So, the value of the Immutable objects cannot be changed but its constituent objects can. change its value.  <\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Python for Data Science Full Course | Data Science Tutorials for Beginners in 2022 | Great Learning\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/JDcZBzb46ts?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"conclusion\"><strong>Conclusion <\/strong><\/h2>\n\n\n\n<p>Understanding the concepts of mutability and immutability in Python is essential for any developer seeking to write robust and efficient code. By recognizing the differences between mutable and immutable objects, programmers can make informed decisions about object manipulation, memory management, and code optimization. Mutable objects can be modified after creation, allowing for flexibility and convenience and posing potential risks such as unintended side effects or unexpected behavior. On the other hand, immutable objects remain constant once created, ensuring predictability, thread safety, and the ability to use them as keys in dictionaries. By leveraging the advantages of mutable and immutable objects, developers can design cleaner, more maintainable code and avoid common pitfalls related to object mutability. Ultimately, a solid understanding of mutability and immutability in Python empowers developers to write efficient, bug-free code that meets the requirements of their applications.<\/p>\n\n\n\n<p>Mastering the concept of mutability is a major milestone in your journey to writing efficient and bug-free code. However, if these topics still feel a bit complex, remember that every expert started with the basics. To ensure you have a rock-solid foundation in Python, we recommend trying our Free Python Course. It is built for absolute beginners to help them master the fundamentals of the language at their own pace, making it the perfect stepping stone to the advanced concepts covered in this guide<\/p>\n\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Free Course<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/python-fundamentals-for-beginners\" class=\"courses-cta-title-link\">Python Fundamentals for Beginners Free Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Master Python basics, from variables to data structures and control flow. Solve real-time problems and build practical skills using Jupyter Notebook.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>13.5 hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>4.55<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/python-fundamentals-for-beginners\" class=\"courses-cta-button\">\n                Enroll for Free\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"understanding-mutable-and-immutable-in-python-faqs\">Understanding Mutable and Immutable in Python <strong>FAQs<\/strong><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"1-difference-between-mutable-vs-immutable-in-python\"><strong>1. Difference between mutable vs immutable in Python?<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Mutable Object<\/strong><\/td><td><strong>Immutable Object<\/strong><\/td><\/tr><tr><td>State of the object can be modified after it is created.<\/td><td>State of the object can\u2019t be modified once it is created.<\/td><\/tr><tr><td>They are not thread safe.<\/td><td>They are thread safe<\/td><\/tr><tr><td>Mutable classes are not final.<\/td><td>It is important to make the class final before creating an immutable object.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"2-what-are-the-mutable-and-immutable-data-types-in-python\"><strong>2. What are the mutable and immutable data types in Python? <\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Some mutable data types in Python are:<\/li>\n<\/ul>\n\n\n\n<p>list, dictionary, set, user-defined classes.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Some immutable data types are:&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>int,&nbsp;float, decimal, bool,&nbsp;string,&nbsp;tuple, range.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"3-are-lists-mutable-in-python\"><strong>3. Are lists mutable in Python?<\/strong><\/h4>\n\n\n\n<p>Lists in Python are mutable data types as the elements of the list can be modified, individual elements can be replaced, and the order of elements can be changed even after the list has been created.<br>(Examples related to lists have been discussed earlier in this blog.)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"4-why-are-tuples-called-immutable-types\"><strong>4. Why are tuples called immutable types?<\/strong><\/h4>\n\n\n\n<p>Tuple and list data structures are very similar, but one big difference between the data types is that lists are mutable, whereas tuples are immutable. The reason for the tuple's immutability is that once the elements are added to the tuple and the tuple has been created; it remains unchanged. <\/p>\n\n\n\n<p>A programmer would always prefer building a code that can be reused instead of making the whole data object again. Still, even though tuples are immutable, like lists, they can contain any Python object, including mutable objects.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"5-are-sets-mutable-in-python\"><strong>5. Are sets mutable in Python?<\/strong><\/h4>\n\n\n\n<p>A set is an iterable unordered collection of data type which can be used to perform mathematical operations (like union, intersection, difference etc.). Every element in a set is unique and immutable, i.e. no duplicate values should be there, and the values can't be changed. However, we can add or remove items from the set as the set itself is mutable.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"6-are-strings-mutable-in-python\"><strong>6. Are strings mutable in Python?<\/strong><\/h4>\n\n\n\n<p>Strings are not mutable in Python. Strings are a immutable data types which means that its value cannot be updated. <\/p>\n\n\n\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"Difference between mutable and immutable in Python?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Mutable Object in Python\n1. State of the object can be modified after it is created.\n2. They are not thread safe.\n3. Mutable classes are not final.\n\nImmutable Object in Python\n1. State of the object can\u2019t be modified once it is created.\n2. They are thread safe\n3. It is important to make the class final before creating an immutable object.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"What are the mutable and immutable data types in Python?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Some mutable data types in Python are:  list, dictionary, set, user-defined classes.\nSome immutable data types are: int, float, decimal, bool, string, tuple, range.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Is the list mutable or immutable in Python?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Lists in Python are mutable data types as the elements of the list can be modified, individual elements can be replaced, and the order of elements can be changed even after the list has been created.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Why are tuples called immutable types?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Tuple and list data structures are very similar, but one big difference between the data types is that lists are mutable, whereas tuples are immutable. The reason for the tuple\u2019s immutability is that once the elements are added to the tuple and the tuple has been created; it remains unchanged.\n\nA programmer would always prefer building a code that can be reused instead of making the whole data object again. Still, even though tuples are immutable, like lists, they can contain any Python object, including mutable objects.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Are sets mutable in python?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"A set is an iterable unordered collection of data type which can be used to perform mathematical operations (like union, intersection, difference etc.). Every element in a set is unique and immutable, i.e. no duplicate values should be there, and the values can\u2019t be changed. However, we can add or remove items from the set as the set itself is mutable.\"\n    }\n  }]\n}\n<\/script>\n\n\n\n<p>Join Great Learning Academy's free online courses and upgrade your skills today. <\/p>\n\n\n\n<p>Embarking on a journey towards a career in data science opens up a world of limitless possibilities. Whether you\u2019re an aspiring data scientist or someone intrigued by the power of data, understanding the key factors that contribute to success in this field is crucial. The below path will guide you to become a proficient data scientist.<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table class=\"has-cyan-bluish-gray-background-color has-background\"><tbody><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/data-science\/courses\/certificates\" target=\"_blank\" rel=\"noreferrer noopener\">Data Science Course Certificates<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/data-science\/courses\/placements\" target=\"_blank\" rel=\"noreferrer noopener\">Data Science Course Placements<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/data-science\/courses\/syllabus\" target=\"_blank\" rel=\"noreferrer noopener\">Data Science Course Syllabus<\/a><\/td><\/tr><tr><td><a href=\"https:\/\/www.mygreatlearning.com\/data-science\/courses\/eligibility\" target=\"_blank\" rel=\"noreferrer noopener\">Data Science Course Eligibility<\/a><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Contributed by: Karuna Kumari In the programming world, understanding the concepts of mutability and immutability is crucial, especially when working with Python. Python, being a dynamically-typed language, allows us to manipulate objects and change their state during program execution. However, not all objects in Python behave in the same way when it comes to modification. [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":20689,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36796],"content_type":[36248],"class_list":["post-20683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-python","content_type-career-guide"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Understand What is Mutable and Immutable in Python<\/title>\n<meta name=\"description\" content=\"Mutable and Immutable in Python: Learn the Mutable and Immutable Objects in Python with this guide on the greatlearning blog.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Understanding Mutable and Immutable in Python\" \/>\n<meta property=\"og:description\" content=\"Mutable and Immutable in Python: Learn the Mutable and Immutable Objects in Python with this guide on the greatlearning blog.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-14T05:55:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-14T18:46:47+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1254\" \/>\n\t<meta property=\"og:image:height\" content=\"836\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/understanding-mutable-and-immutable-in-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/understanding-mutable-and-immutable-in-python\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Understanding Mutable and Immutable in Python\",\"datePublished\":\"2023-06-14T05:55:12+00:00\",\"dateModified\":\"2024-10-14T18:46:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/understanding-mutable-and-immutable-in-python\\\/\"},\"wordCount\":2618,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/understanding-mutable-and-immutable-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/iStock-1162261415.jpg\",\"keywords\":[\"python\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/understanding-mutable-and-immutable-in-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/understanding-mutable-and-immutable-in-python\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/understanding-mutable-and-immutable-in-python\\\/\",\"name\":\"Understand What is Mutable and Immutable in Python\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/understanding-mutable-and-immutable-in-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/understanding-mutable-and-immutable-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/iStock-1162261415.jpg\",\"datePublished\":\"2023-06-14T05:55:12+00:00\",\"dateModified\":\"2024-10-14T18:46:47+00:00\",\"description\":\"Mutable and Immutable in Python: Learn the Mutable and Immutable Objects in Python with this guide on the greatlearning blog.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/understanding-mutable-and-immutable-in-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/understanding-mutable-and-immutable-in-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/understanding-mutable-and-immutable-in-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/iStock-1162261415.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/iStock-1162261415.jpg\",\"width\":1254,\"height\":836,\"caption\":\"Mutable and Immutable in Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/understanding-mutable-and-immutable-in-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Understanding Mutable and Immutable in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Understand What is Mutable and Immutable in Python","description":"Mutable and Immutable in Python: Learn the Mutable and Immutable Objects in Python with this guide on the greatlearning blog.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Understanding Mutable and Immutable in Python","og_description":"Mutable and Immutable in Python: Learn the Mutable and Immutable Objects in Python with this guide on the greatlearning blog.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2023-06-14T05:55:12+00:00","article_modified_time":"2024-10-14T18:46:47+00:00","og_image":[{"width":1254,"height":836,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Understanding Mutable and Immutable in Python","datePublished":"2023-06-14T05:55:12+00:00","dateModified":"2024-10-14T18:46:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/"},"wordCount":2618,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415.jpg","keywords":["python"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/","url":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/","name":"Understand What is Mutable and Immutable in Python","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415.jpg","datePublished":"2023-06-14T05:55:12+00:00","dateModified":"2024-10-14T18:46:47+00:00","description":"Mutable and Immutable in Python: Learn the Mutable and Immutable Objects in Python with this guide on the greatlearning blog.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415.jpg","width":1254,"height":836,"caption":"Mutable and Immutable in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/understanding-mutable-and-immutable-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Understanding Mutable and Immutable in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415.jpg",1254,836,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415-768x512.jpg",768,512,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415-1024x683.jpg",1024,683,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415.jpg",1254,836,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415.jpg",1254,836,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415.jpg",640,427,false],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415.jpg",96,64,false],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/09\/iStock-1162261415.jpg",150,100,false]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":1,"uagb_excerpt":"Contributed by: Karuna Kumari In the programming world, understanding the concepts of mutability and immutability is crucial, especially when working with Python. Python, being a dynamically-typed language, allows us to manipulate objects and change their state during program execution. However, not all objects in Python behave in the same way when it comes to modification.&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/20683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=20683"}],"version-history":[{"count":49,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/20683\/revisions"}],"predecessor-version":[{"id":116976,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/20683\/revisions\/116976"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/20689"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=20683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=20683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=20683"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=20683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}