Open links in new tab
  1. In Python, an array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together, making it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array.

    Creating Arrays in Python

    Python does not have built-in support for arrays, but you can use the array module to create arrays. The array module provides an array type that can be used to store items of the same data type. Here is an example of creating an array using the array module:

    import array as arr

    # Creating an array of integers
    a = arr.array('i', [1, 2, 3])
    print("The new created array is:", a)

    # Creating an array of doubles
    b = arr.array('d', [2.5, 3.2, 3.3])
    print("The new created array is:", b)

    Output:

    The new created array is: array('i', [1, 2, 3])
    The new created array is: array('d', [2.5, 3.2, 3.3])

    Accessing and Modifying Array Elements

    You can access elements in an array using indexing, similar to lists. You can also modify elements by assigning new values to specific indices:

    Feedback
Refresh