- Mastering Python Scripting for System Administrators
- Ganesh Sanjiv Naik
- 166字
- 2021-07-02 14:00:27
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
推薦閱讀
- 零基礎學C++程序設計
- Learning Apex Programming
- TypeScript Blueprints
- 高級C/C++編譯技術(典藏版)
- Go并發編程實戰
- C/C++數據結構與算法速學速用大辭典
- MINECRAFT編程:使用Python語言玩轉我的世界
- Learning iOS Security
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐
- Oracle Database 12c DBA官方手冊(第8版)
- Pandas入門與實戰應用:基于Python的數據分析與處理
- 軟件測試項目實戰之功能測試篇
- AngularJS by Example
- Android Application Programming with OpenCV 3
- C++ Primer(中文版)(第5版)