In the list ['red', 'blue', 'green'], what is at index 0?
COMPUTING AND DIGITAL LITERACY
This document was made by AI and there may be mistakes. If a question or answer looks wrong, use the Flag a mistake button to let Brett know so he can fix it.
This is how a program finds one item in a list by its position, like picking the third song in a playlist or the first name on a class list. Instead of searching through every item, the program goes straight to the position it needs, which is fast even in a list of thousands. Every app that shows a list uses this, from a playlist and a contacts list to a photo album, and it is how a program picks exactly the item you asked for.
List positions start at 0, not at 1.
The first item is at index 0, and the last valid index is one less than the length of the list. Index -1 gives the last item.
fruits
Solution
This is an indexing question, and the one rule to remember is that counting starts at 0, not at 1.
The first item, 'apple', is at index 0. The second item, 'banana', is at index 1.
So fruits[1] is 'banana'.
fruits
Solution
A negative index counts backwards from the end, so -1 is the last item: fruits[-1] is 'cherry'.
The list has 3 items, so the valid indexes run from 0 to 2. Index 3 is one past the end.
fruits[3] gives an out-of-range error. The last valid index is always the length minus 1.
In the list ['red', 'blue', 'green'], what is at index 0?
What is at index 2 in the list ['red', 'blue', 'green']?
What is at index -1 in the list ['red', 'blue', 'green']?
How many values does a list of length 4 hold?
What is the last valid index of a list of length 4?
What happens with index 4 on a list of length 4?
In the string 'hello', what character is at index 1?
What is at index -2 in the list ['red', 'blue', 'green']?
What does len() tell you for indexing?
How do you change colours[1] to 'yellow'?
Your score: 0 / 10
1. red
2. green
3. green
4. 4
5. 3
6. An out-of-range error
7. e
8. blue
9. The length, so valid indexes run from 0 to len - 1
10. colours[1] = 'yellow'