Learning Python
Through Python Koans

Andrew Koebbe

Lead Application Developer 

Ask me about...

  • Mandolin
  • DIY recipes
  • Ramen Hacking
  • MST3k
  • Sous Vide Cooking

What is a Koan?

kōan /ˈkoʊ.ɑːn/

A kōan is a story, dialogue, question, or statement, which is used in Zen practice to provoke the "great doubt" and test a student's progress in Zen practice.

--Wikipedia

"You can hear the sound of two hands when they clap together, now show me the sound of one hand."

First a bit of background...

Background of Python Koans

  • The Little LISPer - Published 1989
  • Ruby Koans - January 2009 (probably earlier)
  • Python Koans - May 2009 (Greg Malcolm)
  • PyOhio 2010, 2012, 2014
  • Last Commit in July

Other Koans Projects

What's Inside?

Overview

  • Separate Python 2.7 & 3 paths
  • 38 Lessons (topics)
  • 304 Koans (tests)
  • Answers are in the "answers" branch

Topics Covered

  • Asserts
  • Strings
  • None
  • Lists
  • List Assignments
  • Dictionaries
  • String Manipulation
  • Tuples
  • Methods
  • Control Statements
  • True And False
  • Sets
  • Exceptions
  • Iteration
  • Comprehension
  • Generators
  • Lambdas
  • Classes
  • New Style Classes
  • With Statements
  • Monkey Patching
  • Method Bindings
  • Decorating With Functions
  • Decorating With Classes
  • Inheritance
  • Multiple Inheritance
  • Scope
  • Modules
  • Packages
  • Class Attributes
  • Attribute Access
  • Deleting Objects
  • Regex

Projects

  • triangle project (1 & 2)

  • dice project

  • scoring project

  • proxy object project

How it works...

Assert Methods

assertEqual(self, first, second, msg=None) # Tests that first = second

# Example
self.assertEqual(3, 3) # Passes
self.assertEqual(2, 3) # Fails
assertMatch(self, pattern, string, msg=None) # Tests that pattern is in string

# Example
self.assertMatch("lumberjack", "I'm a lumberjack and I'm OK") # Passes
self.assertMatch("Spam", "Always look on the bright side of life") # Fails

# P.S. there is also assertNoMatch(self, pattern, string, msg=None)
assertTrue(self, expr, msg=None) # Tests if expr is true

# Example
self.assertTrue(1 == 1) # Passes
self.assertTrue(2 == 1) # Fails

assertEqual()

assertMatch()

assertTrue()

Placeholder Variables

__ = "-=> FILL ME IN! <=-"

____ = "-=> TRUE OR FALSE? <=-"

The double and quadruple underscore variables are placeholders for your answers to the koans.

Replace these with the correct value.

An actual koan

from runner.koan import *


class AboutLists(Koan):

    def test_accessing_list_elements(self):
        noms = ['peanut', 'butter', 'and', 'jelly']

        self.assertEqual(__, noms[0])
        self.assertEqual(__, noms[3])
        self.assertEqual(__, noms[-1])
        self.assertEqual(__, noms[-3])

Projects

Occasionally you will encounter some failing tests that are already filled out. In these cases you will need to finish implementing some code to progress. For example, there is an exercise for writing some code that will tell you if a triangle is equilateral, isosceles or scalene.

def triangle(a, b, c):
    # DELETE 'PASS' AND WRITE THIS CODE
    pass


class AboutTriangleProject(Koan):
    def test_equilateral_triangles_have_equal_sides(self):
        self.assertEqual('equilateral', triangle(2, 2, 2))
        self.assertEqual('equilateral', triangle(10, 10, 10))

    def test_isosceles_triangles_have_exactly_two_sides_equal(self):
        self.assertEqual('isosceles', triangle(3, 4, 4))
        self.assertEqual('isosceles', triangle(4, 3, 4))
        self.assertEqual('isosceles', triangle(4, 4, 3))
        self.assertEqual('isosceles', triangle(10, 10, 2))

    def test_scalene_triangles_have_no_equal_sides(self):
        self.assertEqual('scalene', triangle(3, 4, 5))
        self.assertEqual('scalene', triangle(10, 11, 12))
        self.assertEqual('scalene', triangle(5, 4, 2))

Let's do some live coding!

Install and Contemplate

$ git clone git@github.com:gregmalcolm/python_koans.git
Cloning into 'python_koans'...
remote: Counting objects: 1769, done.
remote: Total 1769 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1769/1769), 498.42 KiB | 0 bytes/s, done.
Resolving deltas: 100% (1039/1039), done.
Checking connectivity... done.

$ cd python_koans/python2/

$ python contemplate_koans.py 

Thoughts & Recommendations

  • For all skill levels
  • Wide range of topics
  • Helpful comments
  • Fork the github and check in your work
  • Try different techniques
  • Manageable file sizes (most under 100 lines)

Questions?

Learning Python through Koans

By akoebbe

Learning Python through Koans

  • 3,521