Quiz 3 Material Flashcards

1
Q

if vs. elif vs. else

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

= vs. ==

A

= assignment
== checking equality

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

does order matter for strings and is repetition ok?

A

yes and yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

does string indexing start at 0 or 1

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

write an expression that would give the length of this string, a positive index for l, and a negative index for l:

s = ‘wilson’

A

len(s)
s[2]
s[-4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

indexing is how to get ___ thing out of a collection
slicing is how to get _____ out of a collection

A

one
a chunk

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what are the different variations of the slicing expression?

A

s[start:end]
s[start:]
s[:end]
s[start:end:step]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

do we include or exclude the end index?

A

exclude

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how are for loops formatted?

A

for <variable> in <collection>:
statements</collection></variable>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

which numbers are include in range(8)

A

0,1,2,3,4,5,6,7

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

in a range, is the stop exclusive or inclusive? what about the start?

A

stop: exclusive
start: inclusive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what if the start is greater than the stop?

A

nothing will print, unless the step is a negative integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

accumulator pattern

A
  • idea: loop through some collection and “accumulate” some stuff
  • start with nothing (initialize the accumulator variable)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

+= -= *= /= %= **= //=

A

augmented assignment operators
variable = variable + 1
variable = variable - 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how are while loops formatted?

A

while boolean_expression:
statement(s)

keeps doing the action over and over so long as the boolean expression is true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

do you have to change some aspect of the guard condition in the body of the while loop?

A

yes, so it will eventually by false

17
Q

we use the word “bug” to refer to….
any ____ in your code
anything your code did that didn’t match _____ __________

A

error
your intention

18
Q

syntax error

A

code violates the rules of the programming language, and thus cannot be run

19
Q

runtime error

A

code runs, but enters an illegal states or tires to do something impossible

20
Q

logical error

A

this cause your program to operate incorrectly, but not crash
syntax is correct, the code doesn’t crash, but the output is incorrect

21
Q

are list mutable?
what would this do:
my_list = [1, 2, 3, 4, 5]
my_list[2:4] = [98, 99, 100]
print(my_list)

A

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

22
Q

what would this do:
my_list = [1, 2, 3, 4, 5]

my_list.insert(2, 77)

A

[1, 2, 77, 3, 4, 5]

23
Q

what would this do:
my_list = [1, 2, 3, 4, 5]

my_list.append(100)

A

[1, 2, 3, 4, 5, 100]
if a list is appended, there will be a list inside of a list

24
Q

what would this do:
my_list = [1, 2, 3, 4, 5]

my_list.extend([81, 82, 83])

A

[1, 2, 3, 4, 5, 81, 82, 83]
it has to be a list

25
Q

what will each of these do?
numbers.remove(4)
numbers.pop(3)
numbers.pop()
del numbers[2]

A

if 4 is in the list, it will remove 4 (if not there is an error)
removes and returns the value at index 3
removes and returns the last value
deletes the value at index 2

26
Q

what will each of these do?
numbers * 3
number + [3]
numbers + numbers

A

take a list and turn it into the list copied three times put into one big list
add 3 to the end of the list
turns the list into a big list with 2 copies of the original list

27
Q

what will each of these do?
numbers.index(6)
numbers.count(6)
numbers.sort()
numbers.reverse()

A

give the index that the value 6 is in the list (error if not in the list)
give the number of times that 6 is in the list (0 if not in the list)
sort from smallest to largest
reverses the order of the list

28
Q

aliasing

A

making a value known by two different names and can be changed by either one

29
Q

for a list passed to functions, modifying the parameter will __________ change the value outside of the function

A

permanently

30
Q

are tuples mutable?

A

no

31
Q

in a nested loop, the inner loop must finish _____ (before/after) outer loop increments once

A

before