#100DaysOfCode in Python Transcripts
Chapter: Days 7-9: Python Data Structures
Lecture: Immutability and Tuples
0:00 If you're learning Python
0:01 you're going to come across two words
0:02 that may or may not make sense to you,
0:04 and they are mutability, and immutability.
0:08 Okay, type them out on screen,
0:11 mutability, immutability.
0:17 And what do they mean?
0:18 Well, mutability means it's something that can be changed,
0:22 it's an object that can be changed.
0:24 You saw in the previous video that we were manipulating
0:27 the mystring variable, and we were dropping j,
0:32 we were dropping n, we were doing all sorts of things
0:35 to that list.
0:37 Now, immutable lists, okay, let's just go with that
0:41 for one second, are lists that cannot be edited,
0:45 so if you tried to edit it, if you tried to
0:48 pop out that j, or pop out that n,
0:51 you'd actually get an error,
0:52 and they're not actually called lists,
0:54 they're called tuples, okay, you've probably heard
0:58 that terminology by now, but we're just covering it again,
1:01 and I'll just quickly demonstrate
1:02 the difference between the two.
1:04 So we're going to say l, again, for list, is
1:10 a list of mystring, so we can see mystring is my name,
1:15 okay, and t for tuple
1:18 is a tuple of mystring.
1:22 So let's just show what the two of them look like.
1:26 And the big difference here is you've got
1:28 the square bracket versus the rounded bracket,
1:33 and that's the telltale sign
1:34 that you're dealing with a tuple, okay?
1:37 Now watch what happens when we try to edit the two,
1:41 okay, we can go, l, let's actually overwrite,
1:45 just like we did in the last video, so,
1:47 l, we're going to assign the word,
1:52 or the letter, T, so my name
1:56 is now Tulean, no that wasn't a name I was called in school,
2:01 so don't laugh, and now if we try and do that to the tuple,
2:05 we can go t,
2:09 is, let's just go with the B from the other video,
2:15 and we get this error.
2:17 Tuple object does not support item assignment,
2:20 and that's because the tuple is immutable,
2:23 we cannot edit it.
2:26 We can still talk to it, imagine a hard drive or something,
2:31 or your SD card in your camera is read-only,
2:34 that's pretty much what a tuple is,
2:36 so we can go t zero, we can return that,
2:41 you know, we can read it, we can talk to it,
2:43 we can iterate over it we can go,
2:45 four letter in t,
2:51 print letter.
2:56 And there we go, we can still iterate over it,
2:58 just like we do with the list, the difference is,
3:01 we can't edit it, and that is what a tuple is,
3:04 and that's a demonstration of immutability.