本文共 3377 字,大约阅读时间需要 11 分钟。
The if
/elif
/else
structure is a common way to control the flow of a program, allowing you to execute specific blocks of code depending on the value of some data.
if
/ elif
/ else
结构是控制程序流程的常用方法,它使您可以根据某些数据的值执行特定的代码块。
If the condition following the keyword if
evaluates as true
, the block of code will execute. Note that parentheses are not used before and after the condition check as in other languages.
如果关键字if
之后的条件评估为true
,则代码块将执行。 请注意,在条件检查之前和之后不会像其他语言一样使用括号。
if True: print('If block will execute!')
x = 5if x > 4: print("The condition was true!") #this statement executes
You can optionally add an else
response that will execute if the condition is false
:
您可以选择添加一个else
响应,如果条件为false
该响应将执行:
if not True: print('If statement will execute!')else: print('Else statement will execute!')
Or you can also see this example:
或者您也可以看到以下示例:
y = 3if y > 4: print("I won't print!") #this statement does not executeelse: print("The condition wasn't true!") #this statement executes
Note that there is no condition following the else
keyword - it catches all situations where the condition was false
请注意, else
关键字后没有条件-它捕获条件为false
所有情况
Multiple conditions can be checked by including one or more elif
checks after your initial if
statement. Just keep in mind that only one condition will execute:
可以通过在初始if
语句之后包含一个或多个elif
检查来检查多个条件。 请记住,只有一种情况会执行:
z = 7if z > 8: print("I won't print!") #this statement does not executeelif z > 5: print("I will!") #this statement will executeelif z > 6: print("I also won't print!") #this statement does not executeelse: print("Neither will I!") #this statement does not execute
Note: only the first condition that evaluates as true
will execute. Even though z > 6
is true
, the if/elif/else
block terminates after the first true condition. This means that an else
will only execute if none of the conditions were true
.
注意:只有第一个条件为true
条件才会执行。 即使z > 6
为true
, if/elif/else
块if/elif/else
在第一个true条件之后终止。 这意味着else
仅在没有条件true
的情况下才会执行。
We can also create nested if’s for decision making. Before preceding please refer to the href=’’ target=’_blank’ rel=‘nofollow’>indentation guide once before preceding.
我们还可以创建嵌套的if决策。 在开始之前,请先参考href =“ ='_ blank'rel ='nofollow'>缩进指南。
Let’s take an example of finding a number which is even and also greater than 10
让我们以发现一个等于甚至大于10的数字为例
python x = 34if x % 2 == 0: # this is how you create a comment and now, checking for even. if x > 10: print("This number is even and is greater than 10") else: print("This number is even, but not greater 10")else: print ("The number is not even. So point checking further.")
This was just a simple example for nested if’s. Please feel free to explore more online.
这只是嵌套if的一个简单示例。 请随时在线探索更多。
While the examples above are simple, you can create complex conditions using and .
尽管上面的示例很简单,但是您可以使用和创建复杂的条件。
We can also use if-else statements inline python functions. The following example should check if the number is greater or equal than 50, if yes return True:
我们还可以使用if-else语句内联python函数。 以下示例应检查该数字是否大于或等于50,如果是,则返回True:
python x = 89is_greater = True if x >= 50 else Falseprint(is_greater)
Output
输出量
>True>
翻译自:
转载地址:http://tmrwd.baihongyu.com/