Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Tuples and lists are the basic array types in Python. tuples are immutable and lists are mutable. tuples are created with the comma operator. Say t = 1, 2, 3, 4, 5 like that, and I get a tuple with those five elements in it. So, element number 0 is the beginning of the list and element 4 is the end of the list. Of course, you can always get the end of the list with -1, like that.
I can take the length of the list, I can get the smallest element, I can get the largest element and so I have a tuple. There is some confusion about how you create a tuple in Python. Sometimes it looks like you use the parenthesis to create a tuple, because more often than not you'll see it done like this to create the tuple, but in fact, it's the comma operator. This is the important distinction because if you want to create a tuple with one element, that won't do it.
That creates an integer.
The comma is required. The parentheses are for binding, and when you have a lot
of elements separated by commas, the parenthesis are convenient for that and
that's why you'll often see it that way.
But in fact, if you want a tuple with one element, you create it like this.
And now, we have a tuple. type(t) is
Lists work very much like tuples in that I can subscript it in the same way or I can look at the last one and I can check its length and I can get the largest element and the smallest element. The difference is, is that I can change things. If I take my tuple and let's put something bigger in the tuple. Now I have a tuple with 25 elements in it and that is a tuple.
Let's say that I try to change one of those elements.
I get this error, 'tuple' object does not support item assignment.
Tuples are immutable, so I can't change things in them.
On the other hand if I create a list, I am using the list constructor here to
convert this range into a list and there is my list and type(x) is
Now, we see that 42 right there in the list. So, a list is mutable and a tuple is not mutable. So, why would you want to use a tuple when you can use a list and the list is more powerful and has more functionality? Tuples are there because more often than not, when you them you don't need to be changing them. It's a smaller class. It's simpler to implement, they are faster for some things, but most importantly they don't allow you to change stuff.
So, if you have a circumstance, and most circumstances are like this, where you need an object that's always going to stay the same size, it's always going to have the same data in it, you want to use a tuple so that you can't accidentally it, and if you do, you'll get an exception and you'll be able to find your problem. So, use your tuples where you can, and where you absolutely need to be able to change your array, that's where you use a list.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 100782 Viewers
61 Video lessons · 87640 Viewers
71 Video lessons · 71497 Viewers
56 Video lessons · 103424 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.