Python loops
There are two kinds of loops in Python. In this guide we'll cover both the "for" loop and the "while" loop. We'll also cover how to control these loops by continuing and breaking out.
There are two kinds of loops in Python. In this guide we'll cover both the "for" loop and the "while" loop. We'll also cover how to control these loops by continuing and breaking out.
The for loop in Python is used to iterate through sequence data types. Some example of sequence data types in Python are string, list, tuple, xrange, etc.
Supposing you have a list:
my_list = [2, 3, 5, 7, 11]
To iterate through this list using a for loop you can do the following:
for item in my_list:
print item
The for loop always looks like this. item
is a variable name that we choose. As we iterate through the list each element will be assigned to the item
variable within the block of code.
For loops are used when we want to look at each element in some sequence. So the block of code inside the for loop is the code that will be executed once for each element in the sequence.
A while loop is used to repeatedly execute a statement as long as the condition is true. Unlike for loops, while is not connected with any sequence. For loops iterate for as many elements as are in the sequence, but while loops iterate as long as the condition is True. Here is a simple example to print all even numbers less than 100.
i = 0
while i < 100:
if i % 2 == 0:
print i
i = i + 1
In this example, we are setting a counter i. Then we set up a while loop to execute the following statement as long as the condition i <= 100 is True. The statement simply tests to see if the number we are testing is even by using the modulo operator. In this case, we need to manually increment to counter which is why we finish with i = i + 1. So each time we loop through, the value of i will be increased by 1.
Sometimes you'll want to continue to the next iteration without executing the following code. Continue can be used the same way in both for loops and while loops. We could rewrite the previous example using continue.
for item in xrange(100):
if not item % 2 == 0:
continue
print item
This does the same thing except we are using a for loop and xrange. Xrange gives us a range of numbers between 0 and 100. And instead of only printing item if it is even, we continue to the next iteration if it is not even. The continue statement means we won't execute the code following it, and instead move on to the next iteration through the loop.
In some cases, we will want to stop executing code altogether if a condition is met within a loop. For example, if we want to test a list to see if it contains any odd numbers we can do this:
test_list = [2, 4, 6, 8, 9, 10]
contains_odd = False
for item in test_list:
if not item % 2 == 0:
contains_odd = True
break
In this case, we are setting up a test list called test_list. This contains some arbitrary values. Then we are setting a flag contains_odd. We will start by assuming that the list contains no odd numbers, then iterate through the list and test for odds. If we find an odd number we set contains_odd to True and completely break out of the code. Due to the nature of our problem, if we find an odd value in the list there is no reason to keep testing.
Python, like many programming languages, implements containers, which are collections of other objects. As such, Python provides the for statement for iterating over each element in the collection.