首页 > Python资料 博客日记

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

2024-10-04 03:00:04Python资料围观25

这篇文章介绍了Python 条件判断:全面掌握 if、else、elif 语句的用法,分享给大家做个参考,收藏Python资料网收获更多编程知识

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


版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