Python Arrays

Python Arrays

Kishore V


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:

['Apple', 'Banana', 'Mango', 'Orange']
Try it Yourself

Accessing Array Elements

Each element in an array has an index number, starting from 0.

Output:

Apple
Mango
Try it Yourself

Modifying Array Elements

Array elements can be changed by assigning a new value to a specific index.

Output:

['Apple', 'Grapes', 'Mango', 'Orange']
Try it Yourself

Length of an Array

Use the len() function to find how many elements are in an array.

Output:

4
Try it Yourself

The highest index value is always length − 1

Looping Through an Array

You can iterate through all elements using a for loop.

Output:

Fruit: Apple
Fruit: Grapes
Fruit: Mango
Fruit: Orange
Try it Yourself

Adding Elements to an Array

Use the append() method to add a new element at the end.

Output:

['Apple', 'Grapes', 'Mango', 'Orange', 'Pineapple']
Try it Yourself

Removing Elements from an Array

Remove by Index (pop())

Output:

['Apple', 'Grapes', 'Orange', 'Pineapple']
Try it Yourself

Remove by Value (remove())

Output:

['Grapes', 'Orange', 'Pineapple']
Try it Yourself

Our website uses cookies to enhance your experience. Learn More
Accept !