# Python `if...else`语句 > 原文: [https://www.programiz.com/python-programming/if-elif-else](https://www.programiz.com/python-programming/if-elif-else) #### 在本文中,您将学习使用不同形式的`if..else`语句在 Python 程序中创建决策。 ## Python 中的`if...else`语句是什么? 仅当满足特定条件时,我们才想执行代码时才需要决策。 `if…elif…else`语句在 Python 中用于决策。 ### Python `if`语句语法 ```py if test expression: statement(s) ``` 在此,程序将求值`test expression`并仅在测试表达式为`True`时才执行语句。 如果测试表达式为`False`,则不执行该语句。 在 Python 中,`if`语句的主体由缩进指示。 主体以缩进开始,第一条未缩进的线标记结束。 Python 将非零值解释为`True`。`None`和`0`解释为`False`。 ### Python `if`语句流程图 ![Flowchart of if statement in Python programming](img/d24ba938b4eb482eb7883046955ce095.png "If Statement Flowchart") Python 编程中`if`语句的流程图 ### 示例:Python `if`语句 ```py # If the number is positive, we print an appropriate message num = 3 if num > 0: print(num, "is a positive number.") print("This is always printed.") num = -1 if num > 0: print(num, "is a positive number.") print("This is also always printed.") ``` 运行该程序时,输出为: ```py 3 is a positive number This is always printed This is also always printed. ``` 在上面的示例中,`num > 0`是测试表达式。 仅当`if`的主体的值为`True`时,才执行它。 当变量`num`等于 3 时,测试表达式为`true`,并且执行`if`主体内的语句。 如果变量`num`等于 -1,则测试表达式为`false`,并且跳过`if`主体内部的语句。 `print()`语句位于`if`块之外(未缩进)。 因此,无论测试表达式如何,都将执行它。 * * * ## Python `if...else`语句 ### `if...else`的语法 ```py if test expression: Body of if else: Body of else ``` `if..else`语句求值`test expression`并仅在测试条件为`True`时才执行`if`的主体。 如果条件为`False`,则执行`else`的主体。 缩进用于分隔块。 ### Python `if..else`流程图 ![Flowchart of if...else statement in Python Programming](img/72f33199a45a63e4f951216b0f88815a.png "if...else statement flowchart") Python 中`if...else`语句的流程图 ### `if...else`的示例 ```py # Program checks if the number is positive or negative # And displays an appropriate message num = 3 # Try these two variations as well. # num = -5 # num = 0 if num >= 0: print("Positive or Zero") else: print("Negative number") ``` **输出** ```py Positive or Zero ``` 在上面的示例中,当`num`等于 3 时,测试表达式为`true`,并且执行`if`的主体,其他的`body`被跳过。 如果`num`等于 -5,则测试表达式为`false`,并且执行`else`的主体,并且跳过`if`的主体。 如果`num`等于 0,则测试表达式为`true`,并且执行`if`的主体,而跳过其他的`body`。 * * * ## Python `if...elif...else`语句 ### `if...elif...else`的语法 ```py if test expression: Body of if elif test expression: Body of elif else: Body of else ``` `elif`是`if`的缩写。 它允许我们检查多个表达式。 如果`if`的条件为`False`,它将检查下一个`elif`块的条件,依此类推。 如果所有条件均为`False`,则执行`else`的主体。 根据条件,仅执行几个`if...elif...else`块中的一个块。 `if`块只能有一个`else`块。 但是它可以具有多个`elif`块。 ### `if...elif...else`的流程图 ![Flowchart of if...elif....else in Python programming](img/ea825cb1d7a6379cd10db87eadcb48a0.png "if...elif...else statement flowchart") Python 中`if...elif....else`语句的流程图 ### `if...elif...else`的示例 ```py '''In this program, we check if the number is positive or negative or zero and display an appropriate message''' num = 3.4 # Try these two variations as well: # num = 0 # num = -4.5 if num > 0: print("Positive number") elif num == 0: print("Zero") else: print("Negative number") ``` 当变量`num`为正时,将打印正数。 如果`num`等于 0,则打印零。 如果`number`为负,则打印负数。 * * * ## Python 嵌套`if`语句 我们可以在另一个`if...elif...else`语句中包含一个`if...elif...else`语句。 这在计算机编程中称为嵌套。 这些语句中的任何数目都可以彼此嵌套。 缩进是弄清楚嵌套级别的唯一方法。 它们可能会造成混淆,因此除非有必要,否则必须避免使用它们。 ### Python 嵌套`if`示例 ```py '''In this program, we input a number check if the number is positive or negative or zero and display an appropriate message This time we use nested if statement''' num = float(input("Enter a number: ")) if num >= 0: if num == 0: print("Zero") else: print("Positive number") else: print("Negative number") ``` **输出 1** ```py Enter a number: 5 Positive number ``` **输出 2** ```py Enter a number: -1 Negative number ``` **输出 3** ```py Enter a number: 0 Zero ```