5

I've written a program in C++ that displays a pyramid of asterisk (see below) and now I'd like to see how it's done in Python but it's not as easy as I'd thought it would be.

Has anyone tried this and if so could you show me code that would help out?

Thanks in advance.

       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************
jww
  • 97,681
  • 90
  • 411
  • 885
jimmyc3po
  • 471
  • 3
  • 6
  • 16
  • 2
    What do you have so far, and how doesn't it work? – Ignacio Vazquez-Abrams Feb 06 '11 at 03:26
  • Check out this [christmas tree](http://stackoverflow.com/a/34241421/389289) – zvone Sep 04 '16 at 17:28
  • Stack overflow is a fantastic resource open to all, but as with many other internet fora, it does help to understand some of the tacit rules you might be brushing up against when first getting involved. Case in point, see this link: https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – Thomas Kimber May 16 '17 at 14:40

14 Answers14

22
def pyramid(rows=8):
    for i in range(rows):
        print ' '*(rows-i-1) + '*'*(2*i+1)

pyramid(8)
       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************

pyramid(12)
           *
          ***
         *****
        *******
       *********
      ***********
     *************
    ***************
   *****************
  *******************
 *********************
***********************
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • 3
    Wow, the line `print ' '*(rows-i-1) + '*'*(2*i+1)` sure does a lot! That's pretty darn cool. :) ...and what the variable i "does" in the program is something that of...hadn't thought of using it like that. Thanks for the reply, I do appreciate it! – jimmyc3po Feb 06 '11 at 05:07
5

Or you could try:

def pyramid(size=8):
    for i in range(size):
        row = '*'*(2*i+1)
        print row.center(2*size)
kleytont
  • 51
  • 1
3

You can use string multiplication like so:

>>> for i in range(size):
...     print '%s%s'%(' '*(size-(i-1)),'*'*((i*2)-1))
...
kelloti
  • 8,705
  • 5
  • 46
  • 82
2

If you like list comprehensions:

> n = 5
> print("\n".join((i*"*").center(n*2) for i in range(1, n*2, 2)))

    *
   ***
  *****
 *******
*********
hansaplast
  • 11,007
  • 2
  • 61
  • 75
2

This code isn't very pythonic, but it's readable. Look at Hugh Bothewell's answer for a compact pyramid drawing function:

def drawPyramid(rows):
  result = ''

  for i in xrange(rows):
    row = ''
    row += ' ' * (rows - i - 1)
    row += '*' * (2 * i + 1)

    result += row + '\n'

  return result

print drawPyramid(20)
Blender
  • 289,723
  • 53
  • 439
  • 496
  • Thanks! Even though this isn't really related to my question I noticed the word "pythonic" and I'm curious. Is there a good book (or guide) in how to write code that is more pythonic? Write more Python is probably the answer but thought I'd ask. :) Everything I seem to try and write in Python always comes out like I'm trying to code in a C-like language and tbh I think that's part of why I've has some trouble, just a guess. Thanks again! – jimmyc3po Feb 06 '11 at 05:37
2

I would suggest the following function:

def pyramid(rows=8):
    pyramid_width = rows * 2
    for asterisks in range(1, pyramid_width, 2):
        print("{0:^{1}}".format("*" * asterisks, pyramid_width))

Then try with:

pyramid()

or with:

pyramid(4)
Chaos Manor
  • 1,160
  • 1
  • 16
  • 17
1
Pyramid, Inverted Pyramid and Diamond Rhombus in Python:

Pyramid

i=1
j=5
while i<=5:
 print((j*' ')+i*'* ')
 j=j-1
 i=i+1




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


Inverted Pyramid

i=1
j=5
while i<=5:
 print((i*' ')+j*'* ')
 j=j-1
 i=i+1

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

Diamond Rhombus

i=1
j=5
while i<=5:
 print((j*' ')+i*'* ')
 while j<=5 & i==5:
  print(((j+1)*' ')+(5-j)*'* ')
  j=j+1
 j=j-1
 i=i+1



     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 
Ramesh
  • 19
  • 3
1
user_input = input("How many rows do you want to print? \n")
def stars(a):
    size = int(a)
    for i in range(1, size+1):
        print(" "*(size-i),"*"*(i*2-1))
    

stars(user_input)

How many rows do you want to print?
20
                    *
                   ***
                  *****
                 *******
                *********
               ***********
              *************
             ***************
            *****************
           *******************
          *********************
         ***********************
        *************************
       ***************************
      *****************************
     *******************************
    *********************************
   ***********************************
  *************************************
 ***************************************
greyk0
  • 35
  • 5
0
$ cat tree.py
def line(n, i):
    line = ''
    for j in range(n - i - 1):
        line += ' '
    for j in range(2 * i + 1):
        line += '*'
    for j in range(n - i - 1):
        line += ' '
    return line

def tree(n):
    for i in range(n):
        line_ = line(n, i)
        print(line_)

def run():
    tree(3)

if __name__ == '__main__':
    run()
$ python3 tree.py 
  *  
 *** 
*****
$ _
1737973
  • 159
  • 18
  • 42
0
n=4
m = (2*n)-2
for i in range(0, m):
    for j in range(0, m):
       print(end=" ")
    m = m - 1 # decrementing m after each loop
    for j in range(0, i + 1):
    # printing full Triangle pyramid using stars
       print("* ", end=' ')
print(" ")  


  *   
 *  *   
*  *  *   



Vinay
  • 29
  • 2
-1

You can also draw a DIAMOND

def pyramid(r):
    for i in range(r):
        print ("  "*(r-i-1) + "*"*(2*i+1))
    for i in range(r-1,-1,-1):
        print ('  '*(r-i-1) + "*"*(2*i+1))

n=int(input("Enter no of rows:"))
pyramid(n)

pyramid(10)

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *  
  * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
      * * * * * * * * * * * * *
        * * * * * * * * * * *
          * * * * * * * * *
            * * * * * * *
              * * * * *
                * * *
                  *
>>> 
jww
  • 97,681
  • 90
  • 411
  • 885
-1
def pyramid(row):
       for n in range(row):
              print(' '*(n+1)+' '*(2*(row-n))+'x'+'x'*(2*n+1))

pyramid(row=8)
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
John Don
  • 97
  • 1
  • 2
  • 12
-1

Though I am very much new to python, so here is how I solved it:

k=int(input("Enter the number of rows"))
for i in range(1,k):
    print(' '*(k-i),'* '*(i))

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


Rohit
  • 3,659
  • 3
  • 35
  • 57
-2
#!/usr/bin/python
for i in range(1,6):
 for j in range(1,i+1):
   print "*",
 print

O/P: 
===
*
* *
* * *
* * * *
* * * * *

2) 
#!/usr/bin/python
for i in range(1,6):
 for j in range(1,7-i):
   print "*",
 print

O/P:
* * * * *
* * * *
* * *
* *
*

3)
#!/usr/bin/python
for i in range(1,6):
 for j in range(1,6-i):
   print "",
 for k in range(1,i+1):
  print "*",
 print

O/P:

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

4)
#!/usr/bin/python
for i in range(1,6):
 for j in range(1,1+i):
   print "",
 for k in range(1,7-i):
  print "*",
 print

O/P:
 * * * * *
  * * * *
   * * *
    * *
     *
5) 
#!/usr/bin/python
for i in range(1,6):
 for j in range(1,6-i):
   print "",
 for k in range(1,i+1):
  print "*",
 print
for i in range(1,5):
 for j in range(1,1+i):
   print "",
 for k in range(1,6-i):
  print "*",
 print


O/P:
    *
   * *
  * * *
 * * * *
* * * * *
 * * * *
  * * *
   * *
    *
jww
  • 97,681
  • 90
  • 411
  • 885