ICP22 lecture notes

Thursday, September 8

topics for the day:

what we have done so far (first lecture, first lab):

example, suppose in the IDLE editor we have save a file called simple.py consisting of

def add_one(x):
    y = x + 1
    return y

we then use the IDLE option to “Run Module” and then that function (add_one) becomes “visible” in the shell:

>>> add_one(6)
    7

We can visualize how the 6 becomes the value of x when Python evaluates add_one(6) by using: Python Tutor


syntax of simple functions

they take one ARGUMENT and RETURN return one value

def cool(x):   # x is the "argument" (a.k.a. input)
    y = 3 * x + 1
    return y

schematically:

def <identifier>(<identifier>):
    <one or more statements>
    return <identifier>

from lab, we saw one way to do this:

c = (f - 32) / 9/5

the computer reads (SCANS) that as:

c = ( f - 32 ) / 9 / 5

which gives us the wrong answer! We (humans) are often used to seeing 9/5 represent the fraction “nine fifths” - a single entity

computer SCAN sequences of symbols (the file!) and breaks them into discrete LEXICAL ELEMENTS

it then PARSES the code following usual order of operation rules and then “breaks ties” using left-to-right reading

so the problem above was that it first divides by 9, then by 5. One way to fix the problem would be to use ()s to disambiguate:

    (f - 32) / (9/5)

WHITESPACE

Programming languages (PLs) generally ignore whitespace other than to separate lexical elements. However, Python is an exception: it ignores most whitespace, but linebreaks and spaces at the beginning of lines (INDENTATION) matter.

Python scans

def simple(blah):
    blech = blah + 47
    return blech

as

DEF <ID> LPAREN <ID> RPAREN COLON NEWLINE
INDENT <ID> EQ <ID> PLUS <NUMBER> NEWLINE
INDENT RETURN <ID> NEWLINE

what is an identifier?

Examples:

students
computer_science_students
person0
person1
_99_fooBAR             # <-- bad idea!

syntactic categories

function definition syntax

DEF <ID>(<ID>):
   <assignment statements>
   return <ID>

assignment-statement syntax

<ID> = <expression>

expressions (where the fun is at!)

compound expressions refer to other (smaller) expressions - their syntax is recursive


strings

strings represent text

they are sequences of “characters” (loosely, keyboard symbols)

string literals can be specified using either '' ore "" - but we will almost exclusively use single quotes in this class

string literals are another type of data (values)

>>> 'Whatever'
    'Whatever'
>>> first_name = 'Mike'
>>> first_name
    'Mike'
>>> last_name = 'Siff'

string concatenation: the gluing together of two strings to form a larger string

we use the + symbol to concatenate two strings

>>> full_name = first_name + last_name
>>> full_name
    `MikeSiff`

caution: + looks identical to numeric addition symbol (it’s the same symbol) but using it on string is really different (more on that soon)

we can compute the length of a string using the built-in len function:

>>> len('Hello')
    5
>>> len('')   # '' is the "empty string"
    0
>>> len(full_name)
    8

“print” is an anachronism - it is a holdover from a time when computers did not have screens; to get output we needed to print it on a printer

print is a built-in function in Python

it is side-effecting - in this case, it *changes what the user perceives on the screen

it does not return anything

functions that do not return anything, but instead have side effects are often referred to as “procedures”

procedures calls act as a new form of statements that can be thought of as a command:

print('Hello!')    # command to output the string hello

in IDLE shell:

>>> print('Hello!')
    Hello!

print displays the string to the screen without the surrounding quotes