ICP22 lecture notes

Monday, September 26

We have been using the form:

from simple_graphics import *

which means import all the functions from the simple_graphics module.

Going forward, we will prefe to use this form:

from <identifier> import <list of one or more identifiers>

as in:

from simple_graphics import clear, paint, unpaint, ROWS, COLUMNS

This way we make it clear which functions (and constant values, in the case of ROWS and COLUMNS) we need from a module.

We have introduced these standard library imports:

from random import randrange
from time import sleep

We using allcaps for constants: identifiers that represent values that we do not plan on changing. Why use ROWS rather than 25? symbolic abstraction: easier to understand our code, and should it ever change in value we need not change our code.

Reminder:

# wrong
m == 'April' or 'June' or 'September' or 'November'

That’s legal Python grammar, but it does nto mean what we think it means.

Instead use form like:

m == 'April' or m == 'June' or ...

indefinite loops

syntax:

while <exp>:
    <one or more statements>

It means: evaluate <exp>, if it is True then execute the loop body, then repeat until <exp> evaluates to False.

WITH POWER COMES RESPONSIBILITY: unlike with definite (for) loops, we can write indefinite loops that never end: “infinite loops”; this often happens unintentionally and is one of largest sources of software bugs

Turing completeness: computational system that is powerful enough to compute anything that can be computed with any other system.

Indefinite loops give us that completeness; they are more powerful than definite loops.

we can always translate:

for x in range(start, stop):
    ...

to:

x = start
while x < stop:
    ...
    x = x + 1

INDEFINITE LOOPS are more GENERAL than DEFINITE LOOPS; anything you can do with a for you can do with a while but NOT vice versa