Today we’re going to learn how to execute the same block of code multiple times using loops, and how to take actions on each element in a list.
Goal for this Tutorial:
- Create our first loop to execute code multiple times.
- Learn how to iterate through a list.
Last time we worked with Python, we learned about lists and tuples, but we didn’t really do anything with them. For lists to really be useful we will need to be able to do things with the data. That means we either need to be able to find and work on a specific piece of data (which we know how to do already), or we need to take each element of the list or tuple and do something with it. We probably won’t do this with tuples, so from this point on I’ll just be talking about lists.
To work on each element we another language feature: loops.
The Original Loop: The While Loop
Like a lot of things in Python, loops are incredible readable, and because of that make a lot of sense when you see them. Let’s start by taking a look at the while
loop:
>>> a = 0
>>> while a < 5:
... print("This is a: %s" % a)
... a += 1
This is a: 0
This is a: 1
This is a: 2
This is a: 3
This is a: 4
The while loop takes an expression and executes the block underneath if that expression is true, much the same way that an if
statement would. The difference here is that it will look at that expression over and over again until it is false.
This type of loop allows us to do something over and over again, but if you forget to modify a variable used in the expression it is possible to run into an infinite loop, so look out for that.
There are a few other keywords associated with loops: break
, continue
, and else
.
Looking at else
first, it’s used to run something when the looping finishes. Building on our first loop this is how we could utilize else
:
>>> a = 0
>>> while a < 5:
... print("This is a: %s" % a)
... a += 1
... else:
... print("We've reached 5")
...
This is a: 0
This is a: 1
This is a: 2
This is a: 3
This is a: 4
We've reached 5
To learn about break
and continue
we’ll take a look at the
other type of list that you’ll use in Python: the for
loop.
Looping Over Lists: The For Loop
The while
loop is powerful and can be used to accomplish any type of looping that we need, but most of the time what we want to do is iterate over a list, and for that we have the for
loop.
This next example will show both a for
loop and the continue
keyword in action:
>>> my_list = [1,2,3,4,5]
>>> for num in my_list:
... if num % 2 == 0:
... print(num, 'is even')
... continue
... print(num, 'is odd')
...
1 is odd
2 is even
3 is odd
4 is even
5 is odd
Looking at the creation of the for
loop you can see the readability that I mentioned earlier. We create a temporary variable for each element in our list called num
and then we can use that in our loop block. Once we’ve gone through every item of the list then our loop is finished.
We wrote this loop specifically to showcase the continue
keyword and we would normally write this differently, but it gets the point across. continue
allows us to stop executing our current loop iteration and move onto the next one. If we hadn’t used continue
here we would have printed that some numbers were both even and odd.
The last keyword that loops have is break
, so let’s see how that works.
>>> my_list = [1,2,3,6,4,5]
>>> for num in my_list:
... if num <= 5:
... print(num, 'is less than 6')
... else:
... break
...
1 is less than 6
2 is less than 6
3 is less than 6
Looking at the list we should have been able to see all but one of the items print out, except we hit the break
statement. Where continue
halts a single iteration of a loop, break
halts the entire loop. The 4
and 5
that were in the loop after the 6
were never iterated over because the break
was triggered when we iterated over the 6
.
Recap
Now you know how to create loops in Python. Loops are one of the most common tools that we will use in programming (for
more than while
), so commit these to memory sooner rather than later.