栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python 的 if语句

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python 的 if语句

if 语句的核心是一个值为 True 或 False 的表达式,Python根据测试值为 True 或 False 来决定是否执行代码

格式          if condition :

                        do something

stu = 'sd'
print(stu == 'fd')
print(stu != 'fd')
stu = 18
print(stu == 4)
print(stu != 4)
print(stu >= 4)
print(stu <= 4)
print(stu != 4 and stu > 4)     # and 逻辑与
print(stu != 4 or stu > 4)      # or  逻辑或
stu = {'rew','fey',43,2}
s1 = 'fey'
if s1 in stu:                   # s1 在 stu 中输出
    print("s1 为 ",s1)
s1 = 'fwe'
if s1 not in stu:               # s1 不在 stu 中输出
    print("s1 为 ",s1)
False
True
False
True
True
False
True
True
s1 为  fey
s1 为  fwe

格式(1)    if condition:                    (2)   if condition:

                        do something                        do something

                else:                                        elif  condition:

                        do something                        do something

                                                                else:

                                                                       do something

stu = {'rew','fey',43,2}
s1 = 'fey'
if s1 in stu:                   # s1 在 stu 中输出
    print("s1 在 stu")
else:                # s1 不在 stu 中输出
    print("s1 不在 stu")

s1 = 5
if s1 not in stu:
    print("s1 不在 stu")
elif s1 in stu:
    print("s1 在 stu")
else:
    print("其他")
s1 在 stu
s1 不在 stu
fruit = {'apple','banana','orange','pear'}
foods = {'rice','beef','pork','pear'}
for val in foods:
    if val in fruit:
        print(val.title())
    else:
        print("foods not fruit")
foods not fruit
foods not fruit
foods not fruit
Pear

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/273232.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号