Python does not have built-in support for Arrays, but Python Lists can be used instead.
Arrays are used to store multiple values in one single variable
This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you will have to import a library, like the NumPy library.
An array can hold many values under a single name, and you can access the values by referring to an index number.
To declare an array:-
cars = ["Ford", "Volvo", "BMW"]
We have now declared a variable that holds an array of strings.
Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
Accessing and Changing Elements of Array:-
You access an array element by referring to the index number.
To change the value of a specific element, refer to the index number
cars[0] = "Opel"
To find out how many elements an array has, use the length property as:
x = len(cars)
Loop through an array:-
You can use the for in loop to loop through all the elements of an array.
Syntax:-
Adding Array Elements
You can use the append() method to add an element to an array.
cars.append("Honda")
Removing Array
You can use the pop() method to remove an element from the array.
You can also use the remove() method to remove an element from the array.
cars.remove("Volvo")
There is also a "for-each" loop, which is used exclusively to loop through elements in an array
Syntax:-
Example:-
Multidimensional Arrays:-
A multidimensional array is an array of arrays.
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
myNumbers is now an array with two arrays as its elements.
To access the elements of the myNumbers array, specify two indexes: one for the array, and one for the element inside that array.
This example accesses the third element (2) in the second array (1) of myNumbers
We can also use a for loop inside another for loop to get the elements of a two-dimensional array (we still have to point to the two indexes)