CS106A - Lecture 1 - Code and Bit

Today: Bit, code, functions, and the while-loop

CS106A: Computer - Code - You


Story #1 - Bit's World - Simple

We have this "bit" robot, lives in a rectangular world divided into squares, like a chessboard. We'll use bit at the start of CS106A to introduce and play with key parts of Python. The nice thing about Bit code is that each action has a tangible result, as bit move around in the world.

Bit can do 4 actions in the world: move, left, right, paint

Here is a more detailed guide of Bit's features: Bit Reference

Bit Example

Suppose Bit starts at the upper left corner facing to the right, and then the following code runs:

    bit = Bit('5x5.world')  # Create "before" picture
    bit.move()
    bit.paint('blue')
    bit.right()
    bit.move()
    bit.paint('green')

Before:
alt: bit in empty world

After:
alt: bit in world with 2 squares painted

The syntax bit.move() is a Python function call. A function hold some code. To "call" a function runs its code. This is the noun.verb function call form.

Story #2 - Program and Functions

A "program" (or "app") is a collection of code for the computer to run. The code in a program is divided into smaller, logical units of code called "functions", much as the sentences of an essay are divided into logical paragraphs.

Python programs are written as text in files named like "example.py". The program text will be divided into many functions, each marked by the word def:

alt: program is made of functions, each with def

Story #3 - Example Function Def Syntax

Here is the Python code for one function that moves bit around a little. There is syntax here - not pretty, but easy enough to get used to. Computers need a fixed syntax to understand the code you type in. More about syntax below.

def blue_green(filename):
    bit = Bit(filename)
    bit.move()
    bit.paint('blue')
    bit.right()
    bit.move()
    bit.paint('green')

Next: (1) we can run this code, see what it does. (2) Look at the syntax of a function-def

Let's Run This Function

Here is a "live" on the web version of the above def. Click "Run" on the page and watch what it does. You can use the "steps" slider to run the code forwards and backwards, seeing the details of the run.

> bit1 blue_green function

5 Things About def

So having seen a function run, now look at the details of the syntax. Sorry, it's a bunch of small syntactic points.

The def syntax looks weird, but is basically superficial and you will get used to it quickly.

def blue_green(filename):
    bit = Bit(filename)
    bit.move()
    bit.paint('blue')
    bit.right()
    bit.move()
    bit.paint('green')

The first code line sets up a variable named "bit" to point to the robot-world as pictured above, explained in a later lecture
bit = Bit(filename)

What Does bit.move() Mean?

What Does bit.paint('blue') Mean?


A Little Talk About Syntax Errors

Errors - Sport and Fix!

(optional) You Try One: green2

Bit begins at the upper left corner facing right. Paint the square below the start square green, and the square below that green as well, ending like this:

alt: 2 square below start square are now green

The word pass is a placeholder for your code, remove it. Feel free to make some syntax errors getting this to work!).

> green2

Suppose I Give You This Problem

Bit starts at the upper left. Move bit forward until reaching a wall, painting every moved-to square green. The resulting world looks like:

alt: bit moves across top of world, painting every moved-to square green

Statement vs. Expression

bit.front_clear()

Run go-green code

First we'll run the code. This code is complete. Study it to see how the loop works.

> go-green

For reference, here is the code:

def go_green(filename):
    bit = Bit(filename)
    while bit.front_clear():
        bit.move()
        bit.paint('green')

While Loop Syntax

Syntax 4 parts: while, test-expression, colon, indented body lines

while test-expression:
  body lines inside the while
  indented

While Loop Operation

While Loop Observations

One Code - Many Cases - Generality

While Loop - Power!