
if 结构允许程序选择,并根据不同的情况执行不同的操作
基本用法
比较运算符
根据 PEP 8 标准,比较操作符两侧应有一个空间,如:5 == 3。 PEP8 标准 ==(相等):如果运算符两侧的值完全相同,则返回
回 True !=(不等):与相等相反
推荐:Python教程
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的用法
if 进行判断
desserts=['icecream','chocolate','applecrisp','cookies']
favorite_dessert='applecrisp'
hate_dessert='chocolate'
fordessertindesserts:
ifdessert==favorite_dessert:
print("%sismyfavoritedessert!"%dessert.title())更多技术请关注Python视频教程。