Python's `range` function with 3 parameters

后端 未结 3 985
花落未央
花落未央 2020-12-09 20:04

I understand that the following line will give the given result:

for in range(5):
   print(i)

0 1 2 3 4

But I don\'t understand how

3条回答
  •  遥遥无期
    2020-12-09 20:26

    for i in range(4, 10, 2):
     print(i) 
    

    in the above code, range has 3 parameters :

    1. Start of Range (inclusive)
    2. End of Range (Exclusive)
    3. Incremental value

    For more clarity refer below for the java representation of above python code:

    for (int i=0; i<10; i+=2){
        System.out.println(i)
    }
    

提交回复
热议问题