head icon

Python

The interpreter, IDLE

Indentation, lines, and statements

Variables

Types

Sequences

Working with sequences

More collections

Function definitions

Optional arguments to functions

Loops

Importing

Object-oriented programming

class Sentence:
    """Simple representation of a sentence."""

    def __init__(self, words, verb='', sbj=''):
        """Assign basic attributes.

        words: list of strings
        verb: string
        sbj: string
        """
        self.words = words
        self.verb = verb
        self.sbj = sbj

    def __repr__(self):
        return "<" + ' '.join(self.words) + '>'

    def search_verb(self, lexicon):
        """Look for the verb in a lexicon (a dictionary)."""
        return lexicon.get(self.verb)

    def n_grams(self, n):
        """List of n-grams within the sentence."""
        words = [''] + self.words + ['']
        return [words[i:i+n] for i in range(len(words)-n+1)]
#        result = []
#        for i in range(len(words)-n+1):
#            result.append(words[i:i+n])
#        return result

class TransitiveSentence(Sentence):
    """Sentence with an object."""

    def __init__(self, words, verb='', sbj='', obj=''):
        """Assign object as well as other attributes."""
        Sentence.__init__(self, words, verb=verb, sbj=sbj)
        self.obj = obj

File IO

__main__

Miscellaneous features

Some Python resources