Simple pattern printing programs in Python

Python pattern printing programs: Here, we will find some of the programs based on pattern printing in Python. By Anshuman Das Last updated : April 09, 2023

Pattern Programs in Python

Print the following patterns using Python program:

  1. Python Program for Half Pyramid of Stars (*)
  2. Python Program for Half Pyramid of Ones (1)
  3. Python Program for Half Pyramid of Numbers | Pattern 1
  4. Python Program for Half Pyramid of Numbers | Pattern 2
  5. Python Program for Half Pyramid of Alphabets

1. Python Program for Half Pyramid of Stars (*)

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

Python code

for row in range (0,5):
    for column in range (0, row+1):
        print ("*", end="")

    # ending row
    print('\r')

2. Python Program for Half Pyramid of Ones (1)

Now if we want to print numbers or alphabets in this pattern then we need to replace the * with the desired number you want to replace. Like if we want pattern like,

1
1  1
1  1  1
1  1  1  1
1  1  1  1  1

Python code

#row operation
for row in range(0,5):

# column operation

    for column in range(0,row+1):
        print("1 ",end="")

    # ending line
    print('\r')

3. Python Program for Half Pyramid of Numbers | Pattern 1

If want increasing numbers in this pattern like,

1
1  2  
1  2  3  
1  2  3  4
1  2  3  4  5

Here we need to declare a starting number from which the patter will start. In the above case the number is starting from 1. So, here we have to create a variable and assigns its value to 1 then we need to print only the value of variable.

As its value is increasing every row by 1, but starting value is always 1.

So, for that we have to declare the value of the starting number before column operation (second for loop) and need to increase it by 1 after the column operation section after the printing value.

Python code

#row operation
for row in range (0, 5):
    n = 1
    # column operation
    for column in range (0, row+1):
        print(n, end=" ")
        n = n+1
    # ending line
    print('\r')

4. Python Program for Half Pyramid of Numbers | Pattern 2

1
2 3
4 5 6
7 8 9 10
11 12 13 14

To get the above pattern only we have to declare the variable before the row operation. Follow the code below,

Python code

n = 1
#row operation
for row in range (0, 5):

    # column operation
    for column in range (0, row+1):
        print(n, end=" ")
        n = n+1
    # ending line
    print('\r')

5. Python Program for Half Pyramid of Alphabets

A
A  B
A   B  C
A  B  C  D
A  B  C  D  E

The above pattern can also be another type.

For that should have the knowledge of ASCII values of 'A'.

Its ASCII value is 65.

In column operation We have to convert the ASCII value to character using chr() function.

Python code

#row operation
for row in range (0, 5):
    n = 65
    # column operation
    for column in range (0, row+1):
        c = chr(n)
        print(c, end=" ")
        n = n+1
    # ending line
    print('\r')

Practice more python experiences here: python programs

Python Basic Programs »


Related Programs

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.