range Failure?

Colin J. Williams cjw at sympatico.ca
Sun Mar 10 11:07:18 EST 2002


  
Thanks to Oleg and Sean.  It was a silly error. 

Sean, 

What you say is, on reflection, obvious. 
Perhaps the doc could better bring this out? 

The help now has: 
     range([start,] stop[, step]) 
     This is a versatile function to create lists containing arithmetic
     progressions. It is most often used in for loops. The arguments
     must be plain integers. If the step argument is omitted, it
     defaults to 1. If the start argument is omitted, it defaults to
     0. The full form returns a list of plain integers [start, start +
     step, start + 2 * step, ...].  If step is positive, the last
     element is the largest start + i * step less than stop; if step is
     negative, the last element is the largest start + i * step greater
     than stop. step must not be zero (or else ValueError is raised). 

It might say:   

     range([start= 0,] stop[, step= 1]) 
     This function creates a list containing an arithmetic progression.
     The arguments must be integers.   If only one argument is passed it 
     is the 'stop' value, if two values are passed then the first is the
     'start' value and the second the 'stop' value.
     The function returns a list of integers [start, start + step, start
     + 2 * step, ...]. The 'step' must not be zero; if 'step' is positive, 
     the last element is the largest; if  'step' is negative, the last
     element is the smallest.

Colin W. 

      
Sean 'Shaleh' Perry wrote: 

  On 09-Mar-2002 Colin J. Williams wrote: 
  > <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> 
  > <html> 
  > It would seem that the first execution below should deliver the same 
  > <br>result as the second. 
  > <br>>>> range(5, 2) 
  > <br>[] 
  > <br>>>> range(0, 5, 2) 
  > <br>[0, 2, 4] 

  GAH! kill the html please. 

  range(5,2) says "start at 5 and go to 2" which makes no sense and thus
returns 
  an empty list. 

  from the python source: 

          if (PyTuple_Size(args) <= 1) { 
                  if (!PyArg_ParseTuple(args, 
                                  "l;range() requires 1-3 int
arguments", 
                                  &ihigh)) 
                          return NULL; 
          } 
          else { 
                  if (!PyArg_ParseTuple(args, 
                                  "ll|l;range() requires 1-3 int
arguments", 
                                  &ilow, &ihigh, &istep)) 
                          return NULL; 
          } 

  Look at the last arguments to each funtion.  If one argument is passed
  it because the high value (endpoint).  Otherwise the values are 
  assigned in order to low, high and then step (which defaults to 1).




More information about the Python-list mailing list