Arrays in Python

In this module we are going to discuss about Arrays in Python

Basic array operations like

  1. Finding the length of the array
  2. Addition
  3. Removal
  4. Concatenation
  5. Slicing
  6. Looping
  • Creating arrays in python
  • Accessing array elements

Basic Array definition

  • An array is a collection of items stored at contiguous memory locations.
  • It is a data structure which holds multiple values of same data type.
  • Python arrays and lists have the same way of storing data but the main difference is arrays take a single data type and lists can store any type of data.

Creating an array in python

Arrays in python can be created after importing the array module.This can be done by using 3 ways.They are

  • without using ALIAS

Syntax – import array;

  • using ALIAS (most commonly used)

Syntax – import array as arr;

  • using * (imports all which is present in the array)

Syntax – from array import *; 

Accessing elements in an Array

  • Generally in an array,indexing starts from 0.So index number is always 1 less than the length of the array.
  • Negative indexing can also be used.It starts from the reverse order of traversal i.e. from right to left.
  • An example on how the elements are positioned in an array is shown below.
  • Let us observe an example program for better understanding purpose.

Basic array operations

1.Finding the length of the array

  • It is the number of elements that are actually present in an array.
  • We use len() function to achieve this.
  •  len() function returns a value that is equal to the number of elements present in an array.
  • Let us observe the below execution to achieve this.

2.Adding elements to an array

We can add elements to an array by using 3 functions.They are

  1. append() – used when you want to add a single element to the end of the array.
  2. extend() – used when you want to add more than one element to the end of the array.
  3. insert() – used when you want to add the element at the specific position in the array.

3.Removing elements from an array

Generally we use 2 functions to remove elements from the array.They are

  1. pop() – used when you want to remove an element and return it.
  2. remove() – used when you want to remove a specific element from an array without returning it.

4.Array concatenation

  • Array concatenation means combining both the arrays.
  • This can be done by using ‘+’ symbol.

5.Slicing an array

  • An array can be sliced using the ‘:’ symbol.This returns a range of elements that we have specified by the index numbers.
  • If we take an example a[0,4] ; here element at the 0th index is included but the element at the 4th index is excluded.

6.Looping through an array

Array looping is done by 2 functions.They are

  1. for() – iterates over the items of an array specified no of times.

2.while() – iterates over the elements until a certain condition is met.

Conclusion

In this module we have discussed the total concept of array in python.For more interesting updates , do visit our website http://roboticelectronics.in/

Spread knowledge

Leave a Comment

Your email address will not be published. Required fields are marked *