Python's `range` function with 3 parameters

后端 未结 3 966
花落未央
花落未央 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)
    }
    
    0 讨论(0)
  • 2020-12-09 20:44

    range(start_pos, end_pos, increment)

    0 讨论(0)
  • 2020-12-09 20:47

    Starts at 4, then increments by 2, to end at 8 because 10 < 10 is false. So 4 6 8

    0 讨论(0)
提交回复
热议问题