Printing patterns is a great way to practice and get a better understanding of loops in any programming language. In this post, I’ll show how to print some of these patterns.
General steps to print patterns in any programming language using loops
With disregard of the pattern you want to print, there are general steps that will help you to print the right pattern. One way to focus on the solution is to use rows and columns to organize what you need to print.
See the steps below:
1- Find out how many rows you must print
2- Find out how many columns you must print
3- How many elements you must print in each row?
4- Where do you need separators: at the beginning of the rows/columns? in between the elements you must print?
5- What calculations you must make. This is useful for patterns that are not simply printing stars or a certain character, but that involve calculations like the Pascal Triangle, Multiplication tables, etc.
Let’s see an example of a pattern and how these general steps are applied.
Pattern 1: Square of stars
Let’s follow the 4 steps mentioned before:
1- The rows can be entered using the keyboard, so we can use the same code to print squares of different sizes.
2- The number of columns is the same as the number of columns (it is a square).
3- The number of stars in each row is the same always (it is a square).
4- Separators are not necessary in this case, but we can use a blank space in between each star.
5- Calculations are not needed for this example.
See the code below.
if __name__=="__main__":
n = int(input('Enter the size: '))
for row in range (n): # number of rows
for col in range (n): # number of columns
print("*", end=' ') # a space at the end of each star
print() # a new line after each row
In Python, when the loop control variables are not used withing the loop, it is recommended to substitute them by an underscore. See below the final code.
if __name__=="__main__":
n = int(input('Enter the size: '))
for _ in range (n): # number of rows
for _ in range (n): # number of columns
print("*", end=' ') # a space at the end of each star
print() # a new line after each row
Pattern 2: Triangle of stars I
Again, we are going to follow the 4 steps mentioned before:
1- The number of rows can be entered using the keyboard, so we can use the same code to print triangles of different sizes.
2- The number of columns is the same as the number of columns.
3- The number of elements per row is one less than the previous row
4- Separators are not necessary in this case, but we can use a blank space in between each star.
5- Calculations are not needed for this example.
See the code below.
"""
* * * * *
* * * *
* * *
* *
*
"""
for row in range (n): # number of rows
for _ in range (n - row): # number of columns, each row will
# print one star less per row
print("*", end=' ') # a space at the end of each star
print() # a new line after each row
Pattern 3: Triangle of stars II
Let’s review the 4 steps mentioned before:
1- The number of rows can be entered using the keyboard, so we can use the same code to print triangles of different sizes.
2- The number of columns is the same as the number of columns.
3- The number of elements per row is one less than the previous row
4- In this case, we have to print two extra spaces at the beginning of each row to give the shape that we suppose to print. Separators are not necessary in this case, but we can use a blank space in between each star.
5- Calculations are not needed for this example.
See the python code below.
"""
* * * * *
* * * *
* * *
* *
*
"""
space=''
for row in range (n): # number of rows
for _ in range (n - row): # number of columns, each row will
# print one star less
print("*", end=' ') # a space at the end of each star
print() # a new line after each row
space = space + ' ' # add 2 extra spaces at the beginning
print(space, end='')
Pattern 4: Triangle of stars III
Let’s review the 4 steps mentioned before:
1- The number of rows can be entered using the keyboard, so we can use the same code to print triangles of different sizes.
2- The number of columns is the same as the number of columns.
3- The number of elements per row is one less than the previous row
4- In this case, we have to print n*2 extra spaces at the beginning of the first row. Then, at each row, we print two spaces less to give the shape that we suppose to print. Separators are not necessary in this case, but we can use a blank space in between each star.
5- Calculations are not needed for this example.
See the python code below.
"""
*
* *
* * *
* * * *
* * * * *
"""
print() # Print a new line
space = ' ' * (n*2) # Create the spaces that has to
# printed at the beginning of the first row
for row in range (1,n+1): # number of rows
print(space, end='')
for _ in range (row): # number of columns, each row will
# print one star less
print("*", end=' ') # a space at the end of each star
print() # a new line after each row
space = space[:-2] # remove two spaces
Pattern 5: Triangle of stars IV
Let’s review the 4 steps mentioned before:
1- The number of rows can be entered using the keyboard, so we can use the same code to print triangles of different sizes.
2- The number of columns is the same as the number of columns.
3- The number of elements per row is one less than the previous row
4- In this case, we have to print one extra space at the beginning of each row to give the shape that we suppose to print. Separators are not necessary in this case, but we can use a blank space in between each star.
5- Calculations are not needed for this example.
See the code below.
"""
* * * * *
* * * *
* * *
* *
*
"""
print() # Print a new line
space=''
for row in range (n): # number of rows
for _ in range (n - row): # number of columns, each row will
# print one star less
print("*", end=' ') # a space at the end of each star
print() # a new line after each row
space = space + ' ' # add an extra space at the beginning
print(space, end='')
Pattern 6: Triangle of stars V
Let’s review the 4 steps mentioned before:
1- The number of rows can be entered using the keyboard, so we can use the same code to print triangles of different sizes.
2- The number of columns is the same as the number of columns.
3- The number of elements per row is one less than the previous row
4- In this case, we must print n spaces at the beginning of the first row. Then we print 1 space less on each subsequent row to give the shape that we suppose to print. Separators are not necessary in this case, but we can use a blank space in between each star.
5- Calculations are not needed for this example.
Find the python code below.
"""
*
* *
* * *
* * * *
* * * * *
"""
print() # Print a new line
space = ' ' * n # Create n blank spaces for the first row
for row in range (1,n+1): # number of rows
print(space, end='')
for _ in range (row): # number of columns, each row will
# print one star less
print("*", end=' ') # a space at the end of each star
print() # a new line after each row
space = space[:-1] # remove one space
Pattern 7: Square numbers and subtractions
See the pattern we want to print in the figure below.
Notice that the numbers in the first row are the squares of the first five positive integers: 1, 2, 3, 4, 5. Then, in the second row, we subtract every pair of numbers from the previous row. 4-1=3, 9-4=5, 16-9=7 and 29-16=9. Then we keep adding rows doing the same operations until we get the last row with a 0.
Let’s examine the general steps from the previous section:
1- The number of rows will be the number of columns minus 1 (see the figure above). If at each iteration we print 1 less number, then in the row n-1 we print only one element.
2- The number of columns will be entered by the keyboard. So, we can use the same code to print several patterns of different sizes.
3- We print one number less in each row.
3- We need separators between the numbers, in this case, I will use 3 blank spaces (you can change this) and in front of each row, I’ll add 2 extra blank spaces, so it creates the figure of the triangle in the same way that shows the picture above.
4- In this case, the value we must print from the second row is the subtraction of the two corresponding values in the previous row.
See the code below.
if __name__=="__main__":
n = 6
space=''
numbers = [i ** 2 for i in range(1,n)] # create the first row
print(*numbers, sep = ' ')
for _ in range (n-2): # number of rows, notice that the
# first row was already printed in the previous line
space = space + ' ' # adding to blank extra spaces
# at the beginning of each row
print (space, end = '') # printing blank spaces
numbers_1 = [] # auxiliar list to store the calculations
for col in range (len(numbers)-1): #number of columns
numbers_1.append(numbers[col+1]-numbers[col]) # calculations
print(*numbers_1, sep = ' ')
numbers = numbers_1
I also created posts to explain other patterns that can be more interesting. You can find the links below in the related posts section.
Related posts:
- How to use command-line arguments in a Python console application
- Python tutorial: create a multiplication table
- The sequence 1 4 9 17 in python: a tricky pattern
- How to implement the Pascal Triangle in Python: practice with loops and strings
- How to extract digits from a number from right to left without using strings in Python