Data structures and algorithms · Lesson 15 of 60 · Code lesson, JavaScript or Python · Optional
Arrays and lists
An array (a list in Python) keeps many values in order.
Each value has a position called an index, and the first index is 0.
You can read an item by its index, add items, and ask how many there are.
Worked example
In JavaScript, fruits[1] reads the second item, and fruits.length tells how many items there are.
const fruits = ["apple", "banana", "cherry"];
console.log(fruits[1]);Output: banana
In Python, fruits[1] reads the second item, len(fruits) tells how many items there are, and fruits[-1] is the last item.
fruits = ["apple", "banana", "cherry"]
print(fruits[1])Output: banana
Your turn
The program reads some numbers. Make it print the last number instead of the first.
Your program is checked with these 3 inputs:
Input:
10 20 30 40
Expected output:
40
Input:
5 9
Expected output:
9
Input:
7
Expected output:
7
Loading your practice…
Preparing your workspace…