What is an array (or list)?
In short
An array, called a list in Python, keeps many values in order under one name. You get each value by its position, starting from 0.
If a variable is one box, an array is a row of boxes. A shopping list, the scores in a game or the messages in a chat are all arrays. Each item has a position called its index, and the first one is at index 0, not 1.
You can add and remove items, sort them, and loop through them one by one. Python calls them lists, JavaScript and C call them arrays, but the idea is the same.
Python
Make a list, read from it and add to it
fruits = ["apple", "banana", "cherry"]
print(fruits[0])
fruits.append("date")
print(len(fruits))
print(fruits)Output
apple
4
['apple', 'banana', 'cherry', 'date']JavaScript
The same idea in JavaScript
const fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]);
fruits.push("date");
console.log(fruits.length);
console.log(fruits);Output
apple
4
[ 'apple', 'banana', 'cherry', 'date' ]Learn it properly
These free lessons teach it step by step, with questions and a program you write yourself.
- Creating and Indexing ListsLearn Python →
- Creating and Indexing ArraysLearn JavaScript →
- Declaring and Indexing ArraysLearn C →
Try your first lesson free
Codiqo teaches coding in five-minute lessons with real code. Free on the web, iPhone and Android.