#100DaysOfCode in Python Transcripts
Chapter: Days 7-9: Python Data Structures
Lecture: Immutability and Tuples

Login or purchase this course to watch this video and the rest of the course contents.
0:00 If you're learning Python you're going to come across two words that may or may not make sense to you, and they are mutability, and immutability.
0:09 Okay, type them out on screen, mutability, immutability. And what do they mean? Well, mutability means it's something that can be changed,
0:23 it's an object that can be changed. You saw in the previous video that we were manipulating the mystring variable, and we were dropping j,
0:33 we were dropping n, we were doing all sorts of things to that list. Now, immutable lists, okay, let's just go with that
0:42 for one second, are lists that cannot be edited, so if you tried to edit it, if you tried to pop out that j, or pop out that n,
0:52 you'd actually get an error, and they're not actually called lists, they're called tuples, okay, you've probably heard
0:59 that terminology by now, but we're just covering it again, and I'll just quickly demonstrate the difference between the two.
1:05 So we're going to say l, again, for list, is a list of mystring, so we can see mystring is my name, okay, and t for tuple is a tuple of mystring.
1:23 So let's just show what the two of them look like. And the big difference here is you've got the square bracket versus the rounded bracket,
1:34 and that's the telltale sign that you're dealing with a tuple, okay? Now watch what happens when we try to edit the two,
1:42 okay, we can go, l, let's actually overwrite, just like we did in the last video, so, l[0], we're going to assign the word,
1:53 or the letter, T, so my name is now Tulean, no that wasn't a name I was called in school, so don't laugh, and now if we try and do that to the tuple,
2:06 we can go t[0], is, let's just go with the B from the other video, and we get this error. Tuple object does not support item assignment,
2:21 and that's because the tuple is immutable, we cannot edit it. We can still talk to it, imagine a hard drive or something,
2:32 or your SD card in your camera is read-only, that's pretty much what a tuple is, so we can go t zero, we can return that,
2:42 you know, we can read it, we can talk to it, we can iterate over it we can go, four letter in t, print letter.
2:57 And there we go, we can still iterate over it, just like we do with the list, the difference is, we can't edit it, and that is what a tuple is,
3:05 and that's a demonstration of immutability.


Talk Python's Mastodon Michael Kennedy's Mastodon