Python Arrays real Use Showed with Examples

Firstly, you need to install Python. Secondly, do install NUMPY using the “import NumPy as np” command. Make sure Numpy installed correctly.


I am very enthusiastic to try new things. Using Numpy I did a small experiment. As I have mentioned the ‘NumPy’ is a python package used very much in data analytics.

List of Python Array Features

  1. You can create an array
  2. Mathematical operations like-add, Subtract, Multiply, and Division you can do on arrays
  3. You can join two arrays
  4. You can Copy one array to other
  5. Read and Write of Array data onto Files
  6. You can create Structured Arrays
  7. Vectorization of array elements also possible
  8. You can covert an array of “m” dimensions into “n” dimensions

1). How to Declare (Create) an Array

Using Python array function, now I am defining an array

array()
>>>a = np.array([1,2,3,5])
>>>a
>>>type(a)

The type() function tells you the type of an array.

Create an array

How to get Dimension of an Array in Python. Explained with the best example.

Array dimensions

Also You May Like : Top Libraries You Need for Data Analytics


2). How to Use Mathematical Operations on Arrays

You can do Addition, Subtraction, Multiply, Division on arrays

Array Mathematical Operations.

You can also get the index of each element. Index starts with ‘0’. So, to get an element of the second index is ‘3’.

index of an array

3). How to JOIN Two Arrays

You can join arrays either horizontal stacking or vertical stacking. Use vstack() or hstack().

joining the arrays

4). How to Copy Data from One Array to Other

You can copy data of one array to another. Explained the best example.

copy of an array to another

5). How to Write Data From an Array to a File

You can write data of an array into a file. There are two functions. One is save() and the other one is load().

The file which is saved will have the extension’.npy’

How to Save Array data to a file

6). How to Create a Structured Array

Creating more complex arrays is called “structured arrays”. In an array, you can give of value with any data type.

structured arrays

7). What is an Array Vectorization

Vectorization is the absence of an explicit loop during the development of the code. These loops actually cannot be omitted, but are implemented internally and then are replaced by other constructs in the code.

The application of vectorization leads to a more concise and readable code, and you can say that it will appear more “Pythonic” in its appearance.

In fact, thanks to the vectorization, many operations take on a more mathematical expression. For example, NumPy allows you to express the multiplication of two arrays as shown:

a * b
Or even two matrices:
A * B

8). How to Modify Dimension of an Array

I want to tell you the best example. Suppose you created an array of 4×4. You can split the same array as 2×2. This is called change of array dimension.

splitting arrays

Related Posts

Python Interview Questions

Don’t Forget to Share This Post!

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.