… but there need to be one if you are operating with loops in Python.
In genereal you have two possibilites:
- Using a while-loop
- Using a for-loop
A while loop always starts with the word while. Then follows a condition and a colon.
Normally, a loop is only terminated if the condition in the loop header is no longer fulfilled. With break you can leave a loop prematurely. This is important if you use while: true, for example.
The for statement in Python is different from the for loop in other computer languages.
In Python, the for loop is for iteration over a sequence of objects.
The range() function is particularly useful in conjunction with the for loop.
In this example it is important to note the following:
- the first digit indicates the lower limit
- the second digit indicates the upper limit, but you have to specify your desired value n+1
- the third digit indicates the step size