week 6 nested loops Flashcards

1
Q

while

A

executes code as long as statement is true, you want it to be the condition you don’t want so it runs until it meets you goal

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

while loop syntax

A

while (condition):
# code

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

sentinel while loop

A

has expected value, loops run while you don’t have the expected value

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

counting while loop

A

loop runs a set number of times and the iteration value is increased inside the while loop

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

for loops

A

initializes increment value and step size within first line

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

for i in range(1, 4, 1):

what is the significance of each number?

A

1 is the initial value (inclusive)
4 is end value (exclusive)
1 is step size

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

range(5)

A

0 1 2 3 4

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

for each

A

for loops iterates over container and accesses each element of the container

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

for each syntax

A

for ch in text:
# code

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

loop

A

program construct that repeatedly execute statements based on set conditions

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

iterations

A

each time through the loop’s statements are executed

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

loop body

A

code inside the loop

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

sentinel value

A

expected value you want to reach after the loop is executed

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

infinite loop

A

loop will always execute because expression is always true

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

how do you exit an infinite loop?

A

ctrl-c

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

docstring

A

multi-line string literal delimited at start and end with triple quotes ‘’’

17
Q

random.randint()

A

function that generates random integer

18
Q

random.randint(0, 2)

A

generates random int between 0 and 2 (2 exclusive)

19
Q

loop variable

A

counts number of loop iterations and is updated in the loop body

20
Q

count down

A

can be done by decreasing the loop variable

21
Q

uses of a for loop

A

updating elements of a container, printing certain elements, checking each elements, etc

22
Q

reversed() function

A

goes through list backwards

23
Q

when to use for loop versus while loop

A

for:
iterations are computable before entering loop
accessing containers

while: iterations not computable before entering loop

24
Q

nested loop

A

loop within a loop, consists of inner and outer loop