Change language

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 ( "" )

    Shop

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    Common xlabel/ylabel for matplotlib subplots

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    12 answers

    NUMPYNUMPY

    Flake8: Ignore specific warning for entire file

    12 answers

    NUMPYNUMPY

    glob exclude pattern

    12 answers

    NUMPYNUMPY

    How to avoid HTTP error 429 (Too Many Requests) python

    12 answers

    NUMPYNUMPY

    Python CSV error: line contains NULL byte

    12 answers

    NUMPYNUMPY

    csv.Error: iterator should return strings, not bytes

    12 answers


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    sin

    How to specify multiple return types using type-hints

    exp

    Printing words vertically in Python

    exp

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    cos

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically