From 73f61f23f176602903f9080933da8b0a0f196c42 Mon Sep 17 00:00:00 2001 From: Ananya Date: Fri, 24 Oct 2025 22:03:22 +0530 Subject: [PATCH 1/6] added nbytes.md --- .../concepts/ndarray/terms/nbytes/nbytes.md | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 content/numpy/concepts/ndarray/terms/nbytes/nbytes.md diff --git a/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md b/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md new file mode 100644 index 00000000000..049e4c7de32 --- /dev/null +++ b/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md @@ -0,0 +1,76 @@ +--- +Title: '.nbytes' +Description: 'The total number of bytes consumed by the elements of the array.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'NumPy' + - 'Attributes' + - 'Arrays' + - 'Memory' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +The **`.nbytes`** attribute returns the total number of bytes consumed by the elements of a NumPy array. 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`**). + +## Syntax + +```pseudo +ndarray.nbytes +```` + + - `ndarray`: The NumPy array whose memory consumption is to be determined. + - `.nbytes` is an **attribute**, not a method, so it does not take any arguments or require parentheses. + +----- + +## Example + +In this example, we create a one-dimensional NumPy array `arr` with 12 elements. We then use the `.nbytes` attribute to determine the total bytes consumed by the array's elements. Assuming a standard 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}$. + +```py +# Import NumPy +import numpy as np + +# Create a NumPy array with 12 elements (default type is usually int64, or 8 bytes per item) +arr = np.arange(12) + +# Use the '.nbytes' attribute +total_bytes_nbytes = arr.nbytes + +print(f"Array: {arr}") +print(f"Bytes per element (.itemsize): {arr.itemsize}") +print(f"Total number of elements (.size): {arr.size}") +print(f"Total bytes consumed (.nbytes): {total_bytes_nbytes}") +``` + +We will get a result similar to the following (the value of `arr.itemsize` might vary based on system architecture): + +```shell +Array: [ 0 1 2 3 4 5 6 7 8 9 10 11] +Bytes per element (.itemsize): 8 +Total number of elements (.size): 12 +Total bytes consumed (.nbytes): 96 +``` + +----- + +## Codebyte Example + +In the following codebyte example, a two-dimensional NumPy array `arr` is created with a specified data type (`float32`), and then the `.nbytes` attribute is used to report its memory consumption. Since `float32` uses 4 bytes per element, and the array has $2 \times 3 = 6$ elements, the total memory consumed is $6 \times 4 = 24$ bytes. + +```codebyte/python +import numpy as np + +# Create a 2x3 array of type float32 (4 bytes per element) +arr = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32) + +print(f"Array shape: {arr.shape}") +print(f"Array data type: {arr.dtype}") +print(f"Bytes per element (.itemsize): {arr.itemsize}") +print(f"Bytes consumed by elements (.nbytes): {arr.nbytes}") +``` + From fa10ee16255615a0b087aa5027e06934e565753f Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 25 Oct 2025 13:22:38 +0530 Subject: [PATCH 2/6] Revise .nbytes documentation for clarity and detail Updated the description and parameters for the .nbytes attribute in the NumPy documentation. Improved clarity and consistency in examples. --- .../concepts/ndarray/terms/nbytes/nbytes.md | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md b/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md index 049e4c7de32..9b29e77c840 100644 --- a/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md +++ b/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md @@ -1,35 +1,39 @@ --- Title: '.nbytes' -Description: 'The total number of bytes consumed by the elements of the array.' +Description: 'Returns the total number of bytes consumed by the elements of the array.' Subjects: - 'Code Foundations' - 'Computer Science' Tags: - - 'NumPy' - - 'Attributes' - 'Arrays' + - 'Attributes' - 'Memory' + - 'NumPy' CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -The **`.nbytes`** attribute returns the total number of bytes consumed by the elements of a NumPy array. 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`**). +The **`.nbytes`** attribute returns the total number of bytes consumed by the elements of a NumPy array. 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`). ## Syntax ```pseudo ndarray.nbytes -```` +``` - - `ndarray`: The NumPy array whose memory consumption is to be determined. - - `.nbytes` is an **attribute**, not a method, so it does not take any arguments or require parentheses. +**Parameters:** ------ +The `.nbytes` attribute takes no parameters. + +**Return value:** + +Returns an integer representing the total number of bytes consumed by the array elements. ## Example -In this example, we create a one-dimensional NumPy array `arr` with 12 elements. We then use the `.nbytes` attribute to determine the total bytes consumed by the array's elements. Assuming a standard 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}$. +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}$: ```py # Import NumPy @@ -56,11 +60,9 @@ Total number of elements (.size): 12 Total bytes consumed (.nbytes): 96 ``` ------ - ## Codebyte Example -In the following codebyte example, a two-dimensional NumPy array `arr` is created with a specified data type (`float32`), and then the `.nbytes` attribute is used to report its memory consumption. Since `float32` uses 4 bytes per element, and the array has $2 \times 3 = 6$ elements, the total memory consumed is $6 \times 4 = 24$ bytes. +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: ```codebyte/python import numpy as np @@ -73,4 +75,3 @@ print(f"Array data type: {arr.dtype}") print(f"Bytes per element (.itemsize): {arr.itemsize}") print(f"Bytes consumed by elements (.nbytes): {arr.nbytes}") ``` - From 6c5a766b9751d754fe3e80d6f2246e824cc1a51a Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 25 Oct 2025 13:25:03 +0530 Subject: [PATCH 3/6] Enhance `.nbytes` description with documentation link Updated the description of the `.nbytes` attribute to include a link to the NumPy array documentation. --- content/numpy/concepts/ndarray/terms/nbytes/nbytes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md b/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md index 9b29e77c840..2ed8f2de8be 100644 --- a/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md +++ b/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -The **`.nbytes`** attribute returns the total number of bytes consumed by the elements of a NumPy array. 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`). +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`). ## Syntax From 21b7ac618f8ceb2888fa45086abe7b8883f89683 Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Wed, 29 Oct 2025 21:38:36 +0530 Subject: [PATCH 4/6] Apply suggestion from @avdhoottt --- content/numpy/concepts/ndarray/terms/nbytes/nbytes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md b/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md index 2ed8f2de8be..f7b640662f3 100644 --- a/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md +++ b/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md @@ -33,6 +33,7 @@ Returns an integer representing the total number of bytes consumed by the array ## Example 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}$: ```py From 1a72706b10644344684b3766bc7a40f16bbe0101 Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Wed, 29 Oct 2025 21:39:39 +0530 Subject: [PATCH 5/6] Apply suggestion from @avdhoottt --- content/numpy/concepts/ndarray/terms/nbytes/nbytes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md b/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md index f7b640662f3..58a3fabf519 100644 --- a/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md +++ b/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md @@ -52,7 +52,7 @@ print(f"Total number of elements (.size): {arr.size}") print(f"Total bytes consumed (.nbytes): {total_bytes_nbytes}") ``` -We will get a result similar to the following (the value of `arr.itemsize` might vary based on system architecture): +The result will be similar to the following (the value of `arr.itemsize` might vary based on system architecture): ```shell Array: [ 0 1 2 3 4 5 6 7 8 9 10 11] From 1b38112e8e3ffa21e7a46bc17e1274831a07c145 Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Wed, 29 Oct 2025 21:40:23 +0530 Subject: [PATCH 6/6] Apply suggestion from @avdhoottt --- content/numpy/concepts/ndarray/terms/nbytes/nbytes.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md b/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md index 58a3fabf519..b8852ebe500 100644 --- a/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md +++ b/content/numpy/concepts/ndarray/terms/nbytes/nbytes.md @@ -32,9 +32,7 @@ Returns an integer representing the total number of bytes consumed by the array ## Example -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}$: +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}$: ```py # Import NumPy