Ten Small Python Programs

by jesse in ,


A comp.lang.python thread caught my eye (after I was done gouging them out after reading the PEP8 discussion - we lovesOurBouncyName) entitled "Ten Small Python Programs". To quote Steve Howell:

I've always thought that the best way to introduce new programmers to Python is to show them small code examples.When you go to the tutorial, though, you have to wade through quite a bit of English before seeing any Python examples.

Below is my attempt at generating ten fairly simple, representative Python programs that expose new users to most basic concepts, as well as the overall syntax.

It was an interesting exercise. I constrained myself to ten lines or less, and it was pretty easy to incorporate loops, conditionals, print, open(), lists, tuples, dictionaries, and imported modules.

It was harder to show classes, and my ShoppingCart class is nothing more than an encapsulation of a list, which has dubious value (although it's the start of something more useful).

This is a fantastic idea. This follows the same type of teaching style a fellow at work and I used to explain Python concepts to Java guys new to Python. Steve started a python wiki page here.