|
| 1 | +--- |
| 2 | +Title: '.nbytes' |
| 3 | +Description: 'Returns the total number of bytes consumed by the elements of the array.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'Attributes' |
| 10 | + - 'Memory' |
| 11 | + - 'NumPy' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`.nbytes`** attribute returns the total number of bytes consumed by the elements of a [NumPy array](https://www.codecademy.com/resources/docs/numpy/ndarray). This value is calculated as the product of the number of elements in the array (given by `.size`) and the number of bytes per element (given by `.itemsize`). |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +ndarray.nbytes |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +The `.nbytes` attribute takes no parameters. |
| 28 | + |
| 29 | +**Return value:** |
| 30 | + |
| 31 | +Returns an integer representing the total number of bytes consumed by the array elements. |
| 32 | + |
| 33 | +## Example |
| 34 | + |
| 35 | +The following example creates a one-dimensional NumPy array `arr` with 12 elements. The `.nbytes` attribute reports the total bytes used by all array elements. On a 64-bit system where the default integer type (`int64`) uses 8 bytes per element, $12 \text{ elements} \times 8 \text{ bytes}/\text{element} = 96 \text{ bytes}$: |
| 36 | + |
| 37 | +```py |
| 38 | +# Import NumPy |
| 39 | +import numpy as np |
| 40 | + |
| 41 | +# Create a NumPy array with 12 elements (default type is usually int64, or 8 bytes per item) |
| 42 | +arr = np.arange(12) |
| 43 | + |
| 44 | +# Use the '.nbytes' attribute |
| 45 | +total_bytes_nbytes = arr.nbytes |
| 46 | + |
| 47 | +print(f"Array: {arr}") |
| 48 | +print(f"Bytes per element (.itemsize): {arr.itemsize}") |
| 49 | +print(f"Total number of elements (.size): {arr.size}") |
| 50 | +print(f"Total bytes consumed (.nbytes): {total_bytes_nbytes}") |
| 51 | +``` |
| 52 | + |
| 53 | +The result will be similar to the following (the value of `arr.itemsize` might vary based on system architecture): |
| 54 | + |
| 55 | +```shell |
| 56 | +Array: [ 0 1 2 3 4 5 6 7 8 9 10 11] |
| 57 | +Bytes per element (.itemsize): 8 |
| 58 | +Total number of elements (.size): 12 |
| 59 | +Total bytes consumed (.nbytes): 96 |
| 60 | +``` |
| 61 | + |
| 62 | +## Codebyte Example |
| 63 | + |
| 64 | +The example below demonstrates a two-dimensional NumPy array `arr` with a specified data type (`float32`). Since `float32` uses 4 bytes per element and the array contains $2 \times 3 = 6$ elements, the total memory consumed is $6 \times 4 = 24$ bytes: |
| 65 | + |
| 66 | +```codebyte/python |
| 67 | +import numpy as np |
| 68 | +
|
| 69 | +# Create a 2x3 array of type float32 (4 bytes per element) |
| 70 | +arr = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32) |
| 71 | +
|
| 72 | +print(f"Array shape: {arr.shape}") |
| 73 | +print(f"Array data type: {arr.dtype}") |
| 74 | +print(f"Bytes per element (.itemsize): {arr.itemsize}") |
| 75 | +print(f"Bytes consumed by elements (.nbytes): {arr.nbytes}") |
| 76 | +``` |
0 commit comments