Loops
A loop in a computer program is an instruction that repeats until a specified condition is reached. In a loop structure, the loop asks a question. If the answer requires action, it is executed. The same question is asked again and again until no further action is required. Each time the question is asked is called an iteration.
for loop
A for loop is a loop that runs for a preset number of times. It is often used when you have a piece of code which you want to repeat "n" number of times.
for value in sequence:
statement(s)
Above we see the for loop syntax, Here, val is the variable that takes the value of the item inside the sequence on each iteration. Let’s now take a look at an example.
In [2]: months = ["Jan", "Feb", "Mar","April", "May","June"]
for month in months:
print(month)
Above, we have a list of the first 6 months of the year. We also have a for loop that reads, for every element that we assign the variable month, in the list months, print out the variable month.
Out[2]: Jan
Feb
Mar
April
May
June
And above we have the output of our loop.
while loop
The while loop is used to repeat a specific block of code an unknown number of times, until a condition is met. Let’s now take a look at an example.
In [2]: numbers = 0
while numbers <= 10:
print(numbers)
numbers += 1
Above we have a loop that starts from 0 and increases by 1 until it is equal to 10. This means that while numbers is less than or equal to 10, print out numbers. Let’s take a look at the output
Out[2]: 0
1
2
3
4
5
6
7
8
9
10
Above we can see that the outputs we got are the numbers from 0 to 10.
The same results we got from our while loop can be gotten in Python using the range()
function. The range()
function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Let's take a look at an example of that.
In [2]: for numbers in range(0,10):
print(numbers)
Out[2]: 0
1
2
3
4
5
6
7
8
9
So above we can see that the outputs we got are the numbers from 0 to 9. Some would be wondering why in the while loop we got 0 - 10 and here 0 - 9, this is be in our while loop we used the condition less than or equal to 10, this makes 10 included in the output displayed.
Break and Continue statements
With the break statement we can stop the loop before it has looped through all the items,
In [2]: numbers = 0
while numbers <= 10:
if numbers = 5:
break
print (numbers)
numbers += 1
Out[2]: 0
1
2
3
4
Above we included a break statement that if our loop get to 5 it should exit, and that can be seen in the output.
With continue statement we can stop the current iteration of the loop, and continue with the next
In [2]: months = ["Jan", "Feb", "Mar", "April", "May", "June"]
for month in months:
if month == "April":
continue
print(month)
print("\nThe end")
Out[2]: Jan
Feb
Mar
May
June
The end
Above we have used the continue statement to skip “April”, and return to the for loop statement. In the output we can see that April isn’t part of the results.