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:好