if-elif-else语句
一般格式:
age = 12
if age<20 :
#句子
elif age<15:
#句子
else:
#句子
if可确认列表是否为空
requested = []
if requested:
for requested in requested_toppings:
print("Adding " + requested + ".")
print("\nFinished making your pizza!")
else:
print("Are you sure you want a plain pizza?")
Are you sure you want a plain pizza?
关键字
- and和or
- in:判断特定的值是否已包含在列表中
- not in
>>> requested_toppings = ['mushrooms', 'onions', 'pineapple']
>>> 'mushrooms' in requested_toppings
True
>>> 'pepperoni' in requested_toppings
False
- 布尔表达式,要么为True,要么为False,布尔值通常用于记录条件,如游戏是否正在运行,或用户是否可以编辑网站的特定内容:
game_active = True
can_edit = False
代码规范
- 在诸如==、 >=和<=等比较运算符两边各添加一个空格,例如, if age < 4:要比if age<4:好
本文详细介绍了Python中的if-elif-else语句的使用方法,包括如何使用这些语句来检查数值条件、列表是否为空,以及如何利用and、or、in和not in关键字进行复杂的条件判断。同时,文章通过实例演示了如何根据列表的内容执行不同的操作。
2948

被折叠的 条评论
为什么被折叠?



