首页 > Python资料 博客日记
Python 条件判断:全面掌握 if、else、elif 语句的用法
2024-10-04 03:00:04Python资料围观25次
这篇文章介绍了Python 条件判断:全面掌握 if、else、elif 语句的用法,分享给大家做个参考,收藏Python资料网收获更多编程知识
Python 条件判断:全面掌握 if、else、elif 语句的用法
文章目录
在 Python 编程中,条件判断语句是控制程序执行流程的重要工具。本文将带你深入了解 Python 中的
if
、
if-else
、和
if-elif-else
语句的使用方法,以及如何利用逻辑运算符
and
、
or
和
not
来构建更复杂的条件判断。通过多个代码示例,展示了在不同场景下如何编写条件语句,使得代码更具灵活性和可读性。文章最后提供了一个完整的代码示例,帮助你在实际项目中应用这些条件判断语句。掌握这些知识,将为你编写更加智能和高效的 Python 程序奠定坚实的基础。
一 if 如果
# if 如果
in_trash = True
if in_trash:
print("可以被彻底删除")
in_trash = True
if not in_trash:
print("不可以被彻底删除")
二 if-else 如果否则
# if-else 如果否则
in_trash = True
if in_trash:
print("可以被彻底删除")
else:
print("不可以被彻底删除")
三 判断条件
判断 | 含义 |
---|---|
a == b | a 是否等于 b |
a > b | a 是否大于 b |
a >= b | a 是否大于等于 b |
a < b | a 是否小于 b |
a <= b | a 是否小于等于 b |
a != b | a 是否不等于 b |
四 文字之间的判断
a, b = "文件1", "文件2"
print(a == b)
print("2 < 3", 2 < 3)
print("3 < 2", 3 < 2)
print("2 != 2", 2 != 2)
print()
a, b = 1, 2
if a > b:
print("a 大于 b")
else:
print("a 不大于 b")
五 and or not 判断
判断 | 含义 |
---|---|
True and True | 需要两边同时满足才能返回 True |
True or False | 只要一边是 True 则返回 True |
not True | 给出相反结果 |
print(2 < 3 and 2 < 5)
print(2 > 3 or 3 == 3)
print(2 > 3 or not 3 == 3 and 5 < 10)
六 if -elif - else
today = 4
if today == 1:
print("周一")
elif today == 2:
print("周二")
elif today == 3:
print("周三")
else:
print("周一周二周三之外的一天")
七 完整代码示例
# This is a sample Python script.
# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint.
# if 如果
in_trash = True
if in_trash:
print("可以被彻底删除")
in_trash = True
if not in_trash:
print("不可以被彻底删除")
# if-else 如果否则
in_trash = True
if in_trash:
print("可以被彻底删除")
else:
print("不可以被彻底删除")
# 判断条件
# 判断 含义
# a == b a 是否等于 b
# a > b a 是否大于 b
# a >= b a 是否大于等于 b
# a < b a 是否小于 b
# a <= b a 是否小于等于 b
# a != b a 是否不等于 b
# 文字之间的判断
a, b = "文件1", "文件2"
print(a == b)
print("2 < 3", 2 < 3)
print("3 < 2", 3 < 2)
print("2 != 2", 2 != 2)
print(2 < 3 and 2 < 5)
print(2 > 3 or 3 == 3)
print(2 > 3 or not 3 == 3 and 5 < 10)
# and or not 的含义
#True and True 需要两边同时满足才能返回 True
#True or False 只要一边是 True 则返回 True
#not True 给出相反结果
a, b = 1, 2
if a > b:
print("a 大于 b")
else:
print("a 不大于 b")
# if -elif - else
today = 4
if today == 1:
print("周一")
elif today == 2:
print("周二")
elif today == 3:
print("周三")
else:
print("周一周二周三之外的一天")
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('条件判断')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
复制粘贴并覆盖到你的 main.py 中运行,运行结果如下。
Hi, 条件判断
可以被彻底删除
可以被彻底删除
False
2 < 3 True
3 < 2 False
2 != 2 False
True
True
False
a 不大于 b
周一周二周三之外的一天
八 源码地址
详情见:莫烦 Python
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
相关文章
最新发布
- Python 列表全方位解析:创建、操作、删除与遍历的全面指南
- Python 安卓应用构建教程:使用 Kivy 和 AndroidStudio(一)
- Python中类(class)的使用方法
- 【已解决】com.alibaba.excel.exception.ExcelGenerateException: java.lang.InternalError: java.lang.reflect.
- 097、Python 获取文件夹下的所有文件的方法
- 【Python知行篇】代码的曼妙乐章:探索数据与逻辑的和谐之舞
- 将NC栅格表示时间维度的数据提取出来的方法
- 如何使用 matplotlib 在 Python 3 中绘制数据
- mysql操作线上数据要有备份和恢复
- 在Ubuntu小设备上使用VSCode+SSH开发部署nicegui的Web应用,并设置系统开机自动启动应用
点击排行
- 版本匹配指南:Numpy版本和Python版本的对应关系
- 版本匹配指南:PyTorch版本、torchvision 版本和Python版本的对应关系
- Python 可视化 web 神器:streamlit、Gradio、dash、nicegui;低代码 Python Web 框架:PyWebIO
- 相关性分析——Pearson相关系数+热力图(附data和Python完整代码)
- Python与PyTorch的版本对应
- Anaconda版本和Python版本对应关系(持续更新...)
- Python pyinstaller打包exe最完整教程
- Could not build wheels for llama-cpp-python, which is required to install pyproject.toml-based proj