Python Fundamentals

Practice: Advanced Data Types

Check out a free preview of the full Python Fundamentals course:
The "Practice: Advanced Data Types" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to try typing out their own lists, dictionaries, tuples, and sets using the instructions on the course website. Nina live codes the exercise with students.

Get Unlimited Access Now

Transcript from the "Practice: Advanced Data Types" Lesson

[00:00:05]
>> Nina Zakharenko: Let's look at our lists, so that would be this exercise right here.
>> Nina Zakharenko: So if I had a new list with the characters,
>> Nina Zakharenko: Of hello. Can look at it, I can append something to my list. Where does append insert the that new value?
>> Student: At the end of the list.

[00:00:42]
>> Nina Zakharenko: At the end, that's right. We can look at the length of my list.
>> Nina Zakharenko: And we can do a little bit of list slicing. So, I mentioned earlier that you can access items in a list by position. There's something called slicing that lets you access more than one item in the list at a time.

[00:01:16] So if I wanted to access the first few items, let's put 3 here, that would give me back the items from position 0 to position 3.
>> Nina Zakharenko: I can also optionally, if I'm looking at the beginning or the end, I can leave off one of these values. So if this is not present then,

[00:01:41]
>> Nina Zakharenko: It does the same thing that's kind of the inferred value. And,
>> Nina Zakharenko: Negative indexing is possible too. So if I wanted to look at the very last item in the list, I would use index -1.
>> Nina Zakharenko: There are a few other ways of interacting with our list.

[00:02:08] I can call remove on the list in person the string out, will that remove all of the instances of l?
>> Student: The first instance.
>> Nina Zakharenko: Just the first one, that's right. I can also insert,
>> Nina Zakharenko: Don't forget, like I did, where I got an error that you have to pass a position to insert a required argument.

[00:02:42]
>> Nina Zakharenko: So I talked about popping. But there's another way of deleting items from a list, and that's with the del keyword. And you can say delete the item at my_list 0,
>> Nina Zakharenko: And it'll be gone. Personally I really don't like this type of syntax, it doesn't really fit in with the other data types that we looked at.

[00:03:13] It's kind of there you might see it in other Python code, but I recommend not using it. Or we could just pop,
>> Nina Zakharenko: The last item on the list. Optionally we can pass an argument into pop, and that would be the position to pop from.
>> Nina Zakharenko: We can see if a particular value is in my list with the in keyword, it's not.

[00:03:46] And we can sort in place with my_list.sort(reverse=True), oops. Reverse=True, and now we'll see my list is sorted in reverse order. Remember that calling .sort doesn't return anything. If we wanted a copy instead of modifying the list in place, we would call sorted on my_list, and also parse in the reverse=True argument.

[00:04:24] Okay, working with sets.
>> Nina Zakharenko: Remember that tape of empty brackets is a dictionary, to make a new set we need to use the set constructor
>> Nina Zakharenko: We can check that with type or just look at the variable and see that it's a set. We can add items to our set, or we can just override it with a non-empty,

[00:04:57]
>> Nina Zakharenko: With some values. I can, oops, sorry. I can add a value to it. I can also remove,
>> Nina Zakharenko: A value from it. Okay, so that's mutable. I can also test for membership with the in keyword
>> Nina Zakharenko: Unlike with less, every item in the start has to be unique.

[00:05:28] So if I have my_set and I try to add 3 to it again, we'll see that it's only in there once. Looking at some set operations, making a new set
>> Nina Zakharenko: Oops, I now wanted to call it my_other_set, so let me fix that. We can combine the two sets with a union.

[00:05:55] We use the symbols before but we can also use the methods.
>> Nina Zakharenko: So that's all of the items that are in both. We can get the difference.
>> Nina Zakharenko: So,
>> Nina Zakharenko: What's the value that is in both? Or we can get the intersection.
>> Nina Zakharenko: So, these are the values that are unique to each set.

[00:06:32] Each set already had four in it. Okay, looking at tuples, we know that we can't make a one item tuple with just parentheses, we know that we need to insert a comma there.
>> Nina Zakharenko: So let's make a new tuple.
>> Nina Zakharenko: What happens if we try to add or change a value in our tuple.

[00:07:05] Is this possible? It's not. Tuples are immutable, does not support item assignment. Let's make a new person. Jim, Jim has an age, he's 29 and he lives in Austin, Taxes. We can use tuple unpacking,
>> Nina Zakharenko: To quickly pull out all of the values in a tuple and assign variables to them.

[00:07:33] Now we can look at Jim's name, age and hometown. For dictionaries, remember, they kind of use those curly braces just like a set, but instead it contains key value pairs. The key value is separated with a colon, and each key value pair is separated with a comma. What happens if we try to get the item at position zero in our dictionary?

[00:08:04]
>> [INAUDIBLE]
>> Nina Zakharenko: Unless key zero happens to be there, but dictionaries do not have an order. Okay, let's add a few more items in our dictionary.
>> Nina Zakharenko: And we can take a look at it here. Now there are three items, to get the value for the key Hello, I would use the square bracket like I would for a list access.

[00:08:42] But instead of putting in a position, I would put in a key.
>> Nina Zakharenko: I can also use the mideck.get for a key. What is the difference between using the square brackets to access a key in a dictionary and using .get?
>> Student2: Once you use the key, error that t comes back, or lack of an error.

[00:09:06]
>> Nina Zakharenko: Yeah,lack of an error. So if I use .get on a key that is not in my dictionary, I will not get an error, whereas I will if I use the square. So my dictionary doesn't have baz in it, right? I will get a key error for baz, but if I use my_dict.get("baz") no error.

[00:09:31] And I can also provide a default value to get. So if I call get on a key that's no there, I will get the default value back. If I call get on a key that is there, what result will I see?
>> Student3: The key that's there.
>> Nina Zakharenko: Or the value.

[00:09:51]
>> Student3: The value.
>> Nina Zakharenko: The value for the key that's there, right? So, remember dictionaries are key value pairs, so I can look at dictionary keys, I can look at the dictionary values. Oops, I made a typo not value but values. But if I wanna look at the key value pairs at once, I will need to look at items.

[00:10:17] Now we'll return into data structure that kind of look like a list, but it basically contain the sequence of tuples. And we'll use that later with tuple unpacking when we talk about looping. Very quickly, mutability.
>> Nina Zakharenko: Okay, lists are mutable.
>> Nina Zakharenko: We'll see that the value changed. Dictionaries are also mutable.

[00:11:04]
>> Nina Zakharenko: To add a new key, her foo, and the value is bar. Okay, sets are also mutable.
>> Nina Zakharenko: But we can't change our set with,
>> Nina Zakharenko: With the index here. Why is that?
>> Student3: Because it's.
>> Student: Not ordered.
>> Nina Zakharenko: It's not ordered.
>> Nina Zakharenko: Okay, I can use .add, .remove, and .discard.

[00:11:45] Tuples on the other hand are immutable.
>> Nina Zakharenko: So if I try to change, I can do this, right? I can look at the first item in my tuple that index 0. But if I try to change it, when I get an error, the tuple object does not support item assignment.