Intro to Iteration Flashcards
Iteration
A sequence of instructions that repeats
for loop
A kind of loop that will loop over every element in something iterable, such as a list.
Syntax for for Loops
for my_element in my_list:
print(my_element)
for is a reserved keyword in Python. Python recognizes for as the beginning of a for loop.
Replace the my_element part with a name that represents what each list element is. It will be used as a variable, which during iteration will take on the value of each list element, one at a time.
in is a reserved keyword in Python. It separates the name of the my_element variable and the list we’re iterating over.
Replace my_list with the desired list to iterate over. This can be a list literal, a variable that holds a list, or any other expression that evaluates to a list.
The colon : begins the for loop’s body
Replace print(my_element) with any code that should execute during each loop. This is the loop’s body.
for Loops for Keys and Values Need .items()
To iterate over the key-value pairs of a dictionary, we use the following syntax:
for my_key, my_value in my_dict.items():
print(my_key, my_value)
break
A keyword that will exit an entire loop. break is a keyword that can be used in a for loop. When the Python interpreter encounters a break, it will immediately exit the current loop, skipping any instructions from after the break until the end of the loop. We also say that it breaks out of the loop.
continue
A keyword that will immediately advance one interation in a loop
range()
A Python function that creates a sequence
for-else
It turns out we can pair a for loop with an else clause! The else clause will run after a for loop finishes if it did not encounter a break. The else keyword must be at the same level of indentation as the for, and be followed by a :. The else body begins indented one level on the following line.
continue Advances One Loop
continue is another keyword that can be used in a for loop. When the Python interpreter encounters a continue, it will immediately jump to the beginning of the loop, skipping any instructions from after the continue until the end of the loop. The loop will resume execution with the next item in the iteration, if one exists. Otherwise, the loop will terminate.
range() Creates a Sequence
Python allows us to create a sequence of numbers using the range() function, much like:
1, 2, 3 499, 500, 501, 502 3, 6, 9, 12, 15 0, -1, -2, -3.
The function signature of range is:
range(start, stop, step)
start is the number at which the range should start, and stop is the number at which the range should stop. stop is exclusive, which means the stop number itself will not be included in the range. step is the amount by which the range should increment. step is an optional argument, and its default value is 1.
Ranges in for Loops
We can pair ranges with for loops so that we iterate over a sequence of numbers. To do this, instead of a list or dictionary, we put in a range.
Look at this example:
for number in range(0, 6):
print(number)
Note that it printed up to 5, and it didn’t print 6. Recall that the stop parameter, is exclusive. It goes up to, but excludes, the end value.