Learning Python

May 31, 2008

Like many other people, I decided to learn Python so that I could give Google App Engine a try (and I did not want to “cheat” by getting my Java code automatically translated to Python). In order to get a feel for the language, I wrote a simple Tic-tac-toe game. I thought this example might be useful for others to get a quick overview of some of the highlights and peculiarities of Python, especially if you are more used to statically typed languages like I am. For a more detailed introduction, check out the official Python Tutorial or Dive Into Python.

Defining a class

Although Python is not specifically an object-oriented language, it does support OOP to a certain extent. So I created a couple of classes to represent the tic-tac-toe game. Let’s start with a class for the grid-shaped game board:

class Board:
    def __init__(self, size, symbol1, symbol2):
        self.size = size
        self.symbol1 = symbol1
        self.symbol2 = symbol2
        self.grid=[size * [' '] for i in range(size)]

Read the rest of this entry »