Open In App

Numpy ndarray.tobytes() function | Python

Improve
Improve
Like Article
Like
Save
Share
Report

numpy.ndarray.tobytes() function construct Python bytes containing the raw data bytes in the array.

Syntax : numpy.ndarray.tobytes(order=’C’)

Parameters :
order : [{‘C’, ‘F’, None}, optional] Order of the data for multidimensional arrays: C, Fortran, or the same as for the original array.

Return : Python bytes exhibiting a copy of arr’s raw data.

Code #1 :




# Python program explaining
# numpy.ndarray.tobytes() function
  
# importing numpy as geek 
import numpy as geek
  
arr = geek.array([[0, 1], [2, 3]], dtype ='<u2')
  
gfg = arr.tobytes()
  
print (gfg)


Output :

b'\x00\x00\x01\x00\x02\x00\x03\x00'

 
Code #2 :




# Python program explaining
# numpy.ndarray.tobytes() function
  
# importing numpy as geek 
import numpy as geek
  
arr = geek.array([[0, 1], [2, 3]], dtype ='<u2')
  
gfg = arr.tobytes('F')
  
print (gfg)


Output :

b'\x00\x00\x02\x00\x01\x00\x03\x00'

Last Updated : 22 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads