Python day 1, python get the first day of this week
Dict key-value set.
1 d = {2 '1': 'a',3 '2': 'b',4 '3': 'c'5 }
(1) obtain the length of a set: len (d) = 3
(2) obtain the value:
Method 1: d [key]; example: d ['1'] = 'A'
If the key does not exist, the system reports a keyError. You can use
If '1' in d:
Print (d ['1'])
Method 2: d. get (key) method. If the key does not exist, None is returned.
Example: d. get ('1 ')
(3) dist features
The first feature of dist is that the search speed is fast. No matter whether dict has 10 or 0.1 million elements, the search speed is the same.. The search speed of list gradually decreases with the increase of elements.
However, there is no cost for fast dict search,The disadvantage of dict is that it occupies a large amount of memory and wastes a lot of content., The list is the opposite. The memory usage is small, but the query speed is slow. Because dict searches by key, keys in a dict cannot be repeated.
The second feature of dict is that the stored key-value pairs have no order.
The third feature of dict is that the key elements must be unchangeable. The basic types of Python, such as strings, integers, and floating-point numbers, can all be immutable. However, if the list is variable, it cannot be used as the key..
(4)
Dict is variable. You can assign a value through d ['4'] = 'D'. If the key already exists, update it.
(5) traverse dict
For key in d:
Print (key)
AM