Python provides sequential types for many purposes, including both mutable and immutable versions of lists, tuples, and dictionaries.
- [Narrator] Python provides some built-in sequence types, including lists, tuples, and dictionaries. Here in Komodo, I've opened a working copy of sequence.py, from chapter three of the exercise files. A list is created with square brackets, like this, here on line four, and if I save and run this, you'll notice that we get this result, I is one, I is two, etc. The for loop is sequencing through the list, and for each item in the list, it assigns I the value of the item, and I is used in the print function.
A list is a mutable sequence, so I can reassign one of the values after I have assigned it. I can say X sub two equals three, and so I'm accessing, with this index in the square brackets, I'm accessing a particular specific item in the list. And I'm going to change its value to 42, like that. So, when I save and run it, you'll see that the item number two, which is the third item, because the index starts at zero for all sequence types in Python, and I've reassigned that to the value, 42.
So, I can say item number zero, or all the way up to item number four, and it will reassign that value. Now on the other hand, a tuple is exactly like a list, only it's immutable, and it's indicated by parentheses. And so, if I try and run this with a tuple instead of a list, you notice I get the error, "TypeError: 'tuple' object "does not support item assignment." That's because a tuple is not mutable, you cannot change it, or sometimes it's referred to as being immutable.
So, if I remove this line, and run it again, you'll notice that it works exactly like the list. I find that it's generally a good idea to favor the immutable type tuple over the immutable type list, unless you know that you need to change the elements in the list. So, I'll tend to use the parentheses by default, and only use the square brackets when I know that I need to change something. You can also create a sequence using range.
And so instead I can say here range sub five, like that, and when I save and run it, again, it starts at zero, and it ends at the number before what you specify. So, that end is actually the last item minus one. So, when I specify a five, it'll end at four, if I specify a 10, save and run that, you notice I get a sequence only up to nine, and it also starts at zero, I have to scroll backwards to be able to see that.
There's actually three possible parameters for range. If you give it just one parameter, it takes this as the end mark, and it starts at zero. I can change the start, I can say start at five instead, and when I save and run this, you notice I get five, six, seven, eight, nine, or I can say end at 50, but step by five. And when I save and run that, I get five, 10, 15, 20, so that third parameter is a step by.
If you specify all three, they're start, end, and step, and if you specify a just two, it start and end, and if you specify just one, it's just end. And so, if I save and run this again, I get zero, one, two, three, four. Like the tuple, a range is not mutable, If I try to say X sub two equals 42, save and run, I get this error, "TypeError: 'range' object "does not support item assignment," because it's immutable.
If I want a mutable list, I simply construct a list with the results from range using the list constructor, like this, and save and run it, and now I have zero, one, 42, three, and four. A dictionary is a searchable sequence of key value pairs, and you construct it like this. And so that's a dictionary. And if I just save and run this, you notice that I get, I is one, I is two, I is three, I'm just getting the keys, I'm not getting the values.
If I want to get the keys and values, I can say in my for loop, key comma V, in X.items. So, that'll actually return a to tuple of each of the items, with the key and the value. And then I can print it out like this, I can say, key and value, like that, and it will print the key and the value for each element in the dictionary.
Now, each of these values, the keys and the values can be any type, and the same is true for lists and tuples, any element can be any type. And dictionaries are mutable, so I can come up here and I can say, X sub three, use the key to index it, equals 42, and when I save and run it here, you notice that key three now has a value of 42. So, this is an overview of Python's built in sequence types.
You'll see a lot of examples of these in the rest of the course. These built-in sequence types are very useful, and flexible.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api