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

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
主站蜘蛛池模板: 习水县| 海南省| 唐河县| 张家港市| 巧家县| 旺苍县| 新密市| 奈曼旗| 桂平市| 湛江市| 诏安县| 竹北市| 双柏县| 湖州市| 留坝县| 辰溪县| 外汇| 唐河县| 太湖县| 周至县| 黑龙江省| 桑植县| 油尖旺区| 莱州市| 武安市| 湘潭县| 巴南区| 山丹县| 伊宁县| 大化| 嘉祥县| 佛坪县| 商丘市| 昌邑市| 龙南县| 长治市| 论坛| 共和县| 鄂托克前旗| 丹寨县| 伽师县|