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.
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.
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
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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.
