🚂 Arrays Explained Like You're 5
Source: Dev.to
The Train
Imagine a train with numbered compartments:
🚂 [Car 0] [Car 1] [Car 2] [Car 3] [Car 4]
Each car has a number (starting from 0!) and can hold one thing.
Arrays are trains for your data!
Full deep‑dive with code examples
In Code
fruits = ["apple", "banana", "cherry", "date"]
Index: 0 1 2 3
["apple", "banana", "cherry", "date"]
To get banana: fruits[1] (index 1, the second item)
Why Start at 0?
Tradition from early computers! Just remember:
- First item = index 0
- Second item = index 1
- Third item = index 2
What Can You Do?
| Action | Code | Result |
|---|---|---|
| Get item | fruits[2] | "cherry" |
| Change item | fruits[0] = "apricot" | Updates first item |
| Add item | fruits.append("elderberry") | Adds to end |
| Count | len(fruits) | 5 |
In One Sentence
Arrays store multiple items in a numbered list, like numbered train compartments in a row.