Python loop statement while loop 22
I. Recycling Mechanism and Application Scenario 1. the while loop is used to compile a general iteration structure (non-traversal). When the top test is true, the loop body is executed, and other statements after the loop are executed after multiple tests are repeated until the test is false. 2. a for Loop is a general sequence iterator used to traverse elements in any ordered sequence object. It can be used for strings, tuples, lists, and other built-in iteratable objects, and the new object created through the class; 3. python also provides some tools that can perform implicit iteration in the member relationship test list parsing map, reduce and filter function 2, while loop 1. syntax format
while boolean_exception: while_suiteelse else_suite
2. syntax feature 1) The else branch is an optional part; 2) the loop is executed as long as the boolean_exception result is True; 3) when the boolean_exception result is False, the loop is terminated, at this time, if there is an else branch, it will be executed once; 4) break: When a break jumps out of the innermost loop; 5) continue: When a continue jumps to the beginning of the cycle of the closest layer; 6) pass: placeholder statement; 7) else code block: it is executed only when the loop ends normally. else Will not execute the loop if the loop ends because the break jumps out. 3. while syntax example
// While slice In [29]: url = 'www .magedu.com 'In [30]: while url :....: print url ....: url = url [1:]...: Large. comcomomm // while generates a number 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 ....: url = Url [:-1]...: else:...: print "Game over! "...: Www. magedu. comwww. magedu. cowww. magedu. cwww. magedu. www. mageduwww. magedwww. magewww. magwww. mawww. mwww. wwwwwwGame over! // When the while branch statement encounters a break, it jumps out of the innermost 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