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 = 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 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 = 7if 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 Falseprint(is_greater)

Output

输出量

>
True
>

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

  • How to get out of if/else hell

    如何摆脱if / else地狱

  • If/else in JavaScript

    JavaScript中的if / else

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390783.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Hadoop安装及配置

Hadoop的三种运行模式 单机模式(Standalone,独立或本地模式):安装简单,运行时只启动单个进程,仅调试用途;伪分布模式(Pseudo-Distributed):在单节点上同时启动namenode、datanode、secondarynamenode、resourcemanage…

漏洞发布平台-安百科技

一个不错的漏洞发布平台:https://vul.anbai.com/ 转载于:https://blog.51cto.com/antivirusjo/2093758

Android 微信分享图片

private String APP_ID "00000000000000000"; //微信 APPID private IWXAPI iwxapi; private void regToWx() {iwxapi WXAPIFactory.createWXAPI(context, APP_ID, true);//这里context记得初始化iwxapi.registerApp(APP_ID); } IMServer.getDiskBitmap(IMServer.u…

蒙蒂霍尔问题_常见的逻辑难题–骑士和刀,蒙蒂·霍尔和就餐哲学家的问题解释...

蒙蒂霍尔问题While not strictly related to programming, logic puzzles are a good warm up to your next coding session. You may encounter a logic puzzle in your next technical interview as a way to judge your problem solving skills, so its worth being prepare…

西格尔零点猜想_我从埃里克·西格尔学到的东西

西格尔零点猜想I finished reading Eric Siegel’s Predictive Analytics. And I have to say it was an awesome read. How do I define an awesome or great book? A book that changes your attitude permanently. You must not be the same person that you were before y…

C/C++实现删除字符串的首尾空格

StdStringTrimTest.cpp #include <iostream> int main() {std::string str(" 字符串 String ");std::cout << str << std::endl;std::cout << str.size() << std::endl;str.erase(str.find_first_of( ), str.find_first_not_of…

assign复制对象_JavaScript标准对象:assign,values,hasOwnProperty和getOwnPropertyNames方法介绍...

assign复制对象In JavaScript, the Object data type is used to store key value pairs, and like the Array data type, contain many useful methods. These are some useful methods youll use while working with objects.在JavaScript中&#xff0c; Object数据类型用于存…

HDFS 技术

HDFS定义 Hadoop Distributed File System&#xff0c;是一个使用 Java 实现的、分布式的、可横向扩展的文件系 统&#xff0c;是 HADOOP 的核心组件 HDFS特点 处理超大文件流式地访问数据运行于廉价的商用机器集群上&#xff1b; HDFS 不适合以下场合&#xff1a;低延迟数据…

深度学习算法和机器学习算法_啊哈! 4种流行的机器学习算法的片刻

深度学习算法和机器学习算法Most people are either in two camps:大多数人都在两个营地中&#xff1a; I don’t understand these machine learning algorithms. 我不了解这些机器学习算法。 I understand how the algorithms work, but not why they work. 我理解的算法是如…

Python第一次周考(0402)

2019独角兽企业重金招聘Python工程师标准>>> 一、单选 1、Python3中下列语句错误的有哪些&#xff1f; A s input() B s raw_input() C print(hello world.) D print(hello world.) 2、下面哪个是 Pycharm 在 Windows 下 默认 用于“批量注释”的快捷键 A Ctrl d…

express 路由中间件_Express通过示例进行解释-安装,路由,中间件等

express 路由中间件表达 (Express) When it comes to build web applications using Node.js, creating a server can take a lot of time. Over the years Node.js has matured enough due to the support from community. Using Node.js as a backend for web applications a…

ASP.NET 页面之间传值的几种方式

对于任何一个初学者来说&#xff0c;页面之间传值可谓是必经之路&#xff0c;却又是他们的难点。其实&#xff0c;对大部分高手来说&#xff0c;未必不是难点。 回想2016年面试的将近300人中&#xff0c;有实习生&#xff0c;有应届毕业生&#xff0c;有1-3年经验的&#xff0c…

Mapreduce原理和YARN

MapReduce定义 MapReduce是一种分布式计算框架&#xff0c;由Google公司2004年首次提出&#xff0c;并贡献给Apache基金会。 MR版本 MapReduce 1.0&#xff0c;Hadoop早期版本(只支持MR模型)MapReduce 2.0&#xff0c;Hadoop 2.X版本&#xff08;引入了YARN资源调度框架后&a…

数据可视化图表类型_数据可视化中12种最常见的图表类型

数据可视化图表类型In the current era of large amounts of information in the form of numbers available everywhere, it is a difficult task to understand and get insights from these dense piles of data.在当今时代&#xff0c;到处都是数字形式的大量信息&#xff…

三大纪律七项注意(Access数据库)

三大纪律&#xff08;规则或范式&#xff09; 要有主键其他字段依赖主键其他字段之间不能依赖七项注意 一表一主键(订单表&#xff1a;订单号&#xff1b;订单明细表&#xff1a;订单号产品编号)经常查&#xff0c;建索引&#xff0c;小数据&#xff08;日期&#xff0c;数字类…

CentOS下安装JDK的三种方法

来源&#xff1a;Linux社区 作者&#xff1a;spiders http://www.linuxidc.com/Linux/2016-09/134941.htm 由于各Linux开发厂商的不同,因此不同开发厂商的Linux版本操作细节也不一样,今天就来说一下CentOS下JDK的安装: 方法一&#xff1a;手动解压JDK的压缩包&#xff0c;然后…

MapReduce编程

自定义Mapper类 class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> &#xff5b; … }自定义mapper类都必须实现Mapper类&#xff0c;有4个类型参数&#xff0c;分别是&#xff1a; Object&#xff1a;Input Key Type-------------K1Text: Input…

统计信息在数据库中的作用_统计在行业中的作用

统计信息在数据库中的作用数据科学与机器学习 (DATA SCIENCE AND MACHINE LEARNING) Statistics are everywhere, and most industries rely on statistics and statistical thinking to support their business. The interest to grasp on statistics also required to become…

IOS手机关于音乐自动播放问题的解决办法

2019独角兽企业重金招聘Python工程师标准>>> 评估手机自带浏览器不能识别 aduio标签重的autoplay属性 也不能自动执行play()方法 一个有效的解决方案是在微信jssdk中调用play方法 document.addEventListener("WeixinJSBridgeReady", function () { docum…

svg标签和svg文件区别_什么是SVG文件? SVG图片和标签说明

svg标签和svg文件区别SVG (SVG) SVG or Scalable Vector Graphics is a web standard for defining vector-based graphics in web pages. Based on XML the SVG standard provides markup to describe paths, shapes, and text within a viewport. The markup can be embedded…