Unlike a while loop, a for loop is used for this type of iteration, meaning it runs a fixed, known number of times.
What is definite iteration?
A while loop continuously executes its indented code block as long as its test condition evaluates to this value.
What is True?
Python use this number inside square brackets to access the very first element.
What is 0?
This list method adds a single item of any data type to the very end of a list.
What is .append()?
If you forget to update your control variable inside a while loop, you create this type of bug.
What is an infinite loop?
Every for loop header must end with this to indicate the beginning of the indented loop body.
What is a colon (:)?
Along with Initialization and a Test Condition, this is the third essential ingredient of a while loop that modifies a variable so the loop eventually stops.
What is Update State (or updating the control variable)?
Because elements in a Python list can be changed, added, or replaced after creation, lists are described by this property.
What is mutable?
Given a list of numbers, these two built-in utility functions quickly return the smallest and largest values stored in the list.
What are min() and max()?
Using while i < 5: instead of while i <= 5: when trying to print the numbers 1 through 5 is an example of this common programming bug.
What is an off-by-one error?
Calling range(5) produces this exact sequence.
What is 0, 1, 2, 3, 4?
Unlike an if statement that checks a condition once, a while loop checks its condition at this exact point in every single repetition.
What is before running any inner code (at the top of the loop)?
Given letters = ["A", "B", "C"], using negative indexing with letters[-3] returns this element.
What is "A"?
Starting with items = ["A", "C"], calling items.insert(1, "B") shifts "C" to this index position.
What is index 2?
In the code below, this is the exact sequence of numbers printed before break terminates the loop:
for i in range(1, 6):
if i == 4:
break
print(i)
What is 1, 2, 3?
If you run a for loop over a string using for char in "CODE": and print char on each pass, this is the value stored in char during the third iteration.
What is "D"?
In an input validation loop that starts with password = "" and loops while password != "secret123":, typing "secret123" causes the loop condition to evaluate to this value, exiting the loop so "Access Granted!" can print.
What is False (since "secret123" != "secret123" is False)?
Given vals = [5, 10, 15, 20, 25], vals[2:] returns this list.
What is [15, 20, 25]?
Given scores = [80, 95, 70, 90], this built-in expression calculates the average score using two list utility functions covered in class.
What is sum(scores) / len(scores)?
Calling fruits.remove("grape") when "grape" is not present anywhere in the fruits does this.
What is raise a ValueError?
This is the exact output printed by the following code segment:
total = 0
for x in range(2, 11, 3):
total = total + x
print(total)
What is 15 (2 + 5 + 8)?
This is the final value of x after the following loop finishes executing:
x = 5
while x > 2:
print("Loading Game...")
x = x - 1
What is 2 (the loop runs for 5, 4, and 3, then stops when x becomes 2)?
Given nums = [10, 20, 30, 40, 50], slicing with nums[1:4] means the element at index 4 (50) is treated this way by Python.
What is excluded (the stop index is exclusive, so slicing stops before index 4)?
Given letters = ["a", "b", "a", "c"], this is the exact sum returned by evaluating letters.index("b") + letters.count("a").
What is 3 (letters.index("b") returns 1, and letters.count("a") returns 2)?
This is the exact output printed by the following code segment combining range(), continue, and a list:
items = ["a", "b", "c", "d"]
for i in range(len(items)):
if i == 2:
continue
print(items[i])
What is a, b, d (on separate lines; index 2, which is "c", is skipped)?