+ 1

Why the index of list always start from '0' ?

25th Mar 2021, 3:48 PM
Kamal Kishor Mahto
Kamal Kishor Mahto - avatar
3 odpowiedzi
+ 3
That's a design decision made by the makers of Python so they'd know for sure but I will speculate. The designers of Python probably wanted to make that similar to arrays in most programming languages such as c and c++ which were very popular before Python was introduced in the early 1990's. Another benefit could be a small performance benefit to 0-based arrays. Calculating the memory address associated with a[x] breaks down to a + x * sizeof(a single element of a). If a was a 1-based array, the memory address could require an extra decrement of x like: a + (x - 1) * sizeof(a single element of a). Java, JavaScript, c# and many other newer languages have zero-based arrays for the same reason. Some languages like Delphi have 1-based arrays but this is less popular.
25th Mar 2021, 3:53 PM
Josh Greig
Josh Greig - avatar
25th Mar 2021, 4:39 PM
Abir Hasan
Abir Hasan - avatar
0
Thank you so much
18th Apr 2021, 9:18 AM
Kamal Kishor Mahto
Kamal Kishor Mahto - avatar