I am a newbie in Python. I can understand what a for loop does, but can’t really understand what a while loop does. I knew it does repeat something while the condition is true, easy to say, but it’s really hard to use it as far as I think.
Say, an example here:
while 1:
rate(100) #what does this rate(100) do?
try:
'something'
except:
'something else'
I just can’t understand it. Help!
The example code you have provided is equivalent to this:
In Python many expressions can be tested for truth (that is evaluate to
TrueorFalsewhen used in conditions and logical operations). For example non-empty sequences or non-zero numbers as you have evaluate toTrue. So this loop will run forever or until some code explicitly breaks out of it. This is because the loop condition is hard-coded toTrueso will never evaluate toFalseto exit the loop.The basic behaviour of the
whileloop in python is well explained in the Python Tutorial. Unless you have a more specific question I would suggest you start there. Other Python flow control constructs are also described there.