About python dictionary operations

Dictionary and a list of differences:

  • The list is ordered
  • A dictionary is unordered
  • Dictionary definitions using {}
  • Using the key-value dictionary of stored data, using key-value pairs between "," separator
  1. Key index is key
  2. Value is the value of the data
  3. Use between the key and value a ';'
  4. The key is the only
  5.  Value can be any data, the key can only be strings, numbers, or the tuple

Dictionary of operation:

xiaoniu_dict = {"name" : "xiaoniu",
                "age" : 21}


# 取值
print("********取值************")
print(xiaoniu_dict["name"])

# 增加
print("********增加************")
xiaoniu_dict["height"] = 1.65
print(xiaoniu_dict)

# 修改
print("******** 修改************")
xiaoniu_dict["age"] = 22
print(xiaoniu_dict)

# 删除
print("********删除************")

xiaoniu_dict.pop("height")
print(xiaoniu_dict)

# 合并
print("********合并************")
temp_dcit = {"height" :1.65}
xiaoniu_dict.update(temp_dcit)
print(xiaoniu_dict)



# 清空
print("*******清空************")
xiaoniu_dict.clear()

print(xiaoniu_dict)

result:

 

Guess you like

Origin www.cnblogs.com/icebluelp/p/11615749.html