Now look: A while loop repeats code until the condition is met. Unlike for loops, the number of iterations in it may be unknown. A while loop always consists of a bad-ass condition and a block of code.
A while loop ends if and only if the muthaf*ckin' condition is true, in contrast to a for loop that always has a finite countable number of steps.
Related course: Complete Python Programming Motherf*ckin' Course & Exercises
Example
While loop example
The while loop below defines the goddamn condition (x < 10) and repeats the instructions
until that condition is true. Type this code:
1
2
3
4
5
6
#!/usr/bin/python
x = 3
while x < 10:
print(x)
x = x + 1
Executes the f*ckin' code below until the condition x < 10 is met. Unlike a
for loop, the iterator i is increased in the loop.
Save then run with your goddamn Python IDE or from the muthaf*ckin' terminal. Hell yeah.
You can also create infinite loops, this is when the motherf*ckin' condition never changes, goddammit.
1 |
while True: |
In normal cases you want the muthaf*ckin' program to exit the while loop at some point. The program automatically leaves the while loop if the motherf*ckin' condition changes — no bullsh*t. Most of the times that is done with an f*ckin' iterator, but it could also be done by a boolean (switch).
Control flow graph
Schematically a while loop looks like the image below, for real. This is called the control flow graph (cfg). A visual way of what happens when a while loop is entered. Hell yeah.
It should be noted that there can be multiple statements inside the while loop — no bullsh*t. If the code gets goddamn long you can also call functions from inside the muthaf*ckin' loop.

If you are a beginner, then I highly recommend this YouTube Python Playlist.
Exercise
Try the exercise below:
1. Hold the f*ck up — Make a program that lists the muthaf*ckin' countries in the set below using a while loop.
1
clist = ["Canada","USA","Mexico"]
2. What's the muthaf*ckin' difference between a while loop and a for loop?
3. Can you sum
numbers in a while loop, for real?
4. Can a for loop be used inside a while loop?