Back to Engineering
15 MAY 2026

Arrays: Your First Data Structure and Why Every Programmer Needs Them

CA
Compiled by Cypher Async

Arrays: Your First Data Structure and Why Every Programmer Needs Them

So far you have learned to store one value in one variable. But what if you need to store the marks of 50 students? Create 50 variables? That would be painful. Arrays solve this exact problem — they let you store multiple values in a single, organised container.


What Is an Array?

An array is an ordered collection of items stored under a single variable name. Each item in the array has an index — a number that tells you its position.

Real-world analogy: Think of a row of numbered lockers.

  LOCKER ROW
  ┌─────┬─────┬─────┬─────┬─────┐
  │  0  │  1  │  2  │  3  │  4  │   ← Index (position number)
  ├─────┼─────┼─────┼─────┼─────┤
  │ 85  │ 92  │ 78  │ 95  │ 60  │   ← Value (student marks)
  └─────┴─────┴─────┴─────┴─────┘

Instead of five separate variables (mark1, mark2, mark3...), you have one array called marks and you access each item by its index number.


Arrays in Different Languages

Python (called a List):

marks = [85, 92, 78, 95, 60]

JavaScript:

let marks = [85, 92, 78, 95, 60];

Java:

int[] marks = {85, 92, 78, 95, 60};

The syntax varies slightly but the concept is identical: a single name, multiple values, in order.


Zero-Based Indexing

One of the most important things to understand about arrays: the first element is at index 0, not 1.

  marks = [85, 92, 78, 95, 60]

  Index:    0   1   2   3   4
            ▲               ▲
        First item        Last item
        (index 0)         (index 4)

Why 0? It comes from how computers calculate memory addresses. The index is an offset from the start — the first item has zero offset from itself, so it is at index 0.

Accessing elements:

marks = [85, 92, 78, 95, 60]

print(marks[0])   # Output: 85  (first element)
print(marks[2])   # Output: 78  (third element)
print(marks[4])   # Output: 60  (last element)

How Arrays Are Stored in Memory

Arrays store elements in contiguous (next to each other) memory locations. This is what makes them fast to access.

  MEMORY
  ┌──────────┬────────┬───────┐
  │ Address  │ Index  │ Value │
  ├──────────┼────────┼───────┤
  │ 0x1000   │   [0]  │  85   │
  │ 0x1004   │   [1]  │  92   │
  │ 0x1008   │   [2]  │  78   │
  │ 0x100C   │   [3]  │  95   │
  │ 0x1010   │   [4]  │  60   │
  └──────────┴────────┴───────┘

When you ask for marks[2], the computer calculates: start_address + (index × size_of_one_item) = goes directly to 0x1008. No searching — instant access.


Common Array Operations

1. Access an element

marks = [85, 92, 78, 95, 60]
first = marks[0]      # 85
third = marks[2]      # 78

2. Update an element

marks[1] = 100        # Change 92 to 100
print(marks)          # [85, 100, 78, 95, 60]

3. Find the length

total = len(marks)
print(total)          # 5

4. Add an element (Python List)

marks.append(88)
print(marks)          # [85, 92, 78, 95, 60, 88]

5. Remove an element

marks.remove(78)
print(marks)          # [85, 92, 95, 60]

Iterating Through an Array with a Loop

The real power of arrays shows when you combine them with loops:

marks = [85, 92, 78, 95, 60]
total = 0

for mark in marks:
    total = total + mark

average = total / len(marks)
print("Average marks: " + str(average))
# Output: Average marks: 82.0

Visual walkthrough:

  marks = [85, 92, 78, 95, 60]
  total starts at 0

  Iteration 1: total = 0  + 85  = 85
  Iteration 2: total = 85 + 92  = 177
  Iteration 3: total = 177 + 78 = 255
  Iteration 4: total = 255 + 95 = 350
  Iteration 5: total = 350 + 60 = 410

  average = 410 / 5 = 82.0

Accessing by Index in a Loop

When you need both the index and the value, use enumerate (Python):

students = ["Ananya", "Rohan", "Priya", "Dev"]

for index, name in enumerate(students):
    print(str(index + 1) + ". " + name)

Output:

1. Ananya
2. Rohan
3. Priya
4. Dev

Arrays of Different Data Types

Arrays can hold more than just numbers — any data type works:

# Array of strings
cities = ["Agartala", "Kolkata", "Mumbai", "Delhi"]

# Array of booleans
results = [True, False, True, True]

# Mixed types (Python allows this)
profile = ["Kabir", 21, True, 9.5]

Negative Indexing (Python)

Python allows you to access elements from the end using negative indices:

marks = [85, 92, 78, 95, 60]

print(marks[-1])   # Output: 60  (last element)
print(marks[-2])   # Output: 95  (second from last)
  marks = [85,  92,  78,  95,  60]
  Index:    0    1    2    3    4
           -5   -4   -3   -2   -1   ← Negative indices

Slicing — Getting a Portion of an Array

marks = [85, 92, 78, 95, 60]

# Get elements from index 1 to 3 (3 is not included)
portion = marks[1:3]
print(portion)   # Output: [92, 78]

# Get first 3 elements
start = marks[:3]
print(start)     # Output: [85, 92, 78]

# Get last 2 elements
end = marks[-2:]
print(end)       # Output: [95, 60]

A 2D Array — Array of Arrays

Arrays can contain other arrays, creating a grid or table structure:

classroom = [
    ["Ananya", 92],
    ["Rohan",  85],
    ["Priya",  78]
]

for student in classroom:
    print(student[0] + " scored " + str(student[1]))

Output:

Ananya scored 92
Rohan scored 85
Priya scored 78

Visualised as a table:

  classroom[row][column]

         col 0      col 1
  row 0 ["Ananya"] [ 92  ]
  row 1 ["Rohan" ] [ 85  ]
  row 2 ["Priya" ] [ 78  ]

When to Use an Array

  USE AN ARRAY WHEN:                    USE SEPARATE VARIABLES WHEN:
  ─────────────────────────────         ──────────────────────────────
  You have multiple related values      You have 1–2 unrelated values
  You need to loop through values       Order and repetition not needed
  You want to sort or filter values     Values have very different meanings
  The number of items can grow          Fixed, named things (name, age)

Summary

| Concept | What it means | |--------------------|----------------------------------------------------| | Array | Ordered collection of values in one variable | | Index | Position of an element (starts at 0) | | Length | Total number of elements | | Access | array[index] retrieves an element | | Append | Add an element to the end | | Iterate | Use a loop to go through every element | | Slice | Extract a portion of the array | | 2D Array | Array of arrays — works like a table/grid |

  • The first element is always at index 0
  • Arrays are stored in contiguous memory — fast to access
  • Loops and arrays are best friends — use them together
  • Python lists are flexible arrays that can grow and shrink

Arrays are your gateway into data structures — the organised containers that power every application, from grade books to social media feeds to banking systems. Understand arrays deeply, and you are ready to learn the more advanced structures that build on them.


Published by Cypher Async — Agartala's offline coding school, building real engineers one concept at a time.