Intro to Iteration Flashcards

1
Q

Iteration

A

A sequence of instructions that repeats

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

for loop

A

A kind of loop that will loop over every element in something iterable, such as a list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Syntax for for Loops

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

for Loops for Keys and Values Need .items()

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

break

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

continue

A

A keyword that will immediately advance one interation in a loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

range()

A

A Python function that creates a sequence

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

for-else

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

continue Advances One Loop

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

range() Creates a Sequence

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Ranges in for Loops

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly