Blog

Python Array Examples

  • (4.0)
  • | 1519 Ratings |
  • Last Updated September 12, 2017

Python is a powerful programming language that has started regaining its fame for its usage in the Data Science along with the latest technologies like R and etc. Having said that, let us take a look at the tiny winy bits of concepts to get ourselves stronger in this programming language.

In this article, we will try to understand the concept of arrays in Python programming language. To provide you a better understanding on what arrays can be used for in Python programming language, take a look at the following section.

Python Array Examples

Python arrays

Arrays are by far the most common data structures that could be used in any programming language. Python as such doesn’t support an array notation by default but relies on the list structure to be used as a multidimensional array. This article will help you provide all the details that are needed for you to make your understanding clearer on how the list structures are used as an array in Python. Since this article is to deal with the usage of arrays in Python, but it is worth nothing to use NumPy for hardcore array usage, library provided for specific usage on Arrays.

Learn how to use Python, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Python Training Demo!

Let us look at the array class provided by Python programming language:

Class array.array(typecode[,initializer])

A new array of items that are restricted by the typecode, and can be initialized by an optional initializer values which can be a list or a string or an iterable over elements of an appropriate type. Now let’s look at some of the functions provided by the programming language itself:

array.typecode : This function provides the typecode character used to create the array in itself

    • ‘b’ -> Denotes signed integer of size 1 byte

    • ‘B’ -> Denotes unsigned integer of size 1 byte

    • ‘c’ -> Denotes character of size 1 byte

    • ‘u’ -> Denotes unicode character of size 2 bytes

    • ‘h’ -> Denotes signed integer of size 2 bytes

    • ‘H’ -> Denotes unsigned integer of size 2 bytes

    • ‘i’ -> Denotes signed integer of size 2 bytes

    • ‘I’ -> Denotes unsigned integer of size 2 bytes

    • ‘w’ -> Denotes unicode character of size 4 bytes

    • ‘l’ -> Denotes signed integer of size 4 bytes

    • ‘L’ -> Denotes unsigned integer of size 4 bytes

    • ‘f’ -> Denotes floating point of size 4 bytes

    • ‘d’ -> Denotes floating point of size 8 bytes

array.itemsize : This function provides the length in bytes of one array in its internal representation

array.append(x) : This function appends a new element with value “x” to the array on which this method is invoked upon.

array.count(x) : This function returns the number of occurrences of the element with value “x” in the array object provided in the command.

Frequently Asked Python Interview Questions & Answers

array.extend(iterable) : This function appends the items of the iterable object (list, string, or an array itself) at the end of the array object.

array.fromlist(list) : This function appends the items of the list into the array object as specified in the command provided above.

array.fromstring(s) : This function appends the items of the string into the array object as specified in the command provided above.

Related Page: Python String Functions

array.index(x) : This function returns the index of the first occurrence of the element with value “x” in the array object specified.

array.insert(i,x) : This function inserts an element into the array with a value of “x” before the index of “i” on the provided array. Index of -1 is defaulted if there is no value provided for it, meaning that the element will be inserted at the end of the array object.

array.pop([i]) : This function removes the element available at the index “i” and returns the same element. This means that it removes the last element of the array, if there is no index provided to the function discussed above.

Related Page: Generators & Iterators In Python

array.remove(y) : This function removes the first occurrence of the element x from the array object provided.

array.reverse() : This function reverses the elements of the provided array into the same array object

array.tostring() : This function converts the array object into a string representation, the same bytes will be written to a file using the tofile() method instead.

array.tolist() : This function converts the array object into an ordinary list with the same items in the list as well.

Related Page: Python GET And POST Requests

Conclusion

In this article, we have seen what cases arrays can be used and why are they used in Python programming language. We have also tried to take a deeper look into the concept with various examples as well.

Hope that you were clear with the concepts after going through this detailed article, please do comment if you have any suggestions to make.

Explore Python Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

 


Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Free Demo Popup -->