Python Arrays (With Examples)

PYTHON ARRAYS:

In Python, an array is a data structure used to store a collection of elements of the same data type in a sequential order. While Python's built-in data structure for this purpose is the list, an array-like structure can also be created using the `array` module from the standard library or the more powerful `ndarray` type provided by the NumPy library. 

reload ! diplomawaale.blogspot.com
Python Arrays



Arrays enable efficient element access and modification, making them useful for tasks like data manipulation, numerical computing, and implementing algorithms. The array's fixed or dynamic size, depending on the implementation, allows for flexible storage of data, and in the case of NumPy arrays, enables optimized mathematical operations. Arrays play a vital role in various programming contexts, including scientific computing, data analysis, and algorithmic problem-solving.

Array Representation:


An array can be declared in various ways and in different languages. The important points that should be considered are as follows:


  1. The index starts with 0.
  2. We can easily find any elements within this Array using the Index value.
  3. The length of the Array defines the capacity to store the elements. It is written like x[100], which means the length of array x is specified by 100.

ARRAY OPERATIONS:

  • Traverse - It prints all the elements one by one.
  • Insertion - It adds an element at the given index.
  • Deletion - It deletes an element at the given index.
  • Search - It searches an element using the given index or by the value.
  • Update - It updates an element at the given index.

Array operations in Python, particularly using NumPy arrays, allow you to efficiently manipulate and perform calculations on arrays of data. Here's a list of common array operations and examples using NumPy:

1. Creation and Initialization:

EXAMPLE:

                        import numpy as np
                        # Creating an array
                        arr = np.array([1, 2, 3, 4, 5])
                        # Creating an array of zeros
                       zeros = np.zeros(5)
                       # Creating an array of ones
                       ones = np.ones(5)
                       # Creating a range of values
                      sequence = np.arrange(0, 10, 2)

2. Element-wise Arithmetic:

EXAMPLE:

                      a = np.array([1, 2, 3])
                      b = np.array([4, 5, 6])
                      # Addition
                      result = a + b
                     # Subtraction
                     result = a - b
                    # Multiplication
                    result = a * b
                    # Division
                    result = a / b

3. Universal Functions (ufuncs):

EXAMPLE:

                       arr = np.array([1, 2, 3, 4, 5])
                       # Square root
                       sqrt_arr = np.sqrt(arr)
                      # Exponential
                      exp_arr = np.exp(arr)
                      # Trigonometric functions
                     sin_arr = np.sin(arr)
                     cos_arr = np.cos(arr)

4. Aggregation and Reduction:

EXAMPLE:

                    arr = np.array([1, 2, 3, 4, 5])
                    # Sum of all elements
                   total = np.sum(arr)
                   # Mean value
                   mean = np.mean(arr)
                  # Maximum and minimum values
                  max_val = np.max(arr)
                  min_val = np.min(arr)

5. Indexing and Slicing:

EXAMPLE:

                      arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
                      # Accessing an element
                      element = arr[2]  # Third element
                     # Slicing
                      subset = arr[2:6]  # Elements from index 2 to 5
                     # Conditional indexing
                     even_numbers = arr[arr % 2 == 0]

6. Reshaping and Broadcasting:

EXAMPLE:

                     arr = np.array([1, 2, 3, 4, 5, 6])
                     # Reshaping array
                     reshaped = arr.reshape(2, 3)
                     # Broadcasting (element-wise operations on arrays with different shapes)
                     scaled = arr * 2

Lists as Arrays:

In Python, you can use lists to create arrays. Lists can hold elements of any data type, and they automatically resize to accommodate new elements. Here's an example of creating an array-like structure using a list:

EXAMPLE:

# Creating an "array" using a list
                    my_array = [1, 2, 3, 4, 5]
# Accessing elements
print(my_array[0])  # Output: 1
print(my_array[2])  # Output: 3
# Modifying elements
                    my_array[1] = 10
                    print(my_array)     # Output: [1, 10, 3, 4, 5]

Arrays with `array` Module:

If you need a more memory-efficient array implementation for numeric data, you can use the `array` module from the Python standard library. The `array` module provides a compact way to store arrays of a specific data type.

EXAMPLE:

                      from array import array
                      # Creating an array of integers
                      int_array = array('i', [1, 2, 3, 4, 5])
                      # Accessing and modifying elements
                      print(int_array[0])   # Output: 1
                      int_array[1] = 10
                      print(int_array)      # Output: array('i', [1, 10, 3, 4, 5])

NumPy Arrays:

For more advanced array manipulation, mathematical operations, and numerical computing, the popular library NumPy provides a powerful `ndarray` (n-dimensional array) data type. NumPy arrays are more memory-efficient and optimized for numerical operations.

EXAMPLE:

                           import numpy as np
                           # Creating a NumPy array
                           numpy_array = np.array([1, 2, 3, 4, 5])
                           # Accessing and modifying elements
                           print(numpy_array[0])  # Output: 1
                           numpy_array[1] = 10
                           print(numpy_array)     # Output: array([ 1, 10,  3,  4,  5])

Post a Comment

0 Comments