Understanding Mappings:
A map is the correspondence of a key (index) and a value (data). A dictionary is a collection of key-value pairs that are unordered between key values. The curly braces denote {}, and Dict () are created, and the key-value pairs are represented by a colon:.
{key: Value, key: Value, key: Value}
>>> d={"China":"Beijing","United States":"Washington","United Kingdom":"London"}>>>d{'China':'Beijing','United States':'Washington','United Kingdom':'London'}>>> d["China"]'Beijing'>>> de={}" "define an empty dictionary" ">>>type (DE)<class'Dict'>
{} is used to generate an empty dictionary type, and if the collection type is empty, a set function is required and cannot be generated using {}.
How to handle dictionary types:
Del D[k] Delete the data value corresponding to K in dictionary D.
K in D whether a key is in dictionary D
D.keys () returns all key information in dictionary D
D.values () Returns all value information in dictionary D
D.items () returns information for all key-value pairs in dictionary D
>>>"China" inchdtrue>>>D.keys () Dict_keys (['China','United States','United Kingdom']) >>>d.values<built-inchMethod values of DictObjectAt0x0000000003011828>>>>d.values () dict_values (['Beijing','Washington','London'])/returned is not a list type and can be traversed in a for-in way, but not as a list type>>>D.items () dict_items ([('China','Beijing'), ('United States','Washington'), ('United Kingdom','London')])>>> del d["China"]>>>d{'United States':'Washington','United Kingdom':'London'}
D.get (k,<default>) key k exists, the corresponding value is returned, the default value is not returned
D.pop (k,<default>) key k is present, the corresponding value is removed, the corresponding key-value pair is deleted, and the default value is not returned
D.popitem () Randomly extracts a key-value pair from dictionary D and returns it as a tuple
D.clear () Removes all key-value pairs.
Len (d) Returns the number of elements in D
>>> d={}>>> d["type"]=2>>> d[" value "] =>>> d{'type'2'value ' + '
The application scenario of a dictionary: the expression of a map
Python Learning Notes (10)--Combined data type (dictionary type)