And dictionary data type immutable type

Immutable type: plastic, string, a tuple

Variable types: lists, dictionaries

Dictionary: Dictionary two main characteristics: disorderly, Unique

Create dictionary:

dic1={'name':louis,'age':21}

dic2=dict((('name','louis'),('age',20)))

dic3=dict([('name','louis'),('age',20)])

dict6 = dict.fromkeys(['host1','host2','host3'],'test')
print(dict6) #{'host1': 'test', 'host2': 'test', 'host3': 'test'}

increase:

= {DIC1 'name': 'Alex'} 
DIC1 [ 'Age'] = 18 is
Print (DIC1)

dic1.setdefault appropriate key corresponding to the value returned by the dictionary ( 'age', 34) # key exists, not to launch,
Print (DIC1)

RET = dic1.setdefault ( 'Hobby', 'Girl') # key does not exist, adding a new key-value pairs in the dictionary, the corresponding value
print (dic1)


check:
= {dic3 'Age': 18 is, 'name': 'Alex', 'Hobby': 'Girl'} 
Print (dic3 [ 'name']) by the # key to find
print (dic3.keys ()) # Query Key
print return value is not the list (type (dic3.keys ())) # query type, Keys ()
print (list (dic3.keys ())) # is converted to a list of
print (list (dic3.values ()) ) # inquiry value
print (list (dic3.items ()) ) # query key

modifications:
dic3 = { 'Age': 18 is, 'name': 'Alex', 'Hobby': 'Girl'} 
dic3 [ 'Age'] = 55
Print (dic3)
DIC4 = { '. 1': 'R & lt'}
dic3. update (dic4) # Add all the elements of the dictionary in the dictionary 4 to 3, the same overlay
print (dic3)
delete:
= {dic3 'Age': 18 is, 'name': 'Alex', 'Hobby': 'Girl'} 
RET = dic3.pop ( 'name') # key to delete the name and returns the value of the name
a = dic3.popitem () # delete a random key pair, the key difference between the return 
print (a, dic3)

Print (RET)
Print (dic3)
del dic3 [ 'Age'] # key to delete a
Print (dic3)
dic3.clear () # the dictionary is cleared, but there is an empty dictionary
Print (dic3)

del # delete the entire dictionary dic3
 


    

Guess you like

Origin www.cnblogs.com/styleonme/p/11116877.html