Python 简明教程
Python - Decision Making
Python 的决策功能存在于其关键字 − if..elif…else 中。 if 关键字需要一个 boolean expression ,后跟冒号 (:) 符号。冒号 (:) 符号开始一个缩进块。如果 if statement 中的布尔表达式为 True ,则执行具有相同缩进级别的语句。如果表达式不是 True(False),则 interpreter 绕过缩进块并继续执行更早缩进级别的语句。
Python’s decision making functionality is in its keywords − if..elif…else. The if keyword requires a boolean expression, followed by colon (:) symbol. The colon (:) symbol starts an indented block. The statements with the same level of indentation are executed if the boolean expression in if statement is True. If the expression is not True (False), the interpreter bypasses the indented block and proceeds to execute statements at earlier indentation level.
决策结构会评估产生 TRUE 或 FALSE 结果的多个表达式。您需要确定在结果为 TRUE 或 FALSE 时要采取什么操作以及要执行哪些语句。
Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise.
以下是大多数编程语言中常见的典型决策结构的一般形式 −
Following is the general form of a typical decision making structure found in most of the programming languages −
Python 编程语言将任何 non-zero 和 non-null 值视为 TRUE,并且如果它为 zero 或 null ,则视为 FALSE 值。
Python programming language assumes any non-zero and non-null values as TRUE, and if it is either zero or null, then it is assumed as FALSE value.
Types of Decision Making Statements in Python
Python 编程语言提供了以下决策语句类型。单击以下链接以查看其详细信息。
Python programming language provides following types of decision making statements. Click the following links to check their detail.
让我们简单地浏览每个决策:
Let us go through each decision making briefly −
Single Statement Suites
如果 if 子句的套件仅由一行组成,则它可能与标题语句位于同一行。
If the suite of an if clause consists only of a single line, it may go on the same line as the header statement.
if…else statement
在此决策语句中,如果 if 条件为 true,则执行此块中的语句,否则,执行 else 块。
In this decision making statement, if the if condition is true, then the statements within this block are executed, otherwise, the else block is executed.
程序将根据 if 语句中的条件为 true 还是 false 选择要执行哪块代码。
The program will choose which block of code to execute based on whether the condition in the if statement is true or false.
Example
以下示例展示了 if 的使用…else 语句。
The following example shows the use of if…else statement.
var = 100
if ( var == 100 ):
print ("Value of var is equal to 100")
else:
print("Value of var is not equal to 100")
在运行以上代码时,它将显示以下输出:
On running the above code, it will show the following output −
Value of var is equal to 100
Nested if statements
嵌套 if 是另一个决策语句,其中一个 if 语句位于另一个内部。它允许我们依次检查多个条件。
A nested if is another decision making statement in which one if statement resides inside another. It allows us to check multiple conditions sequentially.
Example
在此示例中,我们将了解嵌套 if 语句的使用。
In this example, we will see the use of nested-if statement.
var = 100
if ( var == 100 ):
print("The number is equal to 100")
if var % 2 == 0:
print("The number is even")
else:
print("The given number is odd")
elif var == 0:
print("The given number is zero")
else:
print("The given number is negative")
在执行以上代码时,它将显示以下输出:
On executing the above code, it will display the below output −
The number is equal to 100
The number is even