Pythonic way to iterate through a range starting at 1

后端 未结 5 807
伪装坚强ぢ
伪装坚强ぢ 2021-01-01 16:04

Currently if I want to iterate 1 through n I would likely use the following method:

for _ in rang         


        
5条回答
  •  孤城傲影
    2021-01-01 16:40

    Not a general answer, but for very small ranges (say, up to five), I find it much more readable to spell them out in a literal:

    for _ in [1,2,3]:
        print _
    

    That's true even if it does start from zero.

提交回复
热议问题