Python Programming for Pattern Printing

Sandeep Jain
Forsk Coding School
3 min readJan 13, 2020

Using Python programming for various Pattern Printing is not a difficult task. Programs for these printings are simple and interesting. All of these programs are based on looping concepts.

And if we are looking in “Python” then it will be easier. Because we also know that Python is easy to use with an easy syntax.

Some Examples of Pattern Programs in this image.

Note: Patterns can be printed in Python using simple “for loop”.

Syntax (Python) :

for <variable> in <sequence>:  

Types of Most used Patterns :

  • Pyramid
  • Triangle star pattern(Right/left)
  • Pascal’s Triangle
  • Sand-glass Star
  • Alphabet
  • Number
  • Hollow (All included)

For all Pattern link :

https://github.com/hellosandeep1999/python/blob/master/ALL_Pattern.py

Pattern programs in Python only based on “For loop” , and we have to use two or more than two for loops. so need to know about the “For loop” syntax and how to work it.

In the program that prints pattern contains two for loops, “the first loop is responsible for rows and the second for loop is responsible for columns.” Here you will find the code and learn how to print patterns.

Some examples with code:-

  1. Pyramid Printing :

To print star pyramid patterns in python, you have to use two or more than two for loops. In program that prints pattern contains two for loops, the first loop is responsible for rows and the second for loop is responsible for columns.

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

Here is the sample run of the above python program to illustrates how to print the pattern using stars:

Output :

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

2. Number Pyramid :

Number pyramid pattern is made by digits like (0,1,2,3…)

n = int(input("Enter a number of n: "))
for i in range(n):
print(" "*(n-i-1),end="")
for j in range(i+1):
print(i+1,"",end="")
print()

Output :

Alphabetical
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7

3. Alphabetical Pyramid :

It is an alphabetical pyramid and made by the use of alphabetical letters like (A, B, C…..).

Note:- Alphabetical letters can be in any form “Uppercase or Lowercase”

n = int(input("Enter a number of n: "))
for i in range(n):
print((chr(65+i)+" ")*(i+1))

Output :

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

4. Diamond Printing :

The diamond program is divided into two parts “first will be upper half part and second lower half” so that is known as Diamond pattern

n = int(input("Enter a number of n: "))
for i in range(n):
print(" "*(n-i-1),end="")
for j in range(i+1):
print("* ",end="")
print()
for i in range(n-1):
print(" "*(i+1),end="")
for j in range(n-i-1):
print("* ",end="")
print()

Output :

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

5. Hollow Pattern Printing :

The pattern is similar to the pyramid pattern.

n = int(input("Enter a number of n: "))
for i in range(n):
print(" "*(n-i-1)+"* ",end="")
if i>=1:
print(" "*(2*i-1)+"* ",end="")
print()

Output :

        *
* *
* *
* *
* *
* *

So we can understand all pattern programs. and Increase our Programming skills.

For More Patterns: https://github.com/hellosandeep1999/python/blob/master/ALL_Pattern.py

Thank you!

--

--

Sandeep Jain
Forsk Coding School

I am a college student pursuing B.tech(Computer Science Branch) from Jaipur Engineering College and Research Center. and learning Data science(Python, ML, AI).