Loops
break
A break statement in a loop causes an immediate exit of the loop.
continue
A continue statement in a loop causes an immediate jump to the while or for loop header statement.
reversed()
A for loop may also iterate backwards over a sequence, starting at the last element and ending with the first element, by using the reversed(container) function to reverse the order of the elements.
nested loop
A loop that appears as part of the body of another loop
iteration
Each execution of the loop body is called an iteration, and looping is also called iterating.
develop programs incrementally
Experienced programmers develop programs incrementally, starting with a simple version of the program, and then growing the program little-by-little into a complete version.
enumerate(container)
The enumerate() function retrieves both the index and corresponding element value at the same time. returns (index, value)
count
The programmer can use a variable to count the number of iterations, called a count
Unpacking
Unpacking is a process that performs multiple assignments at once, binding comma-separated names on the left to the elements of a sequence on the right. For example, num1, num2 = [350, 400] is equivalent to the statements num1 = 350 and num2 = 400.
when to use while or for loops
Use a for loop when the number of iterations is computable before entering the loop, as when counting down from X to 0, printing a string N times, etc. Use a for loop when accessing the elements of a container, as when adding 1 to every element in a list, or printing the key of every entry in a dict, etc. Use a while loop when the number of iterations is not computable before entering the loop, as when iterating until a user enters a particular character.
infinite loop
a loop that will always execute because the loop's expression is always True.
multi-line comment
delimited at the beginning and end by triple-quotes
while loop
executes a block of code as long as the loop's expression is True. ex : while expression: # Loop expression # Loop body: Sub-statements to execute... # if the loop expression evaluates to True... # Statements to execute after the expression evaluates to False
range(start , end , stride)
generates a sequence of numbers, starting at zero and ending before a value given inside the parentheses. For example, for i in range(3) sets i to 0 during the first iteration of the for loop, i to 1 during the second iteration, and finally i to 2 on the third iteration. The value within the parentheses is not included in the generated sequence. Evaluating the range() function creates a new "range" type object. Ranges represent an arithmetic progression, i.e., some sequence of integers with a start, end, and step between integers. The range type is a sequence type like lists and tuples, but are immutable. In general, range objects are only used as a part of a for loop statement.
loop body
indented code block of the loop
for loop
loops over each element in a container one at a time, assigning the next element to a variable that can then be used in the loop body.
outer loop and inner loops
nested loops are the inner loops and outer loops are the outer loops