Python 字典与条件语句详解
·
字典(Dictionary)
什么是字典
字典是Python中无序、可变、可索引的集合,用于存储键值对。字典用大括号表示,每个键值对用冒号分隔。
创建字典
python
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
访问字典元素
python
# 通过键名访问
x = thisdict["model"]
print(x) # 输出: Mustang
# 使用get()方法
x = thisdict.get("model")
print(x) # 输出: Mustang
修改字典
python
# 修改值 thisdict["year"] = 2019 print(thisdict) # 添加新键值对 thisdict["color"] = "red" print(thisdict)
遍历字典
python
# 遍历所有键
for x in thisdict:
print(x)
# 遍历所有值
for x in thisdict:
print(thisdict[x])
# 使用values()方法遍历值
for x in thisdict.values():
print(x)
# 使用items()方法遍历键值对
for x, y in thisdict.items():
print(x, y)
字典操作
python
# 检查键是否存在
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
# 获取字典长度
print(len(thisdict))
# 删除元素
thisdict.pop("model") # 删除指定键
thisdict.popitem() # 删除最后插入的项
del thisdict["year"] # 删除指定键
thisdict.clear() # 清空字典
del thisdict # 删除整个字典
复制字典
python
# 使用copy()方法
thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964}
mydict = thisdict.copy()
print(mydict)
# 使用dict()构造函数
mydict = dict(thisdict)
print(mydict)
嵌套字典
python
# 直接创建嵌套字典
myfamily = {
"child1": {
"name": "Emil",
"year": 2004
},
"child2": {
"name": "Tobias",
"year": 2007
},
"child3": {
"name": "Linus",
"year": 2011
}
}
# 通过已有字典创建
child1 = {"name": "Emil", "year": 2004}
child2 = {"name": "Tobias", "year": 2007}
child3 = {"name": "Linus", "year": 2011}
myfamily = {
"child1": child1,
"child2": child2,
"child3": child3
}
字典构造函数和方法
python
# 使用dict()构造函数
thisdict = dict(brand="Ford", model="Mustang", year=1964)
print(thisdict)
# 字典方法示例
thisdict = {"brand": "Ford", "model": "Mustang", "year": 1964}
print(thisdict.keys()) # 返回所有键
print(thisdict.values()) # 返回所有值
print(thisdict.items()) # 返回所有键值对
字典方法总结
| 方法 | 描述 |
|---|---|
| clear() | 删除字典所有元素 |
| copy() | 返回字典副本 |
| fromkeys() | 创建具有指定键和值的字典 |
| get() | 返回指定键的值 |
| items() | 返回包含键值对的列表 |
| keys() | 返回包含字典键的列表 |
| pop() | 删除指定键的元素 |
| popitem() | 删除最后插入的键值对 |
| setdefault() | 返回指定键的值,不存在则插入 |
| update() | 使用指定键值对更新字典 |
| values() | 返回字典中所有值的列表 |
Python 条件语句(if...else)
逻辑条件
Python支持以下逻辑条件:
-
等于:
a == b -
不等于:
a != b -
小于:
a < b -
小于等于:
a <= b -
大于:
a > b -
大于等于:
a >= b
基本if语句
python
a = 33
b = 200
if b > a:
print("b is greater than a")
缩进的重要性
Python使用缩进来定义代码块范围:
python
# 正确
if b > a:
print("b is greater than a")
# 错误(会报错)
if b > a:
print("b is greater than a")
elif语句
python
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else语句
python
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
# 简化的else
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
简写语法
python
# 单行if语句
if a > b: print("a is greater than b")
# 单行if...else
a = 2
b = 330
print("A") if a > b else print("B")
# 多条件单行
a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")
逻辑运算符
python
# and 运算符
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
# or 运算符
if a > b or a > c:
print("At least one of the conditions is True")
嵌套if语句
python
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
pass语句
python
# 避免空if语句错误
a = 33
b = 200
if b > a:
pass # 什么也不做,避免语法错误
总结
字典要点
-
字典是键值对的集合,无序但可索引
-
键必须是不可变类型,值可以是任意类型
-
提供快速查找、插入和删除操作
-
支持嵌套结构,可以创建复杂的数据结构
条件语句要点
-
使用缩进定义代码块,而不是花括号
-
支持多种条件判断:if、elif、else
-
提供逻辑运算符and、or进行复杂条件判断
-
支持嵌套条件语句
-
可以使用简写语法简化代码
这两种结构是Python编程的基础,字典用于高效存储和检索数据,条件语句用于控制程序流程,两者结合可以构建复杂的逻辑和数据处理程序。
更多推荐




所有评论(0)