| Start | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Access individual elements through indexes Individual elements can be accessed through indexes. Python arrays are zero-indexed. Here is an example : |
||||||||||||||||||||||||||||
| Basic Introduction to Arrays An array is a data structure that stores values of same data type. In Python, this is the main difference between arrays and lists. While python lists can contain values corresponding to different data types, arrays in python can only contain values corresponding to same data type. In this tutorial, we will understand the Python arrays with few examples. If you are new to Python, get started with the Python Introduction article. To use arrays in python language, you need to import the standard Once you have imported the In the declaration above, Typecodes are the codes that are used to define the type of array values or the type of array. The table in the parameters section shows the possible values you can use when declaring an array and it's type. Here is a real world example of python array declaration : In the example above, typecode used is Here is a simple example of an array containing 5 integers |
||||||||||||||||||||||||||||
Append any value to the array using append() method Note that the value |
||||||||||||||||||||||||||||
| Insert value in an array using insert() method We can use the In the above example, the value 0 was inserted at index 0. Note that the first argument is the index while second argument is the value. |
||||||||||||||||||||||||||||
| Extend python array using extend() method A python array can be extended with more than one value using We see that the array my_array was extended with values from |
||||||||||||||||||||||||||||
| Add items from list into array using fromlist() method Here is an example: So we see that the values 11,12 and 13 were added from list |
||||||||||||||||||||||||||||
| Remove any array element using remove() method Here is an example : We see that the element 4 was removed from the array. |
||||||||||||||||||||||||||||
| Remove last array element using pop() method
So we see that the last element ( |
||||||||||||||||||||||||||||
| Fetch any element through its index using index() method
Note in that second example that only one index was returned, even though the value exists twice in the array |
||||||||||||||||||||||||||||
| Reverse a python array using reverse() method The |
||||||||||||||||||||||||||||
| Get array buffer information through buffer_info() method This method provides you the array buffer start address in memory and number of elements in array. Here is an example: |
||||||||||||||||||||||||||||
| Check for number of occurrences of an element using count() method
|
||||||||||||||||||||||||||||
| Convert array to string using tostring() method
|
||||||||||||||||||||||||||||
| Convert array to a python list with same elements using tolist() method When you need a Python |
||||||||||||||||||||||||||||
| Append a string to char array using fromstring() method You are able to append a string to a character array using Parameters
|
Read Article
https://develop.horje.com/learn/1434/reference