# Python 循環
> 原文: [https://thepythonguru.com/python-loops/](https://thepythonguru.com/python-loops/)
* * *
于 2020 年 1 月 7 日更新
* * *
Python 只有兩個循環:
1. `for`循環
2. `while`循環
## `for`循環
* * *
`for`循環語法:
```py
for i in iterable_object:
? ?# do something
```
**注意**:
`for`和`while`循環內的所有語句必須縮進相同的空格數。 否則,將拋出`SyntaxError`。
讓我們舉個例子
```py
my_list = [1,2,3,4]
for i in my_list:
? ? print(i)
```
**預期輸出**:
```py
1
2
3
4
```
這是`for`循環的工作方式:
在第一次迭代中,為`i`分配了值`1`,然后執行了`print`語句。 在第二次迭代中,為`i`賦值`2`,然后再次執行`print`語句。 此過程將繼續進行,直到列表中沒有其他元素并且存在`for`循環為止。
## `range(a, b)`函數
* * *
`range(a, b)`函數從`a`,`a + 1`,`a+ 2` ....,`b - 2`和`b - 1`返回整數序列。 例如:
```py
for i in range(1, 10):
? ? print(i)
```
**預期輸出**:
```py
1
2
3
4
5
6
7
8
9
```
您還可以通過僅提供一個參數來使用`range()`函數,如下所示:
```py
>>> for i in range(10):
... ? ? ? ?print(i)
0
1
2
3
4
5
6
7
8
9
```
循環打印的范圍是 0 到 9。
`range(a, b)`函數具有可選的第三個參數,用于指定步長。 例如:
```py
for i in range(1, 20, 2):
? ? print(i)
```
**預期輸出**:
```py
1
3
5
7
9
11
13
15
17
19
```
## `While`循環
* * *
句法:
```py
while condition:
? ? # do something
```
`while`循環在其中繼續執行語句,直到條件變為假。 在每次迭代條件檢查之后,如果其條件為`True`,則會在`while`循環中再次執行語句。
讓我們舉個例子:
```py
count = 0
while count < 10:
? ? print(count)
? ? count += 1
```
**預期輸出**:
```py
0
1
2
3
4
5
6
7
8
9
```
在此處,將繼續打印,直到`count`小于`10`為止。
## `break`語句
* * *
`break`語句允許突破循環。
```py
count = 0
while count < 10:
count += 1
? ? if count == 5:
? ? ? ? ?break? ?
? ? print("inside loop", count)
print("out of while loop")
```
當`count`等于`5`時,如果條件求值為`True`,并且`break`關鍵字跳出循環。
**預期輸出**:
```py
inside loop 1
inside loop 2
inside loop 3
inside loop 4
out of while loop
```
## `continue`語句
* * *
當在循環中遇到`continue`語句時,它將結束當前迭代,并且程序控制將轉到循環主體的末尾。
```py
count = 0
while count < 10:
? ? count += 1
? ? if count % 2 == 0:
? ? ? ? ? ?continue
? ? print(count)
```
**預期輸出**:
```py
1
3
5
7
9
```
如您所見,當`count % 2 == 0`時,將執行`continue`語句,該語句導致當前迭代結束,并且控件繼續進行下一個迭代。
在下一課中,我們將學習 [Python 數學函數](/python-mathematical-function/)。
* * *
* * *
- 初級 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 轉儲對象