Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Python has several sequential types that look like lists of things, and we're going to look at a few of them right now. Let's make a working copy of variables.py and we'll call this variables-lists.py. And we'll open that working copy. So let's create a variable. And we'll just call it x. And we'll assign it a list of values in parentheses (1, 2, 3). And we'll go ahead and we'll print(x).
What print will do with this is it will try to print it in a way that Python could actually create one if it needed to. And so it has it in the parentheses just like that. So let's print the type of it, also. Save that and run it. So the class is tuple. So this is a tuple. And a tuple is created with parentheses. And a tuple is an immutable object. So I can't insert things. I can't append things. I can't delete things. Once I've created it, I've created it.
And I can't change it. So this is useful for a lot of stuff. Perhaps you just to operate on a fixed list of things. In fact, most of time, when you need a list of something, you're probably not going to need an immutable list. And to use a tuple is going to be faster than to use immutable list. On the other hand, if we create this with the square brackets, then we get the list type. I'll save this and run it. And the list type is mutable. I can, in fact, add something to the end of it.
I can say x.append and call it the number 5, and save that and run it, and now we have the number 5 at the end. Or I can say x.insert, and insert it at the beginning, which I use the index 0 for the beginning, and put a 7 there, and save that and run it, and now I have a 7 at the beginning. I could, of course, insert that at any point. If I say 2, use the number 2 there.
So this is item 0 and this is item 1 and this would be item 2. And we can expect the 7 to show up in there. So I'll save it and I'll run it. And there we have the 7 in there. So you can actually modify the list that is mutable, but if I were to change this back to the tuple type, which is immutable, and save it and run it, then I get an error. That tuple object has no attribute append, and in fact it has no attribute insert either, because it's immutable.
And it cannot be changed. So we'll go ahead and we'll take these out. And I'm going to show you one other sequence type that you've seen before, but you might not have thought it in this way. And that is the string. If I save this and run it, we'll see that we have a string. Just like with any sequential type, I can actually look at particular elements of the string by subscripting it to say 2. So that will be 0, 1, 2. I'll just get the r. Save that and run it.
And there's the r. Or I can do what's called a slice. And we'll learn more about slices later on. I can say 2:4. And I'll get a slice of just the r and the i. And in slices, it's worth noting that even though I'm using the 2 index for the beginning, which would be 0, 1, 2, that's the r, that I'm using the 4 index for the end which would be 2, 3, 4, it would be the n. The way slices work in Python, they don't actually return that last element.
And there are explanations in the documentation just to why that is. And we will get more into slices in more detail in a later chapter. It's also notable that all of these sequence types can be used as iterators. So if I go back to (1, 2, 3, 4, 5), I have an immutable tuple. And I'm going to for I in x: print(i).
And so that will print each one on a separate line, each of the different elements. And in fact, the string also works like that. If I put a string in here and save and run, I get each of those individual letters from the string. So this is how you use sequences in Python. There are a number of them. These are the most common. The tuple is immutable. the list is mutable and is designated with the square brackets instead of the parenthesis for the tuple, and a string is also a sequence, an immutable sequence.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97164 Viewers
61 Video lessons · 84457 Viewers
71 Video lessons · 68665 Viewers
56 Video lessons · 101148 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.