elif python_elif关键字,带Python示例

elif python

Python Elif关键字 (Python elif keyword)

elif is a keyword (case-sensitive) in python, it is used in the conditional statement, if we have multiple conditions to be checked, we have to use elif keyword to check next condition.

elif是python中的一个关键字(区分大小写),在条件语句中使用,如果我们要检查多个条件,则必须使用elif关键字检查下一个条件。

Note: First condition is checked with if keyword and other all are checked with elif keyword and if the last/default block (if any condition is not true) is written with else keyword.

注意:第一个条件用if关键字检查,其他全部用elif关键字检查,最后/默认块(如果任何条件不成立)是否用else关键字写。

Syntax of elif keyword

elif关键字的语法

    if test_condition1:
statement(s)-true-1
elif test_condition2:
statement(s)-true-2
.
.
..
else:
statement(s)-false

Here, if test_condition1 is True, then statement(s)-true-1 will be executed, if the test_condition2 is True, then statement(s)-true-2 will be executed, and so on... we can test multiple conditions, if all conditions are not True, then else block (statement(s)-false) will be executed.

在这里,如果test_condition1为True ,则将执行语句-true-1 ,如果test_condition2为True ,则将执行语句-true-2 ,依此类推...我们可以测试多个条件,如果所有条件都不都是True ,那么将执行else块( statement-s false )。

Example:

例:

    Input:
num = -21
# condition
if num>0:
print(num," is a positive value")
elif num<0:
print(num," is a negative value")
else:
print(num," is a zero")
Output:
-21  is a negative value

if,else,elif关键字的Python示例 (Python examples of if, else, elif keywords)

Example 1: Input a number and check whether it is positive, negative or zero.

示例1:输入一个数字并检查它是正数,负数还是零。

# python code to demonstrate example of 
# if, else and elif keywords 
# Input a number and check whether 
# it is positive, negative or zero.
# input
num = int(input("Enter a number: "))
# conditions
if num>0:
print(num," is a positive value")
elif num<0:
print(num," is a negative value")
else:
print(num," is a zero")

Output

输出量

First run:
Enter a number: -21
-21  is a negative value
Second run:
Enter a number: 2
2  is a positive value
Third run:
Enter a number: 0
0  is a zero

Example 2: Input three numbers and find largest number.

示例2:输入三个数字并找到最大的数字。

# python code to demonstrate example of 
# if, else and elif keywords 
# Input three numbers and find largest number.
a = int(input("Enter first number :"))
b = int(input("Enter second number:"))
c = int(input("Enter third number :"))
large =0
# conditions
if a>b and a<c:
large = a;
elif b>a and b>c:
large = b;
else:
large = c;
# printing largest number 
print("largest number is: ", large)

Output

输出量

Enter first number :10
Enter second number:30
Enter third number :20
largest number is:  30

翻译自: https://www.includehelp.com/python/elif-keyword-with-example.aspx

elif python

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

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

相关文章

今天看明白了,为什么有些属性会这样写了:public string status{get;set;}

自动实现的属性在 C# 3.0 和更高版本中&#xff0c;当属性的访问器中不需要其他逻辑时&#xff0c;自动实现的属性可使属性声明更加简洁。 客户端代码还可通过这些属性创建对象。 如下面的示例所示声明属性时&#xff0c;编译器将创建一个私有的匿名支持字段&#xff0c;该字段…

数字图像的大小、所需比特数(二维)

二维数字图像所需的比特数根据公式&#xff1a; 其中&#xff1a; b&#xff1a;数字图像所需的比特数 MN&#xff1a;数字图像的行和列 k&#xff1a;由灰度级算出&#xff0c;公式如下&#xff1a; L&#xff1a;图像的灰度级 比如&#xff1a; 存储一幅大小为 1024x1024&a…

ASP.NET中使用web.config配置web应用程序中的数据库连接

[摘要]&#xff1a;在web.config文件中保存数据库连接配置信息,可以让你无须重新编译应用程序即可更新应用程序的某些属性。当你想把数据库迁移到另一个不同的服务器&#xff0c;你只需要修改web.config文件中的数据库连接配置信息而已&#xff0c;并不需要重新编译和重新部署这…

c+ +三角函数_C ++中的三角函数

c 三角函数C 三角函数 (C Trigonometric functions) Trigonometric functions are also called circular functions or angle functions or goniometric functions, which are used to trigonometric computations like computing cosine, sine, tangent values. 三角函数也称为…

4邻接、8邻接、m邻接

在认识这些之前&#xff0c;我们首先要认识4领域、8领域 4领域&#xff1a; 像素p的坐标是(x,y)&#xff0c;那么他的4领域坐标N4是&#xff1a;&#xff08;x1,y&#xff09;、(x-1,y)&#xff0c;&#xff08;x&#xff0c;y1&#xff09;、(x,y-1) 8领域&#xff1a; 点p的…

