Python if,else和elif语句

In this tutorial, we shall focus on Python if, else and elif statement.

在本教程中,我们将专注于Python的if,else和elif语句。

Decisions are one of the most important feature of any computer program. It basically helps to systematically and logically execute the program based upon the input of the user (sometimes) or other factors which lets the user receive the desired output depending upon the input.

决策是任何计算机程序中最重要的功能之一。 它基本上有助于基于用户的输入(有时)或其他因素来系统地和逻辑地执行程序,这使用户可以根据输入来接收所需的输出。

Decision-making can be provided by making use of branching structures and loops. These Branching structures helps in conditional programming. In this tutorial you will learn about branching structures.

可以通过使用分支结构和循环来提供决策。 这些分支结构有助于条件编程。 在本教程中,您将学习分支结构。

Python if语句 (Python if Statement)

An if statement or an if block as it is commonly said, is useful to make decisions based on conditions in a program sometimes based on user input data.

如通常所说的if语句或if块,对于基于程序中的条件(有时基于用户输入数据)进行决策很有用。

An if block needs the keyword ‘if’ with a following condition and a set of statements to be executed in case the condition turns out to be True. A condition is mostly an expression which can be evaluated either to true or false.

一个if块需要带有以下条件的关键字“ if”,并在条件变为True的情况下执行一组语句。 条件主要是可以被评估为true或false的表达式。

Syntax

句法

1
2
3
4
if Condition:
statement 1
statement 2
statement n
1
2
3
4
if Condition :
statement 1
statement 2
statement n

Example

1
2
3
a=10
if a>8:
print("A is Greater than 8")
1
2
3
a = 10
if a > 8 :
print ( "A is Greater than 8" )

Output

输出量

A is Greater than 8

A大于8

Here, a variable ‘a’ is initialized to 10. Then, an if block is used with the condition that checks whether a is greater than 8 or not. If it evaluates to be true, then the set of statements following that condition will be executed and then the program will come out of that particular block and execute the next set of statements.

在这里,变量“ a”被初始化为10。然后,使用if块,条件是检查a是否大于8。 如果评估结果为true,则将执行该条件之后的语句集,然后程序将从该特定块中出来并执行下一组语句。

Note: We generally use indentations to make the program look more readable that follows a much better logical approach. Here, it is important to use indentations as it helps to construct the if block in a logical manner.

注意:我们通常使用缩进使程序看起来更具可读性,并且遵循更好的逻辑方法。 在此,使用缩进非常重要,因为它有助于以逻辑方式构造if块。

Python if else语句 (Python if else Statement)

The if-else condition is useful when you have multiple conditions to be evaluated in a program. Suppose you want to check for a particular condition and if that evaluates to false, you can then opt for another condition checking to evaluate it. This helps to produce a better output based on verification.

如果在程序中要评估多个条件,则if-else条件很有用。 假设您要检查特定条件,如果该条件的值为假,则可以选择另一种条件检查以对其求值。 这有助于基于验证产生更好的输出。

In this type of structure, the program will execute at least one of the blocks; either if block or else block.

在这种类型的结构中,程序将执行至少一个块。 如果阻塞或否则阻塞。

Note: You must use proper indentations in if-else structure and if you dont do so, you will get an error as the Python Interpreter wont be able to understand the difference between the if block and else block.

注意:您必须在if-else结构中使用适当的缩进,如果不这样做,则会出现错误,因为Python解释器将无法理解if块和else块之间的区别。

Syntax

句法

1
2
3
4
5
6
7
8
if Condition:
statement 1
statement 2
statement n
else:
statement 1
statement 2
statement n
1
2
3
4
5
6
7
8
if Condition :
statement 1
statement 2
statement n
else :
statement 1
statement 2
statement n

Example

1
2
3
4
5
a=10
if a<8:
print("A is Less than 8")
else:
print("A is Greater than 8")
1
2
3
4
5
a = 10
if a < 8 :
print ( "A is Less than 8" )
else :
print ( "A is Greater than 8" )

Output

输出量

A is Greater than 8

A大于8

Here, first the variable a is initialized to 10. In the if block, the condition is checked whether it is less than 8 and it is evaluated to be false. The Python Interpreter now has to go to the else block so as to execute the else condition statements which is mandatory. So, this is better than a single if block as this provides a better informed output to the user.

在此,首先将变量a初始化为10。在if块中,检查条件是否小于8,并将其评估为false。 Python解释器现在必须转到else块,以便执行必需的else条件语句。 因此,这比单个if块更好,因为这可以为用户提供更好的信息输出。

Python if elif else语句 (Python if elif else Statement)

Suppose, you need to check for multiple conditions, you cant do it with a single if Block or an if-else Block. The if-elif-else block comes in use when you want to comapare a variable or any object for that matter to multiple other values. You can add as many elif conditions you want. elif stands for else-if. Here, else block is optional.

假设您需要检查多个条件,则无法使用单个if块或if-else块来做到这一点。 当您想将变量或与此相关的任何对象映射为多个其他值时,将使用if-elif-else块。 您可以添加任意多个elif条件。 elif代表else-if。 在这里,else块是可选的。

Note: You must have a single condition that must evaluate to true otherwise it will go to the else Block. Whenever a condition evaluates to be true, the interpreter will execute that particular block and will then exit out of the if-elif-else structure and wont even check for any other succeeding conditions.

注意:您必须具有一个条件,该条件的值必须为true,否则它将进入else块。 每当条件评估为真时,解释器将执行该特定块,然后退出if-elif-else结构,甚至不检查其他任何后续条件。

Syntax

句法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if Condition 1:
statement 1
statement 2
statement n
elif Condition 2:
statement 1
statement 2
statement n
elif Condition 3:
statement 1
statement 2
statement n
else:
statement 1
statement 2
statement n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if Condition 1 :
statement 1
statement 2
statement n
elif Condition 2 :
statement 1
statement 2
statement n
elif Condition 3 :
statement 1
statement 2
statement n
else :
statement 1
statement 2
statement n

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
a=4
if a==1:
print("A is 1")
elif a==2:
print("A is 2")
elif a==3:
print("A is 3")
elif a==4:
print("A is 4")
elif a==5:
print("A is 5")
else:
print("A is not between 1 and 5")
1
2
3
4
5
6
7
8
9
10
11
12
13
a = 4
if a == 1 :
print ( "A is 1" )
elif a == 2 :
print ( "A is 2" )
elif a == 3 :
print ( "A is 3" )
elif a == 4 :
print ( "A is 4" )
elif a == 5 :
print ( "A is 5" )
else :
print ( "A is not between 1 and 5" )

Output

输出量

A is 4

A是4

Here, we have used comparison operator == to compare values of the variable a in the condition. Suppose the first condition evaluated to be true, then the control will execute that particular block and will go directly out of the if-elif-else Structure.

在这里,我们使用了比较运算符==来比较条件中变量a的值。 假设第一个条件评估为true,则控件将执行该特定块,并将直接退出if-elif-else结构。

If you found any mistake in above Python if, else and elif statement tutorial then mention it by commenting below.

如果您在上述Python if,else和elif语句教程中发现任何错误,请通过在下面的注释中提及它。

翻译自: https://www.thecrazyprogrammer.com/2015/08/python-if-else-and-elif-statement.html