Free Online Flashcard Deck

4 Loops and Iteration Free Online FlashCards

Study 4 Loops and Iteration with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.

12 cards
01
Front

What does a Python while loop do?

Back

A while loop repeats its indented body as long as its condition remains true.

02
Front

What does a Python for loop iterate over?

Back

A for loop visits each item supplied by an iterable, such as a list, string, tuple, dictionary, set, or range object.

03
Front

Why can a while loop run forever?

Back

A while loop may never terminate if its controlling condition never becomes false and no break statement executes.

04
Front

What is a sentinel in a loop?

Back

A sentinel is a special value that signals the end of input, often allowing a loop to stop with break.

05
Front

Which values does range(2, 6) supply?

Back

The stop value is excluded. Therefore, range(2, 6) supplies 2, 3, 4, and 5.

06
Front

What happens when range() has a zero step?

Back

A range with step zero is invalid. A positive-step range can also be empty when start is greater than or equal to stop.

07
Front

When should you use enumerate()?

Back

Use enumerate() when both the index and the item are needed; it is generally clearer than manually indexing with range(len(...)).

08
Front

What does break do?

Back

break immediately terminates the innermost enclosing loop, then execution continues with the first statement after that loop.

09
Front

When does a loop else clause run?

Back

A loop else clause runs when the loop finishes normally, without executing break. For a for loop, this means the iterable is exhausted.

10
Front

How does a loop accumulate a result?

Back

Initialize the accumulator before the loop and update it during each iteration, such as total += value.

11
Front

How can a loop count matching items?

Back

Use a counter initialized before the loop, then increment it whenever an item satisfies the condition.

12
Front

What is a safe way to filter a collection?

Back

Build a new collection, such as with append(), instead of changing the collection currently being traversed.