Name
zeros
Synopsis
zeros(shapetuple
,typecode
=Int,savespace
=False)
Returns an array a
such that
a
.shape==
shapetuple
. All of
a
’s elements are
0
.
Note that, by default, identity
,
ones
, and zeros
all return
arrays whose type is Int
. Be sure to specify
explicitly a different type code, such as Float
,
if that is what you really want. For example, be sure to avoid the
following common mistake:
a = zeros(3) a[0] = 0.3 # a is array([0,0,0])
Since a
is Int
in this snippet,
the 0.3
we assign to one of its items gets
truncated to the integer 0
. Instead, you typically
want something closer to the following:
a = zeros(3,Float) a[0] = 0.3 # a is array([0.3,0.,0.])
Here, we have explicitly specified Float
as the
type code for a
, and therefore no truncation
occurs when we assign 0.3
to one of
a
’s items.
Get Python in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.