Saturday, January 23, 2016

Print triangle shape using python


k = 5
for j in range(5 ,0, -1):
 if(j < k):
  print ((" " * (k - j)) + ("*" + " ") * (j))
 else:
  print  ("*" + " ") * (j)
#print "Done with descending"
for x in range(1, 6):

 if(x < k):
  print ((" " * (k - x - 1)) + ("*" + " ") * (x + 1))

Program to print shape "X" in python

k = 5
for j in range(5 ,0, -1):
 if(j < k):
  #print ((" " * (k - j)) + ("*" + " ") * (j))
  if (j - 2 > 0):
    print ((" " * (k - j)) + "*" + (" " * ((j - 2) * 2 + 1)) + "*")
  else:
   print ((" " * (k - j)) + ("*" + " ") * (j))
 else:
  #print  ("*" + (" " * (j -2))) * 2
  print "*" + (" " * ((j - 2) * 2 + 1)) + "*"
#print "Done with descending"
for x in range(1, 6):

 if(x < k):

  if(x - 2 >= 0):
   print ((" " * (k - x - 1)) + "*" + (" " * ((x - 1) * 2 + 1)) + "*")
  else:
   print ((" " * (k - x - 1)) + ("*" + " ") * (x + 1))
 
   
Output:

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