All topics

Python 3: functions, lists, and files

Learners organize code into functions they test, store groups of data, and save results to files.

Outline

Who it is for: Learners who finished Python 2: decisions and loops.

Most useful programs keep lists of records and save them between runs. A program that saves a grade book or a stock list can replace a paper register. At the end, the learner can write functions, test them, store records in lists and dictionaries, and save data to a file that the program reads next time.

Each function gets its tests before the learner moves on. Tests are how a programmer checks code, whether a person or an AI wrote it.

AI policy: Builders may use AI only as a tutor (explain or give a hint, never the answer), with the teacher present, and only after they have shown the skill without it. Pioneers and adults may use AI after the checkpoint, and must test any code and explain it in their own words. The checkpoint and the capstone defence are done without AI.

Planned lessons

  1. Defining your own function
  2. Parameters and return values: trace the call by hand
  3. Testing a function with assert
  4. Lists: adding, removing, and indexing
  5. Looping over a list
  6. Dictionaries for named data
  7. Reading a text file
  8. Writing results to a file
  9. Handling errors with try and except
  10. Using the standard library: import
  11. A contacts or grades program that saves its data

Teach it as

One sessionPredict what a short function returns, then write one function that takes a list and returns a total, and test it with assert, in one 2 hour class.
One dayMorning: functions, parameters, return values, and testing with assert. Afternoon: lists, looping over a list, and saving a list to a file.
One weekDay 1: functions, parameters, and return values. Day 2: testing functions and lists. Day 3: looping over lists and dictionaries. Day 4: reading and writing files, and try and except. Day 5 (Build and Show): checkpoint without AI, then each learner shows and explains a contacts or grades program that saves its data.

Know this first

Standards this follows

Videos to download

No videos listed yet.