Today: finish nested range loops, expression math,

First: back to lecture-4 to finish off nested y,x range loops

Recall: Statement and Expression

A statement is a bit of code that runs, but does not return a value, like this example with =:

x = 67

Bit statement

bit.move()

An expression is a bit of code that runs and produces a value, here represented by a little arrow:

Math example expression

1 + 2 * 3 → 7

Bit function-call expression

bit.front_clear() → True

Python Interpreter

You can get the interpreter on your computer by typing "python3" ("python" on Windows) in a terminal. On parlante.org, use the hack mode interpreter page

For details see Python Guide: Python Interpreter

Type code directly to the Python interpreter at a >>> prompt. If the typed line is an expression, Python evaluates it and prints the resulting value. If the typed line is a statement, Python runs it and prints nothing. Very handy way to explore; we'll use this below to see how computer math works.

>>> 23 + 6    # user types this
29            # see result
>>> x = 67    # statement
>>> x + 1     # expression
68
>>>

Math

Python has support for two types of number - "int" and "float". For more detail, see Python Guide: Python Math

int numbers

Use of ints - Indexing

float numbers

>>> 1.5 + 1.5
3.0
>>> 
>>> 1.0 + 2.0
3.0
>>> 

Math Operators: + - * /

>>> 2 + 3
5
>>> 2 + 3 * 4   # Precedence
14
>>> 2 * (1 + 2) # Use parenthesis
6
>>> 10 ** 2
100
>>> 2 ** 100  # Grains of sand in universe
1267650600228229401496703205376
>>>
>>>
>>> 1 + 2 + 3
6
>>> 1 + 2.0 + 3  # Makes result float
6.0

Division / → float

>>> 7 / 2
3.5
>>> 8 / 2
4.0

Int Division // → int

>>> 7 // 2
3
>>> 8 // 2
4
>>> 102 // 4
25

The Command Line

The command line is how your computer works "under the hood", and we'll use it in CS106A. Not pretty, but very powerful.

For details see Python Guide: Command Line

The above guide has instructions to download this example folder: hello.zip

Unzip that folder. Open the folder in PyCharm (not the hello.py, the folder). In PyCharm, select "Terminal" at lower left - that's the command-line area. Then try running the hello.py program ("python3" on the Mac, "python" or "py" on Windows).

$ python3 hello.py Alice
Hello Alice
$ python3 hello.py Bob
Hello Bob
$ python3 hello.py -n 100 Alice
Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice Alice 

Graphics Command Line Demo

Fri's example will use graphics and the command line - Nick demos to show off the power of the command line a little.

$ python3 draw1.py -lines2 600 400 7