Quiz 3 Material Flashcards
if vs. elif vs. else
- if is the first and main expression
- elif is optional and it is another conditional that python checks for (you can have as many as you want)
- else is also optional, if none of the above expressions are true, then these statements run
= vs. ==
= assignment
== checking equality
does order matter for strings and is repetition ok?
yes and yes
does string indexing start at 0 or 1
0
write an expression that would give the length of this string, a positive index for l, and a negative index for l:
s = ‘wilson’
len(s)
s[2]
s[-4]
indexing is how to get ___ thing out of a collection
slicing is how to get _____ out of a collection
one
a chunk
what are the different variations of the slicing expression?
s[start:end]
s[start:]
s[:end]
s[start:end:step]
do we include or exclude the end index?
exclude
how are for loops formatted?
for <variable> in <collection>:
statements</collection></variable>
which numbers are include in range(8)
0,1,2,3,4,5,6,7
in a range, is the stop exclusive or inclusive? what about the start?
stop: exclusive
start: inclusive
what if the start is greater than the stop?
nothing will print, unless the step is a negative integer
accumulator pattern
- idea: loop through some collection and “accumulate” some stuff
- start with nothing (initialize the accumulator variable)
+= -= *= /= %= **= //=
augmented assignment operators
variable = variable + 1
variable = variable - 1
how are while loops formatted?
while boolean_expression:
statement(s)
keeps doing the action over and over so long as the boolean expression is true