利用for循环和range打印出下面列中每个元素的索引。 li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
时间: 2024-06-02 21:04:27 浏览: 77
可以使用for循环和range函数打印出列表中每个元素的索引,具体代码如下:
```
li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
for i in range(len(li)):
print(i, li[i])
```
输出结果为:
```
0 alex
1 WuSir
2 ritian
3 barry
4 wenzhou
```
相关问题
通过for循环和range打印出下面列表的索引,另外利用while循环如何实现? 比如:li = ["alex", "WuSir", "ritian", "barry", "wenzho"]
利用for循环和range打印出下面列表的索引的代码如下:
```
li = ["alex", "WuSir", "ritian", "barry", "wenzho"]
for i in range(len(li)):
print(i)
```
利用while循环实现的代码如下:
```
li = ["alex", "WuSir", "ritian", "barry", "wenzho"]
i = 0
while i < len(li):
print(i)
i += 1
```
python通过for循环和range打印出下面列表的索引,另外利用while循环如何实现? 比如:li = ["alex", "WuSir", "ritian", "barry", "wenzho"]
利用for循环和range打印出列表的索引可以使用以下代码:
li = ["alex", "WuSir", "ritian", "barry", "wenzho"]
for i in range(len(li)):
print(i)
利用while循环实现可以使用以下代码:
li = ["alex", "WuSir", "ritian", "barry", "wenzho"]
i = 0
while i < len(li):
print(i)
i += 1
阅读全文