One, cycle mechanism and application scenario
1.while Cycle
Used to write generic iterative structures (non-traversal);
The top test executes the loop body as true, and repeats the test several times until the other statement after the loop is executed.
2.for Cycle
A general-purpose sequence iterator used to traverse the elements within any ordered sequence object;
Can be used for strings, tuples, lists, and other built-in iterative objects, as well as new objects created through the class;
3.python also provides some tools to perform covert iterations
In member relationship testing
List parsing
Map, reduce, and filter functions
Two, while loop
1. Syntax format
While Boolean_exception:while_suiteelse Else_suite
2. Grammatical features
1) The Else branch is an optional part;
2) As long as the boolean_exception result is true, the loop executes;
3) The boolean_exception result is false when the loop terminates, if there is an else branch, it will be executed once;
4) Break: Encounter break out of the inner layer of the loop;
5) Continue: Encounter continue jump to the beginning of the nearest layer loop;
6) Pass: Placeholder statement;
7) Else code block: The loop terminates normally and else does not execute if the loop termination is caused by a break.
While slicing in [29]: url = ' www.magedu.com ' in [30]: while url: ....: print url ....: url = url[1:] ....: www.magedu.comww.magedu.comw.magedu.com.magedu.commagedu.comagedu.comgedu.comedu.comdu.comu.com.comcomomm// While generating the digital in [33]: x = 0;y = 10in [35]: while x < y: ....: print x, ....: x += 1 ....: 0 1 2 3 4 5 6 7 8 9//while Branch Statement in [37]: url = ' www.magedu.com ' In [38]: while url: ....: print url : &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;URL&NBSP;=&NBSP;URL[:-1]&Nbsp; ....: else: ....: print "Game over! " ....: Www.magedu.comwww.magedu.cowww.magedu.cwww.magedu.www.mageduwww.magedwww.magewww.magwww.mawww.mwww.wwwwwwGame over!//while Branch statement encountered break, jumping out of the inner loop in [39]: url = ' www.magedu.com '; x = 0in [40]: while url: ....: print url ....: url = url[:-1] ....: x += 1 ....: if x > 7: ....: break ....: else: ....: print "Game over" ....: www.magedu.comwww.magedu.cowww.magedu.cwww.maGedu.www.mageduwww.magedwww.magewww.mag
This article is from "Jessen Liu's blog," Please make sure to keep this source http://zkhylt.blog.51cto.com/3638719/1707747
The while loop of the Python looping statement 22