Practical Guide to Python Practical Guide to Python

Sets, Tuples, & Dictionaries Practice

Check out a free preview of the full Practical Guide to Python course:
The "Sets, Tuples, & Dictionaries Practice" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina walks the students through the Sets, Tuples, & Dictionaries practice.

Get Unlimited Access Now

Transcript from the "Sets, Tuples, & Dictionaries Practice" Lesson

[00:00:00]
>> We went through the practice for sets, tuples, and dictionaries, as well as the practice for logic and functions. After lunch, we're going to review loops and the exercises for loops. I feel like I am doing a pretty good job today cuz I think everybody is back, so you're learning and you're sticking, you're sticking around for the long haul.

[00:00:26] So super thrilled about that. Now, before I kick things off, does anyone have any quick questions that are not related to the exercises because I'm gonna be going through those with you. Now, all right, you're all Python experts. Awesome, do my job. Let's talk about sets, tuples and dictionaries.

[00:00:54] This is the practice that you should have gone through. So let's take a walk. Starting the python ripple. We remember that in order to create an empty set. If we check for the type of my set, it's going to be a dictionary. So we create an empty set by calling the set constructor.

[00:01:22] Empty list is just two brackets, empty dictionary is two curly braces. In empty tuple is parentheses with a sorry, empty tuple is one set a parenthesis with one item in it has a comma. And an empty set is created by using this set constructor. Remember that lists, sets, and dictionaries are mutable.

[00:01:59] Tuples are immutable. If you need a shortcut to figure out if an item is mutable or not, you can pass it into the hash function. So a hash of an empty tuple has a value hash of an empty dictionary. Sorry, an empty, set Does not have a value.

[00:02:27] A hash of an empty dictionary does not have a value and a hash of an empty list does not have a value. Those are all unhashable types because they are mutable and they can't be changed. While if we check the hash for an integer or the hash for a string, those do have values because they are immutable.

[00:02:51] Now, remember that for a set, every item in that set needs to be unique. So if I have a list of names that has some duplicates in it, [SOUND] and I pass these into the constructor per set. To make them into a set, that set is only going to have two values in it.

[00:03:20] Sets don't have order. So this order is not guaranteed. That also means that I cannot ask for the zero item sorry, I need to make a variable here, call this my names set. I cannot ask for the zero if items in a set. I'll get an error that says the set object is not sub scriptable.

[00:03:51] That means that it does not have an order to it. That also means that I cannot change values in my set at, for example, the zero index. The set object does not support item assignment. We talked a little bit about tuples, tuples need to have that comma if they only have one value.

[00:04:32] You need to type that correctly. Something else that you might see that is valid Python syntax. But I recommend against using it is you can actually create these tuples without the parentheses around them. So I can say, my tuple is equal to 1 and a comma. This is valid syntax in Python.

[00:05:02] If I check the type of my tuple it's a tuple. I can take a look at it. I very much recommend that you do not do this and that you be as explicit as possible. That's because this can be the source of a lot of really weird errors.

[00:05:21] When you have a function, you're passing an arguments to it, each argument is on its own line with that trail link comma. Let's say you're doing a little bit of refactoring and you pull out one of those arguments onto its own line to make it a variable and you forget to remove this trail link comma, now it's a tuple.

[00:05:44] You aren't expecting a tuple but you got a tuple. And even recently, this is the kind of bug that has just bitten me for hours because I'm looking at it. And this commas just invisible in my brain. And I'm not understanding why I'm getting this error about tuples when I'm not seeing the tuple so I recommend always being explicit and adding those parentheses around your tuples.

[00:06:13] Now, we talked about how you can unpack tuples. So if you have a student and the student has a name and an age and a favorite subject and a GPA. Let's say John is about student. We can use tuple unpacking to grab multiple values out of this tuple all at once.

[00:06:36] So we can say name, age, subject. GPA is equal to student. And now I will have all of those variables defined. If I do not have a match between the number of variables on the left hand side with the number of values in the tuple, this is gonna blow up with an error that says too many values to unpack.

[00:07:01] Remember when you are doing any sort of unpacking like this remember this error, you'll probably be seeing a quite a bit as you go through python code and now you can just zero in to exactly what that means. If there is a variable that we don't care about saving to use later, can use this anonymous underscore to say, hey, here's a placeholder so that I have the right amount of variables on the left side of the expression, I don't care about saving it though.

[00:07:39] And now I don't have that value error. We talked about dictionaries, and how dictionaries are defined with the curly braces, and the colons. So, I can say that my dictionary has a key separated by a colon, and then the value. And I can not access items in a dictionary by position, because dictionaries do not have an order, they only have keys.

[00:08:14] So if I tried to ask my dictionary for something at position zero, doesn't know what that is. I'm just going to get a key error. If we wanted to add new items to our dictionary, we can do that with a square bracket notation. So if I wanted to get back the value at key, I would use the square bracket notation.

[00:08:38] But unlike a list where I was passing in a position, instead I will pass in a key that is in the dictionary. And I will get back that value for the key. If I wanted to update that value in place, I could just use the equals to assign a value to it.

[00:08:59] And now if I looked in my dictionary, I would see that the value has been overwritten. If I wanted to check to see if a key was in my dictionary, I would use the end keyword. So I can say key in my dict. And that will return true or not a key in my dict and that will return false.

[00:09:24] Remember that lists are mutable, you can change them. So if I had a list here, I can say that my list at zero is not now going to be negative 99. And that will go ahead and change it. Dictionaries are also mutable so I can say my dictionary at key equals some other value.

[00:09:54] And that works just fine. Sets are also mutable. So if I had a set here I can use the .add operator to add a new item to the set tuples not mutable. So if I have my student, I can get the value at position zero, because tuples do have an order.

[00:10:20] In fact, that order is very, very important, but they are not mutable. So if I try to change the name here, I'm going to get an error because the tuple object does not support item assignment. So all of these have different purposes, they all have different pluses, minuses, advantages, and scenarios.

[00:10:43] I recommend that you spend some time reading Python code, and that will give you a better understanding of which data type is best used when. And also, while you're learning, referring back to the cheat sheets, because those are going to give you hints about mutability, about how to access objects about order and about what methods you can use on these different objects.