Who this book is for
Confident Python learners who want to compare algorithms, organise records and work more independently.
Before you begin
Practise functions, lists, searches and nested loops first. Python GCSE Core is the preceding book in this series.
Includes enrichment such as recursion alongside GCSE programming practice. The title does not mean that every topic is required by every GCSE board.
Inside the book
Learning themes across 14 chapters
- Comparing searching algorithms
- Tracing and testing sorting algorithms
- Organising records with dictionaries
- Exploring recursion
- Choosing and explaining an approach
- Building and testing independent projects
Work through explanations and examples, try the independent practice, then use the explained answers to review your reasoning.
Try the approach
Count repeated values with a dictionary
A dictionary can connect each value to a running count. Trace the count after each pass through the loop.
counts = {}
for colour in ['blue', 'gold', 'blue']:
counts[colour] = counts.get(colour, 0) + 1
print(counts['blue'])
print(counts['gold'])What it prints
2
1get(colour, 0) starts a new count at zero. Each occurrence adds one. The dictionary stores the result for each colour separately.
This short public example introduces an idea explored in the book.
