1 / 25

MIS 4395.A Structured Programming Language

MIS 4395.A Structured Programming Language. Ahmed Imran Kabir Week 9, Dictionaries. Characteristics of Dictionaries. Dictionaries and lists share the following characteristics: Both are mutable. Both are dynamic. They can grow and shrink as needed.

Télécharger la présentation

MIS 4395.A Structured Programming Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MIS 4395.AStructured Programming Language Ahmed Imran Kabir Week 9, Dictionaries

  2. Characteristics of Dictionaries • Dictionaries and lists share the following characteristics: • Both are mutable. • Both are dynamic. They can grow and shrink as needed. • Both can be nested. A list can contain another list. A dictionary can contain another dictionary. A dictionary can also contain a list, and vice versa. • Dictionaries differ from lists primarily in how elements are accessed: • List elements are accessed by their position in the list, via indexing. • Dictionary elements are accessed via keys.

  3. Defining Dictionaries • Dictionaries are Python’s implementation of a data structure that is more generally known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value. • You can define a dictionary by enclosing a comma-separated list of key-value pairs in curly braces ({}). A colon (:) separates each key from its associated value:

  4. Defining Dictionaries (Cont.) • Retrieving the values in the sublist or subdictionary requires an additional index or key: This example exhibits another feature of dictionaries: the values contained in the dictionary don’t need to be the same type. In person, some of the values are strings, one is an integer, one is a list, and one is another dictionary.

  5. Defining Dictionaries (Cont.) • Just as the values in a dictionary don’t need to be of the same type, the keys don’t either: Here, one of the keys is an integer, one is a float, and one is a Boolean. It’s not obvious how this would be useful, but you never know. Notice how versatile Python dictionaries are. In MLB_team, the same piece of information (the baseball team name) is kept for each of several different geographical locations. person, on the other hand, stores varying types of data for a single person. You can use dictionaries for a wide range of purposes because there are so few limitations on the keys and values that are allowed. But there are some. Read on!

  6. Restrictions on Dictionary Keys • Almost any type of value can be used as a dictionary key in Python. You just saw this example, where integer, float, and Boolean objects are used as keys:

  7. Restrictions on Dictionary Keys (Cont.) • You can even use built-in objects like types and functions:

  8. Restrictions on Dictionary Keys (Cont.) • However, there are a couple restrictions that dictionary keys must abide by. First, a given key can appear in a dictionary only once. Duplicate keys are not allowed. A dictionary maps each key to a corresponding value, so it doesn’t make sense to map a particular key more than once. You saw above that when you assign a value to an already existing dictionary key, it does not add the key a second time, but replaces the existing value:

  9. Restrictions on Dictionary Keys (Cont.) • Similarly, if you specify a key a second time during the initial creation of a dictionary, the second occurrence will override the first:

  10. Restrictions on Dictionary Keys (Cont.) • Secondly, a dictionary key must be of a type that is immutable. You have already seen examples where several of the immutable types you are familiar with—integer, float, string, and Boolean—have served as dictionary keys. A tuple can also be a dictionary key, because tuples are immutable:

  11. Restrictions on Dictionary Keys (Cont.) • Recall from the discussion on tuples that one rationale for using a tuple instead of a list is that there are circumstances where an immutable type is required. This is one of them.) However, neither a list nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable:

  12. Restrictions on Dictionary Values • By contrast, there are no restrictions on dictionary values. Literally none at all. A dictionary value can be any type of object Python supports, including mutable types like lists and dictionaries, and user-defined objects, which you will learn about in upcoming tutorials. There is also no restriction against a particular value appearing in a dictionary multiple times:

  13. Python Dictionary Method

  14. Built in functions in Dictionaries

  15. Practice on built in functions

  16. Practice on built in functions (cont.)

  17. Practice on built in functions (cont.)

  18. Practice on built in functions (cont.)

  19. Practice on built in functions (cont.)

  20. Practice on built in functions (cont.)

  21. Practice on built in functions (cont.)

  22. Practice on built in functions (cont.)

  23. Practice on built in functions (cont.)

  24. Practice on built in functions (cont.)

  25. Conclusion Lists and dictionaries are two of the most frequently used Python types. As you have seen, they have several similarities, but differ in how their elements are accessed. Lists elements are accessed by numerical index based on order, and dictionary elements are accessed by key Because of this difference, lists and dictionaries tend to be appropriate for different circumstances. You should now have a good feel for which, if either, would be best for a given situation.

More Related