Python "why" questions

Lie Ryan lie.1296 at gmail.com
Fri Aug 13 14:43:50 EDT 2010


Sorry the message gets cuts off by an accidental press of send button.

On 08/14/10 04:31, Lie Ryan wrote:
> On 08/10/10 06:36, Bartc wrote:
>> And if the context is Python, I doubt whether the choice of 0-based over a 
>> 1-based makes that much difference in execution speed.
> 
> And I doubt anyone cares about execution speed when deciding whether to
> use 1-based or 0-based array. The reason why you want to choose the
> alternative that use less conversion to the other system is to simplify
> the source code.
> 
> Many common mathematical/physics/economics formulas are expressed much
> simply if we use 0-based counting:
> 
> * arithmetic series:
>   - 1-based: s(n) = a + (n - 1) * d
>   - 0-based: s(n) = a + n * d
> * geometric series:
>   - 1-based: g(n) = a * r**(n - 1)
>   - 0-based: g(n) = a * r**n
> * equation of motion:
>   - 1-based: x(t) = a + 1/2 * a * (t - 1)**2
>   - 0-based: x(t) = a + 1/2 * a * t**2
> * exponential growth/decay:
>   - 1-based: d(t) = a * e**(k * (t - 1))
>   - 0-based: d(t) = a * e**(k*t)
> 
> 
> In fact, most textbooks would already uses 0-based formula for some of
> these formulas already. Most physics and economic textbooks would show
> the base 0 variation of the formula, and refers to t=0 as the point in
> time where the "event" started.
> 
> I often used this model of thinking for 0-based array indices (and
> negative indices):
> 
> -7  -6  -5  -4  -3  -2  -1
>  +---+---+---+---+---+---+---+
>  | c | h | a | r | l | i | e |
>  +---+---+---+---+---+---+---+
> 0   1   2   3   4   5   6  (7)
> 
> instead of:
> 

so to repeat, I often use this model of thinking:

-7  -6  -5  -4  -3  -2  -1
 +---+---+---+---+---+---+---+
 | c | h | a | r | l | i | e |
 +---+---+---+---+---+---+---+
 0   1   2   3   4   5   6  (7)

instead of:

  -7  -6  -5  -4  -3  -2  -1
 +---+---+---+---+---+---+---+
 | c | h | a | r | l | i | e |
 +---+---+---+---+---+---+---+
   0   1   2   3   4   5   6

that is, the indices refers to the "gap" between the array entries. The
"gap index" model highlights the naturalness of using 0-based array,
negative indices, array slicing, and half-open.




More information about the Python-list mailing list