#用python打印出直角三角形:

1 #!/usr/bin/env python
  2 # coding=utf-8
  3 i = 0
  4 while i < 5:
  5     j = 0
  6     while j <= i:
  7         print "*",
  8         j+=1
  9     print ""
 10     i+=1             
 
 输出效果如下:
* 
* * 
* * * 
* * * * 
* * * * *

#对上面的程进行深化,可以得到两个相对的三角形:

#!/usr/bin/env python
  2 # coding=utf-8
  3 i = 0
  4 while i < 5:
  5     j = 0
  6     while j <= i:
  7         print "*",
  8         j+=1
  9     print ""
 10     i+=1
 11 i = 5·
 12 while i > 0:
 13     j = 0
 14     while j <= i:
 15         print "*",
 16         j+=1                                                                                                                                  
 17     print ""
 18     i-=1

输出结果如下:

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

#最后上个心形:

print'\n'.join([''.join([('PYTHON!'[(x-y)%7]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else' ')for x in range(-30,30)])for y in range(15,-15,-1)])

输出结果如下:

THON!PYTH           YTHON!PYT               
            !PYTHON!PYTHON!PY   N!PYTHON!PYTHON!P           
          N!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON!PYTH         
         N!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON        
        N!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON!P       
        !PYTHON!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON!PY       
        PYTHON!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON!PYT       
        YTHON!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON!PYTH       
        THON!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON!PYTHO       
        HON!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON       
         N!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON        
          PYTHON!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON         
          YTHON!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON!         
            ON!PYTHON!PYTHON!PYTHON!PYTHON!PYTHON           
             !PYTHON!PYTHON!PYTHON!PYTHON!PYTHON            
              YTHON!PYTHON!PYTHON!PYTHON!PYTHON             
                ON!PYTHON!PYTHON!PYTHON!PYTHO               
                  PYTHON!PYTHON!PYTHON!PYTH                 
                    HON!PYTHON!PYTHON!PYT                   
                       PYTHON!PYTHON!P                      
                          ON!PYTHON                         
                             YTH                            
                              H



转载于:https://blog.51cto.com/bryanguo/1900737