The main problem with a Python loop is that it can easily become unruly. If the code inside the loop is complicated, it can be difficult to understand what’s happening.
def loop(n): while n > 0: print(n) n = n - 1 print('Blast off!')
This is a function definition for a function called ‘loop’. The function takes an input of ‘n’.
The function contains a while loop which will run as long as ‘n’ is greater than 0.
Each time the while loop runs, it will print the value of ‘n’, and then reduce the value of ‘n’ by 1.
Once ‘n’ reaches 0, the while loop will stop running and the string ‘Blast off!’ will be printed to the console.
Types of loops
There are three types of loops in Python: while, for, and range.
Most used loop
The most common loop in Python is the for loop.