no message

This commit is contained in:
yinjun.chen1
2026-10-10 17:30:24 +08:00
parent f73d5deacb
commit 9af18aeb35
9 changed files with 75 additions and 0 deletions
@@ -0,0 +1,7 @@
contacts = {
'name':'张三',
'sex':"男",
'salary':'8000'
}
contacts.clear()
print(contacts)
+10
View File
@@ -0,0 +1,10 @@
contacts = {
'name':'张三',
'sex':"男",
'salary':'8000'
}
contacts_copy = contacts.copy() # 复制字典
contacts_copy['age']='18' # 1添加或更新键值对
print(contacts)
print('-------------------------------------------------------------------')
print(contacts_copy)
+7
View File
@@ -0,0 +1,7 @@
contacts = {
'name':'张三',
'sex':"男",
'salary':'8000'
}
del contacts['name'] # 删除键值对
print(contacts)
+8
View File
@@ -0,0 +1,8 @@
contacts = {
'name':'张三',
'sex':"男",
'salary':'8000'
}
print(contacts.get('name')) # 按键访问值
print(contacts.get('age')) # 按键访问值,键不存在返回None
print(contacts.get('age','默认值')) # 按键访问值,键不存在返回默认值
+6
View File
@@ -0,0 +1,6 @@
contacts = {
'name':'张三',
'sex':"男",
'salary':'8000'
}
print('age' in contacts) # 判断键是否存在字典里面
+11
View File
@@ -0,0 +1,11 @@
contacts = {
'name':'张三',
'sex':"男",
'salary':'8000'
}
print(contacts.pop('name')) # 删除键值对,返回值
del contacts['sex'] # 删除键值对
print(contacts)
# pop能返回被删除的值,而del不能返回被删除的值
# pop()能指定默认值,而del不能指定默认值
@@ -0,0 +1,7 @@
contacts = {
'name':'张三',
'sex':"男",
'salary':'8000'
}
print(contacts.popitem()) # 删除随机键值对,返回键值对
print(contacts)
@@ -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