Python Arrays
An array is a data structure that allows you to store multiple values in a single variable.
Instead of creating many separate variables:
item1 = "Pen"
item2 = "Pencil"
item3 = "Eraser"
You can store all values together in one array (list):
stationery = ["Pen", "Pencil", "Eraser"]
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 [].
Output:
Accessing Array Elements
Each element in an array has an index number, starting from 0.
Output:
Mango
Modifying Array Elements
Array elements can be changed by assigning a new value to a specific index.
Output:
Length of an Array
Use the len() function to find how many elements are in an array.
Output:
The highest index value is always length − 1
Looping Through an Array
You can iterate through all elements using a for loop.
Output:
Fruit: Grapes
Fruit: Mango
Fruit: Orange
Adding Elements to an Array
Use the append() method to add a new element at the end.
