diff --git a/pythonProject/数据类型/字典/clear.py b/pythonProject/数据类型/字典/clear.py new file mode 100644 index 0000000..a725938 --- /dev/null +++ b/pythonProject/数据类型/字典/clear.py @@ -0,0 +1,7 @@ +contacts = { + 'name':'张三', + 'sex':"男", + 'salary':'8000' +} +contacts.clear() +print(contacts) \ No newline at end of file diff --git a/pythonProject/数据类型/字典/copy.py b/pythonProject/数据类型/字典/copy.py new file mode 100644 index 0000000..5a53a6e --- /dev/null +++ b/pythonProject/数据类型/字典/copy.py @@ -0,0 +1,10 @@ +contacts = { + 'name':'张三', + 'sex':"男", + 'salary':'8000' +} +contacts_copy = contacts.copy() # 复制字典 +contacts_copy['age']='18' # 1添加或更新键值对 +print(contacts) +print('-------------------------------------------------------------------') +print(contacts_copy) diff --git a/pythonProject/数据类型/字典/del.py b/pythonProject/数据类型/字典/del.py new file mode 100644 index 0000000..e5654e1 --- /dev/null +++ b/pythonProject/数据类型/字典/del.py @@ -0,0 +1,7 @@ +contacts = { + 'name':'张三', + 'sex':"男", + 'salary':'8000' +} +del contacts['name'] # 删除键值对 +print(contacts) \ No newline at end of file diff --git a/pythonProject/数据类型/字典/get.py b/pythonProject/数据类型/字典/get.py new file mode 100644 index 0000000..f110c29 --- /dev/null +++ b/pythonProject/数据类型/字典/get.py @@ -0,0 +1,8 @@ +contacts = { + 'name':'张三', + 'sex':"男", + 'salary':'8000' +} +print(contacts.get('name')) # 按键访问值 +print(contacts.get('age')) # 按键访问值,键不存在返回None +print(contacts.get('age','默认值')) # 按键访问值,键不存在返回默认值 \ No newline at end of file diff --git a/pythonProject/数据类型/字典/in.py b/pythonProject/数据类型/字典/in.py new file mode 100644 index 0000000..88e6816 --- /dev/null +++ b/pythonProject/数据类型/字典/in.py @@ -0,0 +1,6 @@ +contacts = { + 'name':'张三', + 'sex':"男", + 'salary':'8000' +} +print('age' in contacts) # 判断键是否存在字典里面 \ No newline at end of file diff --git a/pythonProject/内置函数/items.py b/pythonProject/数据类型/字典/items.py similarity index 100% rename from pythonProject/内置函数/items.py rename to pythonProject/数据类型/字典/items.py diff --git a/pythonProject/数据类型/字典/pop.py b/pythonProject/数据类型/字典/pop.py new file mode 100644 index 0000000..39ebf92 --- /dev/null +++ b/pythonProject/数据类型/字典/pop.py @@ -0,0 +1,11 @@ +contacts = { + 'name':'张三', + 'sex':"男", + 'salary':'8000' +} +print(contacts.pop('name')) # 删除键值对,返回值 +del contacts['sex'] # 删除键值对 +print(contacts) + +# pop能返回被删除的值,而del不能返回被删除的值 +# pop()能指定默认值,而del不能指定默认值 \ No newline at end of file diff --git a/pythonProject/数据类型/字典/popitem.py b/pythonProject/数据类型/字典/popitem.py new file mode 100644 index 0000000..36fa961 --- /dev/null +++ b/pythonProject/数据类型/字典/popitem.py @@ -0,0 +1,7 @@ +contacts = { + 'name':'张三', + 'sex':"男", + 'salary':'8000' +} +print(contacts.popitem()) # 删除随机键值对,返回键值对 +print(contacts) \ No newline at end of file diff --git a/pythonProject/数据类型/字典/字典.md b/pythonProject/数据类型/字典/字典.md new file mode 100644 index 0000000..d7601a9 --- /dev/null +++ b/pythonProject/数据类型/字典/字典.md @@ -0,0 +1,19 @@ +# 字段的基本信息 +## 1. 字典是Python中唯一的映射类型 +## 2. 字典是无序的 +## 3. 字典是可迭代对象 +## 4. 字典的构成 + 键:key + 值:value + 映射:键映射值 + 键-值:键值对,又叫项 +# 字典的操作 +## 1. 添加、更新键值对 +## 2、访问字典 get()按键访问值 +## 3、浅拷贝 copy() +## 3. 删除键值对 del删除键值对 pop() popitem() clear()清空字典 +## 4. 判断键是否存在字典里面 in +## 5. len属性 +## 6. keys +## 7. values +## 8. items