Why does range(start, end) not include end?

后端 未结 9 1197
夕颜
夕颜 2020-11-22 10:24
>>> range(1,11)

gives you

[1,2,3,4,5,6,7,8,9,10]

Why not 1-11?

Did they just decide to do it lik

9条回答
  •  长发绾君心
    2020-11-22 11:13

    The length of the range is the top value minus the bottom value.

    It's very similar to something like:

    for (var i = 1; i < 11; i++) {
        //i goes from 1 to 10 in here
    }
    

    in a C-style language.

    Also like Ruby's range:

    1...11 #this is a range from 1 to 10
    

    However, Ruby recognises that many times you'll want to include the terminal value and offers the alternative syntax:

    1..10 #this is also a range from 1 to 10
    

提交回复
热议问题