An Introduction to Python Programming #3

Welcome to my third Python programming introduction post. In post #2 we looked at the basics of flow control using conditional statements. In this post we will be taking a look at another fundamental aspect of flow control; loops. A loop is a way of running a particular block of code more than once based upon a conditional. For example, we could have a loop ask a user for input continuously until they input “Exit”. That may not seem very useful but its just an example, loops are an extremely useful tool and definitely should not be overlooked. The two types of loops that we will be looking at are the for loop and the while loop.

First off, the for loop. The for loop is usually used for iterating a particular number of times. The usual first demonstration of a python for loop uses the range(number) function. The range function when run outside of a loop returns a list object containing (depending on the arguments given) numbers from (unless stated otherwise) 0 through to number. So, lets take a look at a for loop in action. In the following code we will be printing out the numbers 1 through to 100.

j = 1
for i in range(100):
    print j
    j += 1

This will print out, on separate lines, the numbers 1 through to 100. Lets pick this code apart. Firstly we have the variable declaration, pretty simple. The next line is the loop, and this is the slightly confusing part.

for i in range(100)

What this means is, for every object in the list, assign that object to the variable i, then iterate over the following code block. After the code block is complete, the next object in the list is assigned to the variable i and the code block is run again. This process is repeated untill there are no more objects in the list, thus it iterates 100 times. The next 2 lines are the code block.

print j
j += 1

This should make sense, first print the variable j then increase j by 1. Hopefully this makes a bit of sense, if it doesn’t, don’t fret, play around a bit, try different numbers and variables.

Next we are going to be taking a look at the while loop. The while loop, unlike the for loop, will iterate continuously until its condition is met. This type of loop often causes problems for new programmers because it can easily be used to create infinite loops, loops where its conditional will never be met, we will look at an infinite loop after we have looked at a basic implementation of a while loop.

So, the while loop, below is an example of a while loop performing the same task as our previous for loop

i = 1
while i != 101:
    print i
    i += 1

As you can see, there is not much difference. The main difference is in the line while i != 101 what this means is that while the variable i does not equal 101, print the variable then increase it by 1. Now, as promised, lets take a quick look at a slight variation of this loop, an infinite loop.

i = 1
while i != 101:
     print i

That will continuously print 1 until stopped by the user. Another, more interesting infinite loop would be this

i = 1
while True:
    print i
    i += 1

This loop will continuously print the variable i incrementing it by 1 each time. The reason this loop is infinite is because the loops conditional is True which is what a positive conditional returns, thus, an infinite loop.

Infinite loops are not all bad, there are many very valid reasons for using them, but be careful. In some compiled languages your program will segfault when you write an infinite loop.

I hope this post has helped you in your journey to becoming a python programmer. I will continue to write more introductory posts. Also, look out for my upcoming ‘Practical Python Programming Post’ which will be taking a more advanced approach (Suitable for beginner > intermediate programmers) looking at how python can be useful in everyday Ubuntu life.

Thanks for reading,