Python II: lists and strings
Lists and indexing, append and remove, iterating, slicing and string methods, functions with parameters and return values, and validation.
What you will know by the end
- Lists start at 0; the last item is at len(list) - 1.
- append, remove, len, in: what each does and returns.
- A slice s[a:b] takes from a up to but not including b.
- A function takes parameters and returns a value; print shows, return hands back.
- Validate input with normal, boundary and erroneous test data.
Syllabus and key words
- Topic 14: Programming further
- Read and change lists (including 2D lists) using indexes that start at 0.
- Slice strings with s[a:b] and predict the result, every time.
- Write procedures and functions with parameters and return values, and explain local vs global scope.
- Make programs robust: validation, authentication, try/except and file handling.
- Plan tests with normal, boundary and erroneous data.
- Topic 13: Programming basics
- Read a Scratch script and write the same thing in Python.
- Use variables, the four data types and casting, and spot the input() bug.
- Work out DIV (//) and MOD (%) by hand, and use MOD to test for even numbers.
- Trace if / elif / else, for with range() and while loops line by line.
- Tell syntax, runtime and logic errors apart, and fix each kind.
- List
- One name holding many values in order. Also called an array.
- Index
- An item's position in a list, counting from 0.
- Subroutine
- A named block of code you can call when needed.
- Function
- A subroutine that returns a value.
- Parameter
- A placeholder in a subroutine for a value passed in.
- Validation
- Checking input is reasonable before using it.
- Boundary data
- Test data at the very edge of what's allowed.
- Variable
- A named place in memory that holds a value.
- Assignment
- Storing a value in a variable. = in Python, ← in pseudocode.
- Casting
- Changing a value from one data type to another, like int("13").
- Concatenation
- Joining strings together: "7" + "7" = "77".
- Selection
- Choosing a path with if / else.
- Iteration
- Repeating steps: a loop.
- Boolean
- A value that is only True or False.
- Infinite loop
- A loop whose condition never becomes false.
- Lists: creating and indexingPractical
- append, remove, len and inPractical
- Iterating with forPractical
- Slicing and string methodsPractical
- Functions with parameters and return valuesPractical
End-of-unit challenge: A quiz program that stores questions in lists and uses at least two functions.
Homework: Study page 14 to the quiz.
Extra challenge: A function that turns a name into initials.