User guide

NumPy User Guide, Release 1.9.0
>>> np.issubdtype(d, float)
False
2.1.2 Array Scalars
Numpy generally returns elements of arrays as array scalars (a scalar with an associated dtype). Array scalars differ
from Python scalars, but for the most part they can be used interchangeably (the primary exception is for versions
of Python older than v2.x, where integer array scalars cannot act as indices for lists and tuples). There are some
exceptions, such as when code requires very specific attributes of a scalar or when it checks specifically whether a
value is a Python scalar. Generally, problems are easily fixed by explicitly converting array scalars to Python scalars,
using the corresponding Python type function (e.g., int, float, complex, str, unicode).
The primary advantage of using array scalars is that they preserve the array type (Python may not have a matching
scalar type available, e.g. int16). Therefore, the use of array scalars ensures identical behaviour between arrays and
scalars, irrespective of whether the value is inside an array or not. NumPy scalars also have many of the same methods
arrays do.
2.2 Array creation
See Also:
Array creation routines
2.2.1 Introduction
There are 5 general mechanisms for creating arrays:
1. Conversion from other Python structures (e.g., lists, tuples)
2. Intrinsic numpy array array creation objects (e.g., arange, ones, zeros, etc.)
3. Reading arrays from disk, either from standard or custom formats
4. Creating arrays from raw bytes through the use of strings or buffers
5. Use of special library functions (e.g., random)
This section will not cover means of replicating, joining, or otherwise expanding or mutating existing arrays. Nor will
it cover creating object arrays or record arrays. Both of those are covered in their own sections.
2.2.2 Converting Python array_like Objects to Numpy Arrays
In general, numerical data arranged in an array-like structure in Python can be converted to arrays through the use of
the array() function. The most obvious examples are lists and tuples. See the documentation for array() for details for
its use. Some objects may support the array-protocol and allow conversion to arrays this way. A simple way to find
out if the object can be converted to a numpy array using array() is simply to try it interactively and see if it works!
(The Python Way).
Examples:
>>> x = np.array([2,3,1,0])
>>> x = np.array([2, 3, 1, 0])
>>> x = np.array([[1,2.0],[0,0],(1+1j,3.)]) # note mix of tuple and lists,
2.2. Array creation 11