topics for the day: * strings * definite loops
identify reserved words and identifiers:
def mystery(whatever):
answer = whatever % 2
return answer
reserved words: def return
(2 total)
identifiers: mystery whatever answer
(5
total)
List all the expressions (including subexpressions) that occur in the statement:
neato = (3 * cool) + beans
answer, there are 5 expressions (4 subexpressions): 3 cool (3
* cool) beans (3 * cool) + beans
neato
is an
identifier, but not an expression in this context.
we can use strings as we do other expressions, write functions with them etc, as in:
def question(s):
t = s + '?'
return t
def strange(t):
y = 3*len(t) + 1
return y
continuing our grammar, sentences can be grammatical and come close to making sense but then still not make sense when we have a category (or type) error:
types refer to categories of values for which certain operations/functions are defined (and many others are not)
so, for example, 15 - 'hello'
does not make sense
care, though must be taken with strings due to operator
overloading: the same operator can be used to mean very different
things depending on the types of its operands; this does not happen that
often in Python, but it does, notably, with the +
operator
which in the context of numbers means, as expected, arithmetic addition;
in the context of strings it means concatenation: glue two
strings together to result in new string. Arithmetic addition is
commutative, string concatenation is not!
>>> 756 + 1
757
>>> 1 + 756
757
>>> 'f' + 'oo'
'foo'
>>> 'oo' + 'f'
'oof'
Loops are statements that tell the computer to repeatedly execute one or more other statements. Definite loops tell the computer to execute those statements a precise number of times, independent (largely) of what those statements do.
>>> for _ in range(4):
print('CS stands for Cool Stuff')
'CS stands for Cool Stuff'
'CS stands for Cool Stuff'
'CS stands for Cool Stuff'
'CS stands for Cool Stuff'
The first form of definite loop looks like:
for _ in range(<integer>):
<one or more statements> # <-- we call this the "loop body"
a lot of “magic” in there: for
and in
are
reserved words; range
is a built-in function (we will see a
bit more about what range means later, but for now think of it a
required syntax for this form of loop). The argument inside
range
can be any expressions that evaluates to an
integer.
the effect is: do the loop body the number of times that the
expression in range
evaluates to.
we will sometimes use an intefer literal (like 4
, above)
for simple examples; this form simply saves us the effort (comptuer
scientists are lazy!) of typing the code to be repeatedly executed over
and over.
more frequently, we will use a variable in the range expression - that not only saves typing, but offers us generality: we the computer will executre the loop body the number of times corresponding the value of that variable.
we can achieve multiplication by repeatedly adding:
def slow_mult_by_4(n):
a = 0
for _ in range(4):
a = a + n
return a
although we would not choose to do it this way! (much easier to just
use n * 4
)
moreover, we can repeately multiply to achieve exponentiation (python has that built in… but it’s just a short hand for same effect):
def pow2(m):
p = 1
for _ in range(m):
p = p * 2
return p
we add up the first n numbers:
def sum_n(n):
total = 0
counter = 1
for _ in range(n):
total += counter
counter += 1
return total