For loop with custom steps in python

后端 未结 6 2188
旧时难觅i
旧时难觅i 2020-11-29 07:48

I can make simple for loops in python like:

for i in range(10):

However, I couldn\'t figure out how to make more complex ones, which are re

6条回答
  •  囚心锁ツ
    2020-11-29 08:11

    for i in range(0, 10, 2):
        print(i)
    
    >>> 0
    >>> 2
    >>> 4
    >>> 6
    >>> 8
    

    http://docs.python.org/2/library/functions.html

    >>> range(10)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> range(1, 11)
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> range(0, 30, 5)
    [0, 5, 10, 15, 20, 25]
    >>> range(0, 10, 3)
    [0, 3, 6, 9]
    

提交回复
热议问题