In this post, I will write about While loops in Python.
If you have read earlier posts For and While Loops you will probably recognize a lot of this.
Count from 0 to 9
This small script will count from 0 to 9.
i = 0 while i < 10: print i i = i + 1
What does it do?
Between while and the colon, there is a value that first is True but will later be False.
As long as the statement is True , the rest of the code will run.
The code that will be run has to be in the indented block.
The i = i + 1 adds 1 to the i value for every time it runs.
Be careful to not make an eternal loop, which is when the loop continues until you press Ctrl+C.
while True: print "Hello World"
This loop means that the while loop will always be True and will forever print Hello World.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.