Tutorial by Examples

An array is a data structure that stores values of same data type. In Python, this is the main difference between arrays and lists. While python lists can contain values corresponding to different data types, arrays in python can only contain values corresponding to same data type. In this tutorial...
Individual elements can be accessed through indexes. Python arrays are zero-indexed. Here is an example : my_array = array('i', [1,2,3,4,5]) print(my_array[1]) # 2 print(my_array[2]) # 3 print(my_array[0]) # 1
my_array = array('i', [1,2,3,4,5]) my_array.append(6) # array('i', [1, 2, 3, 4, 5, 6]) Note that the value 6 was appended to the existing array values.
We can use the insert() method to insert a value at any index of the array. Here is an example : my_array = array('i', [1,2,3,4,5]) my_array.insert(0,0) #array('i', [0, 1, 2, 3, 4, 5]) In the above example, the value 0 was inserted at index 0. Note that the first argument is the index while se...
A python array can be extended with more than one value using extend() method. Here is an example : my_array = array('i', [1,2,3,4,5]) my_extnd_array = array('i', [7,8,9,10]) my_array.extend(my_extnd_array) # array('i', [1, 2, 3, 4, 5, 7, 8, 9, 10]) We see that the array my_array was extended...
Here is an example: my_array = array('i', [1,2,3,4,5]) c=[11,12,13] my_array.fromlist(c) # array('i', [1, 2, 3, 4, 5, 11, 12, 13]) So we see that the values 11,12 and 13 were added from list c to my_array.
Here is an example : my_array = array('i', [1,2,3,4,5]) my_array.remove(4) # array('i', [1, 2, 3, 5]) We see that the element 4 was removed from the array.
pop removes the last element from the array. Here is an example : my_array = array('i', [1,2,3,4,5]) my_array.pop() # array('i', [1, 2, 3, 4]) So we see that the last element (5) was popped out of array.
index() returns first index of the matching value. Remember that arrays are zero-indexed. my_array = array('i', [1,2,3,4,5]) print(my_array.index(5)) # 5 my_array = array('i', [1,2,3,3,5]) print(my_array.index(3)) # 3 Note in that second example that only one index was returned, even though...
The reverse() method does what the name says it will do - reverses the array. Here is an example : my_array = array('i', [1,2,3,4,5]) my_array.reverse() # array('i', [5, 4, 3, 2, 1])
This method provides you the array buffer start address in memory and number of elements in array. Here is an example: my_array = array('i', [1,2,3,4,5]) my_array.buffer_info() (33881712, 5)
count() will return the number of times and element appears in an array. In the following example we see that the value 3 occurs twice. my_array = array('i', [1,2,3,3,5]) my_array.count(3) # 2
tostring() converts the array to a string. my_char_array = array('c', ['g','e','e','k']) # array('c', 'geek') print(my_char_array.tostring()) # geek
When you need a Python list object, you can utilize the tolist() method to convert your array to a list. my_array = array('i', [1,2,3,4,5]) c = my_array.tolist() # [1, 2, 3, 4, 5]
You are able to append a string to a character array using fromstring() my_char_array = array('c', ['g','e','e','k']) my_char_array.fromstring("stuff") print(my_char_array) #array('c', 'geekstuff')

Page 1 of 1