# 7 For 迴圈

``print("Jennifer Aniston")``
``## Jennifer Aniston``
``print("Courteney Cox")``
``## Courteney Cox``
``print("Lisa Kudrow")``
``## Lisa Kudrow``
``print("Matt LeBlanc")``
``## Matt LeBlanc``
``print("Matthew Perry")``
``## Matthew Perry``
``print("David Schwimmer")``
``## David Schwimmer``

``````starrings = ["Jennifer Aniston", "Courteney Cox", "Lisa Kudrow", "Matt LeBlanc", "Matthew Perry", "David Schwimmer"]
print(starrings[0])``````
``## Jennifer Aniston``
``print(starrings[1])``
``## Courteney Cox``
``print(starrings[2])``
``## Lisa Kudrow``
``print(starrings[3])``
``## Matt LeBlanc``
``print(starrings[4])``
``## Matthew Perry``
``print(starrings[5])``
``## David Schwimmer``

## 7.1 for 迴圈結構

``````for i in x:
# 每次迭代執行的程式``````

Python 使用縮排（indent）來載明每次迭代要處理的事情，假如 x 是一個 list 或是 tuple，`i` 會由 `x[0]` 起始；當一次迭代結束，`i` 會更動為 `x[1]`，以此類推；而在每一次迭代中，Python 都會執行縮排部分的程式。

``````starrings = ["Jennifer Aniston", "Courteney Cox", "Lisa Kudrow", "Matt LeBlanc", "Matthew Perry", "David Schwimmer"]
for starring in starrings:
print starring``````
``````## Jennifer Aniston
## Courteney Cox
## Lisa Kudrow
## Matt LeBlanc
## Matthew Perry
## David Schwimmer``````

``````starrings = ["Jennifer Aniston", "Courteney Cox", "Lisa Kudrow", "Matt LeBlanc", "Matthew Perry", "David Schwimmer"]
for i in range(len(starrings)):
print starrings[i]``````
``````## Jennifer Aniston
## Courteney Cox
## Lisa Kudrow
## Matt LeBlanc
## Matthew Perry
## David Schwimmer``````

### 7.1.1 不只是 print

``````starrings = ["Jennifer Aniston", "Courteney Cox", "Lisa Kudrow", "Matt LeBlanc", "Matthew Perry", "David Schwimmer"]
upper_firstnames = []
for starring in starrings:
upper_name = starring.upper()
upper_firstnames.append(upper_name)
print(upper_firstnames)``````
``## ['JENNIFER ANISTON', 'COURTENEY COX', 'LISA KUDROW', 'MATT LEBLANC', 'MATTHEW PERRY', 'DAVID SCHWIMMER']``

## 7.2 List Comprehension

``````starrings = ["Jennifer Aniston", "Courteney Cox", "Lisa Kudrow", "Matt LeBlanc", "Matthew Perry", "David Schwimmer"]
upper_firstnames = [x.upper() for x in starring]
print(upper_firstnames)``````
``## ['D', 'A', 'V', 'I', 'D', ' ', 'S', 'C', 'H', 'W', 'I', 'M', 'M', 'E', 'R']``

## 7.3 enumerate 函數

enumerate 函數能夠同時取用索引與元素。

``````starrings = ["Jennifer Aniston", "Courteney Cox", "Lisa Kudrow", "Matt LeBlanc", "Matthew Perry", "David Schwimmer"]
for i, starring in enumerate(starrings):
print("{}: {}".format(i, starring))``````
``````## 0: Jennifer Aniston
## 1: Courteney Cox
## 2: Lisa Kudrow
## 3: Matt LeBlanc
## 4: Matthew Perry
## 5: David Schwimmer``````

## 7.4 zip 函數

zip 函數能夠讓兩個迴圈平行運作。

``````starrings = ["Jennifer Aniston", "Courteney Cox", "Lisa Kudrow", "Matt LeBlanc", "Matthew Perry", "David Schwimmer"]
characters = ["Racheal Green", "Monica Geller", "Phoebe Buffay", "Joey Tribbiani", "Chandler Bing", "Ross Geller"]
for char, starring in zip(characters, starrings):
print("{}: {}".format(char, starring))``````
``````## Racheal Green: Jennifer Aniston
## Monica Geller: Courteney Cox
## Phoebe Buffay: Lisa Kudrow
## Joey Tribbiani: Matt LeBlanc
## Chandler Bing: Matthew Perry
## Ross Geller: David Schwimmer``````