Python 条件判断:全面掌握 if、else、elif 语句的用法

Python 条件判断:全面掌握 if、else、elif 语句的用法


在 Python 编程中,条件判断语句是控制程序执行流程的重要工具。本文将带你深入了解 Python 中的 ifif-else、和 if-elif-else 语句的使用方法,以及如何利用逻辑运算符 andornot 来构建更复杂的条件判断。通过多个代码示例,展示了在不同场景下如何编写条件语句,使得代码更具灵活性和可读性。文章最后提供了一个完整的代码示例,帮助你在实际项目中应用这些条件判断语句。掌握这些知识,将为你编写更加智能和高效的 Python 程序奠定坚实的基础。

一 if 如果

    # if 如果
    in_trash = True
    if in_trash:
        print("可以被彻底删除")

    in_trash = True
    if not in_trash:
        print("不可以被彻底删除")

二 if-else 如果否则

   # if-else 如果否则
    in_trash = True
    if in_trash:
        print("可以被彻底删除")
    else:
        print("不可以被彻底删除")

三 判断条件

判断含义
a == ba 是否等于 b
a > ba 是否大于 b
a >= ba 是否大于等于 b
a < ba 是否小于 b
a <= ba 是否小于等于 b
a != ba 是否不等于 b

四 文字之间的判断

    a, b = "文件1", "文件2"
    print(a == b)
    print("2 < 3", 2 < 3)
    print("3 < 2", 3 < 2)
    print("2 != 2", 2 != 2)
    print()
    a, b = 1, 2
    if a > b:
        print("a 大于 b")
    else:
        print("a 不大于 b")

五 and or not 判断

判断含义
True and True需要两边同时满足才能返回 True
True or False只要一边是 True 则返回 True
not True给出相反结果
   print(2 < 3 and 2 < 5)
   print(2 > 3 or 3 == 3)
   print(2 > 3 or not 3 == 3 and 5 < 10)

六 if -elif - else

    today = 4
    if today == 1:
        print("周一")
    elif today == 2:
        print("周二")
    elif today == 3:
        print("周三")
    else:
        print("周一周二周三之外的一天")

七 完整代码示例

# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press ⌘F8 to toggle the breakpoint.

    # if 如果
    in_trash = True
    if in_trash:
        print("可以被彻底删除")

    in_trash = True
    if not in_trash:
        print("不可以被彻底删除")

    # if-else 如果否则
    in_trash = True
    if in_trash:
        print("可以被彻底删除")
    else:
        print("不可以被彻底删除")

    # 判断条件

    # 判断	含义
    # a == b	a 是否等于 b
    # a > b	a 是否大于 b
    # a >= b	a 是否大于等于 b
    # a < b	a 是否小于 b
    # a <= b	a 是否小于等于 b
    # a != b	a 是否不等于 b
    # 文字之间的判断
    a, b = "文件1", "文件2"
    print(a == b)
    print("2 < 3", 2 < 3)
    print("3 < 2", 3 < 2)
    print("2 != 2", 2 != 2)

    print(2 < 3 and 2 < 5)
    print(2 > 3 or 3 == 3)
    print(2 > 3 or not 3 == 3 and 5 < 10)
    # and or not 的含义
    #True and True 需要两边同时满足才能返回 True
    #True or False 只要一边是 True 则返回 True
    #not True 给出相反结果

    a, b = 1, 2
    if a > b:
        print("a 大于 b")
    else:
        print("a 不大于 b")

    # if -elif - else
    today = 4
    if today == 1:
        print("周一")
    elif today == 2:
        print("周二")
    elif today == 3:
        print("周三")
    else:
        print("周一周二周三之外的一天")

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('条件判断')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

复制粘贴并覆盖到你的 main.py 中运行,运行结果如下。

Hi, 条件判断
可以被彻底删除
可以被彻底删除
False
2 < 3 True
3 < 2 False
2 != 2 False
True
True
False
a 不大于 b
周一周二周三之外的一天

八 源码地址

国内看 Gitee条件判断.py

国外看 GitHub条件判断.py

详情见:莫烦 Python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲代码不忘补水

感谢有你,让我的创作更有价值!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值