
Array in python -operation-basic concept & structure
The array is a container, it defines a collection of items that modules refer to as an object value. The array is a basic value that represents characters, integers, floating-point numbers. This is a collection of numbers that can hold a fixed number of items that should be the same type.
Array work as sequence type data and behave like lists. Arrays are most popular because they work with C, C++, JavaScript programming languages.
In this article, you will learn the basic concept of arrays, how to create arrays by using python and when you need to use them.
What is Array in Python?
An array is a special data structure that can hold more than one value at a time. It is a collection of elements of the same type.
Example of Array in python –
a=arr.array ( ‘d’[1.2, 1,3, 2.3] )
If needed you can loop array items easily and collect the required values to specify the index number. Arrays are always changeable to various manipulations required.
Creating Array in Python
When you want to create an array of numeric values, then you need to import the array modules.
Example of this array modules-
import array as arr
x = arr.array(‘d’, [1.1, 3.5, 4.5])
print(x)
The output of this –
This array imports two parameters that are data type and data list of values. Remember that point arr is the alias name. If you want you can import an array without an alias name.
Like these-
# Import array as arr
# from array import
Syntax of array in python-
x=arr.array(data type, value list) [Note- this use when you want to import alias ]
x=array(daa type, value list) [Note- it use when you import array without alias]
Example of array with alias –
x=arr.array(‘d’[1.4,1.5, 2.4])
Here including ‘d’ is a data type that helps to specify values of the next parameters.
Type code | Data Type in Python | Byte size |
i | int | 2 |
l | int | 2 |
u | Unicode character | 2 |
h | int | 2 |
H | int | 2 |
l | int | 4 |
L | int | 4 |
f | float | 4 |
d | float | 8 |
Basic array operation in python
There are five kinds of array in python that are
- Adding/ changing array –
using this when you append() .extend() and insert(i.x) functions. Append() function is used when you need some single element and end any array.
Example of this array-
x=arr.array (‘d’[1.2, 1.3, 2.1])
x.append(2.4)
print(x)
Output of these code-\
array(‘d’[1.2, 1.3, 2.1, 2.4])
- Concatenating array-
Any array will be concentrated using + symbols.
Example of concatenating array –
x=arr.array(‘d’[1.1, 2.1, 3.1, 2.6, 7.8])
y=arr.array(‘’d)[3.7, 8.6]
z=arr.array(‘d’)
z=x+y
print=(“array c=”, c)
Output of these code –
Array c = array(‘d’, [1.1, 2.1, 3.1, 2.6, 7.8, 3.7, 8.6])
- Removing/deleting array –
Remove and deleting basically remind the same but they don’t work the same. Array elements can be used pop() and remove() methods.
Example of these-
a=arr.array(‘d’[1.1, 2.2, 3.8, 3.1, 3.8, 3.7, 1.2, 4.6 ])
print(a.pop())
print(a.pop(3))
Output of this-
4.6
3.1
- Slicing array –
Arrays can always be used to symbolize that range of elements.
Example of these code –
x=arr.array(‘d’[1.1, 2.1, 3.1, 2.6, 7.8])
Output of these-
array(‘d’, [1.1, 2.1, 3.1])
- Looping array –
Using the loop through the an array that like –
x=arr.array(‘d’[1.2, 2.3, 3.8, 3.1, 3.8, 1.2, 4.6])
print(“All values”)
For a in x:
print(a)
print(“specific values ”)
For a in x [1:3] :
print(a)
Output of the values-
All values-
1.2, 2.3, 3.8, 3.1, 3.8, 1.2, 4.6, specific values, 2.3, 3.8
Others array methods in python language-
# Clear – used these elements remove all list
# Copy – get a set of of copy of lists
# Count – get specify values of the array lists
# append- include elements at the end of array lists
# index – get the index of first specific values
# sort- sorting the array lists
In this article, you will learn more about what has been shared with you today. Learn to continue python, stay here..