An array is a special variable, that can hold more than one value at a time. An array can hold many values under a single name and you can access the values by referring to an index number.
Python does not have built-in support for Arrays, but python lists can be used instead.
Access the elements of an array
You refer to an array element by referring to the index number.
Get the value of the first array item:
1 | x = names[0] |
Modify the value of the first array item:
1 | names[0] = "Troposal" |
Adding Array elements
You can use the append() method to add an element to an array.
1 | names.append("David") |
Removing array Elements
you can use the pop() method to remove an element from the array.
1 | names.pop(1) |
you can also use the remove() method to remove an element from the array.
Looping Array Elements
You can use the for loop to print the array elements.
1 2 | for x in cars: print(x) |
List of Array Methods
append(): adds an element at end of the list.
clear(): Remove all the elements from the list.
copy(): Return the copy of the list.
count(): It returns the specific element count.
index(): It returns the index of the element.
insert(): Adds an element at the specified position.
pop(): Removes the element at the specified position.
remove(): Removes the first item with the specified value.
reverse(): Reverses the order of the list.
sort(): Sorts the list.
Len(): It returns the elements count of the list.