使用 while 的遍历序列
需要维护一个循环变量,这个变量来作为遍历的时候的下标(索引)
结束条件就是索引达到了最后。
遍历字符串
s = "abc你好啊"
i = 0
while i < len(s):
# 遍历出字符之后可以做任何的事情,这里仅仅是输出
print(s[i])
i += 1
遍历列表
nums = [10, 20, 13, 17, 30]
i = 0
while i < len(nums):
print(nums[i])
i += 1
遍历元组
nums = (100, 200, 130, 170, 300)
i = 0
while i < len(nums):
print(nums[i])
i += 1