Python does not have built-in support for Arrays, but Python Lists can be used instead. You can import the array library at the beginning of the code import array as arr.
You can declare a variable Array like this:
Here, we created an array of float type. The letter 'd' is a type code. This determines the type of the array during creation.
Commonly used type codes:
Code |
C Type |
Python Type |
Min bytes |
'b' |
signed char |
int |
1 |
'B' |
unsigned char |
int |
1 |
'h' |
signed short |
int |
2 |
'H' |
unsigned short |
int |
2 |
'i' |
signed int |
int |
2 |
'I' |
unsigned int |
int |
2 |
'l' |
signed long |
int |
4 |
'L' |
unsigned long |
int |
4 |
'f' |
float |
float |
4 |
'd' |
double |
float |
8 |
Arrays are indexed by integers starting on position 0 and until n-1, being n the length of the Array.
Example:
Has length 5 and can be indexed like this:
0 | 1 | 2 | 3 | 4 |
Output:
Which will print out every member of Array4.
But we can also access it with negative numbers which do it from the end to the start. Like this:
-5 | -4 | -3 | -2 | -1 |
Output:
This will print the Array1 backward.
Or we can also use a for to iterate over the Array.
Output:
We can also concatenate Arrays by adding them.
Example:
Output:
We can repeat the contents by x times.
Example:
Output:
Will print the Array1 tripled.
There is a way to finding out if a specific value exists in a specific Array.
Example:
This will print out True because integer 2 indeed exists in Array1.
Iteration
Using the Membership operator, we can iterate over every member of an Array.
Output:
For each iteration of this loop, the temporary variable member will be assigned a value of the Array being printed out and passing on to the next value.
There is a way to get the size of a specific Array. Using the method len().
Example:
This code will print out the size of Array1 which is 5.
Arrays can be sliced to show only desired values or to iterate over desired indexes.
First-line will print from index 1 to 3 and the second will print starting on index 2.
Lists are much more flexible than arrays. They can store elements of different data types including string. Also, lists are faster than arrays.
Function |
Description |
E.g |
len(Array) |
It is used to calculate the length of the Array. |
len( Array1 ) will return 3 |
max(Array) |
It returns the maximum element of the Array. |
max( Array1) will return 8 |
min(Array) |
It returns the minimum element of the Array. |
min( Array1) will return 1 |
Function |
Description |
E.g |
Array.append(obj) |
The object obj is added to the Array. |
Array1 = arr.array("i", [1,2,3,4,5]) Array1.append(9) Will add the integer 9 to Array1 |
Array.count(obj) |
It returns the number of occurrences of the specified object in the Array. |
print(Array1.count(3)) It will print out 1 which is the number of times 3 is in Array1. |
Array.extend(seq) |
Object seq is extended to the Array. |
Array1.extend((7,8,9,10,11,12) It extends Array1 adding those 5 elements. |
Array.index(obj) |
It returns the lowest index in the Array that object appears. |
print(Array1.index(2)) It prints out 1 which is the index of 2. |
Array.insert(index, obj) |
The object is inserted into the Array at the specified index. |
Array1.insert(2, 18) Inserts 18 right after 2 pushing 3 and the rest to the right. |
Array.pop() |
It removes and returns the last object of the Array. |
print(Array1.pop()) Will remove the integer 5 and print it. |
Array.remove(obj) |
It removes the specified object. |
Array1.remove(3) Removes the integer 3 from Array1. |
Array.reverse() |
It reverses the Array. |
Array1.reverse() Reverses the order of Array1. |