1.2.3.3 while/break/continue

典型的C式While循环(Mandelbrot问题):

In [13]:

  1. z = 1 + 1j
  2. while abs(z) < 100:
  3. z = z**2 + 1
  4. z

Out[13]:

  1. (-134+352j)

更高级的功能

bread 跳出for/while循环:

In [103]:

  1. z = 1 + 1j
  2. while abs(z) < 100:
  3. if z.imag == 0:
  4. break
  5. z = z**2 + 1
  6. print z
  1. (1+2j)
  2. (-2+4j)
  3. (-11-16j)
  4. (-134+352j)

continue 继续下一个循环迭代:

In [101]:

  1. a = [1, 0, 2, 4]
  2. for element in a:
  3. if element == 0:
  4. continue
  5. print 1. / element
  1. 1.0
  2. 0.5
  3. 0.25