06- control statement: & conditional loop

  Overview: Main analysis conditions used herein and cycle determination method


A condition judging

  Code templates:

1  IF <condition judgment 1> :
 2      <1 performs>
 3  elif <conditional 2> :
 4      <performing 2>
 . 5  elif <conditional 3> :
 6      <performed 3>
 . 7  the else :
 . 8      <4 performs>

  note:

  1. Python no braces, by the indentation to distinguish between code block.

  2. Conditions for Analyzing end colon.

  3. if the condition can be abbreviated as:  IF X: . This and similar C.

  4. if the statement is determined from the top down, once a condition is detected, the corresponding code block, directly out the end of the remaining branches not determined. So if listed branch, pay attention to after the first little big.

 

Second, circulation

1. for ... in loop

  for ... in loop is similar to Java in the foreach, followed by iterative list and tuple each element.

  Format:

1 names = ['Albert', 'Chin', 'Chris']
2 for x in names:
3     print(name)

  We can see, we turn into the names of elements x, executing code in the loop body.

  If we want to control the loop portion of the code specified number of times (for example, 1000), I can not really go write such a long list. At this time, necessary to use  range (num)  function to generate a sequence of integers, and then through the list function, can be converted to list (). Note: The generated sequence range is [0, num).

2. while loop

  while circulation is not specified range, as long as the loop condition is satisfied, it has been circulating, until the loop condition is not met until the end. Note: In the while loop, the mechanism must be able to rewrite cycling conditions, otherwise it will turn into an endless loop.

  Format:

. 1  the while loop condition:
 2      code Block

3. break 和 continue

  break can exit the loop early (completely out of the loop).

  continue this cycle may be skipped, directly into the next cycle.

Guess you like

Origin www.cnblogs.com/murongmochen/p/11694467.html