The Usage of the Python Numeric.Arange Function
Besides the traditional mathematical tools available for Python, another module exists called "NumPy" exists for special calculations. With this module, programmers can perform linear algebra, use n-dimensional arrays, and integrate other programming languages such as C++ into their code. One of the fundamental functions in the NumPy module is the "arange" function, used for a variety of purposes in the context of complex mathematics done in NumPy.
-
Basic "arange" Usage
-
The arange function does exacly what its name suggests: it generates a series of values within a fixed range. At minimum, the arange function takes one arguments: an ending value from which a range will generate from 0 to that value. The programmer can provide another argument, an ending value, and the range generated will fall between the beginning and ending values:
>>>np.arange(5)
array([0, 1, 2, 3, 4])
>>>np.arange(1, 5)
array([1, 2, 3, 4])
Stepping
-
A third argument the programmer can supply to the function is the "step" value. The step value determines what sort of intervals are in between the values returned by the arange function. Typically, without a provided argument the step value is 1. With a provided step range value, the intervals can be made smaller or larger:
>>>np.arange(3, 15, 2)
array([3, 5, 7, 9, 11, 13]) -
Controlling Data Types
-
The arange function returns a range of values based on the data type of the arguments provided. When the programmer provides the "dtype" argument in the function call, however, she can tell the function to return a different type. For example, a call to the arange function can use regular integers, but specify that it only return 8-bit integers, saving space for use with smaller numbers:
>>>np.arange(5, dtype=np.int8)
array9[0, 1, 2, 3, 4]) //integers are 8 bit integers
Arrays vs. Lists
-
In all cases, the arange function returns an array. In traditional programming, an array represents a collection of a single data type. These differ from Python lists in that Python lists can contain any data type. Furthermore, while Python lists have efficient addition and removal methods included in their structure, NumPy arrays returned by arange have their own particular set of functions, such as those that allow the programmer to do element-wise arithmetic or efficient iterations through C-Loops.
-