ICP22 lecture notes

Monday, September 19

topics for the day:


the simple_graphics module

paint and unpaint are different than functions we have used so far they take two arguments (see below)

>>> paint(5, 7)    # draws a box at row 7, column 5 (counting from 0)
>>> unpaint(5, 7)  # erases the same box
>>> for x in range(20):
        paint(x, 9)   # paints 20 boxes in horizontal line at row 9

provides a different way to visualize effect of loops

what is a module? for now: it’s just a file that defines functions that we can use in other files (much more on modules later)

graphics are side effecting like print. We use to illustrate how things work (and it’s fun); later we will use for games. But it masks a lot of complexity! (and it’s not “timeless” - graphics and how to use them vary widely from system to system and from era to era)


functions can take more than one argument

# trivial example
def add(a, b):
    c = a + b
    return c

Boolean values and expressions

we can generate Boolean values such as these examples:

>>> False
    False
>>> True
    True
>>> b = 3 < 4
>>> b
    True

>>> name = 'Mike'
>>> name == 'Mike'
    True
>>> name == 'MIKE'     # <--- observe case *sensitivity*
    False 
>>> 'no' != 'yes'      # <--- != is the "not equals" operator
    True

>>> a = 99
>>> b = a + 1
>>> a <= a             # 99 is less than *or equal* to 99
    True
>>> a <= b
    True
>>> a < a
    False
>>> a < b
    True
>>> a > b
    False
>>> a != b
    True
>>> b == 100
    True
>>> b >= 10 * 10
    True

Boolean-type expressions are a third type of value we have encountered to date (along with numbers and strings). Actually since numbers fall into two distinct types (integers and floating-point numbers), Booleans make up a fourth category. Python allows programmer to ask for the type of a value using the built-in type function:

>>> type(47)
    <class 'int'>
>>> type(3.14)
    <class 'float'>
>>> type('hello')
    <class 'str'>
>>> type(False)
    <class 'bool'>
>>> x = 13
>>> type(x < 100)
    <class 'bool'>

we can define functions that return Boolean values:

def is_even(n):
    b = n % 2 == 0
    return b

if statements

Although Boolean values can be thought of as an end result in their own right, far and way the most common use of Boolean values in programming is to affect control flow. Up until now, most of the programs we have considered have been “straight line” - meaning each statement is executed in the order they are written within a given function definition. The exception has been definite loops which execute their statements in order a predetermined number of times. With Boolean values we can write algorithms that behave conditionally: “if this then that…” An example:

>>> even = 98 % 2 == 0
>>> even
    True
>>> if even:
        print('Even it is!')
    else:
        print('NOT even')
    Even it is!

>>> even = 4103 % 2 == 0
>>> even
    False
>>> if even:
        print('yes')
    else:
        print('NO!')
    NO!

The syntax for an if-else statement is:

    if <expression>:
        <one or more statements>   # <-- call this the "then" branch
    else:
        <one or more statements>   # <-- call this the "else" branch

The <expression> immediately following if is an expression that evaluates to a Boolean value. If it evaluates to True the “then” branch of statements is executed after which the the “else” branch of statements is skipped over; if it evaluates toe False the then branch is skipped, and the else branch is executed.

Here are two canonical examples:

def absolute_value(x):
    if x < 0:
        y = -x
    else:
        y = x
    return y

def minimum(x, y):
    if x < y:
        z = x
    else:
        z = y
    return z

Python has a command

pass

it means “do nothing”. It is sometimes used as a placeholder (somewhat like how I use ... in lab and homework starter files). So consider this version of absolute value:

def absolute_value:
    y = x
    if x < 0:
        y = -x
    else:
        pass
    return y

Because it is often the case that if a condition is not true then we wish the computer to do nothing, there is a short form version of if which only has the “then” branch:

if something:
    ...   # the "then" branch

as in:

def absolute_value:
    y = x
    if x < 0:
        y = -x
    return y