At the ‘predict’ & ‘run’ stages students work entirely with example code. They should inspect it carefully and write a prediction about what it will do.
They then run the code and compare the result to their prediction.
In this unit you will learn about: Lists
It is possible to output some consecutive items from a list by customising the print function to add a start and end index for the output.
There are a couple of traps to be careful of here.
The start value IS included in the output.
The end value IS NOT included in the output.
# Create and populate a list
countries = ["UK", "USA", "Chad", "Australia", "Thailand"]
# Outputs 'USA', 'Chad', 'Australia'
print(countries[1:4])
# Outputs from the beginning of the list to 'Chad'.
print(countries[:3])
# Outputs from 'USA' to the end of the list.
print(countries[1:])
Help! My Code Does Not Work!
- Square brackets for items in the list.
- Commas between each item in the list.
- String items are in quotation marks.
- Range – the end value is NOT included in the output.
- Search – not initialising counter OUTSIDE the loop.
- Search – not incrementing counter INSIDE the loop.