美文网首页
Python系列6-Python分支结构if语句

Python系列6-Python分支结构if语句

作者: 只是甲 | 来源:发表于2021-03-23 13:56 被阅读0次

    一.if 语句介绍

    编程时经常需要检查一系列条件,并据此决定采取什么措施。在Python中,if 语句让你能够检查程序的当前状态,并据此采取相应的措施。

    if 语句语法:

    if <条件判断1>:
        <执行1>
    elif <条件判断2>:
        <执行2>
    elif <条件判断3>:
        <执行3>
    else:
        <执行4>
    

    1.1 简单的if语句

    最简单的if,一个测试和一个操作。如果测试结果为true,则执行操作

    语法:

    if conditional_test:
        do something
    

    此时有一个场景,判断登陆用户是否是普通用户。

    代码:

    user_status = 'Normal';
    if  user_status == 'Normal':
        print("Welcom " + user_status + " users;")
    

    测试记录:

    >>> user_status = 'Normal';
    >>> if  user_status == 'Normal':
    ...     print("Welcom " + user_status + " users;")
    ...
    Welcom Normal users;
    >>>
    

    1.2 if-else语句

    经常需要在条件测试通过了时执行一个操作,并在没有通过时执行另一个操作;在这种情况下,可使用Python提供的if-else 语句。if-else 语句块类似于简单的if 语句,但其中的else 语句让你能够指定条件测试未通过时要执行的操作。

    语法:

    if conditional_test:
        do something
    else:
        do another thing
    

    此时有一个场景,判断登陆用户是否是普通用户,如果不是,给一个输出
    代码:

    user_status = 'Normal';
    if  user_status == 'Normal':
        print("Welcom " + user_status + " users;")
    else:
        print("You are not a Normal users;")
    
    user_status = 'N/A';
    if  user_status == 'Normal':
        print("Welcom " + user_status + " users;")
    else:
        print("You are not a Normal users;")
    

    测试记录:

    >>> user_status = 'Normal';
    >>> if  user_status == 'Normal':
    ...     print("Welcom " + user_status + " users;")
    ... else:
    ...     print("You are not a Normal users;")
    ...
    Welcom Normal users;
    >>>
    >>> user_status = 'N/A';
    >>> if  user_status == 'Normal':
    ...     print("Welcom " + user_status + " users;")
    ... else:
    ...     print("You are not a Normal users;")
    ...
    You are not a Normal users;
    >>>
    

    1.3 if-elif-else 结构

    经常需要检查超过两个的情形,为此可使用Python提供的if-elif-else 结构。Python只执行if-elif-else 结构中的一个代码块,它依次检查每个条件测试,直到遇到通过
    了的条件测试。测试通过后,Python将执行紧跟在它后面的代码,并跳过余下的测试。

    语法:

    if conditional_test1:
        do something1
    elif conditional_test2:
        do something2
    else:
        do another thing
    

    此时有一个场景,判断登陆用户是否是普通用户或者VIP,如果都不是给一个其它输出
    代码:

    user_status = 'Normal';
    if  user_status == 'Normal':
        print("Welcom " + user_status + " users;")
    elif user_status == 'VIP':
        print("Welcom respect " + user_status + " users;")
    else:
        print("You are not my users;")
    
    user_status = 'N/A';
    if  user_status == 'Normal':
        print("Welcom " + user_status + " users;")
    elif user_status == 'VIP':
        print("Welcom respect " + user_status + " users;")
    else:
        print("You are not my users;")
    
    user_status = 'VIP';
    if  user_status == 'Normal':
        print("Welcom " + user_status + " users;")
    elif user_status == 'VIP':
        print("Welcom respect " + user_status + " users;")
    else:
        print("You are not my users;")
    

    测试记录:

    >>> user_status = 'Normal';
    >>> if  user_status == 'Normal':
    ...     print("Welcom " + user_status + " users;")
    ... elif user_status == 'VIP':
    ...     print("Welcom respect " + user_status + " users;")
    ... else:
    ...     print("You are not my users;")
    ...
    Welcom Normal users;
    >>> user_status = 'N/A';
    >>> if  user_status == 'Normal':
    ...     print("Welcom " + user_status + " users;")
    ... elif user_status == 'VIP':
    ...     print("Welcom respect " + user_status + " users;")
    ... else:
    ...     print("You are not my users;")
    ...
    You are not my users;
    >>>
    >>>
    >>> user_status = 'VIP';
    >>> if  user_status == 'Normal':
    ...     print("Welcom " + user_status + " users;")
    ... elif user_status == 'VIP':
    ...     print("Welcom respect " + user_status + " users;")
    ... else:
    ...     print("You are not my users;")
    ...
    Welcom respect VIP users;
    >>>
    

    1.4 使用多个elif 代码块

    可根据需要使用任意数量的elif 代码块

    语法:

    if conditional_test1:
        do something1
    elif conditional_test2:
        do something2
    elif conditional_test3:
        do something3
    else:
        do another thing
    

    此时有一个场景,判断登陆用户是否是普通用户、VIP或者SVIP,如果都不是给一个其它输出
    代码:

    user_status = 'Normal';
    if  user_status == 'Normal':
        print("Welcom " + user_status + " users;")
    elif user_status == 'VIP':
        print("Welcom respect " + user_status + " users;")
    elif user_status == 'SVIP':
        print("Welcom respect " + user_status + " users,you are my father;")
    else:
        print("You are not my users;")
    
    user_status = 'N/A';
    if  user_status == 'Normal':
        print("Welcom " + user_status + " users;")
    elif user_status == 'VIP':
        print("Welcom respect " + user_status + " users;")
    elif user_status == 'SVIP':
        print("Welcom respect " + user_status + " users,you are my father;")
    else:
        print("You are not my users;")
    
    user_status = 'VIP';
    if  user_status == 'Normal':
        print("Welcom " + user_status + " users;")
    elif user_status == 'VIP':
        print("Welcom respect " + user_status + " users;")
    elif user_status == 'SVIP':
        print("Welcom respect " + user_status + " users,you are my father;")
    else:
        print("You are not my users;")
    
    user_status = 'SVIP';
    if  user_status == 'Normal':
        print("Welcom " + user_status + " users;")
    elif user_status == 'VIP':
        print("Welcom respect " + user_status + " users;")
    elif user_status == 'SVIP':
        print("Welcom respect " + user_status + " users,you are my father;")
    else:
        print("You are not my users;")
    

    测试记录:

    >>> user_status = 'Normal';
    >>> if  user_status == 'Normal':
    ...     print("Welcom " + user_status + " users;")
    ... elif user_status == 'VIP':
    ...     print("Welcom respect " + user_status + " users;")
    ... elif user_status == 'SVIP':
    ...     print("Welcom respect " + user_status + " users,you are my father;")
    ... else:
    ...     print("You are not my users;")
    ...
    Welcom Normal users;
    >>> user_status = 'N/A';
    >>> if  user_status == 'Normal':
    ...     print("Welcom " + user_status + " users;")
    ... elif user_status == 'VIP':
    ...     print("Welcom respect " + user_status + " users;")
    ... elif user_status == 'SVIP':
    ...     print("Welcom respect " + user_status + " users,you are my father;")
    ... else:
    ...     print("You are not my users;")
    ...
    You are not my users;
    >>> user_status = 'VIP';
    >>> if  user_status == 'Normal':
    ...     print("Welcom " + user_status + " users;")
    ... elif user_status == 'VIP':
    ...     print("Welcom respect " + user_status + " users;")
    ... elif user_status == 'SVIP':
    ...     print("Welcom respect " + user_status + " users,you are my father;")
    ... else:
    ...     print("You are not my users;")
    ...
    Welcom respect VIP users;
    >>> user_status = 'SVIP';
    >>> if  user_status == 'Normal':
    ...     print("Welcom " + user_status + " users;")
    ... elif user_status == 'VIP':
    ...     print("Welcom respect " + user_status + " users;")
    ... elif user_status == 'SVIP':
    ...     print("Welcom respect " + user_status + " users,you are my father;")
    ... else:
    ...     print("You are not my users;")
    ...
    Welcom respect SVIP users,you are my father;
    >>>
    >>>
    

    1.5 省略else模块

    Python并不要求if-elif 结构后面必须有else 代码块。
    语法:

    if conditional_test1:
        do something1
    elif conditional_test2:
        do something2
    elif conditional_test3:
        do something3
    

    此时有一个场景,判断登陆用户是否是普通用户、VIP或者SVIP,如果都不是不给出输出

    代码:

    user_status = 'N/A';
    if  user_status == 'Normal':
        print("Welcom " + user_status + " users;")
    elif user_status == 'VIP':
        print("Welcom respect " + user_status + " users;")
    elif user_status == 'SVIP':
        print("Welcom respect " + user_status + " users,you are my father;")
    
    

    测试记录:

    >>> user_status = 'N/A';
    >>> if  user_status == 'Normal':
    ...     print("Welcom " + user_status + " users;")
    ... elif user_status == 'VIP':
    ...     print("Welcom respect " + user_status + " users;")
    ... elif user_status == 'SVIP':
    ...     print("Welcom respect " + user_status + " users,you are my father;")
    ...
    >>>
    >>>
    

    1.6 测试多个条件

    if-elif-else 结构功能强大,但仅适合用于只有一个条件满足的情况:遇到通过了的测试后,Python就跳过余下的测试。这种行为很好,效率很高,让你能够测试一个特定的条件。

    然而,有时候必须检查你关心的所有条件。在这种情况下,应使用一系列不包含elif 和else 代码块的简单if 语句。在可能有多个条件为True ,且你需要在每个条件为True时都采取相应措施时,适合使用这种方法。

    语法:

    if conditional_test1:
        do something1
    
    
    if conditional_test2:
        do something2
    
    
    if conditional_test3:
        do something3
    
    

    此时有一个场景,判断登陆用户是否是普通用户、VIP或者SVIP,并给出相应输出

    代码:

    user_status = 'SVIP';
    if  user_status == 'Normal':
        print("Welcom " + user_status + " users;")
    
    
    if user_status == 'VIP':
        print("Welcom respect " + user_status + " users;")
    
        
    if user_status == 'SVIP':
        print("Welcom respect " + user_status + " users,you are my father;")
    
    
    

    测试记录:

    >>> user_status = 'SVIP';
    >>> if  user_status == 'Normal':
    ...     print("Welcom " + user_status + " users;")
    ...
    >>>
    >>> if user_status == 'VIP':
    ...     print("Welcom respect " + user_status + " users;")
    ...
    >>>
    ... if user_status == 'SVIP':
    ...     print("Welcom respect " + user_status + " users,you are my father;")
    ...
    Welcom respect SVIP users,you are my father;
    >>>
    >>>
    >>>
    

    二.使用if 语句处理列表

    通过结合使用if 语句和列表,可完成一些有趣的任务:对列表中特定的值做特殊处理;高效地管理不断变化的情形。

    2.1 检查特殊元素

    现在有一个场景,有一个列表是当前市面上主流的数据库,公司对数据库进行技术选型,如果员工选择MySQL,需要输出 "MySQL is the most popular Databases"

    代码:

    dbs = ['Oracle','MySQL','SQL Server','Postgresql','Redis']
    
    for db in dbs:
        if db == 'MySQL':
            print("MySQL is the most popular Databases")
    

    测试记录:

    >>> dbs = ['Oracle','MySQL','SQL Server','Postgresql','Redis']
    >>> for db in dbs:
    ...     if db == 'MySQL':
    ...         print("MySQL is the most popular Databases")
    ...
    MySQL is the most popular Databases
    >>>
    

    2.2 确定列表不是空的

    到目前为止,对于处理的每个列表都做了一个简单的假设,即假设它们都至少包含一个元素。我们马上就要让用户来提供存储在列表中的信息,因此不能再假设循环运行时列表不是空的。有鉴于此,在运行for 循环前确定列表是否为空很重要。

    代码:

    dbs = []
    
    if dbs:
        for db in dbs:
            if db == 'MySQL':
                print("MySQL is the most popular Databases")
    else:
        print("The dbs is empty!")
    

    测试记录:

    >>> dbs = []
    >>>
    >>> if dbs:
    ...     for db in dbs:
    ...         if db == 'MySQL':
    ...             print("MySQL is the most popular Databases")
    ... else:
    ...     print("The dbs is empty!")
    ...
    The dbs is empty!
    >>>
    

    2.3 使用多个列表

    现在有一个场景,有一个列表是当前市面上主流的数据库,公司目前已在使用几种数据库,需要区分开公司使用的数据库和未使用的数据库。

    代码:

    dbs = ['Oracle','MySQL','SQL Server','Postgresql','Redis']
    use_dbs = ['Oracle','MySQL','Redis']
    
    if dbs:
        for db in dbs:
            if db in use_dbs:
                print("We have used "+ db + ";")
            else:
                print("We have not use "+ db + ";")
    else:
        print("The dbs is empty!")
    

    测试记录:

    >>> dbs = ['Oracle','MySQL','SQL Server','Postgresql','Redis']
    >>> use_dbs = ['Oracle','MySQL','Redis']
    >>>
    >>> if dbs:
    ...     for db in dbs:
    ...         if db in use_dbs:
    ...             print("We have used "+ db + ";")
    ...         else:
    ...             print("We have not use "+ db + ";")
    ... else:
    ...     print("The dbs is empty!")
    ...
    We have used Oracle;
    We have used MySQL;
    We have not use SQL Server;
    We have not use Postgresql;
    We have used Redis;
    >>>
    

    参考:

    1.Python编程:从入门到实践

    相关文章

      网友评论

          本文标题:Python系列6-Python分支结构if语句

          本文链接:https://www.haomeiwen.com/subject/cxmkfltx.html