Programming technique
Arrays and lists
Lists and indexed data
Arrays and lists store several related values under one variable name. An index identifies each position, beginning at zero.
Python lists store several related values under one variable name. An index identifies each position, beginning at zero, and a list can grow or shrink.
Fixed-size arrays
An array has a fixed length after creation. It is appropriate when the number of positions is known and stable.
Indexed lists
A Python list is dynamic, but it can still represent a known set of indexed positions when the program deliberately avoids adding or removing elements.
The final valid index is always one less than the collection length.
Dynamic ArrayLists
An ArrayList can grow and shrink. Use the generic type to state what each element stores.
Growing and shrinking a list
A list can grow with append() and shrink with pop() or remove(). Every later index may shift after an element is removed.
Two-dimensional arrays
Two-dimensional lists
A two-dimensional structure uses a row index and a column index.
| Feature | Array | ArrayList |
|---|---|---|
| Size | Fixed after creation | Can grow and shrink |
| Length | array.length | list.size() |
| Read | array[index] | list.get(index) |
| Replace | array[index] = value | list.set(index, value) |
| Add/remove | Not directly | add() and remove() |
| Feature | Fixed-capacity use | Dynamic list use |
|---|---|---|
| Size | Program keeps the original number of positions | Program may grow or shrink the list |
| Length | len(values) | len(values) |
| Read | values[index] | values[index] |
| Replace | values[index] = value | values[index] = value |
| Add/remove | Avoided by design | append(), insert(), pop(), remove() |
Index boundaries
Valid indexes run from 0 to length - 1.
Match related data carefully
Parallel collections only work when corresponding values stay at the same index.
Choose the structure deliberately
Use an array for fixed capacity and an ArrayList when the number of elements changes.
Choose the behaviour deliberately
Python lists are dynamic, but the problem may still require a stable number of positions or controlled growth.
Challenges Choose one
Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.
Text Dice
SelectedStore the words one, two, three, four, five and six in an array. Generate a random index and output the matching word without using an if or switch statement to translate the number.
Notebook
SelectedCreate a notebook that stores up to 10 notes in an array or ArrayList. Repeatedly display the numbered notes, ask which position to edit and replace the note at that position. Reject invalid indexes and allow the user to quit.
Currency Converter
SelectedStore at least five currency names and exchange rates in matching arrays or ArrayLists. Ask for an amount in British pounds and a target currency, then output the selected rate and converted amount. Keep each name and rate at matching indexes and handle an unknown currency clearly.
One-Dimensional Battleships
SelectedCreate a one-player Battleships game using a board with positions 1 to 50. Randomly place five single-position ships without duplicates. The player keeps guessing until every ship is found. Report hits, misses and total attempts, and prevent the same position being guessed twice.
Optional extension: make each ship occupy several adjacent positions while keeping every ship inside the board boundaries. Do not build the full six-part chain unless your teacher requests it.
Selected challenge
This choice is shared with the portfolio setup page.
Create your challenge folder
Run this command before starting. It creates the correct empty folder inside your portfolio.
Complete the challenge
Write and test your own solution in the folder created above.
Optional scaffolded support
Use only as much support as you need. The templates organise the program but leave the important algorithm unfinished.
Submit for review
Run this when your program is complete. It creates the README, commits the folder and pushes it.