Further information#

Why can I not only use a while loop#

The for loop allows us to iterate over any selection of objects. Some languages do not have a generic for loop like this. In some cases it is only possible to iterate over a set of integers (similar to the for i in range(n) pattern) or to only use a while loop.

Because of this, it is often the case that you will see code that uses while loops instead of for loops. For example:

seasons = ("Winter", "Spring", "Summer", "Autumn")

number_of_seasons = len(seasons)
i = 0
while i < number_of_seasons:
    season = seasons[i]
    print(season)
    i += 1
Winter
Spring
Summer
Autumn

The above code is equivalent to:

seasons = ("Winter", "Spring", "Summer", "Autumn")
for season in seasons:
    print(season)
Winter
Spring
Summer
Autumn

While it is possible to use a while loop instead of a for loop there are no advantages to doing that and in fact only disadvantages:

  • Using the while loop requires iterating over the iterable twice: the first time when counting the length of it using len and the second time during the while statement itself.

  • There is more potential for error in the code: it would not be unlikely to have an off by one error in the boolean condition.

  • It is less readable.

The following is a good guideline:

  • Use a for loop when you know what you are iterating over.

  • Use a while loop when only know a specific condition under which you should iterate.

Why should I not check if a boolean is equal to True or False#

It is possible to create a boolean by comparing another boolean to True or False for example:

boolean = False
boolean == True
False

Thus when using if or while statements you might sometimes see things like the following:

import random

random.seed(4)
selected_integer = random.randint(0, 10)
number_of_selections = 1
while (selected_integer % 2 == 1) == True:
    selected_integer = random.randint(0, 10)
    number_of_selections += 1
number_of_selections
2

or:

random.seed(4)
selected_integer = random.randint(0, 10)
number_of_selections = 1
while (selected_integer % 2 == 1):
    selected_integer = random.randint(0, 10)
    number_of_selections += 1
number_of_selections
2

However this is not best practice. A better approach is to use is instead of ==:

import random

random.seed(4)
selected_integer = random.randint(0, 10)
number_of_selections = 1
while (selected_integer % 2 == 1) is True:
    selected_integer = random.randint(0, 10)
    number_of_selections += 1
number_of_selections
2

This is due to the fact that when using == variables that are not booleans will be converted to booleans and this might not be the expected behaviour.

For example:

number = 0
number == False
True

however:

number is False
False