Introduction to Patterns in Python
In the python language, we can create the patterns by using the For Loops. Here we can manipulate them for loops, and with that, we can print the statement in order to have a unique pattern such as stars, Numeric and Character pattern. We can use any of the python compilers available on the market to execute programs.
Types of Patterns and Examples in Python
Let us first try to create the most interesting ones, the star patterns. We can use two for loops; the outer for loop can be used to take care of a number of rows, while the inner for loop can be used to take care of the number of columns.
Type 1. Star pattern
Program to print pyramid patterns of stars.
Example #1
In this example, we will print a single star in the first row, 2 stars in the second row and continue doing this in a similar fashion until we reach row number five.
Code:
#Python Program for printing pyramid pattern using stars
for i in range(0, 5):
for j in range(0, i+1):
print("* ",end="")
print()
Output:
Example #2
Now let us try to rotate the above pyramid by 180 degrees so that we can get a different style for the star pattern. In this example, we have started printing stars in the same manner but from the right side or very last column from the left side or from the very first column from the right side or from the 0th row and 4th column or from the 1st row and 5th column.
Code:
#Python Program for printing pyramid pattern using stars
a = 8
for i in range(0, 5):
for j in range(0, a):
print(end=" ")
a = a - 2
for j in range(0, i+1):
print("* ", end="")
print()
Output:
Example #3
Now let us see how we can print a triangle using stars:
In this program, we will also be asking for the range, which will determine the height t which this triangle can be extended. Here we are running 3 for loops, out of which the first for loop is for looping of the column and the other 2 for loops (the sub loops) for looping of the row.
Code:
# Python Program to print a Triangle
# Ask the Range of the triangle
num = int(input("Enter the range: \t "))
# i loop for range(height) of the triangle
# first j loop for printing space ' '
# second j loop for printing stars '*'
for i in range(num):
for j in range((num - i) - 1):
print(end=" ")
for j in range(i + 1):
print("*", end=" ")
print()
Output:
Example #4
Now let us see yet another program, after which we will wind up the star pattern illustration. There is a little difference between the above program and the second one, i.e. b), here we are trying to print a single star in the first line, then 3 stars in the second line, 5 in third and so on, so we are increasing the “l” count by 2 at the end of second for loop.
Code:
# Python Pyramid pattern using a star pattern
k = 16
l = 1
for i in range(0, 5):
for j in range(0, k):
print(end=" ")
k = k - 4
for j in range(0, l):
print("* ", end="")
l = l + 2
print()
Output:
Type 2. Numeric pattern
Now we will discuss some of the examples of the numeric pattern.
Example #1
We will now try to print a pyramid pattern of numbers. So it will go like this: In the first row, you will see the number 1, and in the second row, it will consist of numbers 2 and 3, the third row will consist of numbers 4, 5 and 6, and the series will continue like this.
Code:
# Python Numeric Pattern Example 1
k = 1
for i in range(0, 5):
for j in range(0, i+1):
print(k, end=" ")
k = k + 1
print()
Output:
Example #2
Let us now try to print the pyramid pattern on numbers in yet another style. Here we will try to print number 1 in the first row, number 1 and 2 in the second row, number 1, 2 and 3 in the third row, and it will continue like that.
Code:
# Python Numeric Pattern Example 2
for i in range(0, 5):
num = 1
for j in range(0, i+1):
print(num, end=" ")
num = num + 1
print()
Output:
Example #3
Here is yet another example of a python program to print numbers in the form of patterns. Here we are trying to end the row with the squares the row number by using two for loops and incrementing the numb variable’s value by 1 in the inner for loop and incrementing the value of variable inc by 2 for the outer for loop.
Code:
# Python Numeric Pattern Example 3
numb = 1
inc = 1
for i in range(0, 5):
for j in range(0, inc):
print(numb, end=" ")
numb = numb + 1
print()
inc = inc + 2
Output:
There are various other examples, and I will try to present them briefly over here.
Example #4
In this program, we will be printing the numbers that will be the same for a given row number, and the previous row will have one value less than the current row number. This result can be achieved by using outer and inner for loops and running the outer loop in range 10 (assigning to a value), and the inner loop runs inside the specified range of outer for loop.
Code:
# Python Numeric Pattern Example 4
for num in range(10):
for i in range(num):
print (num, end=" ") #printing the number
# We will use new line in order to display the pattern correctly
print("\n")
Output:
Example #5
In this example, we will try to see the number and hence print it column-wise. The first column will start with the specified range of values like (1,2,3,4 and 5), the second column will start from row number second, the third column will start from row number 3, and it will continue till the loop ends.
Code:
# Python Numeric Pattern Example 5
last_num = 6
for row in range(1, last_num):
for column in range(row, 0, -1):
print(column, end=' ')
print("")
Output:
Example #6
It is similar to the previous program, but here we are taking the squares of the numbers.
Code:
# Python Numeric Pattern Example 6
last_num = 9
for i in range(1, last_num):
for j in range(-1+i, -1, -1):
print(format(2**j, "4d"), end=' ')
print("")
Output:
Example #7
It is similar to the previous programs.
Code:
# Python Numeric Pattern Example 7
last_num = 9
for i in range(1, last_num):
for i in range(0, i, 1):
print(format(2**i, "4d"), end=' ')
for i in range(-1+i, -1, -1):
print(format(2**i, "4d"), end=' ')
print("")
Output:
Example #8
Here we will try to print the number in reverse order in a right angle triangle pattern.
Code:
# Python Numeric Pattern Example 8
stop = 2
start = 1
current_num = stop
for row in range(2, 6):
for col in range(start, stop):
current_num -= 1
print (current_num, end=' ')
print("")
start = stop
stop +=row
current_num = stop
Output:
Type 3. Character pattern
Now we will see some of examples of the character pattern.
Example #1
Here we will try to display the pyramid pattern of alphabets. The ASCII value of capital letter A starts at 65, which means the ASCII value for capital letter Z will be 90. In this example, we are converting the numeric value 65 into the capital letter A and hence iterating over the loop to increment the “value” variable.
Code:
# Python Character Pattern Example 1
value = 65
for i in range(0, 5):
for j in range(0, i+1):
ch = chr(value)
print(ch, end=" ")
value = value + 1
print()
Output:
Example #2
Here we will repeat the character the number of times the row number. Here is a simple illustration of how we can do that.
Code:
# Python Character Pattern Example 2
value = 65
for i in range(0, 5):
for j in range(0, i+1):
ch = chr(value)
print(ch, end=" ")
value = value + 1
print()
Output:
Example #3
This will be similar to example a) except where we are ending the row number with 2 more values than the previous row. To achieve this, we have included another variable called “inc”, and we are incrementing it by 2 in the outer for loop.
Code:
# Python Character Pattern Example 3
value = 65
inc = 1
for i in range(0, 5):
for j in range(0, inc):
ch = chr(value)
print(ch, end=" ")
value = value + 1
inc = inc + 2
print()
Output:
Example #4
This example is similar to a), but here we rotated the pattern by 180 degrees.
Code:
# Python Character Pattern Example 4
decrement = 8
counter = 64
value = 65
for i in range(0, 5):
for k in range(0, decrement):
print(end=" ")
for j in range(0, i+1):
counter = counter + 1
value = counter
temp = value
for j in range(0, i+1):
ch = chr(value)
print(ch, end=" ")
value = value - 1
value = temp
decrement = decrement - 2
print()
Output:
Now, as you have seen these many examples, let us try to draw some interesting patterns.
Example #5
In this program, we will try to print the square pattern using any value (It can be numeric, character, etc.) Here is how it looks.
Code:
# Python Program to Print Square Star Pattern
square_side = int(input("Please enter the square dimension : "))
print("Square Pattern")
for i in range(square_side):
for i in range(square_side):
print("$", end = ' ')
print()
Output:
Example #6
We have a very similar example, but we have replaced $ with * to come up with something like below.
Code:
# Python Program to Print Square Star Pattern
square_side = int(input("Please Enter dimension of square : "))
print("Star Square Pattern")
for i in range(square_side):
for i in range(square_side):
print('*', end = ' ')
print()
Output:
Conclusion
This article has seen multiple examples of printing patterns using python, which included the star pattern, numeric pattern, and character pattern. We have also given a brief explanation of what the program is created to do and the working behind it. Once these are understood and compiled in your own python environment, one should feel easy about pattern creation problems in python; our article has covered a wide range of examples.
Recommended Articles
We hope that this EDUCBA information on “Patterns in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.