Latest web development tutorials

Python includes exercises 23

Python 100 Li Python 100 Li

Topic: Print out the following pattern (diamond):

   *
  ***
 *****
*******
 *****
  ***
   *

Program analysis: first to look at the graphic divided into two parts, the first four lines of a law, a law after three lines, the use of a double for the cycle, the first layer of the control line, the second layer of the control column.

Source Code:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

from sys import stdout
for i in range(4):
    for j in range(2 - i + 1):
        stdout.write(' ')
    for k in range(2 * i + 1):
        stdout.write('*')
    print

for i in range(3):
    for j in range(i + 1):
        stdout.write(' ')
    for k in range(4 - 2 * i + 1):
        stdout.write('*')
    print

The above example output is:

   *
  ***
 *****
*******
 *****
  ***
   *

Python 100 Li Python 100 Li