Real tools are made of the small ideas that you already know, put together with care. In this course you plan and build three tools: a contact book that uses a dictionary and try and except, a code cracker that counts letters and uses the shift of a Caesar code, and a marks report that uses the statistics module, sorting and binary search. Each project starts with a plan and a table of expected answers, keeps the thinking in small functions, and ends with ideas for you to extend. A lesson on a command menu joins tools into one program that reads commands, and a last lesson gives a method for planning your own project and shows what comes next. The practice tests your functions on many cases and tells you which ones fail.
5 lessons, about 2 h 25 min5 simulations13 problems10 practice questionsPython in every lesson
A contact book has four jobs: add a contact, find a number, remove a contact and list everybody, and a dictionary is the right tool because we look things up by name. Two rules make it safe: the key is always the name in small letters so that Ravi and ravi are one contact, and a number must be digits only, which add enforces by raising a ValueError. Each job is a small function with a note that says what it does, and the main program catches the error so that it does not stop. We plan the cases and the expected book by hand, test the edges, and extend the book with a search by the start of a name.
A Caesar code can be read without knowing the shift by counting letters. In English the letter e is the most common, so in a long secret the most common letter probably stands for e, and the shift is the place of that letter minus the place of e, with percent 26 so that it stays from 0 to 25. A dictionary counts the letters and max with key equals counts.get picks the commonest. A short secret can fool the rule, because with few letters the commonest one may not be e. Then try every one of the 26 shifts, and keep the shift that gives the most common English words.
A class report summarises the marks, ranks the students, answers a question such as who is above the mean, and searches for a mark. The marks are in a dictionary with the name as the key. The statistics module gives the mean, the median and the standard deviation, and an empty list raises a StatisticsError, which the function catches and answers with None. The ranking sorts by two things with a key that is a tuple, the mark with a minus sign and then the name. Binary search needs a sorted list, and is fast. We plan the expected numbers by hand, put each job in a function, and test the edges, such as an empty class, one student and equal marks.
A command menu joins the tools into one program that reads commands from the user, such as add asha 98765, find asha and list. A dictionary of functions maps each command word to the function that does the job, so adding a command means adding one function and one entry. The loop reads a line with input, splits it into the command and its arguments, refuses an unknown command, ignores an empty line, and leaves on quit. Catching an EOFError ends the program cleanly when the input has run out. Each command function checks its arguments and answers with a message, and the loop only prints it. This design keeps the tools testable and the menu small.
To plan your own project, write down the goal in one sentence, list the jobs it must do, choose the container for each piece of data, write the expected answers for a small example by hand, build the thinking as small functions that you can test, add a table of tests with the edges, and only then write the part that talks to the user. Build one small piece first, run it, and grow it. Keep a list of ideas for later, and finish one version before you begin the next. The Essentials end here. The next steps are objects and classes, files, comprehensions and generators, and larger projects.
Log in to join the discussion.
Loading…