- Pattern Program in Python
- Main Features of Python
- 18 Examples of Pattern Programs in Python
- Pattern #1 — Number Pattern Semi-Pyramid
- Pattern
- Code
- Output
- Pattern #2 — Printing the Same Digit Pattern Using Inverted Pyramid
- Pattern
- Code
- Output
- Become a Data Science Expert & Get Your Dream Job
- Pattern #3 — Printing Numbers Using Inverted Pyramids
- Pyramid
- Code
- Output
- Pattern #4 — Printing Numbers in a Simple Triangle Pattern
- Pyramid
- Code
- Output
- Pattern #5 — Printing Descending Numbers Using the Inverted Pyramid
- Pyramid
- Code
- Output
- Your Data Science Career Starts Today!
- Pattern #6 — Printing Natural Numbers < 17 Using a Pyramid
- Pattern
- Code
- Output
- Pattern #7 — Printing Numbers in a Reverse Semi-pyramid
- Pattern
- Code
- Output
- Pattern #8 — Printing Digits From 22 in a Reverse Pattern
- Pattern
- Code
- Output
- Become a Data Science Expert & Get Your Dream Job
- Pattern #9 — Printing a Number Pattern Using an Inverted Semi-pyramid
- Pattern
- Code
- Output
- Pattern #10 — Printing a Number Pyramid in a Connected Pyramid
- Pattern
- Code
- Output
- Pattern #11 — Printing a Horizontal Table Using a Pyramid
- Pattern
- Code
- Output
- Learn Everything You Need to Know About Data!
- Pattern #12 — Printing a Right-Angled Triangle of Number Pyramid by Mirroring
- Pattern
- Code
- Output
- Pattern #13 — Printing a Pattern of Unique Digits Using a Pyramid
- Pattern
- Code
- Output
- Pattern #14 — Printing a Pyramid Pattern With Even Numbers
- Pattern
- Code
- Output
- Become a Data Scientist With Real-World Experience
- Pattern #15 — Printing a Number Pyramid of Alternate Numbers
- Pattern
- Code
- Output
- Pattern #16 — Printing a Star Pyramid Pattern
- Pattern
- Code
- Output
- Pattern #17 — Printing a Star Equilateral Triangle
- Pattern
- Code
- Output
- Become a Data Scientist With Real-World Experience
- Pattern #18 — Printing a Star Pattern Triangle That Goes Downwards
- Pattern
- Code
- Output
- Learn More About Python
- How Can You Print a Pattern in Python?
- What Are Python Functions?
- What Are the Different Types of Design Patterns Being Used in Python?
- Learn Python and Data Science In-Depth
- About the Author
- Recommended Programs
- Pattern programs in python
- Left triangle pattern
- Source code
- Right triangle pattern
- Source code
- Pyramid shape pattern
- Source code
- Inverted Pyramid shape pattern
- Source code
- Diamond shape pattern
- Source code
- Pascals shape pattern
- Source code
- Hourglass pattern
- Source code
- butterfly pattern
- Source code
Pattern Program in Python
Python is a scripting language that is highly readable, interactive, high-level, object-oriented, and interpreted. Python has lesser syntactic structures than other programming languages, and it typically uses English terms instead of punctuation.
It is a popular language that is used in Data Science.
Main Features of Python
- Beginner-friendly Language — Python is easy to learn, maintain, implement and read. It is interactive in nature.
- Object-oriented — Python encapsulates code within objects by supporting the Object-Oriented programming approach or style or approach.
- Industry-oriented — Python is extendable, portable, scalable, cross-platform friendly with a standard library, and has support for GUI applications and interactive mode.
18 Examples of Pattern Programs in Python
Pattern #1 — Number Pattern Semi-Pyramid
Pattern
Code
Output
Pattern #2 — Printing the Same Digit Pattern Using Inverted Pyramid
Pattern
Code
Output
Become a Data Science Expert & Get Your Dream Job
Pattern #3 — Printing Numbers Using Inverted Pyramids
Pyramid
Code
Output
Pattern #4 — Printing Numbers in a Simple Triangle Pattern
Pyramid
Code
Output
Pattern #5 — Printing Descending Numbers Using the Inverted Pyramid
Pyramid
Code
Output
Your Data Science Career Starts Today!
Pattern #6 — Printing Natural Numbers < 17 Using a Pyramid
Pattern
Code
Output
Pattern #7 — Printing Numbers in a Reverse Semi-pyramid
Pattern
Code
Output
Pattern #8 — Printing Digits From 22 in a Reverse Pattern
Pattern
Code
Output
Become a Data Science Expert & Get Your Dream Job
Pattern #9 — Printing a Number Pattern Using an Inverted Semi-pyramid
Pattern
Code
Output
Pattern #10 — Printing a Number Pyramid in a Connected Pyramid
Pattern
Code
Output
Pattern #11 — Printing a Horizontal Table Using a Pyramid
Pattern
0 9 18 27 36 45 54 63 72 81
Code
Output
0 9 18 27 36 45 54 63 72 81
Learn Everything You Need to Know About Data!
Pattern #12 — Printing a Right-Angled Triangle of Number Pyramid by Mirroring
Pattern
Code
Output
Pattern #13 — Printing a Pattern of Unique Digits Using a Pyramid
Pattern
1 2 3 4 5 6 7 6 5 4 3 2 1
Code
Output
1 2 3 4 5 6 7 6 5 4 3 2 1
Pattern #14 — Printing a Pyramid Pattern With Even Numbers
Pattern
Code
Output
Become a Data Scientist With Real-World Experience
Pattern #15 — Printing a Number Pyramid of Alternate Numbers
Pattern
Code
Output
Pattern #16 — Printing a Star Pyramid Pattern
Pattern
Code
Output
Pattern #17 — Printing a Star Equilateral Triangle
Pattern
Code
Output
Become a Data Scientist With Real-World Experience
Pattern #18 — Printing a Star Pattern Triangle That Goes Downwards
Pattern
Code
Output
Learn More About Python
Now, let us discuss some further concepts in Python to understand the concepts of printing patterns in Python.
How Can You Print a Pattern in Python?
The most typical sort of programming question posed in interviews is printing given patterns with just a few tweaks. A pattern can be easily printed in Python using loop concepts in programming. Popular loops include the for and while loop but the for loop is mainly used to print a pattern in Python. We use multiple such loops to print a pattern in Python efficiently.
Main concepts for pattern printing in Python include —
- The outer loop is used to output the number of rows.
- The inner loop is used to output the number of columns.
- To print the whitespaces in the program, a variable is utilized in the appropriate position in Python.
What Are Python Functions?
A function is a reusable, ordered code block that may be used to repeat a single action several times. Functions make it simple to reuse code and to increase modularity, as well as to keep the application code up to date. Python allows users to write their own functions while also providing built-in functions such as ascii(), print(), etc.
Four types of Python functions —
- Function with no arguments but with a return value
- Function with both arguments and return value
- Function with no argument and no return value
- Function with an argument but no return value
What Are the Different Types of Design Patterns Being Used in Python?
In Python, patterns are utilized to emphasize code readability and execute various tasks through the use of significant indentation. With the help of design patterns, programmers may build clear and logical code for both small and large projects. These design patterns are used in many enterprise development software. By obtaining a thorough clarity of these design patterns, an application can be as basic as possible while simultaneously making the code more understandable.
With the help of these design patterns, programmers may build clear and logical code for both small and large projects. The three different design patterns in Python include:
Learn Python and Data Science In-Depth
The concept of pattern printing in Python includes understanding the concept of loops, and implementing programming logic.
To learn more about Python and to deep dive into its implementation in areas, like data science, one might need to refer to more in-depth concepts and resources. One such resource is offered by Simplilearn that will help you learn the A-Z of Python and its applications and implementation in data science. This Caltech Post Graduate Program in Data Science taught by Caltech faculty and IBM experts holds the first rank in data science courses, and will help you upskill yourself.
About the Author
Nikita Duggal
Nikita Duggal is a passionate digital marketer with a major in English language and literature, a word connoisseur who loves writing about raging technologies, digital marketing, and career conundrums.
Recommended Programs
Caltech Post Graduate Program in Data Science
Pattern programs in python
In this post, you will learn different pattern programs in python such as left triangle pattern, right triangle pattern, diamond shape pattern, pyramid shape pattern, pascals shape pattern, inverted pyramid pattern, hourglass pattern, butterfly pattern, etc.
But before writing a program you should know about:
So let us start writing python programs for all the patterns one by one.
Left triangle pattern
Source code
# left triangle pattern length = int(input('Enter length of the pattern: ')) for i in range(length): for j in range(i+1): print('*',end='') print() # for new line
In the above program, outer loop acts like rows, and the inner loop acts like a column.
NOTE: Most pattern programs are based on rows and columns. The first loop or outer loop is used for rows and the second or inner loop is used for the column.
Right triangle pattern
Source code
size = int(input('ENter length of the pattern: ')) for i in range(size): for j in range(2*(size-i)): # first loop for empty space print(' ',end='') for j in range(i+1): #second loop for printing * print('*',end=' ') print()
2*(size — i) formula helps you to print space in the right triangle pattern where i is the number of rows and size is the length of the pattern.
Pyramid shape pattern
Source code
size = int(input('Enter lenght of the pattern: ')) for i in range(size): for j in range(size,i+1,-1): print(' ',end='') for k in range(2*(i-1)+3): print('*',end='') print()
2*(i-1)+3 formula helps you to print * in the Pyramid shape pattern (you can also use the 2*(i-1)+1 formula to print * in the pyramid shape pattern).
Inverted Pyramid shape pattern
Source code
size = int(input('Enter lenght of the pattern: ')) for i in range(size-1): for j in range(i): print(' ',end='') for k in range((size-i)*2-3): print('*',end='') print()
(size-i)*2-3 formula helps you to print * in an Inverted Pyramid shape pattern where i is the number of rows.
Diamond shape pattern
Source code
size = int(input('Enter length of the pattern: ')) # upper pyramid for i in range(size): for j in range(size,i+1,-1): print(' ',end='') for k in range(2*(i-1)+1): print('*',end='') print() # lower pyramid for i in range(size-1): for j in range(i): print(' ',end='') for k in range((size-i)*2-3): print('*',end='') print()
You can observe that diamond shape pattern made up of 2 section the upper section is the pyramid pattern and lower section is inverted pyramid pattern.
Pascals shape pattern
Source code
rows = int(input('ENter number of rows: ')) #upper section for i in range(rows): for j in range(i,rows): print(' ',end='') for k in range(i+1): print('*',end='') print() #lower section for i in range(rows,0,-1): for j in range(i,rows+1): print(' ',end='') for k in range(i): print('*',end='') print()
You can observe that Pascal’s shape pattern is made up of 2 sections the upper section is a right triangle pattern and the lower section is an inverted right triangle pattern.
Hourglass pattern
Source code
my_rows = int(input('Enter number of rows: ')) # Upper section for i in range(my_rows, 0, -1): for j in range(my_rows-i): print(" ", end="") for k in range(1, 2*i): print("*", end="") print() #lower section for i in range(2, my_rows+1): for j in range(my_rows-i): print(" ", end="") for k in range(1, 2*i): print("*", end="") print()
butterfly pattern
Source code
length = int(input('Enter number of rows: ')) # upper section for i in range(length): for j in range(i): print('*',end='') for k in range(2*(length-i)): print(' ',end='') for l in range(i): print('*',end='') print() # for new line # lower section for i in range(length): for j in range((length-1)-i+1): print('*',end='') for k in range(2*i): print(' ',end='') for l in range((length-1)-i+1): print('*',end='') print() #for new line
there are two sections of the butterfly pattern the first section contains the first N rows and the second section contains the last N-1 rows in the butterfly pattern.