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
do you have to change some aspect of the guard condition in the body of the while loop?
yes, so it will eventually by false
we use the word “bug” to refer to….
any ____ in your code
anything your code did that didn’t match _____ __________
error
your intention
syntax error
code violates the rules of the programming language, and thus cannot be run
runtime error
code runs, but enters an illegal states or tires to do something impossible
logical error
this cause your program to operate incorrectly, but not crash
syntax is correct, the code doesn’t crash, but the output is incorrect
are list mutable?
what would this do:
my_list = [1, 2, 3, 4, 5]
my_list[2:4] = [98, 99, 100]
print(my_list)
yes
my_list = [1, 2, 98, 99, 100, 3, 4, 5]
if it is just one that you are adding, it can be an integer
if it is more than one, it has to be a list
what would this do:
my_list = [1, 2, 3, 4, 5]
my_list.insert(2, 77)
[1, 2, 77, 3, 4, 5]
what would this do:
my_list = [1, 2, 3, 4, 5]
my_list.append(100)
[1, 2, 3, 4, 5, 100]
if a list is appended, there will be a list inside of a list
what would this do:
my_list = [1, 2, 3, 4, 5]
my_list.extend([81, 82, 83])
[1, 2, 3, 4, 5, 81, 82, 83]
it has to be a list