Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Python has one more very powerful aggregate type. It's called Dictionary, and it's much like what we would call an associative array or a hash in another language. So let's go ahead and make a working copy of variables.py. We'll call this variables-dictionary.py. Go ahead and open up that working copy, and we will start by defining a dictionary, and I will call it d, and when you use the curly brace notation for defining it, and we will use names for keys and numbers for values.
Just going to put five of them in here, and space that out just a little bit and then we will - say, we will print it first here, d, and we'll go ahead and we'll save and run, and you'll see there is our dictionary. Now, this can be useful for a number of things. For example, we can step through the keys and print values, for k in d, and print.
print the key, and we will print the value, which would be d k like this, and if we save that and run it, you will see that we get each of the keys and each of the values from our print statement, so we've iterated through the dictionary, and it's given us the key in the k variable, and we've used that to subscript and to print out the value. Now this is nice, but notice that it's in no particular order, that it's not alphabetical.
It's not numerical. It's in no particular order whatsoever, and this is common for hashed variables in virtually all environments that supports such. So in Python, if you want to get this in assorted order, use the Sorted built-in function and you subscript the dictionary object and use its keys method. So sorted(d.keys()) like that, save it and run it, and now they are in alphabetical order by the keys.
Five, four, one, three, and two. those are alphabetical by the keys. Now this particular syntax for defining the dictionary is a little bit awkward, don't you think? Wouldn't it be nice if there is a better one? Well there is. Python has something called keyword arguments, and we will learn about those more in our chapter on functions. But the keyword arguments are used to a great advantage in defining dictionary objects because we can do it like this, say dict using the constructor for the dictionary class, and I am just going to go ahead and put that closing parenthesis on a separate line and go ahead and show you how this works.
So I can say one = 1, two = 2. Notice I don't have to type any of those nasty quotes. Three = 3, four = 4. Now if I had string objects that I was using at the values, of course, I would need quote, so I could say five = and put the number five in quotes, like that. And if I save this and run it, you will notice that this works exactly the same way, except we have this spelled out, word five there.
So that's a much easier, more convenient, and a more common way to define a dictionary. Dictionaries are mutable objects, so I can add a new value to a dictionary, like this, d 'seven' = 7, and save that, and run it, and now we have the seven value in the middle there in our sorted list. So that's dictionaries in a nutshell. They are very powerful and very useful, and you will see, when we get to our example code, they get used a lot in Python.
Get unlimited access to all courses for just $25/month.Become a member