Advertisement
array(typecode, initializer)
Type Code | C Type | Python Type | Minimum Size in Bytes |
---|---|---|---|
'b' | signed char |
int |
1 |
'B' | unsigned char |
int |
1 |
'b' | signed char |
int |
1 |
'u' | Py_UNICODE |
Unicode Character |
2 |
'h' | signed short |
int |
2 |
'H' | unsigned char |
int |
2 |
'i' | signed int |
int |
2 |
'I' | unsigned char |
int |
2 |
'l' | signed long |
int |
4 |
'L' | unsigned long |
int |
4 |
'f' | float |
float |
4 |
'd' | double |
float |
8 |
# Python - Example of an array
import array as arr
# Creating an array of type int
first_array = arr.array('i', [1, 5,3,4,2])
print(first_array)
# Creating an array of type float i.e. with the precision of float
second_array = arr.array('f', [1.6, 3.8, 4.6])
print(second_array)
# Creating an array of type double i.e. with the precision of double
third_array = arr.array('d', [1.6, 3.8, 4.6])
print(third_array)
array('i', [1, 5, 3, 4, 2])
array('f', [1.600000023841858, 3.799999952316284, 4.599999904632568])
array('d', [1.6, 3.8, 4.6])
Function name | Description |
---|---|
insert(i, x) | Inserts an item x at an index i within an array. |
pop(i) | Removes an item at an index i from an array and returns it. |
remove(x) | Removes the first occurrence of an element x from an array. |
reverse() | Reverses the items of an array. |
count(x) | Counts the number of occurrences of x in an array. |
index(x) | Returns the first occurrence of x in an array. |
append(x) | Appends a new item to the end of an array. |
extend(iterable) | Appends items from iterable to the end of an array, items in iterable must be of the same type as the array in question. |
Advertisement
# Python - Example of an array
import array as arr
# Creating an array of type int
first_array = arr.array('i', [1, 5, 3, 4, 2])
# Extracting the first element of an array using its index i.e 0
print(first_array[0])
# Extracting the second element of an array using its index i.e 1
print(first_array[1])
# Extracting the third element of an array using its index i.e 2
print(first_array[2])
# Extracting the four element of an array using its index i.e 3
print(first_array[3])
# Extracting the fifth element of an array using its index i.e 4
print(first_array[4])
1
5
3
4
2
# Python - Example of an array
import array as arr
# Creating an array of type int
first_array = arr.array('i', [1, 5, 3, 4, 2])
# Traversing through the array and printing its each element using a for loop
for x in first_array:
print(x)
1
5
3
4
2
# Python - Example of an array
import array as arr
# Creating an array of type int
first_array = arr.array('i', [1, 5, 3, 4, 2])
# The index of first element of an array
i = 0
# Using while loop to extract each of its element using while loop
# And len() function which takes an iterable type in its parameters i.e. list, tuple, array etc
while(i<len(first_array)):
print(first_array[i])
i = i + 1
1
5
3
4
2
# Python - Example of an array
import array as arr
# Creating an array of type int
first_array = arr.array('i', [1, 5, 3, 4, 2])
# Printing the array
print(first_array)
# Calling the insert function of array module to insert integer value 21 at index 1
first_array.insert(1, 21)
# Printing the modified array
print(first_array)
# Calling the insert function to insert integer value 19 at index 4
first_array.insert(4, 19)
# Printing the modified array again
print(first_array)
array('i', [1, 5, 3, 4, 2])
array('i', [1, 21, 5, 3, 4, 2])
array('i', [1, 21, 5, 3, 19, 4, 2])
# Python - Example of an array
import array as arr
# Creating an array of type int
first_array = arr.array('i', [1, 5, 3, 4, 2])
# Printing the array
print(first_array)
# Calling the pop function of array module to remove an item at index 1 in the array
first_array.pop(1)
# Printing the modified array
print(first_array)
# Calling the pop function of array module to remove an item at index 3 in the array
first_array.pop(3)
# Printing the modified array again
print(first_array)
array('i', [1, 5, 3, 4, 2])
array('i', [1, 3, 4, 2])
array('i', [1, 3, 4])
# Python - Example of an array
import array as arr
# Creating an array of type int
first_array = arr.array('i', [1, 5, 1, 4, 24, 4])
# Printing the array
print(first_array)
# Calling the remove function of array module to remove the first occurrence of an element 1 from array.
first_array.remove(1)
# Printing the modified array
print(first_array)
# Calling the remove function of array module to remove the first occurrence of an element 4 from array.
first_array.remove(4)
# Printing the modified array again
print(first_array)
array('i', [1, 5, 1, 4, 24, 4])
array('i', [5, 1, 4, 24, 4])
array('i', [5, 1, 24, 4])
# Python - Example of an array
import array as arr
# Creating an array of type int
first_array = arr.array('i', [1, 5, 1, 4, 24, 4])
# Calling the count() function of array module to find the total occurrence of 1 in it
print('Total occurrences of 1 : ', first_array.count(1))
# Calling the count() function of array module to find the total occurrence of 4 in it
print('Total occurrences of 4 : ', first_array.count(4))
# Calling the count() function of array module to find the total occurrence of 5 in it
print('Total occurrences of 5 : ', first_array.count(5))
Total occurrences of 1 : 2
Total occurrences of 4 : 2
Total occurrences of 5 : 1
# Python - Example of an array
import array as arr
# Creating an array of type int
first_array = arr.array('i', [1, 5, 1, 4, 24, 4])
# Printing the original array.
print(first_array)
# Calling the append() function to append 10 to the end of array
first_array.append(10)
# Printing the modified array.
print(first_array)
# Calling the append() function to append 10 to the end of array
first_array.append(99)
# Printing the modified array.
print(first_array)
array('i', [1, 5, 1, 4, 24, 4])
array('i', [1, 5, 1, 4, 24, 4, 10])
array('i', [1, 5, 1, 4, 24, 4, 10, 99])
# Python - Example of an array
import array as arr
# Creating an array of type int
first_array = arr.array('i', [1, 5, 1, 4, 24, 4])
# Creating another array of type int
second_array = arr.array('i', [75,10, 100])
# Printing the original first array.
print('First array : ', first_array)
# Printing the original second array.
print('Second array : ",second_array)
# Calling the append() function to append second array to the end of first array
first_array.extend(second_array)
# Printing the extended first array, which also contains second array at its end
print('Extended first array with second array : ', first_array)
First array : array('i', [1, 5, 1, 4, 24, 4])
Second array : array('i', [75, 10, 100])
Extended first array with second array: array('i', [1, 5, 1, 4, 24, 4, 75, 10, 100])
# Python - Example of an array
import array as arr
# Creating an array of type int
first_array = arr.array('i', [1, 5, 24, 4])
# Creating an iterable list
a_list = [ 5,7,2,2,1]
# Creating a iterable tuple
a_tuple =( 27, 17)
# Creating a iterable set
a_set ={99, 21}
# Printing the priginal first array, list, tuple and set
print('First array : ',first_array)
print('List : ', a_list)
print('Tuple : ', a_tuple)
print('Set : ', a_set)
# Calling the append() function to append the list to the end of first array
first_array.extend(a_list)
# Printing the modified first array, after extending it to add a list
print('Extended first array with a list : ', first_array)
# Calling the append() function to append the tuple to the end of first array
first_array.extend(a_tuple)
# Printing the modified first array, after extending it to add a tuple
print('Extended first array with a tuple : ', first_array)
# Calling the append() function to append the set to the end of first array
first_array.extend(a_set)
# Printing the modified first array, after extending it to add a set
print('Extended first array with a set : ', first_array)
First array : array('i', [1, 5, 24, 4])
List : [5, 7, 2, 2, 1]
Tuple : (27, 17)
Set : {99, 21}
Extended first array with a list : array('i', [1, 5, 24, 4, 5, 7, 2, 2, 1])
Extended first array with a tuple : array('i', [1, 5, 24, 4, 5, 7, 2, 2, 1, 27, 17])
Extended first array with a set : array('i', [1, 5, 24, 4, 5, 7, 2, 2, 1, 27, 17, 99, 21])
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement