Introduction
Before going on to the discussion about how to use NumPy, there are several information exist about it for further description. So, basically NumPy is a library exist in Python programming language. Also, NumPy is specifically the fundamental package for scientific computing in Python. Being said, it becomes a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more. Moreover, the explanation about NumPy exist in the description in this link which is the documentation page of NumPy.
How to Use NumPy
After the explanation in the previous part, this part will start with the usage of NumPy library. The usage of NumPy library will focus and start from the very basic function or usage. It is definitely just define or declare the variable for NumPy library and print it further. In a more simple way, NumPy is a Python library which is useful to work with arrays. Instead a traditional list, NumPy is a lot faster and it is also suitable for more complex computations. In the context of using NumPy, there are several requirements which must be exist before. Just read the article with the title of ‘How to Install Numpy’ in this link. It is important to have NumPy library exist in the first place.
How to Use NumPy to Define an Array
-
First of all, just run the Command Prompt in case of using a device with Microsoft Office as its operating system.
Microsoft Windows [Version 10.0.22000.856] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal>
-
After that, execute ‘python’ command to get in to the Python command console as it exist below :
Microsoft Windows [Version 10.0.22000.856] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal>python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
-
Next, continue on the previous step, just create a source code for defining variable which is using NumPy library as follows :
Microsoft Windows [Version 10.0.22000.856] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal>python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as np
How to Use NumPy to Define an Array Directly
Continue on from the previous part, this part will truly define an array using NumPy library. Without the help from any other means such as list, just pass the array into the NumPy library. Actually, NumPy library has an array which is available to create a NumPy array. Below is the continuation from the previous part which is focusing on to create a NumPy array using ‘array()’ method exist in NumPy library :
-
Just execute the following command which is a Python source code for creating NumPy array :
>>> numpy_array = np.array([1,2,3,4])
-
Next, continue the process by printing the variable which is basically a NumPy array :
>>> print(numpy_array)
[1 2 3 4]
How to Use MumPy to Define an Array from List
On the other hand, following the previous step as an alternative for defining an array, it is possible to get it from a list. Rather than passing in directly to the array() method of NumPy library, just define a list first as follows :
-
Execute the following command in a sequence. Starting from defining the list
>>> list = [1,2,3,4]
-
Continue the previous step, just pass the list variable as a parameter in the array() method as follows :
>>> list_numpy_array = np.array(list)
-
Finally, print the array variable from the NumPy library as follows :
>>> print(list_numpy_array) [1 2 3 4] >>>