Pattern Program in Python
Overview
One of the most commonly asked questions during an interview is to print patterns using Python. While this might sound tough, you need not worry! With the below module, we shall be exploring the beauty of loops in python by running them down with lots of illustrative examples.
Let's dive in!
Steps to Print Patterns in Python
Let us dive below to grasp how we can print patterns using Python.
-
First, we must decide how many rows and columns we need to incorporate. When a typical structure is designed to print any pattern, we need to know the number of rows and columns.
-
Second, we use loops for printing the pattern program in Python, where the number of rows represents the outer loop, and the number of columns represents the inner loop. Hence, to print out the pattern, we use two loops that are nested loops for printing the patterns in python.
-
Third, we give the user the ability to decide the pattern size by introducing the input() function to accept the number of rows for the same.
-
Iterate rows and columns: We use the for loop and range() function to write the outer loop based on the number of rows given by the user from previous steps.
-
Now, to analyze the inner loop, which depends on the value of the number of rows specified, we use a nested loop or inner loop to specify the number of columns that will go inside the outer loop.
-
Star| Number | Alphabet Printing: We can make a pattern in Python of any symbol that is stars (asterisk *), numbers or alphabets, etc. For each iteration of the for loop, we use the print() function in Python.
-
Lastly, after every iteration, we add a new line to display the pattern appropriately by using Add new line after each iteration of the outer loop print() function in Python.
Algorithm to understand the Print pattern program in Python -
1. Simple Pyramid Pattern
Listed below are the various simple pyramid pattern program in python using the loop:
First, let us create a Simple pyramid pattern using a star symbol to warm up.
Output:
Explanation:
In the above code, we have printed a simple pyramid pattern using the star symbol. We used int() to ask the user for the number of rows. Then we use for loop and range() twice to iterate for every single line until we reach the number of rows the user gave, that is, 7.
a. Reverse Right Angle Pyramid
Let us create a Reverse right angle pyramid pattern program in python using the star symbol.
Output:
Explanation:
In the above code, we have printed a Reverse right angle pyramid using star symbols. We used int() to ask the user for the number of rows. Then we use for loop and range() twice to iterate for every single line until we reach the number of rows given by the user, that is, 9 in this case. We made use of for defining the number of spaces. Here we printed the star in decreasing order for each line. Therefore, we see -1 as the steps used.
b. Printing Downward Half-Pyramid
Let us create a Printing Downward half - Pyramid pattern program in python using star symbols.
Output:
Explanation:
In the above code, we have printed a Printing Downward half - Pyramid using the star symbol. We used int() to ask the user for the number of rows. Then we use for loop and range() twice to iterate for every line until we reach the number of rows the user gave, that is, 5. We have used a nested reverse loop to bring the decreasing order of stars.
c. Printing Equilateral Triangle Pyramid
Let us create a Printing Equilateral Triangle Pyramid pattern program in python using the star symbol.
Output:
Explanation:
We have printed an Equilateral Triangle Pyramid in the above code using the star symbol. We used int() to ask the user for the number of rows. Then we use for loop and range() twice to iterate for every line until we reach the number of rows that is '5'. For printing the Equilateral Triangle pyramid, we need to be very particular about the number of spaces we are defining as it will define the symmetry of the triangle. We used formula for same.
d. Diamond Shaped Pyramid Pattern
Let us create a Diamond Shaped Pattern Pyramid using the star symbol.
Output:
Explanation:
We have printed a Diamond Shaped Pyramid Pattern in the above code using a star symbol. We used int() to ask the user for the number of rows. As studied above, we need to write a program for an equilateral triangle pyramid and then for a reverse right angle pyramid. We used the same logic in this as defined above.
e. Print two Pyramid in a Single Pattern
Let us create a Print of two pyramids in a single pattern program in python using the star symbol.
Output:
Explanation:
We have printed two pyramids in a single pattern with a star symbol in the above code. We used int() to ask the user for the number of rows. As studied above, we are simply printing two pyramids and using the logic of nested loops to draw out the figure as shown above in output and also keeping a check on the logic of spaces to be given.
g. Hourglass Pattern
Let us create an Hourglass Pattern program in python using the star symbol.
Output:
Explanation:
We have printed an Hourglass Pattern with a star symbol in the above code. We used int() to ask the user for the number of rows. As studied above, we are now just reversing the logic that we used for drawing the Diamond Shaped Pyramid Pattern.
2. Number Pattern in Python
So far, we are well versed with the Simple pyramid pattern using the star symbol, and now, let us take a dip into the Number pattern program in python, where we shall be learning about the different types of number patterns.
a. Number Pattern
Let us create a Number Pattern using numbers.
Output:
Explanation:
Here to draw a Number Pattern, we have represented each row with a number, i.e., the first row is number 1, the second row with number 2 is used twice, the third row with number 3 is used thrice, and so on till 8.
b. Half Pyramid Pattern With the Number
Let us create a Half pyramid pattern with the number.
Output:
Explanation:
Here to draw a Half pyramid pattern with the number, we have represented each row with a number, i.e., the first row is number 1, the second row with numbers 1 and 2, the third row with numbers 1, 2, and 3 and so on till 8. The inner loop helped elaborate the number of columns and iterate it to the row number as explained above.
c. Inverted Pyramid Pattern
Let us create an Inverted Pyramid Pattern using numbers.
Output:
Explanation:
As seen above, we are replicating the Number pattern in reversed order. Here we can see that as the number of rows given by the user was 8, so for the first row, the number 1 was replicated eight times, and moving further with every passing row, this number of repetitions decreased by 1. The number in decreasing value is printed.
d. Same Number Inverted Pyramid
Now we shall be creating the same number Inverted Pyramid as shown below.
Output:
Explanation:
Here we are printing the Same number Inverted Pyramid, and we have used the reverse loop logic to print the downward pattern. And to make all the numbers equal, we are not increasing the number in each iteration so that the number is same throughout.
e. Descending Order of Number
Now we shall be drawing the Descending Order of Numbers as shown below.
Output:
Explanation:
Here we are drawing the Descending Order of Number, for which we shall use the logic of the Inverted Pyramid Pattern in a reversed manner. We have assigned the value to 't' of 'i' after each iteration, as seen from the code. Each row is printing the same number, but the order of the number is decreasing.
f. Reverse Pattern from 10 to 1
We shall draw the Reverse Pattern from 5 to 1, as shown below.
Output:
Explanation:
Here we are printing the Reverse Pattern from 5 to 1 (depending upon what number of rows the user inputs). As seen from output the number of values decrementing in each each row uptill 1. With each row, the number decreases by one, and the row ends at the number 1.
g. Print Odd Numbers
Now we shall be drawing the Print Odd Numbers as shown below.
Output:
Explanation: Here we are printing the odd values only for each row. The user gives the number of rows as input. Then with each row, the odd number is repeated by following the formula (i * 2 - 1).
h. Square Pattern with Number
So far, we have created various types of triangle patterns. Now, we shall be drawing the Square Pattern with the number shown below.
Output:
Explanation:
Here we are printing Square Pattern with the number. For same, we need to understand the logic that if the value of 'j' of the inner loop is smaller or equal to 'i' then by printing 'i' we are creating a row of numbers that end on the input of rows given by the user. Here, we can also find the reverse L-shaped pattern getting created for a single number.
i. Multiplication Number in Column
Now we shall be drawing the Multiplication Number in the Column below.
Output:
Explanation: Here we are printing the Multiplication of row to the column for the number of rows specified by the user. We used the simple multiplication formula i*j to capitulate the above pattern shown in output with the help of using a nested for loop.
3. Alphabets and Letters Pattern in Python
So far, we have covered how by simply using the nested inner loop, we can draw various patterns like a pyramid, squares, etc. We also learned how we could draw a pattern using numbers as a character.
We shall dive deep into learning the Pattern in Python using Alphabets and Letters. We know that for each letter, Python has an ASCII value attached to it.
The big case letter from to is linked to ASCII values from to , respectively. The small case letters to are linked to ASCII values to , respectively. Any ASCII value outside the defined value results in special characters for to or a failure below and more than .
We shall use that concept to convert the letter into ASCII values. To do so, we need to follow the below-given steps:
- First, we must decide on the number of rows we will print.
- Second, we label the letter 'A' with its ASCII value as 65 and go on that way.
- Third, we use the nested for loop while iterating a loop and the chr function to replicate the ASCII number for its equivalent letter.
Let us dive below and understand with elaborative example and understand the concept:
a. Right-Angled Pattern With Characters
Let us learn how to draw Right-angled patterns with characters in python below.
Output:
Explanation:
Here we followed the abovementioned steps, and we started by assigning the ASCII_Value as 65, representing the character 'A'. Then we gave the number of rows to be printed as 6. With the nested for loop, we described the inner loop where we have converted the ASCII value to its respective character by chr() in python. Then after each passing iteration, it shall print the respective character linked to its ASCII value.
b. Right-angled Pattern with Same Character
Let us learn how to draw Right-angled Patterns with the Same Characters in python by below.
Output:
Explanation:
Here we started by asking for ASCII value input from the user. We must know that the ASCII values range for capital letters range between 65 to 90, and for small case letters, it ranges from 97 to 122, and any value outside the bracket 65 to 122 shall result in failure. The user in the above example entered the value as 76, which Letter ' L represents'. Value gets printed according to the inner nested for loop. As we did not change the ASCII value in the entire code, it prints the same character.
c. Display Letter of the Word in Pattern
Let us learn how to draw the Display letter of the word in Pattern in python below.
Output:
Explanation:
It is a straightforward and easy-to-understand pattern that we displayed as the word_pattern is - PythonisCool! We started by printing each letter in each iteration and eventually forming a pattern, as it seems. Here nested for loop was not required, and basic for loop did the job.
You, too, can try replacing the word pattern and customize this for you.
d. Equilateral Triangle Pattern With Characters
Let us learn how to draw an Equilateral triangle pattern with characters in python below.
Output:
Explanation: Here we are printing the equilateral triangle pattern with characters for which we define the number of rows as 6. The maximum number of rows that we can input is 6 as if we try to go for any row more than 6 it shall result in the addition of special characters. Then we use the nested loop to decrease the value of 't' after each iteration and then increase the value of 'ASCII_value'. This way, it draws out the equilateral pattern, which is pointed at the top and spread at its base.
Conclusion
-
The concept of a Simple pyramid pattern program in python depends on the appropriate use of nested loops, and the logic of giving spaces must be clear in each case.
-
The concept of drawing out the Number Pattern program in python uses the logic of nested for loop. This space needs to be kept between each number, and also, in each row, the count of the number must be equal to its current row number to maintain the pattern.
-
The concept of drawing out Alphabets and Letters Pattern program in Python uses the concept of each letter having an ASCII value. We use a nested for loop while iterating a loop and using the char function to replicate the ASCII number for its equivalent letter.