As I’ve posted before conditionals are very useful in python, it’s kind of the main idea of a computer to choose between do something or to don´t. So far we´ve seen If, Elif and else.

But what happened when we want to evaluate the same statement more than one time. The Hackeable way is to write tons of times the loop if, along with elif. But python gives us an alternative, and not just python but any other language well conformed. There exists loops, which are flow control statements.

This means that, as in If, we first have to evaluate an expression, if the expression turn out TRUE the loops start to work and run the block of code that belongs to them.

There are two kinds of loops; each of them can be rewritten as the other one, but there are some advantages in certain situations of using one over the other.

The word reserved for this loops are: WHILE and FOR

While loop

First i´m going to explain you the ‘easiest’ to understand, the WHILE loop. When I say this is easiest I don’t mean is weaker, or less efficient. It´s easiest in the meaning that it can be understand because it has a related meaning in English. So let’s see an example.

x = 0

while (x != 5):

            x = x+1

            print(x)

This will print this:

1

2

3

4

5

This is easy to understand. It set zero as the value of ‘x’. Then it evaluates that WHILE x is not equal to 5, the program will perform the block of code its written below, in this case each time the program runs through that line, it will add 1 to x, which at the beginning was 0, and the program will stop running once the value of x is equal to 5.

So, x will start at 0, then the program will evaluate if x is equal to 5, while x isn´t equal to five, the program will take the value of x, in this case 0 and add 1 to it, then It’ll print it.

After that the program will return at the beginning, will evaluate x, which now is equal to 1, but because it still isn´t 5, It will do that all over again until x is equal to 5, then it will stop.

For loop

A for loop consts of various parts, i understand it like this:

for (variable we´ll use for repeating the process) in (under what conditions it will repeat):

what to do once you´re inside the loop

So first of all we must type the word ‘for’ which indicates that we are going to write a For loop.

Then we must set the variable we´re going to use to repeat the process. Lets pick ‘i’.

After that we type ‘in’ followed by a function that indicated us under what conditions we must repeat the loop, we can use ‘range’ or ‘lenght’ for example. Lets pick ‘range’ wich is a function where you set a range of numbers between which you want to repeat the loop. And what our programm will do is to simple print ‘i’ each time.

So our final code will be something like

for i in range (6):

     print(i)

This will display: (NOTE: Range uses the number you assing MINUS 1, thats why the programm prints only until 5)

1

2

3

4

5

This is a simple piece of ‘for loop’. It’s said that this is a very powerful part of languages, so it’ll be nice to learn to use all of its potential.