1. 程式人生 > >python列印各種三角形

python列印各種三角形

列印等腰直角三角形

for i in range(1, 5):
     for j in range(1, 5):
         if j>=i:
             print " * ",

     print "\n"


 *   *   *   *  

 *   *   *  

 *   *  

 *  

列印實心三角形

for i in range(1, 6):
     for j in range(0, 6 - i):
         print " ",
     for k in range(1, i + 1):
             print
" * ", print "\n" * * * * * * * * * * * * * * *

列印空心三角形

rows = 5
for i in range(0, rows + 1):
 for j in range(0, rows - i):
  print " ",
 for k in range(0, 2 * i - 1):
  if k == 0 or k == 2 * i - 2 or i == rows:
    print "*"
, else: print " ", print "\n" * * * * * * * * * * * * * * * *