官术网_书友最值得收藏!

Python if...elif...else statement

The elif statement checks multiple statements for a true value. Whenever the value evaluates to true, that code block gets executed. Refer to the following syntax: 

if test expression:
if block statements
elif test expression:
elif block statements
else:
else block statements

elif is short for else if. It allows us to check for multiple expressions. If the condition written in the if statement is false, then it will check the condition of the next elif block, and so on. If all of the conditions are false, the body of else is executed.

Only one block among the several if...elif...else blocks is executed according to the condition. The if block can have only one else block. But it can have multiple elif blocks. Let's take a look at an example:

a = 10
if a > 50:
print("a is greater than 50")
elif a == 10:
print("a is equal to 10")
else:
print("a is negative")

Output:
a is equal to 10
主站蜘蛛池模板: 贡觉县| 怀集县| 德化县| 黄冈市| 弋阳县| 乐平市| 郓城县| 兖州市| 来凤县| 崇仁县| 呼伦贝尔市| 邵东县| 鹤壁市| 乌拉特后旗| 察隅县| 华容县| 九江市| 万山特区| 胶南市| 三江| 大英县| 镇江市| 渑池县| 乐至县| 延边| 鄂伦春自治旗| 大厂| 监利县| 浏阳市| 吴堡县| 乌兰县| 肃南| 措勤县| 井研县| 长垣县| 响水县| 剑河县| 荔波县| 安龙县| 肇庆市| 邓州市|