CS106A - Lecture 1 - Code and Bit

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

Heads Up - Lots of Shallow

Background - 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

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

For now we'll just jump in with an example.

Bit1 Example - 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.

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

def do_bit1(filename):
    bit = Bit(filename)  # Creates the "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

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.

> do_bit1() example

There's a lot to see with this first example - what do we see here?

1. What does "Run" of a function do?

2. What Does bit.move() Mean?

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

4. Def Syntax

Here is the Python code for the "do_bit1" function

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

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.

The first code line sets up a variable named "bit" to point to the robot-world as pictured above. The first line sets up the bit world, and we're going to gloss over how that works for now.


A Little Talk About Syntax Errors

"Syntax" is not a word that regular people use very often, but it is central in computer code. The syntax of code takes some getting used to, but is not actually difficult.

Error Exercise - Sport and Fix!

(optional) You Try One: green2

Here is an exercise like bit1 with the code left for you to do.

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. Conceptually this does not look hard, but expect to make some mistakes as you get the little syntax and naming details right as required by the (lame, needy) computer

> green2


Suppose I Give You This Go-Green Problem

Bit starts at the upper left as usual. 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

Run go-green code

First we'll run the code. This code is complete. Watch it run to get a feel for how the loop works.

> go-green

Checkmark / Diff Features

bit.front_clear()

go_green() Code

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!

We'll pick up with more while-loop driven exercises next lecture.