###### tags: `chapter 7` # 7-5 enumerate()函式 # `enumerate()`函式是將一個可以用`for`迴圈走訪的對象ex:串列、字串、元組(chapter 8)組合成一個索引序列,也就是經過enumerate()函數,當你走訪串列元素時,除了變數值之外,你還會得到他的索引值。相當於從原本的一個排隊的人(變數值),變成一個排隊的人和一張號碼牌(索引值)。 而使用`enumerate()`搭配`for`迴圈時,就可以準備兩個變數分別去接索引值和變數值了! ```python= all_player = ["Lebron" , "Wade" , "Kobe" , "Nash" , "Durant"] for num , player in enumerate(all_player): print("{}是串列裡的第{}個!".format(player , num)) ``` 藉由上面的範例程式可以發現,這次使用`for`迴圈時使用了兩個變數去迭代,分別是`num`、`player`,分別對應`enumerate()`函數幫你產生的索引值(index),和串列本身的變數值(value)!!