✨ Created and Taught by The Coders ✨

All About Arrays

What is an Array?

An Array is a data structure that stores multiple values in a single variable. It’s like container that holds a collection of items in an ordered list.

Key Features:

  • Elements have a specific position or index
  • Each element can be accessed by its position (index) number
  • You can change, add, or remove elements
  • Usually stores elements of the same data type

Why are Arrays Important?

Arrays allow you to:

  • Store multiple related values together
  • Process large amounts of data efficiently
  • Organize information in a structured way
  • Perform operations on collections of data

Real-world examples:

  • List of student grades
  • Shopping cart items

Arrays in Python

In Python, arrays are called lists.

Tip: Python lists are more flexible than traditional arrays in other languages!

# Example 1: Creating a simple array/list
fruits = ["apple", "banana", "orange", "grape"]
print("Fruits list:", fruits)
print("Type:", type(fruits))

Explanation:

  • fruits is a list containing 4 string elements
  • Square brackets [] create a list
  • Elements are separated by commas
  • type() shows this is a list object

Array Indexing

Each element in an array has a position number. This is called an index:

Index Element Description
0 First Arrays start at 0
1 Second Next Position
2 Third Next Position
-1 Last Negative indices count backwards
# Array indexing examples
colors = ["red", "blue", "green", "yellow", "purple"]

print("Full array:", colors)
print("\nIndexing examples:")
print("First element (index 0):", colors[0])
print("Second element (index 1):", colors[1])
print("Last element (index -1):", colors[-1])
print("Second to last (index -2):", colors[-2])

Key Indexing Concepts:

  • Zero-Indexed: Arrays start counting from 0
  • Negative Indices: -1 is the last element
  • Direct Access: colors[0] gets first element
  • Backwards Count: colors[-1] gets last element

Array Operations

Basic Operations:

  • Add Elements: append(), insert()
  • Remove Elements: remove(), pop()
  • Search Elements: in keyword, index()
  • Get Length: len() function
# Starting with an empty array
shopping_cart = []
print("Empty cart:", shopping_cart)

# Adding elements (append)
shopping_cart.append("milk")
shopping_cart.append("bread")
shopping_cart.append("eggs")
print("After adding items:", shopping_cart)

# Inserting at specific position
shopping_cart.insert(1, "butter")
print("After inserting butter at index 1:", shopping_cart)

# Removing elements
shopping_cart.remove("bread")
print("After removing bread:", shopping_cart)

# Checking if element exists
if "milk" in shopping_cart:
    print("Milk is in the cart!")

Operation Details:

Adding Elements:

  • append() adds to the end
  • insert(index, item) adds at specific position

Removing Elements:

  • remove(item) removes first occurrence
  • in keyword checks if item exists

Array Slicing

You can get portions of an array using slicing:

Slicing Syntax:

array[start:end]

Examples:

  • array[:3] - First 3 elements
  • array[2:6] - Elements from index 2 to 5
  • array[::2] - Every 2nd element
  • array[::-1] - Reverse the array
numbers = [10, 20, 30, 40, 50] # let's slice it now!
numbers[1:4] #this will give us [20, 30, 40]. It stops before index 4, so it includes indices 1, 2, and 3, the end index is exclusive.

# Slicing with a step
numbers[0:5:2] # this will give us [10, 30, 50]. It starts at index 0, goes up to (but not including) index 5, and takes every second element.

# Omitting start or end index
numbers[:3] # this will give us [10, 20, 30]. It starts from the beginning and goes up to (but not including) index 3.
numbers[2:] # this will give us [30, 40, 50]. It starts from index 2 and goes to the end of the list.

# Negative indices
numbers[-3:] # this will give us [30, 40, 50]. It starts from the third-to-last element and goes to the end.
numbers[:-2] # this will give us [10, 20, 30]. It goes up to (but not including) the second-to-last element

Arrays inside Arrays

Arrays can contain other arrays, where an element in a data structure is an array itself. These can be known as multi-dimensional arrays.

It’s almost like a spreadsheet:

Student Math Science English
Student 1 85 92 78
Student 2 90 88 95
Student 3 78 85 82

Summary

Key Concepts:

  • Store Multiple Values in an ordered collection
  • Zero-Indexed - First element is at position 0
  • Mutable - Modify, add, and remove elements
  • Versatile - Support many operations

Essential Operations:

| Operation | Syntax | Description | |———–|——–|————-| | Creation | array = [1, 2, 3] | Create new array | | Accessing | array[index] | Get element by index | | Adding | array.append(item) | Add to end | | Removing| array.remove(item) | Remove item | | Length | len(array) | Get array size | | Slicing | array[start:end] | Get portion |

When to Use Arrays:

  • Storing lists of related data
  • Processing collections of information
  • Implementing algorithms with sequences
  • Managing ordered data that needs indexing

Works Cited

https://github.com/precia-verma/Group-projects/blob/main/_notebooks/Foundation/javascript/Array/2025-09-24-The-Coders-Array-js-Hack.ipynb - Hacks

https://github.com/precia-verma/Group-projects/blob/main/_notebooks/Foundation/javascript/Array/2025-10-03-The-Coders-Array-homework.ipynb - Homework