Lecture 1: Part 1 Welcome

June 21st, 2021


CS106A - Lecture 1 - Code and Bit

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

Program Made of Functions

A computer 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 all the words that make up 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 Robot - Getting Started

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 code driving Bit around is 100% Python code. The nice thing about Bit code is that the actions have tangible results, so you can see what the code is doing.

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

For later reference, here is a more detailed guide to Bit's features: Bit Reference

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

Example do_bit1() Function

Below is a code in a Python function that creates bit in a little world and commands bit with a few actions. There is syntax here - not pretty. Don't worry about all the syntax in there just yet. First let's see what it does.

def do_bit1(filename):
    bit = Bit(filename)  # Creates the "before" picture
    bit.move()
    bit.paint('blue')
    bit.right()
    bit.move()
    bit.paint('green')

1. The function starts bit off in the upper left square of the world, like this (before-run picture):
alt: bit in empty world

2. The code in the function moves bit over a few squares, painting some of them. So running the function does those actions, and afterwards the world looks like this (after-run picture):
alt: bit in world with 2 squares painted

Run It First

1. Experimental Server

Here is the bit1 problem on the experimental server: do_bit1() example

We'll use Nick's experimental server for code practice. See the "Bit1" section at the very top; the "do_bit1" problem is there. Often the code in a lecture example like this will be incomplete, and we'll work it out in lecture. There is a Show Solution button on each problem, so you can always play with it again later and check your answer.

2. Run It

Click the Run button and see what it does. See how bit takes actions in the world first, and then we'll fill in what's going on with the lines of code. You can use the "steps" slider to run the code forwards and backwards, seeing the details of the run.


Now let's look at all of that syntax to see how this works.

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

1. Run a Function = Run Its Lines

The function here, do_bit1, has lines of code in it. Clicking the Run button runs that function - running its lines of code from top to bottom. Running a function is also known as "calling" that function. In this case, each line in the function commands bit to do this or that, so we see bit move around as the function runs from top to bottom.

2. bit.move() = Call the "move" Function

3. bit.paint('blue') = Call "paint" function

4 Other functions: bit.left() and bit.right()

5. Def Syntax

Here are the first few lines of this Python example:

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

This is a def which defines a new function in Python.

6. bit = Bit(filename)


alt: bit in empty world

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.

"Bit Errors" Exercise - Spot and Fix!

Other Errors

Checkmark / Diff Features

(optional) You Try One: bit2

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

Bit begins at the upper left square 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 (needy) computer

> do_bit2() example


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

Statement vs. Expression

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.