Python中的If,Elif和Else语句

如果Elif Else声明 (If Elif Else Statements)

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 statement )

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 = 5

if x > 4:
  print("The condition was true!") #this statement executes

其他陈述 (else statement)

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 = 3

if y > 4:
  print("I won't print!") #this statement does not execute
else:
  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所有情况

elif声明 (elif statement)

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 = 7

if z > 8:
  print("I won't print!") #this statement does not execute
elif z > 5:
  print("I will!") #this statement will execute
elif z > 6:
  print("I also won't print!") #this statement does not execute
else:
  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 > 6trueif/elif/elseif/elif/else在第一个true条件之后终止。 这意味着else仅在没有条件true的情况下才会执行。

嵌套if语句 (Nested if statements)

We can also create nested if’s for decision making. Before preceding please refer to the href=’https://guide.freecodecamp.org/python/code-blocks-and-indentation’ target=’_blank’ rel=‘nofollow’>indentation guide once before preceding.

我们还可以创建嵌套的if决策。 在开始之前,请先参考href =“ https://guide.freecodecamp.org/python/code-blocks-and-indentation'target ='_ blank'rel ='nofollow'>缩进指南。

Let’s take an example of finding a number which is even and also greater than 10

让我们以发现一个等于甚至大于10的数字为例

python 
x = 34
if 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 boolean comparisons and boolean operators.

尽管上面的示例很简单,但是您可以使用布尔比较布尔运算符创建复杂的条件。

内联python if-else语句 (Inline python if-else statement)

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 = 89
is_greater = True if x >= 50 else False

print(is_greater)

Output

输出量

>
True
>

有关if / elif / else语句的更多信息: (More info on if/elif/else statements:)

翻译自: https://www.freecodecamp.org/news/if-elif-else-statements/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值