博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python中的If,Elif和Else语句
阅读量:2518 次
发布时间:2019-05-11

本文共 3377 字,大约阅读时间需要 11 分钟。

如果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 = 5if 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 = 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所有情况

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

尽管上面的示例很简单,但是您可以使用和创建复杂的条件。

内联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 = 89is_greater = True if x >= 50 else Falseprint(is_greater)

Output

输出量

>True>

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

翻译自:

转载地址:http://tmrwd.baihongyu.com/

你可能感兴趣的文章
os模块
查看>>
LINQ to SQL vs. NHibernate
查看>>
基于Angular5和WebAPI的增删改查(一)
查看>>
windows 10 & Office 2016 安装
查看>>
最短路径(SP)问题相关算法与模板
查看>>
js算法之最常用的排序
查看>>
Python——交互式图形编程
查看>>
经典排序——希尔排序
查看>>
团队编程项目作业2-团队编程项目代码设计规范
查看>>
英特尔公司将停止910GL、915GL和915PL芯片组的生产
查看>>
Maven配置
查看>>
HttpServletRequest /HttpServletResponse
查看>>
SAM4E单片机之旅——24、使用DSP库求向量数量积
查看>>
从远程库克隆库
查看>>
codeforces Unusual Product
查看>>
hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
查看>>
正则表达式的搜索和替换
查看>>
个人项目:WC
查看>>
地鼠的困境SSL1333 最大匹配
查看>>
flume+elasticsearch+kibana遇到的坑
查看>>