ARRAY

In Python, arrays are not a built-in data type, but instead, lists are commonly used to represent arrays. Lists in Python are dynamic arrays, meaning they can grow or shrink in size as needed. Python lists can store elements of different data types, and they support indexing, slicing, and a variety of methods for manipulation.

Here's a basic overview of working with lists in Python:

Creating a list

my_list = [1, 2, 3, 4, 5]

Accessing elements

print(my_list[0])   # Output: 1 (indexing starts from 0)

Slicing

print(my_list[1:4])  # Output: [2, 3, 4] (slicing returns a new list)

Modifying elements

my_list[2] = 10

print(my_list)      # Output: [1, 2, 10, 4, 5]

Adding elements

my_list.append(6)

print(my_list)      # Output: [1, 2, 10, 4, 5, 6]

Removing elements

my_list.remove(4)

print(my_list)      # Output: [1, 2, 10, 5, 6]

Length of a list

length = len(my_list)

print(length)       # Output: 5

Iterating over a list

for item in my_list:

    print(item)

Lists in Python are versatile and can be used to represent one-dimensional arrays. However, if you need more advanced array functionality (e.g., for numerical computations), you may want to consider using the NumPy library, which provides a powerful array object called numpy.array.

import numpy as np

 

my_array = np.array([1, 2, 3, 4, 5])

print(my_array)