Python range function

后端 未结 6 951
广开言路
广开言路 2021-02-07 05:53

Say I want to loop from 0 to 100 but with a step of 1/2. If you try

for i in range(0, 100, 0.5):
    whatever
         


        
6条回答
  •  梦毁少年i
    2021-02-07 06:40

    Python2.x:

    for idx in range(0, int(100 / 0.5)):
    
        print 0.5 * idx      
    

    outputs:

    0.0

    0.5

    1.0

    1.5

    ..

    99.0

    99.5


    Numpy:

    numpy.arange would also do the trick.

    numpy.arange(0, 100, 0.5)
    

提交回复
热议问题