Bit Reference

We'll use the simple "bit" robot for our first examples as an on-ramp for Python functions in CS106A. Bit lets us play with 100% standard Python code, but in a way where the results of each line are visual and immediate, so it's a handy way to see how the pieces of code fit together.

The examples showing how to use bit are in the lecture notes; this page is just the reference of bit functions.

The simple bit robot exists in a rectangular world made of little squares. Each square in bit's world can be painted one of 'red' 'green' or 'blue' or left unpainted white. The black squares are solid, and bit can't move there.

The bit robot can take just a few actions in the world - each action corresponds to a Python "function" that performs that action.

Here is a picture of bit at the upper left of a world, before taking some actions:

alt bit in empty world

If bit then does these actions:


Then the world will look like this:

alt: bit in world with 2 squares painted

Bit Functions

As a practical matter, bit code is inside functions like this:
show go() function and before/after bit world

Bit From Filename

Your code is shielded from the details, but a bit world is stored in a plain text file with a name like '' or ''.

The function "Bit" (uppercase 'B') is a function which takes a filename parameter (e.g. ''), reads the rectangular world into memory with a bit in it, and returns a reference to the bit. A bit always exists within the context of a specific world in this way.

So your code can create a bit in, say, a world named '' like this:

  bit = Bit('')

More often the first line in a function looks like this:

def do_something(filename):
    bit = Bit(filename)

The filename is passed in as a parameter, e.g. '', and the first line creates bit from the that filename and then goes ahead.