1. {} input is curly brace
Mycat = {' Size ': ' fat ', ' color ': ' Gray ', ' disposition ': ' Loud '} Key: Value
mycat[' size ' = fat
2, 3 Methods of the dictionary:
Keys (), values (), items (), the values returned by these methods are not real lists, they cannot be modified, but can be traversed with a for loop
For V in Mycat.values ():
Print (v)
For I in Mycat.keys ():
Print (i)
For k,v in Mycat.items ():
Print (k ': ' V)
3. Check if there is a key-value in the dictionary:
' Name ' in Spam.keys ()
' Value ' in Spam.values ()
4. Get () Method:
Before accessing the value of a key, it is troublesome to check that the key exists in the dictionary, and the Get () method is a good solution to the problem: if the key is present, the value is returned, otherwise the alternate value is returned
Spam.get (' CPUs ', 0)
4, SetDefault () method:
The SetDefault () method provides a way to join the key if a key does not exist in the hash and set it to the passed value, and if the key exists in the hash, the value of the key is returned. This method ensures that a key exists. Very practical
Spam = {' name ': ' Pooka ', ' Age ', 5}
Spam.setdefault (' Color ', ' write ')
8-13. Python Hash Review