Other than some generic containers like list, Python in its definition can also handle containers with specified data types. Array can be handled in python by module named “array“, like we used to store in other programming language. They can be useful when we have to manipulate only a specific data type values.
Operations on Array :
1. array(data type, value list) :- This function is used to create an array with data type and value list specified in its arguments. Some of the data types are mentioned in the table below.
Lets have a look into them
2. append() :- This function is used to add the value mentioned in its arguments at the end of the array.
3. insert(i,x) :- This function is used to add the value at the position specified in its argument.
import
array
arr
=
array.array(
'i'
, [
1
,
2
,
3, 4
])
print
(
"The new created array is : "
,end
=
" "
)
for
i
in
range
(
0
,
3
):
print
(arr[i], end
=
" "
)
print
(
"\r"
)
arr.append(
8
);
print
(
"The appended array is : "
, end
=
"")
for
i
in
range
(
0
,
4
):
print
(arr[i], end
=
" "
)
arr.insert(
2
,
5
)
print
(
"\r"
)
print
(
"The array after insertion is : "
, end
=
"")
for
i
in
range
(
0
,
5
):
print
(arr[i], end
=
" "
)
output :
The new created array is : 1 2 3
The appended array is : 1 2 3 4
The array after insertion is : 1 2 5 3 4
4. pop() :- This function removes the element at the position mentioned in its argument, and returns it.
5. remove() :- This function is used to remove the first occurrence of the value mentioned in its arguments.
import
array
arr
=
array.array(
'i'
,[
1
,
2
,
3
,
1
,
5
])
print
(
"The new created array is : "
,end
=
"")
for
i
in
range
(
0
,
5
):
print
(arr[i],end
=
" "
)
print
(
"\r"
)
print
(
"The popped element is : "
,end
=
"")
print
(arr.pop(
2
));
print
(
"The array after popping is : "
,end
=
"")
for
i
in
range
(
0
,
4
):
print
(arr[i],end
=
" "
)
print
(
"\r"
)
arr.remove(
1
)
print
(
"The array after removing is : "
,end
=
"")
for
i
in
range
(
0
,
3
):
print
(arr[i],end
=
" "
)
output :
The new created array is : 1 2 3 1 5
The popped element is : 3
The array after popping is : 1 2 1 5
The array after removing is : 2 1 5
6. index() :- This function returns the index of the first occurrence of value mentioned in arguments.
7. reverse() :- This function reverses the array.
import
array
arr
=
array.array(
'i'
,[
1
,
2
,
3
,
1
,
2
,
5
])
print
(
"The new created array is : "
,end
=
"")
for
i
in
range
(
0
,
6
):
print
(arr[i],end
=
" "
)
print
(
"\r"
)
print
(
"The index of 1st occurrence of 2 is : "
,end
=
"")
print
(arr.index(
2
))
arr.reverse()
print
(
"The array after reversing is : "
,end
=
"")
for
i
in
range
(
0
,
6
):
print
(arr[i],end
=
" "
)
output :
The new created array is : 1 2 3 1 2 5
The index of 1st occurrence of 2 is : 1
The array after reversing is : 5 2 1 3 2 1
In this article i have given an introduction on array.
Next Article : Array in Python Set 2
#machine learning using python
#matplotlib
#python 3
#scikit learn
#tkinter
#python training
#python data analysis
#best python tutorial
#python classes near me
#learn python for beginners
#python online training
#django course
#advanced python course
#python course fees
#introduction to data science in python
#free python tutorial
#learn python basics
2 Comments
I would love to see you write more of such informative things regarding python. Very nice and impressive work :D
ReplyDeleteinformative article
ReplyDeleteclick here
If you have any doubt, Please let me know.