loop
while Statement is very flexible , It can be used to repeatedly execute code blocks when the condition is true .
Is a sequence ( Or other iteratable objects ) Each element in the executes a code block .
Use keys Wait for the dictionary method to get all the keys . If you're only interested in values , You can use d.values .
Code example
-- Parallel iteration
Iterating over several sequences simultaneously
-- Get index on iteration
Get the index of the current object while iterating the object sequence
-- Reverse iteration and sorting before iteration
reversed and sorted . They are similar to list methods reverse and sort ( sorted The accepted parameters are also related to sort similar ), But it can be used for any sequence or iteratable object , And do not modify the object in place , Instead, it returns the inverted and sorted version .
--break
To end ( Jump out of ) loop , You can use break.
--continue
It ends the current iteration , And skip to the beginning of the next iteration .
-- while True/break precedent
while True Cause the cycle to never end , But you put the condition in one of the loops if In the sentence , And this one if Statement will call... When the condition is met break .
Add a line to the loop else Clause , It's only when... Is not called break Execution only .
Code example
Friends who want complete code , can toutiao Search for “ Programming workshop ” After attention s Believe in me , reply “Python Programming Notes 6“ obtain