Python pyramid printing software

Patterns can be printed in Python using simple for loops. First outer loop is used to process row count and inner nested loop — to handle column count . By manipulating the print statements, various numeric, alphabetical or star designs can be printed.
Some of the patterns are shown in this article.

  • Simple pyramid drawing

    # Python 3.x code for star structure demonstration

     
    # Print template demonstration function

    def pypart (n):

      

    # outer loop to handle line count

    # n in this case

      for i in range ( 0 , n):

     

    # inner loop to handle the number of columns

    # the values ​​change accordingly. to outer loop

    for j in range ( 0 , i + 1 ):

      

      # print stars

    print ( "*" , end = "")

     

    # trailing line after each line

      print ( " " )

      
    Driver code

    n = 5

    pypart (n)

    Output:

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

    Another approach:
    Using List in Python 3, this can be done in an easier way

    # Python 3.x code to demonstrate star structure

     
    # Demonstration function print template

    def pypart (n):

    myList = []

      for i in range ( 1 , n + 1 ):

    myList.append ( " * " * i)

    print ( "" . join (myList))

     
    Driver code

    n = 5

    pypart (n)

  • After 180 degree rotation

    # Python 3.x code to demonstrate star structure

     
    # Print template demo function

    def pypart2 (n):

     

    # number of spaces

    k = 2 * n - 2

     

    # outer loop to handle the number of lines

    for i in range ( 0 , n):

     

    # inner loop for processing numeric spaces

      # values ​​change acc. to demand

    for j in range ( 0 , k):

    print (end = "" )

      

      # decreasing k after each loop

    k = k - 2

     

    # internal cycle for processing a quantity of one hundred lbtsov

    # values ​​change acc. to outer loop

    for j in range ( 0 , i + 1 ):

      

      # print stars

    print ( "*" , end = "")

     

    # trailing line after each line

      print ( " " )

      
    Driver code

    n = 5

    pypart2 (n)

    Output:

     * * * * * * * * * * * * * * * 
  • Print triangle

    # Python 3.x code to demonstrate star structure

     
    # Printed triangle function

    def triangle (n):

     

    # number of spaces

    k = 2 * n - 2

     

    # outer loop to process the number of lines

    for i in range ( 0 , n):

     

    # inner loop to handle number spaces

      # values ​​change acc. to demand

    for j in range ( 0 , k):

    print (end = "" )

      

      # decreasing k after each loop

    k = k - 1

     

    # internal cycle for processing a quantity of one hundred lbtsov

    # values ​​change acc. to outer loop

    for j in range ( 0 , i + 1 ):

      

      # print stars

    print ( "*" , end = "")

     

    # trailing line after each line

      print ( " " )

      
    Driver code

    n = 5

    triangle (n)

    Output:

     * * * * * * * * * * * * * * * 
  • Number Template

    # Python 3.x code to demonstrate star structure

     
    # Number printing template demo function

    def numpat (n):

     

    # initialize start number

    num = 1

     

    # outer loop to process the number of lines

      for i in range ( 0 , n):

     

    # reassign num

    num = 1

     

    # inner loop for processing count column numbers

    # the values ​​change acc. to outer loop

    for j in range ( 0 , i + 1 ):

      

      # print number

    print (num, end = "" )

     

    # increment the number in each column

    num = num + 1

      

    # trailing line after each line

    print ( "" )

     
    # Driver code

    n = 5

    numpat (n)

    Exit:

     1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 
  • Numbers without reassignment

    # Python 3.x code to demonstrate star structure

      
    # Function for demonstrating the pattern of printing numbers

    def contnum (n):

     

    # initialization of the starting number

      num = 1

     

    # outer loop to process the number of lines

    for i in range ( 0 , n) :

     

      # don`t assign number

    # number = 1

     

    # inner loop to handle the number of columns

    # values ​​change acc. to outer loop

    for j in range ( 0 , i + 1 ):

      

      # print number

    print (num, end = "" )

     

    # increment the number in each column

    num = num + 1

     

    # trailing line after each line

    print ( "" )

     

    n = 5

     
    # send 5 as an argument
    # calling function
    contnum (n)

    Exit:

     1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
  • Sample character

    # Python 3.x code to demonstrate star structure

      
    # Function for demonstrating the alphabet printing template

    def alphapat (n):

     

    # initializing value corresponding to & # 39; A & # 39;

    # ASCII value

      num = 65

     

    # outer loop to process the number of lines

    # 5 in this case

      for i in range ( 0 , n):

     

    # inner loop to handle the number of columns

    # the values ​​change accordingly. to outer loop

    for j in range ( 0 , i + 1 ):

      

      # explicit conversion to char

    ch = chr (num)

     

    # print character value

    print (ch, end = "" )

     

    # increasing number

    num = num + 1

     

      # trailing line after each line

    print ( "" )

     
    Driver code

    n = 5

    alphapat (n)

    Output:

     ABBCCCDDDDEEEEE 
  • Continuous

    # Python 3.x code for star drawing

      
    # Alphabet printing template demo function

    def   contalpha (n):

     

    # initializing value corresponding to & # 39; A & # 39;

    # ASCII value

    num = 65

     

    # outer loop to process the number of lines

    -   for i in range ( 0 , n):

      

    # inner loop to handle the number of columns

    # values ​​change acc. to outer loop

    for j in range ( 0 , i + 1 ):

      

      # explicit conversion to char

    ch = chr (num)

     

    # print character value

    print (ch, end = "" )

     

    # increment in each column

    num = num + 1

     

     

      # trailing line after each line

    print ( " " )

     plain "> (num)

      

      # print character value

    print (ch, end = "" )

     

    # increment in each column

      num = num + 1

     

     

    # trailing line after each line

      p rint ( "" )

    plain "> (num)

      

      # print character value

    print (ch, end = "" )

     

    # increment in each column

      num = num + 1

     

     

    # trailing line after each line

      p rint ( "" )