Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The simplest form of a loop in Python is the while loop. So, let's make a working copy of while.py, while-working.py, and we'll go ahead and open our working copy there. The while loop is controlled with the while keyword, which we can see here, and then there is an expression. So, the expression gets evaluated, and if the expression is true, then the code inside the loop, the suite, is executed. After that code is executed, the expression is evaluated again, and if it's still true, the code gets executed again.
This continues until the expression is evaluated as false, and then the execution begins after the while loop. So, let's go ahead and run this, and there is our Fibonacci series. So, this expression is b<50, so as long as the result there is <50, the while loop will continue. So, if I were to increase this to say 150, save that, and run it, then we get a few more numbers out there in the Fibonacci series.
So, while is the simplest form of loop in Python, and you can see all it does is it evaluates this expression. As long as the expression is true, it executes the code in the while loop, and as soon as the expression is not true, the loop ends. So, that's the while loop in Python.
Get unlimited access to all courses for just $25/month.Become a member