Doubt on the syntax of zeros method
Title
Question
Mam why do we have to use 2 brackets in the zeros method for giving only 1 set of values to it? I have tried it with one braces(opening and closing) then it is giving error. My OS used : Windows 10 ==>
TypeError Traceback (most recent call last)
<ipython-input-3-4b18b0e09899> in <module>
----> 1 b=np.zeros(2,2)
TypeError: data type not understood
Python-3.4.3 Getting-started-with-arrays 07-08 min 10-20 sec
Answers:
np.zeros accepts the shape of the array i.e. the number of rows and columns of the array as an argument. So it should be either a tuple or an integer value.
For example:
Executing np.zeros((2, 3)) gives
array([[0., 0., 0.],
[0., 0., 0.]])
which is a 2x3 array.
Executing np.zeros(2) gives
array([0., 0.])
which is a 1x2 array.
A tuple is one of the data structures in python. The tuples<span style="font-family: arial, sans-serif; font-size: 14px;"> are sequences, just like lists or arrays.</span>
You will learn more about tuple in the Sequence datatypes tutorial.
Login to add comment