English subtitles

← Dictionaries - Intro to Computer Science

Get Embed Code
2 Languages

Showing Revision 5 created 05/25/2016 by Udacity Robot.

  1. So now that we've built it ourselves, I'm going to show
  2. you the easy way to do it, which is to
  3. use the built in Python type called a dictionary. And
  4. what a dictionary is is really an implementation of a hash
  5. table. It's built into Python. So it's much easier to
  6. use than defining our own hash table. So far, we've
  7. seen two complex types in Python. We've seen the string
  8. type and we've seen the list type. Now we're going to introduce
  9. the dictionary type. So there are many things that are
  10. common between these three. There are other things that are
  11. different. So to create a string we could have a
  12. sequence of characters inside quotes. To create a list, we
  13. use the square bracket. And we could have a sequence
  14. of elements inside the square bracket, and the elements could
  15. be any type, unlike a string where they had to
  16. be characters. So, our string was a sequence of characters.
  17. Our list was a list of any kind of value. So, a list of elements of any value.
  18. The dictionary type we're going to create using the curly
  19. bracket. And the entries inside the dictionary are key
  20. value pairs. So here I've created a dictionary with
  21. two elements. And each element, the key is a
  22. string. Here is the string hydrogen and the value
  23. associated with that element is a number. The keys
  24. in a dictionary can be any immutable value, so they don't
  25. need to be strings, they could be numbers. They could be
  26. other things. The values can be any value. So what a
  27. dictionary is, is a set of key value pairs and the property
  28. that a dictionary will give us is like the hash table,
  29. that we can look up a key and get a value associated
  30. with that key. So one important property of all types is
  31. whether they're immutable or mutable. We saw that the string was immutable.
  32. That means once we create a string, we can't
  33. modify that string. It has the same value it always
  34. has. A list was mutable, we could change the values
  35. of elements in the list after we create the list.
  36. We could append to the list, adding new elements
  37. to it. A dictionary is also mutable. So after we
  38. create a dictionary, we could add new key value pairs
  39. to it. We could change the value of values associated
  40. with keys. We saw with strings, if s is a string, then we can do s index i.
  41. That will give us the ith letter in the
  42. string. With lists, we can do something very similar. If
  43. p is a list, we can do p index i to get the ith element of the list. With
  44. dictionaries, it's a little bit different. The syntax is
  45. the same. We can do d index k. With both
  46. strings and lists, these were numbers. Here the k is
  47. whatever the key value is. So this is a key
  48. in the dictionary. What d index k will give us
  49. the value associated with that key in the dictionary. We'll
  50. see some examples soon. We can also use our indexing
  51. and assignments. We saw, with the list, we could do
  52. this. That would replace the value of the ith element
  53. of p with whatever we have here. With the string,
  54. we can't do that. That produces an error.
  55. And the reason we can't do it is because
  56. strings are immutable. So there's no way to
  57. modify characters in a string. With the dictionary we
  58. can. And what the meaning of this, is
  59. very similar to the meaning of update in our
  60. hash table. So that's equivalent to update the
  61. value. The value associated with k to be v.