A few other pieces of Python syntax we want to be aware of as we work with loops:
range()¶
Python’s range() function allows us to generate a list of integer values. The general syntax:
range(START VALUE, END VALUE, STEP INTERVAL)The default start value for range() is 0, and the default step interval is 1.
So for example, range(6) would include the values [0, 1, 2, 3, 4, 5]. While range(1, 6, 2) would include the values [1, 3, 5].
We can use range() in combination with list() to generate a list of numbers.
list(range(6))would generate the list[0, 1, 2, 3, 4, 5]
We can also use range() as part of a for loop to iterate over a list of numeric values (without having to create that list manually).
# for loop that iterates over values in range
for i in range(0, 3):
print(i)Additional Information¶
For more information on Python’s range() function:
W3Schools, “Python Range Function”
Python documentation, “4.3 The range() Function”
enumerate()¶
In a previous lab, we talked about how each item in a list has an index, or a number that indicates its position in the list. We can use the enumerate() function to generate a list of pairs containing each item in the list and its index.
We can use the enumerate() function as part of a for loop.
# for loop that iterates over list index and values
for index, letter in enumerate('abc'):
print(index, letter)In this last example, for index, letter instructed Python to iterate over both components in the enumerate() output. print(index, letter) instructed Python to print both components for each element.
Additional Resources¶
For more information on Python’s enumerate() function:
W3Schools, “Python Enumerate Function”
Python documentation, “enumerate”
Infinite Loop¶
Loops that have no endpoint are called infinite loops. For example, given the following program:
# assign count variable
count = 1
# while loop
while count <= 5: # initial condition
print ("Python") # print statement
count = count + 1 # reassign count
# final print statement
print ("Done")What would happen if we removed count = count + 1 from the loop? The value of count would never change, the initial condition’s truth value (count <= 5) would never change (because count would always equal 1), and we would have an infinite loop.
Break & Continue¶
We can exit a loop immediately by using the break statement. break will stop or exit the while loop even if the condition is true.
# assign i variable
i = 1
# while loop
while i < 6: # initial condition
print(i) # print statement
if i == 3: # if statement
break # break statement
i += 1 # reassign i1
2
3
In this example, the loop breaks as soon as the i == 3 condition is True.
We can skip the rest of the body of a loop and move on to the next iteration using continue.
Another example:
# assign the i variable
i = 0
# while loop
while i < 6: # initial condition
i += 1 # reassign i
if i ==3: # if statement
continue # continue statement
print(i) # print statementIn this example, the current iteration of the loop will stop when i == 3 is true. Unlike with break, the loop will not end. Instead when i == 3 is true, the loop will skip over the final nested print statement and return to the beginning of the loop for a new iteration.
Additional Resources¶
For more on break and continue:
W3Schools, “Python break Keyword”
W3Schools, “Python For Break”
W3Schools, “Python Continue For Loop”
Comprehension Check¶
![]() | Additional Python Loop Considerations Comprehension Check |
