Python Programming pt. 3: An Introduction to While Loops
While loops are one method of iteration (repeating instructions). As the name suggests, while loops will continue to loop while a condition is met. Let’s start by simply counting to ten.
First we need to define a variable to store the current number and give it a value of 1. We will then loop while our count is less than 10, however we need to be careful here. If we want our count to include the number 10 we need to use ‘<=‘ (less than or equal to) instead of ‘==‘. Using ‘==‘ will stop our count at 9. We also need to remember to increment (increase) count, or else the loop will never end. We could use ‘count = count + 1′ to assign count the value of 1 added to itself, but a more ‘Pythonic’ way of doing that is to use ‘count +=1′. They both do the same thing, but the later is generally considered the more readable way to increment variables.

You can see the colon and indentation I discussed in my previous tutorial. That tells the python interpreter that ‘print(count)’ and ‘count +=1′ are to be repeated every time the code iterates through the while loop.
I think a good way to learn is to play around with the code and see how it changes, so here are some suggested changes you can make:
- Change what value is initially given to count
- Change the value of count in the ‘while’ statement
- Replace <= with ==
- Switch the print statement and the incrementing of count
Try and figure out why each of those has the effect it does. They may not seem like important changes, but they can make a big difference and later on they could be the reason for the bug you’ve spent hours trying to fix.