python中if及if-else如何使用
作者:silencement 时间:2022-12-06 08:14:15
if 结构
if 结构允许程序做出选择,并根据不同的情况执行不同的操作
基本用法
比较运算符
根据 PEP 8 标准,比较运算符两侧应该各有一个空格,比如:5 == 3。 PEP8 标准
==(相等):如果该运算符两侧的值完全相同则返回 True
!=(不等):与相等相反
print(5 == '5')
print(True == '1')
print(True == 1)
print('Eric'.lower() == 'eric'.lower())
>(大于):左侧大于右侧则输出 True
<(小于):与大于相反
>=(大于等于):左侧大于或者等于右侧则输出 True
<=(小于等于):左侧小于或者等于右侧则输出 True
print(5 > 3)
print(2 > True)
print(True > False)
if的用法
1.只有 if 进行判断
desserts = ['ice cream', 'chocolate', 'apple crisp', 'cookies']
favorite_dessert = 'apple crisp'
hate_dessert = 'chocolate'
for dessert in desserts:
if dessert == favorite_dessert:
print("%s is my favorite dessert!" % dessert.title())
2. if - else 进行判断
for dessert in desserts:
# 比较运算符(== 相等 、!= 不等、> 大于、>= 大于等于、< 小于、<=小于等于)
if dessert == favorite_dessert:
print("%s is my favorite dessert!" % dessert.title())
# elif => else + if 当前值不符合上面 if 的判断条件,执行 elif 的判断条件
else:
print("I like %s." % dessert)
3. if - elif - else 进行判断,其中 elif 不是唯一的,可以根据需要添加,实现更细粒度的判断
# 对不同的 dessert 输出不完全相同的结果
for dessert in desserts:
# 比较运算符(== 相等 、!= 不等、> 大于、>= 大于等于、< 小于、<=小于等于)
if dessert == favorite_dessert:
print("%s is my favorite dessert!" % dessert.title())
# elif => else + if 当前值不符合上面 if 的判断条件,执行 elif 的判断条件
elif dessert == hate_dessert:
print("I hate %s." % dessert)
# 当前值不符合上面所有的判断条件,就执行 else 里的语句
# 当然如果这个else 不需要的话,可以不写
else:
print("I like %s." % dessert)
值得注意的一点是:当整个 if 判断满足某一个判断条件时,就不会再继续判断该判断条件之后的判断
4.特殊的判断条件
if 0: # 其他数字都返回 True
print("True.")
else:
print("False.") # 结果是这个
if '': #其他的字符串,包括空格都返回 True
print("True.")
else:
print("False.") # 结果是这个
if None: # None 是 Python * 殊的对象
print("True.")
else:
print("False.") # 结果是这个
if 1:
print("True.") # 结果是这个
else:
print("False.")
实例扩展:
实例(Python 3.0+)实例一:
# Filename : test.py
# author by : www.runoob.com
# 用户输入数字
num = float(input("输入一个数字: "))
if num > 0:
print("正数")
elif num == 0:
print("零")
else:
print("负数")
实例(Python 3.0+)实例二:
# Filename :test.py
# author by : www.runoob.com
# 内嵌 if 语句
num = float(input("输入一个数字: "))
if num >= 0:
if num == 0:
print("零")
else:
print("正数")
else:
print("负数")
来源:https://www.py.cn/jishu/jichu/10538.html
标签:python,条件语句
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python创建和使用字典实例详解
2021-06-11 11:37:06
selenium+python设置爬虫代理IP的方法
2021-10-01 11:14:33
vue判断input输入内容全是空格的方法
2024-04-09 10:48:01
解决在keras中使用model.save()函数保存模型失败的问题
2021-09-12 17:32:41
Python实现自动添加脚本头信息的示例代码
2022-07-02 18:12:42
python实现一次性封装多条sql语句(begin end)
2022-09-27 13:20:34
Python time库的时间时钟处理
2022-12-18 23:04:23
![](https://img.aspxhome.com/file/2023/8/95908_0s.png)
Go html/template 模板的使用实例详解
2024-04-26 17:17:00
搭建 Selenium+Python开发环境详细步骤
2022-10-12 17:34:02
![](https://img.aspxhome.com/file/2023/0/83750_0s.png)
JScript RuntimeObject检测全局污染
2010-09-13 12:26:00
基于PHP+Ajax实现表单验证的详解
2023-11-14 12:52:43
python实操练习案例(六)
2021-01-06 08:09:23
![](https://img.aspxhome.com/file/2023/3/127843_0s.png)
JS实现css边框样式设置工具
2008-05-25 16:22:00
![](https://img.aspxhome.com/file/UploadPic/20085/25/border-style-tool_82s.gif)
golang gin框架实现大文件的流式上传功能
2024-05-08 10:44:11
Python实现字典排序、按照list中字典的某个key排序的方法示例
2022-06-10 15:21:38
oracle-快速删除重复的记录
2008-01-16 19:12:00
Mysql存储过程学习笔记--建立简单的存储过程
2024-01-23 14:41:21
![](https://img.aspxhome.com/file/2023/0/71120_0s.png)
浅谈PyTorch的可重复性问题(如何使实验结果可复现)
2021-07-16 06:34:33
Python数据分析之使用matplotlib绘制折线图、柱状图和柱线混合图
2023-09-16 23:18:09
![](https://img.aspxhome.com/file/2023/5/93625_0s.png)
谈谈CSS样式表的命名规范
2007-10-08 12:41:00