RST_n的问题

有一个灰常郁闷的问题。。。 module CLK_Generater( input CLOCK_100, input RST_n, input Key, output reg [3:0] CLK_DivChoose, );reg [19:0] count; …

java aop注解拦截_Spring AOP 拦截指定注解标识的类或方法

代码DemoAspectComponentOrder(10)public class BidAuthorityProxy {/*** 扫描指定包下的类中使用EnableRoleAuthority注解修饰的类*/Around("within(com.core.annotation.EnableRoleAuthority) && within(com.bid..*)")public Object verifyRoleExecuteComm…

其实我是一个很偏激的人,不信你打我一下试试看

rt转载于:https://www.cnblogs.com/rexhost/archive/2004/09/23/45969.html

python关键字和保留字_或带有Python示例的关键字

python关键字和保留字Python或关键字 (Python or keyword) or is a keyword (case-sensitive) in python, it is a logical operator actually, it is used to validate more than one conditions. It is similar to Logical OR (||) operator in C, C programming. It require…

php的文件包含总结 include require include_once require_once

文件包含相当于将另一个文件的代码全部复制到另一个文件中&#xff0c;然后执行。包含文件很有用&#xff0c;如果您需要在网站的多张页面上引用相同的 PHP、HTML 或文本的话。比如说我们在浏览csdn很多页面中&#xff0c;基本都是看到下面的内容&#xff0c;为了不要每次都要写…

社会生活中的著名法则(z)

社会生活中的著名法则 [摘要]社会生活中的著名法则 一、 马太效应 二、 手表定理 三、 不值得定律 四、 彼得原理 五、 零和游戏原理 六、 华盛顿合作规律 七、 酒与污水定律 八、 水桶定律 九、 蘑菇管理 十、 奥卡姆剃刀定律 十一、 二八法则 十二、 钱的问题  一、马太效应…

判断滚动条是否到达页面的尾部

//取窗口滚动条高度 functiongetScrollTop() { varscrollTop0; if(document.documentElement&&document.documentElement.scrollTop) { scrollTopdocument.documentElement.scrollTop; } elseif(document.body) { scrollTopdocument.body.scrollTop; } returnscr…

python的pass语句_适用于pass语句的Python程序

python的pass语句Prerequisite: pass statement in Python 先决条件&#xff1a; Python中的pass语句 In the below program, we have a string and printing only alphabets and passing the else statement – where the character in not an alphabet. 在下面的程序中&…

php终止脚本执行(exit、die、return)

终止php的脚本执行&#xff0c;我们可以使用exit&#xff0c;die&#xff0c;return 0x01 exit和die&#xff0c; 当程序运行到他们时&#xff0c;直接退出程序&#xff0c;不在运行 <?phpheader(content-type:text/html;charsetutf-8);echo 使用exit前;echo <br>…

Python中的break语句

Python break语句 (Python break statement) Like other programming languages, in python, break statement is used to terminate the loops execution. It terminates the loops execution and transfers the control to the statement written after the loop. 与其他编程…

php的延时sleep函数

语法&#xff1a;sleep&#xff08;秒数&#xff09; <?phpheader(content-type:text/html;charsetutf-8);sleep&#xff08;5&#xff09;;echo 我的名字是DL_one; ?>输出时可以发现要等待一段时间才能输出 sleep函数在代码测试时很有用

25种成为百万富翁的方法

1、做你真正感兴趣的事——你会花很多时间在上面&#xff0c;因此你一定要感兴趣才行&#xff0c;如果不是这样的话&#xff0c;你不合愿意把时间花在上面&#xff0c;就得不到成功。  2、自己当老板&#xff0c;为别人打工&#xff0c;你绝不会变成巨富&#xff0c;老板一心…

java byte数组string_byte数组和String之间的转化

JAVA里面关于byte数组和String之间的转换问题把byte转化成string&#xff0c;必须经过编码。例如下面一个例子:import java.io.UnsupportedEncodingException;public class test{public static void main(String g[]) {String s "12345abcd";byte b[] s.getBytes()…

arcsde 和oracle(双机热备)分布式安装(转载)

环境&#xff1a; oracle10gR2安装在两台服务器上&#xff0c;但只是做双机热备&#xff0c;操作系统均为windows server 2003 sp2 arcsde安装在第三台机器上&#xff0c;操作系统为windows server 2003 sp2 安装步骤&#xff1a; 1、需要在ArcSDE的安装机器上安装Or…

python中文件描述符_Python中的描述符

python中文件描述符In Python, a class that implements a get, set or delete methods for an object is called descriptors. Descriptors are the way to create attributes and add managed attributes to objects. These are used to protect the attributes from changes…