for
Loops
A for
loop is a control structure that we use to repeat certain pieces of code (and avoid all that copy-pasting).
control structure: a part of the language that allows us to dictate when and how (and how many times) certain pieces of code are run. We can consider functions to be one type of control structure. (Though they are so important that they just get to be called functions most of the time).
Let’s take a look at the syntax of a for
loop.
for variable_name in [options_list]:
# do things
print(variable_name) # for example, print each element
print("done") # this is outside of the for loop
There are a few important pieces here:
variable_name
this will be how we access the next value in the sequence that we are iterating over.[options_list]
this is a sequence of values. Thefor
loop will iterate (repeat) once per value in this list.- indentation: notice that everything that we want repeated must be indented “inside” the
for
loop. Once we return to our base level of indentation, the code will no longer be repeated.
Let’s take a look at an example that we can run.
for
loops with range()
Remember when we made you explore the range()
function earlier in the course? You may have wondered what possible use its wacky return value would serve.
range_return = range(10)
print(range_return) # range(0, 10)
print(type(range_return)) # <class 'range'>
It turns out that this function is extremely useful (and extremely commonly used) with for
loops!
Take a look at the following code:
We can use the range()
function to easily produce long lists of integers that we can use to control how many times a for
loop iterates.
Say, for example, that we want to repeat the same code 10 times:
for num in range(10):
print("yay!")
Say we want to repeat the same code 1000 times:
for num in range(1000):
print("yay1000!")
That’s a whole lot easier than typing out a list with 1000 elements!
parameters with range()
: you can pass the range function 1, 2, or 3 ints. Let’s take a look.
for
loops over strings
We can also iterate over strings as if they were lists of characters using a for
loop.
for letter in "my string":
print(letter)
Notice that we can also iterate over a string that has been saved in a variable in this way.
Iterating over strings and lists using indices
Lastly, let’s take a look at combining these strategies. We’ll write for loops that do the same things as the examples above, but using the range()
function and the len()
function.
There are some instances in which we must use this this strategy with strings. Say, for instance that we want to write a function that takes a string and returns the last half of that string to the user. (Without using tools that we haven’t learned about yet).