# Python 控制語句
> 原文: [https://thepythonguru.com/python-control-statements/](https://thepythonguru.com/python-control-statements/)
* * *
于 2020 年 1 月 7 日更新
* * *
程序根據某些條件執行語句是很常見的。 在本節中,我們將了解 Python 中的`if else`語句。
但是在我們需要了解關系運算符之前。 關系運算符使我們可以比較兩個對象。
| 符號 | 描述 |
| --- | --- |
| `<=` | 小于或等于 |
| `<` | 小于 |
| `>` | 大于 |
| `>=` | 大于或等于 |
| `==` | 等于 |
| `!=` | 不等于 |
比較的結果將始終為布爾值,即`True`或`False`。 請記住,`True`和`False`是用于表示布爾值的 python 關鍵字。
讓我們舉一些例子:
```py
>>> 3 == 4
False
>>> 12 > 3
True
>>> 12 == 12
True
>>> 44 != 12
True
```
現在您可以處理`if`語句了。 `if`語句的語法如下所示:
```py
if boolean-expression:
#statements
else:
#statements
```
**注意**:
`if`塊中的每個語句都必須使用相同數量的空格縮進,否則將導致語法錯誤。 這與 Java,C,C# 等使用花括號(`{}`)的語言完全不同。
現在來看一個例子
```py
i = 10
if i % 2 == 0:
? ?print("Number is even")
else:
? ?print("Number is odd")
```
在這里您可以看到,如果數字為偶數,則將打印`"Number is even"`。 否則打印`"Number is odd"`。
**注意**:
`else`子句是可選的,您可以根據需要僅使用`if`子句,如下所示:
```py
if today == "party":
? ? print("thumbs up!")
```
在此,如果`today`的值為`"party"`,則將打印`thumbs up!`,否則將不打印任何內容。
如果您的程序需要檢查一長串條件,那么您需要使用`if-elif-else`語句。
```py
if boolean-expression:
? ?#statements
elif boolean-expression:
? ?#statements
elif boolean-expression:
? ?#statements
elif boolean-expression:
? ?#statements
else:
? ?#statements
```
您可以根據程序要求添加`elif`條件。
這是一個說明`if-elif-else`語句的示例。
```py
today = "monday"
if today == "monday":
? ?print("this is monday")
elif today == "tuesday":
? ?print("this is tuesday")
elif today == "wednesday":
? ?print("this is wednesday")
elif today == "thursday":
? ?print("this is thursday")
elif today == "friday":
? ?print("this is friday")
elif today == "saturday":
? ?print("this is saturday")
elif today == "sunday":
? ?print("this is sunday")
else:
? ?print("something else")
```
## 嵌套`if`語句
* * *
您可以將`if`語句嵌套在另一個`if`語句中,如下所示:
```py
today = "holiday"
bank_balance = 25000
if today == "holiday":
? ?if bank_balance > 20000:
? ? ? print("Go for shopping")
? ?else:
? ? print("Watch TV")
else:
print("normal working day")
```
在下一篇文章中,我們將學習 [Python 函數](/python-functions/)。
* * *
* * *
- 初級 Python
- python 入門
- 安裝 Python3
- 運行 python 程序
- 數據類型和變量
- Python 數字
- Python 字符串
- Python 列表
- Python 字典
- Python 元組
- 數據類型轉換
- Python 控制語句
- Python 函數
- Python 循環
- Python 數學函數
- Python 生成隨機數
- Python 文件處理
- Python 對象和類
- Python 運算符重載
- Python 繼承與多態
- Python 異常處理
- Python 模塊
- 高級 Python
- Python *args和**kwargs
- Python 生成器
- Python 正則表達式
- 使用 PIP 在 python 中安裝包
- Python virtualenv指南
- Python 遞歸函數
- __name__ == "__main__"是什么?
- Python Lambda 函數
- Python 字符串格式化
- Python 內置函數和方法
- Python abs()函數
- Python bin()函數
- Python id()函數
- Python map()函數
- Python zip()函數
- Python filter()函數
- Python reduce()函數
- Python sorted()函數
- Python enumerate()函數
- Python reversed()函數
- Python range()函數
- Python sum()函數
- Python max()函數
- Python min()函數
- Python eval()函數
- Python len()函數
- Python ord()函數
- Python chr()函數
- Python any()函數
- Python all()函數
- Python globals()函數
- Python locals()函數
- 數據庫訪問
- 安裝 Python MySQLdb
- 連接到數據庫
- MySQLdb 獲取結果
- 插入行
- 處理錯誤
- 使用fetchone()和fetchmany()獲取記錄
- 常見做法
- Python:如何讀取和寫入文件
- Python:如何讀取和寫入 CSV 文件
- 用 Python 讀寫 JSON
- 用 Python 轉儲對象