Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Objects in Python may be mutable or immutable. Mutable objects may change value; immutable objects may not. Some immutable objects at times may look like they are changing their values. They are not actually changing their values though. The distinction is visible using the id function. Integers, container objects, strings, things like these may appear to be changing values when they are not. For example, here in the Python shell, we can create an object, and we'll call it x, and we'll assign a number to it, 42.
Integers are immutable objects in Python, and if we look at the value, we see that it's 42. If we look at its type, we see that its type is int. If we look at the id, we see that it has this unique identifier. Now if I change the value of x, and remember it's an immutable object so I cannot change the value of an integer, but I assign x = 43, what I've actually done is I've changed this variable x to refer to a different integer.
Variables in Python are references to objects. So this reference is now referring to a different object. If I type id of x, you'll see I get a different identifier. If I type x, I see that it's 43. If I assign x back to 42 and take its id, you'll see that this id is now exactly the same as the one up there. So I did not actually change the immutable value. I simply changed the variable to refer to a different object.
This is an important distinction, and it's one that may take a while for you to get your head around, but we'll cover some of those as we get into this in more detail. There will be times when something that you want to do, like change the value of a string or to insert objects in a tuple, you may find that you need to use a different type, a mutable type instead of an immutable type, in order to be able to accomplish where it is you are trying to accomplish. In fact, most fundamental types in Python are immutable. Numbers, strings, and tuples are the fundamental types that are immutable.
Mutable objects include lists, dictionaries and some other objects, depending upon their implementation. So as you create your code in Python, simply be aware that there are some types that are immutable - and these are typically the more fundamental types - and other types that are mutable. And as we get into the details of these types, we'll talk about the things you can and cannot do with mutable and immutable objects.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101414 Viewers
61 Video lessons · 88175 Viewers
71 Video lessons · 72028 Viewers
56 Video lessons · 103820 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.