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
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
Code
n = 9
for n in range(1, n+1):
for m in range(1, n + 1):
print(m, end=’ ’‘)
print(“”)
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
Pattern #2 - Printing the Same Digit Pattern Using Inverted Pyramid
Pattern
7 7 7 7 7 7 7
7 7 7 7 7 7
7 7 7 7 7
7 7 7 7
7 7 7
7 7
7
Code
n = 7
digit = n
for k in range(n, 0, -1):
for m in range(0, k):
print(digit, end=' ')
print("\r")
Output
7 7 7 7 7 7 7
7 7 7 7 7 7
7 7 7 7 7
7 7 7 7
7 7 7
7 7
7
Pattern #3 - Printing Numbers Using Inverted Pyramids
Pyramid
1 1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3
4 4 4 4
5 5 5
6 6
7
Code
n = 7
m = 0
for k in range(n, 0, -1):
m += 1
for n in range(1, k + 1):
print(m, end=' ')
print('\r')
Output
1 1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3
4 4 4 4
5 5 5
6 6
7
Pattern #4 - Printing Numbers in a Simple Triangle Pattern
Pyramid
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
Code
n = 8
for digit in range(n):
for k in range(digit):
print(digit, end=" ")
print(" ")
Output
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
Pattern #5 - Printing Descending Numbers Using the Inverted Pyramid
Pyramid
7 7 7 7 7 7 7
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Code
n = 7
for k in range(n, 0, -1):
digit = k
for m in range(0, k):
print(digit, end=' ')
print("\r")
Output
7 7 7 7 7 7 7
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Pattern #6 - Printing Natural Numbers < 17 Using a Pyramid
Pattern
1
2 3 4
5 6 7 8 9
10 11 12 13 14 15 16
Code
curr = 1
end = 2
n = 4
for k in range(n):
for m in range(1, end):
print(curr, end=' ')
curr += 1
print("")
end += 2
Output
1
2 3 4
5 6 7 8 9
10 11 12 13 14 15 16
Pattern #7 - Printing Numbers in a Reverse Semi-pyramid
Pattern
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
Code
n = 8
for r in range(1, n):
for m in range(r, 0, -1):
print(m, end=' ')
print("")
Output
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
Pattern #8 - Printing Digits From 22 in a Reverse Pattern
Pattern
1
2
4 3
7 6 5
11 10 9 8
16 15 14 13 12
22 21 20 19 18 17
Code
beg = 1
end = 2
curr = end
for r in range(1, 8):
for c in range(beg, end):
curr -= 1
print(curr, end=' ')
print("")
beg = end
end += r
curr = end
Output
1
2
4 3
7 6 5
11 10 9 8
16 15 14 13 12
22 21 20 19 18 17
Pattern #9 - Printing a Number Pattern Using an Inverted Semi-pyramid
Pattern
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
Code
n = 8
for k in range(n, 0, -1):
for m in range(0, k + 1):
print(m, end=' ')
print("\r")
Output
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
Pattern #10 - Printing a Number Pyramid in a Connected Pyramid
Pattern
1234567
234567
34567
4567
567
67
7
Code
n = 8
for k in range(0, n):
for m in range(n-1, k-1):
print(m, end="")
for j in range(k):
print(' ', end="")
for h in range(k + 1, n):
print(h, end="")
print('\n')
Output
1234567
234567
34567
4567
567
67
7
Pattern #11 - Printing a Horizontal Table Using a Pyramid
Pattern
0
0 1
0 2 4
0 3 6 9
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
0 7 14 21 28 35 42 49
0 8 16 24 32 40 48 56 64
0 9 18 27 36 45 54 63 72 81
Code
n = 10
for k in range(0, n):
for m in range(0, k + 1):
print(k * m, end=' ')
print()
Output
0
0 1
0 2 4
0 3 6 9
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
0 7 14 21 28 35 42 49
0 8 16 24 32 40 48 56 64
0 9 18 27 36 45 54 63 72 81
Pattern #12 - Printing a Right-Angled Triangle of Number Pyramid by Mirroring
Pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
Code
n = 8
for r in range(1, n):
digit = 1
for m in range(n, 0, -1):
if m > r:
print(" ", end=' ')
else:
print(digit, end=' ')
digit += 1
print("")
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
Pattern #13 - Printing a Pattern of Unique Digits Using a Pyramid
Pattern
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
Code
n = 8
for k in range(1, n + 1):
for m in range(1, k-1):
print(m, end=" ")
for m in range(k-1, 0, -1):
print(m, end=" ")
print()
Output
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1
Pattern #14 - Printing a Pyramid Pattern With Even Numbers
Pattern
16
16 14
16 14 12
16 14 12 10
16 14 12 10 8
16 14 12 10 8 6
16 14 12 10 8 6 4
16 14 12 10 8 6 4 2
Code
n = 8
last_even = 2 * n
even_num = last_even
for k in range(1, n+1):
even_num = last_even
for m in range(k):
print(even_num, end=' ')
even_num -= 2
print("\r")
Output
16
16 14
16 14 12
16 14 12 10
16 14 12 10 8
16 14 12 10 8 6
16 14 12 10 8 6 4
16 14 12 10 8 6 4 2
Pattern #15 - Printing a Number Pyramid of Alternate Numbers
Pattern
1
3 3
5 5 5
7 7 7 7
9 9 9 9 9
11 11 11 11 11 11
13 13 13 13 13 13 13
Code
n = 7
k = 1
while k <= n:
m = 1
while m <= k:
print((k*2-1), end=" ")
m = m + 1
k = k + 1
print()
Output
1
3 3
5 5 5
7 7 7 7
9 9 9 9 9
11 11 11 11 11 11
13 13 13 13 13 13 13
Pattern #16 - Printing a Star Pyramid Pattern
Pattern
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
Code
n = 8
for k in range(0, n):
for m in range(0, k + 1):
print("*", end=' ')
print("\r")
Output
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
Pattern #17 - Printing a Star Equilateral Triangle
Pattern
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
Code
s = 9
y = (2 * s)-2
for k in range(0, s):
for m in range(0, y):
print(end=" ")
y = y-1
for m in range(0, k + 1):
print("* ", end=' ')
print(" ")
Output
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
Pattern #18 - Printing a Star Pattern Triangle That Goes Downwards
Pattern
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Code
n = 7
s = 2 * n-2
for k in range(n, -1, -1):
for m in range(s, 0, -1):
print(end=" ")
s = s + 1
for m in range(0, k + 1):
print("*", end=" ")
print("")
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:
- Behavioral Patterns
- Creational Patterns
- Structural Patterns
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.