Python Arrays

Python Arrays

Python does not have built-in support for Arrays. However, Python Lists can be used instead.

If you want to create highly optimized arrays (especially for mathematical computations), you should use the NumPy library, or Python's built-in array module. For general programming, lists are the standard substitute.


Arrays vs. Lists

To understand Arrays in Python, you just need to understand Lists. An array is a special variable, which can hold more than one value at a time.

Array/List Example

cars = ["Ford", "Volvo", "BMW"]

// Access the first item x = cars[0] print("First car:", x)

// Change the first item cars[0] = "Toyota" print("Updated cars:", cars)


Array Methods (List Methods)

Python has a set of built-in methods that you can use on lists/arrays.

Common Methods

cars = ["Ford", "Volvo", "BMW"]

cars.append("Honda") // Adds an element to the end cars.pop(1) // Removes the element at index 1 (Volvo)

print(cars) // ['Ford', 'BMW', 'Honda'] print(len(cars)) // Returns the length: 3

Exercise

?

Which built-in Python data structure is commonly used as an Array?