python emunerate
时间: 2024-05-08 08:15:13 浏览: 14
The `enumerate()` function in Python is used to add a counter to an iterable object and returns it as an enumerate object. The enumerate object contains pairs of the index and the item at that index in the iterable object.
Syntax:
```python
enumerate(iterable, start=0)
```
Parameters:
- `iterable` - an iterable object like list, tuple, string, etc.
- `start` (optional) - an integer value that specifies the starting index of the counter. Default value is 0.
Example:
```python
fruits = ['apple', 'banana', 'orange', 'mango']
for i, fruit in enumerate(fruits, start=1):
print(i, fruit)
```
Output:
```
1 apple
2 banana
3 orange
4 mango
```
In the above example, the `enumerate()` function is used to add a counter to the `fruits` list and the `for` loop is used to print the index and the fruit at that index. The `start` parameter is set to 1 to start the counter from 1 instead of the default value of 0.
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)