Introduction
In this article, I will explain Array in Python. Arrays are used to store one or more values in one single variable. An array is a set of items saved at contiguous reminiscence locations. The concept is to store a couple of items of the identical kind together. This makes it less difficult to calculate the position of each element.
Access the Elements of an Array.
Array element is referring to the index number.
Example
- color= ["red", "blue", "green"]
- print (color [0])
- print (color [2])
Output
First array element and last array element value.
Modifying elements
Change the array element.
Example
- color= ["red", "blue", "green"]
- color [0] ="black"
- print(color)
Output
First array element value change.
Access the array Elements in reverse order.
The array element is accessed in a reverse direction.
Example
- color= ["red", "blue", "green"]
- print (color [-1])
Output
Last array element value.
The length of an array.
The len() method is used to return the number of elements present in an array.
Syntax
Example
- color= ["red", "blue", "green"]
- print(len(color))
Output
Return the number of elements in the “color” array.
Adding array elements.
The append () method is used to add an element in the last position of the array.
Syntax
Example
- color= ["red", "blue", "green"]
- color.append("yellow")
- print(color)
Output
Add an element in the last position.
Removing array elements.
The pop () method is used to remove an element from the array.
Syntax
Example
- color= ["red", "blue", "green"]
- color.pop(1)
- print(color)
Output
Remove the element from the array.
Reverse array element.
The reverse () method is used to reverse the order of elements.
Syntax
Example
- color= ["red", "blue", "green"]
- color.reverse()
- print(color)
Output
Reverse the order of elements
Count array element.
The count () method is used to return the number of elements with the specified value.
Syntax
Example
- color= ["red", "blue", "green"]
- print(color.count("blue"))
Output
Extend array element.
The extend () method is used to add two list elements.
Syntax
Example
- color= ["red", "blue", "green"]
- animal=["rat","cat","dog"]
- color.extend(animal)
- print(color)
Output
Add two list elements.
Conclusion
In this article, we have seen the Array in Python. I hope this article was useful to you. Thanks for reading!