Functions: Composite Data Types
00:06 In this section on built-in functions—some functions for composite data types; you’re going to take a look at some math—or maths, if you’re from the UK; you’re going to look at type conversion functions; you’re going to see some functions to do with iterables and iterators; some functions for input and output; functions for variables, references, and scope; and finally, some miscellaneous functions that don’t easily fit into other categories.
Composite Data Types. Composite data types are made up of other data types. The first one we’re going to look at is the
list is defined using square brackets and a series of other values inside it separated by commas.
and elements of the list can be accessed using square bracket notation, which is zero-indexed as seen here, so
0 will access the first element. So as you can see, that
0 has accessed this first element
It’s also possible to use slicing. Here we can go from number
1 to number
3, but as you can see—that’s number
1, that’s number
2—but it doesn’t include the third element of the list, so much like many of the things we’ve seen already, the last of this is not included.
Now, lists are what’s called mutable, which means it’s possible to change them after they’d been created. So we can change an element using the square bracket notation. As seen here, we can change element
1 by setting it to
6. And now if we look at the list, we can see that element
1 has changed from being
2 to being
So as we can see here, we can access it in the way we’ve seen before with
b[-1], and applying slices as seen before. But one thing we can’t do with a tuple is reassign an element after it’s been created. When we try to do that, we can see that the tuple
does not support item assignment.
A set is a data structure which can only contain unique elements. This will best be demonstrated in the context of a program. So firstly, we have a list which has multiple elements. And in this case, there are several examples of
3 in it. A set cannot do this, but it can be really useful for seeing the unique values within a list.
So this is one application for a set, where we can find the unique values within that list. And we can produce a list from that as well. So, we can cast that to a list by
c = list() based on
b, and then we print
Now we have a list that we can access in the normal manner, with only the unique values which were in that first list
a. Now, sets are the kind of thing you may have implemented yourself if you didn’t know that a set existed, but as with all these examples, using the built-in Python function will be faster and less error-prone than coding your own version of it.
Now, if you’re not sure what that is, this example should hopefully clear that up for you. A dictionary can be created using the keyword
dict(), creating an empty dictionary, and then elements can be added to it using the following notation.
And it’s possible to print out the entire contents of the dictionary just by printing
a. So here, you’re going to see the contents of the dictionary and there’s a few things to note. Firstly, the keys, in this case, are strings with a string
'name'—they don’t have to be, they could be ints or other values.
Now, if you want to access the information stored in a dictionary, we can do this by printing
a, square brackets, and then the key—in this case, the string
'age'—and that will work and it returns the
But what if we tried to access something that isn’t there? Let’s try accessing
'country' and see what happens. And here, you can see that a
KeyError has happened because we’ve tried to access something with a key that doesn’t exist. This could cause a problem in your code, so a safer way to access values in dictionaries is using the
.get() method, and here we’re putting the key string in there and it returns the value
.get() method is a safer way of accessing values because it’s trying to look up the value for
'country', and instead of generating a
KeyError it’s returned the value
None, which is a much safer value for your code to have to handle.
.get() is a much safer way of accessing than trying to access the values directly, and while your code would need to deal with that problem of returning
None, it’s much easier than having to deal with an exception.
Become a Member to join the conversation.