Dictionary
1. Querying memory Addresses
A = 10
Print (ID (a))
b = A
Print (ID (b))
b = 15
Print (ID (b))
2. Data type
Immutable types: Integer, string, tuple
Mutable type: list, dictionary
The value of a dictionary can be any type, and the key can only be an immutable type.
The dictionary store is unordered. For example:
DIC = {' name ': ' YSW ', ' age ': $, ' hobby ': ' Game ', ' Is_handsome ': True}
Print (DIC)
Print (dic[' name '])
#dic = {1: ' ysw ', ' age ': $, ' hobby ': ' Game ', ' Is_handsome ': True}
#print (DIC)
#print (Dic[1])
#dic = {[up]: ' YSW ', ' age ': $, ' hobby ': ' Game ', ' Is_handsome ': True}
#print (DIC)
#print (dic[[1,2]])
#dic = {' name ': ' YSW ', ' age ': $, ' hobby ': {' game_name ': ' wow ', ' level ': ' + '}, ' Is_handsome ': True}
#print (dic[' hobby ')
#dic = {' 1 ': ' 123 '}, ' age ': $, ' hobby ': {' game_name ': ' wow ', ' level ': ' + '}, ' Is_handsome ': True}
#print (DIC)
#字典两大特点: unordered; key unique
#dic = {' age ': ' YSW ', ' age ': $, ' hobby ': ' Game ', ' Is_handsome ': True}
#print (DIC)
3. Two ways to create a dictionary:
#dic1 = {' name ': ' YSW '}
#dic2 = Dict ((' Name ', ' YSW '),)
4. Corresponding operation
(1) Increase
Dic1 = {' name ': ' YSW '}
Dic1[' age '] = 18
Print (DIC1)
dic1[' name '] = 18
Print (DIC1)
#没有重复的键就添加, replace the value with a duplicate key
------------------------------
Dic1 = {' name ': ' YSW '}
Dic1[' age '] = 18
Print (DIC1)
Dic1.setdefault (' age ', 36)
Print (DIC1)
#dic1. SetDefault (' hobby ', ' game ')
#print (DIC1)
#setdefault的特点: No duplicate keys are added, there is no change to duplicate keys
Python Learning: Dictionaries