ITPub博客

首页 > IT职业 > IT生活 > python if判断在哪写

python if判断在哪写

IT生活 作者:mac_xuyi 时间:2021-09-11 10:19:51 0 删除 编辑

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 = ['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())

更多技术请关注Python视频教程。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4273/viewspace-2834174/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    75
  • 访问量
    496