当前位置: 首页 > news >正文

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 = 41if 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 = 200if b > a:pass  # 什么也不做,避免语法错误

总结

字典要点

  • 字典是键值对的集合,无序但可索引

  • 键必须是不可变类型,值可以是任意类型

  • 提供快速查找、插入和删除操作

  • 支持嵌套结构,可以创建复杂的数据结构

条件语句要点

  • 使用缩进定义代码块,而不是花括号

  • 支持多种条件判断:if、elif、else

  • 提供逻辑运算符and、or进行复杂条件判断

  • 支持嵌套条件语句

  • 可以使用简写语法简化代码

这两种结构是Python编程的基础,字典用于高效存储和检索数据,条件语句用于控制程序流程,两者结合可以构建复杂的逻辑和数据处理程序。

http://www.dtcms.com/a/463284.html

相关文章:

  • 怎么用手机制作网站网站怎么做内链接地址
  • 罗定市住房和城乡建设局网站WordPress自己安装了插件
  • java应用dubbo接口超时问题分析,数据库方向
  • 免费的开发网站建设易居系统登录
  • 网站建设中的功能模块描述网站开发的学习路线
  • 个人网站自己怎么做网站首页
  • 第28节:网络同步与多人在线3D场景
  • 怎么做网站报告小勐拉网站建设
  • 小游戏网站网址个人网站示例
  • dedeseo网站wordpress 分类文章数
  • 房山网站建设优化seo小企业网站建设的连接方式
  • Linux系统性能监控与故障定位实战:CPU/内存/I/O/网络
  • 网站地图生成工具深圳物流公司哪家便宜又好
  • 站内优化主要从哪些方面进行古典 网站模板
  • 网站建设 经典书籍做网站需要购买网站空间吗
  • 网站建设用的服务器注册资金是什么意思
  • 重庆网站建设的公司熊猫办公ppt模板下载
  • 天门市住房和城乡建设局网站新东方教育培训机构官网
  • 活动 网站 源码网站制作收费标准
  • 移动网站建设可信赖药品网站 icp
  • 360的网站排名怎么做wordpress主题如何升级
  • OpenShift Virtualization - 为使用 ovn-k8s-cni-overlay 类型网络的 VM 分配 IP 地址
  • 查网站备案名称有什么可以在线做奥数题的网站
  • 设计模式--装饰器模式:动态扩展对象功能的优雅设计
  • 西安网站建设公司都有哪些wordpress幻灯片的调用
  • 文章资讯类网站哪里有做网站服务商
  • 走进 OpenCV 人脸识别的世界
  • 做网站的如何说服客户关键词投放
  • 河北省建设局材料备案网站推荐一个seo优化软件
  • 牛客算法_动态规划