Python中字典的增删查操作示例
在Python中,字典是一种可变容器模型,它通过键(key)来存储和访问数据。下面是一些字典的增删查操作示例:
创建字典:
dict1 = {'name': 'Alice', 'age': 25}
查询(查找键或值):
```python
print(‘Name:’, dict1[‘name’]) # 查找键
print(‘Age:’, dict1.get(‘age’)) # 查找键,若键不存在则返回None
print(‘Is Alice in the dictionary?’, ‘Alice’ in dict1) # 判断值是否存在
3. 增加(添加新的键值对):
```python
dict1['gender'] = 'Female'
- 删除(删除特定的键值对):
del dict1['age']
以上就是Python中字典的增删查操作示例。
还没有评论,来说两句吧...