How to use Python Arrays – with example

  • Reading time:16 mins read

What is a Python array?

A Python array is a data structure in python that can hold multiple elements of the same type at the same time. For example, they all have to be integers or numbers or strings of characters.

 

 

Creating a Python array

Python arrays do not have inbuilt support in python, therefore we need to import the array module into our workspace in order to use Python arrays. This can be done using the statement “import array as arr”. Note that we have used “arr” as an alias since it is easier to type.

This lets us have access to class data objects from the python standard library. The next thing that we need to do is define a name for our array. So in this article, we are just going to name it ‘abc’ for lack of a better word. The next thing we need to do is we need to access the class that we imported as “arr”.

We are now going to type the word array, the next thing that comes is quite important. So the whole thing is in the brackets.

 

Learn how to use Python Arrays online

 

However, the first letter or the first thing that we place inside the parentheses denotes the data type that is going to go into this specific array.

There are several types of code each representing different data types that can be contained in an array, in this case, we are going to use “d”. Which represents a double float type of data. 

The next thing we need is the comma and now we can use the square brackets to actually input the values that we want into this array. So because these are all floats, we are going to be using decimal point numbers. So let’s just do 2.5 and then a comma 3.6, another/comma 5.6 And then let’s do 11.5 

We have just created an array containing floating-point numbers. Now we can just print the whole thing out, and we expect to get an array as the output as shown below.

 

 

Iterating a Python array

Using a for loop we can go through all the values in an array and print them all then display them on a screen. We can simply do that by saying for i in abc print(i).

 

Indexing a Python array

Now it’s worth noting we can actually index arrays as well. So we can say “print(abc[0])”. This lets us print the first item which should be 2.5 and then the 3rd one which is 11.5. We may also slice with minus numbers, so [-1] will be the end number.

 

 

Python arrays are useful tools while coding

 

Inserting values into a Python array

So to insert a new value into our array, we simply need to do our array name, which is abc and then dot insert, open up brackets. And now we need to choose the location that we’re going to insert into our array just by using the indexing that we have just covered above. Let’s do zero because that is where we want to put it right at the very beginning and then the value that we intend to insert which is 1.8 and then going to print out the whole of the array again.

 


 

In the output above we can see that 1.8 has been added right at the front. If we try to input a character, let’s say ‘h‘ it’s going to come back and it’s going to give us an error because it must be a real number which we have chosen when we added ‘d’. 

 

 

If we wanted to modify a value that is in our array, we can do that by saying we want abc to determine which array we want to change. And then we’re going to say our index and this time we’re going to use a square bracket. So we’re going to say we want to modify the first index, And then we’re going to change the value 1.8 to the value of 1.9. And then let’s print out the array once again.

 

 

In the second line of the output, we have successfully changed the first element of the array from 1.8 to 1.9.

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.