Why does the range within Python contain an upper bound?

Source: Internet
Author: User
Tags ranges
For example, range (1,5), which actually contains only 1,2,3,4 even range (0,6,2), does not contain 6. If the number is not in range, what is the meaning of this number in range? Array starting at 0 I have endured, range does not contain an upper bound what does that mean? This also let slice and so on how the operation by human understanding AH? In addition, left closed right open is where to get the habit? What's the source? The Lord agrees with the answer: "The upper bound parameter of range is not the upper bound of the mathematical meaning, but the condition of jumping out of the loop in the sense of calculation (when the loop structure is applied to this range, the" upper bound "is used to mean jumping out); The reason why an array pointer is allowed to point to an element following an array is similar. The Lord can look into Andrew Koenig's very detailed discussion in this regard. "See the wheel of the teacher's answer I can't help but understand one thing: the programmer is stubborn. The second argument that range accepts is not the array length, but the upper bound of the array. Do you want us to think that 6 in range 1,6 is the length of this array? Is the length of the 1,2,3,4,5 6? Range (5,6) 6 is the array length? Actually I agree that range (Start,end) is corresponding to the for (int i=start, I.). Don't use C to justify it. I learned C, I know the array starts with 0, but I can't understand that range has no upper bound. Learn how engineers and scientists think about it in MATLAB and Mathematica and other engineering software, wheel brother. In addition, the focus of the subject is not the array from 0, but the upper bounds of range. Don't catch Dijkstra this wonderful flower not put thank you. The mathematician defines a range that does not contain the right side, please tell me. I must humbly study

Reply content:

Why is standard iterator ranges [begin, end] instead of [begin, end]? Compare the following three scenarios for dividing real numbers

[0, 1] (1, 2) [2, 3] ...
(0, 1) [1, 2] (2, 3) ...
[0, 1) [1, 2) [2, 3) ...

It is clear that the last scheme is the most beautiful, and all the intervals contain "as many" elements.

Of course, it is only explained that the half-open half-closed/half-closed half-open partition is the most suitable, and the two are more natural than half-closed half-open.

Every day to say what mathematicians mathematicians, the title of the Lord you learn physics do not come to guide Jiangshan teach us how to design language AH. Although you are stubborn, I still want to civilized you.


Why left closed right open, why array starting from 0, this is because C language obtains an element of array a address &a[b] can be translated directly into &a+b*sizeof (*a), eliminating the subtraction. This subtraction is very important when the C language was first invented. Then you have a length s, and the For loop that you iterate through the array is for (i=0;i<>< p="">
<>

Well, Python is not a C language, but the first implementation of Python is written in C! Almost all languages are affected by C. Of course, a small part of the language is not, for example, Visual HTTP/Basic.NET, for example , I am my own www.gaclib.net Built-in scripting engine to do the silly little feet (and finally found that the key design of Swift is completely consistent with mine, too humiliating). This is purely the language of his father's liking. python His father is not a mathematician , haha haha.


But we have been pulling each other black, I do not have to see the title of your comments, so happy.


Why is standard iterator ranges [begin, end] instead of [begin, end]?


Downstairs @shell a link to von, and I found the mathematician Dijkstra thinking the same way.

On this question, I think it should be this:

I try to answer this question from the contents of the iterator inside C + +,
For a container, we need to know its tail, the most common operation is from begin to end continuous + one loop, at this time how to know the loop to reach the end, if the end is an upper bound (that is, the inclusion of this element), we also need to its next element to throw a discriminant signal, Then end has little meaning, so for loops, the next element at the end is more important and can terminate the loop.
The idea is that if you take a reversed iterator, begin points to the last element (inclusive), and the end points to the previous element of the first element, which is a left-to-right-closed state for the container, and the iterator +1 is actually going forward.

The range in Python does not also produce a list, the traversal always needs to exit, left closed right because we always start from the left side of the list, traversing to the right end.

Hope to be of some help. Which mathematician often uses range
What is the dignity of a mathematician? Why is the upper floor saying the array starts with 0 this. I think it has nothing to do with the problem.

In fact, the reason for using left-right open in the program is:

Consider an interval [L, R], and if I need to traverse this interval, an iterator is required to support the comparison operation.
That
i = l;while (i <= r) do {    process(i);    i++;}
Everyone likes to open the left-right interval. The designer: "I would like to" suggest you still use basic language good, array can start from 1, for can 1 to 2 write like this, but the main problem seems to be a soft black.
——————————————————————————————————————————
Python array

>>> s=[1,2,3,4,5,6]

>>> s

[1, 2, 3, 4, 5, 6]

>>> for I in Range (0,6):

... print S[i]



Actually comes from

Array of C languages
int arr[6]={0,1,2,3,4,5};//0 to 5 ...
Array traversal
for (int i=0;i printf ("%d\n", Arr[i]);
}
Ps:c language is commonly used i<[length] instead of i<=[length]-1.
1.i<[Length] more intuitive
2. In extreme cases, if the array length is 0 and the use of unsigned int,i<=-1 is obviously a bug.
——————————————————————————————————————————
For example
2011 to 2012 How many years, if the left and right bound:
Len (year[2011:2012)) = 2
This is not common sense

so the left and right bounds only one
is
Len (year[2011:2012]) =1 don't jiangzui, see the official doc bad?

4.3 The range () Function

Plainly, range (A, b) is default as 0 at design time, so B is is as offset or the length of the existence of an ARG, is not tail index, when your A is not the default, you naturally think "should be a first tail two index AH?!" "I'm sorry, but it's not."

Jiangzui not a long learning.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.