Skip to main content


Python - Fun with Python Pyramid △


Hello Everyone,
I thought today I will give out some tuts regarding Python asterisk Pyramid △. If you don't know what Python asterisks Pyramid is then this is your chance to learn a few tips.
Its not much of a programming core stuff. I mean you can just code in such a way that you can create an image or a pattern using '*' (asterisk) or some other symbol you like.

So lets get on with the code:-
1) Creating simple asterisk '*' Pyramid △.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
>>> #_1)creating simple asterisk '*' pyramid
>>> x = 9
>>> for i in range(1 , 10, 2):
 print(x * ' ' + i * '*')
 x = x - 1

 
         *
        ***
       *****
      *******
     *********
>>> 

2) Creating simple asterisk '*' Pyramid ▽ (Upside-Down).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
>>> #_2)creating simple asterisk '*' pyramid (Upside-Down)
>>> x = 1
>>> for i in range(11, 0, -2):
 print(x * ' ' + i * '*')
 x = x + 1

 
 ***********
  *********
   *******
    *****
     ***
      *
>>> 

3) Creating asterisk '*' Diamond ♢ using input function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
>>> #_3)creating star asterisk '*' diamond using input function
>>> x = int(input("Input the Size of Diamond:- "))
Input the Size of Diamond:- 10
>>> for i in list (range(x)) + list(reversed(range(x-1))):
 print('{:<{w1}}{:*<{w2}}'.format('','',w1=x-i-1, w2=i*2+1))

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

3.1) If we only add print( ) in the above function there will be a slight change in shape. Example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
>>> #_3.1)creating star asterisk '*' diamond using input function 
>>> #but only adding print( ) changing its shape
>>> x = int(input("Input the Size of Diamond:- "))
Input the Size of Diamond:- 10
>>> for i in list (range(x)) + list(reversed(range(x-1))):
 print('{:<{w1}}{:*<{w2}}'.format('','',w1=x-i-1, w2=i*2+1))
 print()

 
         *

        ***

       *****

      *******

     *********

    ***********

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

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

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

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

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

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

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

    ***********

     *********

      *******

       *****

        ***

         *

4) Creating asterisk '*' Diamond ♢ by creating a function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
>>> #_4)creating star asterisk '*' pyramid
>>> # The trick here is to give a space in "*" as you notice the difference in the code.
>>> def star():
 j = 10; k = 10; p = 1
 for i in range(10):
  print(" " * k, " *" * i)
  k -= 1
 while j > 1:
  j -= 1
  print(" " * p, "*#" * j)
  p += 1

  
>>> star()
           
           *
          * *
         * * *
        * * * *
       * * * * *
      * * * * * *
     * * * * * * *
    * * * * * * * *
   * * * * * * * * *
  *#*#*#*#*#*#*#*#*#
   *#*#*#*#*#*#*#*#
    *#*#*#*#*#*#*#
     *#*#*#*#*#*#
      *#*#*#*#*#
       *#*#*#*#
        *#*#*#
         *#*#
          *#

5) Creating a rectangular block with asterisk '*' ⏹.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
>>> #_5)creating a rectangular block with asterisk '*'.
>>> row = 20
>>> column = 30
>>> for i in range(row):
 for j in range(column):
  print('*', end='')
 print()

 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
****************************** 
>>> 


6) Creating a rectangular hollow block with asterisk '*' ⬚.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
>>> #_5)creating a rectangular hollow block with asterisk '*'.
>>> row = 20
>>> column = 30
>>> for i in range(row):
 for j in range(column):
  if(i == 0 or i == row -1 or j == 0 or j == column -1):
   print('*', end = '')
  else:
   print('', end = ' ')
 print()

 
******************************
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
*                            *
******************************
>>> 


Hope you like the tutorial.

Python is simple and fun to play with and yet very powerful. 🔥


🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍

Popular Posts

Python underline string, Python underline text, Underline python print

Python pip - Installing modules from IDLE (Pyton GUI) for python 3.7

Top 40 Python - String Processing in Python | Working with String in Python

Python Program - When was I born? / Date of Birth / MY BIRTHDAY (using Python3+)

Top 11 Essential Python Tips and Tricks





Subscribe to our Channel


Follow us on Facebook Page

Join our python facebook groups



Join us on Telegram