Home » Python » Python programs

Simple pattern printing programs in Python

Python pattern printing programs: Here, we will find some of the programs based on pattern printing in Python.
Submitted by Anshuman Das, on September 24, 2019

Pattern 1:

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

Code:

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

    # ending row
    print('\r')

Pattern 2:

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

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')

Pattern 3:

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.

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')

Pattern 4:

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,

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')

Pattern 5:

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.

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')





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.