4.3. Attributes Values — Python
4.3.1. SetUp
4.3.2. Size
Number of elements
>>> a = np.array([1, 2, 3]) >>> >>> a.size 3
>>> b = np.array([[1, 2, 3], ... [4, 5, 6]]) >>> >>> b.size 6
>>> c = np.array([[1, 2, 3], ... [4, 5, 6], ... [7, 8, 9]]) >>> >>> c.size 9
>>> d = np.array([[[ 1, 2, 3], ... [ 4, 5, 6], ... [ 5, 6, 7]], ... ... [[11, 22, 33], ... [44, 55, 66], ... [77, 88, 99]]]) >>> >>> d.size 18
4.3.3. Shape
>>> a = np.array([1, 2, 3]) >>> >>> a.shape (3,)
>>> b = np.array([[1, 2, 3], ... [4, 5, 6]]) >>> >>> b.shape (2, 3)
>>> c = np.array([[1, 2, 3], ... [4, 5, 6], ... [7, 8, 9]]) >>> >>> c.shape (3, 3)
>>> d = np.array([[[ 1, 2, 3], ... [ 4, 5, 6], ... [ 5, 6, 7]], ... ... [[11, 22, 33], ... [44, 55, 66], ... [77, 88, 99]]]) >>> >>> d.shape (2, 3, 3)
4.3.4. NDim
Number of Dimensions
len(ndarray.shape)
>>> a = np.array([1, 2, 3]) >>> >>> a.ndim 1
>>> b = np.array([[1, 2, 3], ... [4, 5, 6]]) >>> >>> b.ndim 2
>>> c = np.array([[1, 2, 3], ... [4, 5, 6], ... [7, 8, 9]]) >>> >>> c.ndim 2
>>> d = np.array([[[ 1, 2, 3], ... [ 4, 5, 6], ... [ 5, 6, 7]], ... ... [[11, 22, 33], ... [44, 55, 66], ... [77, 88, 99]]]) >>> >>> d.ndim 3
4.3.5. Length
Number of elements in first dimension
ndarray.shape[0]
>>> import numpy as np >>> >>> >>> a = np.array([1, 2, 3]) >>> >>> len(a) 3
>>> b = np.array([[1, 2, 3], ... [4, 5, 6]]) >>> >>> len(b) 2
>>> c = np.array([[1, 2, 3], ... [4, 5, 6], ... [7, 8, 9]]) >>> >>> len(c) 3
>>> d = np.array([[[ 1, 2, 3], ... [ 4, 5, 6], ... [ 5, 6, 7]], ... ... [[11, 22, 33], ... [44, 55, 66], ... [77, 88, 99]]]) >>> >>> len(d) 2
4.3.6. Recap
4.3.7. Assignments
# %% About # - Name: Numpy Attributes # - Difficulty: easy # - Lines: 7 # - Minutes: 5 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 1. Define `result: dict` with: # - number of dimensions; # - number of elements; # - data type; # - element size; # - shape; # - strides. # 2. Run doctests - all must succeed # %% Polish # 1. Zdefiniuj `result: dict` z: # - liczbę wymiarów, # - liczbę elementów, # - typ danych, # - rozmiar elementu, # - kształt, # - przeskoki (strides). # 2. Uruchom doctesty - wszystkie muszą się powieść # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> assert type(result) is dict, \ 'Variable `result` has an invalid type; expected: `dict`.' >>> result # doctest: +NORMALIZE_WHITESPACE {'number of dimensions': 2, 'number of elements': 6, 'data type': dtype('float64'), 'element size': 8, 'shape': (2, 3), 'strides': (24, 8)} """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports import numpy as np # %% Types result: dict[str, int|tuple[int,...]|np.dtype] # %% Data DATA = np.array([[-1.1, 0.0, 1.1], [2.2, 3.3, 4.4]]) # %% Result result = { 'number of dimensions': ..., 'number of elements': ..., 'data type': ..., 'element size': ..., 'shape': ..., 'strides': ..., }