Looping

Loops simplify the need of writing multiple lines with instructions (code) and it simplify the code by providing code re-usability.

For

The for loop is used to iterate a statement several numbers of times. We use a for loop to execute the statement until the given condition is satisfied and also the for loop is used if the number of iterations is known in advance.

In python, the syntax of for loop is the following:

for numbers in number_sequence:
    print(numbers)

 
Now, let’s take a look of python code example of a for loop:

for number in range(0,11):
    print(number)

 

Output:
0
1
2
3
4
5
6
7
8
9
10


In this example, we call a variable called “number” and we iterate from number 0 to 10, storing temporally a number in the variable and printing it for each loop.

While

In while loop a part of the code is executed as long as a given condition is true. We can take as an example, the buying house loop as mentioned above.

The syntax for python for while loop is the following:

while (true):

    Do something


Now, let’s take a look to a real python code example of while loop:

number = 1
while number < 11:
    print(number)
    number = number + 1

 

Output:
1
2
3
4
5
6
7
8
9
10


On the example above, we create a variable “number”. After that, we create a while loop condition, that prints the variable “number” if number is less than 11. For each loop in while, the number variable is incremented by one. When the number is 11, the while loop breaks and the code ends.
Important: You need to be careful while using while loops. If your while loop doesn’t meet a condition, it runs continuously for an infinite amount of time until you stop it with an escape character or manually close the running process.

Nested Loops

Nested loops in simple terms is a loop inside a loop. The inner loop is executed n number of times for every iteration of the outer loop. It can be applied in for loops and in while loops.

A nested for loop, in terms of syntax in Python is the following:

for numbers_1 in number_sequence:
    print(numbers_1)
    for numbers_2 in number_sequence:
        print(numbers_2)


Now, let’s take a look to a real python code example of a nested for loop:

for numbers_1 in range(0,2):
    print("1st loop number: " + str(numbers_1))
    for numbers_2 in range(0,6):
        print("2nd loop number: " + str(numbers_2))

 

Output:
1st loop number: 0
2nd loop number: 0
2nd loop number: 1
2nd loop number: 2
2nd loop number: 3
2nd loop number: 4
2nd loop number: 5
1st loop number: 1
2nd loop number: 0
2nd loop number: 1
2nd loop number: 2
2nd loop number: 3
2nd loop number: 4
2nd loop number: 5


Besides the nested for loop, you can also make a nested combination between while loop and a for loop. Take a look to the next example:

number = 1
while number < 3:
    print("While number: " + str(number))
    for another_number in range(0,5):
        print("Another number: " + str(another_number))
    number = number + 1

 

Output:
While number: 1
Another number: 0
Another number: 1
Another number: 2
Another number: 3
Another number: 4
While number: 2
Another number: 0
Another number: 1
Another number: 2
Another number: 3
Another number: 4


In this example above, we created a while loop with a for loop nested inside the while loop.
To finish, you can also do a nested while loop, in other words, you can do a while inside a while. Take a look to the next example:

number_1 = 1
number_2 = 1
while number_1 < 3:
    print("Number 1 value: " + str(number_1))
    while number_2 < 3:
        print("Number 2 value: " + str(number_2))
        number_2 = number_2 + 1
    number_1 = number_1 + 1

 

Output:
Number 1 value: 1
Number 2 value: 1
Number 2 value: 2
Number 1 value: 2


The nested while example shows that the inner while finish first and after that, the outer while progresses and finishes. 
 

Related Tutorials