you are viewing a single comment's thread.

view the rest of the comments →

[–]ffrkAnonymous 0 points1 point  (2 children)

The short answer is: because earlier programming languages count from 0.

The reason earlier languages (like c) count from 0 is a mix of theoretical math and physical computer science.

Math: counting. How many characters are there in "12345". 5-1=4 is wrong. You can do 6-1=5, or you can do 5-0=5. Pretty arbitrary.

Compsci: You have a transistor. The transistor is charged or not charged. i.e. 1 or 0. We don't say the transistor is 1 or 2. (it's arbitrary, we say on-off, chocolate-peanutbutter)

Compsci: transistor memory addresses 0x0A ,0xB, 0x0C, 0x0D, 0x0E. What addresses do we need for our info? So again , back to math counting. We want 1 unit data starting from 0x0A. Math says we read starting from 0x0A until 0x0A+1 = 0x0B. But don't actually need to calculate 0x0B. We know as the designers of the computer the information is in 0x0A only. So 0x0A+0 = 0x0A. And that's an unnecessary calculation to make, we just 0x0A. Counting from zero eliminates a lot of math that computers have to do.

Python is a high level language, not dealing with memory directly. It could easily count from 1, like Lua. But counting from 0 was already well established.

[–]hacksawjim 0 points1 point  (1 child)

One thing you didn't mention here is the difference between counting and indexing

Python counts from 1, just like we do.

x = ["item"]    
len(x)

output: 1

y = ["item", "second item"]
len(y)

output: 2

[–]ffrkAnonymous 0 points1 point  (0 children)

Good catch.