1/18
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
loop
A loop is a program construct that repeatedly executes the loop's statements (known as the loop body) while the loop's expression is true; when the expression is false, execution proceeds past the loop
iteration
Each time through a loop's statements is called an iteration.
While loop
A while loop runs a block of code repeatedly as long as the while loop's condition is True.
sentinel value
A sentinel value is a value that causes a loop to end.
Infinite loop
An infinite loop is a loop that never stops running because the loop's condition is always True.
randint() function
provides a new random number each time the function is called
doc string
a multi-line string literal delimited at the beginning and end by triple quotes, which can be either single (') or double (") quotes.
for loop statement
iterates over each element in a container one at a time, assigning a variable to the next element to use in the loop body.
reversed () function
A for loop may also iterate backward over a sequence, starting at the last element and ending with the first element
range() function
generates a sequence of integers between a starting integer that is included in the range, an ending integer that is not included in the range, and an integer step value. The sequence is generated by starting at the start integer and incrementing by the step value until the ending integer is reached or surpassed.
for i in range(4):
print(i, end=" ")
0 1 2 3
Positive integer months_upp is read from input, representing the months lapsed since the beginning of the year. Complete the for loop as follows:
Use curr_month as the loop variable.
Iterate through the decreasing sequence of all the integers from months_upp down to 1, both inclusive.
months_upp = int(input())
print("Months passed:", end=" ")
for curr_month in range(months_upp, 0, -1):
print(curr_month, end=" ")
print()
Integers begin_item and end_item are read from input, representing the items in an assembly line, with begin_item less than end_item. Integer items_passed is initialized with 0. For every fourth item between begin_item and end_item, both inclusive:
Output "Sampling item: " followed by the item's value.
Increment items_passed.
begin_item = int(input())
end_item = int(input())
items_passed = 0
for item in range(begin_item, end_item +1, 4):
print("Sampling item:", item)
items_passed += 1
print(f"Total items passed: {items_passed}")
nested loop
a loop that appears as part of the body of another loop. The nested loops are commonly referred to as the outer loop and inner loop.
incremental programming
by starting with a simple version of the program, and growing the program little by little into a complete version
break statement
cases the loop to exit immediately
continue statement
A continue statement in a loop causes an immediate jump to the while or for loop header statement.
loop else construct
executes if the loop completes normally.
enumerate() function
retrieves both the index and corresponding element value at the same time, providing a cleaner and more readable solution