Python Array Declaration with Examples

Python Array:

An array in Python is a linear data structure that stores similar data items in contiguous memory locations.

When compared to a List which is a dynamic Array, Python Arrays keeps elements of the same datatype. A Python List, on the other hand, can store entries of several/different data types.

Python does not provide a direct method for creating or working with arrays as a data structure. Rather, it gives us the following Array variants:

  • List of Python: It has all of the characteristics of an Array.
  • Array module in Python: This module is used to construct an array and alter the data using the functions provided.
  • Python NumPy array: The NumPy module generates an array for mathematical applications.

Declaration of an array can be done in the above-mentioned ways. Let’s now move on further.

1)Using the Python List

Like an array, a Python list can be used to dynamically construct and store elements.

Syntax:

list = [data]

Example

Approach:

  • Give the list as static input and store it in a variable.
  • Print the given list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ["hello", "btechgeeks", 15, 16, 17]
# Print the given list.
print("The given list = ", gvn_lst)

Output:

The given list =  ['hello', 'btechgeeks', 15, 16, 17]

Note: List can store any type of data element.

2)Using Array module in Python

The array() function in the Python Array module allows us to generate an array in the Python environment.

Syntax:

array.array( 'format_code', [data] )

format_code: format code denotes the type of elements that an array will accept. The letter ‘i’ stands for numerical values.

Example

Approach:

  • Import array module using the import Keyword.
  • Give the array as static input using the array() function by passing the format_code, # some random elements as arguments to it.
  • Store it in a variable.(here ‘i’ represents numerical values(int)).
  • Print the given array.
  • The Exit of the Program.

Below is the implementation:

# Import array module using the import Keyword
import array
# Give the array as static input using the array() function by passing the format_code,
# some random elements as arguments to it.
# Store it in a variable.
# here 'i' represents numerical values(int)
gvn_arry = array.array('i', [3, 5, 100, 25])
# Print the given array. 
print("The given array is:\n", gvn_arry)

Output:

The given array is: 
 array('i', [3, 5, 100, 25])

3)Using Python NumPy array

The NumPy module includes a number of functions for creating and manipulating arrays as data structures.

In Python, the numpy.array() function can be used to build both single-dimensional and multi-dimensional arrays. It generates an array object with the name ‘ndarray’.

Syntax:

numpy.array( [data] )

Approach:

  • Import numpy module using the import Keyword.
  • Create an array using the array() function by passing some random list of data as an argument to it.
  • Store it in a variable.
  • Print the given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import Keyword
import numpy 
# Create an array using the array() function by passing some random list of data as an argument to it
# Store it in a variable.
gvn_arry= numpy.array([15,3,2,9,8])
# Print the given array
print("The given array = ", gvn_arry) 

Output:

The given array = [15 3 2 9 8]

In addition, we can use the numpy.arange() function to generate an array inside a given range of data values.

Syntax:

numpy.arange(start, stop, step)

Parameters

start: The array’s first element.
end: The array’s last element.
step: The number of steps or intervals between array members.

Example

# Import numpy module as np using the import Keyword
import numpy as np 
# Create an array using the arange() function by passing start, stop, step values as the arguments to it.
# Store it in a variable.
# The numpy.arange() method is used to create an array with values within the given range.
gvn_arry = numpy.arange(2,15,2)
# Print the given array
print("The given array = ", gvn_arry)

Output:

The given array = [ 2 4 6 8 10 12 14]