Notepad/enter/Coding Tips (Classical)/Terminal Tips/2. CLI Tools/Languages/Python/code/arrays/About arrays.md

61 lines
1.6 KiB
Markdown
Raw Normal View History

2023-07-05 18:29:11 +00:00
# Arrays in Python
Arrays in python are a largely different data structure entirely than the more commonly used [list](obsidian://open?vault=Coding%20Tips&file=Python%2Fcodes%2FLists%2FLists), and different still than the pythonic dictionary. Arrays are different than lists than in the sense that they are used for entirely different purposes as lists are more iterable and arrays have the advantage of being of fixed size.
In python3, they are most commonly called by using [numpy](obsidian://open?vault=Coding%20Tips&file=Python%2Ftools%2Fnumpy%2Fnumpy) array.
It is used by creating an ```np.array([])``` call.
---
```
#finding max of an array
```
#### 0-D array
This is simply the same as just having one object
```
import numpy as np
arr = np.array(42)
print(arr)
```
#### 1-D array
This is a more classic array that we can set of fixed size.
```
import numpy as np
arr = np.arr([1, 2, 3, 4, 5])
print(arr)
```
#### 2-D array
Arrays that have 1-D arrays as its elements and are often used to represent matrix or 2nd order tensors. Numpy has a sub module called ```numpy.mat``` to deal with such operations.
```
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
```
#### Higher DImensional arrays
Create a defined number of dimensions by using ```ndmin``` argument like so:
A 5 dimensional array:
```
import numpy as np
arr = np.array([1, 2, 3, 4], ndmin=5)
print(arr)
print('number of dimensions :', arr.ndim)
```
## To check the number of dimensions
Simply use the ```ndim ``` attribute on an object.