使用 while 的遍历序列

  1. 需要维护一个循环变量,这个变量来作为遍历的时候的下标(索引)

  2. 结束条件就是索引达到了最后。


遍历字符串

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

Copyright © 李振超 2018 all right reserved,powered by Gitbook
该文件最后修订时间: 2018-02-25 07:12:09

results matching ""

    No results matching ""