When you first start learning to code, there are some basic challenges that you must overcome to get accustomed to the basics of programming. One such challenge is understanding looping and iterations. This concept is taught very early on during your programming journey, but it stays with you till the very advanced stages and is helpful in all programming projects you undertake.
The idea behind looping is to create easily understandable pieces of repeatable codes that work based on some predefined conditions. Loops ensure that you can run some functions several times, depending on your needs, without hard coding them multiple times. This idea is extremely useful in the programming world but can seem challenging to beginners.
Don’t worry, we’ll help you with that.
When it comes to iterations and loops, the best way to understand how they work is by going through various pattern printing programs and trying to dry-run how the code is producing the required article.
Get data science certification from the World’s top Universities. Learn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
So, in this article, we will walk you through ten important python design patterns you should know. You will have a comfortable grasp of the concept of loops in Python programming by the end of this article.
However, before we dive into the python design patterns, let’s first look at the basic steps involved in pattern printing in Python.
Basic steps for printing different patterns using Python
Here is a series of steps that you can take to print different patterns:
- Determine the number of rows and columns needed. This will help you decide how to program the outer and inner loops. The outer loop takes care of the number of rows, whereas the inner loop is all about the current column.
- Write an outer loop to iterate in the range of the number of rows. You can do this using for or while loops.
- Write an inner loop for handling the various columns of your pattern. The iteration of this loop is directly linked to the outer loop.
- Use the print() function to display whatever you need to display according to the pattern.
- Don’t forget to add a new line once every iteration of the outer loop completes, ensuring the next printing will occur in the following line instead of the same line itself.
Important pattern programs using Python
Pattern 1:
Here is the pattern that we need to print:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Here is the required Python program that will help you get the following pattern matching python on your screen:
rows_number = int(input(‘How many rows do you want in the pattern?”))
for i in range(rows_number):
for j in range(i):
print(i, end=’ ‘)
print(”)
In the above program, we will display a single digit on the first row, two digits in the second row, and so on. For this reason, we ran the outer loop in the range of the number of rows, whereas each iteration of the outer loop fixed the column values for inner rows.
Pattern 2:
Here is the second pattern that we need to learn how to print:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
As you can see, in this pattern, every second number is an increment by 1 of the last number in each row.
Here’s the required Python program for this:
rows_number = int(input(‘How many rows do you want in the pattern?”))
for i in range(1, rows_number + 1):
for j in range(1, i + 1):
print(j, end=’ ‘)
print(”)
Pattern 3:
So far, we have printed upright pyramids. Now, we see how inverted pyramids are printed using Python. For that, we will begin with the following pattern printing:
1 1 1 1 1
2 2 2 2
3 3 3
4 4
5
For this, we can use the following Python program:
number_rows = 5
b = 0
for i in range(number_rows, 0, -1):
b += 1for j in range(1, i + 1):
print(b, end=’ ‘)
print(‘\r’)
Pattern 4:
Now, we need to print the following pattern using Python programming language:
5 5 5 5 5
5 5 5 5
5 5 5
5 5
5
Here’s how it can be done:
number_rows = 5
n = number_rows
for i in range(number_rows, 0, -1):
for j in range(0, i):
print(n, end=’ ‘)
print(“\r”)
Pattern 5:
The next pattern that we want to print is:
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
For that, we can use the following Python program:
number_rows = 5
for i in range(number_rows, 0, -1):
for j in range(0, i + 1):
print(j, end=’ ‘)
print(“\r”)
Pattern 6:
This pattern focuses on printing odd numbers in a pyramid structure, as follows:
1
3 3
5 5 5
7 7 7 7
9 9 9 9 9
Here is how this pattern matching python can be achieved using a Python program:
number_rows = 5
itr= 1
while itr <= number_rows:
j = 1
while j <= itr:
print((itr * 2 – 1), end=” “)
j = j + 1
itr = itr + 1
print(”)
Pattern 7:
In the next pattern, we want to look at printing an equilateral triangle consisting of stars or any other special symbols, as follows:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
This is how it can be achieved using a Python program:
size = 7
m = (2 * size) – 2
for i in range(0, size):
for j in range(0, m):
print(end=” “)
# decrementing m after each loop
m = m – 1
for j in range(0, i + 1):
print(“* “, end=’ ‘)
print(” “)
Pattern 8:
This is about Pascal’s pattern and how we can print it using Python:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Here is a Python program that can get that done:
number_rows = 5
itr = 1
while itr <= number_rows:
j = itr
while j < number_rows:
# display space
print(‘ ‘, end=’ ‘)
j += 1
k = 1
while k <= itr:
print(‘*’, end=’ ‘)
k += 1
print()
itr += 1
itr = number_rows
while itr >= 1:
j = i
while j <= rows:
print(‘ ‘, end=’ ‘)
j += 1
k = 1
while k < itr:
print(‘*’, end=’ ‘)
k += 1
print(”)
itr -= 1
Pattern 9:
This pattern is also referred to as a sandglass pattern, and is essential to understand how nested loops work together!
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
Here is how we can print this pattern using the Python programming language:
number_rows = 5
itr = 0
while itr <= number_rows – 1:
j = 0
while j < itr:
# display space
print(”, end=’ ‘)
j += 1
k = itr
while k <= rows – 1:
print(‘*’, end=’ ‘)
k += 1
print()
itr += 1
i = rows – 1
while itr >= 0:
j = 0
while j < itr:
print(”, end=’ ‘)
j += 1
k = itr
while k <= number_rows – 1:
print(‘*’, end=’ ‘)
k += 1
print(”)
itr -= 1
Pattern 10:
This pattern is about printing horizontal tables of numbers. Here is how it looks:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
10 20 30 40 50 60 70 80 90 100
That can be done using the following Python program:
number_rows = 10
for i in range(1, number_rows + 1):
for j in range(1, i + 1):
print(i * j, end=’ ‘)
print()
In conclusion
Looping is a fundamental programming concept that every beginner should learn.
We hope this article, and the examples discussed, gives you a better understanding of how loops and nested loops work together to help with different pattern Python printing.
Remember, programming is all about practice. So, even if you don’t get it at once, please keep retrying and practicing. Eventually, it will start falling into place. At upGrad, we have mentored students worldwide from various educational backgrounds and helped them grow professionally. We understand the challenges students face and are always one step ahead when it comes to solving those challenges.
Our Professional Certification in Data Science, offered along with the University of Maryland, is designed for graduates looking to grow in Data Science. Check out our course and get yourself enrolled soon!