Updated On : Jun-25,2020 Time Investment : ~10 mins

Learning Numpy - Simple Tutorial For Beginners - NumPy - Array Attributes Part 4

Here we would be learning about the attributes of the array.

Table Of Contents

  1. ndarray.shape
  2. ndarray.ndim
  3. numpy.itemsize
  4. numpy.flags


This array attribute returns a tuple consisting of array dimensions. It can also be used to resize the array.

Example 1

import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print (a.shape)
(2, 3)

Example 2

# this resizes the ndarray 
import numpy as np

a = np.array([[1,2,3],[4,5,6]])
a.shape = (3,2)
print (a)
[[1 2]
 [3 4]
 [5 6]]

Example 3

NumPy also provides a reshape function to resize an array.

import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = a.reshape(3,2)
print (b)
[[1 2]
 [3 4]
 [5 6]]


This array attribute returns the number of array dimensions.

Example 1

# an array of evenly spaced numbers 
import numpy as np
a = np.arange(24)
print (a)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]

Example 2

# this is one dimensional array 
import numpy as np
a = np.arange(24)

# now reshape it 
b = a.reshape(2,4,3)
print (b)
# b is having three dimensions
[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]
  [ 9 10 11]]

 [[12 13 14]
  [15 16 17]
  [18 19 20]
  [21 22 23]]]


This array attribute returns the length of each element of array in bytes.

Example 1

# dtype of array is int8 (1 byte) 
import numpy as np
x = np.array([1,2,3,4,5], dtype = np.int8)
print (x.itemsize)

Example 2

# dtype of array is now float32 (4 bytes) 
import numpy as np
x = np.array([1,2,3,4,5], dtype = np.float32)
print (x.itemsize)


The ndarray object has the following attributes. Its current values are returned by this function.

Example 1

import numpy as np
x = np.array([1,2,3,4,5])
print (x.flags)
  OWNDATA : True
  ALIGNED : True
Attribute Description
C_CONTIGUOUS (C) The data is in a single, C-style contiguous segment
F_CONTIGUOUS (F) The data is in a single, Fortran-style contiguous segment
OWNDATA (O) The array owns the memory it uses or borrows it from another object
WRITEABLE (W) The data area can be written to. Setting this to False locks the data, making it read-only
ALIGNED (A) The data and all elements are aligned appropriately for the hardware
UPDATEIFCOPY (U) This array is a copy of some other array. When this array is deallocated, the base array will be updated with the contents of this array
Dolly Solanki  Dolly Solanki

YouTube Subscribe Comfortable Learning through Video Tutorials?

If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.

Need Help Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.

Subscribe to Our YouTube Channel

YouTube SubScribe

Newsletter Subscription