Using_the_numpy.array_Function
Using_the_numpy.array_Function
array Function
numpy.array function:
The basic ndarray is created using array function in NumPy as follows –
numpy.array
1|Page
Let us make Python programs to understand the ndarray better.
import numpy as np
a = np.array([4,6,8])
print(a)
Output
[4 6 8]
This code creates a NumPy array named a, with the elements 4, 6, and
8, and then prints the array to the console.
i. import numpy as np: This line imports the NumPy library and gives
it the alias np. This is a common convention to make the code
more readable and concise. Now, you can use np instead of typing
out numpy every time you want to use a function from the library.
iii. print(a): This line prints the contents of the array a to the console.
In this case, it will output something like:
Output
[[3 4]
[9 6]]
2|Page
This code creates a 2-dimensional NumPy array named a, with
elements arranged in two rows and two columns and then prints the
array to the console.
i. import numpy as np: This line imports the NumPy library and
aliases it as np for convenience.
ii. a = np.array([[3, 4], [9, 6]]): This line creates a NumPy array
named a. The array is a 2x2 matrix, meaning it has two rows and
two columns. The values in the matrix are provided as a nested list
[[3, 4], [9, 6]].
3. Creating Array with minimum dimension: You can create an array with
a minimum dimension using the ndmin parameter when creating the
array. This parameter specifies the minimum number of dimensions that
the resulting array should have.
# minimum dimensions
import numpy as np
a = np.array([1, 2, 3,4,5], ndmin = 2)
print(a)
Output
[[1 2 3 4 5]]
3|Page
4. Creating Array of Complex Numbers: You can create an array of
complex numbers by specifying the dtype parameter as complex when
creating the array.
# dtype parameter
import numpy as np
a = np.array([1, 2, 3], dtype = complex)
print(a)
Output
[1.+0.j 2.+0.j 3.+0.j]
The dtype parameter specifies the data type of array elements. Here,
dtype=complex is used to create an array of complex numbers from
the integer values 1, 2, and 3.
4|Page