好用的for循环与range

  1. for循环

    # for 变量 in  可迭代对象:
    #	pass
    s = "1234567890"
    for each in s:          # 遍历字符串
        print(each)         # 1 2 3 4 5 6 7 8 9 0
    print(each)             # 0   注意
    
  2. 占位符

    pass 和 ...

  3. range

    • 语法range(start,end,step): 范围,从start到end,不包含end,步长step默认是1

    • python3中打印range()会打印本身,python2中会打印出列表

      for each in range(1,10):
      	print(each)
      # 1  2  3  4  5  6  7  8  9
      
posted @ 2019-06-05 18:06  怀心抱素  阅读(530)  评论(0编辑  收藏  举报