An array is a data structure that allows you to store multiple values in a
single variable. Instead of creating many separate variables, you can store
all values together in one collection.
Instead of creating many separate variables like this:
You can store all values together in one array (which Python calls a
list):
This makes your code cleaner, easier to manage, and more
efficient—especially when working with large amounts of data.
Creating an Array (List)
In Python, arrays are created using square brackets
[].
Accessing Array Elements
Each element in an array has an index number, starting from
0.
Modifying Array Elements
Array elements can be changed by assigning a new value to a specific index.
Length of an Array
Use the
len()
function to find how many elements are in an array.
Note: The highest index value is always
length - 1.
Looping Through an Array
You can iterate through all elements using a
for loop.
Adding Elements to an Array
Use the
append()
method to add a new element at the end.