Arrays are ordered collections that store multiple values in a single variable. Think of them as egg cartons - organized containers with numbered slots.
An egg carton has numbered slots (0, 1, 2, 3...) and each slot can hold one egg. Arrays work the same way - they have numbered positions (called indexes) and each position holds one value.
Why Arrays Matter
Without arrays, storing multiple related items would be messy:
- ❌ student1 = "Alice"
- ❌ student2 = "Bob"
- ❌ student3 = "Charlie"
- ❌ ...100 more variables!
With arrays:
- ✅ students = ["Alice", "Bob", "Charlie", ...]
- ✅ One variable, unlimited items!
Array Basics
Index Numbers: Arrays start counting at 0
Index 0: Apple
Index 1: Banana
Index 2: Orange
Common Array Operations
- ➕ Add: Push new item to end
- ➖ Remove: Pop item from end
- 🔍 Find: Search for specific item
- 📏 Length: Count how many items
- 🔄 Loop: Go through each item
- 🔀 Sort: Arrange in order
Real-World Examples
- 📧 Email: inbox = [email1, email2, email3...]
- 🛒 Shopping: cart = [item1, item2, item3...]
- 📱 Social Media: posts = [post1, post2, post3...]
- 🎵 Music: playlist = [song1, song2, song3...]
- 📸 Photos: gallery = [photo1, photo2, photo3...]
Accessing Array Items
todos = ["Buy milk", "Call mom", "Finish project"]
First task: todos[0] = "Buy milk"
Last task: todos[2] = "Finish project"
Arrays vs Single Variables
Single Variable: One box, one item
Array: One carton, many items organized by position
The bottom line: Arrays are essential for storing collections of related data. Just like an egg carton keeps eggs organized and easy to access, arrays keep your data organized with numbered positions. Master arrays, and you can handle lists of any size!