It is common to use the built-in range() function to generate a sequence of numbers within some interval
the range() function is similar to NumPy arange(), but range() is better for this purpose!
The range() function takes 3 parameters: (start, stop, step)
The stop value is required, but the start and step values are optional
The default value (if unspecified) for start is 0 and for step is 1
The range is inclusive of start, exclusive of stop. i.e.: [start, stop)
Start: the value in which the range function will start at (inclusive)
Stop: the value in which the range function will end out (exclusive)
Step: the value in which the range function will increment its value
The video to the left explains more about how the inputs affect the outputs!
Play with the values of start, step, and stop below and keep running the cell to see how the output changes! See if you can determine what the output will be before you run the cell to test if you really understand the function!
Note: This example uses the built-in list() function here to convert the range to a readable list!
A for loop executes all the statements within its scope for each number in a provided range.
It is common to use the range() function to create the array of numbers passed into the for statement.
In Python, we can also directly iterate over all of the elements in a list without having to use a range.
Watch the video below to see how Python for loops work with both of these methods!
Watch this short video for another example of a for loop!
Nested for loops are loops within other loops, which can also be very useful for solving different kinds of problems!
With nested loops, the inner loop runs through completely each time the outer loop runs once.
For example, this means if we have an outer loop that loops 5 times and an inner loop that loops 3 times, the inner loop of code will run all 3 loops on 5 separate occasions (15 times total), while the outer loop will run 5 times.
Check out this video for a walk through of nested loops!
After watching the video, what do you think will be the output of the following code?
for i in range(1, 4):
for j in range(5, 7):
print(f"i: {i}, j: {j}")
Think about it, write down your answer, then try it out in the trinket below to check!
After you've tested the above combination, change the numbers to see how the answer changes.
Sometimes, you need to iterate through a list or a string while also keeping track of the current index.
For example, if we want to write a program that returns a certain character and its location within a string.
In this case, the previous method of using “for char in string” wouldn’t work because it avoids indexing, but we can use another built-in function called enumerate()
The enumerate() function returns pairs of indices and corresponding elements
Using this in a for loop looks like this:
For strings: for index, char in string
For lists: for index, element in list
(again, where index, char, element, string, and list are identifiers, and can be named anything)
This way, for every element or character, we also have its index value stored.
If this is confusing to you, watch the video below!
Run the code below to observe how enumerating the list produces indices for each element!
Then, change my_list to be a list of length 8 and run it again!
We can use break statements within for loops to exit the loop prematurely
ie. to break the loop, even if the full range has not been reached
Take a look at the code below to see how a break statement can make a loop end without reaching the end of the specified range!