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.
00:38
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
. A list
is defined using square brackets and a series of other values inside it separated by commas.
00:59 Once created, it can be printed out as seen here,
01:03
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 1
.
01:20
Element 1
is the number 2
, and so on. It’s also possible to use negative indexing to access from the back of the list. So this will be -1
, -2
will give us 4
, and so on.
01:35 This is useful because you don’t have to get the length of the list and then subtract away from that. That makes life much easier when dealing with lists.
01:44
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.
02:03 That’s something that initially will probably catch you out a few times, but after you get used to it it will make perfect sense and it can make some elements of coding much easier.
02:14
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 6
.
02:34 This allows us to work with lists in multiple different ways, and lists will be a core part of your Python programming throughout your career.
02:43
The next data type we’re going to look at is a tuple
, which looks very similar to a list, but it’s defined using normal curve brackets, or braces.
02:53
So as we can see here, we can access it in the way we’ve seen before with b[0]
, 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
.
03:17 Once you’ve created it, it can’t be changed—it is immutable. Now, that’s a feature which can be useful, but it’s also something that may trip you up from time to time.
03:29 So, that’s tuples and lists. Next will be the set.
03:40
Composite Data Types: set
.
03:45
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.
04:09
Here we’re going to create a set b
by passing it the list a
when it’s constructed, so all the values within a
will be passed into b
, and we’re going to print out a
and also print out b
.
04:24
And now you can see the program in action, and you can see that while the list contains multiple 3
values, our set contains only those unique values 1
, 2
, 3
, and 4
.
04:37
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 c
out.
04:55
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.
05:22
Composite Data Types: dict
. A dictionary is probably best thought of at this point as a lookup table which allows you to store values in it so you can store values with a key and value pairing.
05:38
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.
05:55
We have a square bracket and the key inside that, and then we can set the value after the equal sign. So here we’ve sat the value of 'name'
to be 'Darren'
.
06:07
And we can put in other keys. So here, the value of 'age'
has been set to 48
.
06:16
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 'age'
and 'name'
—they don’t have to be, they could be ints or other values.
06:40
And in this case, we stored different types of data related to them, so 'name'
is a str
, whereas the 'age'
is an int
.
06:49
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 int
48
.
07:08 It’s looked it up in the dictionary and then printed that out.
07:13
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 None
.
07:43
So the .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.
07:58
So .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.
08:11 Dictionaries are an incredibly useful data structure and can help you with many situations across your programming career, and they’re worth learning how to deal with efficiently and safely.
08:23 Real Python has a video series based on dictionaries which will help reinforce your understanding and allow you to understand them much better and use them efficiently.
Jonathan Velez on March 29, 2020
Arrays and lists are essentially the same thing. Python just calls it a list as opposed to other languages call it an array.
Zarata on March 31, 2020
Fortunately, I have done enough Python tutorial prior and elsewhere that I can follow just fine. I am traveling this basic intro. path to fill in any gaps. However, at this point I see the true, raw novice – who has just installed Python for the first time and chosen an editor / IDE – is in sudden confrontation with methods, if-else conditionals, separately written modules launched from command line, lists, and dictionaries. These provide flavour and meaning to the basic data types, but may be a bit of a jolt for some / many trying to learn from scratch.
birajksahu on June 21, 2020
@Jonathan: Arrays and lists are not the same in python, even when they have lot of similarities. The most important difference is list can be used to store heterogeneous data types, but array can be used to store data of a single type.
Tobi Olusa on July 10, 2020
@birajkshau Would you be more kind by telling us some instances in the differences of list and array?
Become a Member to join the conversation.
ricardoaparicio92 on March 22, 2020
In python, is there no difference between lists and arrays?